170 lines
6.7 KiB
Plaintext
170 lines
6.7 KiB
Plaintext
#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;
|
|
|
|
int num_threads = 160;
|
|
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 size[2];
|
|
MPI_Request request;
|
|
MPI_Status status;
|
|
if ( mpi_world_size ==2) size[1] = _N/2; //TBC
|
|
else size[1] = 0;
|
|
size[0] = N - size[1];
|
|
|
|
|
|
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
|
|
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
|
|
|
|
if (mpi_rank == 0 && mpi_world_size ==2)
|
|
{
|
|
MPI_Isend(&input[size[0]*C*H*W], size[1]*C*H*W, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(filter, K*C*R*S, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &request);
|
|
}
|
|
else if(mpi_world_size ==2)
|
|
{
|
|
alloc_tensor(&input, size[1], C, H, W);
|
|
alloc_tensor(&output, size[1], K, OH, OW);
|
|
alloc_tensor(&filter, K, C, R, S);
|
|
MPI_Recv(input, size[1]*C*H*W, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(filter, K*C*R*S, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
if (C%8==0 && R==16 && S==16 && dilation==1 && pad==0 && stride==1)
|
|
{
|
|
//#pragma omp parallel for num_threads(num_threads) collapse(3) schedule(dynamic)
|
|
//#pragma omp parallel for num_threads(num_threads) collapse(3) schedule(dynamic)
|
|
#pragma omp parallel for num_threads(num_threads) collapse(3) schedule(dynamic)
|
|
for (int n = 0; n < size[mpi_rank]; ++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=c+16) {
|
|
for (int c = 0; c < C; c=c+8) {
|
|
for (int r = 0; r < 16; ++r) {
|
|
int h = oh + r;
|
|
//if (h < 0 || h >= H) continue;
|
|
for (int s = 0; s < 16; ++s) {
|
|
int w = ow + s ;
|
|
//if (w < 0 || w >= W) continue;
|
|
float i0 = input[n * C * H * W + c * H * W + h * W + w];
|
|
float f0 = filter[k * C * R * S + c * R * S + r * S + s];
|
|
float i1 = input[n * C * H * W + (c+1) * H * W + h * W + w];
|
|
float f1 = filter[k * C * R * S + (c+1) * R * S + r * S + s];
|
|
float i2 = input[n * C * H * W + (c+2) * H * W + h * W + w];
|
|
float f2 = filter[k * C * R * S + (c+2) * R * S + r * S + s];
|
|
float i3 = input[n * C * H * W + (c+3) * H * W + h * W + w];
|
|
float f3 = filter[k * C * R * S + (c+3) * R * S + r * S + s];
|
|
float i4 = input[n * C * H * W + (c+4) * H * W + h * W + w];
|
|
float f4 = filter[k * C * R * S + (c+4) * R * S + r * S + s];
|
|
float i5 = input[n * C * H * W + (c+5) * H * W + h * W + w];
|
|
float f5 = filter[k * C * R * S + (c+5) * R * S + r * S + s];
|
|
float i6 = input[n * C * H * W + (c+6) * H * W + h * W + w];
|
|
float f6 = filter[k * C * R * S + (c+6) * R * S + r * S + s];
|
|
float i7 = input[n * C * H * W + (c+7) * H * W + h * W + w];
|
|
float f7 = filter[k * C * R * S + (c+7) * R * S + r * S + s];
|
|
|
|
//float i8 = input[n * C * H * W + (c+8) * H * W + h * W + w];
|
|
//float f8 = filter[k * C * R * S + (c+8) * R * S + r * S + s];
|
|
//float i9 = input[n * C * H * W + (c+9) * H * W + h * W + w];
|
|
//float f9 = filter[k * C * R * S + (c+9) * R * S + r * S + s];
|
|
//float i10 = input[n * C * H * W + (c+10) * H * W + h * W + w];
|
|
//float f10 = filter[k * C * R * S + (c+10) * R * S + r * S + s];
|
|
//float i11 = input[n * C * H * W + (c+11) * H * W + h * W + w];
|
|
//float f11 = filter[k * C * R * S + (c+11) * R * S + r * S + s];
|
|
//float i12 = input[n * C * H * W + (c+12) * H * W + h * W + w];
|
|
//float f12 = filter[k * C * R * S + (c+12) * R * S + r * S + s];
|
|
//float i13 = input[n * C * H * W + (c+13) * H * W + h * W + w];
|
|
//float f13 = filter[k * C * R * S + (c+13) * R * S + r * S + s];
|
|
//float i14 = input[n * C * H * W + (c+14) * H * W + h * W + w];
|
|
//float f14 = filter[k * C * R * S + (c+14) * R * S + r * S + s];
|
|
//float i15 = input[n * C * H * W + (c+15) * H * W + h * W + w];
|
|
//float f15 = filter[k * C * R * S + (c+15) * R * S + r * S + s];
|
|
|
|
//o += i0*f0 + i1*f1 + i2*f2 + i3*f3 + i4*f4 + i5*f5 + i6*f6 + i7*f7
|
|
// +i8*f8 + i9*f9 + i10*f10 + i11*f11 + i12*f12 + i13*f13 + i14*f14 + i15*f15 ;
|
|
o += i0*f0 + i1*f1 + i2*f2 + i3*f3 + i4*f4 + i5*f5 + i6*f6 + i7*f7;
|
|
}//s
|
|
}//r
|
|
}//c
|
|
output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o;
|
|
}//ow
|
|
}//oh
|
|
}//k
|
|
}//n
|
|
}
|
|
else
|
|
{
|
|
#pragma omp parallel for num_threads(num_threads) collapse(3) schedule(dynamic)
|
|
for (int n = 0; n < size[mpi_rank]; ++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;
|
|
}//s
|
|
}//r
|
|
}//c
|
|
output[n * K * OH * OW + k * OH * OW + oh * OW + ow] = o;
|
|
}//ow
|
|
}//oh
|
|
}//k
|
|
}//n
|
|
}
|
|
|
|
if (mpi_rank == 0 && mpi_world_size ==2)
|
|
{
|
|
MPI_Recv(&output[size[0]*K*OH*OW], size[1]*K*OH*OW, MPI_FLOAT, 1, 0, MPI_COMM_WORLD, &status);
|
|
}
|
|
else if(mpi_world_size ==2)
|
|
{
|
|
MPI_Isend(output, size[1]*K*OH*OW, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
}
|