CUDA 的随机数算法 API
一、具体使用场景
如下是是在 dropout 优化中手写的 uniform_random 的 Kernel:
#include <cuda_runtime.h>
#include <curand_kernel.h>
__device__ inline float cinn_nvgpu_uniform_random_fp32(int seed){
curandStatePhilox4_32_10_t state;
int idx = threadIdx.x + blockIdx.x * blockDim.x;
curand_init(seed, idx, 1, &state);
return curand_uniform(&state);
}
二、API 解析
我们首先来看 curand_init 函数的签名和语义:
__device__ void
curand_init(unsigned long long seed,
unsigned long long subsequence,
unsigned long long offset,
curandStatePhilox4_32_10_t *state)
给定相同的seed、sequence、offset 参数下,curand_init 会保证产生相同的其实状态 state。另外此函数会在调用 2^67 ⋅ sequence + offset次 cu_rand API 之后「重置」为起始状态。关于 sequence 和 offset 的如何生效的机制,参考 StackOverflow。
注意:在 seed 相同、sequence 不同时,一般不会产生有统计学上关联的结果。原文:Sequences generated with the same seed and different sequence numbers will not have statistically correlated values.
另外在CUDA 并行产生随机数实践上,也有一些经验上的建议:
- 若保证高质量的伪随机数,建议使用不同的 seed
- 若是并行在一个「实验」里,建议指定不同的sequence参数,且最好「单调递增」
- 若果线程里的config都是一样,即 state 一样,则可以把「随机状态量」放到 global memory里,以减少 setup 开销
参考原文:For the highest quality parallel pseudorandom number generation, each experiment should be assigned a unique seed. Within an experiment, each thread of computation should be assigned a unique sequence number. If an experiment spans multiple kernel launches, it is recommended that threads between kernel launches be given the same seed, and sequence numbers be assigned in a monotonically increasing way. If the same configuration of threads is launched, random state can be preserved in global memory between launches to avoid state setup time.
然后我们看下Nvidia 主要提供了哪些常用的随机数生成API:
__device__ float
curand_uniform (curandState_t *state); // <---- It may return from 0.0 to 1.0, where 1.0 is included and 0.0 is excluded.
__device__ float
curand_normal (curandState_t *state); // <----- returns a single normally distributed float with mean 0.0 and standard deviation 1.0.
__device__ float
curand_log_normal (curandState_t *state, float mean, float stddev); // <----- returns a single log-normally distributed float based on a normal distribution with the given mean and standard deviation.
// 如下是上述 3 个API 的 double 版本
__device__ double
curand_uniform_double (curandState_t *state);
__device__ double
curand_normal_double (curandState_t *state);
__device__ double
curand_log_normal_double (curandState_t *state, double mean, double stddev);
上面的 device API 在每次调用时,只会生成一个 float/double 的随机数。Nvidia 同样提供了一次可以生成 2个或4个 device API:
__device__ uint4
curand4 (curandStatePhilox4_32_10_t *state);
__device__ float4
curand_uniform4 (curandStatePhilox4_32_10_t *state);
__device__ float4
curand_normal4 (curandStatePhilox4_32_10_t *state);
__device__ float4
curand_log_normal4 (curandStatePhilox4_32_10_t *state, float mean, float stddev);
从上面的函数接口以及 Nvidia 的文档来看,在初始化某种类型的 state 状态量后,每次调用类似 curand() 的 API 后,state 都会自动进行 offset 偏移。
因此,Nvidia 官网上也提供了单独对 state 进行位移的 API,其效果等价于调用多次无返回值的 curand() API,且性能更好:
__device__ void
skipahead(unsigned long long n, curandState_t *state); // <----- == calls n*curand()
__device__ void
skipahead_sequence(unsigned long long n, curandState_t *state); // <----- == calls n*2^67 curand()
三、性能分析
Nvidia 的官网明确指出了存在的性能问题,给开发者实现高性能 Kernel 提供了充分的经验指导:
- curand_init()要比curand()和curand_uniform()慢!
- curand_init()在 offset 比较大时性能也会比小 offset 差!
- save/load操作 state 比每次重复创建起始 state 性能要快很多 !
原文如下:Calls to curand_init() are slower than calls to curand() or curand_uniform(). Large offsets to curand_init() take more time than smaller offsets. It is much faster to save and restore random generator state than to recalculate the starting state repeatedly.
对于上述第三点,Nvidia 建议可以将 state 存放到 global memory 中,如下是一个样例代码:
__global__ void example(curandState *global_state)
{
curandState local_state;
local_state = global_state[threadIdx.x];
for(int i = 0; i < 10000; i++) {
unsigned int x = curand(&local_state);
...
}
global_state[threadIdx.x] = local_state;
}
从另一个维度来讲,相对于A产生随机数操作的API,初始化 state 会占用更多的「寄存器」和 local memory 资源。因此 nvidia 建议将 curand_init 和 curand() API 拆分放到不同的 Kernel 中,可以获得最大的性能收益;
原文:Initialization of the random generator state generally requires more registers and local memory than random number generation. It may be beneficial to separate calls to curand_init() and curand() into separate kernels for maximum performance.
State setup can be an expensive operation. One way to speed up the setup is to use different seeds for each thread and a constant sequence number of 0. This can be especially helpful if many generators need to be created. While faster to set up, this method provides less guarantees about the mathematical properties of the generated sequences. If there happens to be a bad interaction between the hash function that initializes the generator state from the seed and the periodicity of the generators, there might be threads with highly correlated outputs for some seed values. We do not know of any problem values; if they do exist they are likely to be rare.
Nvidia 提供的 API 样例代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <curand_kernel.h>
#define CUDA_CALL(x) do { if((x) != cudaSuccess) { \
printf("Error at %s:%d\n",__FILE__,__LINE__); \
return EXIT_FAILURE;}} while(0)
__global__ void setup_kernel(curandState *state)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
/* Each thread gets same seed, a different sequence
number, no offset */
curand_init(1234, id, 0, &state[id]);
}
__global__ void generate_uniform_kernel(curandStatePhilox4_32_10_t *state,
int n,
unsigned int *result)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
unsigned int count = 0;
float x;
/* Copy state to local memory for efficiency */
curandStatePhilox4_32_10_t localState = state[id];
/* Generate pseudo-random uniforms */
for(int i = 0; i < n; i++) {
x = curand_uniform(&localState);
/* Check if > .5 */
if(x > .5) {
count++;
}
}
/* Copy state back to global memory */
state[id] = localState;
/* Store results */
result[id] += count;
}
CUDA 的随机数算法 API的更多相关文章
- 硬核 - Java 随机数相关 API 的演进与思考(上)
本系列将 Java 17 之前的随机数 API 以及 Java 17 之后的统一 API 都做了比较详细的说明,并且将随机数的特性以及实现思路也做了一些简单的分析,帮助大家明白为何会有这么多的随机数算 ...
- 硬核 - Java 随机数相关 API 的演进与思考(下)
本系列将 Java 17 之前的随机数 API 以及 Java 17 之后的统一 API 都做了比较详细的说明,并且将随机数的特性以及实现思路也做了一些简单的分析,帮助大家明白为何会有这么多的随机数算 ...
- Mahout推荐算法API详解
转载自:http://blog.fens.me/mahout-recommendation-api/ Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, ...
- 转】Mahout推荐算法API详解
原博文出自于: http://blog.fens.me/mahout-recommendation-api/ 感谢! Posted: Oct 21, 2013 Tags: itemCFknnMahou ...
- C语言生成32位和64位随机数算法
C语言生成32位和64位随机数算法 /** * randstd.h * * Standard definitions and types, Bob Jenkins * * 2015-01-19: re ...
- [转]Mahout推荐算法API详解
Mahout推荐算法API详解 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeepe ...
- Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性 应该内置到语言里面
Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性 应该内置到语言里面 1. 常用算法1 1.1. 目录2 1.2. 定义和用法编辑2 1.3 ...
- Mahout推荐算法API具体解释【一起学Mahout】
阅读导读: 1.mahout单机内存算法实现和分布式算法实现分别存在哪些问题? 2.算法评判标准有哪些? 3.什么会影响算法的评分? 1. Mahout推荐算法介绍 Mahout推荐算法,从数据处理能 ...
- 基于“均态分布”随机数算法的一次性口令OneTimePassword(原创)
/* 所谓均态分布随机数算法是指:每个数(整数或实数)无序地分布在数轴上,值只出现一次永不重复.体现了香农的一次一密理论. * 均体现在每个数的值是平均概率,即都有出现:态体现在每个数在数轴上的位置是 ...
- java基础 - 冒泡排序,随机数算法
从简单做起 任何困难的事情都是由简单的一步步一件件事情堆起来 理解好算法才是最重要 1.冒泡排序: public class Test { public static void main(String ...
随机推荐
- k8s中的kubeclt命令
一.kubectl 基本命令 1.陈述式资源管理方法: 1.kubernetes集群管理集群资源的唯一入口是通过相应的方法调用apiserver的接口 2.kubectl 是官方的CLI命令行工具,用 ...
- 【学习】蓝牙的一些基础知识or什么是蓝牙
蓝牙----Bluetooth(短距离无线通信技术) 2022-07-29 14:31:27 蓝牙技术有什么特点(体积小,易集成,低功耗,适用广,抗干扰,成本低,开放性) (1) 蓝牙模块体积很 ...
- CUDA基础2
二. 1.指令调度,对于多条指令怎样调度让他们运行更快. 对于有冲突的两条指令,采用寄存器重命名技术. 2.指令重排 乱序执行,为了获取最大的吞吐率. 增大功耗 增加芯片面积. 3.缓存,容量越大 ...
- Linux值得收藏的40个命令总结,常用的正则表达式
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} \; 2 查看进程 按内存从大到小排列 PS -e -o "%C : %p : %z : %a& ...
- Dubbo和Zookeeper(Springboot集成)
Dubbo和Zookeeper集成: 分布式理论: 分布式系统是由一组通过网络进行通信.为了完成共同的任务而协调工作的计算机节点组成的系统.分布式系统的出现是为了用廉价的.普通的机器完成单个计算机无法 ...
- el与data的两种写法
el的两种写法 Vue初始化(被创建)后会判断有无el 有el:创建Vue实例对象的时候配置el属性 无el:通过vm.$mount('#root')指定el的值 data的两种写法 对象式:data ...
- Innodb的Buffer Pool
什么是Buffer Pool 为了缓存磁盘中的页,MySQL服务器启动的时候就向操作系统申请了一片连续的内存,他们给这片内存起了个名,叫做Buffer Pool(中文名是缓冲池).innodb_buf ...
- 3.错误代码C4996
3.错误代码C4996 错误 C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s inst ...
- NGINX配置SSL支持
前言 在文章-腾讯云申请免费SSL证书中, 我们已经申请好了SSL证书. 那么现在, 我们就要配置全站SSL了! 这次的工作主要是NGINX的配置, 同时会有一些我的博客本身的配置. 博客本身配置更改 ...
- Asp-Net-Core开发笔记:使用RateLimit中间件实现接口限流
前言 最近一直在忙(2月份沉迷steam,3月开始工作各种忙),好久没更新博客了,不过也积累了一些,忙里偷闲记录一下. 这个需求是这样的,我之前做了个工单系统,现在要对登录.注册.发起工单这些功能做限 ...