【问题标题】:Cuda: Compact and result sizeCuda:紧凑和结果大小
【发布时间】:2015-09-09 20:45:24
【问题描述】:

我正在尝试使用 CUDA 来查找具有 3D 坐标的对象之间的距离。也就是说,我只对两种类型的对象感兴趣。对象表示为数组中的数字。对于这个问题,我只对获取对象数组中第一类对象(用户指定的数字)的位置感兴趣。

为此,我目前正在尝试将此列表和结果列表传递给我的设备,并让设备检查数组中的每个位置是否是指定的数字(代表第一个对象) - 如果是,则放置要返回给主机的结果数组中的该数组位置。

作为示例输入,假设我有:

int objectArray[10] = { 1, 11, 7, 2, 7, 23, 6, 6, 9, 11 };

int results[10]={0,0,0,0,0,0,0,0,0,0};

我正在尝试获取所有 7 实例的位置(最好是一个返回值,说明找到了多少个 7 实例) IE。 (objectArray 的位置 2 和 4 中存在 7 的实例)

results={2,4,0,0,0,0,0,0,0,0};

resultCount=2;

我是 Cuda 的新手,如果有人知道这是如何完成的,我将不胜感激。

【问题讨论】:

  • 使用thrust 应该很容易。

标签: cuda


【解决方案1】:

正如@RobertCrovella 已经指出的那样,您可以使用推力来做到这一点。 以下示例使用 thrust::copy_if 复制满足条件(“等于 7”)的所有元素的索引。 thrust::counting_iterator 用于避免显式创建索引序列。

#include <thrust/copy.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/functional.h>
#include <iostream>

using namespace thrust::placeholders;

int main()
{
    const int N = 10;
    int objectArray[N] = { 1, 11, 7, 2, 7, 23, 6, 6, 9, 11 };
    int results[N]={0};

    int* end = thrust::copy_if(thrust::make_counting_iterator(0), thrust::make_counting_iterator(N), objectArray, results, _1 == 7);

    thrust::copy(results, results+N, std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl << "result count = " << end-results << std::endl;
    return 0;
}

输出:

2 4 0 0 0 0 0 0 0 0
result count = 2

【讨论】:

  • 代码有效,很好的演示,但实际上并没有用到GPU。
  • @RobertCrovella 你是对的,我只是从问题中复制了输入数据。 OP 希望可以更改为推力::device_vector。
  • 效果很好。我一直在尝试 copy_if 一段时间,但无法做到这一点(或更简单的任务)。我实际上正在处理 10 GB 的 pdb 蛋白质文件,因此我无法提前指定对象数组的大小。很确定我可以用推力矢量把它拉下来。感谢您抽出宝贵时间回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 2010-10-30
  • 2010-09-18
相关资源
最近更新 更多