147 lines
3.9 KiB
C++
147 lines
3.9 KiB
C++
#include "mat_mul.h"
|
|
#include "util.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <mpi.h>
|
|
|
|
static float *A, *B, *C;
|
|
static int M, N, K;
|
|
static int num_threads;
|
|
static int mpi_rank, mpi_world_size;
|
|
|
|
#define min(x, y) (((x) > (y)) ? (y) : (x))
|
|
|
|
#define ITILESIZE (32)
|
|
#define JTILESIZE (1024)
|
|
#define KTILESIZE (1024)
|
|
|
|
//#define ITILESIZE (16)
|
|
//#define JTILESIZE (512)
|
|
//#define KTILESIZE (512)
|
|
|
|
static void mat_mul_omp(int limit) {
|
|
|
|
|
|
//int tid = (long)data;
|
|
//int is = M / num_threads * tid + min(tid, M % num_threads);
|
|
//int ie = M / num_threads * (tid + 1) + min(tid + 1, M % num_threads);
|
|
|
|
//int is = M / mpi_world_size * mpi_rank + min(mpi_rank, M % mpi_world_size);
|
|
//int ie = M / mpi_world_size * (mpi_rank + 1) + min(mpi_rank + 1, M % mpi_world_size);
|
|
|
|
// printf("[mat_mul_omp] _mpi_rank : %d / is : %d / ie : %d\n", mpi_rank, is, ie);
|
|
|
|
//printf("[mat_mul_omp] %d %d\n", mpi_rank, limit);
|
|
|
|
#pragma omp parallel for num_threads(num_threads)
|
|
for (int ii = 0; ii < limit; 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(limit, ii + ITILESIZE); i++) {
|
|
|
|
float ar = A[i * K + k];
|
|
for (int j = jj; j < min(N, jj + JTILESIZE); j++) {
|
|
C[i * N + j] += ar * B[k * N + j];
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
//for(int i = 0; i < limit*K; i++){
|
|
// printf("A %d %d: %lf\n", mpi_rank, i, A[i]);
|
|
//}
|
|
|
|
//for(int i = 0; i < limit*N; i++){
|
|
// printf("C %d %d : %lf\n", mpi_rank, i, C[i]);
|
|
//}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
num_threads = _num_threads, mpi_rank = _mpi_rank,
|
|
mpi_world_size = _mpi_world_size;
|
|
M = _M, N = _N, K = _K;
|
|
|
|
MPI_Status status;
|
|
int is[mpi_world_size];
|
|
int ie[mpi_world_size];
|
|
|
|
MPI_Request a_req;
|
|
MPI_Request b_req;
|
|
|
|
if(mpi_rank == 0){
|
|
A = _A, B = _B, C = _C;
|
|
for(int i = 1; i < mpi_world_size; i++){
|
|
is[i] = M / mpi_world_size * i + min(i, M % mpi_world_size);
|
|
ie[i] = M / mpi_world_size * (i + 1) + min(i + 1, M % mpi_world_size);
|
|
//MPI_Send(A + is[i]*K, (ie[i] - is[i])*K , MPI_FLOAT, i, 0, MPI_COMM_WORLD);
|
|
//MPI_Send(B , K*N , MPI_FLOAT, i, 1, MPI_COMM_WORLD);
|
|
MPI_Isend(A + is[i]*K, (ie[i] - is[i])*K , MPI_FLOAT, i, 0, MPI_COMM_WORLD, &a_req);
|
|
MPI_Isend(B , K*N , MPI_FLOAT, i, 1, MPI_COMM_WORLD, &b_req);
|
|
//printf("mpi send AB %d\n", i);
|
|
}
|
|
is[0] = 0;
|
|
ie[0] = M / mpi_world_size * 1 + min(1, M % mpi_world_size);
|
|
|
|
} else{
|
|
is[0] = M / mpi_world_size * mpi_rank + min(mpi_rank, M % mpi_world_size);
|
|
ie[0] = M / mpi_world_size * (mpi_rank + 1) + min(mpi_rank + 1, M % mpi_world_size);
|
|
//printf("malloc size %d : %d\n", mpi_rank, ie - is);
|
|
|
|
|
|
A = (float*) aligned_alloc(32, sizeof(float)*(ie[0] - is[0])*K);
|
|
B = (float*) aligned_alloc(32, sizeof(float)*K*N);
|
|
C = (float*) aligned_alloc(32, sizeof(float)*(ie[0] - is[0])*N);
|
|
//zero_mat(C, (ie[0] - is[0]), N);
|
|
|
|
MPI_Recv(A, (ie[0] - is[0])*K, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(B, K*N, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status);
|
|
|
|
//printf("mpi receive AB %d\n", mpi_rank);
|
|
|
|
}
|
|
|
|
mat_mul_omp(ie[0] - is[0]);
|
|
|
|
// A : M*K
|
|
// B : K*N
|
|
// C : M*N
|
|
|
|
|
|
//for(int i = 0; i < M*N; i++){
|
|
// printf("A %d %d: %lf\n", mpi_rank, i, A[i]);
|
|
//}
|
|
|
|
//for(int i = 0; i < K*N; i++){
|
|
// printf("B %d %d : %lf\n", mpi_rank, i, B[i]);
|
|
//}
|
|
|
|
//for(int i = 0; i < M*N; i++){
|
|
// printf("C %d %d : %lf\n", mpi_rank, i, C[i]);
|
|
//}
|
|
|
|
if(mpi_rank == 0){
|
|
for(int i = 1; i < mpi_world_size; i++){
|
|
MPI_Recv(C + is[i]*N, (ie[i] - is[i])*N, MPI_FLOAT, i, 2, MPI_COMM_WORLD, &status);
|
|
//printf("mpi receive C %d\n", i);
|
|
}
|
|
}
|
|
else{
|
|
//MPI_Send(C, (ie[0] - is[0])*N, MPI_FLOAT, 0, 2, MPI_COMM_WORLD);
|
|
MPI_Isend(C, (ie[0] - is[0])*N, MPI_FLOAT, 0, 2, MPI_COMM_WORLD, &a_req);
|
|
//printf("mpi send C %d\n", mpi_rank);
|
|
free(A);
|
|
free(B);
|
|
free(C);
|
|
}
|
|
|
|
}
|