cuda随机数的生成

cuda随机数的生成书上和视频并没有讲 为了搞这个查阅了大量 英伟达的cudaAPI和百度了大量的

我简单来说是为了生成0-1之间的 随机数 在kernel函数里面

API 为

  1. __device__ void
  2. curand_init (
  3.     unsigned long long seed, unsigned long long sequence,
  4.     unsigned long long offset, curandState_t *state)

    原文解释如下

    The curand_init() function sets up an initial state allocated by the caller using the given seed, sequence number, and offset within the sequence. Different seeds are guaranteed to produce different starting states and different sequences. The same seed always produces the same state and the same sequence. The state set up will be the state after

    2

    67 ⋅ sequence + offset calls to curand() from the seed state.

    CUDA可以产生若干随机序列,每一个随机序列产生有两个属性:第一是产生其的种子,也就是seed,另一是随机数产生的偏移量,因为我们知道随机数产生都是一个迭代过程,CUDA提供了在初始化时候就迭代若干次的算法来解决种子过少的问题。
    传统而常见的种子提供做法是使用当前系统时间(微秒数),来作为种子给出,对于串行程序这简单而有效,但是显然对于并行程序,这并非明智的做法,因为我们在主机启动GPU内核的时候时间是一定的且只能用这个值作为种子,即使进行一些处理也未必可以达到要求,于是乎,CUDA提供了这样一个解决方案,对于非严谨的使用者反而更加使用。对于每一个随机序列给出state标志。对于生成的数转换为0-1之间的double数

    __shared__ double f[100];
     curandState state[100];
     int id=threadIdx.x;
      long seed=(30+id+1)*(id+1);
     curand_init(seed,id,0,&state[id]);
     f[id]=abs(curand_normal_double(&state[threadIdx.x]))/3;
     printf("%lf\n",f[id]); 除以三我也不知道为什么 因为不除以3 就会产生0-3之间的数 所以 除以3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值