chundoong-lab-ta/SamsungDS22/submissions/HW4/jhyeon93.kim/mat_mul.cpp

135 lines
3.7 KiB
C++
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "mat_mul.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;
static int slice_node, slice;
static int min (int x, int y) { return (x < y) ? x : y;}
#define ITILESIZE (32)
#define JTILESIZE (1024)
#define KTILESIZE (1024)
static void mat_mul_omp(){//int tid) {
// TODO: parallelize & optimize matrix multiplication
// Use num_threads per node
#pragma omp parallel num_threads (num_threads)
{
int tid = omp_get_thread_num();
int is = slice_node / num_threads * tid + min(tid, slice_node % num_threads);
int ie = slice_node / num_threads * (tid + 1) + min(tid + 1, slice_node % num_threads);
for (int ii = is; ii < ie; 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(ie, ii + ITILESIZE); ++i){
float ar = A[i * K + k];
int row_C = i * N;
for (int j = jj; j < min(N, jj + JTILESIZE); ++j) {
C[row_C + j] += ar * B[k * N + j];
}
}
}
}
}
}
if (mpi_rank == 0 && tid == 0) {
int edge = slice_node * mpi_world_size;
if (edge < M) {
int is = edge;
int ie = M;
for (int ii = is; ii < ie; 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(ie, ii + ITILESIZE); ++i){
float ar = A[i * K + k];
int row_C = i * N;
for (int j = jj; j < min(N, jj + JTILESIZE); ++j) {
C[row_C + j] += ar * B[k * N + j];
}
}
}
}
}
}
}
}
}
}
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;
// TODO: parallelize & optimize matrix multiplication on multi-node
// You must allocate & initialize A, B, C for non-root processes
/*----------seperate---------------*/
slice_node = M / mpi_world_size;
int numElements_A = slice_node * K;
int numElements_C = slice_node * N;
MPI_Status status;
MPI_Request request = MPI_REQUEST_NULL;
if(mpi_rank == 0){
int offset = slice_node;
for(int i=1; i<mpi_world_size; ++i){
//MPI_Send(&A[offset * K], numElements_A, MPI_FLOAT, i, 0, MPI_COMM_WORLD );
MPI_Isend(&A[offset * K], numElements_A, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &request);
offset += slice_node;
}
} else {
A = (float*)malloc(numElements_A*sizeof(float));
B = (float*)malloc(K*N*sizeof(float));
C = (float*)malloc(numElements_C*sizeof(float));
MPI_Recv (A, numElements_A, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
}
MPI_Bcast(B, K*N, MPI_FLOAT, 0, MPI_COMM_WORLD);
/*-------------calculate-----------------------*/
// FIXME: for now, only root process runs the matrix multiplication.
if (slice_node < num_threads) num_threads = slice_node;
slice = slice_node / num_threads;
if (mpi_rank != 0){
for (int i=0; i<numElements_C; ++i) C[i] = 0;
}
mat_mul_omp();
//MPI_Barrier(MPI_COMM_WORLD);
/*--------Merge-----------------------*/
if (mpi_rank == 0){
int offset = slice_node;
for (int i=1; i<mpi_world_size; ++i){
MPI_Recv (&C[offset * N], numElements_C, MPI_FLOAT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
offset += slice_node;
}
} else {
//MPI_Send (C, numElements_C, MPI_FLOAT, 0, 0, MPI_COMM_WORLD );
MPI_Isend (C, numElements_C, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request );
}
//MPI_Barrier(MPI_COMM_WORLD);
}