#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 slice_node, slice; 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(){//int tid) { // TODO: parallelize & optimize matrix multiplication // Use num_threads per node #pragma omp parallel num_threads (num_threads) { int tid = omp_get_thread_num(); int is = slice_node / num_threads * tid + min(tid, slice_node % num_threads); int ie = slice_node / num_threads * (tid + 1) + min(tid + 1, slice_node % num_threads); 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]; int row_C = i * N; for (int j = jj; j < min(N, jj + JTILESIZE); ++j) { C[row_C + j] += ar * B[k * N + j]; } } } } } } if (mpi_rank == 0 && tid == 0) { int edge = slice_node * mpi_world_size; if (edge < M) { int is = edge; int ie = M; 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]; int row_C = i * N; for (int j = jj; j < min(N, jj + JTILESIZE); ++j) { C[row_C + 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 /*----------seperate---------------*/ slice_node = M / mpi_world_size; int numElements_A = slice_node * K; int numElements_C = slice_node * N; MPI_Status status; MPI_Request request = MPI_REQUEST_NULL; if(mpi_rank == 0){ int offset = slice_node; for(int i=1; i