Cuda:紧凑和结果大小
Cuda: Compact and result size
我正在尝试使用 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。 (7 的实例出现在 objectArray
的位置 2 和 4)
results={2,4,0,0,0,0,0,0,0,0};
resultCount=2;
我是 Cuda 的新手,如果有人知道这是如何完成的,我将不胜感激。
正如@RobertCrovella 已经指出的那样,您可以使用推力来做到这一点。
以下示例使用 thrust::copy_if
to copy all of elements' indices for which the condition ("equals 7") is fulfilled. 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
我正在尝试使用 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。 (7 的实例出现在 objectArray
的位置 2 和 4)
results={2,4,0,0,0,0,0,0,0,0};
resultCount=2;
我是 Cuda 的新手,如果有人知道这是如何完成的,我将不胜感激。
正如@RobertCrovella 已经指出的那样,您可以使用推力来做到这一点。
以下示例使用 thrust::copy_if
to copy all of elements' indices for which the condition ("equals 7") is fulfilled. 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