thrust :: remove_if的返回值类型 [英] Type of return value of thrust::remove_if

查看:277
本文介绍了thrust :: remove_if的返回值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在
的设备上有两个整数数组 dmap dflag b $ b和我已经用推力设备指针, dmapt
包装他们 dflagt

I have two arrays of integers dmap and dflag on the device of the same length and I have wrapped them with thrust device pointers, dmapt and dflagt

dmap数组中有一些元素的值为-1。我想要
删除这些-1和相应的值从
dflag数组。

There are some elements in the dmap array with value -1. I want to remove these -1's and the corresponding values from the dflag array.

我使用remove_if函数做到这一点,但我不能弄清楚
这个调用的返回值是什么或我应该如何使用
返回值得到。

I am using the remove_if function to do this, but I cannot figure out what the return value of this call is or how I should use this returned value to get .

(我想将这些缩减的数组传递给 reduce_by_key 函数
其中dflagt将用作

( I want to pass these reduced arrays to the reduce_by_key function where dflagt will be used as the keys. )

我使用以下调用进行缩小。请让我
知道我如何存储返回的值在一个变量和
使用它来解决各个数组 dflag dmap

I am using the following call for doing the reduction. Please let me know how I can store the returned value in a variable and use it to address the individual arrays dflag and dmap

thrust::remove_if( 
    thrust::make_zip_iterator(thrust::make_tuple(dmapt, dflagt)), 
    thrust::make_zip_iterator(thrust::make_tuple(dmapt+numindices, dflagt+numindices)), 
    minus_one_equality_test() 
); 

其中上面使用的谓词函子定义为

where the predicate functor used above is defined as

struct minus_one_equality_test
{ 
    typedef typename thrust::tuple<int,int> Tuple; 
    __host__ __device__ 
    bool operator()(const Tuple& a ) 
    { 
        return  thrust::get<0>(a) ==  (-1); 
    } 
} 


推荐答案

返回值是一个zip_iterator,它标记了在remove_if调用期间函子返回true的元组序列的新结束。要访问底层数组的新端迭代器,需要从zip_iterator中检索元组迭代器;该元组的内容就是用于构建zip_iterator的原始数组的新端迭代器。它在代码中比字更复杂:

The return value is a zip_iterator which marks the new end of the sequence of tuples for which your functor returned true during the remove_if call. To access the new end iterator of the underlying array you will need to retrieve a tuple iterator from the zip_iterator; the contents of that tuple are then the new end iterators of the original arrays you used to build the zip_iterator. It is a lot more convoluted in words than in code:

#include <thrust/tuple.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/remove.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/copy.h>

#include <iostream>

struct minus_one_equality_test
{ 
    typedef thrust::tuple<int,int> Tuple; 
    __host__ __device__ 
    bool operator()(const Tuple& a ) 
    { 
        return  thrust::get<0>(a) ==  (-1); 
    }; 
}; 


int main(void)
{
    const int numindices = 10;

    int mapt[numindices] = { 1, 2, -1, 4, 5, -1, 7, 8, -1, 10 };
    int flagt[numindices] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    thrust::device_vector<int> vmapt(10);
    thrust::device_vector<int> vflagt(10);

    thrust::copy(mapt, mapt+numindices, vmapt.begin());
    thrust::copy(flagt, flagt+numindices, vflagt.begin());

    thrust::device_ptr<int> dmapt = vmapt.data();
    thrust::device_ptr<int> dflagt = vflagt.data();

    typedef thrust::device_vector< int >::iterator  VIt;
    typedef thrust::tuple< VIt, VIt > TupleIt;
    typedef thrust::zip_iterator< TupleIt >  ZipIt;

    ZipIt Zend = thrust::remove_if(  
        thrust::make_zip_iterator(thrust::make_tuple(dmapt, dflagt)), 
        thrust::make_zip_iterator(thrust::make_tuple(dmapt+numindices, dflagt+numindices)), 
        minus_one_equality_test() 
    ); 

    TupleIt Tend = Zend.get_iterator_tuple();
    VIt vmapt_end = thrust::get<0>(Tend);

    for(VIt x = vmapt.begin(); x != vmapt_end; x++) {
        std::cout << *x << std::endl;
    }

    return 0;
}



如果编译并运行它,你应该看到这样: / p>

If you compile this and run it, you should see something like this:

$ nvcc -arch=sm_12 remove_if.cu 
$ ./a.out
1
2
4
5
7
8
10


b $ b

在这个例子中,我只检索元组的第一个元素的缩短内容,第二个以相同的方式访问,即。标记向量的新结尾的迭代器是 thrust :: get< 1>(Tend)

这篇关于thrust :: remove_if的返回值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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