2012-02-15 124 views
4

在GPU上的一些计算,我需要在一个矩阵是按比例的行,使得在一个给定的行总和的所有元素为1。缩放矩阵的行与CUDA

 
| a1,1 a1,2 ... a1,N | | alpha1*a1,1 alpha1*a1,2 ... alpha1*a1,N | 
| a2,1 a2,2 ... a2,N | => | alpha2*a2,1 alpha2*a2,2 ... alpha2*a2,N | 
| .   . | | .        . | 
| aN,1 aN,2 ... aN,N | | alphaN*aN,1 alphaN*aN,2 ... alphaN*aN,N | 

其中

 
alphai = 1.0/(ai,1 + ai,2 + ... + ai,N) 

我需要alpha的向量和缩放矩阵,我想在尽可能少的blas调用中做到这一点。代码将在nvidia CUDA硬件上运行。有谁知道有什么聪明的方法来做到这一点?

回答

2

如果您使用带单位矢量的BLAS gemv,结果将是您需要的缩放因子倒数的向量(1/alpha)。这是很容易的部分。

因为标准BLAS没有像您可以使用的Hadamard产品操作符那样的东西,所以将这些因素按行应用有点困难。另外,因为您提到了BLAS,我认为您正在为矩阵使用列主要顺序存储,这对于行方式操作来说并不那么简单。 真的很慢这样做的方式是BLAS scal每行有一个音高,但这需要每行一个BLAS调用由于合并效果和L1缓存一致性,倾斜的内存访问会导致性能下降。

我的建议是使用自己的内核进行第二次操作。它不必是那么复杂,也许只有这样的事:

template<typename T> 
__global__ void rowscale(T * X, const int M, const int N, const int LDA, 
          const T * ralpha) 
{ 
    for(int row=threadIdx.x; row<M; row+=gridDim.x) { 
     const T rscale = 1./ralpha[row]; 
     for(int col=blockIdx.x; col<N; col+=blockDim.x) 
      X[row+col*LDA] *= rscale; 
    } 
} 

这只是有一堆通过排纵列踩着块,缩放,因为他们走。应适用于任何大小的列主要有序矩阵。内存访问应该被合并,但取决于你对性能的担忧程度,你可以尝试一些优化。它至少给出了要做什么的总体思路。

+0

这就是我自己得出的结论(对于整个行与列,如果一个比另一个更好,我将重新排列我的数据 - 转置在这里我来:) – 2012-02-15 13:29:00

+0

@MartinKristiansen:没有'除了一个简单的,纯粹面向行的缩放操作(即逐行'scal')在列主要顺序数据上不能很好地执行,因为行条目的跨度(至少矩阵的高度)。但是一个设计合理的方案在列主要数据上的表现也将与列主要数据一样好。 – talonmies 2012-02-15 13:48:22

5

Cublas 5.0引入了一个叫做cublas(Type)dgmm的类似Blas的例程,它是矩阵乘对角线矩阵(用向量表示)的乘法。

有一个左边的选项(它将缩放行)或右边的选项来缩放列。

有关详细信息,请参阅CUBLAS 5.0文档。

因此,在您的问题中,您需要创建一个包含GPU上的所有alpha的矢量,并使用带有左边选项的cublasdgmm。

+0

该死的,我在20天前递交了我的论文..也许我会在防守中提及它:-)谢谢。 – 2012-09-28 09:27:24

2

我想更新上面的答案,并考虑使用CUDA Thrust的thrust::transformcuBLAScublas<t>dgmm。我跳过比例的计算系数alpha的,因为这已经在

Reduce matrix rows with CUDA

被已办理

Reduce matrix columns with CUDA

下面是一个完整的例子:

#include <thrust/device_vector.h> 
#include <thrust/reduce.h> 
#include <thrust/random.h> 
#include <thrust/sort.h> 
#include <thrust/unique.h> 
#include <thrust/equal.h> 

#include <cublas_v2.h> 

#include "Utilities.cuh" 
#include "TimingGPU.cuh" 

/**************************************************************/ 
/* CONVERT LINEAR INDEX TO ROW INDEX - NEEDED FOR APPROACH #1 */ 
/**************************************************************/ 
template <typename T> 
struct linear_index_to_row_index : public thrust::unary_function<T,T> { 

    T Ncols; // --- Number of columns 

    __host__ __device__ linear_index_to_row_index(T Ncols) : Ncols(Ncols) {} 

    __host__ __device__ T operator()(T i) { return i/Ncols; } 
}; 

/***********************/ 
/* RECIPROCAL OPERATOR */ 
/***********************/ 
struct Inv: public thrust::unary_function<float, float> 
{ 
    __host__ __device__ float operator()(float x) 
    { 
     return 1.0f/x; 
    } 
}; 

/********/ 
/* MAIN */ 
/********/ 
int main() 
{ 
    /**************************/ 
    /* SETTING UP THE PROBLEM */ 
    /**************************/ 

    const int Nrows = 10;   // --- Number of rows 
    const int Ncols = 3;   // --- Number of columns 

    // --- Random uniform integer distribution between 0 and 100 
    thrust::default_random_engine rng; 
    thrust::uniform_int_distribution<int> dist1(0, 100); 

    // --- Random uniform integer distribution between 1 and 4 
    thrust::uniform_int_distribution<int> dist2(1, 4); 

    // --- Matrix allocation and initialization 
    thrust::device_vector<float> d_matrix(Nrows * Ncols); 
    for (size_t i = 0; i < d_matrix.size(); i++) d_matrix[i] = (float)dist1(rng); 

    // --- Normalization vector allocation and initialization 
    thrust::device_vector<float> d_normalization(Nrows); 
    for (size_t i = 0; i < d_normalization.size(); i++) d_normalization[i] = (float)dist2(rng); 

    printf("\n\nOriginal matrix\n"); 
    for(int i = 0; i < Nrows; i++) { 
     std::cout << "[ "; 
     for(int j = 0; j < Ncols; j++) 
      std::cout << d_matrix[i * Ncols + j] << " "; 
     std::cout << "]\n"; 
    } 

    printf("\n\nNormlization vector\n"); 
    for(int i = 0; i < Nrows; i++) std::cout << d_normalization[i] << "\n"; 

    TimingGPU timerGPU; 

    /*********************************/ 
    /* ROW NORMALIZATION WITH THRUST */ 
    /*********************************/ 

    thrust::device_vector<float> d_matrix2(d_matrix); 

    timerGPU.StartCounter(); 
    thrust::transform(d_matrix2.begin(), d_matrix2.end(), 
         thrust::make_permutation_iterator(
           d_normalization.begin(), 
           thrust::make_transform_iterator(thrust::make_counting_iterator(0), linear_index_to_row_index<int>(Ncols))), 
         d_matrix2.begin(), 
         thrust::divides<float>()); 
    std::cout << "Timing - Thrust = " << timerGPU.GetCounter() << "\n"; 

    printf("\n\nNormalized matrix - Thrust case\n"); 
    for(int i = 0; i < Nrows; i++) { 
     std::cout << "[ "; 
     for(int j = 0; j < Ncols; j++) 
      std::cout << d_matrix2[i * Ncols + j] << " "; 
     std::cout << "]\n"; 
    } 

    /*********************************/ 
    /* ROW NORMALIZATION WITH CUBLAS */ 
    /*********************************/ 
    d_matrix2 = d_matrix; 

    cublasHandle_t handle; 
    cublasSafeCall(cublasCreate(&handle)); 

    timerGPU.StartCounter(); 
    thrust::transform(d_normalization.begin(), d_normalization.end(), d_normalization.begin(), Inv()); 
    cublasSafeCall(cublasSdgmm(handle, CUBLAS_SIDE_RIGHT, Ncols, Nrows, thrust::raw_pointer_cast(&d_matrix2[0]), Ncols, 
        thrust::raw_pointer_cast(&d_normalization[0]), 1, thrust::raw_pointer_cast(&d_matrix2[0]), Ncols)); 
    std::cout << "Timing - cuBLAS = " << timerGPU.GetCounter() << "\n"; 

    printf("\n\nNormalized matrix - cuBLAS case\n"); 
    for(int i = 0; i < Nrows; i++) { 
     std::cout << "[ "; 
     for(int j = 0; j < Ncols; j++) 
      std::cout << d_matrix2[i * Ncols + j] << " "; 
     std::cout << "]\n"; 
    } 

    return 0; 
} 

Utilities.cuUtilities.cuh文件被挡住了here并在此处省略。 TimingGPU.cuTimingGPU.cuh保持为here并且也被省略。

我已经测试上的开普勒K20C上面的代码,并且这些是结果:

    Thrust  cuBLAS 
2500 x 1250  0.20ms  0.25ms 
5000 x 2500  0.77ms  0.83ms 

cuBLAS定时,我不包括cublasCreate时间。即使如此,CUDA Thrust版本似乎更方便。