chundoong-lab-ta/SamsungDS22/submissions/HW2/youngsik.eom/mat_mul.cpp

98 lines
2.6 KiB
C++

#include "mat_mul.h"
#include <cstdlib>
#include <cstdio>
#include <pthread.h>
#include <immintrin.h>
static float *A, *B, *C;
static int M, N, K;
static int num_threads;
#define min(a,b) (a>b? b:a)
static void* mat_mul_thread(void *data) {
// TODO: parallelize & optimize matrix multiplication
int id = *(int *)data;
//printf("id:%d\n", id);
int slice = M/num_threads;
int start = id*slice;
int end = id == num_threads-1? M: (id+1)*slice;
float Aik;
int bs = 32;
/*for (int kk = 0; kk < K; kk += bs) {
for (int i = start; i < end; ++i) {
for (int j =0; j < N; j++) {
int max_k = min(kk+bs, K);
float acc = 0.0;
int k;
for (k = kk; k < (max_k/4)*4; k+=4) {
acc += A[i*K+k+0] * B[k*N+j];
acc += A[i*K+k+1] * B[(k+1)*N+j];
acc += A[i*K+k+2] * B[(k+2)*N+j];
acc += A[i*K+k+3] * B[(k+3)*N+j];
}
for (; k < max_k; k++)
acc += A[i*K+k] * B[k*N+j];
C[i*N+j] += acc;
//for (; j < N; j++)
// C[i * N + j] += Aik * B[k * N + j];
}
}
}*/
for (int kk = 0; kk < K; kk += bs) {
for (int i = start; i < end; ++i) {
for (int k = kk; k < min(kk + bs, K); ++k) {
Aik = A[i*K + k];
int j;
for (j =0; j < (N/8)*8; j+=8) {
C[i*N + j + 0] += Aik * B[k*N + j + 0];
C[i*N + j + 1] += Aik * B[k*N + j + 1];
C[i*N + j + 2] += Aik * B[k*N + j + 2];
C[i*N + j + 3] += Aik * B[k*N + j + 3];
C[i*N + j + 4] += Aik * B[k*N + j + 4];
C[i*N + j + 5] += Aik * B[k*N + j + 5];
C[i*N + j + 6] += Aik * B[k*N + j + 6];
C[i*N + j + 7] += Aik * B[k*N + j + 7];
}
for (; j < N; j++)
C[i*N + j] += Aik * B[k*N + j];
}
}
}
/*for (int kk = 0; kk < K; kk += bs) {
for (int jj = 0; jj < N; jj += bs) {
for (int i = start; i < end; ++i) {
for (int k = kk; k < min(kk + bs, K); ++k) {
Aik = A[i * K + k];
for (int j = jj; j < min(jj + bs, N); j++) {
C[i * N + j] += Aik * 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' pthread
pthread_t *threads;
threads = (pthread_t *)malloc(sizeof(pthread_t) * _num_threads);
for(int i=0; i<_num_threads; i++){
int *pId = (int *)malloc(sizeof(int));
*pId = i;
pthread_create(&threads[i], NULL, mat_mul_thread, pId);
}
for(int i=0; i<_num_threads; i++)
pthread_join(threads[i], NULL);
}