chundoong-lab-ta/SamsungDS22/submissions/final/hj614.yoo/A/convolution.cpp

182 lines
5.1 KiB
C++

#include "convolution.h"
#include "util.h"
#include <mpi.h>
#include <stdio.h>
#include <immintrin.h>
#define VEC_BYTE (32)
#define VEC_SIZE (VEC_BYTE>>2)
#define VEC_NUM (2)
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 sn, en, firstSize, modN;
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 vec = S % (VEC_SIZE * VEC_NUM);
MPI_Request request[2];
MPI_Status status[2];
if (N > 1)
{
if (mpi_rank == 0)
{
int snTmp = firstSize;
int enTmp = N;
MPI_Isend(input+snTmp*C*H*W, (enTmp-snTmp)*C*H*W, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request[0]);
MPI_Isend(filter, K*C*R*S, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request[1]);
}
else
{
if (modN > 0)
{
alloc_tensor(&input, modN, C, H, W);
alloc_tensor(&output, modN, K, OH, OW);
alloc_tensor(&filter, K, C, R, S);
}
MPI_Irecv(input, modN*C*H*W, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request[0]);
MPI_Irecv(filter, K*C*R*S, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request[1]);
}
MPI_Waitall(2,request, status);
}
if (vec == 0 && stride == 1 && pad == 0 && dilation == 1)
{
#pragma omp parallel for collapse(4) schedule(dynamic, 4)
for (int n = 0; n < modN; ++n) {
for (int k = 0; k < K; ++k) {
for (int oh = 0; oh < OH; ++oh) {
for (int ow = 0; ow < OW; ++ow) {
__m256 vo[VEC_NUM];
vo[0] = _mm256_setzero_ps();
vo[1] = _mm256_setzero_ps();
for (int c = 0; c < C; ++c) {
__m256 vi[VEC_NUM], vf[VEC_NUM];
for (int r = 0; r < R; r=r+1) {
for (int s = 0; s < S; s=s+(VEC_SIZE * VEC_NUM)) {
int h = oh + r;
int w = ow + s;
vf[0] = _mm256_load_ps(&filter[k*C*R*S+c*R*S+r*S+s]);
vf[1] = _mm256_load_ps(&filter[k*C*R*S+c*R*S+r*S+s+8]);
vi[0] = _mm256_load_ps(&input[n*C*H*W+c*H*W+h*W+w]);
vi[1] = _mm256_load_ps(&input[n*C*H*W+c*H*W+h*W+w+8]);
vo[0] = _mm256_fmadd_ps(vf[0],vi[0],vo[0]);
vo[1] = _mm256_fmadd_ps(vf[1],vi[1],vo[1]);
}
}
}
vo[0] = _mm256_add_ps(vo[0],vo[1]);
float *out = (float *)&vo[0];
out[0] += out[1];
out[2] += out[3];
out[4] += out[5];
out[6] += out[7];
out[0] += out[2];
out[4] += out[6];
out[0] += out[4];
//alloc_tensor(&out, VEC_SIZE, 1, 1, 1);
//_mm256_store_ps(out,vo[0]);
output[n*K*OH*OW+k*OH*OW+oh*OW+ow] = out[0];
//free(out);
}
}
}
}
}
else
{
#pragma omp parallel for collapse(4) schedule(dynamic, 4)
for (int n = 0; n < modN; ++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;
}
}
}
}
}
if (N > 1)
{
if (mpi_rank == 0)
{
int snTmp = firstSize;
int enTmp = N;
MPI_Irecv(output+snTmp*K*OH*OW, (enTmp-snTmp)*K*OH*OW, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request[0]);
}
else
{
MPI_Isend(output, modN*K*OH*OW, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request[0]);
}
MPI_Wait(&request[0], &status[0]);
}
}
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;
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_world_size);
firstSize = (N + 1) / mpi_world_size;
if (mpi_rank == 0)
{
sn = 0;
en = firstSize;
modN = firstSize;
}
else
{
sn = firstSize;
en = N;
modN = N - firstSize;
}
}
void convolution_final(
int _N, int _C, int _H, int _W,
int _K, int _R, int _S,
int _pad, int _dilation, int _stride) {
if (mpi_rank != 0)
{
free(input);
free(output);
free(filter);
}
}