首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >thrust::remove_if返回值类型

thrust::remove_if返回值类型
EN

Stack Overflow用户
提问于 2012-09-05 03:02:12
回答 1查看 2K关注 0票数 3

我在相同长度的设备上有两个整数数组dmapdflag,并且我用推力设备指针dmaptdflagt包装了它们

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

我使用remove_if函数来做这件事,但是我不知道这个调用的返回值是什么,或者我应该如何使用这个返回值来获取。

(我希望将这些简化的数组传递给reduce_by_key函数,其中将使用dflagt作为键。)

我正在使用下面的调用来进行缩减。请告诉我如何将返回值存储在变量中,并使用它来处理单独的数组dflagdmap

代码语言:javascript
复制
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() 
); 

其中,上面使用的谓词函数器定义为

代码语言:javascript
复制
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); 
    } 
} 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-05 04:28:05

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

代码语言:javascript
复制
#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;
}

如果编译并运行它,您应该会看到如下所示:

代码语言:javascript
复制
$ nvcc -arch=sm_12 remove_if.cu 
$ ./a.out
1
2
4
5
7
8
10

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12269773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档