166 lines
4.8 KiB
C++
166 lines
4.8 KiB
C++
|
#include "mat_mul.h"
|
||
|
|
||
|
#include <cstdio>
|
||
|
#include <cstdlib>
|
||
|
#include <mpi.h>
|
||
|
#include <omp.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;
|
||
|
}
|
||
|
MPI_Request request;
|
||
|
MPI_Status status;
|
||
|
#define ITILESIZE (32)
|
||
|
#define JTILESIZE (1024)
|
||
|
#define KTILESIZE (1024)
|
||
|
static void mat_mul_omp(int tid,int bounds) {
|
||
|
// TODO: parallelize & optimize matrix multiplication
|
||
|
// Use num_threads per node
|
||
|
int i,j,k,ii,jj,kk;
|
||
|
float ar=0.0;
|
||
|
int is, ie;
|
||
|
//int per_process = M/mpi_world_size;
|
||
|
//MPI_Status status;
|
||
|
|
||
|
//omp_set_num_threads(num_threads);
|
||
|
|
||
|
//int tid;
|
||
|
// #pragma omp parallel shared(A,B,C) private(i,j,k,ii,jj,kk,tid,is,ie,ar)
|
||
|
//{
|
||
|
// tid = omp_get_thread_num();
|
||
|
|
||
|
is = bounds / num_threads * tid + min(tid, bounds % num_threads);
|
||
|
ie = bounds / num_threads * (tid + 1) + min(tid + 1, bounds % num_threads);
|
||
|
//int interval = M/mpi_world_size;
|
||
|
//if(mpi_rank == mpi_world_size-1) interval = interval + M%mpi_world_size;
|
||
|
for (ii = is; ii < ie; 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(ie, 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];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
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;
|
||
|
|
||
|
int i;
|
||
|
int cal_node = mpi_world_size;
|
||
|
int offset = M/cal_node;
|
||
|
int bound = (mpi_rank == mpi_world_size-1)? M/cal_node + M%cal_node : M/cal_node;
|
||
|
// 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) {
|
||
|
offset = M/cal_node;
|
||
|
for(i=1; i<mpi_world_size; ++i)
|
||
|
{
|
||
|
bound = (i == mpi_world_size-1)? M/cal_node +(M%cal_node) : M/cal_node;
|
||
|
MPI_Isend(&A[offset*K],bound*K,MPI_FLOAT,i,0,MPI_COMM_WORLD,&request);
|
||
|
MPI_Isend(B,K*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,&request);
|
||
|
//MPI_Send(&A[offset*K],bound*K,MPI_FLOAT,i,0,MPI_COMM_WORLD);
|
||
|
//MPI_Send(B,K*N,MPI_FLOAT,i,0,MPI_COMM_WORLD);
|
||
|
//MPI_Irecv(&C[offset*N],bound*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,&request);
|
||
|
//MPI_Recv(&C[offset*N],bound*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,&status);
|
||
|
offset += bound;
|
||
|
}
|
||
|
//MPI_Wait(&request, &status);
|
||
|
#pragma omp parallel num_threads(num_threads)
|
||
|
{
|
||
|
#pragma omp for nowait
|
||
|
for(i=0; i<num_threads; ++i)
|
||
|
mat_mul_omp(i,bound);
|
||
|
#pragma omp barrier
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/* for(i=1; i<mpi_world_size; ++i) {
|
||
|
//MPI_Irecv(&C[offset*N],bound*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,&request);
|
||
|
MPI_Recv(&C[offset*N],bound*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,&status);
|
||
|
|
||
|
//MPI_Wait(&request, &status);
|
||
|
} */
|
||
|
|
||
|
// MPI_Wait(&request, &status);
|
||
|
}
|
||
|
else {
|
||
|
A = (float*)malloc(sizeof(float)*bound*K);
|
||
|
B = (float*)malloc(sizeof(float)*K*N);
|
||
|
C = (float*)malloc(sizeof(float)*bound*N);
|
||
|
|
||
|
//MPI_Irecv(A,bound*K,MPI_FLOAT,0,0,MPI_COMM_WORLD,&request);
|
||
|
//MPI_Irecv(B,K*N,MPI_FLOAT,0,0,MPI_COMM_WORLD,&request);
|
||
|
|
||
|
MPI_Recv(A,bound*K,MPI_FLOAT,0,0,MPI_COMM_WORLD,&status);
|
||
|
MPI_Recv(B,K*N,MPI_FLOAT,0,0,MPI_COMM_WORLD,&status);
|
||
|
#pragma omp parallel num_threads(num_threads)
|
||
|
{
|
||
|
|
||
|
#pragma omp for nowait
|
||
|
for(i=0; i<num_threads; ++i)
|
||
|
mat_mul_omp(i,bound);
|
||
|
#pragma omp barrier
|
||
|
}
|
||
|
|
||
|
//MPI_Isend(C, bound*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD,&request);
|
||
|
//MPI_Send(C, bound*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
|
||
|
//MPI_Wait(&request, &status);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
//#pragma omp parallel num_threads(num_threads)
|
||
|
//{
|
||
|
// #pragma omp for nowait
|
||
|
// for(i=0; i<num_threads; ++i)
|
||
|
// mat_mul_omp(i,bound);
|
||
|
// #pragma omp barrier
|
||
|
|
||
|
//}
|
||
|
|
||
|
|
||
|
|
||
|
if (mpi_rank == 0) {
|
||
|
offset = M/cal_node;
|
||
|
for(i=1; i<mpi_world_size; ++i){
|
||
|
bound = (i == mpi_world_size-1)? M/cal_node + (M%cal_node) : M/cal_node;
|
||
|
//MPI_Recv(&C[offset*N],bound*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
|
||
|
MPI_Irecv(&C[offset*N],bound*N,MPI_FLOAT,i,0,MPI_COMM_WORLD,&request);
|
||
|
offset += bound;
|
||
|
}
|
||
|
//MPI_Wait(&request, &status);
|
||
|
|
||
|
}
|
||
|
else {
|
||
|
MPI_Send(C, bound*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
|
||
|
//MPI_Isend(C, bound*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD,&request);
|
||
|
|
||
|
//MPI_Wait(&request, &status);
|
||
|
}
|
||
|
//MPI_Finalize();
|
||
|
} //)
|