随机发生器& CUDA [英] Random generator & CUDA

查看:186
本文介绍了随机发生器& CUDA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于CUDA中的随机生成器的问题。我使用Curand用以下代码生成随机数:

I have a question regarding the random generators in CUDA . I am using Curand to generate random numbers with the following code:

__device__ float priceValue(int threadid){

    unsigned int seed = threadid ;
    curandState s;
    curand_init (seed , 0, 0, &s);

    float randomWalk = 2;
    while (abs(randomWalk)> 1) {
        randomWalk = curand_normal(&s);
    }
    return randomWalk; 
}



我尝试重新启动此代码多次,我总是相同的输出。我找不到这里代码有什么问题。线程给出相同的ID,但curand_normal函数应该在每次启动时更改,对吧?

I have tried to relaunch this code many times, I have always the same output. I could not find what’s wrong in this code. The threads give the same Ids but the curand_normal functions should change at each launching, right?

推荐答案

每次请求一个随机值时,您都在运行init。相反,您应该在代码开头的单独内核中运行 curand_init()一次。然后当你想要一个新的随机值,只需调用 curand_normal()

You're running init each time you ask for a random value. Instead you should run curand_init() once, in a separate kernel at the start of your code. Then when you want a new random value, just call curand_normal(). Then the values will change each time you call your device function.

有关示例,请参阅我的回答此处

For an example see my answer here.

如果要使用时间作为种子而不是线程ID,通过由<$返回的值 c $ c> clock()或任何你最喜欢的时间函数:

If you want to use time as a seed instead of thread ID, then pass the value returned by clock() or whatever is your favorite time function:

unsigned int seed = (unsigned int) clock64();

这篇关于随机发生器&amp; CUDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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