#include "mat_mul.h" #include #include #include #include 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