Я попытался написать программу, которая вычисляет число пи для произвольно сгенерированных чисел N.
__global__ void kernel(int* count_d, float* randomnums, double N){
int i;
double x, y, z;
//Find the overall ID of the thread
int tid = blockDim.x*blockIdx.x+threadIdx.x;
i = tid;
int xidx=0;
int yidx=0;
//Start the MonteCarlo
xidx = i + i;
yidx = xidx +1;
//Get the random x,y points
x = randomnums[xidx];
y = randomnums[yidx];
z = ((x*x) + (y*y));
if (z<=1)
count_d[tid] = 1;
else
count_d[tid] = 0;
}
int main(){
double N = 100000;
float *randomnums;
double pi;
//Threads per thread block to be launched
int threads = 1024;
//Number of thread blocks launched
int blocks = 100;
int* count_d;
unsigned int reducedcount = 0;
for (int i=threads*blocks; i<=N; i+=threads*blocks){
//Allocate the array for the random numbers
cudaMalloc((void**)&randomnums,(i)*sizeof(float));
//Use CuRand to generate an array of random numbers on the device
int status;
curandGenerator_t gen;
status = curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_MRG32K3A);
status |= curandSetPseudoRandomGeneratorSeed(gen, 4294967296ULL^time(NULL));
status |= curandGenerateUniform(gen, randomnums, (i));
status |= curandDestroyGenerator(gen);
//Check to see if there was any problem launching the CURAND kernels and generating
//the random numbers on the device
if (status != CURAND_STATUS_SUCCESS){
printf("CuRand Failure\n");
exit(EXIT_FAILURE);
}
int *count = (int*)malloc(blocks*threads*sizeof(int));
unsigned int reducedcount = 0;
//Allocate the array to hold a value (1,0) whether the point in is the circle (1) or not (0)
cudaMalloc((void**)&count_d, (blocks*threads)*sizeof(int));
kernel <<<blocks, threads>>> (count_d, randomnums, i);
cudaDeviceSynchronize();
cudaMemcpy(count, count_d, blocks*threads*sizeof(int),cudaMemcpyDeviceToHost);
//Reduce array into int
for(int j = 0; j<N; j++){
reducedcount += count[j];
}
//Free the cudaMalloc()'d arrays
cudaFree(randomnums);
cudaFree(count_d);
free(count);
}
//Find the ratio
pi = ((double)reducedcount/N)*4.0;
printf("Pi: %f\n", pi);
return
0;
}
Мне пришла в голову идея разбить N на части с размером блока *, а затем обработать ядро.для каждой части.Оказывается, что когда я увеличиваю переменную N, я получаю ошибку сегментации: Segmentation fault (core dumped)
. Почему это происходит?Также я хотел бы спросить, верна ли моя идея с ланчем ядра в цикле.