13.11.2016 Views

OpenMP Application Programming Interface Examples

2fZ58Wr

2fZ58Wr

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.

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

1.11 The single Construct<br />

The following example demonstrates the single construct. In the example, only one thread prints<br />

each of the progress messages. All other threads will skip the single region and stop at the<br />

barrier at the end of the single construct until all threads in the team have reached the barrier. If<br />

other threads can proceed without waiting for the thread executing the single region, a nowait<br />

clause can be specified, as is done in the third single construct in this example. The user must<br />

not make any assumptions as to which thread will execute a single region.<br />

Example single.1.c<br />

C / C++<br />

S-1 #include <br />

S-2<br />

S-3 void work1() {}<br />

S-4 void work2() {}<br />

S-5<br />

S-6 void single_example()<br />

S-7 {<br />

S-8 #pragma omp parallel<br />

S-9 {<br />

S-10 #pragma omp single<br />

S-11 printf("Beginning work1.\n");<br />

S-12<br />

S-13 work1();<br />

S-14<br />

S-15 #pragma omp single<br />

S-16 printf("Finishing work1.\n");<br />

S-17<br />

S-18 #pragma omp single nowait<br />

S-19 printf("Finished work1 and beginning work2.\n");<br />

S-20<br />

S-21 work2();<br />

S-22 }<br />

S-23 }<br />

C / C++<br />

CHAPTER 1. PARALLEL EXECUTION 27

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

Saved successfully!

Ooh no, something went wrong!