Random curand生成器在奇数个元素上失败

Random curand生成器在奇数个元素上失败,random,cuda,Random,Cuda,我尝试了以下使用curand生成随机数的程序。当要生成的元素数(变量n)是一个奇数,如下面的9849,我在curandGenerateNormal行中得到一个错误。偶数个元素没有这个问题。原因是什么 #include <curand.h> #include <iostream> #include <cstdlib> using namespace std; #define CHKcuda(x) do {

我尝试了以下使用curand生成随机数的程序。当要生成的元素数(变量
n
)是一个奇数,如下面的9849,我在
curandGenerateNormal
行中得到一个错误。偶数个元素没有这个问题。原因是什么

#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;
}
#包括
#包括
#包括
使用名称空间std;
#定义CHKcuda(x)do{\
cudaError_t y=(x)\
如果(y!=cudaSuccess){\

通常,对于伪随机RNG,正常生成函数(例如,
curandGenerateNormal
CurandGenerateLogonormal
等)要求请求点的数量为2的倍数

这是:


例如,
cuRAND-generateuniform
没有此限制。

如果输出样本数不是四的倍数,则某些cuRAND-host随机数生成器似乎会读取内部分配内存的末尾,但我目前没有时间进一步研究。
CURAND_STATUS_LENGTH_NOT_MULTIPLE = 105, ///< Length requested is not a multple of dimension
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