I tried the following program which uses curand to generate random numbers. When the number of elements to generate (variable n
) is an odd number like 9849 below, I got an error on the line with curandGenerateNormal
. Even number of elements does not have this problem. What is the reason of that?
#include <curand.h>
#include <iostream>
#include <cstdlib>
using namespace std;
#define CHKcuda(x) do { \
cudaError_t y = (x); \
if (y != cudaSuccess) { \
cout << __LINE__ << ": " << y << endl; exit(1); \
} \
} while(0)
#define CHKcurand(x) do { \
curandStatus_t y = (x); \
if (y != CURAND_STATUS_SUCCESS) { \
cout << __LINE__ << ": " << y << endl; exit(1); \
} \
} while(0)
int main(int argc, char** argv) {
curandGenerator_t g_randgen;
float *ptr, *h_ptr;
int n;
if (argc > 1) {
n = atoi(argv[1]);
}
CHKcurand(curandCreateGenerator(&g_randgen, CURAND_RNG_PSEUDO_DEFAULT));
CHKcuda(cudaMalloc((void**)&ptr, n * sizeof(float)));
CHKcurand(curandGenerateNormal(g_randgen, ptr, n, 0, 0.1));
h_ptr = static_cast<float*>(malloc(sizeof(float) * n));
CHKcuda(cudaMemcpy(h_ptr, ptr, sizeof(float) * n, cudaMemcpyDeviceToHost));
CHKcuda(cudaDeviceSynchronize());
for (int i = 0; i < 5; i++) {
cout << h_ptr[i] << ", ";
}
cout << endl;
return 0;
}
EDIT:
I checked the return value of the generating function. The definition of the error code says the following:
CURAND_STATUS_LENGTH_NOT_MULTIPLE = 105, ///< Length requested is not a multple of dimension
However, in the documentation it only says when generating quasirandom numbers, the number of elements must be a multiple of the dimension. So why it affects the pseudorandom number generation here? Or is the parameter I'm using to create the generator (CURAND_RNG_PSEUDO_DEFAULT
) actually created a quasirandom number generator? And moreover, what is the exact value of the dimension and where can I find it out?