#include "mat_mul.h" #include #include #include 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 //printf("thread #: %d\n", *(int*)data); fflush(stdout); int pid = *(int*)data; int slice = M / num_threads; int start = pid * slice; int end = pid == num_threads -1 ? M : (pid + 1) * slice; int ROW_BLOCK = 64; int COL_BLOCK = 1; //register float A_reg; //for (int kk = 0; kk < K; kk += BLOCK){ for (int kk = 0; kk < K; kk += ROW_BLOCK){ //for (int jj = 0; jj < N; jj += BLOCK){ //for (int jj = 0; jj < N; jj += COL_BLOCK){ for (int i = start; i < end; ++i) { for(int k = kk; k < (kk+ROW_BLOCK < K ? kk+ROW_BLOCK : K); ++k) { //A_reg = A[i * K + k]; //for (int j = jj; j < (jj+COL_BLOCK < N ? jj+COL_BLOCK : N); ++j) { for (int j = 0; j < N; ++j) { C[i * N + j] += A[i * K + k] * B[k * N + j]; //C[i * N + j] += A_reg * B[k * N + j]; //if(i==0 && j==640) { // printf("val c : %f\n",C[i*N+j]); // fflush(stdout); // } } } } //} } 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_t*)malloc(sizeof(pthread_t) * num_threads); for(int i=0; i