我正在编写一个cuda程序,我需要生成一个随机变量,该变量将通过遵循正态分布生成。我希望随机变量的值被限制在0到8之间。所以我希望随机变量在内核函数中生成,然后随机变量结果将用于进一步使用。我打算将cuRAND库用于此目的。我一直在尝试使用curand_normal设备api来生成值但没有任何成功。如果有人可以为我提供内核功能代码,那将非常有帮助。谢谢你的帮助。
下面提供的代码是我在gpu中搜索的cpu实现:
#include "stdafx.h"
#include <iostream>
#include <random>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int nrolls=10000; // number of experiments
const int nstars=100; // maximum number of stars to distribute
int i;
default_random_engine generator;
normal_distribution<double> distribution(0.0,3);
for (i=0;i<=nstars;i++)
{ int number = distribution(generator);
printf("%d\n\n",number);
}
return 0;
}
我想补充一点,我不知道C ++,我只是按照我在其他网站上看到的其他代码编写了这个程序。感谢。
答案 0 :(得分:3)
这是this code的改编,它将产生一个近似“正常”分布的随机数集,可以采用大约0到8之间的离散值。我不理解评论中的请求范围为0到8,平均值为0.
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <curand_kernel.h>
#include <math.h>
#define SCALE 2.0
#define SHIFT 4.5
#define DISCRETE
#define BLOCKS 1024
#define THREADS 512
#define CUDA_CALL(x) do { if((x) != cudaSuccess) { \
printf("Error at %s:%d\n",__FILE__,__LINE__); \
return EXIT_FAILURE;}} while(0)
__global__ void setup_kernel(curandState *state)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
/* Each thread gets different seed, a different sequence
number, no offset */
curand_init(7+id, id, 0, &state[id]);
}
__global__ void generate_normal_kernel(curandState *state,
int *result)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
float x;
/* Copy state to local memory for efficiency */
curandState localState = state[id];
/* Generate pseudo-random uniforms */
for(int n = 0; n < 10; n++) {
x = (curand_normal(&localState) * SCALE)+SHIFT;
/* Discretize */
#if defined DISCRETE
x = truncf(x);
#endif
}
/* Copy state back to global memory */
state[id] = localState;
/* Store last generated result per thread */
result[id] = (int) x;
}
int main(int argc, char *argv[])
{
int i;
unsigned int total;
curandState *devStates;
int *devResults, *hostResults;
int device;
struct cudaDeviceProp properties;
CUDA_CALL(cudaGetDevice(&device));
CUDA_CALL(cudaGetDeviceProperties(&properties,device));
/* Allocate space for results on host */
hostResults = (int *)calloc(THREADS * BLOCKS, sizeof(int));
/* Allocate space for results on device */
CUDA_CALL(cudaMalloc((void **)&devResults, BLOCKS * THREADS *
sizeof(int)));
/* Set results to 0 */
CUDA_CALL(cudaMemset(devResults, 0, THREADS * BLOCKS *
sizeof(int)));
/* Allocate space for prng states on device */
CUDA_CALL(cudaMalloc((void **)&devStates, THREADS * BLOCKS *
sizeof(curandState)));
/* Setup prng states */
setup_kernel<<<BLOCKS, THREADS>>>(devStates);
/* Generate and use uniform pseudo-random */
generate_normal_kernel<<<BLOCKS, THREADS>>>(devStates, devResults);
/* Copy device memory to host */
CUDA_CALL(cudaMemcpy(hostResults, devResults, BLOCKS * THREADS *
sizeof(int), cudaMemcpyDeviceToHost));
/* Show result */
if (THREADS*BLOCKS > 20){
printf("First 20 stored results:\n");
for (i=0; i<20; i++)
printf("%d\n", hostResults[i]);
}
total = 0;
for(i = 0; i < BLOCKS * THREADS; i++) {
total += hostResults[i];
}
printf("Results mean = %f\n", (total/(1.0*BLOCKS*THREADS)));
/* Cleanup */
CUDA_CALL(cudaFree(devStates));
CUDA_CALL(cudaFree(devResults));
free(hostResults);
return EXIT_SUCCESS;
}
您可以轻松修改此代码以生成连续值正态分布(浮点数)。
正态分布的两个参数是均值和标准差。这些是使用SHIFT和SCALE参数表示的。 SHIFT将均值从零移动。 SCALE修改标准偏差(从1.0到任何SCALE指示)。因此,您可以使用SHIFT和SCALE参数来获得所需的分布。请注意,截断随机数生成器的实值输出会影响统计信息。您可以通过调整SCALE或SHIFT进行调整,也可以从truncf()
切换到某种舍入方式。
您可以使用以下命令编译:
nvcc -arch=sm_20 -o uniform uniform.cu
假设您拥有cc2.0或更高版本的GPU。
如果没有,可以编译:
nvcc -o uniform uniform.cu
在这种情况下,编译器警告double将被降级为float,可以忽略。
THREADS
和BLOCKS
是机器范围内的任意选择。您可以修改这些以适合您自己代码的特定启动配置。