gpt4 book ai didi

c++ - cuRAND 期望一个表达式

转载 作者:行者123 更新时间:2023-11-28 04:58:59 25 4
gpt4 key购买 nike

我正在为一个项目学习 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.cu 中(添加 -> 新项目 -> CUDA 9.0 -> 代码 -> CUDA C/C++ 文件)。

我将函数 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();

我现在可以从我的 miner.cpp 调用 cuRND()
点击开始,我就开始比赛了!
感谢您的帮助,我希望这个答案可以帮助以后的人!

关于c++ - cuRAND 期望一个表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com