使用推力它简单明了到求和行的交织(即支持由矢量)阵列,如图的例子这里。
我想做的是对数组的列求和。
我尝试使用类似的构造,即:
// 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
行,那么我们可以看到R
,在reduce_by_key
操作过程中,由推力创建的第一个线程将全部读取矩阵的第一个“行” 。由于与第一行关联的内存位置全部分组在一起,因此可以合并访问。
解决此问题(如何对列求和)的一种方法是使用与行求和示例类似的策略,但使用apermutation_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快得多。
如果您不熟悉置换迭代器,请参阅推力快速入门指南。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句