Bar*_*Bar 2 c++ arrays cuda thrust
使用推力它简单明了到求和行的交织(即支持由矢量)阵列,如图的例子这里。
我想做的是对数组的列求和。
我尝试使用类似的结构,即:
// convert a linear index to a column index
template <typename T>
struct linear_index_to_col_index : public thrust::unary_function<T,T>
{
T C; // number of columns
__host__ __device__
linear_index_to_col_index(T C) : C(C) {}
__host__ __device__
T operator()(T i)
{
return i % C;
}
};
// allocate storage for column sums and indices
thrust::device_vector<int> col_sums(C);
thrust::device_vector<int> col_indices(C);
// compute row sums by summing values with equal row indices
thrust::reduce_by_key
(thrust::make_transform_iterator(thrust::counting_iterator<int>(0), linear_index_to_col_index<int>(C)),
thrust::make_transform_iterator(thrust::counting_iterator<int>(0), linear_index_to_col_index<int>(C)) + (R*C),
array.begin(),
col_indices.begin(),
col_sums.begin(),
thrust::equal_to<int>(),
thrust::plus<int>());
Run Code Online (Sandbox Code Playgroud)
然而,这导致仅对第一列求和,其余的将被忽略。我对为什么会发生这种情况的猜测是,如reduce_by_key 文档中所述:
对于[keys_first, keys_last) 范围内的每组连续键,reduce_by_key 将组的第一个元素复制到 keys_output。[强调我的]
如果我的理解是正确的,因为行迭代器中的键是连续的(即索引 [0 - (C-1)] 将给出 0,然后 [C - (2C-1)] 将给出 1,依此类推),它们最终被总结在一起。
但是列迭代器会将索引 [0 - (C-1)] 映射到 [0 - (C-1)] 然后重新开始,索引 [C - (2C-1)] 将被映射到 [0 - (C -1)] 等,使产生的值不连续。
这种行为对我来说是不直观的,我希望分配给同一键的所有数据点都组合在一起,但这是另一个讨论。
无论如何,我的问题是:如何使用 Thrust 对交错数组的列求和?
这些操作(对行求和、对列求和等)通常在 GPU 上受内存带宽限制。因此,我们可能要考虑如何构建一种算法来优化利用 GPU 显存带宽。特别是,如果可能的话,我们希望合并从推力代码生成的底层内存访问。简而言之,这意味着相邻的 GPU 线程将从内存中的相邻位置读取。
原始行求和示例显示了此属性:推力产生的相邻线程将读取内存中的相邻元素。例如,如果我们有R行,那么我们可以看到R,在reduce_by_key操作期间,推力创建的第一个线程都将读取矩阵的第一“行” 。由于与第一行关联的内存位置都组合在一起,因此我们可以合并访问。
解决此问题(如何对列求和)的一种方法是使用与行求和示例类似的策略,但使用 apermutation_iterator使所有属于同一键序列的线程读取一列数据的行数据。这个置换迭代器将采用底层数组和映射序列。该映射序列是通过 atransform_iterator使用应用于a 的特殊函子创建的counting_iterator,将线性(行主)索引转换为列主索引,以便第一个C线程将读取矩阵第一列的元素,而不是第一行。从第一C线程将属于相同的键序列,它们将在reduce_by_key操作中相加。这就是我在下面的代码中所说的方法 1。
然而,这种方法的缺点是相邻线程不再读取内存中的相邻值——我们已经破坏了合并,正如我们将看到的,性能影响是显而易见的。
对于以行优先顺序存储在内存中的大型矩阵(我们在本问题中讨论的排序),对列求和的一种相当理想的方法是让每个线程使用 for 循环对单个列求和。这在 CUDA C 中实现起来相当简单,我们可以类似地在 Thrust 中使用适当定义的函子执行此操作。
我在下面的代码中将其称为方法 2。此方法只会启动与矩阵中的列一样多的线程。对于具有足够多列(例如 10,000 或更多)的矩阵,此方法将使 GPU 饱和并有效地使用可用的内存带宽。如果您检查函子,您会发现这是推力的某种“不寻常”改编,但完全合法。
这是比较两种方法的代码:
$ cat t994.cu
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
#include <iostream>
#define NUMR 1000
#define NUMC 20000
#define TEST_VAL 1
#include <time.h>
#include <sys/time.h>
#define USECPSEC 1000000ULL
long long dtime_usec(unsigned long long start){
timeval tv;
gettimeofday(&tv, 0);
return ((tv.tv_sec*USECPSEC)+tv.tv_usec)-start;
}
typedef int mytype;
// from a linear (row-major) index, return column-major index
struct rm2cm_idx_functor : public thrust::unary_function<int, int>
{
int r;
int c;
rm2cm_idx_functor(int _r, int _c) : r(_r), c(_c) {};
__host__ __device__
int operator() (int idx) {
unsigned my_r = idx/c;
unsigned my_c = idx%c;
return (my_c * r) + my_r;
}
};
// convert a linear index to a column index
template <typename T>
struct linear_index_to_col_index : public thrust::unary_function<T,T>
{
T R; // number of rows
__host__ __device__
linear_index_to_col_index(T R) : R(R) {}
__host__ __device__
T operator()(T i)
{
return i / R;
}
};
struct sum_functor
{
int R;
int C;
mytype *arr;
sum_functor(int _R, int _C, mytype *_arr) : R(_R), C(_C), arr(_arr) {};
__host__ __device__
mytype operator()(int myC){
mytype sum = 0;
for (int i = 0; i < R; i++) sum += arr[i*C+myC];
return sum;
}
};
int main(){
int C = NUMC;
int R = NUMR;
thrust::device_vector<mytype> array(R*C, TEST_VAL);
// method 1: permutation iterator
// allocate storage for column sums and indices
thrust::device_vector<mytype> col_sums(C);
thrust::device_vector<int> col_indices(C);
// compute column sums by summing values with equal column indices
unsigned long long m1t = dtime_usec(0);
thrust::reduce_by_key(thrust::make_transform_iterator(thrust::counting_iterator<int>(0), linear_index_to_col_index<int>(R)),
thrust::make_transform_iterator(thrust::counting_iterator<int>(R*C), linear_index_to_col_index<int>(R)),
thrust::make_permutation_iterator(array.begin(), thrust::make_transform_iterator(thrust::make_counting_iterator<int>(0), rm2cm_idx_functor(R, C))),
col_indices.begin(),
col_sums.begin(),
thrust::equal_to<int>(),
thrust::plus<int>());
cudaDeviceSynchronize();
m1t = dtime_usec(m1t);
for (int i = 0; i < C; i++)
if (col_sums[i] != R*TEST_VAL) {std::cout << "method 1 mismatch at: " << i << " was: " << col_sums[i] << " should be: " << R*TEST_VAL << std::endl; return 1;}
std::cout << "Method1 time: " << m1t/(float)USECPSEC << "s" << std::endl;
// method 2: column-summing functor
thrust::device_vector<mytype> fcol_sums(C);
thrust::sequence(fcol_sums.begin(), fcol_sums.end()); // start with column index
unsigned long long m2t = dtime_usec(0);
thrust::transform(fcol_sums.begin(), fcol_sums.end(), fcol_sums.begin(), sum_functor(R, C, thrust::raw_pointer_cast(array.data())));
cudaDeviceSynchronize();
m2t = dtime_usec(m2t);
for (int i = 0; i < C; i++)
if (fcol_sums[i] != R*TEST_VAL) {std::cout << "method 2 mismatch at: " << i << " was: " << fcol_sums[i] << " should be: " << R*TEST_VAL << std::endl; return 1;}
std::cout << "Method2 time: " << m2t/(float)USECPSEC << "s" << std::endl;
return 0;
}
$ nvcc -O3 -o t994 t994.cu
$ ./t994
Method1 time: 0.034817s
Method2 time: 0.00082s
$
Run Code Online (Sandbox Code Playgroud)
很明显,对于足够大的矩阵,方法 2 比方法 1 快得多。
如果您不熟悉置换迭代器,请查看推力快速入门指南。