Search code examples
cmultithreadingperformanceparallel-processingopenmp

LU-factorization with OpenMP seems to slow, need advice


I have written a code which performs LU-factorization with OpenMP. I feel like the code is performing too slow. The computer I am working on have 16 cores and the time for 16 threads is 12.5s and for 4 threads 14.3s. This is the section of the code where the parallelization is happening. I am new to C programming and feel like I am missing something that is slowing the multithreading down.

    /*__Initialize lock__*/
omp_lock_t lock[n];

for(i=0;i<n;i++)
   omp_init_lock(&lock[i]);

/*___Create theads____*/
#pragma omp parallel private(i,j,k,thread) num_threads(nThreads)
{
  thread=omp_get_thread_num();
  #pragma omp for schedule(static)
  for(i=0;i<n;i++) omp_set_lock(&lock[i]);
  if(thread==0)
    omp_unset_lock(&lock[0]);

  for(i=0;i<n;i++){
    div=1/U[i][i];
    #pragma omp for schedule(static)
      for(j=i+1;j<n;j++)
         L[j][i]=U[j][i]*div;

    #pragma omp for schedule(static)
      for(j=i+1;j<n;j++){
        for(k=i;k<n;k++)
           U[j][k]=U[j][k]-L[j][i]*U[i][k];
        if(j==i+1)
           omp_unset_lock(&lock[i+1]);
      }
}
}

Solution

  • Based on the code that you have provided it seems to me that you do not need any lock whatsoever, try the following parallel version:

    #pragma omp parallel num_threads(nThreads)
    {
      for(int i=0;i<n;i++){
          div=1/U[i][i];
          #pragma omp for schedule(static)
          for(j=i+1;j<n;j++)
             L[j][i]=U[j][i]*div;
    
          #pragma omp for schedule(static)
          for(int j=i+1;j<n;j++){
            for(int k=i;k<n;k++)
               U[j][k]=U[j][k]-L[j][i]*U[i][k];
          }
    }