2012-02-15 121 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版本似乎更方便。