【问题标题】:removing entries in sorted list: efficiently in gpu删除排序列表中的条目:在 gpu 中有效
【发布时间】:2011-11-04 23:28:53
【问题描述】:

我正在尝试在 cuda/thrust 中编写以下问题。我得到一个键列表和与每个键关联的三个值。我设法按字典顺序对它们进行了排序。如果具有相同键的输入具有每个值关系,则现在需要减少输入。在下面的示例中,V1(a)

示例输入:

       Key      V1      V2      V3  
a.      1       2       5       3
b.      1       2       6       2
c.      1       2       7       4           
d.      1       3       6       5           
e.      2       8       8       8
f.      3       1       2       4

示例输出:

         Key    V1  V2  V3
 a.        1    2   5   3
 b.        1    2   6   2
 e.        2    8   8   8
 f.        3    1   2   4
  • 输入 a c 已删除
  • 输入 a d 已删除

我已经能够使用 for 循环和 if 语句解决上述问题。我目前正在尝试使用基于 gpu 的 cuda/thrust 来解决这个问题。这可以在 gpu 上完成(最好是推力)还是必须用 cuda 编写单个内核?

我没有像 Thrust: Removing duplicates in key-value arrays 中讨论的那样使用 unique 来制定这个问题

已编辑以包含程序“stl/c++”程序以生成上述场景:“Reducing myMap”部分是我使用 for 循环和 if 语句的实现。

#include <iostream>
#include <tr1/array>
#include <vector>
#include <algorithm>

struct mapItem {
    mapItem(int k, int v1, int v2, int v3){
        key=k; 
        std::tr1::array<int,3> v = {v1, v2, v3};
        values=v;
    };
    int key;
    std::tr1::array<int,3> values;
};

struct sortLexiObj{
    bool operator()(const mapItem& lhs, const mapItem& rhs){ 
        return lhs.values < rhs.values; 
    }
};
struct sortKey{
    bool operator()(const mapItem& lhs, const mapItem& rhs){ 
        return lhs.key < rhs.key; 
    }
};

int main(int argc, char** argv){

    std::vector<mapItem> myMap;

    // Set up initial matrix:
    myMap.push_back(mapItem(3, 1, 2, 4));
    myMap.push_back(mapItem(1, 2, 6, 2));
    myMap.push_back(mapItem(1, 2, 5, 3));
    myMap.push_back(mapItem(1, 3, 6, 5));
    myMap.push_back(mapItem(2, 8, 8, 8));
    myMap.push_back(mapItem(1, 2, 7, 4));

    std::sort(myMap.begin(), myMap.end(), sortLexiObj());
    std::stable_sort(myMap.begin(), myMap.end(), sortKey());
    std::cout << "\r\nOriginal sorted Map" << std::endl;
    for(std::vector<mapItem>::iterator mt=myMap.begin(); mt!=myMap.end(); ++mt){
        std::cout << mt->key << "\t";
        for(std::tr1::array<int,3>::iterator it=(mt->values).begin(); it!=(mt->values).end(); ++it){
            std::cout << *it << " ";
        }
        std::cout << std::endl;
    }
    /////////////////////////

    // Reducing myMap
    for(std::vector<mapItem>::iterator it=myMap.begin(); it!=myMap.end(); ++it){
        std::vector<mapItem>::iterator jt=it; ++jt;
        for (; jt != myMap.end();) {
            if (   (it->key == jt->key)){
                if ( it->values.at(0) <= jt->values.at(0) && 
                    it->values.at(1) <= jt->values.at(1) &&
                    it->values.at(2) <= jt->values.at(2) ) {
                    jt = myMap.erase(jt);
                } 
                else ++jt;
            }
            else break;
        }
    }

    std::cout << "\r\nReduced Map" << std::endl;
    for(std::vector<mapItem>::iterator mt=myMap.begin(); mt!=myMap.end(); ++mt){
        std::cout << mt->key << "\t";
        for(std::tr1::array<int,3>::iterator it=(mt->values).begin(); it!=(mt->values).end(); ++it){
            std::cout << *it << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

【问题讨论】:

  • 无法准确识别您需要什么,但似乎thrust::unique_by_key 可能是解决方案。您能否发布您的串行解决方案的链接?
  • 我不认为thrust::unique_by_key 会以简单的方式工作,因为它通过查看相邻元素来完成独特的步骤。解决方案需要查看所有可能的配对。

标签: cuda thrust


【解决方案1】:

我认为您可以将thrust::unique 与谓词一起使用,如Thrust: Removing duplicates in key-value arrays 所示。 实际上,我们可以做到这一点是因为unique的以下特点:

对于范围 [first, last) 中具有相同值的每一组连续元素,unique 会删除该组的除第一个元素之外的所有元素。

因此,您应该定义一个谓词来测试 -相等性,对于具有相同键且第一个元组中所有值都较小的元组,该谓词将返回 true

    typedef thrust::tuple<int, int, int, int> tuple_t;
    // a functor which defines your *uniqueness* condition
    struct tupleEqual
    {
      __host__ __device__
        bool operator()(tuple_t x, tuple_t y)
        {
          return ( (x.get<0>() == y.get<0>()) // same key
              && (x.get<1>() <= y.get<1>())   // all values are smaller
              && (x.get<2>() <= y.get<2>()) 
              && (x.get<3>() <= y.get<3>()));
        }
    };

你必须将它应用到一个排序集合。这样,只有第一个元组(最小的)不会被删除。 在 V1、V2 或 V3 中具有相同键和更大值的元组将产生 false,因此不会被删除。

    typedef thrust::device_vector< int >                IntVector;
    typedef IntVector::iterator                         IntIterator;
    typedef thrust::tuple< IntIterator, IntIterator, IntIterator, IntIterator >   IntIteratorTuple;
    typedef thrust::zip_iterator< IntIteratorTuple >    ZipIterator;

    IntVector keyVector;
    IntVector valVector1, valVector2, valVector3;

    tupleEqual predicate;
    ZipIterator newEnd = thrust::unique(
        thrust::make_zip_iterator(
            thrust::make_tuple(
                keyVector.begin(),
                valVector1.begin(),
                valVector2.begin(),
                valVector3.begin() ) ),
        thrust::make_zip_iterator(
            thrust::make_tuple(
                keyVector.end(),
                valVector1.end(),
                valVector2.end(),
                valVector3.end() ) ),
        predicate );

    IntIteratorTuple endTuple = newEnd.get_iterator_tuple();

    keyVector.erase( thrust::get<0>( endTuple ), keyVector.end() );
    valVector1.erase( thrust::get<1>( endTuple ), valVector1.end() );
    valVector2.erase( thrust::get<2>( endTuple ), valVector2.end() );
    valVector3.erase( thrust::get<3>( endTuple ), valVector3.end() );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 2012-04-07
    • 1970-01-01
    • 2021-08-20
    • 2020-11-17
    • 2011-12-20
    • 1970-01-01
    • 2014-05-14
    相关资源
    最近更新 更多