#include #include #include int f1( ) { int UB = 1000; double **a = malloc(UB*sizeof(double *)); double **b = malloc(UB*sizeof(double *)); double **c = malloc(UB*sizeof(double *)); a[0] = malloc(UB*UB*sizeof(double)); b[0] = malloc(UB*UB*sizeof(double)); c[0] = malloc(UB*UB*sizeof(double)); for (int i = 1; i < UB; i++) { a[i] = a[0] + (UB * i); b[i] = b[0] + (UB * i); c[i] = c[0] + (UB * i); } // initialize the arrays for (int i = 0; i < UB; i++) { for (int j = 0; j < UB; j++) { a[i][j] = 1.0; b[i][j] = 1.0; c[i][j] = 0.0; } } // Perform the unoptimized matrix multiply for (int i = 0; i < UB; i++) { for (int j = 0; j < UB; j++) { for (int k = 0; k < UB; k++) { c[i][j] += a[i][k] + b[k][j]; } } } // free allocated storage free(a[0]); free(b[0]); free(c[0]); free(a); free(b); free(c); } int f2( ) { int UB = 1000; // allocate 2 dimensional arrays for A, B and C double **a = malloc(UB * sizeof(double *)); double **b = malloc(UB * sizeof(double *)); double **c = malloc(UB * sizeof(double *)); a[0] = malloc(UB*UB*sizeof(double)); b[0] = malloc(UB*UB*sizeof(double)); c[0] = malloc(UB*UB*sizeof(double)); for (int i = 1; i < UB; i++) { a[i] = a[0] + (UB * i); b[i] = b[0] + (UB * i); c[i] = c[0] + (UB * i); } // initialize the arrays for (int i = 0; i < UB; i++) { for (int j = 0; j < UB; j++) { a[i][j] = 1.0; b[i][j] = 1.0; c[i][j] = 0.0; } } for (int i = 0; i < UB; i++) { for (int j = 0; j < UB; j++) { for (int k = 0; k < UB; k++) { c[i][j] += a[i][k] + b[k][j]; } } } // free allocated storage free(a[0]); free(b[0]); free(c[0]); free(a); free(b); free(c); } int main (int argc, char *argv[]) { double t = omp_get_wtime( ); // make these two calls run in parallel using OpenMP parallel sections // run on scholar #pragma omp parallel sections { #pragma omp section { f1( ); } #pragma omp section { f2( ); } } t = omp_get_wtime( ) - t; printf("time for sequential lab3d is %g\n", t); }