2017-01-21 43 views
-1

当我打电话给curand时,我总是在一个线程中得到相同的数字。但是,它们对于每个线程都不相同。我在下一个代码中做错了什么?curand每次在一个线程中给出相同的数字

#define MAXTHREADS 2 
#define NBBLOCKS 2 


__global__ void testRand (curandState * state, int nb){ 
    int id = threadIdx.x + blockIdx.x * blockDim.x; 
    int value; 
    for (int i=0;i<nb;i++){ 
     curandState localState = state[id]; 
     value = curand(&localState); 
     printf("Id %i, value %i\n",id,value); 
    } 
} 
__global__ void setup_kernel (curandState * state, unsigned long seed) 
{ 
    int id = threadIdx.x + blockIdx.x * blockDim.x; 
    curand_init (seed, id , 0, &state[id]); 
} 

/** 
* Image comes in in horizontal lines 
*/ 
void findOptimum() { 
    const dim3 blockSize(MAXTHREADS); 
    const dim3 gridSize(NBBLOCKS); 

    curandState* devStates; 
    cudaMalloc (&devStates,MAXTHREADS*NBBLOCKS*sizeof(curandState)); 
    time_t t; 
    time(&t); 
    setup_kernel <<< gridSize, blockSize >>> (devStates, (unsigned long) t); 
    int nb = 4; 
    testRand <<< gridSize, blockSize >>> (devStates,nb); 
    testRand <<< gridSize, blockSize >>> (devStates,nb); 

    cudaFree(devStates); 
} 

它输出:

Id 0, value -1075808309 
Id 1, value -1660353324 
Id 2, value 1282291714 
Id 3, value -1892750252 
Id 0, value -1075808309 
Id 1, value -1660353324 
Id 2, value 1282291714 
Id 3, value -1892750252 
... 

这重复了几次。

+0

您从不修改全局内存生成器状态。你期望会发生什么? – talonmies

+0

好的,谢谢! 我认为它是自动完成的,但由于我没有通过任何参考全球国家,这是不可能的课程。 – Curantil

回答

1

正如talonmies指出的那样,我没有修改全局状态。

加上state[id] = localState后跟te curand(localState)修正了这个问题。

相关问题