线性化矩阵CUDA中的唯一行

Unique rows from Linearized Matrix CUDA

本文关键字:唯一 CUDA 线性化      更新时间:2023-10-16

我有一个线性化矩阵,存储为thrust::device_vector<int>

本质上,它是存储在这种大小的线性数组中的一个维数为nc x nv的矩阵。

我想从这个矩阵中得到唯一的行。如果至少有一个元素不同,则两行是唯一的。

我想使用CUDA thrust::sortthrust::unique函数来执行此操作。我认为我需要构造一个对应于每一行的迭代器,然后用一个按元素比较行的函子调用sort。但我不清楚如何做到这一点。

使用跨步范围迭代器可以指定每行的开头,但函数的实现尚不清楚。

这似乎是一个应该用推力来解决的问题。有更好的方法吗?

我认为你的方法是可行的。与其直接对矩阵进行排序,我建议对单独的行索引数组进行排序,以便按照矩阵行的排序顺序对得到的行索引进行排序。

我们将创建一个排序函子,该函子采用两个行索引,并使用这些行索引到矩阵的适当行中。然后,该排序函子将使用逐元素比较对指示的两行进行排序。

对于传递给thrust::unique的"equality"函子,我们将使用类似的方法(传递两个行索引)。然后,相等函子将测试指示的两行是否相等。我本可以像在排序函子中那样在这里使用for循环,逐元素测试相等性,但为了多样性,我选择使用嵌套的thrust::mismatch算法。

下面是一个工作示例:

$ cat t1033.cu
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/unique.h>
#include <thrust/sequence.h>
#include <assert.h>
#include <iostream>
#include <thrust/execution_policy.h>
#include <thrust/mismatch.h>

typedef int mytype;
struct my_sort_func
{
  int cols;
  mytype *data;
  my_sort_func(int _cols, mytype *_data) : cols(_cols),data(_data) {};
  __host__ __device__
  bool operator()(int r1, int r2){
    for (int i = 0; i < cols; i++){
      if (data[cols*r1+i] < data[cols*r2+i])
        return true;
      else if (data[cols*r1+i] > data[cols*r2+i])
        return false;}
    return false;
    }
};
struct my_unique_func
{
  int cols;
  mytype *data;
  my_unique_func(int _cols, mytype *_data) : cols(_cols),data(_data) {};
  __device__
  bool operator()(int r1, int r2){
    thrust::pair<mytype *, mytype *> res = thrust::mismatch(thrust::seq, data+(r1*cols), data+(r1*cols)+cols, data+(r2*cols));
    return (res.first == data+(r1*cols)+cols);
    }
};
int main(){
  const int ncols = 3;
  mytype data[] = { 1, 2, 3, 1, 2, 3, 1, 3, 5, 2, 3, 4, 1, 2, 3, 1, 3, 5};
  size_t dsize = sizeof(data)/sizeof(mytype);
  assert ((dsize % ncols) == 0);
  int nrows = dsize/ncols;
  thrust::device_vector<mytype> d_data(data, data+dsize);
  thrust::device_vector<int> rowidx(nrows);  // reference rows by their index
  thrust::sequence(rowidx.begin(), rowidx.end());
  thrust::sort(rowidx.begin(), rowidx.end(), my_sort_func(ncols, thrust::raw_pointer_cast(d_data.data())));
  int rsize = thrust::unique(rowidx.begin(), rowidx.end(), my_unique_func(ncols, thrust::raw_pointer_cast(d_data.data()))) - rowidx.begin();
  thrust::host_vector<int> h_rowidx = rowidx;
  std::cout << "Unique rows: " << std::endl;
  for (int i = 0; i < rsize; i++){
    for (int j = 0; j < ncols; j++) std::cout << data[h_rowidx[i]*ncols+j] << ",";
    std::cout << std::endl;}
  return 0;
}
$ nvcc -o t1033 t1033.cu
$ ./t1033
Unique rows:
1,2,3,
1,3,5,
2,3,4,
$

注:

  1. 我怀疑,如果输入矩阵被转置,并且我们比较的是列(在转置矩阵中)而不是行,那么整体性能会提高。它可能为分拣操作提供一些好处,我怀疑它也可能为独特的操作提供一些益处。然而,给定的代码与您在问题中的描述相匹配,它应该是在列案例中如何做到这一点的一个很好的路线图,尽管为此必须进行重构。

  2. 此方法实际上不会对矩阵行重新排序。为了提高效率,我想避免做大量的数据移动,因为问题陈述似乎并不依赖于它。如果你真的想要一个矩阵行按排序的中间数据集,我仍然建议你做上面的排序操作,然后用结果在一个操作中重新排列矩阵,使用两种可能的方法之一:散射/聚集操作,或thrust::permuation_iteratorthrust::copy操作相结合。

  3. 如果稍微聪明一点,嵌套的thrust::mismatch操作也可以用在排序函子中,代替for循环。