141 lines
3.6 KiB
C++
141 lines
3.6 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;
|
|
|
|
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() {
|
|
// TODO: parallelize & optimize matrix multiplication
|
|
|
|
int i,j,k,ii,jj,kk;
|
|
float ar;
|
|
omp_set_num_threads(num_threads);
|
|
|
|
int slaveTaskCount = mpi_world_size;
|
|
|
|
int offset = M / slaveTaskCount * (mpi_rank) + min(mpi_rank, M % slaveTaskCount);
|
|
int rows = M / slaveTaskCount * (mpi_rank+1) + min(mpi_rank+1, M % slaveTaskCount) - offset;
|
|
|
|
|
|
|
|
|
|
|
|
#pragma omp parallel for private(i,j,k,ii,jj,kk,ar) shared(A,B,C)//schedule(dynamic,50) collapse(2)
|
|
for (ii = 0; ii <rows ; ii += ITILESIZE) {
|
|
for (jj = 0; jj < N; jj += JTILESIZE) {
|
|
for (kk = 0; kk < K; kk += KTILESIZE) {
|
|
|
|
for (k = kk; k < min(K,kk + KTILESIZE); k++) {
|
|
for (i = ii; i < min(rows,ii + ITILESIZE); i++) {
|
|
ar = A[i * K + k];
|
|
for (j = jj; j < min(N,jj + JTILESIZE); j+=1) {
|
|
C[i * N + j] += ar * B[k * N + j];
|
|
//printf("%f %f %f\n",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;
|
|
|
|
// 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.
|
|
|
|
int rows;
|
|
int source,dest;
|
|
|
|
int slaveTaskCount = mpi_world_size;
|
|
|
|
int offset=0;
|
|
int processId;
|
|
MPI_Status status;
|
|
MPI_Request request1,request2;
|
|
|
|
processId = mpi_rank;
|
|
|
|
|
|
if (processId == 0) {
|
|
for (dest=1; dest <= slaveTaskCount-1; dest++)
|
|
{
|
|
offset = M / slaveTaskCount * (dest) + min(dest, M % slaveTaskCount);
|
|
rows = M / slaveTaskCount * (dest+1) + min(dest+1, M % slaveTaskCount) - offset;
|
|
|
|
|
|
MPI_Isend(&offset, 1, MPI_INT, dest, 1, MPI_COMM_WORLD,&request1);
|
|
MPI_Isend(&rows, 1, MPI_INT, dest, 1, MPI_COMM_WORLD,&request2);
|
|
MPI_Isend(&A[offset*K], rows*K, MPI_FLOAT,dest,1, MPI_COMM_WORLD,&request1);
|
|
MPI_Isend(B, K*N, MPI_FLOAT, dest, 1, MPI_COMM_WORLD,&request2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
mat_mul_omp();
|
|
|
|
|
|
for (int i = 1; i <= slaveTaskCount-1; i++)
|
|
{
|
|
source = i;
|
|
MPI_Recv(&offset, 1, MPI_INT, source, 2, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, source, 2, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&C[offset*N], rows*N, MPI_FLOAT, source, 2, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if (processId > 0) {
|
|
|
|
source = 0;
|
|
|
|
MPI_Recv(&offset, 1, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
|
|
|
|
alloc_mat(&A, rows, K);
|
|
alloc_mat(&B, K, N);
|
|
alloc_mat(&C, rows, N);
|
|
|
|
|
|
MPI_Recv(A, rows*K, MPI_FLOAT, source, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(B, K*N, MPI_FLOAT, source, 1, MPI_COMM_WORLD, &status);
|
|
|
|
|
|
zero_mat(C,rows,N);
|
|
mat_mul_omp();
|
|
|
|
MPI_Send(&offset, 1, MPI_INT, 0, 2, MPI_COMM_WORLD);
|
|
MPI_Send(&rows, 1, MPI_INT, 0, 2, MPI_COMM_WORLD);
|
|
MPI_Send(C, rows*N, MPI_FLOAT, 0, 2, MPI_COMM_WORLD);
|
|
}
|
|
}
|