Effeciently parallelizing the code in fortran

Effeciently parallelizing the code in fortran

Hi,

I am trying to parallelize a certain section of my code which is written in fortran. The code snippet looks as below:

do i=1,50

j = K
... if conditions on K...
....write and reads on j...
... do lot of things ...
K = K+1

So I tried to parallelize using the below code.. which was obviously not as it should have been

!$OMP PARALLEL DO PRIVATE(j)
do i=1,50
j = K
... if conditions on K...
....write and reads on j...
... do lot of things ...
K = K+1
!$OMP END PARALLEL DO


 

The obvious mistake being, all the threads race to the same K. What would be the best way to ensure every thread gets assigned an incremental K and the threads run in parallel.

Thanks
Ajay
 

6 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

I think the moderators of this forum didn't intend to answer general questions which might as well be asked on one of the Fortran forums.

Your description leaves out too much.   If K can be calculated from i, that would be a change in the right direction.

Ajay,

Tim is right, you might get faster/better answer if asking on the right forum (like "Threading on Intel® Parallel Architectures", or Intel Fortran compiler forum, or general OpenMP forun on openmp.org), as your question is not related to Intel Open Source OpenMP library.

As to the subject of your question, you may want to try atomic synchronization of all accesses to K in parallel region, like:

!$OMP atomic read
j = K
...
!$OMP atomic update
K = K+1

Regards,
Andrey

Thank you, I will post it in the fortran section.

Andrey,

I just tried this within the do loop and it throws me error on both read and update lines, saying it was expecting end of statement. 

Also, does ensuring atomicity multithread the code? I mean I hope all the threads wont be waiting for the update, in that case I wont achieve the desired effect.

Tim,

Unfortunately I cant make it change with i. The do function is actually something of this sort
do i,array(K)

Thanks
Ajay

Ajay,

Looks like you are using not the latest compiler.  In this case you can try to not use atomic for read (on x86 architecture the read is usually atomic anyway, unless you have weird data alignment), and skip the "update" keyword at atomic increment.

Regards,
Andrey

Forgot to comment on the scaling. Indeed the atomic operation ensures exclusive assess to the memory and thus impacts scaling of the application. But this impact should be very small because the atomic is implemented in hardware, so it is the "fastest" synchronization construct. You can also achive exclusive access using locks or critical sections, but with much bigger impact on the application scalability.

Regards,
Andrey

Accedere per lasciare un commento.