chundoong-lab-ta/SamsungDS22/submissions/final/jinho.yi/B/convolution.cu

219 lines
7.2 KiB
Plaintext
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "convolution.h"
#include "util.h"
#include <mpi.h>
#include <stdio.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#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 8
#define MAX_NUM_GPU 4
#define MAX_NUM_MPI 2
int num_devices = 1;
static float *input, *output, *filter;
static int N, C, H, W;
static int K, R, S;
static int OH, OW;
static int pad;
static int dilation;
static int stride;
static int mpi_rank, mpi_world_size;
int ns[2], ne[2];
static int Nbegin[MAX_NUM_MPI][MAX_NUM_GPU], Nend[MAX_NUM_MPI][MAX_NUM_GPU];
static float *input_d[MAX_NUM_GPU];
static float *filter_d[MAX_NUM_GPU];
static float *output_d[MAX_NUM_GPU];
MPI_Status status;
MPI_Request request;
__global__ void sgemm(float *_input, float *_filter, float *_output, int _N, int _C, int _H, int _W,
int _K, int _R, int _S, int _pad, int _dilation, int _stride) {
const int globalRow = blockDim.x * blockIdx.x + threadIdx.x;
const int globalCol = blockDim.y * blockIdx.y + threadIdx.y;
int _OH = (_H + 2 * _pad - _dilation * (_R - 1) - 1) / _stride + 1;
int _OW = (_W + 2 * _pad - _dilation * (_S - 1) - 1) / _stride + 1;
if (globalRow >= _OH || globalCol >= _N * _K * _OW) return;
int n = globalCol / (_K * _OW);
int cw = globalCol - n * (_K * _OW);
int k = cw / _OW;
cw -= k * _OW;
float o = 0.f;
int row = globalRow * _stride - _pad;
int col = cw * _stride - _pad;
for (int c = 0; c < _C; ++c) {
for (int r = 0; r < _R; ++r) {
int h = row + r * _dilation;
if (h < 0 || h >= _H) continue;
for (int s = 0; s < _S; ++s) {
int w = col + s * _dilation;
if (w < 0 || w >= _W) continue;
float i = _input[n * _C * _H * _W + c * _H * _W + h * _W + w];
float f = _filter[k * _C * _R * _S + c * _R * _S + r * _S + s];
o += i * f;
} //s
} //r
} //c
_output[n * _K * _OH * _OW + k * _OH * _OW + globalRow * _OW + cw] = o;
// Synchronise before loading the next tile
__syncthreads();
}
void convolution(
float *_input, float *_output, float *_filter,
int _N, int _C, int _H, int _W, int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
input = _input;
output = _output;
filter = _filter;
if (mpi_rank == 0) {
for (int i = 1; i < mpi_world_size; i++) {
MPI_Isend(&input[ns[i]*C*H*W], (ne[i]-ns[i])*C*H*W, MPI_FLOAT, i, 1, MPI_COMM_WORLD, &request);
MPI_Isend(filter, K*C*R*S, MPI_FLOAT, i, 2, MPI_COMM_WORLD, &request);
}
}
else {
alloc_tensor(&input, N, C, H, W);
alloc_tensor(&filter, K, C, R, S);
alloc_tensor(&output, N, K, OH, OW);
MPI_Recv(&input[ns[mpi_rank]*C*H*W], (ne[mpi_rank]-ns[mpi_rank])*C*H*W, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status);
MPI_Recv(filter, K*C*R*S, MPI_FLOAT, 0, 2, MPI_COMM_WORLD, &status);
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(input_d[i], input + Nbegin[mpi_rank][i] *C*H*W,
(Nend[mpi_rank][i] - Nbegin[mpi_rank][i]) *C*H*W * sizeof(float),
cudaMemcpyHostToDevice) );
CUDA_CALL( cudaMemcpy(filter_d[i], filter, K*C*R*S * sizeof(float), cudaMemcpyHostToDevice) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
///////////////// Start Calculation ///////////////////
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
dim3 gridDim((OH+TS-1)/TS,((Nend[mpi_rank][i] - Nbegin[mpi_rank][i])*K*OW+TS-1)/TS, 1);
dim3 blockDim(TS, TS, 1);
CUDA_CALL( cudaSetDevice(i) );
sgemm<<<gridDim, blockDim>>>(input_d[i], filter_d[i], output_d[i],
Nend[mpi_rank][i] - Nbegin[mpi_rank][i], _C, _H, _W, _K, _R, _S, _pad, _dilation, _stride);
}
// NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}
void convolution_init(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
N = _N; C = _C; H = _H; W = _W;
K = _K; R = _R; S = _S;
pad = _pad;
dilation = _dilation;
stride = _stride;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size);
/////////////////////// Init Cuda ///////////////////////
CUDA_CALL( cudaGetDeviceCount(&num_devices) );
if(num_devices > MAX_NUM_GPU) num_devices = MAX_NUM_GPU;
printf("[MPI:%d] Using %d devices\n", mpi_rank, num_devices);
for (int i = 0; i < num_devices; i++) {
cudaDeviceProp prop;
CUDA_CALL( cudaGetDeviceProperties(&prop, i) );
printf("[GPU %d] %s\n", i, prop.name);
}
if (num_devices <= 0) {
printf("No CUDA device found. Aborting\n");
exit(1);
}
for (int i = 0; i < mpi_world_size; i++) {
ns[i] = N / mpi_world_size * i;
ne[i] = N / mpi_world_size * (i + 1);
}
ne[mpi_world_size - 1] = N;
// Setup problem size for each GPU
for (int i = 0; i < num_devices; i++) {
Nbegin[mpi_rank][i] = (ne[mpi_rank]-ns[mpi_rank]) / num_devices * i + ns[mpi_rank];
Nend[mpi_rank][i] = (ne[mpi_rank]-ns[mpi_rank]) / num_devices * (i + 1) + ns[mpi_rank];
}
Nend[mpi_rank][num_devices - 1] = ne[mpi_rank];
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&input_d[i], (Nend[mpi_rank][i] - Nbegin[mpi_rank][i]) * C * H * W * sizeof(float)) );
CUDA_CALL( cudaMalloc(&filter_d[i], K * C * R * S * sizeof(float)) );
CUDA_CALL( cudaMalloc(&output_d[i], (Nend[mpi_rank][i] - Nbegin[mpi_rank][i]) * K * OH * OW * sizeof(float)) );
}
}
void convolution_final(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
// Download output matrix from GPUs
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(output + Nbegin[mpi_rank][i] * K * OH * OW, output_d[i],
(Nend[mpi_rank][i] - Nbegin[mpi_rank][i]) * K * OH * OW * sizeof(float),
cudaMemcpyDeviceToHost) );
}
if (mpi_rank == 0) {
for (int i = 1; i < mpi_world_size; i++) {
MPI_Recv(&output[ns[i]*K*OH*OW], (ne[i]-ns[i])*K*OH*OW, MPI_FLOAT, 1, 1, MPI_COMM_WORLD, &status);
}
}
else {
MPI_Isend(&output[ns[mpi_rank]*K*OH*OW], (ne[mpi_rank]-ns[mpi_rank])*K*OH*OW, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &request);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaDeviceSynchronize() );
}
}