【问题标题】:How to initialize and run Mersenne Twister Random Generator inside kernels in PyCuda如何在 PyCuda 的内核中初始化和运行 Mersenne Twister Random Generator
【发布时间】:2020-02-03 17:52:37
【问题描述】:

我想在 pyCuda 内核中使用 Mersenne Twister 随机生成器进行数值实验。通过 Internet,我没有找到如何做到这一点的简单示例,因此,我尝试从 Cuda 文档和 pyCuda 示例(下面的 pyCuda 代码)构建一些东西。

如何正确完成?

谢谢。

code = """
    #include <curand_kernel.h>
    #include <curand_mtgp32_host.h>
    #include <curand_mtgp32dc_p_11213.h>

    const int nstates = %(NGENERATORS)s;

    __device__ curandStateMtgp32 *devMTGPStates[nstates];
    __device__ mtgp32_kernel_params *devKernelParams;

    curandMakeMTGP32Constants(mtgp32dc_params_fast_11213, devKernelParams);
    curandMakeMTGP32KernelState(devMTGPStates, mtgp32dc_params_fast_11213, devKernelParams, 64, %(seed)s);

    extern "C"
    {
        __global__ void generate_uniform(int N, int *result)
        {
            int tidx = threadIdx.x + blockIdx.x * blockDim.x;

            if (tidx < nstates) 
            {
                curandState_t s = *states[tidx];
                for(int i = tidx; i < N; i += blockDim.x * gridDim.x) 
                {
                    result[i] = curand_uniform(&s);
                }
                *states[tidx] = s;
            }
        }
    }
"""

seed = 0
N = 256 * 64
nvalues = int(10**3)
mod = SourceModule(code % { "NGENERATORS" : N, "seed": seed}, no_extern_c=True)
CompileError: nvcc compilation of C:\Users\limen\AppData\Local\Temp\tmpspxyn4h9\kernel.cu failed
[command: nvcc --cubin -arch sm_50 -m64 -Ic:\program files (x86)\microsoft visual studio\shared\anaconda3_64\lib\site-packages\pycuda\cuda kernel.cu]
[stdout:
kernel.cu
]
[stderr:
kernel.cu(10): error: this declaration has no storage class or type specifier

kernel.cu(10): error: declaration is incompatible with "curandStatus_t curandMakeMTGP32Constants(const mtgp32_params_fast_t *, mtgp32_kernel_params_t *)"
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin/../include\curand_mtgp32_host.h(367): here

kernel.cu(10): error: a value of type "mtgp32_params_fast_t *" cannot be used to initialize an entity of type "int"

kernel.cu(10): error: expected a ")"

kernel.cu(11): error: this declaration has no storage class or type specifier

kernel.cu(11): error: declaration is incompatible with "curandStatus_t curandMakeMTGP32KernelState(curandStateMtgp32_t *, mtgp32_params_fast_t *, mtgp32_kernel_params_t *, int, unsigned long long)"
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin/../include\curand_mtgp32_host.h(481): here

kernel.cu(11): error: a value of type "curandStateMtgp32 **" cannot be used to initialize an entity of type "int"

kernel.cu(11): error: expected a ")"

kernel.cu(21): error: identifier "states" is undefined

9 errors detected in the compilation of "C:/Users/limen/AppData/Local/Temp/tmpxft_00001aa8_00000000-10_kernel.cpp1.ii".
]

【问题讨论】:

    标签: python pycuda mersenne-twister curand


    【解决方案1】:

    我做了以下事情:

    import pycuda.curandom
    print(dir(pycuda.curandom))
    

    这给出了 Python 模块 pycuda.curandom 中存在的所有属性。没有提到任何 Mersenne-Twister (MT) 或基于 MT 的伪随机数生成器。这表明 MT 没有在 PyCUDA 中实现。

    【讨论】:

    • yop,逼我转为纯cpp
    猜你喜欢
    • 2015-03-01
    • 2014-02-22
    • 2010-12-19
    • 1970-01-01
    • 2015-08-19
    • 2015-05-28
    • 2013-01-30
    • 2013-10-13
    • 2014-04-17
    相关资源
    最近更新 更多