chundoong-lab-ta/SamsungDS22/submissions/HW4/ys798.choi/mat_mul.cpp

192 lines
4.9 KiB
C++

#include "mat_mul.h"
#include "util.h"
#include <cstdio>
#include <cstdlib>
#include <mpi.h>
#include <omp.h>
static float *A, *B, *C;
static int M, N, K;
static int num_threads;
static int mpi_rank, mpi_world_size;
#define MASTER (0)
#define FROM_MASTER (1)
#define FROM_WORKER (2)
static int min(int x, int y) {
return x < y ? x : y;
}
/*
static void mat_mul_omp() {
// TODO: parallelize & optimize matrix multiplication
// Use num_threads per node
#pragma omp parallel for
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < K; ++k) {
C[i * N + j] += A[i * K + k] * B[k * N + j];
}
}
}
}
*/
#define TILEM (32)
#define TILEK (16)
#define TILEN (2048)
static void mat_mul_omp(int row) {
// int tid = (long)data;
int is = 0;//row / num_threads * tid + min(tid, row % num_threads);
int ie = row;// / num_threads * (tid + 1) + min(tid + 1, row % num_threads);
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
//#pragma omp parallel for num_threads(num_threads)
for (int ii = is; ii < ie; ii += TILEM) {
for (int kk = 0; kk < K; kk += TILEK) {
for (int jj = 0; jj < N; jj += TILEN) {
int ek = kk + TILEK < K? (kk + TILEK) : K;
int em = ii + TILEM < M? (ii + TILEM) : M;
int en = jj + TILEN < N? (jj + TILEN) : N;
for (int i = ii; i < em; ++i) {
for (int k = kk; k < ek; ++k) {
float Mux_val = A[i * K + k];
for (int j = jj; j < en; ++j) {
C[i * N + j] += Mux_val * B[k * N + j];
}
}
}
}
}
}
// return NULL;
}
/*
#define ITILESIZE (32)
#define JTILESIZE (2048)
#define KTILESIZE (32)
static void mat_mul_omp(int rows) {
omp_set_num_threads(num_threads);
#pragma omp parallel for
for (int ii = 0; ii < rows; ii += ITILESIZE) {
for (int jj = 0; jj < N; jj += JTILESIZE) {
for (int kk = 0; kk < K; kk += KTILESIZE) {
for (int k = kk; k < min(K, kk + KTILESIZE); k++) {
for (int i = ii; i < min(rows, ii + ITILESIZE); i++) {
float ar = A[i * K + k];
for (int j = jj; j < min(N, jj + JTILESIZE); j+=1) {
C[i * N + j] += ar * B[k * N + j];
}
}
}
}
}
}
// return NULL;
}
*/
void mat_mul(float *_A, float *_B, float *_C, int _M, int _N, int _K,
int _num_threads, int _mpi_rank, int _mpi_world_size) {
A = _A, B = _B, C = _C;
M = _M, N = _N, K = _K;
num_threads = _num_threads, mpi_rank = _mpi_rank,
mpi_world_size = _mpi_world_size;
int row_size, rows;
int start, end, offset;
MPI_Status status;
MPI_Request request;
// TODO: parallelize & optimize matrix multiplication on multi-node
// You must allocate & initialize A, B, C for non-root processes
// FIXME: for now, only root process runs the matrix multiplication.
if (mpi_rank ==0)
{
row_size = M / mpi_world_size;
for (int node=1; node<mpi_world_size; node++)
{
start = node * row_size;
offset = node * row_size;
end = (node == mpi_world_size-1) ? M : (node+1)*row_size;
rows = end - start;
MPI_Isend(&offset, 1, MPI_INT, node, FROM_MASTER, MPI_COMM_WORLD, &request);
MPI_Isend(&rows, 1, MPI_INT, node, FROM_MASTER, MPI_COMM_WORLD, &request);
MPI_Isend(&A[offset*K], rows*K, MPI_FLOAT, node, FROM_MASTER, MPI_COMM_WORLD, &request);
MPI_Isend(B, K*N, MPI_FLOAT, node, FROM_MASTER, MPI_COMM_WORLD, &request);
}
/*
for(int i=0; i<rows; i++)
for(int j=0; j<NCA; j++)
{
//C[i * NCB + k] = 0;
Mat = A[i * NCA + j];
for(int k=0; k<NCB; k++)
C[i * NCB + k] += Mat * B[j * NCB + k];
}
*/
rows = row_size;
mat_mul_omp(rows);
for (int node=1; node<mpi_world_size; node++)
{
MPI_Recv(&offset, 1, MPI_INT, node, FROM_WORKER, MPI_COMM_WORLD, &status);
MPI_Recv(&rows, 1, MPI_INT, node, FROM_WORKER, MPI_COMM_WORLD, &status);
MPI_Recv(&C[offset*N], rows*N, MPI_FLOAT, node, FROM_WORKER, MPI_COMM_WORLD, &status);
}
}
else
{
alloc_mat(&A, M, K);
alloc_mat(&B, K, N);
alloc_mat(&C, M, N);
MPI_Recv(&offset, 1, MPI_INT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
MPI_Recv(&rows, 1, MPI_INT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
MPI_Recv(A, rows*K, MPI_FLOAT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
MPI_Recv(B, K*N, MPI_INT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
/*
for(int i=0; i<rows; i++)
for(int j=0; j<NCA; j++)
{
//C[i * NCB + k] = 0;
Mat = A[i * NCA + j];
for(int k=0; k<NCB; k++)
C[i * NCB + k] += Mat * B[j * NCB + k];
}
*/
mat_mul_omp(rows);
MPI_Isend(&offset, 1, MPI_INT, MASTER, FROM_WORKER, MPI_COMM_WORLD, &request);
MPI_Isend(&rows, 1, MPI_INT, MASTER, FROM_WORKER, MPI_COMM_WORLD, &request);
MPI_Isend(C, rows*N, MPI_FLOAT, MASTER, FROM_WORKER, MPI_COMM_WORLD, &request);
}
}