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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

48 3. PRIVATE, SHARED & Co.<br />

it is not necessary to wait at the end of the first parallel do-loop for all the threads<br />

to f<strong>in</strong>ish, s<strong>in</strong>ce the second do-loop has noth<strong>in</strong>g <strong>in</strong> common with the first one. Therefore,<br />

a NOWAIT clause is added at the end of the first !$OMP END DO clos<strong>in</strong>g-directive. In this<br />

way it is possible to speed up the computations, s<strong>in</strong>ce a costly synchronization has been<br />

elim<strong>in</strong>ated.<br />

But it is necessary to use the NOWAIT clause with care, s<strong>in</strong>ce a wrong use of it can<br />

leed to unexpected results without previous notice dur<strong>in</strong>g the development phase of the<br />

program. The programmer should also be aware of the fact that us<strong>in</strong>g the NOWAIT clause<br />

also suppresses the implied data synchronizations.<br />

3.2.4 SCHEDULE(type, chunk)<br />

When a do-loop is parallelized and its iterations distributed over the different threads, the<br />

most simple way of do<strong>in</strong>g this is by giv<strong>in</strong>g to each thread the same number of iterations.<br />

But this is not always the best choice, s<strong>in</strong>ce the computational cost of the iterations may<br />

not be equal for all of them. Therefore, different ways of distribut<strong>in</strong>g the iterations exist.<br />

The present clause is meant to allow the programmer to specify the schedul<strong>in</strong>g for each<br />

do-loop us<strong>in</strong>g the follow<strong>in</strong>g syntaxis:<br />

!$OMP DO SCHEDULE(type, chunk)<br />

The SCHEDULE clause accepts two parameters. The first one, type, specifies the way<br />

<strong>in</strong> which the work is distributed over the threads. The second one, chunk, is an optional<br />

parameter specify<strong>in</strong>g the size of the work given to each thread: its precise mean<strong>in</strong>g depends<br />

on the type of schedul<strong>in</strong>g used.<br />

Four different options of schedul<strong>in</strong>g exist, which are:<br />

STATIC :when this option is specified, the pieces of work created from the iteration space<br />

of the do-loop are distributed over the threads <strong>in</strong> the team follow<strong>in</strong>g the order of<br />

their thread identification number. This assignment of work is done at the beg<strong>in</strong>n<strong>in</strong>g<br />

of the do-loop and stays fixed dur<strong>in</strong>g its execution.<br />

By default, the number of pieces of work is equal to the number of threads <strong>in</strong> the<br />

team and all pieces are approximately equal <strong>in</strong> size. The junction of all these pieces<br />

is equal to the complete iteration space of the do-loop.<br />

If the optional parameter chunk is specified, the size of the pieces is fixed to that<br />

amount. In order to correctly distribute the total work from the iteration space, one<br />

piece of work is allowed to have a different size than chunk. The result<strong>in</strong>g pieces of<br />

work are distributed to the threads <strong>in</strong> a round-rob<strong>in</strong> fashion.<br />

To clarify the ideas, the follow<strong>in</strong>g example with three threads is considered:<br />

!$OMP DO SCHEDULE(STATIC, chunk)<br />

do i = 1, 600<br />

...<br />

enddo<br />

!$OMP END DO

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

Saved successfully!

Ooh no, something went wrong!