253 lines
7.3 KiB
Plaintext
253 lines
7.3 KiB
Plaintext
|
#include "convolution.h"
|
||
|
#include <mpi.h>
|
||
|
#include <omp.h>
|
||
|
#include <stdio.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 MAX_NUM_GPU 4
|
||
|
#define MAX_NUM_NODE 4
|
||
|
|
||
|
#define THREADS_PER_BLOCK 16
|
||
|
#define THREADS_DIM 4
|
||
|
#define WPT 8
|
||
|
#define RTS 2
|
||
|
|
||
|
int num_devices = 0;
|
||
|
|
||
|
__global__ void sgemm(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) {
|
||
|
|
||
|
int row = threadIdx.x;
|
||
|
int col = threadIdx.y;
|
||
|
int dim = threadIdx.z;
|
||
|
|
||
|
int oh = blockDim.x * blockIdx.x + threadIdx.x;
|
||
|
int ow = blockDim.y * blockIdx.y + threadIdx.y;
|
||
|
int k = blockDim.z * blockIdx.z + threadIdx.z;
|
||
|
|
||
|
__shared__ float fsub[THREADS_DIM][THREADS_PER_BLOCK][THREADS_PER_BLOCK];
|
||
|
|
||
|
int r_q = R / THREADS_PER_BLOCK;
|
||
|
int r_r = R % THREADS_PER_BLOCK;
|
||
|
if (r_r != 0)
|
||
|
r_q += 1;
|
||
|
|
||
|
int s_q = S / THREADS_PER_BLOCK;
|
||
|
int s_r = S % THREADS_PER_BLOCK;
|
||
|
if (s_r != 0)
|
||
|
s_q += 1;
|
||
|
|
||
|
|
||
|
for (int n = 0; n < N; n++) {
|
||
|
float temp = 0.0f;
|
||
|
for (int c = 0; c < C; c++) {
|
||
|
for (int rr = 0; rr < r_q; rr++) {
|
||
|
int f_row = THREADS_PER_BLOCK * rr + row;
|
||
|
for (int ss = 0; ss < s_q; ss++) {
|
||
|
int f_col = THREADS_PER_BLOCK * ss + col;
|
||
|
if (f_row >= R || f_col >= S || k >= K)
|
||
|
fsub[dim][row][col] = 0;
|
||
|
else
|
||
|
fsub[dim][row][col] = filter[k * C * R * S + c * R * S + f_row * S + f_col];
|
||
|
|
||
|
__syncthreads();
|
||
|
|
||
|
for (int r = 0; r < THREADS_PER_BLOCK; r++) {
|
||
|
int h = oh * stride - pad + (THREADS_PER_BLOCK * rr + r) * dilation;
|
||
|
for (int s = 0; s < THREADS_PER_BLOCK; s++) {
|
||
|
int w = ow * stride - pad + (THREADS_PER_BLOCK * ss + s) * dilation;
|
||
|
if (oh >= OH || ow >= OW || h < 0 || h >= H || w < 0 || w >= W) continue;
|
||
|
float i = input[n * C * H * W + c * H * W + h * W + w];
|
||
|
float f = fsub[dim][r][s];
|
||
|
temp += i * f;
|
||
|
}
|
||
|
}
|
||
|
//printf("temp : %f\n", temp);
|
||
|
__syncthreads();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (oh < OH && ow < OW && k < K)
|
||
|
output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = temp;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// Array of device (GPU) pointers
|
||
|
static float *a_d[MAX_NUM_GPU];
|
||
|
static float *b_d[MAX_NUM_GPU];
|
||
|
static float *c_d[MAX_NUM_GPU];
|
||
|
static int Mbegin[MAX_NUM_GPU], Mend[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;
|
||
|
|
||
|
int is[MAX_NUM_NODE], ie[MAX_NUM_NODE];
|
||
|
int count;
|
||
|
|
||
|
extern void alloc_tensor(float **t, int D0, int D1, int D2, int D3);
|
||
|
|
||
|
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;
|
||
|
|
||
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
||
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
||
|
|
||
|
MPI_Status status;
|
||
|
|
||
|
for (int i = 0; i < mpi_world_size; i++) {
|
||
|
is[i] = N / mpi_world_size * i;
|
||
|
ie[i] = N / mpi_world_size * (i + 1);
|
||
|
}
|
||
|
|
||
|
ie[mpi_world_size - 1] = N;
|
||
|
|
||
|
count = ie[mpi_rank] - is[mpi_rank];
|
||
|
|
||
|
if (mpi_rank == 0) {
|
||
|
for (int i = 1; i < mpi_world_size; i++) {
|
||
|
MPI_Send(&input[is[i] * C * H * W], (ie[i] - is[i]) * C * H * W, MPI_FLOAT, i, 0, MPI_COMM_WORLD);
|
||
|
}
|
||
|
} else {
|
||
|
alloc_tensor(&input, count, C, H, W);
|
||
|
alloc_tensor(&output, count, K, OH, OW);
|
||
|
alloc_tensor(&filter, K, C, R, S);
|
||
|
MPI_Recv(input, count * C * H * W, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
|
||
|
}
|
||
|
|
||
|
MPI_Bcast(filter, K * C * R * S, MPI_FLOAT, 0, MPI_COMM_WORLD);
|
||
|
|
||
|
// 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("NODE %d [GPU %d] %s\n", mpi_rank, 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] = (count / num_devices) * i;
|
||
|
Mend[i] = (count / num_devices) * (i + 1);
|
||
|
}
|
||
|
Mend[num_devices - 1] = count;
|
||
|
|
||
|
// Allocate device memory for each GPU
|
||
|
for (int i = 0; i < num_devices; i++) {
|
||
|
CUDA_CALL( cudaSetDevice(i) );
|
||
|
CUDA_CALL( cudaMalloc(&a_d[i], (Mend[i] - Mbegin[i]) * C * H * W * sizeof(float)) );
|
||
|
CUDA_CALL( cudaMalloc(&b_d[i], K * C * R * S * sizeof(float)) );
|
||
|
CUDA_CALL( cudaMalloc(&c_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(a_d[i], input + Mbegin[i] * C * H * W,
|
||
|
(Mend[i] - Mbegin[i]) * C * H * W * sizeof(float),
|
||
|
cudaMemcpyHostToDevice) );
|
||
|
CUDA_CALL( cudaMemcpy(b_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() );
|
||
|
}
|
||
|
|
||
|
// Launch kernel on every GPU
|
||
|
for (int i = 0; i < num_devices; i++) {
|
||
|
|
||
|
dim3 blockDim(THREADS_PER_BLOCK, THREADS_PER_BLOCK, THREADS_DIM);
|
||
|
dim3 gridDim(((OH + THREADS_PER_BLOCK -1) / THREADS_PER_BLOCK), ((OW + THREADS_PER_BLOCK -1) / THREADS_PER_BLOCK), (K + THREADS_DIM -1) / THREADS_DIM);
|
||
|
|
||
|
CUDA_CALL( cudaSetDevice(i) );
|
||
|
sgemm<<<gridDim, blockDim>>>(a_d[i], c_d[i], b_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( 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);
|
||
|
}
|
||
|
|
||
|
void convolution_final(
|
||
|
int _N, int _C, int _H, int _W,
|
||
|
int _K, int _R, int _S,
|
||
|
int _pad, int _dilation, int _stride) {
|
||
|
// Do any post-matmul cleanup work here.
|
||
|
|
||
|
MPI_Status status;
|
||
|
|
||
|
// Download C matrix from GPUs
|
||
|
for (int i = 0; i < num_devices; i++) {
|
||
|
CUDA_CALL( cudaMemcpy(output + Mbegin[i] * K * OH * OW, c_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( cudaDeviceSynchronize() );
|
||
|
}
|
||
|
|
||
|
if (mpi_rank == 0) {
|
||
|
for (int i = 1; i < mpi_world_size; i++) {
|
||
|
MPI_Recv(&output[is[i] * K * OH * OW], (ie[i] - is[i]) * K * OH * OW, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &status);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
MPI_Send(output, count * K * OH * OW, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
|
||
|
}
|
||
|
}
|