2013-08-29 87 views
2

我试图在cuda内核中生成随机数随机数。我希望从均匀分布和整数形式生成随机数,从1开始到8,随机数对于每个线程都是不同的。随机数可以生成的范围也会因线程而异。一个线程中的最大范围可能低至2,或者在另一个线程中它可以高达8,但不会高于此范围。所以,我提供以下的我想如何产生的数字为例:在不同范围内在Cuda内核中生成随机数

In thread#1 --> maximum of the range is 2 and so the random number should be between 1 and 2 
In thread#2 --> maximum of the range is 6 and so the random number should be between 1 and 6 
In thread#3 --> maximum of the range is 5 and so the random number should be between 1 and 5 

等等...

任何帮助将是非常赞赏。谢谢。

回答

9

编辑:我编辑了我的答案,以解决其他答案(@tudorturcu)和评论中指出的一些缺陷。

  1. 使用CURAND以产生uniform distribution 0.0 1.0之间和
  2. 然后,通过在所需的范围乘以这个(最大值 - 最小 值+ 0.999999)。
  3. 然后添加偏移量(+最小值)。
  4. 然后截断为一个整数。

像这样的事情在你的设备代码:

int idx = threadIdx.x+blockDim.x*blockIdx.x; 
// assume have already set up curand and generated state for each thread... 
// assume ranges vary by thread index 
float myrandf = curand_uniform(&(my_curandstate[idx])); 
myrandf *= (max_rand_int[idx] - min_rand_int[idx] + 0.999999); 
myrandf += min_rand_int[idx]; 
int myrand = (int)truncf(myrandf); 

您应该:

#include <math.h> 

truncf

这里是一个完全样例:

$ cat t527.cu 
#include <stdio.h> 
#include <curand.h> 
#include <curand_kernel.h> 
#include <math.h> 
#include <assert.h> 
#define MIN 2 
#define MAX 7 
#define ITER 10000000 

__global__ void setup_kernel(curandState *state){ 

    int idx = threadIdx.x+blockDim.x*blockIdx.x; 
    curand_init(1234, idx, 0, &state[idx]); 
} 

__global__ void generate_kernel(curandState *my_curandstate, const unsigned int n, const unsigned *max_rand_int, const unsigned *min_rand_int, unsigned int *result){ 

    int idx = threadIdx.x + blockDim.x*blockIdx.x; 

    int count = 0; 
    while (count < n){ 
    float myrandf = curand_uniform(my_curandstate+idx); 
    myrandf *= (max_rand_int[idx] - min_rand_int[idx]+0.999999); 
    myrandf += min_rand_int[idx]; 
    int myrand = (int)truncf(myrandf); 

    assert(myrand <= max_rand_int[idx]); 
    assert(myrand >= min_rand_int[idx]); 
    result[myrand-min_rand_int[idx]]++; 
    count++;} 
} 

int main(){ 

    curandState *d_state; 
    cudaMalloc(&d_state, sizeof(curandState)); 
    unsigned *d_result, *h_result; 
    unsigned *d_max_rand_int, *h_max_rand_int, *d_min_rand_int, *h_min_rand_int; 
    cudaMalloc(&d_result, (MAX-MIN+1) * sizeof(unsigned)); 
    h_result = (unsigned *)malloc((MAX-MIN+1)*sizeof(unsigned)); 
    cudaMalloc(&d_max_rand_int, sizeof(unsigned)); 
    h_max_rand_int = (unsigned *)malloc(sizeof(unsigned)); 
    cudaMalloc(&d_min_rand_int, sizeof(unsigned)); 
    h_min_rand_int = (unsigned *)malloc(sizeof(unsigned)); 
    cudaMemset(d_result, 0, (MAX-MIN+1)*sizeof(unsigned)); 
    setup_kernel<<<1,1>>>(d_state); 

    *h_max_rand_int = MAX; 
    *h_min_rand_int = MIN; 
    cudaMemcpy(d_max_rand_int, h_max_rand_int, sizeof(unsigned), cudaMemcpyHostToDevice); 
    cudaMemcpy(d_min_rand_int, h_min_rand_int, sizeof(unsigned), cudaMemcpyHostToDevice); 
    generate_kernel<<<1,1>>>(d_state, ITER, d_max_rand_int, d_min_rand_int, d_result); 
    cudaMemcpy(h_result, d_result, (MAX-MIN+1) * sizeof(unsigned), cudaMemcpyDeviceToHost); 
    printf("Bin: Count: \n"); 
    for (int i = MIN; i <= MAX; i++) 
    printf("%d %d\n", i, h_result[i-MIN]); 

    return 0; 
} 


$ nvcc -arch=sm_20 -o t527 t527.cu -lcurand 
$ cuda-memcheck ./t527 
========= CUDA-MEMCHECK 
Bin: Count: 
2 1665496 
3 1668130 
4 1667644 
5 1667435 
6 1665026 
7 1666269 
========= ERROR SUMMARY: 0 errors 
$ 
+0

我可能做了这样的事情。你可以把它写成代码,以便我可以比较两者。再次感谢。 – duttasankha

3

@罗伯特的例子并不会完全生成均匀分布(尽管范围内的所有数字都会生成,并且所有生成的数字都在该范围内)。最小值和最大值都是选择范围内其余数字的概率为0.5。

在第2步,您应该乘以范围内的值的数量:(最大值 - 最小值+ 0.999999)。 *

在步骤3,偏移应该是(+最小值)而不是(+最小值+0.5)。

步骤1和4保持不变。

*由于@Kamil Czerski指出,1.0版本包含在发行版中。添加1.0而不是0.99999有时会导致数字超出所需的范围。

+0

请注意,curand_uniform中的[是] [包含](http://docs.nvidia.com/cuda/curand/device-api-overview.html#distributions)。有一个很小的机会,你画正好1。0和乘以(largest_value - smallest_value + 1)增加(smallest_value)并舍入为零您越界。[Here](http://stackoverflow.com/questions/24537112/uniformly-distributed-pseudorandom-integers- inside-cuda-kernel/24537113#24537113)是我生成统一整数的版本,但其基本思想与Robert的Crovella相同。我使用0.999999而不是1,并且完全像你提议的那样。 –

+0

谢谢你注意到这个错误。我发现在分配中包含1.0的决定并排除了0.0很奇怪。我将修改我的答案以包含您的更改。您可以修改它以包含您的代码示例。 – tudorturcu