118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
|
#include "mat_mul.h"
|
||
|
|
||
|
#include <cstdio>
|
||
|
#include <cstdlib>
|
||
|
#include <mpi.h>
|
||
|
|
||
|
#define MASTER (0)
|
||
|
#define FROM_MASTER (1)
|
||
|
#define FROM_WORKER (2)
|
||
|
|
||
|
#define ITILESIZE (32)
|
||
|
#define JTILESIZE (1024)
|
||
|
#define KTILESIZE (1024)
|
||
|
|
||
|
static float *A, *B, *C;
|
||
|
static int M, N, K;
|
||
|
static int num_threads;
|
||
|
static int mpi_rank, mpi_world_size;
|
||
|
static int offset, rows;
|
||
|
|
||
|
void _alloc_mat(float **m, int R, int C) {
|
||
|
*m = (float *)aligned_alloc(32, sizeof(float) * R * C);
|
||
|
if (*m == NULL) {
|
||
|
printf("Failed to allocate memory for matrix.\n");
|
||
|
exit(0);
|
||
|
}
|
||
|
}
|
||
|
void _zero_mat(float *m, int R, int C) { memset(m, 0, sizeof(float) * R * C); }
|
||
|
|
||
|
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
|
||
|
int is = 0;
|
||
|
int ie = rows;
|
||
|
|
||
|
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
|
||
|
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];
|
||
|
for (int j = jj; j < min(N, jj + JTILESIZE); j+=1) {
|
||
|
C[i * N + 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;
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
int row = M / mpi_world_size;
|
||
|
int start, end;
|
||
|
|
||
|
for(int dest = 1; dest < mpi_world_size; dest++)
|
||
|
{
|
||
|
start = offset = dest * row;
|
||
|
end = dest == mpi_world_size -1 ? M : (dest+1)*row;
|
||
|
rows = end - start;
|
||
|
MPI_Isend(&offset, 1, MPI_INT, dest, FROM_MASTER, MPI_COMM_WORLD, &request);
|
||
|
MPI_Isend(&rows, 1, MPI_INT, dest, FROM_MASTER, MPI_COMM_WORLD, &request);
|
||
|
MPI_Isend(&A[offset*K], rows*K, MPI_FLOAT, dest, FROM_MASTER, MPI_COMM_WORLD, &request);
|
||
|
MPI_Isend(B, K*N, MPI_FLOAT, dest, FROM_MASTER, MPI_COMM_WORLD, &request);
|
||
|
}
|
||
|
|
||
|
rows = row;
|
||
|
mat_mul_omp();
|
||
|
|
||
|
for(int dest = 1; dest < mpi_world_size; dest++)
|
||
|
{
|
||
|
MPI_Recv(&offset, 1, MPI_INT, dest, FROM_WORKER, MPI_COMM_WORLD, &status);
|
||
|
MPI_Recv(&rows, 1, MPI_INT, dest, FROM_WORKER, MPI_COMM_WORLD, &status);
|
||
|
MPI_Recv(&C[offset*N], rows*N, MPI_FLOAT, dest, FROM_WORKER, MPI_COMM_WORLD, &status);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_alloc_mat(&A, M, K);
|
||
|
_alloc_mat(&B, K, N);
|
||
|
_alloc_mat(&C, M, N);
|
||
|
_zero_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_FLOAT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
|
||
|
|
||
|
mat_mul_omp();
|
||
|
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|