按键值推力过滤 [英] Thrust filter by key value

查看:196
本文介绍了按键值推力过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个类如下:

In my application I have a class like this:

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

/*functions, zip_iterators etc. */

};

在给定的索引处,每个向量存储相同边缘的相应数据。

At a given index every vector stores the corresponding data of the same edge.

我想编写一个过滤掉给定图层所有边缘的函数,如下所示:

I want to write a function that filters out all the edges of a given layer, something like this:

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(...) (详情)

The best way I've found to do this is by using 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

The comparing_functor() is an unary function, which means I cant pass my target_layer value to it.

任何人都知道如何解决这个问题,或有一个想法来实现这一点,同时保持类的数据结构完整?

Anyone knows how to get around this, or has an idea for implementing this while keeping the data structure of the class intact?

推荐答案

您可以将特定值传递给函子,以便在谓词测试中使用,除了通常传递给他们的数据。这里有一个工作示例:

You can pass specific values to functors for use in the predicate test in addition to the data that is ordinarily passed to them. Here's a worked example:

#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;
}

这篇关于按键值推力过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆