#include"cuda_runtime.h" #include"device_launch_parameters.h" #include"curand_kernel.h"// this lib shoulb be included #include<ctime> #include<iostream> #include<random>
usingnamespacestd;
//-------------------generate random numbers-------// __device__ floatgenerate(curandState *globalState, int ind) { curandState localState = globalState[ind]; float RANDOM = curand_uniform(&localState);// uniform distribution globalState[ind] = localState; return RANDOM; }
__global__ voidsetup_kernel(curandState *state, unsignedlong seed) { int ix = threadIdx.x + blockIdx.x*blockDim.x; int iy = threadIdx.y + blockIdx.y*blockDim.y; int idx = iy * blockDim.x*gridDim.x + ix; curand_init(seed, idx, 0, &state[idx]);// initialize the state }
//-------------This is our kernel function where the random numbers generated------// __global__ voidour_kernel(curandState *globalState,int nx,int ny) { int ix = threadIdx.x + blockIdx.x*blockDim.x; int iy = threadIdx.y + blockIdx.y*blockDim.y; int idx = iy * blockDim.x*gridDim.x + ix;
if (ix < nx&&iy < ny) { int k = generate(globalState, idx) * 100000; printf("%d\n", k); }
}
intmain() { int nx = 5; int ny = 2;// generate nx*ny random numbers
int blockx = 32; int blocky = 1; dim3 block(blockx, blocky);//(32,1)