小编Sad*_*dam的帖子

在 pyCuda 内核中生成单个随机数

我见过很多生成随机数数组的方法。但我想生成一个随机数。C++ 中有没有像 rand() 这样的函数?我不需要一系列随机数。我只需要在内核内生成一个随机数。有没有内置函数可以生成随机数?我已经尝试过下面给出的代码,但它不起作用。

import numpy as np
import pycuda.autoinit
from pycuda.compiler import SourceModule
from pycuda import gpuarray

code = """
    #include <curand_kernel.h>
       __device__ float getRand()
       {
          curandState_t s;
          curand_init(clock64(), 123456, 0, &s);
          return curand_uniform(&s);
       }

        __global__ void myRand(float *values)
        {
          values[0] = getRand();
        }
"""


mod = SourceModule(code)
myRand = mod.get_function("myRand")
gdata = gpuarray.zeros(2, dtype=np.float32)
myRand(gdata, block=(1,1,1), grid=(1,1,1))
print(gdata)
Run Code Online (Sandbox Code Playgroud)

错误类似于:

/usr/local/cuda/bin/../targets/x86_64-linux/include/curand_poisson.h(548): error: this declaration may not have extern "C" linkage

/usr/local/cuda/bin/../targets/x86_64-linux/include/curand_discrete2.h(69): error: this declaration may not have extern "C" …
Run Code Online (Sandbox Code Playgroud)

python cuda pycuda random-seed

2
推荐指数
1
解决办法
281
查看次数

标签 统计

cuda ×1

pycuda ×1

python ×1

random-seed ×1