使用CUDA的矩阵行中每个元素的排名 [英] Rank of each element in a matrix row using CUDA

查看:256
本文介绍了使用CUDA的矩阵行中每个元素的排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用CUDA或NVidia提供的相同功能在矩阵行中单独查找元素的行列?

Is there any way to find the rank of element in a matrix row separately using CUDA or any functions for the same provided by NVidia?

推荐答案

我不知道CUDA或我熟悉的任何库中的内置排名或argsort函数。

I don't know of a built-in ranking or argsort function in CUDA or any of the libraries I am familiar with.

您当然可以构建

这里是使用推力的一种可能解决方案的(未优化的)轮廓:

Here is a (non-optimized) outline of a possible solution approach using thrust:

$ cat t84.cu
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/sort.h>
#include <thrust/sequence.h>
#include <thrust/functional.h>
#include <thrust/adjacent_difference.h>
#include <thrust/transform.h>
#include <thrust/iterator/permutation_iterator.h>
#include <iostream>

typedef int mytype;

struct clamp
{
  template <typename T>
  __host__ __device__
  T operator()(T data){
    if (data == 0) return 0;
    return 1;}
};

int main(){

  mytype data[]  = {4,1,7,1};
  int dsize = sizeof(data)/sizeof(data[0]);
  thrust::device_vector<mytype> d_data(data, data+dsize);
  thrust::device_vector<int> d_idx(dsize);
  thrust::device_vector<int> d_result(dsize);

  thrust::sequence(d_idx.begin(), d_idx.end());

  thrust::sort_by_key(d_data.begin(), d_data.end(), d_idx.begin(), thrust::less<mytype>());
  thrust::device_vector<int> d_diff(dsize);
  thrust::adjacent_difference(d_data.begin(), d_data.end(), d_diff.begin());
  d_diff[0] = 0;
  thrust::transform(d_diff.begin(), d_diff.end(), d_diff.begin(), clamp());
  thrust::inclusive_scan(d_diff.begin(), d_diff.end(), d_diff.begin());

  thrust::copy(d_diff.begin(), d_diff.end(), thrust::make_permutation_iterator(d_result.begin(), d_idx.begin()));
  thrust::copy(d_result.begin(), d_result.end(), std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl;
}

$ nvcc -arch=sm_61 -o t84 t84.cu
$ ./t84
1,0,2,0,
$

这篇关于使用CUDA的矩阵行中每个元素的排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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