Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 使用CUDA推力确定2个最大元素及其在每个矩阵行中的位置_Algorithm_Sorting_Cuda_Thrust - Fatal编程技术网

Algorithm 使用CUDA推力确定2个最大元素及其在每个矩阵行中的位置

Algorithm 使用CUDA推力确定2个最大元素及其在每个矩阵行中的位置,algorithm,sorting,cuda,thrust,Algorithm,Sorting,Cuda,Thrust,我有一个矩阵,我需要计算2最大数及其在该矩阵每行中的位置。我最初的尝试是对矩阵的每一行进行排序,然后查看最后两个值。虽然我可以对每一行进行排序,但我无法获得排列向量来获得原始索引。因此,我的尝试(在So上使用一些其他线程)如下所示: int my_mod_start = 0; int my_mod() { return (my_mod_start++)/10; } const int rows = 2; const int cols = 10; const int num_points

我有一个矩阵,我需要计算
2
最大数及其在该矩阵每行中的位置。我最初的尝试是对矩阵的每一行进行排序,然后查看最后两个值。虽然我可以对每一行进行排序,但我无法获得排列向量来获得原始索引。因此,我的尝试(在So上使用一些其他线程)如下所示:

int my_mod_start = 0;
int my_mod()
{
    return (my_mod_start++)/10;
}

const int rows = 2;
const int cols = 10;
const int num_points = rows * cols;

thrust::host_vector<float> data(num_points);
// fill with random values
thrust::device_vector<float> d_r = data;
thrust::host_vector<int> h_segments(rows*cols);
thrust::generate(h_segments.begin(), h_segments.end(), my_mod);

thrust::device_vector<int> d_segments = h_segments;
thrust::stable_sort_by_key(d_r.begin(), d_r.end(), d_segments.begin());
thrust::stable_sort_by_key(d_segments.begin(), d_segments.end(), 
                           d_r.begin());
int my_mod_start=0;
int my_mod()
{
返回(my_mod_start++)/10;
}
const int rows=2;
常数int cols=10;
const int num_points=行*列;
推力::主机向量数据(num_点);
//用随机值填充
推力::设备向量d\u r=数据;
推力:主机向量h_段(行*列);
生成(h_段.begin(),h_段.end(),my_mod);
推力::装置\矢量d \ U段=h \ U段;
推力::按键(d_.begin()、d_.end()、d_segments.begin())进行稳定的排序;
推力::按键稳定排序(d_段.begin(),d_段.end(),
d_r.begin());
虽然这种方法按预期对每一行进行排序,但我不确定如何修改它以获得每一个值的原始索引


我还想到,如果我只需要最大
2
值及其位置,那么对整行进行排序可能会浪费时间。

我采用了Robert Crovella在。该方法考虑了确定最小值而非最大值的问题,并生成两个迭代器和一个向量:

  • d_min_index_1
    :迭代器,指向每行最后一个元素的索引
  • d_min_index_2
    :迭代器,指向每行倒数第二个元素的索引
  • d_矩阵
    :原始矩阵,但每行按升序排列
  • 最后一个和倒数第二个元素的值可以通过有序矩阵
    d_矩阵
    确定

    #include <iterator>
    #include <algorithm>
    
    #include <thrust/random.h>
    #include <thrust/device_vector.h>
    #include <thrust/iterator/counting_iterator.h>
    #include <thrust/iterator/transform_iterator.h>
    #include <thrust/iterator/permutation_iterator.h>
    #include <thrust/iterator/zip_iterator.h>
    #include <thrust/iterator/discard_iterator.h>
    #include <thrust/reduce.h>
    #include <thrust/functional.h>
    #include <thrust/sort.h>
    
    template <typename Iterator>
    class strided_range
    {
        public:
    
        typedef typename thrust::iterator_difference<Iterator>::type difference_type;
    
        struct stride_functor : public thrust::unary_function<difference_type,difference_type>
        {
            difference_type stride;
    
            stride_functor(difference_type stride)
                : stride(stride) {}
    
            __host__ __device__
            difference_type operator()(const difference_type& i) const
            { 
                return stride * i;
            }
        };
    
        typedef typename thrust::counting_iterator<difference_type>                   CountingIterator;
        typedef typename thrust::transform_iterator<stride_functor, CountingIterator> TransformIterator;
        typedef typename thrust::permutation_iterator<Iterator,TransformIterator>     PermutationIterator;
    
        // type of the strided_range iterator
        typedef PermutationIterator iterator;
    
        // construct strided_range for the range [first,last)
        strided_range(Iterator first, Iterator last, difference_type stride)
            : first(first), last(last), stride(stride) {}
    
        iterator begin(void) const
        {
            return PermutationIterator(first, TransformIterator(CountingIterator(0), stride_functor(stride)));
        }
    
        iterator end(void) const
        {
            return begin() + ((last - first) + (stride - 1)) / stride;
        }
    
        protected:
        Iterator first;
        Iterator last;
        difference_type stride;
    };
    
    
    /**************************************************************/
    /* CONVERT LINEAR INDEX TO ROW INDEX - NEEDED FOR APPROACH #1 */
    /**************************************************************/
    template< typename T >
    struct mod_functor {
        __host__ __device__ T operator()(T a, T b) { return a % b; }
    };
    
    /********/
    /* MAIN */
    /********/
    int main()
    {
        /***********************/
        /* SETTING THE PROBLEM */
        /***********************/
        const int Nrows = 4;
        const int Ncols = 6;
    
        // --- Random uniform integer distribution between 10 and 99
        thrust::default_random_engine rng;
        thrust::uniform_int_distribution<int> dist(10, 99);
    
        // --- Matrix allocation and initialization
        thrust::device_vector<float> d_matrix(Nrows * Ncols);
        for (size_t i = 0; i < d_matrix.size(); i++) d_matrix[i] = (float)dist(rng);
    
        for(int i = 0; i < Nrows; i++) {
            std::cout << "[ ";
            for(int j = 0; j < Ncols; j++)
                std::cout << d_matrix[i * Ncols + j] << " ";
            std::cout << "]\n";
        }
    
        /******************/
        /* APPROACH NR. 2 */
        /******************/
        // --- Computing row indices vector
        thrust::device_vector<int> d_row_indices(Nrows * Ncols);
        thrust::transform(thrust::make_counting_iterator(0), thrust::make_counting_iterator(Nrows * Ncols), thrust::make_constant_iterator(Ncols), d_row_indices.begin(), thrust::divides<int>() );
    
        // --- Computing column indices vector
        thrust::device_vector<int> d_column_indices(Nrows * Ncols);
        thrust::transform(thrust::make_counting_iterator(0), thrust::make_counting_iterator(Nrows * Ncols), thrust::make_constant_iterator(Ncols), d_column_indices.begin(), mod_functor<int>());
    
        // --- int and float iterators
        typedef thrust::device_vector<int>::iterator        IntIterator;
        typedef thrust::device_vector<float>::iterator      FloatIterator;
    
        // --- Relevant tuples of int and float iterators
        typedef thrust::tuple<IntIterator, IntIterator>     IteratorTuple1;
        typedef thrust::tuple<FloatIterator, IntIterator>   IteratorTuple2;
    
        // --- zip_iterator of the relevant tuples
        typedef thrust::zip_iterator<IteratorTuple1>        ZipIterator1;
        typedef thrust::zip_iterator<IteratorTuple2>        ZipIterator2;
    
        // --- zip_iterator creation
        ZipIterator1 iter1(thrust::make_tuple(d_row_indices.begin(), d_column_indices.begin()));
    
        thrust::stable_sort_by_key(d_matrix.begin(), d_matrix.end(), iter1);
    
        ZipIterator2 iter2(thrust::make_tuple(d_matrix.begin(), d_column_indices.begin()));
    
        thrust::stable_sort_by_key(d_row_indices.begin(), d_row_indices.end(), iter2);
    
        typedef thrust::device_vector<int>::iterator Iterator;
    
        // --- Strided access to the sorted array
        strided_range<Iterator> d_min_indices_1(d_column_indices.begin(), d_column_indices.end(), Ncols);
        strided_range<Iterator> d_min_indices_2(d_column_indices.begin() + 1, d_column_indices.end() + 1, Ncols);
    
        printf("\n\n");
        for(int i = 0; i < Nrows; i++) {
            std::cout << "[ ";
            for(int j = 0; j < Ncols; j++)
                std::cout << d_matrix[i * Ncols + j] << " ";
            std::cout << "]\n";
        }
    
        printf("\n\n");
        std::copy(d_min_indices_1.begin(), d_min_indices_1.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    
        printf("\n\n");
        std::copy(d_min_indices_2.begin(), d_min_indices_2.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    模板
    类步距
    {
    公众:
    typedef typename推力::迭代器_差异::类型差异_类型;
    结构跨步函数:公共推力::一元函数
    {
    差异式步幅;
    步幅函子(差分型步幅)
    :步幅(步幅){}
    __主机设备__
    差分类型运算符()(常数差分类型&i)常数
    { 
    返回步幅*i;
    }
    };
    typedef typename推力::计数迭代器计数迭代器;
    typedef typename推力::transform_迭代器TransformIterator;
    typedef typename推力::置换迭代器置换迭代器;
    //跨步范围迭代器的类型
    typedef置换迭代器;
    //为范围[第一个,最后一个]构建跨步范围
    步幅范围(迭代器优先、迭代器最后、差分类型步幅)
    :第一(第一)、最后(最后)、大步(大步){}
    迭代器开始(void)常量
    {
    返回置换迭代器(第一个,TransformIterator(CountingIterator(0),stride_函子(stride));
    }
    迭代器结束(void)常量
    {
    return begin()+((last-first)+(stride-1))/stride;
    }
    受保护的:
    迭代器优先;
    迭代器last;
    差异式步幅;
    };
    /**************************************************************/
    /*将线性索引转换为行索引-方法#1所需*/
    /**************************************************************/
    模板
    结构模函子{
    __主机\uuuuuuuu设备\uuuuut运算符()(ta,tb){返回a%b;}
    };
    /********/
    /*主要*/
    /********/
    int main()
    {
    /***********************/
    /*设置问题*/
    /***********************/
    常数int Nrows=4;
    常数int Ncols=6;
    //---10到99之间的随机均匀整数分布
    推力:默认随机发动机转速;
    推力:均匀分布区(10,99);
    //---矩阵分配和初始化
    推力:设备矢量d矩阵(Nrows*Ncols);
    对于(size_t i=0;istd::你不能看一看吗?当然,你必须用行来更改列。@Jackolanten由于代码中的这个_1,我无法编译该代码?请阅读推力占位符。谷歌是你的朋友。占位符需要一定的最低级别的C/C++编译器功能,因此取决于你所使用的平台,它们无法工作(编译错误)。对于最新版本的gcc,如4.8.x,您应该不会遇到任何问题。如果您有较旧版本的gcc,如4.1.2,您必须跳过使用占位符。但是,任何占位符实现都可以替换为相应的推力函子实现。关于我链接的帖子,我认为您不需要Eric的方法,但是罗伯特·克罗维拉的。在这里,你不需要占位符。
    
    strided_range<Iterator> d_min_indices_1(d_column_indices.begin(), d_column_indices.end(), Ncols);
    strided_range<Iterator> d_min_indices_2(d_column_indices.begin() + 1, d_column_indices.end() + 1, Ncols);
    
    strided_range<Iterator> d_min_indices_1(d_column_indices.begin() + Ncols - 1, d_column_indices.end() + Ncols - 1, Ncols);
    strided_range<Iterator> d_min_indices_2(d_column_indices.begin() + Ncols - 2, d_column_indices.end() + Ncols - 2, Ncols);