chundoong-lab-ta/SamsungDS22/submissions/final/gyeongmin.ha/B/convolution.cu

234 lines
6.1 KiB
Plaintext

#include "convolution.h"
#include "util.h"
#include <mpi.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 8
//#define TS 16
#define MAX_NUM_GPU 4
static float *input, *output, *filter;
static float *input_d[MAX_NUM_GPU], *output_d[MAX_NUM_GPU], *filter_d[MAX_NUM_GPU];
static int mpi_rank;
static int mpi_world_size;
static int num_devices = 1;
static int N, C, H, W, K, R, S, OH, OW;
static int NN[MAX_NUM_GPU];
static int pad;
static int dilation;
static int stride;
static int size[2];
static int offset;
__global__ void conv(
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)
{
int n, k, w;
int OH, OW;
const int globalRow = blockDim.x * blockIdx.x + threadIdx.x;
const int globalCol = blockDim.y * blockIdx.y + threadIdx.y;
OH = (_H + 2 * _pad - _dilation * (_R - 1) - 1) / _stride + 1;
OW = (_W + 2 * _pad - _dilation * (_S - 1) - 1) / _stride + 1;
w = globalCol;
n = w /(_K*OW);
w = w - n * (_K*OW);
k = w / OW;
w = w - k * OW;
int col = w;
int row = globalRow;
if(globalRow >= OH || globalCol >= _N*_K*OW) return;
int start_row = row * _stride - _pad;
int start_col = col * _stride - _pad;
float o = 0.0f;
for (int c = 0; c < _C; c++) {
for (int i = 0; i < _R; i++) {
for (int j = 0; j < _S; j++) {
int h = start_row + i * _dilation;
int w = start_col + j * _dilation;
if (h < 0 || w < 0 || h >= _H || w >= _W) continue;
float in = _input[n*_C*_H*_W + c*_H*_W + h*_W + w];
float fil = _filter[k*_C*_R*_S + c*_R*_S + i*_S + j];
o += in * fil;
}
}
}
_output[n*_K*OH*OW + k*OH*OW + OW*row + col] = o;
}
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;
offset = 0;
if (mpi_rank == 0 && mpi_world_size == 2 && size[1] != 0)
{
MPI_Send(&input[size[0]*C*H*W], size[1]*C*H*W, MPI_FLOAT, 1, 0, MPI_COMM_WORLD);
MPI_Send(filter, _K*_C*_R*_S, MPI_FLOAT, 1, 0, MPI_COMM_WORLD);
if (size[mpi_rank] < MAX_NUM_GPU)
{
num_devices = size[mpi_rank];
}
}
else if (mpi_rank == 1 && size[mpi_rank] != 0)
{
alloc_tensor(&input, size[1], C, H, W);
alloc_tensor(&output, size[1], K, OH, OW);
alloc_tensor(&filter, _K, _C, _R, _S);
MPI_Recv(input, size[1]*C*H*W, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, nullptr);
MPI_Recv(filter, _K*_C*_R*_S, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, nullptr);
if (size[mpi_rank] < MAX_NUM_GPU)
{
num_devices = size[mpi_rank];
}
}
offset = 0;
for (int i = 0; i < num_devices; i++)
{
CUDA_CALL( cudaMemcpy(input_d[i], input+offset, NN[i]*C*H*W*sizeof(float), cudaMemcpyHostToDevice) );
CUDA_CALL( cudaMemcpy(filter_d[i], filter, C*K*R*S*sizeof(float), cudaMemcpyHostToDevice) );
offset += NN[i]*C*H*W;
}
for (int i = 0; i < num_devices; i++)
{
CUDA_CALL( cudaDeviceSynchronize() );
}
for (int i = 0; i < num_devices; i++)
{
dim3 gridDim((OH+TS-1)/TS, (NN[i]*K*OW + TS - 1)/TS, 1);
dim3 blockDim(TS, TS, 1);
CUDA_CALL( cudaSetDevice(i) );
conv<<<gridDim, blockDim>>>(input_d[i], output_d[i], filter_d[i], NN[i], _C, _H, _W, _K, _R, _S, _pad, _dilation, _stride);
}
for (int i = 0; i < num_devices; i++)
{
CUDA_CALL( cudaSetDevice(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);
OH = (H+2*pad - dilation*(R-1)-1) / stride+1;
OW = (W+2*pad - dilation*(S-1)-1) / stride+1;
if (mpi_world_size == 2 && _N > 4) size[1] = _N/2;
else size[1] = 0;
size[0] = N - size[1];
if (size[mpi_rank] < MAX_NUM_GPU)
{
num_devices = size[mpi_rank];
for (int i=0; i<size[mpi_rank];i++)
{
NN[i] = 1;
}
}
else
{
num_devices = MAX_NUM_GPU;
int quotient = size[mpi_rank] / MAX_NUM_GPU;
int remainder = size[mpi_rank] % MAX_NUM_GPU;
for (int i=0;i<MAX_NUM_GPU;i++)
{
NN[i] = quotient;
if (i<remainder)
{
NN[i]++;
}
}
}
for (int i=0; i<num_devices; i++)
{
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&input_d[i],NN[i]*C*H*W*sizeof(float)) );
CUDA_CALL( cudaMalloc(&output_d[i],NN[i]*K*OH*OW*sizeof(float)) );
CUDA_CALL( cudaMalloc(&filter_d[i],K*C*R*S*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) {
offset = 0;
for (int i = 0; i < num_devices; i++)
{
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMemcpy(output + offset, output_d[i], NN[i]*K*OH*OW*sizeof(float), cudaMemcpyDeviceToHost) );
offset += NN[i]*K*OH*OW;
}
for (int i = 0; i < num_devices; i++)
{
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaDeviceSynchronize() );
}
if (mpi_rank == 0 && mpi_world_size == 2 && size[1] != 0)
{
MPI_Recv(&output[size[0]*K*OH*OW], size[1]*K*OH*OW, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, nullptr);
}
else if(mpi_rank == 1 && size[1] != 0)
{
MPI_Send(output, size[1]*K*OH*OW, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
}
}