#include #include "mat_mul.h" #include #include #include static float *A, *B, *C; static int M, N, K; static int num_threads; static int num_split; inline int MIN(int a, int b) {return (a>b) ? b : a;} static void* mat_mul_thread(void *data) { int pid = * (int *) data; int str = num_split * pid; int end = str + num_split; if (end > M) end = M; //printf("[thread %2d] %0d ~ %0d\n", pid, str, end); // TODO: parallelize & optimize matrix multiplication //register int step_i = 64; //register int step_j = 8; register int step_k = 48; // 40~48 register int i, j, k; //register int ii, jj, kk; register int kk; float A_; //__m256 A0; //__m256 B0; //__m256 C0; for (kk = 0; kk < K; kk += step_k) // k split only //for (jj = 0; jj < N; jj += step_j) //for (ii = str; ii < end; ii += step_i) //for (i = ii; i < MIN(ii+step_i,end); ++i) for (i = str; i < end; ++i) // better { for (k = kk; k < MIN(kk+step_k,K); ++k) { A_ = A[i * K + k]; // A selection //A0 = _mm256_set_ps(A_, A_, A_, A_, A_, A_, A_, A_); //for (jj = 0; jj < N; jj += 8) { // loop 8 // B0 = _mm256_load_ps(B + k * N + jj); // C0 = _mm256_fmadd_ps(A0, B0, C0); // for (j = jj; j < MIN(jj+8,N); ++j) { // C[i * N + j] += A_ * B[k * N + j]; // } //} for (j = 0; j < N; ++j) // better { C[i * N + j] += A_ * 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; if (num_threads > M) num_threads = M; // column split number num_split = (M / num_threads) + ((M % num_threads) > 0); //printf("\n[mat_mul] num_split = %d [%d]\n", num_split, num_threads); // TODO: create '_num_threads' pthreads //pthread_t thread; //pthread_create(&thread, NULL, mat_mul_thread, NULL); //pthread_join(thread, NULL); 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, (void *)pid); // add pid for work } for (int i = 0; i < num_threads; i++) { pthread_join(threads[i], NULL); // check join options } free(threads); }