简体   繁体   English

如何在 PyCuda 的内核中初始化和运行 Mersenne Twister Random Generator

[英]How to initialize and run Mersenne Twister Random Generator inside kernels in PyCuda

I wanted to use Mersenne Twister random generator inside pyCuda kernels for numerical experiment.我想在 pyCuda 内核中使用 Mersenne Twister 随机生成器进行数值实验。 Via Internet I found no simple examples of how to do it, so, I tried to construct something from Cuda documentation and pyCuda examples (pyCuda code below).通过 Internet,我没有找到如何做到这一点的简单示例,因此,我尝试从 Cuda 文档和 pyCuda 示例(下面的 pyCuda 代码)构建一些东西。

How it can be done correctly?如何正确完成?

Thank you.谢谢你。

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".
]

I did the following:我做了以下事情:

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

This gives all attributes that are present in the Python module pycuda.curandom .这给出了 Python 模块pycuda.curandom中存在的所有属性。 There was no mention of any Mersenne-Twister(MT) or MT-based Pseudo Random Number Generator.没有提到任何 Mersenne-Twister (MT) 或基于 MT 的伪随机数生成器。 This indicates that MT isn't implemented in PyCUDA.这表明 MT 没有在 PyCUDA 中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM