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 />

1.16 The omp_get_num_threads Routine<br />

In the following example, the omp_get_num_threads call returns 1 in the sequential part of<br />

the code, so np will always be equal to 1. To determine the number of threads that will be deployed<br />

for the parallel region, the call should be inside the parallel region.<br />

Example get_nthrs.1.c<br />

C / C++<br />

S-1 #include <br />

S-2 void work(int i);<br />

S-3<br />

S-4 void incorrect()<br />

S-5 {<br />

S-6 int np, i;<br />

S-7<br />

S-8 np = omp_get_num_threads(); /* misplaced */<br />

S-9<br />

S-10 #pragma omp parallel for schedule(static)<br />

S-11 for (i=0; i < np; i++)<br />

S-12 work(i);<br />

S-13 }<br />

C / C++<br />

Fortran<br />

Example get_nthrs.1.f<br />

S-1 SUBROUTINE WORK(I)<br />

S-2 INTEGER I<br />

S-3 I = I + 1<br />

S-4 END SUBROUTINE WORK<br />

S-5<br />

S-6 SUBROUTINE INCORRECT()<br />

S-7 INCLUDE "omp_lib.h" ! or USE OMP_LIB<br />

S-8 INTEGER I, NP<br />

S-9<br />

S-10 NP = OMP_GET_NUM_THREADS() !misplaced: will return 1<br />

S-11 !$OMP PARALLEL DO SCHEDULE(STATIC)<br />

S-12 DO I = 0, NP-1<br />

S-13 CALL WORK(I)<br />

S-14 ENDDO<br />

S-15 !$OMP END PARALLEL DO<br />

S-16 END SUBROUTINE INCORRECT<br />

38 <strong>OpenMP</strong> <strong>Examples</strong> Version 4.5.0 - November 2016

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

Saved successfully!

Ooh no, something went wrong!