#include "mat_mul.h" #include "util.h" #include #include #include #include #define HOST 0 #define ITILESIZE 16 #define JTILESIZE 2048 #define KTILESIZE 2048 static float *A, *B, *C; static int M, N, K; static int num_threads; static int mpi_rank, mpi_world_size; static int rows; static int start_node; 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[num_threads]; int ie[num_threads]; for (int i=0; i < num_threads; i++){ is[i] = rows / num_threads * i + min(i, rows % num_threads); ie[i] = rows / num_threads * (i + 1) + min(i + 1, rows % num_threads); } int tid; int ii, jj, kk, i, j, k; omp_set_num_threads(num_threads); #pragma omp parallel shared(A, B, C, M, N, K, num_threads) private(ii, jj, kk, k, i, j, tid) { tid = omp_get_thread_num(); for (ii = is[tid]; ii < ie[tid]; 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[tid], ii + ITILESIZE); i++) { float ar = A[i * K + k]; for (j = jj; j < min(N, jj + JTILESIZE); j++) { 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; MPI_Request request; MPI_Status status; int tag1 = 1001; int tag2 = 1002; // 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 slice_node = M / mpi_world_size; int start[mpi_world_size] = {0}; int end[mpi_world_size] = {0}; for (int i = 0; i < mpi_world_size; i++) { // i is the node pointer start[i] = i * slice_node; end[i] = (i == mpi_world_size - 1) ? M : (i + 1) * slice_node; } if (mpi_rank == 0) { for (int node = 1; node < mpi_world_size; node++) { rows = end[node] - start[node]; start_node = start[node]; MPI_Isend(&start_node, 1, MPI_INT, node, tag1, MPI_COMM_WORLD, &request); MPI_Isend(&rows, 1, MPI_INT, node, tag1, MPI_COMM_WORLD, &request); MPI_Isend(&A[start_node*K], rows*K, MPI_FLOAT, node, tag1, MPI_COMM_WORLD, &request); MPI_Isend(B, K*N, MPI_FLOAT, node, tag1, MPI_COMM_WORLD, &request); } rows = slice_node; mat_mul_omp(); for (int node = 1; node < mpi_world_size; node++) { MPI_Recv(&start_node, 1, MPI_INT, node, tag2, MPI_COMM_WORLD, &status); MPI_Recv(&rows, 1, MPI_INT, node, tag2, MPI_COMM_WORLD, &status); MPI_Recv(&C[start_node*N], rows*N, MPI_INT, node, tag2, MPI_COMM_WORLD, &status); } } else { alloc_mat(&A, slice_node + mpi_world_size - 1, K); alloc_mat(&B, K, N); alloc_mat(&C, slice_node + mpi_world_size - 1, N); zero_mat(C, slice_node + mpi_world_size - 1, N); MPI_Recv(&start_node, 1, MPI_INT, HOST, tag1, MPI_COMM_WORLD, &status); MPI_Recv(&rows, 1, MPI_INT, HOST, tag1, MPI_COMM_WORLD, &status); MPI_Recv(A, rows*K, MPI_FLOAT, HOST, tag1, MPI_COMM_WORLD, &status); MPI_Recv(B, K*N, MPI_FLOAT, HOST, tag1, MPI_COMM_WORLD, &status); mat_mul_omp(); MPI_Isend(&start_node, 1, MPI_INT, HOST, tag2, MPI_COMM_WORLD, &request); MPI_Isend(&rows, 1, MPI_INT, HOST, tag2, MPI_COMM_WORLD, &request); MPI_Isend(C, rows*N, MPI_FLOAT, HOST, tag2, MPI_COMM_WORLD, &request); } }