chundoong-lab-ta/SamsungDS22/submissions/HW6/jinho.yi/mat_mul.cu

212 lines
5.9 KiB
Plaintext

#include "mat_mul.h"
#include "util.h"
#include <cstdio>
#include <cuda_runtime.h>
#define CUDA_CALL(f) \
{ \
cudaError_t err = (f); \
if (err != cudaSuccess) { \
fprintf(stderr, "CUDA error at [%s:%d] %d %s\n", __FILE__, __LINE__, \
err, cudaGetErrorString(err)); \
exit(1); \
} \
}
#define TS 32
#define WPT 16
#define RTS (TS/WPT)
#define MAX_NUM_GPU 4
int num_devices = 0;
__global__ void sgemm(float *A, float *B, float *C, int M, int N, int K) {
const int row = threadIdx.x; // Local row ID (max: TS/WIDTH)
const int col = threadIdx.y; // Local col ID (max: TS)
const int globalRow = blockDim.x * WPT * blockIdx.x + row;
const int globalCol = blockDim.y * blockIdx.y + col; // 0..N
if (globalRow >= M || globalCol >= N) return;
// Local memory to fit a tile of TS*TS elements of A and B
__shared__ float Asub[TS][TS];
__shared__ float Bsub[TS][TS];
const int numTiles = K/TS;
float sum[WPT];
for (int w=0; w<WPT; w++) {
sum[w] = 0.0f;
}
for (int t=0; t<numTiles; t++) {
const int tiledRow = TS*t + row;
const int tiledCol = TS*t + col;
for (int w=0; w<WPT; w++) {
Asub[row + w*RTS][col] = A[(globalRow + w*RTS) * K + tiledCol];
Bsub[row + w*RTS][col] = B[(tiledRow + w*RTS) * N + globalCol];
}
// Synchronise to make sure the tile is loaded
__syncthreads();
// Perform the computation for a single tile
for (int k=0; k<TS; k++) {
for (int w=0; w<WPT; w++) {
sum[w] += Asub[row + w*RTS][k] * Bsub[k][col];
}
}
// Synchronise before loading the next tile
__syncthreads();
}
// Store the final results in C
for (int w=0; w<WPT; w++) {
C[(globalRow + w*RTS) * N + globalCol] = sum[w];
}
}
// Array of device (GPU) pointers
static float *a_d[MAX_NUM_GPU];
static float *b_d[MAX_NUM_GPU];
static float *c_d[MAX_NUM_GPU];
static int M, N, K;
static int Mbegin[MAX_NUM_GPU], Mend[MAX_NUM_GPU];
static float *A, *B, *C;
static int CopyBuf = 0;
void mat_mul(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
dim3 blockDim(TS/WPT, TS, 1);
dim3 gridDim(((Mend[i] - Mbegin[i]+TS-1)/TS), (N+TS-1)/TS, 1);
CUDA_CALL( cudaSetDevice(i) );
sgemm<<<gridDim, blockDim>>>(a_d[i], b_d[i], c_d[i], Mend[i] - Mbegin[i], N, K);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}
void mat_mul_init(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
// M = _M, N = _N, K = _K;
M = (_M & (TS-1))? _M + (TS - (_M & (TS-1))) : _M;
N = (_N & (TS-1))? _N + (TS - (_N & (TS-1))) : _N;
K = (_K & (TS-1))? _K + (TS - (_K & (TS-1))) : _K;
CopyBuf = 0;
if(M == _M && K == _K){
A = _A;
}
else{
alloc_mat(&A, M, K);
for (int i = 0; i < M; i++) {
for (int j = 0; j < K; j++) {
if(i<_M && j<_K)
A[i * K + j] = _A[i * _K + j];
else
A[i * K + j] = 0;
}
}
CopyBuf = 1;
}
if(K == _K && N == _N){
B = _B;
}
else{
alloc_mat(&B, K, N);
for (int i = 0; i < K; i++) {
for (int j = 0; j < N; j++) {
if(i<_K && j<_N)
B[i * N + j] = _B[i * _N + j];
else
B[i * N + j] = 0;
}
}
CopyBuf = 1;
}
if(M == _M && N == _N){
C = _C;
}
else{
alloc_mat(&C, M, N);
zero_mat(C, M, N);
CopyBuf = 1;
}
/////////////////////////
CUDA_CALL( cudaGetDeviceCount(&num_devices) );
if(num_devices > MAX_NUM_GPU) num_devices = MAX_NUM_GPU;
printf("Using %d devices\n", num_devices);
for (int i = 0; i < num_devices; i++) {
cudaDeviceProp prop;
CUDA_CALL( cudaGetDeviceProperties(&prop, i) );
// Try printing more detailed information here
printf("[GPU %d] %s\n", i, prop.name);
}
if (num_devices <= 0) {
printf("No CUDA device found. Aborting\n");
exit(1);
}
// Setup problem size for each GPU
for (int i = 0; i < num_devices; i++) {
Mbegin[i] = (M / num_devices) * i;
Mend[i] = (M / num_devices) * (i + 1);
}
Mend[num_devices - 1] = M;
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&a_d[i], (Mend[i] - Mbegin[i]) * K * sizeof(float)) );
CUDA_CALL( cudaMalloc(&b_d[i], K * N * sizeof(float)) );
CUDA_CALL( cudaMalloc(&c_d[i], (Mend[i] - Mbegin[i]) * N * sizeof(float)) );
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(a_d[i], A + Mbegin[i] * K,
(Mend[i] - Mbegin[i]) * K * sizeof(float),
cudaMemcpyHostToDevice) );
CUDA_CALL( cudaMemcpy(b_d[i], B, K * N * sizeof(float), cudaMemcpyHostToDevice) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}
void mat_mul_final(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
// Do any post-matmul cleanup work here.
// Download C matrix from GPUs
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(C + Mbegin[i] * N, c_d[i],
(Mend[i] - Mbegin[i]) * N * sizeof(float),
cudaMemcpyDeviceToHost) );
}
if(CopyBuf == 1){
for (int i = 0; i < _M; i++) {
for (int j = 0; j < _N; j++) {
_C[i * _N + j] = C[i * N + j];
}
}
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}