chundoong-lab-ta/SamsungDS22/submissions/HW6/ym.tai/mat_mul.cu

177 lines
5.3 KiB
Plaintext
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "mat_mul.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 MAX_NUM_GPU 4
int num_devices = 0;
#define TILE_SIZE 32
#define WPT 8 // work per thread
#define RTS (TILE_SIZE / WPT)
__global__ void sgemm(float *A, float *B, float *C, int M, int N, int K) {
const int row = threadIdx.x; // Local row ID (max: TILE_SIZE/WPT == RTS)
const int col = threadIdx.y; // Local col ID (max: TILE_SIZE)
const int globalRow = TILE_SIZE * blockIdx.x + row; // row index of C (N)
const int globalCol = TILE_SIZE * blockIdx.y + col; // column index of C (M)
const int numTiles = (K + TILE_SIZE - 1) / TILE_SIZE;
// local memory for tile
__shared__ float Asub[TILE_SIZE][TILE_SIZE];
__shared__ float Bsub[TILE_SIZE][TILE_SIZE];
// Init result memory
float res[WPT];
for (int i = 0; i < WPT; i++) {
res[i] = 0.0f;
}
for (int t = 0; t < numTiles; t++) {
const int tiledRow = TILE_SIZE * t + row;
const int tiledCol = TILE_SIZE * t + col;
// Load A and B to local memory
for (int w = 0; w < WPT; w++) {
if (((w * RTS + globalRow) >= M) || (tiledCol >= K)) {
Asub[w * RTS + row][col] = 0;
}
else {
Asub[w * RTS + row][col] = A[(w * RTS + globalRow) * K + tiledCol];
}
if (((w * RTS + tiledRow) >= K) || (globalCol >= N)) {
Bsub[w * RTS + row][col] = 0;
}
else {
Bsub[w * RTS + row][col] = B[(w * RTS + tiledRow) * N + globalCol];
}
}
__syncthreads();
// result for tile
for (int i = 0; i < TILE_SIZE; i++) {
for (int j = 0; j < WPT; j++) {
res[j] += Asub[j * RTS + row][i] * Bsub[i][col];
}
}
__syncthreads();
}
// final results in C
for (int w = 0; w < WPT; w++) {
if ((w * RTS + globalRow < M) && (globalCol < N)) {
C[(w * RTS + globalRow) * N + globalCol] = res[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];
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(RTS, TILE_SIZE, 1);
dim3 gridDim(((Mend[i] - Mbegin[i]) + TILE_SIZE - 1) / TILE_SIZE, (N + TILE_SIZE - 1) / TILE_SIZE, 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( cudaSetDevice(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;
CUDA_CALL( cudaGetDeviceCount(&num_devices) );
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);
}
// // temporary!!!
// num_devices = 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( cudaSetDevice(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) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}