siboehm/SGEMM_CUDA (19 files):
siboehm_sgemm.cu, siboehm_runner.cu, siboehm_runner.cuh, siboehm_kernels.cuh
siboehm_cuBLAS_sgemm.cu, siboehm_simplest_kernel.cu, siboehm_CMakeLists.txt
siboehm_{1_naive..12_kernel_double_buffering}.cuh
wangzyon/NVIDIA_SGEMM_PRACTICE (12 files):
wangzyon_sgemm.cu, wangzyon_utils.cu, wangzyon_utils.cuh, wangzyon_kernel.cuh
wangzyon_CMakeLists.txt, wangzyon_kernel_{1..7}.cuh
edtallison/sgemm-cuda (19 files):
edtallison_sgemm.cu, edtallison_runner.cu, edtallison_runner.cuh
edtallison_kernels.cuh, edtallison_cuBLAS_sgemm.cu, edtallison_simplest_kernel.cu
edtallison_CMakeLists.txt, edtallison_{01_naive..12_kernel_double_buffering}.cuh
cat_files/ total: 25 → 75 files
45 lines
1.2 KiB
Plaintext
45 lines
1.2 KiB
Plaintext
#pragma once
|
||
|
||
#include <cuda_runtime.h>
|
||
#include <cublas_v2.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
template<const int BLOCK_SIZE>
|
||
__global__ void mysgemm_v2(int M, int N, int K, float alpha, float *A, float *B, float beta, float *C) {
|
||
int bx = blockIdx.x;
|
||
int by = blockIdx.y;
|
||
|
||
const int BM = BLOCK_SIZE;
|
||
const int BN = BLOCK_SIZE;
|
||
const int BK = BLOCK_SIZE;
|
||
|
||
int tx = threadIdx.x % BN;
|
||
int ty = threadIdx.x / BN;
|
||
|
||
// 申请共享内存空间
|
||
__shared__ float As[BM * BK];
|
||
__shared__ float Bs[BK * BN];
|
||
|
||
// 移动到当前block
|
||
A = &A[by * BM * K];
|
||
B = &B[bx * BN];
|
||
C = &C[by * BM * N + bx * BN];
|
||
|
||
float tmp = 0.;
|
||
for (int k = 0; k < K; k += BK) {
|
||
// 缓存A_tile和B_tile
|
||
As[ty * BK + tx] = A[ty * K + tx];
|
||
Bs[ty * BN + tx] = B[ty * N + tx];
|
||
// 同步所有线程缓存完成
|
||
__syncthreads();
|
||
A += BK;
|
||
B += BK * N;
|
||
for (int i = 0; i < BK; i++) {
|
||
tmp += As[ty * BK + i] * Bs[i * BN + tx];
|
||
}
|
||
// FMA计算需要读取缓存数据,在新一轮写入缓存前进行同步,确保所有线程计算完成
|
||
__syncthreads();
|
||
}
|
||
C[ty * N + tx] = alpha * tmp + beta * C[ty * N + tx];
|
||
} |