chundoong-lab-ta/SamsungDS22/submissions/HW4/hj614.yoo/mat_mul.cpp

184 lines
5.7 KiB
C++
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "mat_mul.h"
#include "util.h"
#include <cstdio>
#include <cstdlib>
#include <mpi.h>
#include <immintrin.h>
#define IBLOCK (num_threads)
#define JBLOCK (1024)
#define KBLOCK (512)
#define VEC_NUM (1)
#define VEC_SIZE (64)
#define VECDATA_SIZE (VEC_SIZE>>2)
#define MIN(a,b) ((a>b)?b:a)
static float *A, *B, *C;
static int M, N, K;
static int num_threads;
static int mpi_rank, mpi_world_size;
int si, ei, slice;
static void mat_mul_omp() {
// TODO: parallelize & optimize matrix multiplication
// Use num_threads per node
int i,j,k;
int ii, jj, kk;
int EI = ei - si;
if ((K&(KBLOCK-1)) || (N&(JBLOCK-1))) {
//if (1)) {
#pragma omp parallel num_threads(num_threads)
#pragma omp for nowait
for (ii = 0; ii < EI; ii+=IBLOCK) {
for (jj = 0; jj < N; jj+=JBLOCK) {
for (kk = 0; kk < K; kk+=KBLOCK) {
for (k = kk; k < MIN(K,kk+KBLOCK); ++k) {
for (i = ii; i < MIN(EI,ii+IBLOCK); i+=1) {
float Aik = A[i * K + k];
unsigned long spC, epC;
__m512 va = _mm512_set1_ps(Aik);
__m512 vb[VEC_NUM],vc[VEC_NUM];
spC = ((unsigned long)&C[i*N] + (VEC_SIZE*VEC_NUM-1)) & (VEC_SIZE*VEC_NUM-1);
epC = (unsigned long)&C[(i+1)*N] & (VEC_SIZE*VEC_NUM-1);
for (j = jj; (unsigned long)&C[i * N + j] < spC; j=j+1) {
C[i * N + j] += Aik * B[k * N + j];
}
for (; (unsigned long)&C[i * N + j] < epC; j+=VECDATA_SIZE*VEC_NUM) {
vb[0] = _mm512_load_ps(&B[k*N+j]);
vc[0] = _mm512_load_ps(&C[i*N+j]);
vc[0] = _mm512_fmadd_ps(va,vb[0],vc[0]);
_mm512_store_ps(&C[i*N+j],vc[0]);
}
for (; j < MIN(N,jj+JBLOCK); j=j+1) {
C[i * N + j] += Aik * B[k * N + j];
}
}
}
}
}
}
}
else
{
#define KSTEP (4)
#define JSTEP (2)
#pragma omp parallel num_threads(num_threads)
#pragma omp for nowait
for (ii = 0; ii < EI; ii+=IBLOCK) {
for (jj = 0; jj < N; jj+=JBLOCK) {
for (kk = 0; kk < K; kk+=KBLOCK) {
for (k = kk; k < kk+KBLOCK; k=k+KSTEP) {
for (i = ii; i < MIN(EI,ii+IBLOCK); i+=1) {
__m512 va[KSTEP], vb[KSTEP*JSTEP],vc[JSTEP];
va[0] = _mm512_set1_ps(A[i*K+k+0]);
va[1] = _mm512_set1_ps(A[i*K+k+1]);
va[2] = _mm512_set1_ps(A[i*K+k+2]);
va[3] = _mm512_set1_ps(A[i*K+k+3]);
for (j = jj; j < jj+JBLOCK; j+=VECDATA_SIZE*JSTEP) {
vc[0] = _mm512_load_ps(&C[i*N+j]);
vc[1] = _mm512_load_ps(&C[i*N+j+VECDATA_SIZE]);
vb[0] = _mm512_load_ps(&B[(k+0)*N+j]);
vb[1] = _mm512_load_ps(&B[(k+1)*N+j]);
vb[2] = _mm512_load_ps(&B[(k+2)*N+j]);
vb[3] = _mm512_load_ps(&B[(k+3)*N+j]);
vc[0] = _mm512_fmadd_ps(va[0],vb[0],vc[0]);
vc[0] = _mm512_fmadd_ps(va[1],vb[1],vc[0]);
vc[0] = _mm512_fmadd_ps(va[2],vb[2],vc[0]);
vc[0] = _mm512_fmadd_ps(va[3],vb[3],vc[0]);
vb[4] = _mm512_load_ps(&B[(k+0)*N+j+VECDATA_SIZE]);
vb[5] = _mm512_load_ps(&B[(k+1)*N+j+VECDATA_SIZE]);
vb[6] = _mm512_load_ps(&B[(k+2)*N+j+VECDATA_SIZE]);
vb[7] = _mm512_load_ps(&B[(k+3)*N+j+VECDATA_SIZE]);
vc[1] = _mm512_fmadd_ps(va[0],vb[4],vc[1]);
vc[1] = _mm512_fmadd_ps(va[1],vb[5],vc[1]);
vc[1] = _mm512_fmadd_ps(va[2],vb[6],vc[1]);
vc[1] = _mm512_fmadd_ps(va[3],vb[7],vc[1]);
_mm512_store_ps(&C[i*N+j],vc[0]);
_mm512_store_ps(&C[i*N+j+VECDATA_SIZE],vc[1]);
}
}
}
}
}
}
}
return;
}
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.
MPI_Request request;
MPI_Status status;
slice = M / mpi_world_size;
si = mpi_rank * slice;
ei = (mpi_rank + 1) * slice;
if (mpi_rank == mpi_world_size - 1) ei = M;
if (mpi_rank != 0)
{
//timer_start(mpi_rank);
alloc_mat(&A,ei-si,K);
alloc_mat(&B,K,N);
alloc_mat(&C,ei-si,N);
zero_mat(C,ei-si,N);
//double allocTime = timer_stop(mpi_rank);
//printf("[%d] %f sec\n", mpi_rank, allocTime);
}
if (mpi_rank == 0)
{
for (int i = 1; i < mpi_world_size; i++)
{
int siTmp = i*slice;
int eiTmp = (i+1)*slice;
if (i == mpi_world_size - 1) eiTmp = M;
MPI_Isend(A+siTmp*K, (eiTmp-siTmp)*K, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &request);
}
}
else
{
MPI_Irecv(A, (ei-si)*K, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request);
}
if (mpi_world_size > 1)
{
MPI_Wait(&request, &status);
MPI_Bcast(B, K * N, MPI_FLOAT, 0, MPI_COMM_WORLD);
}
mat_mul_omp();
if (mpi_rank == 0)
{
for (int i = 1; i < mpi_world_size; i++)
{
int siTmp = i*slice;
int eiTmp = (i+1)*slice;
if (i == mpi_world_size - 1) eiTmp = M;
MPI_Irecv(C+siTmp*N, (eiTmp-siTmp)*N, MPI_FLOAT, i, 0, MPI_COMM_WORLD, &request);
}
}
else
{
MPI_Isend(C, (ei-si)*N, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &request);
}
if (mpi_world_size > 1) MPI_Wait(&request, &status);
if (mpi_rank != 0)
{
free(A);
free(B);
free(C);
}
}