线性化矩阵CUDA中的唯一行
Unique rows from Linearized Matrix CUDA
我有一个线性化矩阵,存储为thrust::device_vector<int>
本质上,它是存储在这种大小的线性数组中的一个维数为nc x nv的矩阵。
我想从这个矩阵中得到唯一的行。如果至少有一个元素不同,则两行是唯一的。
我想使用CUDA thrust::sort
和thrust::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,
$
注:
我怀疑,如果输入矩阵被转置,并且我们比较的是列(在转置矩阵中)而不是行,那么整体性能会提高。它可能为分拣操作提供一些好处,我怀疑它也可能为独特的操作提供一些益处。然而,给定的代码与您在问题中的描述相匹配,它应该是在列案例中如何做到这一点的一个很好的路线图,尽管为此必须进行重构。
此方法实际上不会对矩阵行重新排序。为了提高效率,我想避免做大量的数据移动,因为问题陈述似乎并不依赖于它。如果你真的想要一个矩阵行按排序的中间数据集,我仍然建议你做上面的排序操作,然后用结果在一个操作中重新排列矩阵,使用两种可能的方法之一:散射/聚集操作,或
thrust::permuation_iterator
与thrust::copy
操作相结合。如果稍微聪明一点,嵌套的
thrust::mismatch
操作也可以用在排序函子中,代替for循环。
- 编译时未启用intel oneApi CUDA支持
- 何时在引用或唯一指针上使用移动语义
- 在cuda线程之间共享大量常量数据
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 计算排序向量的向量中唯一值的计数
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- CUDA内核和数学函数的显式命名空间
- 通过组合不同的类型来创建唯一的id
- 使用Unique_ptr确保工厂中的对象唯一
- c++多进程编写一个唯一的文件
- CUDA:统一内存和指针地址的更改
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 调试 CUDA MMU 故障
- 如何更改唯一指针向量的可见性
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- C++尝试深度复制唯一指针时出现内存访问冲突
- 具有引用成员的结构是否具有唯一的对象表示形式