CURAND使用相同的种子生成不同的随机数 [英] CURAND generating different random numbers with same seed

查看:498
本文介绍了CURAND使用相同的种子生成不同的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CURAND库在CUDA中编写随机生成代码.我所读到的有关随机生成的信息使我相信,如果使用相同的种子,我将获得相同的随机数集.但是当我进行测试时情况并非如此.请解释我在做什么错.我将以下代码粘贴以供参考:

I am writing random generation code in CUDA using the CURAND library. What I read about random generation made me believe that if I use the same seed, I will get the same set of random numbers. But its not the case when I tested it. Please explain what am I doing wrong. I am pasting the code below for reference:

    curandGenerator_t rand_gen;  
    status = curandCreateGenerator (&rand_gen ,CURAND_RNG_PSEUDO_DEFAULT );  
    if(status != CURAND_STATUS_SUCCESS){  
            printf("Error encountered in generating handle\n");  
    }  
    status = curandSetPseudoRandomGeneratorSeed (rand_gen ,1234ULL);  
    if(status != CURAND_STATUS_SUCCESS){  
            printf("Error encountered in setting seed\n");  
    }  

    for(j=0; j<2; j++){  
            status = curandGenerate(rand_gen,a_d,N);  
            if(status != CURAND_STATUS_SUCCESS){  
                    printf("Error encountered in generating random numbers\n");  
             }

            cudaMemcpy ( a_h , a_d , N * sizeof(unsigned int),cudaMemcpyDeviceToHost);  
            for(i = 0; i < N; i++){  
                    printf("%d : %u\n",i,a_h[i]);  
            }
            printf("-----------%d----------------------\n",j);  
    }  
    status = curandDestroyGenerator(rand_gen);  
    if(status != CURAND_STATUS_SUCCESS){  
            printf("Error encountered in destroying handle\n");  
    }  


输出:


Output:

0:624778773
1:3522650202
2:2363946744
3:1266286439
4:3928747533
5:3732235839
6:1382638835
7:3362343509
8:48542993
9:1225999208
----------- 0 ----------------------
0:3356973615
1:1004333919
2:2916556602
3:1213079917
4:2705410958
5:520650207
6:1860816870
7:1645310928
8:2205755199
9:1282999252
----------- 1 ----------------------

0 : 624778773
1 : 3522650202
2 : 2363946744
3 : 1266286439
4 : 3928747533
5 : 3732235839
6 : 1382638835
7 : 3362343509
8 : 48542993
9 : 1225999208
-----------0----------------------
0 : 3356973615
1 : 1004333919
2 : 2916556602
3 : 1213079917
4 : 2705410958
5 : 520650207
6 : 1860816870
7 : 1645310928
8 : 2205755199
9 : 1282999252
-----------1----------------------

推荐答案

存在伪​​随机生成器的状态"概念.例如,梅森捻线器的状态大小约为1024个字,而默认的XORWOW的状态大小仅为几个字(但周期也小得多).

there is a notion of "state" of a pseudo-random generator. For example, Mersenne twister has a state of size about 1024 words while the the default one XORWOW has a state size just a several words (but it also has much smaller period).

无论何时调用"setPseudoRandomGeneratorSeed",都将初始化生成器的状态.然后使用curandGenerate的后续调用将更新此状态(即从一个随机数转到下一个随机数,需要重新计算该状态),因此将生成随机序列的不同部分.

Whenever you call 'setPseudoRandomGeneratorSeed' you initialize the state of the generator. Then with subsequent calls to curandGenerate this state will be updated (i.e. to go from one random number to the next one, the state needs to be recomputed) and hence different parts of the random sequence will be generated.

您还可以尝试使用驱动程序API-在这里curandInit()初始化每个线程的状态,这可能会非常昂贵.然后对curand()或curandUniform()等的后续调用将重用此状态.实际上,每个线程都从随机序列的不同偏移量开始

You might also experiment with the driver API - here curandInit() initializes the state for each thread which can be quite expensive. Then the subsequent calls to curand() or curandUniform() etc. will reuse this state. In fact each thread starts from different offset of a random sequence

这篇关于CURAND使用相同的种子生成不同的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆