chundoong-lab-ta/SamsungDS22/submissions/final/mjstyle.kim/tmp-B/convolution.cu

237 lines
7.7 KiB
Plaintext

#include "util.h"
#include "convolution.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 MAX_NUM_GPU 4
//#define GPU_NUM 1
int num_devices = 0;
#define OH_TILE_WIDTH 16
#define OW_TILE_WIDTH 16
static float *i_d[MAX_NUM_GPU];
static float *f_d[MAX_NUM_GPU];
static float *o_d[MAX_NUM_GPU];
static int Nbegin[MAX_NUM_GPU], Nend[MAX_NUM_GPU];
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;
static int mpi_chunk[2];
__global__ void cuconv(
float *input, float *output, float *filter,
int N, int C, int H, int W,
int K, int R, int S, int OH, int OW,
int pad, int dilation, int stride) {
__shared__ float tile_filter[OH_TILE_WIDTH][OW_TILE_WIDTH];
int bx = blockIdx.x, by = blockIdx.y, nk = blockIdx.z;
int tx = threadIdx.x, ty = threadIdx.y;
int n = nk/K;
int k = nk%K;
int oh = by * OH_TILE_WIDTH + ty;
int ow = bx * OW_TILE_WIDTH + tx;
float o = 0.f;
float Rchunk = (R + OH_TILE_WIDTH - 1)/OH_TILE_WIDTH;
float Schunk = (S + OW_TILE_WIDTH - 1)/OW_TILE_WIDTH;
int hpos = oh*stride - pad;
int wpos = ow*stride - pad;
int nCHW = n * C * H * W;
for(int c = 0; c < C; ++c) {
int cHW = c * H * W;
for(int rtile = 0; rtile < Rchunk; ++rtile) {
for(int stile = 0; stile < Schunk; ++stile) {
int rpos = rtile*OH_TILE_WIDTH+ty;
int spos = stile*OW_TILE_WIDTH+tx;
if(rpos < R && spos < S)
tile_filter[ty][tx] = filter[k*C*R*S + c*R*S + rpos*S + spos];
else
tile_filter[ty][tx] = 0;
__syncthreads();
int Rbegin = (dilation - hpos - 1)/dilation > rtile*OH_TILE_WIDTH ? (dilation - hpos - 1)/dilation : rtile*OH_TILE_WIDTH;
int Sbegin = (dilation - wpos - 1)/dilation > stile*OW_TILE_WIDTH ? (dilation - wpos - 1)/dilation : stile*OW_TILE_WIDTH;
int Rlimit = (H - hpos + dilation - 1)/dilation < R ? (H - hpos + dilation - 1)/dilation : R;
int Slimit = (W - wpos + dilation - 1)/dilation < S ? (W - wpos + dilation - 1)/dilation : S;
int Rend = (rtile+1)*OH_TILE_WIDTH < Rlimit ? (rtile+1)*OH_TILE_WIDTH : Rlimit;
int Send = (stile+1)*OW_TILE_WIDTH < Slimit ? (stile+1)*OW_TILE_WIDTH : Slimit;
if(oh < OH && ow < OW)
for(int r = Rbegin; r < Rend; ++r) {
for(int s = Sbegin; s < Send; ++s) {
o += input[nCHW + cHW + (hpos + r * dilation) * W + (wpos + s * dilation)] * tile_filter[r-rtile*OH_TILE_WIDTH][s-stile*OW_TILE_WIDTH];
}
}
__syncthreads();
}
}
}
if(oh < OH && ow < OW)
output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = 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) {
MPI_Request request;
MPI_Status status;
if (mpi_rank == 0) {
input = _input;
output = _output;
filter = _filter;
}
if(mpi_world_size == 2) {
if (mpi_rank == 0) {
MPI_Isend(input+mpi_chunk[0]*C*H*W, mpi_chunk[1]*C*H*W, MPI_FLOAT, 1, 1000, MPI_COMM_WORLD, &request);
MPI_Isend(filter, K*C*R*S, MPI_FLOAT, 1, 1001, MPI_COMM_WORLD, &request);
} else {
MPI_Recv(input, mpi_chunk[1]*C*H*W, MPI_FLOAT, 0, 1000, MPI_COMM_WORLD, &status);
MPI_Recv(filter, K*C*R*S, MPI_FLOAT, 0, 1001, MPI_COMM_WORLD, &status);
}
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(i_d[i], input + Nbegin[i]*C*H*W,
(Nend[i]-Nbegin[i])*C*H*W*sizeof(float),
cudaMemcpyHostToDevice) );
CUDA_CALL( cudaMemcpy(f_d[i], filter, K*C*R*S*sizeof(float), cudaMemcpyHostToDevice) );
}
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
dim3 blockDim(OW_TILE_WIDTH, OH_TILE_WIDTH, 1);
dim3 gridDim((OW+OW_TILE_WIDTH-1)/OW_TILE_WIDTH, (OH+OH_TILE_WIDTH-1)/OH_TILE_WIDTH, K*(Nend[i]-Nbegin[i]));
CUDA_CALL( cudaSetDevice(i) );
cuconv<<<gridDim, blockDim>>>(i_d[i], o_d[i], f_d[i],
Nend[i] - Nbegin[i], C, H, W, K, R, S, OH, OW, pad, dilation, stride);
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaDeviceSynchronize() );
}
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(output + Nbegin[i]*K*OH*OW, o_d[i],
(Nend[i]-Nbegin[i])*K*OH*OW*sizeof(float),
cudaMemcpyDeviceToHost) );
}
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaDeviceSynchronize() );
}
if(mpi_world_size == 2) {
if (mpi_rank == 0) {
MPI_Recv(output+mpi_chunk[0]*K*OH*OW, mpi_chunk[1]*K*OH*OW, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &status);
} else {
MPI_Isend(output, mpi_chunk[1]*K*OH*OW, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request);
}
}
}
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);
CUDA_CALL( cudaGetDeviceCount(&num_devices) );
CUDA_CALL( cudaDeviceSetCacheConfig(cudaFuncCachePreferL1) );
#if GPU_NUM
num_devices = GPU_NUM;
#endif
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);
}
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) {
mpi_chunk[0] = (N+1)/2;
mpi_chunk[1] = N - mpi_chunk[0];
if (mpi_rank != 0) {
alloc_tensor(&input, mpi_chunk[mpi_rank], C, H, W);
alloc_tensor(&output, mpi_chunk[mpi_rank], K, OH, OW);
alloc_tensor(&filter, K, C, R, S);
}
} else {
mpi_chunk[0] = N;
}
// Setup problem size for each GPU
for (int i = 0; i < num_devices; i++) {
Nbegin[i] = (mpi_chunk[mpi_rank] / num_devices) * i;
Nend[i] = (mpi_chunk[mpi_rank] / num_devices) * (i + 1);
}
Nend[num_devices - 1] = mpi_chunk[mpi_rank];
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&i_d[i], (Nend[i]-Nbegin[i])*C*H*W*sizeof(float)) );
CUDA_CALL( cudaMalloc(&f_d[i], K*C*R*S*sizeof(float)) );
CUDA_CALL( cudaMalloc(&o_d[i], (Nend[i]-Nbegin[i])*K*OH*OW*sizeof(float)) );
}
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaDeviceSynchronize() );
}
}
void convolution_final(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
}