cuda编程[5]:矩阵乘法--使用静态共享内存优化

目录

前言

  所有的代码下载链接:https://github.com/leoda1/the-notes-of-cuda-programming/tree/main/code/matmul-shared-memory

项目结构

  • 📂 .vscode
  • 📂 build
  • 📂 inc
    • 📄 matmul.hpp - 矩阵乘法相关的头文件
    • 📄 timer.hpp - 计时器相关的头文件
    • 📄 utils.hpp - 工具类函数相关的头文件
  • 📂 src
    • 📄 main.cpp - 主程序文件
    • 📄 matmul_gpu_basic.cu - 基础 GPU 矩阵乘法 CUDA 实现
    • 📄 matmul_gpu_shared.cu - 使用共享内存的 GPU 矩阵乘法 CUDA 实现
    • 📄 timer.cpp - 计时器实现文件
    • 📄 utils.cpp - 工具类函数实现文件
  • 📄 CMakeLists.txt - CMake 构建配置文件

核心代码

  这里的普通矩阵乘法的matmul_gpu_basic.cu代码我做了详细的视频解读,视频链接:视频时长32:00,建议2x食用。那么怎么使用静态共享内存来实现矩阵乘法呢?
   首先,共享内存空间比局部和全局内存空间更快,对于一个warp 的所有线程,访问共享内存和访问寄存器一样快,只要在线程之间没有bank 冲突(这里会在cuda编程[7]说到)。定义静态共享内存的修饰词是__shared__
  矩阵从host端cudaMemcpy到device端后,需要再将A和B矩阵切片(矩阵相乘是A的行乘和累加B的列,所以可以将矩阵切片。使用Tiling技术一个tile处理的就是block, 将一个矩阵分为多个小的tile)放到静态共享内存中。然后按索引计算矩阵的元素相乘和累加就可以得到结果。完整代码:

#define BLOCKSIZE 32

__global__ void Matmul_shared_static_kernel(float* M_device, float* N_device, float* P_device, int width)
{
    __shared__ float M_device_shared[BLOCKSIZE][BLOCKSIZE];
    __shared__ float N_device_shared[BLOCKSIZE][BLOCKSIZE];

    //index of the current element in the matrix
    int ix = blockIdx.x * blockDim.x + threadIdx.x;
    int iy = blockIdx.y * blockDim.y + threadIdx.y;
    int idx = iy * width + ix;
    //index of tiles in the matrix
    int tx = threadIdx.x;
    int ty = threadIdx.y;

    float P_element = 0;
    for (int i = 0; i < width / BLOCKSIZE; i++){
/*      iy * width is the row index,  i * BLOCKSIZE + tx is the column index*/
        M_device_shared[ty][tx] = M_device[iy * width + (i * BLOCKSIZE + tx)];
/*      (i * BLOCKSIZE + ty) * width is the row index,  ix is the column index*/       
        N_device_shared[ty][tx] = N_device[(i * BLOCKSIZE + ty) * width + ix];
        __syncthreads();
        for (int j = 0; j < BLOCKSIZE; j++){
            P_element += M_device_shared[ty][j] * N_device_shared[j][tx];
        }
        __syncthreads();
    }
    P_device[idx] = P_element;
}

结果分析

 &esp;接下来就是正式编译,接下来与(cuda编程[4]中一致,是如何使用vscode配置CUDA C++编译环境并运行的。

cmake -B build -G "Visual Studio 16 2019"
cmake --build build
./build/Debug/matmul_shared.exe

得到的结果

(joker) PS C:\Users\22681\Desktop\project\cudalearn\notes\code\matmul-shared-memory> ./build/Debug/matmul_shared.exe
Input size is:4096 x 4096
matmul in gpu warmup                                         uses 187.220901 ms
matmul in gpu without shared memory<<<256, 16>>>             uses 184.976639 ms
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小马敲马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值