Parallel DO - Sample Solution - C

#include <stdio.h>

#define NRA 1000 		/ * number of rows in matrix A * /
#define NCA 1000        	/ * number of columns in matrix A * /
#define NCB 1000		/ * number of columns in matrix B * /
main()
{
int    i, j, k;			/ * misc * /
double a[NRA][NCA], 		/ * matrix A to be multiplied * /
       b[NCA][NCB],      	/ * matrix B to be multiplied * /
       c[NRA][NCB];		/ * result matrix C * /

/ * Initialize A, B, and C matrices * /
   for (i=0; i< NRA; i++)
      for (j=0; j< NCA; j++)
         a[i][j]= 1.;
   for (i=0; i< NCA; i++)
      for (j=0; j< NCB; j++)
         b[i][j]= 1.;
   for(i=0;i< NRA;i++)
      for(j=0;j< NCB;j++)
         c[i][j] = 0.0;

/ * Perform matrix multiply * /
#pragma omp parallel shared(a,b,c) private(i,j,k)
{
#pragma omp for nowait
   for(i=0;i< NRA;i++)
      for(j=0;j< NCB;j++)
         for(k=0;k< NCA;k++)
            c[i][j]+= a[i][k] * b[k][j];
}

   printf("Done
");
   printf("%f
 ", c[0][0]);
   printf("%f
 ", c[NRA-1][NCB-1]);
   printf ("
");
}