2
thrust :: device_vector值移除device_vector中的元素
thrust :: device_vector keys;
初始化後,鍵包含一些等於-1的元素。我想刪除鍵中的元素和值的相同位置。
但我不知道如何處理它並行?
thrust :: device_vector值移除device_vector中的元素
thrust :: device_vector keys;
初始化後,鍵包含一些等於-1的元素。我想刪除鍵中的元素和值的相同位置。
但我不知道如何處理它並行?
有可能有很多方法來做到這一點。一種可能的方式:
thrust::remove_if
(documentation)模板的版本,用鑰匙作爲模板,在值,其中對應的關鍵是去除-1的元素。您將需要爲謂詞測試創建一個函子。thrust::remove
(documentation)取消是-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;
}
另一種方式來做到這一點是隻使用'remove_if'和zip 'keys'和'values'數組一起。賦給'remove_if'的謂詞會檢查元組的第一個元素是否爲感興趣的鍵。這可能會快一點,因爲keys數組的元素只需要加載一次。 –
謝謝。有用!^_^ – GaoYuan
我在調用謂詞函數時遇到了這個實現的問題。該程序不會生成併產生以下錯誤: 「在函數作用域內定義的類型不能用於__global__函數模板實例化的模板參數類型」 有關如何糾正此問題的任何建議? – danieljovan