curand_init(unsigned long long seed,unsigned long long subsequence, unsigned long long offset,curandStateMRG32k3a_t * state)
中 subsequece 和 offset 这两个参数要怎么理解啊
seed - Arbitrary bits to use as a seed
subsequence - Subsequence to start at
offset - Absolute offset into sequence
state - Pointer to state to initialize
简单看了下源码,还是不太理解。另外curand的返回值是随机的出现在0-0xffff之间吗
offset是去掉已经生成的随机数列前面若干个数据,这样可以反复用同一个生成的函数,每次生成不同的随机数。
curand 手册原文为:
“Offset
The offset parameter is used to skip ahead in the sequence. If offset = 100, the first random number generated will be the 100th in the sequence. This allows multiple runs of the same program to continue generating results from the same sequence without overlap. Note that the skip ahead function is not available for the CURAND_RNG_PSEUDO_MTGP32 generator.”
subsequence 字面意思是子数列的意思,我去查下手册,等下修改。
顺便说一下,数学里面无穷数列的子列也是无穷的,不过在这里,函数提供的伪随机数是周期长度超过2^190的数列,而subsequence似乎是里面截取出来的一段,长度大概是2^60~2^70的样子。部分和subsequence相关的参数是让使用者指定跳过多少个subsequence。
手册原文中的示例代码:
__global__ void setup_kernel(curandStateMRG32k3a *state)
{
int id = threadIdx.x + blockIdx.x * 64;
/* Each thread gets same seed, a different sequence
number, no offset */
curand_init(0, id, 0, &state[id]);
}
结合上面浏览文中得出的”subsequence”指的是原随机数列中很长的一段连续的部分,可以看作是分段。
所以这里初始化时指定subsequence/sequence数值就是每个线程使用很长的一个子段里面的随机数。
最后,curand library提供的产生伪随机数的函数,返回值有无符号整数的,64bit的无符号整数的,也有浮点数的(包括单精度,双精度等)。具体情况请参考curand library 手册。
顺便说一下,以上内容参考自CUDA 5.0的curand library的手册,似乎写的还比较清楚,可在如下链接看到:
http://docs.nvidia.com/cuda/curand/index.html
The state set up will be the state after
2^67 * sequence + offset calls to curand() from the seed state。
如果sequence=1,offset=0,那就是说迭代了2^67次,得到了当前的随机数。有点耗时的样子啊。
手册中有关于performance的建议,可供参考。
没有深究伪随机数产生算法,不清楚是必须顺序产生还是可以直接产生某个特定位置的随机数,以及尚不清楚该产生方法是串行的还是可以并行的。
简单一点,你可以设法测试一下,看看在跳过sequence数量不为0的时候,随机数生成的开销如何。