zoukankan      html  css  js  c++  java
  • [翻译]CURAND Libaray--Host API--(2)

    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() 函数用来生成伪随机或者真随机数序列。包括 XORWOWMRG32k3a,MTGP32,and SOBOL32,每个元素都是32unsigned 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
    ",__FILE__,__LINE__);
    
        return EXIT_FAILURE;}} while(0)
    
    #define CURAND_CALL(x) do { if((x)!=CURAND_STATUS_SUCCESS) { 
    
        printf("Error at %s:%d
    ",__FILE__,__LINE__);
    
        return EXIT_FAILURE;}} while(0)
    
     
    
    int main(int argc, char *argv[])
    
    {
    
        size_t n = 100;
    
        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 = 0; i < n; i++) {
    
            printf("%1.4f ", hostData[i]);
    
        }
    
        printf("
    ");
    
     
    
        /* 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个样品是最高效的。(这一块有待研究)。

     

  • 相关阅读:
    HDU 5642 King's Order 动态规划
    HDU 5640 King's Cake GCD
    HDU 5641 King's Phone 模拟
    HDU 5299 Circles Game 博弈论 暴力
    HDU 5294 Tricks Device 网络流 最短路
    HDU 5289 Assignment rmq
    HDU 5288 OO’s Sequence 水题
    星际争霸 虚空之遗 人族5BB 操作流程
    Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
    Codeforces Beta Round #3 C. Tic-tac-toe 模拟题
  • 原文地址:https://www.cnblogs.com/xing901022/p/3369057.html
Copyright © 2011-2022 走看看