197 lines
6.1 KiB
C++
197 lines
6.1 KiB
C++
|
#include "convolution.h"
|
||
|
#include <mpi.h>
|
||
|
#include <stdio.h>
|
||
|
#include "util.h"
|
||
|
|
||
|
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 num_threads;
|
||
|
|
||
|
|
||
|
#if 0
|
||
|
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;
|
||
|
|
||
|
if (mpi_rank == 0) {
|
||
|
|
||
|
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 = 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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
#else
|
||
|
|
||
|
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;
|
||
|
|
||
|
// Divide input to nodes (# of nodes: 1 ~ 2)
|
||
|
int N_s[mpi_world_size], N_e[mpi_world_size];
|
||
|
int K_s[mpi_world_size], K_e[mpi_world_size];
|
||
|
if (N != 1) {
|
||
|
for (int i = 0; i < mpi_world_size; i++) {
|
||
|
N_s[i] = N / mpi_world_size * i;
|
||
|
N_e[i] = N / mpi_world_size * (i + 1);
|
||
|
K_s[i] = 0;
|
||
|
K_e[i] = K;
|
||
|
}
|
||
|
N_e[mpi_world_size - 1] = N;
|
||
|
}
|
||
|
else if (K != 1) { // N = 1
|
||
|
for (int i = 0; i < mpi_world_size; i++) {
|
||
|
N_s[i] = 0;
|
||
|
N_e[i] = N;
|
||
|
K_s[i] = K / mpi_world_size * i;
|
||
|
K_e[i] = K / mpi_world_size * (i + 1);
|
||
|
}
|
||
|
K_e[mpi_world_size - 1] = K;
|
||
|
}
|
||
|
else { // N = 1 & K = 1
|
||
|
for (int i = 0; i < mpi_world_size; i++) {
|
||
|
N_s[i] = 0;
|
||
|
N_e[i] = N;
|
||
|
K_s[i] = 0;
|
||
|
K_e[i] = K;
|
||
|
}
|
||
|
if (mpi_world_size == 2) { //Use single node
|
||
|
N_e[1] = 0;
|
||
|
K_e[1] = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Scatter
|
||
|
if (mpi_rank == 0) {
|
||
|
for (int i = 1; i < mpi_world_size; i++) {
|
||
|
MPI_Send(&input[N_s[i] * C * H * W], ((N_e[i] - N_s[i]) * C * H * W), MPI_FLOAT, i, 0, MPI_COMM_WORLD);
|
||
|
MPI_Send(&filter[K_s[i] * C * R * S], ((K_e[i] - K_s[i]) * C * R * S), MPI_FLOAT, i, 0, MPI_COMM_WORLD);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
alloc_tensor(&input, N, C, H, W);
|
||
|
alloc_tensor(&output, N, K, OH, OW);
|
||
|
alloc_tensor(&filter, K, C, R, S);
|
||
|
|
||
|
MPI_Recv(&input[N_s[mpi_rank] * C * H * W], ((N_e[mpi_rank] - N_s[mpi_rank]) * C * H * W), MPI_FLOAT, 0, 0, MPI_COMM_WORLD, nullptr);
|
||
|
MPI_Recv(&filter[K_s[mpi_rank] * C * R * S], ((K_e[mpi_rank] - K_s[mpi_rank]) * C * R * S), MPI_FLOAT, 0, 0, MPI_COMM_WORLD, nullptr);
|
||
|
}
|
||
|
|
||
|
// Compute convolution
|
||
|
#pragma omp parallel for num_threads(num_threads) collapse(3) schedule(dynamic)
|
||
|
for (int n = N_s[mpi_rank]; n < N_e[mpi_rank]; ++n) {
|
||
|
for (int k = K_s[mpi_rank]; k < K_e[mpi_rank]; ++k) {
|
||
|
for (int oh = 0; oh < OH; ++oh) {
|
||
|
for (int ow = 0; ow < OW; ++ow) {
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Gather output
|
||
|
if (mpi_rank == 0) {
|
||
|
for (int i = 1; i < mpi_world_size; i++) {
|
||
|
if (N != 1) {
|
||
|
MPI_Recv(&output[N_s[i] * K_e[i] * OH * OW], ((N_e[i] - N_s[i]) * (K_e[i] - K_s[i]) * OH * OW), MPI_FLOAT, i, 1, MPI_COMM_WORLD, nullptr);
|
||
|
}
|
||
|
else if (K != 1) {
|
||
|
MPI_Recv(&output[N_e[i] * K_s[i] * OH * OW], ((N_e[i] - N_s[i]) * (K_e[i] - K_s[i]) * OH * OW), MPI_FLOAT, i, 1, MPI_COMM_WORLD, nullptr);
|
||
|
}
|
||
|
else {
|
||
|
MPI_Recv(&output[N_e[i] * K_e[i] * OH * OW], ((N_e[i] - N_s[i]) * (K_e[i] - K_s[i]) * OH * OW), MPI_FLOAT, i, 1, MPI_COMM_WORLD, nullptr);
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
if (N != 1) {
|
||
|
MPI_Send(&output[N_s[mpi_rank] * K_e[mpi_rank] * OH * OW], ((N_e[mpi_rank] - N_s[mpi_rank]) * (K_e[mpi_rank] - K_s[mpi_rank]) * OH * OW), MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
|
||
|
}
|
||
|
else if (K != 1) {
|
||
|
MPI_Send(&output[N_e[mpi_rank] * K_s[mpi_rank] * OH * OW], ((N_e[mpi_rank] - N_s[mpi_rank]) * (K_e[mpi_rank] - K_s[mpi_rank]) * OH * OW), MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
|
||
|
}
|
||
|
else {
|
||
|
MPI_Send(&output[N_e[mpi_rank] * K_e[mpi_rank] * OH * OW], ((N_e[mpi_rank] - N_s[mpi_rank]) * (K_e[mpi_rank] - K_s[mpi_rank]) * OH * OW), MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
#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;
|
||
|
|
||
|
num_threads = 100;
|
||
|
|
||
|
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) {
|
||
|
}
|