175 lines
4.8 KiB
C++
175 lines
4.8 KiB
C++
#include "mat_mul.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <mpi.h>
|
|
#include <omp.h>
|
|
#include "util.h"
|
|
|
|
static float *A, *B, *C;
|
|
static int M, N, K;
|
|
static int num_threads;
|
|
static int mpi_rank, mpi_world_size;
|
|
|
|
// define
|
|
#define NRA M // number of rows in Matrix A
|
|
#define NCA K // number of cols in Matrix A
|
|
#define NCB N // number of cols in Matrix B
|
|
#define MASTER 0 // number of cols in Matrix B
|
|
#define FROM_MASTER 1 // number of cols in Matrix B
|
|
#define FROM_WORKER 2 // number of cols in Matrix B
|
|
#define TM 32
|
|
#define TK 16
|
|
#define TN 2048
|
|
|
|
static int rows[4] = {0,}; // rows of mat A set to each worker
|
|
static int offset[4] = {0,}; // rows of mat A set to each worker
|
|
|
|
static void mat_mul_omp()
|
|
{
|
|
int start = 0;
|
|
int end = rows[mpi_rank];
|
|
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
|
|
for(int ii=start; ii<end; ii+=TM) {
|
|
for(int kk=0; kk<K; kk+=TK) {
|
|
for(int jj=0; jj<N; jj+=TN) {
|
|
int ek = (kk + TK < K) ? (kk + TK) : K;
|
|
int em = (ii + TM < M) ? (ii + TM) : M;
|
|
int en = (jj + TN < N) ? (jj + TN) : N;
|
|
|
|
for(int i=ii; i<em; ++i) {
|
|
for(int k=kk; k<ek; ++k) {
|
|
for(int j=jj; j<en; ++j) {
|
|
C[i*N + j] += A[i*K + k] * 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
|
|
// TODO: parallelize & optimize matrix multiplication
|
|
//
|
|
MPI_Request request;
|
|
MPI_Status status;
|
|
|
|
// number of worker tasks
|
|
int numworkers = mpi_world_size;
|
|
|
|
int mtype; // message type
|
|
|
|
int node_row = _M/numworkers;
|
|
int extra_row = _M - (node_row * (numworkers-1));
|
|
|
|
for (int i = 0; i < numworkers; i++){
|
|
rows[i] = (i == (numworkers-1)) ? extra_row : node_row;
|
|
}
|
|
|
|
for (int i = 0; i < numworkers-1; i++){
|
|
offset[i+1] = offset[i] + rows[i];
|
|
}
|
|
|
|
/*
|
|
A: M x K || NRA = M;
|
|
B: K x N || NCA = K;
|
|
C: M x N || NCB = N;
|
|
|
|
MPI_Send(start_buf_addr_of_sender,
|
|
num_of_send_data,
|
|
data_type_of_send_data,
|
|
rank_of_receiver,
|
|
TAG,
|
|
Communicator_handler);
|
|
|
|
MPI_Recv(start_buf_addr_of_receiver,
|
|
num_of_recv_data,
|
|
data_type_of_recv_data,
|
|
rank_of_sender,
|
|
TAG,
|
|
Communicator_handler,
|
|
MPI_status);
|
|
*/
|
|
|
|
if(mpi_rank != MASTER) {
|
|
M=rows[mpi_rank];
|
|
alloc_mat(&A, rows[mpi_rank], K);
|
|
alloc_mat(&B, K, N);
|
|
alloc_mat(&C, rows[mpi_rank], N);
|
|
}
|
|
mtype = MASTER;
|
|
|
|
MPI_Bcast(B, K*N, MPI_FLOAT, mtype, MPI_COMM_WORLD);
|
|
// Master
|
|
if(mpi_rank == MASTER) {
|
|
for(int i=1; i<numworkers; i++) {
|
|
//rows = (dest == (numworkers-1)) ? extra_row : node_row;
|
|
//offset = node_row*dest;
|
|
//printf("offset=%d, rows=%d, dest=%d\n",offset, rows, dest);
|
|
//printf("node_row=%d, extra_row=%d\n",node_row, extra_row);
|
|
MPI_Isend(&A[offset[i]*K], rows[i]*K, MPI_FLOAT, i, mtype, MPI_COMM_WORLD, &request);
|
|
|
|
//MPI_Send(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD);
|
|
//MPI_Send(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD);
|
|
//MPI_Send(&A[offset*K], rows*K, MPI_FLOAT, dest, mtype, MPI_COMM_WORLD);
|
|
//MPI_Send(&B[0], K*N, MPI_FLOAT, dest, mtype, MPI_COMM_WORLD);
|
|
}
|
|
}
|
|
else {
|
|
MPI_Recv(A, rows[mpi_rank] * K, MPI_FLOAT, 0, mtype, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
mat_mul_omp();
|
|
if(mpi_rank != MASTER) {
|
|
MPI_Isend(C, rows[mpi_rank]*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request);
|
|
}
|
|
else {
|
|
for(int i=1; i<numworkers; i++) {
|
|
MPI_Recv(&C[offset[i] * N], rows[i] * N, MPI_FLOAT, i, mtype, MPI_COMM_WORLD, &status);
|
|
}
|
|
}
|
|
|
|
//for(dest=1; dest<numworkers; dest++) {
|
|
// MPI_Recv(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD, &status);
|
|
// MPI_Recv(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD, &status);
|
|
// MPI_Recv(&C[offset*N], rows*N, MPI_FLOAT, dest, mtype, MPI_COMM_WORLD, &status);
|
|
//}
|
|
|
|
|
|
//else {
|
|
//printf("RECEIVER\n");
|
|
// alloc_mat(&A, M, K);
|
|
// alloc_mat(&B, K, N);
|
|
// alloc_mat(&C, M, N);
|
|
// zero_mat(C, M, N);
|
|
//
|
|
// mtype = FROM_MASTER;
|
|
// MPI_Recv(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status);
|
|
// MPI_Recv(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status);
|
|
// MPI_Recv(A, rows*K, MPI_FLOAT, MASTER, mtype, MPI_COMM_WORLD, &status);
|
|
// MPI_Recv(B, K*N, MPI_FLOAT, MASTER, mtype, MPI_COMM_WORLD, &status);
|
|
//
|
|
// mat_mul_omp();
|
|
//
|
|
// mtype = FROM_WORKER;
|
|
// MPI_Send(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD);
|
|
// MPI_Send(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD);
|
|
// MPI_Send(C, rows*N, MPI_FLOAT, MASTER, mtype, MPI_COMM_WORLD);
|
|
//}
|
|
|
|
//// FIXME: for now, only root process runs the matrix multiplication.
|
|
//if (mpi_rank == 0)
|
|
// // mat_mul_omp();
|
|
// mat_mul_omp();
|
|
}
|