26.06.2015 Views

Parallel Programming in Fortran 95 using OpenMP - People

Parallel Programming in Fortran 95 using OpenMP - People

Parallel Programming in Fortran 95 using OpenMP - People

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

12 2. <strong>OpenMP</strong> constructs<br />

real(8) :: A(1000), B(1000)<br />

do i = 1, 1000<br />

B(i) = 10 * i<br />

A(i) = A(i) + B(i)<br />

enddo<br />

because the correct value of the matrix B is not ensured until the end of the workshar<strong>in</strong>g<br />

construct !$OMP END DO.<br />

The fact that the iterations of the do-loop are distributed over the different threads<br />

<strong>in</strong> an unpredictable way 3 , discards certa<strong>in</strong> do-loops from be<strong>in</strong>g parallelized. For example:<br />

real(8) :: A(1000)<br />

do i = 1, 999<br />

A(i) = A(i+1)<br />

enddo<br />

The reason for the <strong>in</strong>correct work<strong>in</strong>g is that the value at the <strong>in</strong>dex i+1 is needed <strong>in</strong><br />

its unmodified state when perform<strong>in</strong>g the iteration i. This will lead to no trouble when<br />

executed <strong>in</strong> a serial way, but when runn<strong>in</strong>g <strong>in</strong> parallel the unmodified state of i+1 is not<br />

granted at the time of comput<strong>in</strong>g iteration i. Therefore, an unpredictable result will be<br />

obta<strong>in</strong>ed when runn<strong>in</strong>g <strong>in</strong> parallel. This situation is known as rac<strong>in</strong>g condition: the<br />

result of the code depends on the thread schedul<strong>in</strong>g and on the speed of each processor.<br />

By modify<strong>in</strong>g the previous do-loop it is possible to achieve a parallelizable version lead<strong>in</strong>g<br />

to the follow<strong>in</strong>g parallelized code 4 :<br />

real(8) :: A(1000), dummy(2:1000:2)<br />

!Saves the even <strong>in</strong>dices<br />

!$OMP DO<br />

do i = 2, 1000, 2<br />

dummy(i) = A(i)<br />

enddo<br />

!$OMP END DO<br />

!Updates even <strong>in</strong>dices from odds<br />

!$OMP DO<br />

do i = 0, 998, 2<br />

A(i) = A(i+1)<br />

enddo<br />

!$OMP END DO<br />

!Updates odd <strong>in</strong>dices with evens<br />

!$OMP DO<br />

3 Although the expression ”unpredictable” usually means someth<strong>in</strong>g negative, <strong>in</strong> this case it states<br />

someth<strong>in</strong>g that is <strong>in</strong>herent <strong>in</strong> the philosophy of <strong>OpenMP</strong>, namely the separation between the parallel<br />

programm<strong>in</strong>g and the underly<strong>in</strong>g hardware and work distribution.<br />

4 The presented modification is not the only possible one, but at least it allows the parallelization of<br />

the do-loop at expense of additional memory requirements.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!