Cuda:紧凑和结果尺寸 [英] Cuda: Compact and result size

查看:506
本文介绍了Cuda:紧凑和结果尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to use CUDA to find the distance between objects with 3D coordinates. That said, I am only interested in 2 types of objects. The objects are represented as numbers in an array. For this question I am only interested in getting the positions of the first type of object (a user specified number) in the object array.

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

To that end I'm currently trying to pass this list and a results list to my device and have the device check if each location in the array is the specified number (representing the first object) - and if it is, place that array location in a results array to be returned to the host.

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

As an example input, suppose I have:

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)

and I'm trying to get the positions of all the instances of 7 (and preferably, a returned value stating how many instances of 7 were found) ie. (with instances of 7 being present in position 2 and 4 of objectArray)

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

resultCount=2;

我对Cuda很新,如果有人知道这是怎么做的,帮助。

I'm fairly new to Cuda and if anyone knows how this is done, I'd appreciate the help.

推荐答案

您可以使用推力作为@RobertCrovella已经指出。
以下示例使用 thrust :: copy_if 复制满足条件(等于7)的所有元素的索引。使用 thrust :: counting_iterator 以避免明确地创建索引序列。

You can do this using thrust as @RobertCrovella already pointed out. The following example uses thrust::copy_if to copy all of elements' indices for which the condition ("equals 7") is fulfilled. thrust::counting_iterator is used to avoid creating the sequence of indices explicitly.

#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:紧凑和结果尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆