Translated by xingoo

如果有错误请联系:xinghl90@gmail.com

2.3 返回值

所有的CURAND host端的函数返回值都是curandStatus_t.如果调用没有错误,则返回成功,即返回值为CURAND_STATUS_SUCCESS.如果发生了错误,返回值会依据错误的不同而不同。因为cuda允许内核函数异步的调用cpu端的代码,因此返回的错误,有可能是在调用函数库时发生的,而非CURAND内核函数,此时,返回值是CURAND_STATUS_PREEXISTING.

2.4 触发函数

curandStatus_t curandGenerate(curandGenerator_t generator, unsigned int *outputPtr, size_t num)

curandGenerate() 函数用来生成伪随机或者真随机数序列。包括 XORWOW、MRG32k3a,MTGP32,and SOBOL32,每个元素都是32位unsigned int型的每位都是随机产生的。对于SOBOL64触发器来说,产生的是每一位随机产生的64位的unsigned long long型随机数。

curandStatus_t curandGenerateUniform(curandGenerator_t generator,  float *outputPtr, size_t num)

curandGenerateUniform() 函数用来产生0.0-1.0间的服从均匀分布(uniformly distributed)的浮点型随机数,其中0不包含0.0,包含1.0。

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

curandGenerateNormal()函数通过指定的方法和输出目标来产生服从正态分布(normally distributed)的浮点型随机数。

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

curandGenerateLogNormal()函数通过指定的方法和输出目标产生服从对数正态分布(log-normaly distributed)的浮点数。

curandStatus_t curandGeneratePoisson(curandGenerator_t generator, unsigned int *outputPtr, size_t n, double lambda)

curandGeneratePoision()函数通过指定的lamda产生基于泊松分布(possion distributed)的随机数。

curandStatus_tcurandGenerateUniformDouble(curandGenerator_t generator, double *outputPtr, size_t num)

curandGenerateUniformDouble()函数产生双精度的均匀分布的随机数。

curandStatus_tcurandGenerateNormalDouble(curandGenerator_t generator, double *outputPtr, size_t n, double mean, double stddev)

curandGenerateNormalDouble()触发器通过指定的方法和标准输出对象产生基于正态分布的双精度随机数。双精度的随机数只能在计算能力在1.3以上或者host端产生。

curandStatus_t curandGenerateLogNormalDouble(curandGenerator_t generator,double *outputPtr, size_t n, double mean, double stddev)

curandGernerateLogNormalDouble()通过指定的方法和输出对象产生基于正态分布的对数正态分布双精度随机数。

只有多维度的触发器才能产生真随机数。(大概是这个意思,For quasirandom generation,the number of results returned must be a multiple of the dimension of the generator)

生成函数能够被同一个触发器多次调用,来产生连续的结果块。对于伪随机数触发器,多次调用size大小的随机数,相当于一次调用产生n*size大小的随机数。对于真随机数触发器,由于内存的空间排序问题,多次短的调用,与一次长调用产生的结果并不相同;然而,产生的n维动态数组确实相同的。

双精度的随机数只能在计算能力1.3以上的设备或是host端产生。

2.5 Host API Example

/*

 * This program uses the host CURAND API to generate 100 

 * pseudorandom floats.

 */

#include <stdio.h>

#include <stdlib.h>

#include <cuda.h>

#include <curand.h>

#define CUDA_CALL(x) do { if((x)!=cudaSuccess) { \

    printf("Error at %s:%d\n",__FILE__,__LINE__);\

    return EXIT_FAILURE;}} while()

#define CURAND_CALL(x) do { if((x)!=CURAND_STATUS_SUCCESS) { \

    printf("Error at %s:%d\n",__FILE__,__LINE__);\

    return EXIT_FAILURE;}} while()

int main(int argc, char *argv[])

{

    size_t n = ;

    size_t i;

    curandGenerator_t gen;

float *devData, *hostData;

    /* Allocate n floats on host */

    hostData = (float *)calloc(n, sizeof(float));

    /* Allocate n floats on device */

    CUDA_CALL(cudaMalloc((void **)&devData, n*sizeof(float)));

    /* Create pseudo-random number generator */

    CURAND_CALL(curandCreateGenerator(&gen, 

                CURAND_RNG_PSEUDO_DEFAULT));

    /* Set seed */

    CURAND_CALL(curandSetPseudoRandomGeneratorSeed(gen, 

                1234ULL));

    /* Generate n floats on device */

    CURAND_CALL(curandGenerateUniform(gen, devData, n));

    /* Copy device memory to host */

    CUDA_CALL(cudaMemcpy(hostData, devData, n * sizeof(float),

        cudaMemcpyDeviceToHost));

    /* Show result */

    for(i = ; i < n; i++) {

        printf("%1.4f ", hostData[i]);

    }

    printf("\n");

    /* Cleanup */

    CURAND_CALL(curandDestroyGenerator(gen));

    CUDA_CALL(cudaFree(devData));

    free(hostData);    

    return EXIT_SUCCESS;

}

通常通过CURAND库产生的随机数规模越大,产生的性能越好。对于多次调用产生小规模的随机数来说,尽可能少的调用随机函数库而产生大量的随机数来使用,更有效率。比如一次产生n*size大小规模的随机数,然后分n次使用,要比n次调用,每次产生size高效。XORWOW是默认的伪随机数触发器,通过默认的排序,首次调用可能要花费一些时间来启动,后来的调用就不需要这步了。为了避免启动时间,可以使用CURAND_ORDERING_PSEUDO_SEEDED排序。

MTGP32 mersenne Twister算法与线程和块数目紧密联系。MTGP32的产生结果通常是一个通过特定的参数集产生的指定顺序的256个大小的样例,每64个块使用不同的参数集并且每256个线程产生整体中的一个样例。因此使用MTGP32产生16384个样品是最高效的。(这一块有待研究)。

最新文章

  1. MySQL调优系列_日志分析
  2. SSIS的CheckPoint用法
  3. 七个结构模式之组合模式(Composite Pattern)
  4. php闭包函数简析
  5. netbeans项目中排除node_modules文件夹
  6. 测试管理_出色测试管理者的思考[持续更新ing]
  7. &quot;Principles of Reactive Programming&quot; 之 &lt;Persistent Actor State&gt;学习笔记
  8. PetaPoco 存储过程
  9. docker_openwrt
  10. 简单QT应用了可实现手动布局QT应用
  11. Win7搭建NodeJs开发环境
  12. 总结 React 组件的三种写法 及最佳实践 [涨经验]
  13. JavaScript之父谈JavaScript
  14. IKAnalyzer 添加扩展词库和自定义词
  15. java根据jar包反编译后修改再打包回jar的做法
  16. Linux下chkconfig命令详解转载
  17. Gulp构建前端自动化工作流之:常用插件介绍及使用
  18. VPS性能综合测试(7):服务器压力测试,VPS系统负载测试
  19. 如何修改FlashFXP默认编辑工具使用记事本打开
  20. according to tld or attribute directive in tag file attribute *** does not accept any expressions

热门文章

  1. Druid 0.2.25版本hive jdbc 不支持 conn.getHoldability() 兼容处理问题
  2. __thiscalll C++底层识别成员函数
  3. c语言-树的基础知识
  4. Flash 零日漏洞复现(CVE-2018-4878)
  5. 第三章 Java内存模型(下)
  6. redis学习三 redis持久化
  7. Linux 正文处理命令及tar vi 编辑器
  8. Solaris10镜像情况下如何修复boot archive
  9. 猪羊——HTML解析
  10. strcmp()比较函数和strcasecmp()和strnatcmp()