chundoong-lab-ta/SamsungDS22/submissions/final/hyowon12.an/tmp-B/convolution.cu

234 lines
6.9 KiB
Plaintext
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "convolution.h"
#include <mpi.h>
#include <stdio.h>
#include "util.h"
#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_X 2
#define TS_Y 2
#define TS_Z 32
#define MAX_NUM_GPU 4
int num_devices = 0;
static float *input_d[MAX_NUM_GPU];
static float *filter_d[MAX_NUM_GPU];
static float *output_d[MAX_NUM_GPU];
static int Mbegin[MAX_NUM_GPU], Mend[MAX_NUM_GPU];
void convolution_cuda();
void convolution_cuda_init(int,int);
void convolution_cuda_final(int);
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 stride_N[4];
static int offset[4];
MPI_Status status;
MPI_Request request;
__global__ void conv_kernel(float *input, float *filter, float *output, int N, int C, int H, int W,
int K, int R, int S, int OH, int OW, int pad, int dilation, int stride) {
int k = blockDim.x * blockIdx.x + threadIdx.x;
int oh = blockDim.y * blockIdx.y + threadIdx.y;
int ow = blockDim.z * blockIdx.z + threadIdx.z;
if (k >= K || oh>= OH || ow >= OW) return;
for(int n = 0; n < N; n++){
float o = 0.f;
for (int c = 0; c < C; ++c) {
for (int r = 0; r < R; ++r) {
for (int s = 0; s < S; ++s) {
int h = oh * stride - pad + r * dilation;
int w = ow * stride - pad + s * dilation;
if (h < 0 || h >= H || 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;
}
}
}
output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o;
}
}
void convolution_cuda() {
// Launch kernel on every GPU
for (int i = 0; i < num_devices; i++) {
dim3 blockDim(TS_X, TS_Y, TS_Z);
dim3 gridDim((K+TS_X-1)/TS_X, (OH+TS_Y-1)/TS_Y, (OW+TS_Z-1)/TS_Z);
CUDA_CALL( cudaSetDevice(i) );
conv_kernel<<<gridDim, blockDim>>>(input_d[i], filter_d[i], output_d[i], Mend[i] - Mbegin[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() );
}
}
void convolution_cuda_init() {
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] = (stride_N[mpi_rank] / num_devices) * i;
Mend[i] = (stride_N[mpi_rank] / num_devices) * (i + 1);
}
Mend[num_devices - 1] = stride_N[mpi_rank];
// Allocate device memory for each GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaSetDevice(i) );
CUDA_CALL( cudaMalloc(&input_d[i], (Mend[i] - Mbegin[i]) * C * H * W * sizeof(float)) );
CUDA_CALL( cudaMalloc(&filter_d[i], K * C * R * S * sizeof(float)) );
CUDA_CALL( cudaMalloc(&output_d[i], (Mend[i] - Mbegin[i]) * K* OH * OW * sizeof(float)) );
}
// Upload A and B matrix to every GPU
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(input_d[i], input + Mbegin[i] * C * H * W,
(Mend[i] - Mbegin[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( cudaSetDevice(i) );
CUDA_CALL( cudaDeviceSynchronize() );
}
}
void convolution_cuda_final() {
// Do any post-matmul cleanup work here.
// Download C matrix from GPUs
for (int i = 0; i < num_devices; i++) {
CUDA_CALL( cudaMemcpy(output + Mbegin[i] * K * OH * OW, output_d[i],
(Mend[i] - Mbegin[i]) * K * OH * OW * 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() );
}
}
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;
int divided_N = N / mpi_world_size;
int modular_N = N % mpi_world_size;
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
offset[0] = 0;
stride_N[0] = divided_N;
for (int i = 1; i < mpi_world_size; i++) {
if (i <= modular_N) stride_N[i] = divided_N + 1;
else stride_N[i] = divided_N;
offset[1] = divided_N;
if (i > 1) offset[i] = offset[i-1] + stride_N[i-1];
}
if (mpi_rank != 0) {
alloc_tensor(&input, stride_N[mpi_rank], C, H, W);
alloc_tensor(&output, stride_N[mpi_rank], K, OH, OW);
alloc_tensor(&filter, K, C, R, S);
}
MPI_Bcast(filter, K*C*R*S, MPI_FLOAT, 0, MPI_COMM_WORLD);
if (mpi_rank == 0) {
for (int i = 1; i < mpi_world_size; i++)
MPI_Isend(&input[offset[i]*C*H*W], stride_N[i]*C*H*W, MPI_FLOAT, i, 1, MPI_COMM_WORLD, &request);
}
else {
MPI_Recv(input, stride_N[mpi_rank]*C*H*W, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status);
}
convolution_cuda_init();
convolution_cuda();
convolution_cuda_final();
if (mpi_rank == 0) {
for (int i = 1; i < mpi_world_size; i++)
MPI_Recv(&output[offset[i]*K*OH*OW], stride_N[i]*K*OH*OW, MPI_FLOAT, i, 2, MPI_COMM_WORLD, &status);
}
else {
MPI_Send(output, stride_N[mpi_rank]*K*OH*OW, MPI_FLOAT, 0, 2, MPI_COMM_WORLD);
}
}
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);
}
void convolution_final(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
}