2013-07-04 5 views
2

В моем приложении у меня есть класс вроде этого:Упорный фильтр ключевое значение

class sample{ 
    thrust::device_vector<int> edge_ID; 
    thrust::device_vector<float> weight; 
    thrust::device_vector<int> layer_ID; 

/*functions, zip_iterators etc. */ 

}; 

В данном индексе каждый вектор сохраняет соответствующие данные одного и того же края.

Я хочу, чтобы написать функцию, которая отфильтровывает все ребра данного слоя, что-то вроде этого:

void filter(const sample& src, sample& dest, const int& target_layer){ 
     for(...){ 
     if(src.layer_ID[x] == target_layer)/*copy values to dest*/; 
     } 
} 

Лучший способом я нашел, чтобы сделать это с помощью thrust::copy_if(...)(details)

это будет выглядеть следующим образом:

void filter(const sample& src, sample& dest, const int& target_layer){ 
    thrust::copy_if(src.begin(), 
        src.end(), 
        dest.begin(), 
        comparing_functor()); 
} 

И здесь мы достигаем мою проблему:

comparing_functor() - унарная функция, что означает, что я не могу передать значение target_layer.

Кто-нибудь знает, как обойти это, или есть идея для реализации этого, сохраняя целостность структуры данных класса?

ответ

2

Вы можете передать определенные значения функторам для использования в тесте предиката в дополнение к данным, которые обычно передаются им. Вот пример:

#include <iostream> 
#include <thrust/host_vector.h> 
#include <thrust/device_vector.h> 
#include <thrust/sequence.h> 
#include <thrust/copy.h> 

#define DSIZE 10 
#define FVAL 5 

struct test_functor 
{ 
    const int a; 

    test_functor(int _a) : a(_a) {} 

    __device__ 
    bool operator()(const int& x) { 
    return (x==a); 
    } 
}; 

int main(){ 
    int target_layer = FVAL; 
    thrust::host_vector<int> h_vals(DSIZE); 
    thrust::sequence(h_vals.begin(), h_vals.end()); 
    thrust::device_vector<int> d_vals = h_vals; 
    thrust::device_vector<int> d_result(DSIZE); 
    thrust::copy_if(d_vals.begin(), d_vals.end(), d_result.begin(), test_functor(target_layer)); 
    thrust::host_vector<int> h_result = d_result; 
    std::cout << "Data :" << std::endl; 
    thrust::copy(h_vals.begin(), h_vals.end(), std::ostream_iterator<int>(std::cout, " ")); 
    std::cout << std::endl; 
    std::cout << "Filter Value: " << target_layer << std::endl; 
    std::cout << "Results :" << std::endl; 
    thrust::copy(h_result.begin(), h_result.end(), std::ostream_iterator<int>(std::cout, " ")); 
    std::cout << std::endl; 
    return 0; 
} 
+0

Спасибо за помощь, он работает. – Slawo

Смежные вопросы