224 lines
6.4 KiB
C++
224 lines
6.4 KiB
C++
#include "mat_mul.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <mpi.h>
|
|
#include <omp.h>
|
|
#include "util.h"
|
|
#define FROM_MASTER 0
|
|
#define FROM_WORKER 1
|
|
#define MASTER 0
|
|
#define ITILESIZE (1024)
|
|
#define JTILESIZE (1024)
|
|
#define KTILESIZE (1024)
|
|
|
|
static float *A, *B, *C;
|
|
static int M, N, K;
|
|
static int num_threads;
|
|
static int mpi_rank, mpi_world_size;
|
|
|
|
/// for (int ii = is; ii < ie; ii += ITILESIZE) {
|
|
/// for (int jj = 0; jj < N; jj += JTILESIZE) {
|
|
/// for (int kk = 0; kk < K; kk += KTILESIZE) {
|
|
///
|
|
/// for (int k = kk; k < std::min(K, kk + KTILESIZE); k++) {
|
|
/// for (int i = ii; i < std::min(ie, ii + ITILESIZE); i++) {
|
|
/// float ar = A[i * K + k];
|
|
/// for (int j = jj; j < std::min(N, jj + JTILESIZE); j+=1) {
|
|
/// C[i * N + j] += ar * B[k * N + j];
|
|
|
|
static void mat_mul_omp(int num_row) {
|
|
|
|
int bs = 30;
|
|
int k_min;
|
|
int slice = num_row / num_threads;
|
|
omp_set_num_threads(num_threads);
|
|
#pragma omp parallel
|
|
{
|
|
int pid = omp_get_thread_num();
|
|
int start = pid * slice;
|
|
int end = pid == num_threads - 1 ? num_row : (pid + 1) * slice;
|
|
|
|
for (int ii = start; ii < end; ii+=ITILESIZE) {
|
|
for (int jj = 0; jj < N; jj += JTILESIZE) {
|
|
for (int kk = 0; kk < K; kk += bs) {
|
|
//for (int i = start; i < end; ++i) {
|
|
for (int i = ii; i < std::min( end, ii+ITILESIZE); ++i) {
|
|
for (int k = kk; k < std::min(kk+bs,K); ++k) {
|
|
//for (int j = 0; j < N; j=j+1) {
|
|
for (int j = jj; j< std::min(N, jj+JTILESIZE); j=j+1) {
|
|
C[i * N + j] += A[i*K +k] * B[k * N + j];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
///static void mat_mul_omp(int num_row) {
|
|
///
|
|
/// int bs = 41;
|
|
/// int k_min;
|
|
/// int slice = num_row / num_threads;
|
|
/// omp_set_num_threads(num_threads);
|
|
///#pragma omp parallel
|
|
/// {
|
|
/// int pid = omp_get_thread_num();
|
|
/// //int start = pid * slice;
|
|
/// //int end = pid == num_threads - 1 ? num_row : (pid + 1) * slice;
|
|
///
|
|
/// int is = M / num_threads * pid + std::min(pid, M % num_threads);
|
|
/// int ie = M / num_threads * (pid + 1) + std::min(pid + 1, M % num_threads);
|
|
///
|
|
/// for (int ii = is; ii < ie; ii += ITILESIZE) {
|
|
/// for (int jj = 0; jj < N; jj += JTILESIZE) {
|
|
/// for (int kk = 0; kk < K; kk += KTILESIZE) {
|
|
///
|
|
/// for (int k = kk; k < std::min(K, kk + KTILESIZE); k++) {
|
|
/// for (int i = ii; i < std::min(ie, ii + ITILESIZE); i++) {
|
|
/// float ar = A[i * K + k];
|
|
/// for (int j = jj; j < std::min(N, jj + JTILESIZE); j+=1) {
|
|
/// C[i * N + j] += ar * B[k * N + j];
|
|
/// }
|
|
/// }
|
|
/// }
|
|
///
|
|
/// }
|
|
/// }
|
|
/// }
|
|
///
|
|
/// }
|
|
///}
|
|
///
|
|
/*
|
|
#define ITILESIZE (32)
|
|
#define JTILESIZE (1024)
|
|
#define KTILESIZE (1024)
|
|
|
|
static void* mat_mul_thread(void *data) {
|
|
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);
|
|
|
|
for (int ii = is; ii < ie; 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(ie, 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];
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
*/
|
|
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
|
|
|
|
// FIXME: for now, only root process runs the matrix multiplication.
|
|
//if (mpi_rank == 0)
|
|
// mat_mul_omp();
|
|
|
|
MPI_Status status;
|
|
MPI_Request request;
|
|
//int len_blk;
|
|
//int len_B, len_A, len_p_A, len_p_r_A;
|
|
//M_p = int(M/mpi_world_size);
|
|
//M_p_r = M_p + (M%mpi_world_size);
|
|
|
|
|
|
//len_A = M*K;
|
|
//len_B = K*N;
|
|
//len_C = M*N;
|
|
//len_p_A = M_p*K;
|
|
//len_p_C = M_p*N;
|
|
//len_p_r_A = M_p_r*K;
|
|
//len_p_r_C = M_p_r*N;
|
|
//
|
|
|
|
//printf("\n");
|
|
//printf("Seunsik\n");
|
|
//printf("Num node %d\n", mpi_world_size);
|
|
int offset;
|
|
int rows;
|
|
if (mpi_rank == 0)
|
|
{
|
|
int row_size = M/mpi_world_size;
|
|
int st, ed;
|
|
|
|
for (int i =1; i<mpi_world_size; i++)
|
|
{
|
|
st = offset = i * row_size;
|
|
ed = i == mpi_world_size -1 ? M : (i+1)*row_size;
|
|
rows = ed - st;
|
|
|
|
MPI_Isend(&offset, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&rows, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(A+offset*K, rows*K, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(B, K*N, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &request);
|
|
//printf("rows: %d\n:", rows);
|
|
//printf("row_size: %d\n:", row_size);
|
|
//printf("offset: %d\n:", offset);
|
|
//printf("st: %d\n:", st);
|
|
//printf("ed: %d\n:", ed);
|
|
}
|
|
|
|
rows = row_size;
|
|
mat_mul_omp(row_size);
|
|
|
|
for (int i =1; i<mpi_world_size; i++)
|
|
{
|
|
MPI_Recv(&offset, 1, MPI_INT, i, FROM_WORKER, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, i, FROM_WORKER, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(C + offset*N, rows*N, MPI_FLOAT, i, FROM_WORKER, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
//free(A);
|
|
//free(B);
|
|
//free(C);
|
|
|
|
}
|
|
else
|
|
{
|
|
//alloc_mat(&A, M_p, K);
|
|
//alloc_mat(&B, K, N);
|
|
//alloc_mat(&C, M_p, N);
|
|
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, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(&rows, 1, MPI_INT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(A, rows*K, MPI_FLOAT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
|
|
MPI_Recv(B, K*N, MPI_FLOAT, MASTER, FROM_MASTER, MPI_COMM_WORLD, &status);
|
|
|
|
mat_mul_omp(rows);
|
|
|
|
MPI_Isend(&offset, 1, MPI_INT, MASTER, FROM_WORKER, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(&rows, 1, MPI_INT, MASTER, FROM_WORKER, MPI_COMM_WORLD, &request);
|
|
MPI_Isend(C, rows*N, MPI_FLOAT, MASTER, FROM_WORKER, MPI_COMM_WORLD, &request);
|
|
|
|
free(A);
|
|
free(B);
|
|
free(C);
|
|
}
|
|
|
|
}
|