curand发生器在奇数个元素上失效 [英] curand generator fails on odd number of elements

查看:606
本文介绍了curand发生器在奇数个元素上失效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过下面的程序,它使用curand来生成随机数。当生成的元素数量(变量 n )是一个奇数,如下面的9849,我在 curandGenerateNormal 。偶数个元素没有这个问题。是什么原因呢?

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;
}

编辑:

我检查了生成函数的返回值。错误代码的定义如下:

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


它只说明在生成准随机数时,元素的数量必须是维度的倍数。那么为什么它影响这里的伪随机数生成?或者是我用来创建生成器的参数( CURAND_RNG_PSEUDO_DEFAULT )实际创建了一个quasirandom数字生成器?

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?

推荐答案

一般来说,正常的生成函数(例如 curandGenerateNormal curandGenerateLogNormal 等)要求所请求的点数为2的倍数,一个伪随机RNG。

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.

这是文档

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 ,例如,没有此限制。

curandGenerateUniform, for example, does not have this restriction.

这篇关于curand发生器在奇数个元素上失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆