Cuda 从设备向量中删除元素
推力::设备_矢量值 推力:设备_矢量键 初始化后,键包含一些等于-1的元素。我想删除键中的元素和值的相同位置Cuda 从设备向量中删除元素,cuda,thrust,Cuda,Thrust,推力::设备_矢量值 推力:设备_矢量键 初始化后,键包含一些等于-1的元素。我想删除键中的元素和值的相同位置 但我不知道如何并行处理它?可能有很多方法可以做到这一点。一种可能的方式: 使用模具版本的struch::remove_if(),将键作为模具,删除对应键为-1的值中的元素。您需要为谓词测试创建一个函子 使用键上的struch::remove()删除-1的值 下面是一个例子: #include <iostream> #include <thrust/device_vec
但我不知道如何并行处理它?可能有很多方法可以做到这一点。一种可能的方式:
struch::remove_if
(),将键作为模具,删除对应键为-1的值中的元素。您需要为谓词测试创建一个函子李>
struch::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;
}
#包括
#包括
#包括
#包括
#包括
#定义N 12
typedef推力::设备向量::迭代器dintiter;
结构是_减_一
{
__主机设备__
布尔运算符()(常量int x)
{
返回(x==-1);
}
};
int main(){
推力::设备_矢量键(N);
推力:设备_矢量值(N);
顺序(keys.begin(),keys.end());
序列(values.begin(),values.end());
键[3]=-1;
键[9]=-1;
dintiter nve=推力::如果(values.begin(),values.end(),keys.begin(),是_减_一()),则删除_;
dintiter nke=推力::移除(key.begin(),key.end(),-1);
std::cout另一种方法是只使用remove\u if
并将键
和值
数组压缩在一起。如果remove\u if
的谓词将检查元组的第一个元素是否有感兴趣的键。这可能会快一点,因为键数组的元素只需要是l已加载一次。调用谓词函数时,我对此实现有问题。程序未生成并产生以下错误:“函数范围内定义的类型不能用于全局函数模板实例化的模板参数类型”关于如何纠正这一点,有什么建议吗?我刚刚通过在linux上的CUDA 9上逐字编译这段代码再次测试了这一点,它编译得很干净,没有问题。您可能有不正确的构建环境或编译命令。或者,如果您的代码与此处发布的代码不完全相同,那么您可能引入了一个错误。我不会这样做可以在没有看到的情况下对此进行评论。无论如何,可能无法在此处的评论空间中进行排序。我非常确信发布的代码没有问题。如果您有新问题,通常最好的建议是发布新问题。您可以链接到此问题。您是对的……我正在尝试删除If()用于删除在谓词函数中指定值的多个值(除-1外)。我将采纳您的建议,谢谢。您可以通过函子初始化参数(即在运行时)传递所需的值,而不是在函子中指定值。您可以在此处找到推力代码,以便演示如何将初始化值传递给函子。