121 lines
3.2 KiB
C++
121 lines
3.2 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;
|
|
int rows;
|
|
|
|
#define bs 32
|
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
static void mat_mul_omp() {
|
|
// TODO: parallelize & optimize matrix multiplication
|
|
// Use num_threads per node
|
|
int chunk = rows / num_threads;
|
|
|
|
#pragma omp parallel num_threads(num_threads)
|
|
{
|
|
int pid;
|
|
|
|
pid = omp_get_thread_num();
|
|
|
|
int row_start = pid * chunk;
|
|
int row_end = pid == num_threads - 1 ? rows : (pid + 1) * chunk;
|
|
|
|
if ((N & 0b11) == 0){
|
|
for (int kk = 0; kk < K; kk += bs) {
|
|
for (int i = row_start; i < row_end; ++i) {
|
|
for (int k = kk; k < min(kk + bs, K); ++k) {
|
|
float AR = A[i * K + k];
|
|
for(int j= 0; j < N; j+=4) {
|
|
C[i * N + j] += AR * B[k * N + j];
|
|
C[i * N + j+1] += AR * B[k * N + j+1];
|
|
C[i * N + j+2] += AR * B[k * N + j+2];
|
|
C[i * N + j+3] += AR * B[k * N + j+3];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
for (int kk = 0; kk < K; kk += bs) {
|
|
for (int i = row_start; i < row_end; ++i) {
|
|
for (int k = kk; k < min(kk + bs, K); ++k) {
|
|
float AR = A[i * K + k];
|
|
for(int j = 0; j < N; j++) {
|
|
C[i * N + j] += AR * B[k * N + j];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} //#pragma omp paralle
|
|
}
|
|
|
|
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;
|
|
MPI_Status status;
|
|
MPI_Request request;
|
|
|
|
int node, start, end;
|
|
// 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){
|
|
int row_size = M / mpi_world_size;
|
|
|
|
for(node = 1; node < mpi_world_size; node++)
|
|
{
|
|
start = node * row_size;
|
|
|
|
if(node != (mpi_world_size - 1)) end = (node + 1) * row_size;
|
|
else end = M;
|
|
|
|
rows = end - start;
|
|
|
|
MPI_Isend(&rows, 1, MPI_INT, node, 1, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&A[start*K], rows*K, MPI_FLOAT, node, 2, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(B, K*N, MPI_FLOAT, node, 3, MPI_COMM_WORLD, &request);
|
|
}
|
|
|
|
rows = row_size;
|
|
mat_mul_omp();
|
|
|
|
for(node = 1; node < mpi_world_size; node++)
|
|
{
|
|
if(node == (mpi_world_size - 1)) rows = M - node*row_size;
|
|
|
|
start = node * row_size;
|
|
MPI_Recv(&C[start*N], rows*N, MPI_FLOAT, node, 1, MPI_COMM_WORLD, &status);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
alloc_mat(&A, M, K);
|
|
alloc_mat(&B, K, N);
|
|
alloc_mat(&C, M, N);
|
|
|
|
MPI_Recv(&rows, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(A, rows*K, MPI_FLOAT, 0, 2, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(B, K*N, MPI_FLOAT, 0, 3, MPI_COMM_WORLD, &status);
|
|
|
|
mat_mul_omp();
|
|
|
|
MPI_Isend(C, rows*N, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &request);
|
|
}
|
|
}
|