282 lines
9.4 KiB
Plaintext
282 lines
9.4 KiB
Plaintext
|
#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 WPT 16
|
||
|
#define RTS (TS/WPT)
|
||
|
|
||
|
#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;
|
||
|
|
||
|
////////////////////////////////// Kernel /////////////////////////////
|
||
|
__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 row = threadIdx.x; // Local row ID (max: TS/WIDTH)
|
||
|
//const int col = threadIdx.y; // Local col ID (max: TS)
|
||
|
const int globalRow = blockDim.x * blockIdx.x + threadIdx.x;
|
||
|
const int globalCol = blockDim.y * blockIdx.y + threadIdx.y; // 0..N
|
||
|
|
||
|
int _OH = (_H + 2 * _pad - _dilation * (_R - 1) - 1) / _stride + 1;
|
||
|
int _OW = (_W + 2 * _pad - _dilation * (_S - 1) - 1) / _stride + 1;
|
||
|
|
||
|
int w = globalCol;
|
||
|
int n = w / (_K * _OW);
|
||
|
w = w - n * (_K * _OW);
|
||
|
int k = w / _OW;
|
||
|
w = w - k * _OW;
|
||
|
|
||
|
int col = w;
|
||
|
int row = globalRow;
|
||
|
|
||
|
// printf("N :%d, input:0x%X\n", _N, &input);
|
||
|
/*
|
||
|
for (int n = 0; n < _N; ++n) {
|
||
|
for (int k = 0; k < _K; ++k) {
|
||
|
for (int oh = 0; oh < _OH; ++oh) {
|
||
|
for (int ow = 0; ow < _OW; ++ow) {
|
||
|
|
||
|
*/ if (globalRow >= _OH || globalCol >= _N * _K * _OW) return;
|
||
|
|
||
|
int start_row = row * _stride - _pad;
|
||
|
int start_col = col * _stride - _pad;
|
||
|
float o = 0.f;
|
||
|
|
||
|
for (int c = 0; c < _C; ++c) {
|
||
|
for (int r = 0; r < _R; ++r) {
|
||
|
//int h = oh * stride - pad + r * dilation;
|
||
|
for (int s = 0; s < _S; ++s) {
|
||
|
int h = start_row + r * _dilation;
|
||
|
int w = start_col + 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;
|
||
|
} //s
|
||
|
} //r
|
||
|
} //c
|
||
|
|
||
|
|
||
|
_output[n * _K * _OH * _OW + k * _OH * _OW + row * _OW + col] = o;
|
||
|
//printf("_output[%d][%d][%d][%d] : %f\n", n, k, row, col, o);
|
||
|
/* output[n * K * OH * OW + k * OH * OW + oh * OW + ow];
|
||
|
} //ow
|
||
|
} //oh
|
||
|
} //k
|
||
|
} //n
|
||
|
*/
|
||
|
/////////////////////////////////////////////
|
||
|
// Synchronise before loading the next tile
|
||
|
__syncthreads();
|
||
|
// }
|
||
|
|
||
|
// Store the final results in C
|
||
|
//for (int w=0; w<WPT; w++) {
|
||
|
//output[globalRow * N + globalCol] = sum[w];
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
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(((Nend[mpi_rank][i] - Nbegin[mpi_rank][i]+TS-1)/TS), (K+TS-1)/TS, 1);
|
||
|
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);
|
||
|
// printf("[mpi_rank:%d,GPU:%d] Nbegin:%d, Nend:%d\n", mpi_rank, i, Nbegin[mpi_rank][i], Nend[mpi_rank][i]);
|
||
|
}
|
||
|
|
||
|
// DO NOT REMOVE; NEEDED FOR TIME MEASURE
|
||
|
for (int i = 0; i < num_devices; i++) {
|
||
|
CUDA_CALL( cudaDeviceSynchronize() );
|
||
|
}
|
||
|
|
||
|
///////////////// End Calculation ///////////////////
|
||
|
|
||
|
|
||
|
//test
|
||
|
#if 0
|
||
|
printf("Print output result\n"); fflush(stdout);
|
||
|
for (int n = 0; n < N; ++n) {
|
||
|
for (int k = 0; k < K; ++k) {
|
||
|
for (int oh = 0; oh < OH; ++oh) {
|
||
|
for (int ow = 0; ow < OW; ++ow) {
|
||
|
float o = output[n * K * OH * OW + k * OH * OW + oh * OW + ow];
|
||
|
printf("output[%d][%d][%d][%d] : = %f\n", n, k, oh, ow, o); fflush(stdout);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
// printf("[mpi_rank:%d] ns[%d]:%d, ne[%d]:%d\n", mpi_rank, i, ns[i], i, ne[i]);
|
||
|
}
|
||
|
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];
|
||
|
}
|
||
|
//if(num_devices == (MAX_NUM_GPU - 1)) Nend[num_devices - 1] = ne[mpi_rank];
|
||
|
Nend[mpi_rank][num_devices - 1] = ne[mpi_rank];
|
||
|
/*
|
||
|
for (int i = 0; i < num_devices; i++) {
|
||
|
printf("[mpi_rank:%d, GPU:%d] ns[%d]:%d, ne[%d]:%d, Nbegin[%d][%d]:%d, Nend[%d][%d]:%d\n",
|
||
|
mpi_rank, i, mpi_rank, ns[mpi_rank], mpi_rank, ne[mpi_rank], mpi_rank, i, Nbegin[mpi_rank][i], mpi_rank, i, Nend[mpi_rank][i]);
|
||
|
fflush(stdout);
|
||
|
}
|
||
|
*/
|
||
|
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() );
|
||
|
}
|
||
|
|
||
|
}
|