【问题标题】:Parallel reduction on CUDA with array in device使用设备中的阵列在 CUDA 上并行减少
【发布时间】:2012-04-24 20:31:41
【问题描述】:

我需要执行并行归约以找到 CUDA 设备上数组的最小值或最大值。我为此找到了一个很好的库,称为 Thrust。您似乎只能对主机内存中的数组执行并行缩减。我的数据在设备内存中。是否可以减少设备内存中的数据? 我不知道该怎么做。这是 Thrust 的文档:http://code.google.com/p/thrust/wiki/QuickStartGuide#Reductions。谢谢大家。

【问题讨论】:

    标签: cuda thrust reduction


    【解决方案1】:

    如果thrust 或任何其他库没有为您提供这样的服务,您仍然可以自己创建该内核。

    Mark Harris 有一个关于并行缩减及其在 cuda 上的优化的精彩教程。 按照他的幻灯片,根据您的需要实施和修改它并不难。

    【讨论】:

      【解决方案2】:

      您可以减少已在设备内存中的阵列的推力。您需要做的就是将设备指针包装在 thrust::device_pointer 容器中,然后调用其中一个缩减过程,正如您链接到的 wiki 中所示:

      // assume this is a valid device allocation holding N words of data
      int * dmem;
      
      // Wrap raw device pointer 
      thrust::device_ptr<int> dptr(dmem);
      
      // use max_element for reduction
      thrust::device_ptr<int> dresptr = thrust::max_element(dptr, dptr+N);
      
      // retrieve result from device (if required)
      int max_value = dresptr[0];
      

      注意返回值也是device_ptr,所以可以直接在其他内核中使用thrust::raw_pointer_cast

      int * dres = thrust::raw_pointer_cast(dresptr); 
      

      【讨论】:

        猜你喜欢
        • 2015-08-27
        • 1970-01-01
        • 2012-12-25
        • 1970-01-01
        • 2012-04-04
        • 2018-04-15
        • 1970-01-01
        • 2013-12-06
        • 2016-06-08
        相关资源
        最近更新 更多