#include "mat_mul.h" #include #include #include #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define B_SIZE 32 static float *A, *B, *C; static int M, N, K; static int num_threads; static void* mat_mul_thread(void *data) { /* * Non-optimized */ #ifdef _O0 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]; } } } /* * Array dimension change * i-j-k -> i-k-j */ #elif _O1 for (int i = 0; i < M; ++i) { for (int k = 0; k < K; ++k) { for (int j = 0; j < N; ++j) { register float tmp = A[i * K + k]; C[i * N + j] += tmp * B[k * N + j]; } } } /* * Blocked matrix multiplication * Best block size: 256 bytes */ #elif _O2 for (int ii = 0; ii < M; ii+=B_SIZE) for (int kk = 0; kk < K; kk+=B_SIZE) for (int jj = 0; jj < N; jj+=B_SIZE) for (int i=ii; i