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

196 lines
6.5 KiB
Plaintext

#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 BLOCK_SIZE 16
__global__ void sgemm(float *A, float *B, float *C, int M, int N, int K) {
// Block index
int bx = blockIdx.x;
int by = blockIdx.y;
// Thread index
int tx = threadIdx.x;
int ty = threadIdx.y;
// Index of the first sub-matrix of A processed by the block
int aBegin = K * BLOCK_SIZE * by;
// Index of the last sub-matrix of A processed by the block
int aEnd = aBegin + K - 1;
// Step size used to iterate through the sub-matrices of A
int aStep = BLOCK_SIZE;
// Index of the first sub-matrix of B processed by the block
int bBegin = BLOCK_SIZE * bx;
// Step size used to iterate through the sub-matrices of B
int bStep = BLOCK_SIZE * N;
// Csub is used to store the element of the block sub-matrix
// that is computed by the thread
float Csub = 0;
// Loop over all the sub-matrices of A and B
// required to compute the block sub-matrix
for (int a = aBegin, b = bBegin;
a <= aEnd;
a += aStep, b += bStep)
{
// Declaration of the shared memory array As used to
// store the sub-matrix of A
__shared__ float As[BLOCK_SIZE][BLOCK_SIZE];
// Declaration of the shared memory array Bs used to
// store the sub-matrix of B
__shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
// Load the matrices from device memory
// to shared memory; each thread loads
// one element of each matrix
As[ty][tx] = A[a + K * ty + tx];
Bs[ty][tx] = B[b + N * ty + tx];
// Synchronize to make sure the matrices are loaded
__syncthreads();
// Multiply the two matrices together;
// each thread computes one element
// of the block sub-matrix
#pragma unroll
for (int k = 0; k < BLOCK_SIZE; ++k)
{
Csub += As[ty][k] * Bs[k][tx];
}
// Synchronize to make sure that the preceding
// computation is done before loading two new
// sub-matrices of A and B in the next iteration
__syncthreads();
}
// Write the block sub-matrix to device memory;
// each thread writes one element
int c = N * BLOCK_SIZE * by + BLOCK_SIZE * bx;
C[c + N * ty + tx] = Csub;
}
// 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++) {
int rounded_N = (N + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
int rounded_M = ((Mend[i] - Mbegin[i]) + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
dim3 blockDim(BLOCK_SIZE, BLOCK_SIZE);
dim3 gridDim(rounded_N/BLOCK_SIZE, rounded_M/BLOCK_SIZE);
CUDA_CALL( cudaSetDevice(i) );
sgemm<<<gridDim, blockDim>>>(a_d[i], b_d[i], c_d[i], Mend[i] - Mbegin[i], rounded_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);
}
// 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
int rounded_N = (N + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
for (int i = 0; i < num_devices; i++) {
int rounded_M = ((Mend[i] - Mbegin[i]) + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&a_d[i], rounded_M * K * sizeof(float)) );
CUDA_CALL( cudaMalloc(&b_d[i], K * rounded_N * sizeof(float)) );
CUDA_CALL( cudaMalloc(&c_d[i], rounded_M * rounded_N * sizeof(float)) );
CUDA_CALL( cudaMemset(a_d[i], 0, rounded_M * K * sizeof(float)) );
CUDA_CALL( cudaMemset(b_d[i], 0, K * rounded_N * sizeof(float)) );
CUDA_CALL( cudaMemset(c_d[i], 0, rounded_M * rounded_N * sizeof(float)) );
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMemcpy(a_d[i], A + Mbegin[i] * K,
(Mend[i] - Mbegin[i]) * K * sizeof(float),
cudaMemcpyHostToDevice) );
for (int j = 0; j < K; j++) {
CUDA_CALL( cudaMemcpy(b_d[i] + j * rounded_N, B + j * N, 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.
int rounded_N = (N + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
// Download C matrix from GPUs
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
for (int j = 0; j < (Mend[i] - Mbegin[i]); j++) {
CUDA_CALL( cudaMemcpy(C + Mbegin[i] * N + j * N, c_d[i] + j * rounded_N, N * sizeof(float), cudaMemcpyDeviceToHost) );
}
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaDeviceSynchronize() );
}
}