# matrix product openMP code in C runs slower in parallel than series

If I start with 4 threads when I run my code and increase thread number each time I run it, my run time decreases as you would expect. However, my run time for 1 thread is significantly smaller than my run time for 4-16 threads. How can that be? Is it an issue with the time needed to create new threads? Should I change my code or is this to be expected? Originally I thought I could have a racing issue, but I included a private clause, which I think prevents that? My code and run times/ thread are below.

int matrixProduct(int m, int n, int p, double *A, double *B, double *C){
int i, j, k;

for( i = 0 ; i < m*n ; i ++)
C[i] = 0.0;

#pragma omp parallel shared(A,B,C) private(i,j,k)
{
#pragma omp for schedule(static)
for( i = 0 ; i < m ; i ++)    {
for( j = 0 ; j < p ; j ++) {
for( k = 0 ; k < n ; k ++ ){
C[k+i*n] += A[j+i*p]*B[k+i*n];
}
}
}
}
return 0;
}

• It's probably overhead involved in the parallelization process. – Yuval Filmus Mar 4 at 8:36
• This question verges on being off-topic here. On the one hand, this phenomenon is general – due to overhead, parallelization doesn't always improve performance. On the other hand, questions specific to OpenMP and its particular implementations are off-topic here. – Yuval Filmus Mar 4 at 8:47
• OpenMP hides the implementation. Have you tried rewriting your task using posix threads, for example, for more fine control? – zkutch Mar 4 at 15:54