下面的代码计算C = A * B,如何将其展开以进行多平铺乘法? A的一行在每一个循环上乘以一列B。我的问题是如何修改它,使得A的一行与B的多列相乘,这样我们就可以避免再次为同一行重新加载它们。
(代码取自:Dynamic matrix multiplication with CUDA)
// CUDA Kernel
__global__ void matrixMul( float* C, float* A, float* B, int wA, int wB,size_t block_size)
{
int bx = blockIdx.x;
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;
int aBegin = wA * block_size * by;
int aEnd = aBegin + wA - 1;
int aStep = block_size;
int bBegin = block_size * bx;
int bStep = block_size * wB;
float Csub=0;
for (int a = aBegin, b = bBegin; a <= aEnd; a += aStep, b += bStep)
{
extern __shared__ float As[];
extern __shared__ float Bs[];
extern __shared__ float smem[];
smem[ty*block_size+tx] = A[a + wA * ty + tx];
smem[block_size*block_size+ty*block_size+tx] = B[b + wB * ty + tx];
__syncthreads();
for (int k = 0; k < block_size; ++k)
Csub += smem[ty*block_size+k] * smem[block_size*block_size+k*block_size+tx] ;
__syncthreads();
}
int c = wB * block_size * by + block_size * bx;
C[c + wB * ty + tx] = Csub;
}
由于
答案 0 :(得分:1)
Kirk和Hwu有一本书深入探讨了开发有效的矩阵乘法内核:
http://www.amazon.com/Programming-Massively-Parallel-Processors-Hands/dp/0123814723
此前,Stack Overflow上还有一些关于CUDA上平铺矩阵乘法的问题。请参阅以下内容: