cuRAND期待一个表达

时间:2017-10-02 12:30:29

标签: c++ cuda

我正在为一个项目学习C ++,而对于我的项目,我需要在GPU上生成一个随机数。
为此,我决定使用cuRAND 但是,我在这一行遇到了一个小问题:

random << <1, 1 >> >(time(NULL), gpu_x);

我在该行收到错误expected an expression

使用我从here得到的代码:

__global__ void random(unsigned int seed, int* result) {
    /* CUDA's random number library uses curandState_t to keep track of the seed value
    we will store a random state for every thread  */
    curandState_t state;

    /* we have to initialize the state */
    curand_init(seed, /* the seed controls the sequence of random values that are produced */
        0, /* the sequence number is only important with multiple cores */
        0, /* the offset is how much extra we advance in the sequence for each call, can be 0 */
        &state);

    /* curand works like rand - except that it takes a state as a parameter */
    *result = curand(&state) % MAX;
}

void Miner::GoMine() {
    int* gpu_x;
    cudaMalloc((void**)&gpu_x, sizeof(int));

    /* invoke the GPU to initialize all of the random states */
    random << <1, 1 >> >(time(NULL), gpu_x);

    /* copy the random number back */
    int x;
    cudaMemcpy(&x, gpu_x, sizeof(int), cudaMemcpyDeviceToHost);

    printf("Random number = %d.\n", x);

    /* free the memory we allocated */
    cudaFree(gpu_x);
}

由于我是C ++的新手,我无法弄清楚发生了什么 我希望有人能帮到我吗?

干杯

1 个答案:

答案 0 :(得分:1)

我设法通过将CUDA相关代码放在cuRAND.cuAdd -> New Item -> CUDA 9.0 -> Code -> CUDA C/C++ File)中来解决问题。

我将函数void Miner::GoMine()重命名为int cuRND()
我添加了一些额外的代码,所以我的整个cuRAND.cu文件现在看起来像这样:

// For the RNG using CUDA
#include <curand.h>
#include <curand_kernel.h>
#include <iomanip>
#include "sha256.h"

#ifndef __Kernel_CU__  
#define __Kernel_CU__  

#define MAX 100

__global__ void random(unsigned int seed, int* result) {
    /* CUDA's random number library uses curandState_t to keep track of the seed value
we will store a random state for every thread  */
curandState_t state;

    /* we have to initialize the state */
    curand_init(seed, /* the seed controls the sequence of random values that are produced */
    0, /* the sequence number is only important with multiple cores */
    0, /* the offset is how much extra we advance in the sequence for each call, can be 0 */
    &state);

    /* curand works like rand - except that it takes a state as a parameter */
    *result = curand(&state) % MAX;
}


extern "C"

int cuRND() {
    int* gpu_x;
    cudaMalloc((void**)&gpu_x, sizeof(int));

    /* invoke the GPU to initialize all of the random states */
    random <<< 1, 1 >> >(time(NULL), gpu_x);

    /* copy the random number back */
    int x;
    cudaMemcpy(&x, gpu_x, sizeof(int), cudaMemcpyDeviceToHost);

    /* free the memory we allocated */
    cudaFree(gpu_x);

        return floor(99999999 * x);
}
#endif  

然后我继续将此代码添加到我的miner.cpp(这是我需要的文件):

extern "C"   
int cuRND();

我现在可以从cuRND()打电话给miner.cpp 一开始,我就参加了比赛! 感谢您的帮助,我希望这个答案可以帮助后来的人!