[英]CUDA/Thrust: How to sum the columns of an interleaved array?
使用推力它简单明了总结交错阵列(即,由一个矢量支持)的行 ,如示例中所示此处 。
我想做的是对数组的列求和。
我尝试使用类似的构造,即:
// 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>());
但是,这只会导致第一列的总和,其余的将被忽略。 我对为什么会发生这种情况的猜测是,如reduce_by_key
docs中所述 :
对于[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
行,那么我们可以看到在reduce_by_key
操作期间,由推力创建的前R
线程将全部读取矩阵的第一个“行”。 由于与第一行关联的内存位置全部分组在一起,因此我们获得了合并访问。
解决此问题(如何对列求和)的一种方法是使用与行求和示例相似的策略,但使用permutation_iterator
来使所有属于同一键序列的线程读取一列数据而不是一行数据。 此置换迭代器将采用基础数组以及映射序列。 此映射序列是由transform_iterator
使用应用于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
$
显然,对于足够大的矩阵,方法2比方法1快得多。
如果您不熟悉置换迭代器,请查看推力快速入门指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.