Problem in Nested for Loop

General OpenMP discussion

Re: Problem in Nested for Loop

Postby johncampbell » Thu Apr 03, 2014 9:16 pm

Although !$omp offers much performance improvement if suitably used, there are also some basic programming strategies to minimise memory access and improve performance. These become even more significant when using openmp. From the coding example msohail posted on 7th March, I would recommend changing the do loop order to sequentially access memory:

!$omp parallel do collapse(3)

DO K=1,NK
DO J=1,NJ
DO I=1,NI
Q(I,J,K)=V(I,J,K) ( update the variable )
END DO
END DO
END DO
!$OMP END PARALLEL DO

You did do this with the other nested loops, but (depending on the values of NI,NJ,NK) you might be surprised how slow your alternative loop is for accessing memory, especially if arrays Q and V are much larger that the cache size of the computer you are using.

I have not used collapse, but I wonder if NK is much larger than the number of available threads, would collapse have any benefit ? There is no indication of the relative size of NI,NJ,NK to indicate if collapse is worthwile or which loop order might be most effective.

John
johncampbell
 
Posts: 10
Joined: Tue Aug 27, 2013 6:48 pm

Re: Problem in Nested for Loop

Postby MarkB » Fri Apr 04, 2014 4:17 am

johncampbell wrote:I have not used collapse, but I wonder if NK is much larger than the number of available threads, would collapse have any benefit ?


You are correct: if NK is much larger than the maximum number of threads, then there is no real reason to use collapse. In this instance, using collapse(3) could have a detrimental effect - the loop body is very small and the compiler might not optimise the implied index expressions very well. Using collapse(2) might be better if there is enough parallelism in the NK and NJ loops.
MarkB
 
Posts: 480
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Previous

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 7 guests

cron