0

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?

shaoyl85
  • 1,854
  • 18
  • 30

1 Answers1

2

In general, the normal generating functions (e.g. curandGenerateNormal, curandGenerateLogNormal, etc.) require the number of requested points to be a multiple of 2, for a pseudorandom RNG.

This is documented:

curandStatus_t CURANDAPI curandGenerateNormal ( curandGenerator_t generator, float* outputPtr, size_t n, float  mean, float  stddev ) 

Generate normally distributed doubles. 


Parameters 
generator- Generator to use outputPtr- Pointer to device memory to store CUDA-generated results, or Pointer to host memory to store CPU-generated results n- Number of floats to generate mean- Mean of normal distribution stddev- Standard deviation of normal distribution

Returns


•CURAND_STATUS_NOT_INITIALIZED if the generator was never created 
•CURAND_STATUS_PREEXISTING_FAILURE if there was an existing error from a previous kernel launch 
•CURAND_STATUS_LAUNCH_FAILURE if the kernel launch failed for any reason 
•CURAND_STATUS_LENGTH_NOT_MULTIPLE if the number of output samples is not a multiple of the quasirandom dimension, or is not a multiple of two for pseudorandom generators 
•CURAND_STATUS_SUCCESS if the results were generated successfully 

curandGenerateUniform, for example, does not have this restriction.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • It looks like some cuRAND host random number generators read past the end of internally allocated memory if the number of output samples is not a multiple of four, but I currently don't have time to investigate further. – tera Nov 28 '19 at 14:21