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. 完整代码

GitHub

【CUDA开发】 CUDA Thrust 规约求和的更多相关文章

  1. CUDA开发 - CUDA 版本

    "CUDA runtime is insufficient with CUDA driver"CUDA 9.2: 396.xx CUDA 9.1: 387.xx CUDA 9.0: ...

  2. 【CUDA开发】Thrust库

    Thrust库从C++的STL中得到灵感,将最简单的类似于STL的结构放在Thrust库中,比如STL中的vector.此外,Thrust库还包含STL中的算法和迭代器.        Thrust函 ...

  3. Windows平台CUDA开发之前的准备工作

    CUDA是NVIDIA的GPU开发工具,眼下在大规模并行计算领域有着广泛应用. windows平台上面的CUDA开发之前.最好去NVIDIA官网查看说明,然后下载对应的driver. ToolKits ...

  4. 【ARM-Linux开发】【CUDA开发】【深度学习与神经网络】Jetson Tx2安装相关之三

    JetPack(Jetson SDK)是一个按需的一体化软件包,捆绑了NVIDIA®Jetson嵌入式平台的开发人员软件.JetPack 3.0包括对Jetson TX2 , Jetson TX1和J ...

  5. 【CUDA开发】CUDA面内存拷贝用法总结

    [CUDA开发]CUDA面内存拷贝用法总结 标签(空格分隔): [CUDA开发] 主要是在调试CUDA硬解码并用D3D9或者D3D11显示的时候遇到了一些代码,如下所示: CUdeviceptr g_ ...

  6. 【CUDA开发】CUDA编程接口(一)------一十八般武器

    子曰:工欲善其事,必先利其器.我们要把显卡作为通用并行处理器来做并行算法处理,就得知道CUDA给我提供了什么样的接口,就得了解CUDA作为通用高性能计算平台上的一十八般武器.(如果你想自己开发驱动,自 ...

  7. 【神经网络与深度学习】【CUDA开发】caffe-windows win32下的编译尝试

    [神经网络与深度学习][CUDA开发]caffe-windows win32下的编译尝试 标签:[神经网络与深度学习] [CUDA开发] 主要是在开发Qt的应用程序时,需要的是有一个使用的库文件也只是 ...

  8. 【神经网络与深度学习】【CUDA开发】【VS开发】Caffe+VS2013+CUDA7.5+cuDNN配置过程说明

    [神经网络与深度学习][CUDA开发][VS开发]Caffe+VS2013+CUDA7.5+cuDNN配置过程说明 标签:[Qt开发] 说明:这个工具在Windows上的配置真的是让我纠结万分,大部分 ...

  9. 【视频开发】【CUDA开发】ffmpeg Nvidia硬件加速总结

    原文链接:https://developer.nvidia.com/ffmpeg GPU-accelerated video processing integrated into the most p ...

随机推荐

  1. Python模拟浏览器多窗口切换

    # 模拟浏览器多窗口切换 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的Chro ...

  2. MongoDB——增删改查

    文档结构: { "_id": ObjectId("5d5e5de597eb2f0b70005d1a"), , "word_records": ...

  3. Listview操作

    设置 listView1.VirtualMode = true;   listView1.RetrieveVirtualItem += ListView1_RetrieveVirtualItem; p ...

  4. Comet OJ - Contest #10 鱼跃龙门 exgcd+推导

    考试的时候推出来了,但是忘了 $exgcd$ 咋求,成功爆蛋~ 这里给出一个求最小正整数解的模板: ll solve(ll A,ll B,ll C) { ll x,y,g,b,ans; gcd = e ...

  5. Visio:为什么按下方向键,选中的目标不动,绘图区(页面)却在移动

    造冰箱的大熊猫,本文适用于Microsoft Visio 2007@cnblogs 2018/12/12 检查下键盘的“Scroll Lock”键是不是被激活了(键盘上对应的指示灯被点亮).Scrol ...

  6. Uva 10129 Play on Words(欧拉路)

    一些秘密的门包含一个非常有趣的单词拼图.考古学家们必须解决的问题 它打开那门.因为没有其他的方式来打开大门,这个谜是非常重要的 我们. 每扇门上都有大量的磁性板.每一个盘子上都有一个字 它.板块必须以 ...

  7. 关于在mac上使用valet集成环境添加memcache扩展

    由于业务要求需要使用到memcache,直接使用brew安装在phpinfo上面显示并没有加载成功,使用以下方法时我们需要先卸载之前已经安装完成的memcache brew unlink php70- ...

  8. ETL-拉链算法-带删除的拉链算法

    truncate table CUST;truncate table TAG_CUST; truncate table vt_inc;truncate table vt_new; insert int ...

  9. 用python绘制趋势图

    import matplotlib.pyplot as plt #plt用于显示图片 import matplotlib.image as mping #mping用于读取图片 import date ...

  10. layui按回车键实现表单提交

    layui中标准用法如下: <form class="layui-form"> <input type="button" id="q ...