#include #include #include void matmul(float *A, float *B, float *C, int l, int m, int n) { int i, j, k; for (i = 0; i < l; i++) { for (j = 0; j < n; j++) { float sum = 0.0; for (k = 0; k < m; k++) sum += A[i * m + k] * B[k * n + j]; C[i*n+j] = sum; } } } void init_matrix(float *A, int h, int w) { int i, j, k; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { A[i * w + j] = i + j; } } } void alloc_matrix(float **m_h, int h, int w) { *m_h = (float *)malloc(sizeof(float) * h * w); } // // MAIN // int main(int argc, char *argv[]) { double time_spent = 0.0; float *Ah, *Bh, *Ch; int L, M, N; clock_t begin = clock(); L = atoi(argv[1]); M = atoi(argv[2]); N = atoi(argv[3]); // prepare matrix A alloc_matrix(&Ah, L, M); init_matrix(Ah, L, M); // prepare matrix B alloc_matrix(&Bh, M, N); init_matrix(Bh, M, N); // allocate space for matrix C alloc_matrix(&Ch, L, N); // call matmul (Matrix Multiplication) function matmul(Ah, Bh, Ch, L, M, N); clock_t end = clock(); time_spent += (double)(end - begin) / CLOCKS_PER_SEC; printf("The elapsed time to multiply a [%d x %d] matrix with a [%d x %d] matrix is %.2f seconds.\n\n",L,M,M,N,time_spent); return 0; }