2011-10-21 52 views
-1

串行代碼片段看起來是這樣的:CUDA乘法

int tid = blockIdx.x * blockDim.x + threadIdx.x; 
int i,j; 
for(tid = 0; tid <nx*ny; tid++) 
{ 
    j = tid/nx; 
    i = tid - j*nx; 
    x[tid] *= y[i]; 
} 

但是,GPU內核不提供任何加速的改進:

int i, j; 
for(j=0; j<ny; j++) 
{ 
    for(i=0; i<nx; i++) 
    { 
     x[i + j*nx] *= y[i]; 
    } 
} 

我使用這個內核轉換這CUDA?任何建議更好的解決方案?在此先感謝

+0

我,j?多久?你在網格中使用了多少塊? –

+2

CUDA代碼中存在相當可惡的錯字。你計算tid,然後扔掉計算的值,讓它取0和nx * ny之間的值。這幾乎可以肯定不是你想要做的......看到我的答案在下面。 – Patrick87

+1

毫不奇怪,它很慢 - 內核是完全串行的。每個線程都做同樣的事情! – talonmies

回答

-1

區塊有多大?可能需要將少量數據複製到GPU並設置環境的時間比計算時間長得多。

還請記住,CUDA在首次運行時執行jit編譯,因此要獲得準確的基準測試,您需要多次運行該基準測試。

+0

nx * ny = 2205;所以我沒有使用。塊=(nx * ny +(threads-1))/線程和線程= 64. – vivekv80

6

如果這是串行代碼:

int i, j; 
    for(j=0; j<ny; j++) 
    { 
     for(i=0; i<nx; i++) 
     { 
      x[i + j*nx] *= y[i]; 
     } 
    } 

,那麼你應該這樣做:

__global__ void fn(float *x, int nx) 
    { 
    int tid = blockIdx.x * blockDim.x + threadIdx.x; 
    int j = tid/nx, i = tid - j * nx; 
    x[tid] *= y[i]; 
    } 

    fn<<<nx*ny/B, B>>>(x, nx); // with B = 256, 512, etc. 

你在做什麼是很奇怪的:你指示CUDA的每個線程內核遍歷全部 tid在0和nx * ny之間的值,並計算與您的CPU版本相同的功能!而且,不是僅僅遍歷索引,而是實際上比CPU版本更有效地執行以下的更少;換句話說,你在每個線程中執行相同的操作,效率低於在CPU上的1個線程中執行的操作。難怪這比較慢;它應該慢很多。你的CUDA內核是:

int **tid** = blockIdx.x * blockDim.x + threadIdx.x; 
    int i,j; 
    for(**tid** = 0; **tid** <nx*ny; **tid**++) 
    { 
     j = tid/nx; 
     i = tid - j*nx; 
     x[tid] *= y[i]; 
    } 

這確實NX * NY迭代,與您的主機代碼,爲每個線程;你會失去並行的所有好處,因爲每個線程都在做同樣的事情;使用GPU上的一個線程可以獲得相同的性能,並獲得相同的結果!

如果這是CUDA源文件的逐字代碼,則需要對其進行更改並重新進行比較;如果這是您編寫的代碼以幫助解釋您的代碼爲非CUDA非專業觀衆所做的工作,那麼您需要展示您的實際CUDA代碼,以便我們可以看到發生了什麼......實際上,性能分析我已經完成了 - 微不足道的 - 是你所期望的。

-2

嘗試使用共享內存。圍繞一個最好的實現:

// Matrices are stored in row-major order: 
// M(row, col) = *(M.elements + row * M.stride + col) 
typedef struct { 
    int width; 
    int height; 
    int stride; // In number of elements 
    float *elements; 
} Matrix; 

// Thread block size 
#define BLOCK_SIZE 16 

// Get a matrix element 
__device__ float GetElement(const Matrix A, int row, int col) 
{ 
    return A.elements[row * A.stride + col]; 
} 

// Set a matrix element 
__device__ void SetElement(Matrix A, int row, int col, float value) 
{ 
    A.elements[row * A.stride + col] = value; 
} 
// Get the BLOCK_SIZExBLOCK_SIZE sub-matrix Asub of A that is 
// located col sub-matrices to the right and row sub-matrices down 
// from the upper-left corner of A 
__device__ Matrix GetSubMatrix(Matrix A, int row, int col) 
{ 
    Matrix Asub; 
    Asub.width = BLOCK_SIZE; Asub.height = BLOCK_SIZE; 
    Asub.stride = A.stride; 
    Asub.elements = &A.elements[A.stride * BLOCK_SIZE * row + 
           BLOCK_SIZE * col]; 
    return Asub; 
} 

// Forward declaration of the matrix multiplication kernel 
__global__ void MatMulKernel(const Matrix, const Matrix, Matrix); 

// Matrix multiplication - Host code 
// Matrix dimensions are assumed to be multiples of BLOCK_SIZE 
void MatMul(const Matrix A, const Matrix B, Matrix C) 
{ 
    // Same as in previous example, except the followings: 
    // d_A.width = d_A.stride = A.width; 
    // d_B.width = d_B.stride = B.width; 
    // d_C.width = d_C.stride = C.width; 
} 
// Matrix multiplication kernel called by MatMul() 
__global__ void MatMulKernel(Matrix A, Matrix B, Matrix C) 
{ 
    // Block row and column 
    int blockRow = blockIdx.y; 
    int blockCol = blockIdx.x; 

    // Each thread block computes one sub-matrix Csub of C 
    Matrix Csub = GetSubMatrix(C, blockRow, blockCol); 

    // Each thread computes one element of Csub 
    // by accumulating results into Cvalue 
    float Cvalue = 0; 

    // Thread row and column within Csub 
    int row = threadIdx.y; 
    int col = threadIdx.x; 
// Loop over all the sub-matrices of A and B that are 
    // required to compute Csub 
    // Multiply each pair of sub-matrices together 
    // and accumulate the results 
    for (int m = 0; m < (A.width/BLOCK_SIZE); ++m) 
    { 
     // Get sub-matrix Asub of A and Bsub of B 
     Matrix Asub = GetSubMatrix(A, blockRow, m); 
     Matrix Bsub = GetSubMatrix(B, m, blockCol); 

     // Shared memory used to store Asub and Bsub respectively 
     __shared__ float As[BLOCK_SIZE][BLOCK_SIZE]; 
     __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE]; 

     // Load Asub and Bsub from device memory to shared memory 
     // Each thread loads one element of each sub-matrix 
     As[row][col] = GetElement(Asub, row, col); 
     Bs[row][col] = GetElement(Bsub, row, col); 

     // Synchronize to make sure the sub-matrices are loaded 
     // before starting the computation 
     __syncthreads(); 
     // Multiply Asub and Bsub together 
     for (int e = 0; e < BLOCK_SIZE; ++e) 
     Cvalue += As[row][e] * Bs[e][col]; 

     // Synchronize to make sure that the preceding 
     // computation is done before loading two new 
     // sub-matrices of A and B in the next iteration 
     __syncthreads(); 
    } 

    // Write Csub to device memory 
    // Each thread writes one element 
    SetElement(Csub, row, col, Cvalue); 
} 
+0

此代碼不執行Questioner詢問的操作。 – talonmies

2

this answer您的評論:

的NX * NY = 2205;所以我沒有使用。塊= (nx * ny +(線程-1))/線程和線程= 64。

是暗示你正打算推出每計算一個線程,正確的CUDA執行僅是:

int tid = blockIdx.x * blockDim.x + threadIdx.x; 
int j = tid/nx; 
int i = tid - j*nx; 

if (tid < (nx*ny)) 
    x[tid] *= y[i]; 

如果你打算爲每個線程計算每個內核啓動一個以上的計算,那麼你將大小網格以「補」每個SM的目標GPU,不要使用相同數量的線程作爲輸入尺寸,然後像做:

int tid = blockIdx.x * blockDim.x + threadIdx.x; 
int gsize = blockDim.x * gridDim.x; 
int i,j; 

for(; tid <nx*ny; tid+=gsize) 
{ 
    j = tid/nx; 
    i = tid - j*nx; 
    x[tid] *= y[i]; 
} 

這將讓你一個至少對x進行合併讀寫操作,並刪除發佈版本中的大量冗餘計算。還有很多可以進行的優化,但需要更多關於問題的信息,而不是問題中提供的信息和隨後的評論。您的索引方案包含一個整數除法,然後每個計算包含一個整數乘加。對於每個輸入值的單個FLOP來說,這是很大的開銷。然而,儘管如此,如果我所引用的問題大小是您感興趣的實際問題大小,那麼即使是適度的主機CPU,GPU也永遠不會更快。如果使用GPU實現這種低算術強度操作,您將需要許多數量級較大的問題來實現有用的加速。

+0

當我運行一個2205元素的小案例時,我看到這個在Visual Profiler中調用的內核。但是,對於大約2000萬個元素的非常大的情況,我沒有看到在分析器中調用的內核。所以,我試圖通過使用合併的讀取訪問方法來驗證執行時間是否有所改進,正如您在原始實現中所描述的那樣。有什麼想法嗎?我正在使用C2070 btw,並且我正在使用CUDA_SAFE_CALL來確保cudaMemcopy在數據集未填充到GPU上時會給我一個錯誤。 – vivekv80

+0

@ vivekv80:編輯一個你的實現的更完整的描述(最好是一個*簡潔的* repro的情況下)到你原來的問題,有人可能會看看它。 – talonmies

+0

可以用共享內存來實現嗎? – vivekv80