108 lines
3.0 KiB
C++
108 lines
3.0 KiB
C++
#include "mat_mul.h"
|
|
#include "util.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <mpi.h>
|
|
#include <stdio.h>
|
|
#include <algorithm>
|
|
#include <immintrin.h>
|
|
|
|
#define MASTER 0
|
|
#define FROM_MASTER 1
|
|
#define FROM_WORKER 2
|
|
|
|
#define ITS 64
|
|
#define JTS 2048
|
|
#define KTS 8
|
|
|
|
static float *A, *B, *C;
|
|
static int M, N, K;
|
|
static int num_threads;
|
|
static int mpi_rank, mpi_world_size;
|
|
int rows, numworkers, source, dest, mtype,
|
|
averow, offset;
|
|
|
|
MPI_Status status;
|
|
MPI_Request request;
|
|
|
|
static void mat_mul_omp(){
|
|
#pragma omp parallel for num_threads(num_threads) schedule (dynamic) //collapse(2) schedule(static)
|
|
for(int ii=0; ii<rows; ii += ITS){
|
|
for(int jj=0; jj<N; jj+=JTS){
|
|
for(int kk=0; kk<K; kk+=KTS){
|
|
for(int i=ii; i<std::min(rows, ii+ITS); i++){
|
|
for(int k=kk; k<std::min(K, kk+KTS); k++){
|
|
for(int j=jj; j<std::min(N, jj+JTS); 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
|
|
|
|
// FIXME: for now, only root process runs the matrix multiplication.
|
|
|
|
// Master node
|
|
if(mpi_rank == MASTER){
|
|
averow = M/mpi_world_size;
|
|
offset = 0;
|
|
|
|
mtype = FROM_MASTER;
|
|
for(int dest=1; dest<mpi_world_size; dest++){
|
|
offset = dest*averow;
|
|
|
|
if(dest==mpi_world_size-1) rows = M - dest*averow;
|
|
else rows = averow;
|
|
|
|
MPI_Isend(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&A[offset*K], rows*K, MPI_FLOAT, dest, mtype, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(B, K*N, MPI_FLOAT, dest, mtype, MPI_COMM_WORLD, &request);
|
|
}
|
|
rows = averow;
|
|
mat_mul_omp();
|
|
|
|
mtype=FROM_WORKER;
|
|
for(int i=1; i<mpi_world_size; i++){
|
|
source = i;
|
|
MPI_Recv(&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&C[offset*N], rows*N, MPI_FLOAT, source, mtype, MPI_COMM_WORLD, &status);
|
|
}
|
|
}
|
|
|
|
// Worker node
|
|
else{
|
|
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_Isend(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(C, rows*N, MPI_FLOAT, MASTER, mtype, MPI_COMM_WORLD, &request);
|
|
}
|
|
}
|