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.

1 thread: .570s

4 threads: 2.63s

8 threads: 1.40s

16 threads:.825s

32 threads: .594s

64 threads: .510s

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;
  • $\begingroup$ It's probably overhead involved in the parallelization process. $\endgroup$ Mar 4 at 8:36
  • $\begingroup$ 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. $\endgroup$ Mar 4 at 8:47
  • $\begingroup$ OpenMP hides the implementation. Have you tried rewriting your task using posix threads, for example, for more fine control? $\endgroup$
    – zkutch
    Mar 4 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.