101 lines
3.0 KiB
C++
101 lines
3.0 KiB
C++
#include "mat_mul.h"
|
|
#include "util.h"
|
|
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include <algorithm>
|
|
#include <pthread.h>
|
|
#include <omp.h>
|
|
#include <mpi.h>
|
|
|
|
#define ISIZE 32 //32
|
|
#define JSIZE 2048 //2048
|
|
#define KSIZE 16 //16
|
|
|
|
static float *A, *B, *C;
|
|
static int M, N, K;
|
|
static int num_threads;
|
|
static int mpi_rank, mpi_world_size;
|
|
int rows_num, offset;
|
|
|
|
static void mat_mul_omp(){
|
|
|
|
#pragma omp parallel num_threads (num_threads)
|
|
{
|
|
int pid = omp_get_thread_num();
|
|
|
|
int is = pid * rows_num / num_threads;
|
|
int ie = (pid == num_threads - 1) ? rows_num : (pid + 1) * rows_num / num_threads;
|
|
|
|
for(int ii=is; ii<ie; ii+=ISIZE){
|
|
for(int kk=0; kk<K; kk+=KSIZE){
|
|
for(int jj=0; jj<N; jj+=JSIZE){
|
|
|
|
for(int i=ii; i<std::min(ii+ISIZE, ie); i++){
|
|
for(int k=kk; k<std::min(kk+KSIZE, K); k++){
|
|
float ar = A[i*K+k];
|
|
for(int j=jj; j<std::min(jj+JSIZE, N); 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;
|
|
|
|
MPI_Status status;
|
|
MPI_Request request;
|
|
|
|
if (mpi_rank == 0){
|
|
int row_size = M / mpi_world_size;
|
|
int start_row, end_row;
|
|
offset = 0;
|
|
|
|
for (int sub_node=1; sub_node < mpi_world_size; sub_node++){
|
|
start_row = offset = sub_node * row_size;
|
|
end_row = (sub_node == mpi_world_size -1) ? M : (sub_node + 1) * row_size;
|
|
rows_num = end_row - start_row;
|
|
MPI_Isend(&offset, 1, MPI_INT, sub_node, 1, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&rows_num, 1, MPI_INT, sub_node, 1, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&A[offset*K], rows_num*K, MPI_FLOAT, sub_node, 1, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(B, K*N, MPI_FLOAT, sub_node, 1, MPI_COMM_WORLD, &request);
|
|
}
|
|
|
|
rows_num = row_size;
|
|
mat_mul_omp();
|
|
|
|
for(int sub_node = 1; sub_node < mpi_world_size; sub_node++){
|
|
MPI_Recv(&offset, 1, MPI_INT, sub_node, 2, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows_num, 1, MPI_INT, sub_node, 2, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&C[offset*N], rows_num*N, MPI_FLOAT, sub_node, 2, MPI_COMM_WORLD, &status);
|
|
}
|
|
}
|
|
|
|
else{
|
|
alloc_mat(&A, M, K);
|
|
alloc_mat(&B, K, N);
|
|
alloc_mat(&C, M, N);
|
|
zero_mat(C, M, N);
|
|
|
|
MPI_Recv(&offset, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows_num, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(A, rows_num*K, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(B, K*N, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status);
|
|
|
|
mat_mul_omp();
|
|
|
|
MPI_Isend(&offset, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&rows_num, 1, MPI_INT, 0, 2, MPI_COMM_WORLD,&request);
|
|
MPI_Isend(C, rows_num*N, MPI_FLOAT, 0, 2, MPI_COMM_WORLD,&request);
|
|
}
|
|
|
|
} |