I tried implementing matrix multiplication with parallel for loop in OpenMP as follows. It runs correctly but I want to make sure if I'm missing anything. How does this determine the number of threads to run?
Matrix
is a class for square matrices.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
# include <omp.h>
Matrix parallel_mat_mul(Matrix a, Matrix b)
{
int n =a.getSize();
Matrix c(n);
clock_t begin_time = clock();
# pragma omp parallel shared ( a, b, c, n ) // private ( i, j, k )
{
# pragma omp for
for ( int i = 0; i < n; i++ )
{
for (int j = 0; j < n; j++ )
{
double local_sum=0;
for ( int k = 0; k < n; k++ )
{
local_sum+= (a(i,k)*b(k,j));
}
c(i,j)=local_sum;
}
}
}
cout << "Parallel time: "<<float( clock () - begin_time ) / CLOCKS_PER_SEC <<"\n";
return c;
}
Matrix
somewhere? \$\endgroup\$