chundoong-lab-ta/SamsungDS22/submissions/HW2/shinkyu.park/mat_mul.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

2022-09-29 18:01:45 +09:00
#include "mat_mul.h"
#include <immintrin.h>
#include <cstdlib>
#include <cstdio>
#include <pthread.h>
static float *A, *B, *C;
static int M, N, K;
static int num_threads;
static void* mat_mul_thread(void *data) {
int tid = *(int *)data;
int num = (M + num_threads - 1) / num_threads;
int start = num * tid;
int end = (tid == num_threads -1) ? M : num * (tid + 1);
int blocksizek = 32;
int blocksizem = 64;
int blocksizen = 4096;
if (start >= M) return NULL;
// TODO: parallelize & optimize matrix multiplication
for (int ii = start ; ii < end ; ii +=blocksizem) {
for (int jj = 0 ; jj < N ; jj +=blocksizen) {
for (int kk = 0 ; kk < K ; kk += blocksizek) {
int endm = ii + blocksizem < end ? (ii + blocksizem) : end;
int endn = jj + blocksizen < N ? (jj + blocksizen) : N;
int endk = kk + blocksizek < K ? (kk + blocksizek) : K;
for (int i = ii ; i < endm ; i++) {
for (int k = kk ; k < endk ; k++) {
for (int j = jj ; j < endn ; j++) {
C[i * N +j] += A[i * K + k] * B[k * N + j];
}
}
}
}
}
}
return NULL;
}
void mat_mul(float *_A, float *_B, float *_C, int _M, int _N, int _K, int _num_threads) {
A = _A, B = _B, C = _C;
M = _M, N = _N, K = _K;
num_threads = _num_threads;
// TODO: create '_num_threads' pthreads
pthread_t thread[80];
int tid[80];
for (int i = 0 ; i < num_threads ; i++) {
tid[i] = i;
pthread_create(&thread[i], NULL, mat_mul_thread, &tid[i]);
}
for (int i = 0 ; i < num_threads ; i++) {
pthread_join(thread[i], NULL);
}
}