#include "mat_mul.h" #include #include #include #include #include "util.h" static float *A, *B, *C; static int M, N, K; static int num_threads; static int mpi_rank, mpi_world_size; /*static void mat_mul_omp2() { // TODO: parallelize & optimize matrix multiplication // Use num_threads per node #pragma omp parallel for 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]; } } } }*/ #define min(a,b) (a>b?b:a) static void mat_mul_omp() { float Aik; int bs_k = 32; int bs_j = 2048; //omp_set_num_threads(num_threads); for (int kk = 0; kk < K; kk += bs_k) for (int jj = 0; jj < N; jj += bs_j) #pragma omp parallel for num_threads(num_threads) for (int i = 0; i < M; i++) for (int k = kk; k < min(kk + bs_k, K); k++) { Aik = A[i*K + k]; //for (int j =0; j < N; j++) for (int j = jj; j < min(jj + bs_j, N); j++) C[i*N + j + 0] += Aik * B[k*N + j + 0]; //for (int j =0; j < (N/8)*8; j+=8) { // C[i*N + j + 0] += Aik * B[k*N + j + 0]; // C[i*N + j + 1] += Aik * B[k*N + j + 1]; // C[i*N + j + 2] += Aik * B[k*N + j + 2]; // C[i*N + j + 3] += Aik * B[k*N + j + 3]; // C[i*N + j + 4] += Aik * B[k*N + j + 4]; // C[i*N + j + 5] += Aik * B[k*N + j + 5]; // C[i*N + j + 6] += Aik * B[k*N + j + 6]; // C[i*N + j + 7] += Aik * B[k*N + j + 7]; //} //for (; j < N; j++) // C[i*N + j] += Aik * B[k*N + j]; } return; } /*static void mat_mul_omp() { // TODO: parallelize & optimize matrix multiplication int bs = 64; for (int jj = 0; jj < K; jj += bs) for (int kk = 0; kk < K; kk += bs) for (int i = 0; i < M; ++i) #pragma omp parallel for for (int j =jj; j < min(jj+bs, N); j++) for (int k = kk; k < min(kk+bs, K); k++) //C[i*N + j + 0] += Aik * B[k*N + j + 0]; C[i*N + j] += A[i*K + k] * B[k*N + j]; return; }*/ 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 int default_div_size = M/mpi_world_size; MPI_Status status; MPI_Request request; if(mpi_rank == 0){ //1. Send part of A and B to each node for(int target_rank = 1; target_rank < mpi_world_size; target_rank++){ int div_start, div_size; div_start = target_rank * default_div_size; div_size = default_div_size; if(target_rank == (mpi_world_size - 1)) div_size += M - (default_div_size * mpi_world_size); //printf("send. rank=%d, div_start=%d, div_size=%d\n", target_rank, div_start, div_size); //Send part of array A //MPI_Send(A + (div_start * K), div_size * K, MPI_FLOAT, target_rank, 0, MPI_COMM_WORLD); MPI_Isend(A + (div_start * K), div_size * K, MPI_FLOAT, target_rank, 0, MPI_COMM_WORLD, &request); //MPI_Isend(B, K * N, MPI_FLOAT, target_rank, 0, MPI_COMM_WORLD, &request); } MPI_Bcast(B, K * N, MPI_FLOAT, 0, MPI_COMM_WORLD); //MPI_Wait(&request, &status); //2. Calc rank0 mat mul int original_M = M; M = default_div_size; // make mat_mal compute the first division of M. //printf("main. my rank=%d, div_size=%d\n", mpi_rank, default_div_size); //timer_start(1); mat_mul_omp(); //double elapsed_time = timer_stop(1); //printf("[rank %d] time: %f sec\n", mpi_rank, elapsed_time); M = original_M; // Restore M //3. Recv C from other nodes MPI_Request arrC_req[4]; MPI_Status arrC_status[4]; for(int target_rank = 1; target_rank < mpi_world_size; target_rank++){ int div_start, div_size; div_start = target_rank * default_div_size; div_size = default_div_size; if(target_rank == (mpi_world_size - 1)) div_size += M - (default_div_size * mpi_world_size); //MPI_Recv(C + div_start * N, div_size * N, MPI_FLOAT, target_rank, 0, MPI_COMM_WORLD, &status); MPI_Irecv(C + div_start * N, div_size * N, MPI_FLOAT, target_rank, 0, MPI_COMM_WORLD, &arrC_req[target_rank-1]); } MPI_Waitall(mpi_world_size-1, arrC_req, arrC_status); //printf("main. wait. end\n"); }else{ //0. alloc local memory int div_size; div_size = default_div_size; if(mpi_rank == (mpi_world_size - 1)) div_size += M - (default_div_size * mpi_world_size); M = div_size; // Adjust M size alloc_mat(&A, M, K); alloc_mat(&B, K, N); alloc_mat(&C, M, N); //1. Recv part of A MPI_Recv(A, M * K, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status); //2. Recv full B //MPI_Recv(B, K * N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status); MPI_Bcast(B, K * N, MPI_FLOAT, 0, MPI_COMM_WORLD); //3. Calc sub matrix //printf("sub. my rank=%d, div_size=%d\n", mpi_rank, div_size); //timer_start(1); mat_mul_omp(); //double elapsed_time = timer_stop(1); //printf("[rank %d] time: %f sec\n", mpi_rank, elapsed_time); //4. Send C to rank 0 node. //printf("sub. end. my rank=%d, div_size=%d\n", mpi_rank, div_size); MPI_Send(C, M * N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD); //MPI_Isend(C, M * N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request); } }