Cuda:紧凑型和结果大小

Cuda:紧凑型和结果大小,cuda,Cuda,我正在尝试使用CUDA查找具有三维坐标的对象之间的距离。也就是说,我只对两种类型的对象感兴趣。对象以数组中的数字表示。对于这个问题,我只对获取对象数组中第一类对象(用户指定的数字)的位置感兴趣 为此,我目前正试图将此列表和结果列表传递给我的设备,并让设备检查数组中的每个位置是否为指定的数字(表示第一个对象),如果是,则将该数组位置放置在要返回到主机的结果数组中 作为示例输入,假设我有: int objectArray[10] = { 1, 11, 7, 2, 7, 23, 6, 6, 9, 11

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

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

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

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.(7的实例出现在
objectArray
的位置2和4)


我是Cuda的新手,如果有人知道如何做到这一点,我将非常感谢您的帮助。

您可以使用@RobertCrovella已经指出的推力来完成这一点。 以下示例用于复制满足条件(“等于7”)的所有元素索引。用于避免显式创建索引序列

#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;
}

这应该很容易。代码是有效的,是一个很好的演示,但它实际上没有使用GPU。@RobertCrovella你说得对,我只是从问题中复制了输入数据。改变为推力::设备_向量有望用于操作。效果很好。我已经试着复制了一段时间,但无法让它完成这项任务(或者更简单的任务)。我实际上处理的是10GB的pdb蛋白质文件,所以我不能在高级中指定对象数组的大小。我很确定我能用推力矢量完成。感谢您抽出时间回复。
#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