/* OpenMP implementation of matrix multiplication. Each thread takes care a chunk of rows. Compile with gcc -O3 -fopenmp omp_matrixmult.c -o omp_matrixmult */ // Online source: http://users.abo.fi/mats/PP2012/examples/OpenMP/omp_critical.c // permission obtained #include #include #include /* Number of threads used */ #define NR_THREADS 4 /* #define DEBUG 0 #define NRA 1400 // number of rows in matrix A #define NCA 1400 // number of columns in matrix A #define NCB 1400 // number of columns in matrix B */ /* Use smaller matrices for testing and debugging */ #define DEBUG 1 #define NRA 10 // number of rows in matrix A #define NCA 10 // number of columns in matrix A #define NCB 10 // number of columns in matrix B int main (int argc, char *argv[]) { int tid, nthreads, i, j, k; double **a, **b, **c; double *a_block, *b_block, *c_block; double **res; double *res_block; double starttime, stoptime; a = (double **) malloc(NRA*sizeof(double *)); /* matrix a to be multiplied */ b = (double **) malloc(NCA*sizeof(double *)); /* matrix b to be multiplied */ c = (double **) malloc(NRA*sizeof(double *)); /* result matrix c */ a_block = (double *) malloc(NRA*NCA*sizeof(double)); /* Storage for matrices */ b_block = (double *) malloc(NCA*NCB*sizeof(double)); c_block = (double *) malloc(NRA*NCB*sizeof(double)); /* Result matrix for the sequential algorithm */ res = (double **) malloc(NRA*sizeof(double *)); res_block = (double *) malloc(NRA*NCB*sizeof(double)); for (i=0; i