chundoong-lab-ta/SamsungDS22/submissions/HW6/bumhee86.lee/mat_mul.cu

230 lines
6.9 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
#define NUM_WORK_ITEM (32)
#define VECTOR_WIDTH (16)
#define RTS (NUM_WORK_ITEM / VECTOR_WIDTH)
#define ALIGN_UP(_X, _Y) (((_X) + (_Y) - 1) & ~((_Y) - 1))
int num_devices = 0;
__global__ void sgemm(float *__restrict A, float *__restrict B, float *__restrict C, int M, int N, int K, int NON_OPTIMAL)
{
// blockDim.x -> NUM_WORK_ITEM
const int i = threadIdx.x; // row index of C
const int j = threadIdx.y; // column index of C
const int global_row = NUM_WORK_ITEM * blockIdx.x + i;
const int global_col = NUM_WORK_ITEM * blockIdx.y + j;
float intermediate_val[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
__shared__ float tileA[NUM_WORK_ITEM][NUM_WORK_ITEM];
__shared__ float tileB[NUM_WORK_ITEM][NUM_WORK_ITEM];
if (NON_OPTIMAL == 0)
{
const int num_tiles = K / NUM_WORK_ITEM;
// printf("i : %d, j : %d, global_row : %d, global_col : %d\n", i, j, global_row, global_col);
for (int t = 0; t < num_tiles; t++)
{
for (int w = 0; w < VECTOR_WIDTH; w++)
{
const int t_row = NUM_WORK_ITEM * t + i;
const int t_col = NUM_WORK_ITEM * t + j;
tileA[i + w * RTS][j] = A[((global_row + w * RTS)) * K + t_col];
tileB[i + w * RTS][j] = B[((t_row + w * RTS)) * N + global_col];
}
__syncthreads();
for (int k = 0; k < NUM_WORK_ITEM; k++)
{
for (int w = 0; w < VECTOR_WIDTH; w++)
{
intermediate_val[w] += tileA[i + w * RTS][k] * tileB[k][j];
}
}
__syncthreads();
}
for (int w = 0; w < VECTOR_WIDTH; w++)
{
C[(global_row + w * RTS) * N + global_col] = intermediate_val[w];
}
}
else
{
const int num_tiles = (K + NUM_WORK_ITEM - 1) / NUM_WORK_ITEM;
// printf("i : %d, j : %d, global_row : %d, global_col : %d\n", i, j, global_row, global_col);
for (int t = 0; t < num_tiles; t++)
{
for (int w = 0; w < VECTOR_WIDTH; w++)
{
const int t_row = NUM_WORK_ITEM * t + i;
const int t_col = NUM_WORK_ITEM * t + j;
if (global_row + w * RTS >= M || t_col >= K)
{
tileA[i + w * RTS][j] = 0.0f;
}
else
{
tileA[i + w * RTS][j] = A[((global_row + w * RTS)) * K + t_col];
}
if (t_row + w * RTS >= K || global_col >= N)
{
tileB[i + w * RTS][j] = 0.0f;
}
else
{
tileB[i + w * RTS][j] = B[((t_row + w * RTS)) * N + global_col];
}
}
__syncthreads();
for (int k = 0; k < NUM_WORK_ITEM; k++)
{
for (int w = 0; w < VECTOR_WIDTH; w++)
{
intermediate_val[w] += tileA[i + w * RTS][k] * tileB[k][j];
}
}
__syncthreads();
}
for (int w = 0; w < VECTOR_WIDTH; w++)
{
if(global_row + w * RTS >= M || global_col >= N)
{
break;
}
else
{
C[(global_row + w * RTS) * N + global_col] = intermediate_val[w];
}
}
// printf("C[%d] = , %+.3f, %+.3f, %+.3f, %+.3f,...\n", (global_row) * N + global_col, intermediate_val[0],intermediate_val[1],intermediate_val[2],intermediate_val[3]);
}
}
// 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], Msize[MAX_NUM_GPU];
static int NON_OPTIMAL;
void mat_mul(float *_A, float *_B, float *_C, int _M, int _N, int _K) {
if (_M % (NUM_WORK_ITEM * num_devices) != 0
|| _N % NUM_WORK_ITEM != 0
|| _K % NUM_WORK_ITEM != 0)
{
NON_OPTIMAL = 1;
}
else
{
NON_OPTIMAL = 0;
}
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
dim3 blockDim(RTS, NUM_WORK_ITEM, 1);
dim3 gridDim(ALIGN_UP(Msize[i], NUM_WORK_ITEM) / NUM_WORK_ITEM , \
ALIGN_UP(N, NUM_WORK_ITEM) / NUM_WORK_ITEM, 1);
CUDA_CALL( cudaSetDevice(i) );
sgemm<<<gridDim, blockDim>>>(a_d[i], b_d[i], c_d[i], Msize[i], N, K, NON_OPTIMAL);
}
// 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;
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);
}
// 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);
Msize[i] = Mend[i] - Mbegin[i];
}
Mend[num_devices - 1] = M;
Msize[num_devices - 1] = Mend[num_devices - 1] - Mbegin[num_devices - 1];
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&a_d[i], (Msize[i]) * K * sizeof(float)) );
CUDA_CALL( cudaMalloc(&b_d[i], K * N * sizeof(float)) );
CUDA_CALL( cudaMalloc(&c_d[i], (Msize[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,
(Msize[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],
(Msize[i]) * N * sizeof(float),
cudaMemcpyDeviceToHost) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}