chundoong-lab-ta/SamsungDS22/submissions/final/c.w.son/A/convolution.cpp.final

218 lines
6.9 KiB
Plaintext

#include "convolution.h"
#include "util.h"
#include <mpi.h>
#include <stdio.h>
#include <omp.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 thread_num = 20;
#define MASTER_TO_SLAVE_TAG 1 //tag for messages sent from master to slaves
#define SLAVE_TO_MASTER_TAG 4 //tag for messages sent from slaves to master
MPI_Request request;
MPI_Status status;
static int min(int x, int y) {
return x < y ? x : y;
}
void static convolution_micro(
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, int start_n, int end_n) {
input = _input;
output = _output;
filter = _filter;
int cal_threads = 10;
int cal_size = end_n - start_n;
if(cal_size > 20) cal_threads = 20;
else cal_threads = cal_size;
/* int preval1_1= C * H * W;
int preval1_2 = H * W;
int preval2_1 = C * R * S;
int preval2_2 = R * S;
int preval3_1 = K * OH * OW;
int preval3_2 = OH * OW;
*/
#pragma omp parallel for num_threads(cal_threads) schedule(guided, 1)
for (int n = start_n; n < end_n; ++n) {
//#pragma omp parallel for num_threads(cal_threads) schedule(guided, 1)
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;
}
}
}
}
}
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_n;
int start_n;
int end_n;
int start_n_no0;
int end_n_no0;
if(mpi_rank == 0){
for(int node=0; node< mpi_world_size; node++){
size_n = N / (mpi_world_size);
start_n = (node)*size_n;
if(node == 0) start_n_no0 = start_n;
if(((node+1) == mpi_world_size) && ((N%(mpi_world_size)) != 0)){
end_n = N;
if(node == 0) end_n_no0 = end_n;
}
else{
end_n = start_n + size_n;
if(node == 0) end_n_no0 = end_n;
}
if(node >= 1){
MPI_Send(&start_n, 1, MPI_INT, node, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD);
//printf("SEND start_n (%d) to (%d)\n", start_n, node);
MPI_Send(&end_n, 1, MPI_INT, node, MASTER_TO_SLAVE_TAG + 1, MPI_COMM_WORLD);
//printf("SEND end_n (%d) to (%d)\n", end_n, node);
int sizeBuf = (end_n - start_n) * C * H * W;
//printf("SEND input Buff Len : (%d) to (%d)\n", sizeBuf, node);
MPI_Send(&input[start_n*C * H * W], sizeBuf, MPI_FLOAT, node, MASTER_TO_SLAVE_TAG + 2, MPI_COMM_WORLD);
//printf("SEND input size (%d) to (%d)\n", sizeBuf, node);
}
}
}
else{
alloc_tensor(&input, N, C, H, W);
MPI_Recv(&start_n, 1, MPI_INT, 0, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD, &status);
//printf("RECV start_n (%d) at (%d)\n", start_n, mpi_rank);
MPI_Recv(&end_n, 1, MPI_INT, 0, MASTER_TO_SLAVE_TAG + 1, MPI_COMM_WORLD, &status);
//printf("RECV end_n (%d) at (%d)\n", end_n, mpi_rank);
int sizeBuf = (end_n - start_n) * C * H * W;
//printf("RECV input Buff Len : %d \n", sizeBuf);
MPI_Recv(&input[start_n*C * H * W], sizeBuf, MPI_FLOAT, 0, MASTER_TO_SLAVE_TAG + 2, MPI_COMM_WORLD, &status);
//printf("RECV input size (%d) at (%d)\n", sizeBuf, mpi_rank);
}
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
if(mpi_rank > 0){
alloc_tensor(&filter, K, C, R, S);
}
MPI_Bcast(&filter[0], K*C*R*S, MPI_FLOAT, 0, MPI_COMM_WORLD);
//printf("Bcast filter (%d) to (%d) \n", K*C*R*S, mpi_rank);
if(mpi_rank > 0){
alloc_tensor(&output, N, K, OH, OW);
}
//MPI_Bcast(&output[0], N*K*OH*OW, MPI_FLOAT, 0, MPI_COMM_WORLD);
//printf("Bcast output (%d) to (%d) \n", N*K*OH*OW, mpi_rank);
MPI_Barrier(MPI_COMM_WORLD);
if(mpi_rank == 0){
convolution_micro(input, output, filter, N, C, H, W, K, R, S, pad, dilation, stride, start_n_no0, end_n_no0);
}
else{
convolution_micro(input, output, filter, N, C, H, W, K, R, S, pad, dilation, stride, start_n, end_n);
if(mpi_rank >= 1){
MPI_Send(&start_n, 1, MPI_INT, 0, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD);
//printf("SEND output start_n (%d) to (%d)\n", start_n, 0);
MPI_Send(&end_n, 1, MPI_INT, 0, SLAVE_TO_MASTER_TAG + 1, MPI_COMM_WORLD);
//printf("SEND output end_n (%d) to (%d)\n", end_n, 0);
int sizeBuf = (end_n - start_n) * K*OH*OW;
//printf("SEND output Buff Len : (%d) to (%d)\n", sizeBuf, 0);
MPI_Send(&output[start_n*K*OH*OW], sizeBuf, MPI_FLOAT, 0, SLAVE_TO_MASTER_TAG + 2, MPI_COMM_WORLD);
//printf("SEND output size (%d) to (%d)\n", sizeBuf, 0);
}
}
if(mpi_rank == 0){
OH = (H + 2 * pad - dilation * (R - 1) - 1) / stride + 1;
OW = (W + 2 * pad - dilation * (S - 1) - 1) / stride + 1;
for (int node = 1; node < mpi_world_size; node++) {// untill all slaves have handed back the processed data
//receive low bound from a slave
MPI_Recv(&start_n, 1, MPI_INT, node, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD, &status);
//receive upper bound from a slave
MPI_Recv(&end_n, 1, MPI_INT, node, SLAVE_TO_MASTER_TAG + 1, MPI_COMM_WORLD, &status);
//receive processed data from a slave
MPI_Recv(&output[start_n*K*OH*OW], (end_n - start_n) * K*OH*OW, MPI_FLOAT, node, SLAVE_TO_MASTER_TAG + 2, MPI_COMM_WORLD, &status);
}
}
}
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) {
}