chundoong-lab-ta/SamsungDS22/submissions/final/sung_min.kim/tmp-A/convolution.cpp

179 lines
5.8 KiB
C++
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "convolution.h"
#include "util.h"
#include <mpi.h>
#include <stdio.h>
#include <immintrin.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 min(int x, int y) {
return x < y ? x : y;
}
#define C_TILESIZE (8)
#define S_TILESIZE (8)
#define R_TILESIZE (8)
void convolution_omp16(int ns, int ne)
{
#pragma omp parallel for num_threads(80) collapse(4) schedule(dynamic)
for (int n = ns; n < ne; ++n) { // # of output channel (= # of input image)
for (int k = 0; k < K; ++k) { // # of output image (filter) channel
for (int oh = 0; oh < OH; ++oh) { // output height (=row)
for (int ow = 0; ow < OW; ++ow) { // output width (=column)
__m512 vo = _mm512_setzero_ps();
for (int cc = 0; cc < C; cc += C_TILESIZE)
for (int rr = 0; rr < R; rr += R_TILESIZE)
for (int c = cc; c < min(C, cc+C_TILESIZE); ++c) { // # of input channel
for (int r = rr; r < min(R, rr+R_TILESIZE); ++r) { // filter height (=row)
int h = oh * stride - pad + r * dilation;
int w = ow * stride - pad;
if (h < 0 || h >= H || w < 0 || w >= W) continue;
int idx_i = (n * C * H * W) + (c * H * W) + (h * W) + w;
int idx_f = (k * C * R * S) + (c * R * S) + (r * S);
__m512 i = _mm512_loadu_ps( &input[idx_i]);
__m512 f = _mm512_loadu_ps(&filter[idx_f]);
vo = _mm512_fmadd_ps(i, f, vo);
}
}
// float* o = (float*)&vo;
float o[16];
_mm512_store_ps(o, vo);
output[(n * K * OH * OW) + (k * OH * OW) + (oh * OW) + ow] =
o[0] +o[1] +o[2] +o[3] +o[4] +o[5] +o[6] +o[7] +
o[8] +o[9] +o[10] +o[11] +o[12] +o[13] +o[14] +o[15];
// vo[0] +vo[1] +vo[2] +vo[3] +vo[4] +vo[5] +vo[6] +vo[7] ;
}
}
}
}
}
void convolution_omp(int ns, int ne)
{
#pragma omp parallel for num_threads(80) collapse(4) schedule(dynamic)
for (int n = ns; n < ne; ++n) { // # of output channel (= # of input image)
for (int k = 0; k < K; ++k) { // # of output image (filter) channel
for (int oh = 0; oh < OH; ++oh) { // output height (=row)
for (int ow = 0; ow < OW; ++ow) { // output width (=column)
float o = 0.f;
for (int cc = 0; cc < C; cc += C_TILESIZE)
for (int rr = 0; rr < R; rr += R_TILESIZE)
for (int ss = 0; ss < S; ss += S_TILESIZE)
for (int c = cc; c < min(C, cc+C_TILESIZE); ++c) { // # of input channel
for (int r = rr; r < min(R, rr+R_TILESIZE); ++r) { // filter height (=row)
for (int s = ss; s < min(S, ss+S_TILESIZE); ++s) { // filter width (=column)
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(
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;
// Allocate
if(mpi_rank != 0){
alloc_tensor(&input, N, C, H, W);
alloc_tensor(&output, N, K, OH, OW);
alloc_tensor(&filter, K, C, R, S);
}
// else
// printf("mpi_world_size = %d\n", mpi_world_size);
// N split
int ns[mpi_world_size], ne[mpi_world_size];
for (int i = 0; i < mpi_world_size; i++) {
ns[i] = N / mpi_world_size * i;
ne[i] = N / mpi_world_size * (i + 1);
}
ne[mpi_world_size - 1] = N;
// Scatter Input
int input_line = C*H*W;
if (mpi_rank == 0) {
for (int i = 1; i < mpi_world_size; i++) {
MPI_Send(input + ns[i] * input_line, (ne[i] - ns[i]) * input_line, MPI_FLOAT, i, 0,
MPI_COMM_WORLD);
}
} else {
MPI_Recv(input + ns[mpi_rank] * input_line, (ne[mpi_rank] - ns[mpi_rank]) * input_line, MPI_FLOAT,
0, 0, MPI_COMM_WORLD, nullptr);
}
// Broadcast Filter
int filter_size = K*C*R*S;
MPI_Bcast(filter, filter_size, MPI_FLOAT, 0, MPI_COMM_WORLD);
// convolution_omp16(ns[mpi_rank], ne[mpi_rank]);
if(S==16 && R==16 && dilation==1){
// printf("%d: omp16!\n", mpi_rank);
convolution_omp16(ns[mpi_rank], ne[mpi_rank]);
}else{
convolution_omp(ns[mpi_rank], ne[mpi_rank]);
}
// Gather Output
int output_line = K*OH*OW;
if (mpi_rank == 0) {
for (int i = 1; i < mpi_world_size; i++) {
MPI_Recv(output + ns[i] * output_line, (ne[i] - ns[i]) * output_line, MPI_FLOAT, i, 0,
MPI_COMM_WORLD, nullptr);
}
} else {
MPI_Send(output + ns[mpi_rank] * output_line, (ne[mpi_rank] - ns[mpi_rank]) * output_line, MPI_FLOAT, 0, 0,
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) {
}