198 lines
5.2 KiB
C++
198 lines
5.2 KiB
C++
#include "mat_mul.h"
|
|
#include "util.h"
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <mpi.h>
|
|
#include <immintrin.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;
|
|
MPI_Status status;
|
|
MPI_Request request;
|
|
|
|
int numworkers,
|
|
source,
|
|
dest,
|
|
mtype,
|
|
rows,
|
|
averow, extra, offset,
|
|
i,j,k;
|
|
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;
|
|
//schedule(auto)
|
|
// #pragma
|
|
/*
|
|
for (int i = 0; i < M; ++i) {
|
|
for (int j = 0; j < N; ++j) {
|
|
for (int k = 0; k < K; ++k) {
|
|
C[i * N + j] += A[i * K + k] * B[k * N + j];
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
#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 ( k = kk; k < min(K, kk + KTILESIZE); k++) {
|
|
for ( i = ii; i < min(ie, ii + ITILESIZE); i++) {
|
|
float ar = A[i * K + k];
|
|
for ( j = jj; j < min(N, jj + JTILESIZE); j+=1) {
|
|
C[i * N + j] += ar * B[k * N + j];
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
for (int ii = is; ii < ie; ii += ITILESIZE) {
|
|
for (int jj = 0; jj < N; jj += JTILESIZE) {
|
|
for (int kk = 0; kk < K; kk += KTILESIZE) {
|
|
|
|
int forM= (ie<ii+ITILESIZE) ? ie : (ii+ITILESIZE);
|
|
int forK= (K<kk+KTILESIZE) ? K : (kk+KTILESIZE);
|
|
int forN= (N<jj+JTILESIZE) ? N : (jj+JTILESIZE);
|
|
for (int k = kk; k < forK; k++) {
|
|
for (int i = ii; i < forM; i++) {
|
|
float ar = A[i * K + k];
|
|
for (int j = jj; j < forN; 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;
|
|
// TODO: parallelize & optimize matrix multiplication on multi-node
|
|
// You must allocate & initialize A, B, C for non-root processes
|
|
/*
|
|
printf("hello from task %d on !\n", mpi_rank);
|
|
if(mpi_rank ==0)
|
|
printf("MASTER # of MPI task is : %d\n", mpi_world_size);
|
|
|
|
if(mpi_world_size < 2 ) {
|
|
printf("Need at least two MPI tasks. Quit...\n");
|
|
MPI_Abort(MPI_COMM_WORLD, rc);
|
|
exit(1);
|
|
}
|
|
*/
|
|
numworkers = mpi_world_size;
|
|
if(numworkers ==0) {
|
|
rows=M;
|
|
mat_mul_omp();
|
|
} else {
|
|
|
|
|
|
if(mpi_rank == MASTER) {
|
|
// printf("mpi_mm has started with %d tasks. \n", mpi_world_size);
|
|
// printf("Initial arrays...\n");
|
|
/*
|
|
for(i=0;i<M;i++) {
|
|
for(j=0;j<K;j++)
|
|
{A[i*K+j]=i+j;printf(" %f ", A[i*K+j]);}
|
|
printf("\n\n");
|
|
}
|
|
for(i=0;i<K;i++) {
|
|
for(j=0;j<N;j++)
|
|
{B[i*N+j]=i*j;printf(" %f ", B[i*N+j]);}
|
|
printf("\n");
|
|
}
|
|
*/
|
|
averow = M/numworkers;
|
|
int s, e;
|
|
extra = M%numworkers;
|
|
offset =0;
|
|
mtype=FROM_MASTER;
|
|
for(dest=1;dest<numworkers;dest++){
|
|
//rows=(dest+1<=extra) ? averow+1:averow;
|
|
//offset = offset+rows;
|
|
s=offset=dest*averow;
|
|
e=dest==numworkers-1 ? M : (dest+1)*averow;
|
|
rows=e-s;
|
|
printf("sending %d rows to task %d offset =%d\n", rows, dest, offset);
|
|
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= (extra > 0) ? averow+1:averow;
|
|
rows=averow;
|
|
mat_mul_omp();
|
|
mtype=FROM_WORKER;
|
|
for(i=1;i<numworkers;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);
|
|
printf("Received result from task %d\n", source);
|
|
}
|
|
|
|
} //MASTER ==0;
|
|
|
|
if(mpi_rank > MASTER) {
|
|
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);
|
|
/*
|
|
for(k=0;k<N; k++)
|
|
for(i=0; i<rows;i++) {
|
|
C[i*N+k]=0.0;
|
|
for(j=0;j<K;j++)
|
|
C[i*N+k]+=A[i*K+j]*B[j*N+k];
|
|
}
|
|
*/
|
|
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);
|
|
}
|
|
}
|
|
/*
|
|
for(k=0;k<N; k++)
|
|
for(i=0; i<M;i++)
|
|
printf(" %f " , C[i*N+k]);
|
|
|
|
printf("\n\n");
|
|
*/
|
|
// FIXME: for now, only root process runs the matrix multiplication.
|
|
//if (mpi_rank == 0)
|
|
// mat_mul_omp();
|
|
}
|