推力::device_vector 值
推力::device_vector 键;
初始化后,keys包含一些等于-1的元素。我想删除键中和值相同位置的元素。
但不知道并行如何处理?
可能有很多方法可以做到这一点。一种可能的方式:
- 使用模板版本
thrust::remove_if
(文档),使用键作为模板,删除值中对应键为 -1 的元素。您将需要为谓词测试创建一个函子。
- use
thrust::remove
(文档)在键上删除 -1 的值
这是一个例子:
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)