我想编写一个矩阵乘法算法,基于 CUDA 的共享内存示例,它同时执行计算和数据加载。我的代码如下所示:
float As[BLOCK_SIZE][BLOCK_SIZE];
float Bs[BLOCK_SIZE][BLOCK_SIZE];
As[ty][tx] = A[aBegin + wA * ty + tx];
Bs[ty][tx] = B[bBegin + wB * ty + tx];
for (int a = aBegin, b = bBegin; a <= aEnd; a += aStep, b += bStep)
{
__shared__ float A2s[BLOCK_SIZE][BLOCK_SIZE];
__shared__ float B2s[BLOCK_SIZE][BLOCK_SIZE];
A2s[ty][tx] = As[ty][tx];
B2s[ty][tx] = Bs[ty][tx];
__syncthreads();
if (a+1 <= aEnd)
{
As[ty][tx] = A[a+1 + wA * ty + tx];
Bs[ty][tx] = B[b+1 + wB * ty + tx];
}
#pragma unroll
for (int k = 0; k < BLOCK_SIZE; ++k)
{
Csub += A2s[ty][k] * B2s[k][tx];
}
__syncthreads();
}
但它的工作速度比原始解决方案慢,因为第二次数据加载是随着计算顺序执行的。我怎样才能使并行?