chundoong-lab-ta/SamsungDS22/submissions/HW2/dk.han/mat_mul.cpp

204 lines
4.7 KiB
C++

#include "mat_mul.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) {
// // TODO: parallelize & optimize matrix multiplication
// for (int i = 0; i < M; ++i) {
// for (int j = 0; j < N; ++j) {
// for (int k = 0; k < K; ++k) {
// C[i * N + j] += A[i * K + k] * B[k * N + j];
// }
// }
// }
//
// return NULL;
//}
//static void* mat_mul_thread(void *data) {
// float acc00, acc01, acc10, acc11;
// // TODO: parallelize & optimize matrix multiplication
// for (int i = 0; i < M; i+=2) {
// for (int j = 0; j < N; j+=2) {
// acc00 = acc01 = acc10 = acc11 = 0;
// for (int k = 0; k < K; ++k) {
// //acc00 = B[k*2+j] * A[i*2+k];
// acc00 += A[i * K + k] * B[k * N + j];
// acc01 += A[i * K + k] * B[k * N + j+1];
// acc10 += A[(i+1) * K + k] * B[k * N + j];
// acc11 += A[(i+1) * K + k] * B[k * N + j+1];
// //C[i * N + j] += A[i * K + k] * B[k * N + j];
// }
// C[i*N+j] += acc00;
// C[i*N+j+1] += acc01;
// C[(i+1)*N+j] += acc10;
// C[(i+1)*N+j+1] += acc11;
// }
// }
//
// return NULL;
//}
/*
static void* mat_mul_thread(void *data) {
// TODO: parallelize & optimize matrix multiplication
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < K; ++k) {
C[i * N + j] += A[i * K + k] * B[k * N + j];
}
}
}
return NULL;
}
*/
/*
static void* mat_mul_thread(void *data) {
*/
static int min (int a, int b) {
return ( a > b ) ? b : a;
}
static void* worker(void *data) {
int pid = * (int *) data;
int slice = M / num_threads;
int start = pid * slice;
int end = pid == num_threads - 1 ? M : (pid + 1) * slice;
float Aik;
int k_bs = 32;
int j_bs = 1024;
//int bs = 1024;
//int bs = 512;
//int bs = 256;
//int bs = 2048;
// int bs = BLOCKSIZE;
/*
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];
}
}
}
}
}
*/
/*
sum1 = 0, sum2 = 0;
for(i=0; i<n/2; i++) sum1 += a[2*i], sum2 += a[2*i+1];
for(i=(n/2)*2; i<n; i++) sum += a[i]; // clean up for n odd
sum += sum1 + sum2;
*/
/*
__m256 a0;
__m256 b0;
__m256 s0;
s0 = _mm256_set_ps(0.,0.,0.,0.,0.,0.,0.,0.);
*/
for (int kk = 0; kk < K; kk += k_bs) {
for (int jj = 0; jj < N; jj += j_bs) {
for (int i = start; i < end; i++) {
for (int k = kk; k < min(kk + k_bs, K); k++) {
Aik = A[i * K + k];
/*
*/
for (int j = jj; j < min(jj + j_bs, N); j++) {
C[i * N + j] += Aik * B[k * N + j];
}
/*
// ORG Start
// 194 GFLOPs
for (int j = jj; j < min(jj + bs, N) ; j=j+2) {
//if (i==0 && j==256) printf("##### %d %d\n",Aik, j);
C[i * N + j] += Aik * B[k * N + j];
C[i * N + j + 1] += Aik * B[k * N + j + 1];
}
for (int j = (min(jj + bs, N)/2)*2; j < min(jj + bs, N); j++) {
C[i * N + j] += Aik * B[k * N + j];
}
// ORG End
*/
/*
// No j
// 320
for (int j = 0; j < N ; j++) {
C[i * N + j] += Aik * B[k * N + j];
}
*/
/*
for (int j = jj; j < min(jj + bs, N) ; j=j+8) {
//if (i==0 && j==256) printf("##### %d %d\n",Aik, j);
C[i * N + j] += Aik * B[k * N + j];
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];
//C[i * N + j] += Aik * B[k * N + j];
}
*/
/*
for (int j = jj; j < min(jj + bs, N) ; j=j+8) {
a0 = _mm256_load_ps(a + j * 8);
b0 = _mm256_load_ps(b + j * 8);
s0 = _mm256_fmadd_ps(a0, b0, s0);
}
*/
}
}
}
}
return NULL;
}
/*
*/
static void* mat_mul_thread(void *data) {
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, worker, pid);
}
for (int i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
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;
pthread_create(&thread, NULL, mat_mul_thread, NULL);
pthread_join(thread, NULL);
}