I’ve tried to make a random number generator (just a simple linear congruential ported from rand()), but it reports “unknown error” when initializing. It is probably a bad pointer or overflow error, and I’ve tried to fix that, but it still doesn’t work. Can anyone see what it is? Here’s the entire relevant code. I use values of _x, _y and _z as 256, 64 and 256. Thanks in advance.
__device__ unsigned long int* d_rand;
__global__ void _initRand(size_t _x, size_t _y, size_t _z,size_t slice)
{
int x = blockIdx.x*blockDim.x + threadIdx.x;
int y = blockIdx.y*blockDim.y + threadIdx.y;
if(x>=_x || y>=_y) return;
int sliceLoc = x+_x*y;
for(int z = 0; z < _z; z++)
{
unsigned long int i = sliceLoc+slice*z;
d_rand[i] = (i+1)*i*i;
}
}
void initRand(size_t _x, size_t _y, size_t _z)
{
cudaMalloc((void**)&d_rand, _x * _y * _z * sizeof(unsigned long int));
int size = 16;
dim3 blockSize(size,size);
dim3 gridSize((_x+size-1)/size,(_y+size-1)/size);
_initRand<<<gridSize,blockSize>>>(_x,_y,_z,_x*_y);
cudaThreadSynchronize();
}
__device__ float frand(int x, int y, int z, size_t _x, size_t _y, size_t _z)
{
int i = x+_x*y+_x*_y*z;
d_rand[i] = d_rand[i] * 1103515245 + 12345;
float rand = (float)((d_rand[i]/65536) % 32768)/32768.0f;
return rand;
}