0
votes

I am rather new with OpenMP. I want to write all elements of a big matrix into a vector using OpenMP threading to speed things up.

In my serial code I am simply doing the following:

m=1
DO k=1,n_lorentz
   DO i=1,n_channels 
     DO p=1,n_lorentz
          DO j=1,n_channels
           vector(m) = Omega(j,p,i,k)
           m=m+1
       END DO
     END DO
   END DO 
END DO 

Now I'd like to use an OMP loop to write the elements of Omega into vector in a parallel fashion:

!$OMP PARALLEL DO PRIVATE(k,i,p,j)
  ! bla bla 
!$OMP END PARALLEL DO  

The question is how to keep track of the current vector index, since in this case the m parameter from the serial code will be incremented by different threads, resulting in a total mess.

1

1 Answers

4
votes

One answer is: you don't need to keep track of m. Instead, analyzing the loop, we find that:

  • Every time j increases by one, m increases by one;
  • Every time p increases by one, m increases by n_channels;
  • Every time i increases by one, m increases by n_channels*n_lorentz;
  • Every time k increases by one, m increases by n_channels*n_lorentz*n_channels.

From these observations, you can write an explicit expression for m:

m = j + n_channels*((p-1) + n_lorentz*((i-1) + n_channels*(k-1)))

Being able to explicitly calculate the index should solve your problem :).