我认为你的方法是可行的。我建议对单独的行索引数组进行排序,而不是直接对矩阵进行排序,以使生成的行索引按照矩阵行的排序顺序进行排序。
我们将创建一个排序仿函数,它接受两个行索引,并使用它们来索引到矩阵的适当行。然后,该排序函子将使用逐个元素的比较对指示的两个行进行排序。
对于传递给thrust::unique 的“相等”函子,我们将使用类似的方法(传递两个行索引)。然后相等函子将测试两个指示的行是否相等。我本可以在这里使用 for 循环,就像在 sort 函子中一样,逐个元素地测试相等性,但我选择使用嵌套的 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,
$
注意事项:
我怀疑如果输入矩阵被转置,整体性能会提高,并且我们比较的是列(在转置矩阵中)而不是行。它可能为排序操作提供一些好处,我怀疑它也可能为独特操作提供一些好处。然而,给定的代码与您在问题中的描述相匹配,它应该是如何在列案例中执行此操作的一个很好的路线图,尽管它必须为此进行重构。
此方法实际上并不重新排序矩阵行。为了提高效率,我想避免做大量的数据移动,因为问题陈述似乎并不依赖于它。如果您确实想要一个具有排序顺序的矩阵行的中间数据集,我仍然建议执行上述排序操作,然后使用结果在单个操作中重新排序矩阵,使用两种可能的方法之一: 分散/聚集操作,或 thrust::permuation_iterator 与 thrust::copy 操作的组合。
稍微聪明点,也可以在排序函子中使用嵌套的thrust::mismatch 操作来代替 for 循环。