2017-01-21 38 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)修正了這個問題。

相關問題