104 lines
2.9 KiB
C++
104 lines
2.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;
|
|
|
|
static int offset;
|
|
static int rows;
|
|
MPI_Status status;
|
|
|
|
inline 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()
|
|
{
|
|
#pragma omp parallel for schedule (dynamic) num_threads(num_threads)
|
|
for (int ii = offset; ii < (offset + rows); ii += ITILESIZE) {
|
|
//#pragma omp parallel for num_threads(num_threads)
|
|
for (int jj = 0; jj < N; jj += JTILESIZE) {
|
|
for (int kk = 0; kk < K; kk += KTILESIZE) {
|
|
//#pragma omp parallel for num_threads(num_threads)
|
|
for (int k = kk; k < min(K, kk + KTILESIZE); k++) {
|
|
for (int i = ii; i < min(M, ii + ITILESIZE); i++) {
|
|
float ar = A[i * K + k];
|
|
for (int j = jj; j < min(N, jj + JTILESIZE); 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;
|
|
|
|
int chunk = M / mpi_world_size;
|
|
|
|
if (M % mpi_world_size != 0) chunk++;
|
|
|
|
if(mpi_rank != 0)
|
|
{
|
|
alloc_mat(&A, M, K);
|
|
alloc_mat(&B, K, N);
|
|
alloc_mat(&C, M, N);
|
|
}
|
|
|
|
if (mpi_rank == 0)
|
|
{
|
|
for (int rank_id = 1; rank_id < mpi_world_size; rank_id++)
|
|
{
|
|
offset = rank_id * chunk;
|
|
rows = M - offset;
|
|
if (rows > chunk) rows = chunk;
|
|
MPI_Send(&offset, 1, MPI_INT, rank_id, 0, MPI_COMM_WORLD);
|
|
MPI_Send(&rows, 1, MPI_INT, rank_id, 1, MPI_COMM_WORLD);
|
|
MPI_Send(A + (offset * K), rows * K, MPI_FLOAT, rank_id, 2, MPI_COMM_WORLD);
|
|
MPI_Send(B, K * N, MPI_FLOAT, rank_id, 3, MPI_COMM_WORLD);
|
|
}
|
|
offset = 0;
|
|
rows = chunk;
|
|
} else {
|
|
MPI_Recv(&offset, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(A+(offset * K), rows * K, MPI_FLOAT, 0, 2, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(B, K * N, MPI_FLOAT, 0, 3, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
//MPI_Barrier(MPI_COMM_WORLD);
|
|
|
|
mat_mul_omp();
|
|
|
|
if (mpi_rank == 0) {
|
|
for (int rank_id = 1; rank_id < mpi_world_size; rank_id++) {
|
|
MPI_Recv(&offset, 1, MPI_INT, rank_id, 4, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, rank_id, 5, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(C + (N * offset), rows * N, MPI_FLOAT, rank_id, 6, MPI_COMM_WORLD, &status);
|
|
}
|
|
} else {
|
|
MPI_Send(&offset, 1, MPI_INT, 0, 4, MPI_COMM_WORLD);
|
|
MPI_Send(&rows, 1, MPI_INT, 0, 5, MPI_COMM_WORLD);
|
|
MPI_Send(C + (N * offset), rows * N, MPI_FLOAT, 0, 6, MPI_COMM_WORLD);
|
|
}
|
|
}
|
|
|