2 // https://docs.nvidia.com/cuda/curand/host-api-overview.html#host-api-example
5 # define EXPORT __declspec(dllexport)
11 * This program uses the host CURAND API to generate 100
12 * pseudorandom floats.
19 #define CUDA_CALL(x) \
21 if ((x) != cudaSuccess) { \
22 printf("Error at %s:%d\n", __FILE__, __LINE__); \
23 return EXIT_FAILURE; \
26 #define CURAND_CALL(x) \
28 if ((x) != CURAND_STATUS_SUCCESS) { \
29 printf("Error at %s:%d\n", __FILE__, __LINE__); \
30 return EXIT_FAILURE; \
34 EXPORT int curand_main()
38 curandGenerator_t gen;
39 float *devData, *hostData;
41 /* Allocate n floats on host */
42 hostData = (float*)calloc(n, sizeof(float));
44 /* Allocate n floats on device */
45 CUDA_CALL(cudaMalloc((void**)&devData, n * sizeof(float)));
47 /* Create pseudo-random number generator */
48 CURAND_CALL(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT));
51 CURAND_CALL(curandSetPseudoRandomGeneratorSeed(gen, 1234ULL));
53 /* Generate n floats on device */
54 CURAND_CALL(curandGenerateUniform(gen, devData, n));
56 /* Copy device memory to host */
58 cudaMemcpy(hostData, devData, n * sizeof(float), cudaMemcpyDeviceToHost));
61 CURAND_CALL(curandDestroyGenerator(gen));
62 CUDA_CALL(cudaFree(devData));