从device_vector中删除元素 [英] removing elements from an device_vector
本文介绍了从device_vector中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
thrust :: device_vector值
thrust::device_vector values
thrust :: device_vector键;
thrust::device_vector keys;
初始化后,元素等于-1。我想删除键中值和相同位置的元素。
After initialization, keys contains some elements equal to -1. I wanted to delete the elements in keys and in the same position of values.
但我不知道如何处理它并行?
But I do not know how to deal with it parallel?
推荐答案
有很多方法可以做到这一点。一种可能的方式:
There are probably many ways to do this. One possible way:
- 使用模板版本
thrust :: remove_if
a href =http://thrust.github.io/doc/group__stream__compaction.html#ga557e8dd3130229b1a6193b3acc82ee5e =nofollow>文档),将键用作模板,删除值中的元素,其中相应的键为-1。您将需要为谓词测试创建函子。 - 使用
thrust :: remove
(文档),以删除值为-1的
- use the stencil version of
thrust::remove_if
(documentation), with the keys as your stencil, removing the elements in values where the corresponding key is -1. You will need to create a functor for the predicate test. - use
thrust::remove
(documentation) on the keys to remove the values that are -1
$ b 示例:
Here's an example:
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/remove.h>
#include <thrust/sequence.h>
#define N 12
typedef thrust::device_vector<int>::iterator dintiter;
struct is_minus_one
{
__host__ __device__
bool operator()(const int x)
{
return (x == -1);
}
};
int main(){
thrust::device_vector<int> keys(N);
thrust::device_vector<int> values(N);
thrust::sequence(keys.begin(), keys.end());
thrust::sequence(values.begin(), values.end());
keys[3] = -1;
keys[9] = -1;
dintiter nve = thrust::remove_if(values.begin(), values.end(), keys.begin(), is_minus_one());
dintiter nke = thrust::remove(keys.begin(), keys.end(), -1);
std::cout << "results values:" << std::endl;
thrust::copy(values.begin(), nve, std::ostream_iterator<int>( std::cout, " "));
std::cout << std::endl << "results keys:" << std::endl;
thrust::copy(keys.begin(), nke, std::ostream_iterator<int>( std::cout, " "));
std::cout << std::endl;
return 0;
}
这篇关于从device_vector中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文