【CUDA开发】 CUDA Thrust 规约求和
1. 使用 Thrust
Thrust 是一个开源的 C++ 库,用于开发高性能并行应用程序,以 C++ 标准模板库为蓝本实现。
官方文档见这里:CUDA Thrust
/* ... */
float *fMatrix_Device; // 指向设备显存
int iMatrixSize = iRow * iCol; // 矩阵元素个数
cudaMalloc((void**)&fMatrix_Device, iMatrixSize * sizeof(float)); // 在显存中为矩阵开辟空间
cudaMemcpy(fMatrix_Device, fMatrix_Host, iMatrixSize * sizeof(float), cudaMemcpyHostToDevice); // 将数据拷贝到显存
thrust::device_ptr<float> dev_ptr(fMatrix_Device);
float thrustResult = thrust::reduce(dev_ptr, dev_ptr + size_t(iMatrixSize), (float)0, thrust::plus<float>());
其中,fMatrix_Host 为指向主机内存的矩阵的头指针。
2. 我的 Reduction
/**
* 每个 warp 自动同步,不用 __syncthreads();
* volatile : 加上关键字volatile的变量将被定义为敏感变量,意思是加了volatile
* 的变量在内存中的值可能会随时发生变化,当程序要去读取这个变量时,
必须要从内存中读取,而不是从缓存中读取
* sdata 数组头指针,数组位于共享内存
* tid 线程索引
*/
__device__ void warpReduce(volatile float *sdata, int tid)
{
sdata[tid] += sdata[tid + 32];
sdata[tid] += sdata[tid + 16];
sdata[tid] += sdata[tid + 8];
sdata[tid] += sdata[tid + 4];
sdata[tid] += sdata[tid + 2];
sdata[tid] += sdata[tid + 1];
}
/**
* 优化:解决了 reduce3 中存在的多余同步操作(每个warp默认自动同步)。
* globalInputData 输入数据,位于全局内存
* globalOutputData 输出数据,位于全局内存
*/
__global__ void reduce4(float *globalInputData, float *globalOutputData, unsigned int n)
{
__shared__ float sdata[BLOCK_SIZE];
// 坐标索引
unsigned int tid = threadIdx.x;
unsigned int index = blockIdx.x*(blockDim.x * 2) + threadIdx.x;
unsigned int indexWithOffset = index + blockDim.x;
if (index >= n) sdata[tid] = 0;
else if (indexWithOffset >= n) sdata[tid] = globalInputData[index];
else sdata[tid] = globalInputData[index] + globalInputData[indexWithOffset];
__syncthreads();
// 在共享内存中对每一个块进行规约计算
for (unsigned int s = blockDim.x / 2; s>32; s >>= 1)
{
if (tid < s) sdata[tid] += sdata[tid + s];
__syncthreads();
}
if (tid < 32) warpReduce(sdata, tid);
// 把计算结果从共享内存写回全局内存
if (tid == 0) globalOutputData[blockIdx.x] = sdata[0];
}
/**
* 计算 reduce4 函数的时间
* fMatrix_Host 矩阵头指针
* iRow 矩阵行数
* iCol 矩阵列数
* @return 和
*/
float RuntimeOfReduce4(float *fMatrix_Host, const int iRow, const int iCol)
{
float *fReuslt = (float*)malloc(sizeof(float));;
float *fMatrix_Device; // 指向设备显存
int iMatrixSize = iRow * iCol; // 矩阵元素个数
cudaMalloc((void**)&fMatrix_Device, iMatrixSize * sizeof(float)); // 在显存中为矩阵开辟空间
cudaMemcpy(fMatrix_Device, fMatrix_Host, iMatrixSize * sizeof(float), cudaMemcpyHostToDevice); // 将数据拷贝到显存
/* ... */
for (int i = 1, int iNum = iMatrixSize; i < iMatrixSize; i = 2 * i * BLOCK_SIZE)
{
int iBlockNum = (iNum + (2 * BLOCK_SIZE) - 1) / (2 * BLOCK_SIZE);
reduce4<<<iBlockNum, BLOCK_SIZE>>>(fMatrix_Device, fMatrix_Device, iNum);
iNum = iBlockNum;
}
cudaMemcpy(fReuslt, fMatrix_Device, sizeof(float), cudaMemcpyDeviceToHost); // 将数据拷贝到内存
/* ... */
cudaFree(fMatrix_Device);// 释放显存空间
return fReuslt[0];
}
上述程序是优化的最终版本,优化的主要内容包括:
1. 避免每个 Warp 中出现分支导致效率低下。
2. 减少取余操作。
3. 减小不必要的同步操作,每个warp都是默认同步的,不用额外的同步操作。
4. 减小线程的闲置,提高并行度
3. 时间对比
数据的大小为:
iRow = 1000;
iCol = 1000;
时间为:
ReduceThrust 的运行时间为:0.179968ms.
494497
Reduce0 的运行时间为:0.229152ms.
494497
Reduce1 的运行时间为:0.134816ms.
494497
Reduce2 的运行时间为:0.117504ms.
494497
Reduce3 的运行时间为:0.086016ms.
494497
Reduce4 的运行时间为:0.07424ms.
494497
CPU的运行时间为:1 ms.
494497
数据的大小为:
iRow = 2000;
iCol = 2000;
时间为:
ReduceThrust 的运行时间为:0.282944ms.
1.97828e+006
Reduce0 的运行时间为:0.779776ms.
1.97828e+006
Reduce1 的运行时间为:0.42624ms.
1.97828e+006
Reduce2 的运行时间为:0.343744ms.
1.97828e+006
Reduce3 的运行时间为:0.217248ms.
1.97828e+006
Reduce4 的运行时间为:0.160416ms.
1.97828e+006
CPU的运行时间为:3 ms.
1.97828e+006
数据的大小为:
iRow = 4000;
iCol = 4000;
时间为:
ReduceThrust 的运行时间为:0.536832ms.
7.91319e+006
Reduce0 的运行时间为:2.9919ms.
7.91319e+006
Reduce1 的运行时间为:1.56054ms.
7.91319e+006
Reduce2 的运行时间为:1.26618ms.
7.91319e+006
Reduce3 的运行时间为:0.726016ms.
7.91319e+006
Reduce4 的运行时间为:0.531712ms.
7.91319e+006
CPU的运行时间为:11 ms.
7.91319e+006
数据的大小为:
iRow = 6000;
iCol = 6000;
时间为:
ReduceThrust 的运行时间为:0.988992ms.
1.7807e+007
Reduce4 的运行时间为:1.09286ms.
1.7807e+007
CPU的运行时间为:25 ms.
1.7807e+007
数据的大小为:
iRow = 11000;
iCol = 11000;
时间为:
ReduceThrust 的运行时间为:2.9208ms.
5.98583e+007
Reduce4 的运行时间为:3.36998ms.
5.98583e+007
CPU的运行时间为:85 ms.
5.98583e+007
从上可以看出,2 中介绍的几种优化方式取得了良好的效果;另外,当数据量较少时,我自己优化的规约函数比 Thrust 中的规约更高效,但是当数据量大于 4000 * 4000 时,Thrust 更高效,因此还有优化的空间。
4. 完整代码
【CUDA开发】 CUDA Thrust 规约求和的更多相关文章
- CUDA开发 - CUDA 版本
"CUDA runtime is insufficient with CUDA driver"CUDA 9.2: 396.xx CUDA 9.1: 387.xx CUDA 9.0: ...
- 【CUDA开发】Thrust库
Thrust库从C++的STL中得到灵感,将最简单的类似于STL的结构放在Thrust库中,比如STL中的vector.此外,Thrust库还包含STL中的算法和迭代器. Thrust函 ...
- Windows平台CUDA开发之前的准备工作
CUDA是NVIDIA的GPU开发工具,眼下在大规模并行计算领域有着广泛应用. windows平台上面的CUDA开发之前.最好去NVIDIA官网查看说明,然后下载对应的driver. ToolKits ...
- 【ARM-Linux开发】【CUDA开发】【深度学习与神经网络】Jetson Tx2安装相关之三
JetPack(Jetson SDK)是一个按需的一体化软件包,捆绑了NVIDIA®Jetson嵌入式平台的开发人员软件.JetPack 3.0包括对Jetson TX2 , Jetson TX1和J ...
- 【CUDA开发】CUDA面内存拷贝用法总结
[CUDA开发]CUDA面内存拷贝用法总结 标签(空格分隔): [CUDA开发] 主要是在调试CUDA硬解码并用D3D9或者D3D11显示的时候遇到了一些代码,如下所示: CUdeviceptr g_ ...
- 【CUDA开发】CUDA编程接口(一)------一十八般武器
子曰:工欲善其事,必先利其器.我们要把显卡作为通用并行处理器来做并行算法处理,就得知道CUDA给我提供了什么样的接口,就得了解CUDA作为通用高性能计算平台上的一十八般武器.(如果你想自己开发驱动,自 ...
- 【神经网络与深度学习】【CUDA开发】caffe-windows win32下的编译尝试
[神经网络与深度学习][CUDA开发]caffe-windows win32下的编译尝试 标签:[神经网络与深度学习] [CUDA开发] 主要是在开发Qt的应用程序时,需要的是有一个使用的库文件也只是 ...
- 【神经网络与深度学习】【CUDA开发】【VS开发】Caffe+VS2013+CUDA7.5+cuDNN配置过程说明
[神经网络与深度学习][CUDA开发][VS开发]Caffe+VS2013+CUDA7.5+cuDNN配置过程说明 标签:[Qt开发] 说明:这个工具在Windows上的配置真的是让我纠结万分,大部分 ...
- 【视频开发】【CUDA开发】ffmpeg Nvidia硬件加速总结
原文链接:https://developer.nvidia.com/ffmpeg GPU-accelerated video processing integrated into the most p ...
随机推荐
- PHP内置常量,和可变变量,常量的定义
关键常量 可变变量----变量名是变量的变量 常量的定义
- Android浮窗权限研究(转载)
这篇博客主要介绍的是 Android 主流各种机型和各种版本的悬浮窗权限适配,但是由于碎片化的问题,所以在适配方面也无法做到完全的主流机型适配,这个需要大家的一起努力,这个博客的名字永远都是一个将来时 ...
- SDOI2015 寻宝游戏 | noi.ac#460 tree
题目链接:戳我 可以知道,我们相当于是把有宝藏在的地方围了一个圈,求这个圈最小是多大. 显然按照dfs序来遍历是最小的. 那么我们就先来一遍dfs序列,并且预处理出来每个点到根的距离(这样我们就可用\ ...
- sh_17_字符串的查找和替换
sh_17_字符串的查找和替换 hello_str = "hello world" # 1. 判断是否以指定字符串开始 print(hello_str.startswith(&qu ...
- Vue_(基础)商品管理-demo
实现对商品的增加.删除.数量的修改功能 删除商品可选择直接删除当前商品.删除选中商品.删除所有商品 添加商品时会自动添加日期字段 商品的属性 goods : { id : '', name : '', ...
- linux修改ulimit参数
有如下三种修改方式: 1.在/etc/rc.local 中增加一行 ulimit -SHn 655352.在/etc/profile 中增加一行 ulimit -SHn 655353.在/etc/se ...
- This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa
This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...
- synchronized三种使用方式,及锁的类型验证
Synchronized常用三种使用方式 1.修饰普通方法:锁对象即为当前对象 2.修饰静态方法:锁对象为当前Class对象 3.修饰代码块:锁对象为synchronized紧接着的小括号内的对象 一 ...
- Android Butterknife(黄油刀) 使用方法总结
前言: ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤.是大神J ...
- 编译内核时报错./include/net/sch_generic.h:535:28: error: inlining failed in call to always_inline 'qdisc_pkt_len': indirect function call with a yet undetermined callee static inline unsigned int qdisc_pkt_
直接修改头文件include/net/sch_generic.h中的qdisc_pkt_len函数 将static inline unsigned int qdisc_pkt_len修改为: stat ...