188 lines
3.6 KiB
C++
188 lines
3.6 KiB
C++
#include "mat_mul.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <mpi.h>
|
|
#include <omp.h>
|
|
|
|
#include <getopt.h>
|
|
//#include <mpi.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
//#include "mat_mul.h"
|
|
#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 int rows[4] = {0,};
|
|
static int offset[4] = {0,};
|
|
|
|
int omp_get_thread_num(void);
|
|
int omp_get_num_threads(void);
|
|
|
|
static void mat_mul_omp() {
|
|
// TODO: parallelize & optimize matrix multiplication
|
|
// Use num_threads per node
|
|
|
|
int start = 0;
|
|
int end = rows[mpi_rank];
|
|
|
|
int TILE_M = 32;
|
|
int TILE_K =16;
|
|
int TILE_N =2048;
|
|
int end_m, end_k, end_n;
|
|
float temp;
|
|
|
|
#pragma omp parallel for num_threads(num_threads) schedule(static)
|
|
for(int ii=start; ii<end; ii+=TILE_M)
|
|
{
|
|
for(int kk=0; kk<K; kk+= TILE_K)
|
|
{
|
|
for(int jj=0; jj<N; jj+=TILE_N)
|
|
{
|
|
if(TILE_M+ii < M) end_m = TILE_M + ii;
|
|
else end_m = M;
|
|
if(TILE_N+jj < N) end_n = TILE_N + jj;
|
|
else end_n = N;
|
|
if(TILE_K+kk < K) end_k = TILE_K + kk;
|
|
else end_k = K;
|
|
|
|
for(int i=ii; i<end_m; i++)
|
|
{
|
|
for(int k=kk; k<end_k; k++)
|
|
{
|
|
temp = A[i * K + k];
|
|
for(int j=jj; j<end_n; j++)
|
|
{
|
|
C[i*N+(j+0)] += temp*B[k*N+(j+0)];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
#pragma omp parallel
|
|
//#pragma omp parallel num_threads(num_threads)
|
|
{
|
|
int tid = omp_get_thread_num();
|
|
int nthreads = omp_get_num_threads();
|
|
|
|
int M_div = rows[mpi_rank] / nthreads;
|
|
int start = tid * M_div;
|
|
int end;
|
|
|
|
if(tid==nthreads - 1) end = rows[mpi_rank];
|
|
else end = (tid+1)*M_div;
|
|
|
|
float temp;
|
|
int bs = 35;
|
|
int k_min =0;
|
|
|
|
for (int kk = 0; kk < K; kk += bs)
|
|
{
|
|
for(int jj = 0; jj<N; jj++)
|
|
{
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
if(K>=(kk+bs)) k_min = kk+bs;
|
|
else k_min = K;
|
|
for (int k = kk; k < k_min; k++)
|
|
{
|
|
temp = A[i * K + k];
|
|
for (int j = 0; j < N; j++)
|
|
{
|
|
C[i*N+(j+0)] += temp*B[k*N+(j+0)];
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
}
|
|
|
|
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
|
|
|
|
MPI_Request request;
|
|
MPI_Status status;
|
|
|
|
int row_num = M/mpi_world_size;
|
|
|
|
// 노드별 행 개수 정의
|
|
for(int i=0; i<mpi_world_size; i++)
|
|
{
|
|
if(i==mpi_world_size - 1)
|
|
{
|
|
rows[i] = M - (row_num * (mpi_world_size-1));
|
|
}
|
|
else
|
|
{
|
|
rows[i] = row_num;
|
|
}
|
|
}
|
|
|
|
// 노드별 시작 위치 정의 for A matrix
|
|
for(int i=0; i<mpi_world_size; i++)
|
|
{
|
|
offset[i+1] = offset[i] + rows[i];
|
|
}
|
|
|
|
// 행렬 alloc @ task 노드
|
|
if(mpi_rank != 0)
|
|
{
|
|
M = rows[mpi_rank];
|
|
alloc_mat(&A, rows[mpi_rank], K);
|
|
alloc_mat(&B, K, N);
|
|
alloc_mat(&C, rows[mpi_rank], N);
|
|
zero_mat(C,M,N);
|
|
}
|
|
|
|
// A행렬 전송/수신
|
|
if(mpi_rank == 0)
|
|
{
|
|
for(int i=1; i<mpi_world_size; i++)
|
|
{
|
|
MPI_Isend(&A[offset[i]*K], rows[i]*K, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &request);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MPI_Recv(A, rows[mpi_rank]*K, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
|
|
}
|
|
|
|
// B행렬 전송/수신
|
|
MPI_Bcast(B, K*N, MPI_FLOAT, 0, MPI_COMM_WORLD);
|
|
|
|
// mat_mul 수행
|
|
mat_mul_omp();
|
|
|
|
// 계산결과 C행렬 전송/수신
|
|
if(mpi_rank != 0)
|
|
{
|
|
MPI_Isend(C, rows[mpi_rank]*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request);
|
|
}
|
|
else
|
|
{
|
|
for(int i=1; i<mpi_world_size; i++)
|
|
{
|
|
MPI_Recv(&C[offset[i]*N], rows[i]*N, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &status);
|
|
}
|
|
}
|
|
|
|
}
|