我正在为一个项目学习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 ++的新手,所以我不知道发生了什么。
我希望这里有人可以帮助我吗?

干杯

最佳答案

我设法通过将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
快速开始,我就参加比赛了!
感谢您的帮助,我希望这个答案可以在以后对您有所帮助!

07-27 13:35