S-26 S-27 int main() S-28 湖 float array[10000]; sub(array,10000); return 0; C/C++ Fortran Example parallel.If S-1 SUBROUTINE SUBDOMAIN(X,ISTART,IPOINTS) INTEGER ISTART,IPOINTS REAL X(*) INTEGER I 89 DO 100 I=1,IPOINTS X(ISTART+I)=123.456 100 CONTINUE s-10 S-11 END SUBROUTINE SUBDOMAIN S-12 s-13 SUBROUTINE SUB(X,NPOINTS) S-14 INCLUDE "omp lib.h" or USE OMP_LIB S-15 S-16 REAL X(*) S INTEGER NPOINTS INTEGER IAM,NT,IPOINTS,ISTART 828 SOMP PARALLEL DEFAULT(PRIVATE)SHARED(X,NPOINTS) IAM OMP_GET_THREAD_NUM() NT OMP_GET_NUM_THREADS ( IPOINTS NPOINTS/NT IPOINTS NPOINTS -ISTART SOMP END PARALL L SUBROUTINE SUB CHAPTER 5.THE PARALLEL CONSTRUCT 15
S-26 S-27 int main() S-28 { S-29 float array[10000]; S-30 S-31 sub(array, 10000); S-32 S-33 return 0; S-34 } C / C++ Fortran 1 Example parallel.1f S-1 SUBROUTINE SUBDOMAIN(X, ISTART, IPOINTS) S-2 INTEGER ISTART, IPOINTS S-3 REAL X(*) S-4 S-5 INTEGER I S-6 S-7 DO 100 I=1,IPOINTS S-8 X(ISTART+I) = 123.456 S-9 100 CONTINUE S-10 S-11 END SUBROUTINE SUBDOMAIN S-12 S-13 SUBROUTINE SUB(X, NPOINTS) S-14 INCLUDE "omp_lib.h" ! or USE OMP_LIB S-15 S-16 REAL X(*) S-17 INTEGER NPOINTS S-18 INTEGER IAM, NT, IPOINTS, ISTART S-19 S-20 !$OMP PARALLEL DEFAULT(PRIVATE) SHARED(X,NPOINTS) S-21 S-22 IAM = OMP_GET_THREAD_NUM() S-23 NT = OMP_GET_NUM_THREADS() S-24 IPOINTS = NPOINTS/NT S-25 ISTART = IAM * IPOINTS S-26 IF (IAM .EQ. NT-1) THEN S-27 IPOINTS = NPOINTS - ISTART S-28 ENDIF S-29 CALL SUBDOMAIN(X,ISTART,IPOINTS) S-30 S-31 !$OMP END PARALLEL S-32 END SUBROUTINE SUB S-33 CHAPTER 5. THE PARALLEL CONSTRUCT 15
PROGRAM PAREXAMPLE RE ARRAY(10000) END SUB(ARRAY,10000) PROGRAM PAREXAMPLE Fortran OpenMP Examples Version 4.0.2-March 2015
S-34 PROGRAM PAREXAMPLE S-35 REAL ARRAY(10000) S-36 CALL SUB(ARRAY, 10000) S-37 END PROGRAM PAREXAMPLE Fortran 16 OpenMP Examples Version 4.0.2 - March 2015
1 CHAPTER 6 2 Controlling the Number of Threads 3 on Multiple Nesting Levels 4 The following examples demonstrate how to use the OMP_NUM_THREADS environment variable to 5 control the mber of threads on multiple nesting levels: C/C++ 6 Example nthrs_nesting.Ic 82 #include <stdio.h> #include <omp.h> 455 int main (void) omp_set_nested(1); omp_set_dynamic(0); #pragma omp parallel 88 #pragma omp parallel s-1 S-11 #pragma omp single 816 6 If OMP_NUM_THREADS=2,3 was set,the following should print Inner:num_thds=3 *Inner:num_thds=3 If nesting is not supported,the following should print: Inner:num thds=】 Inner:num_thds=1 printf ("Inner:num_thds=%d\n",omp_get_num_threads()); 17
1 CHAPTER 6 2 Controlling the Number of Threads 3 on Multiple Nesting Levels 4 The following examples demonstrate how to use the OMP_NUM_THREADS environment variable to 5 control the number of threads on multiple nesting levels: C / C++ 6 Example nthrs_nesting.1c S-1 #include <stdio.h> S-2 #include <omp.h> S-3 int main (void) S-4 { S-5 omp_set_nested(1); S-6 omp_set_dynamic(0); S-7 #pragma omp parallel S-8 { S-9 #pragma omp parallel S-10 { S-11 #pragma omp single S-12 { S-13 /* S-14 * If OMP_NUM_THREADS=2,3 was set, the following should print: S-15 * Inner: num_thds=3 S-16 * Inner: num_thds=3 S-17 * S-18 * If nesting is not supported, the following should print: S-19 * Inner: num_thds=1 S-20 * Inner: num_thds=1 S-21 */ S-22 printf ("Inner: num_thds=%d\n", omp_get_num_threads()); S-23 } S-24 } 17
#pragma omp barrier omp_set_nested(0); #pragma omp paralle】 1 pragma omp single *Even the following should Inner Inner numthds-1 printf ("Inner:num_thds=%d\n",omp_get_num_threads()); pragma omp pragma omp single 44404044 If OMP_NUM_THREADS=2,3 was set, the following should print Outer num_thds=2 printf ("Outer: num_thds=%d\n", omp_get_num_threads())i return 0; s-51 C/C++ Fortran Example nthrs_nesting.If 15o吧 1 1If OMP NUM_THREADS=2,3 was set, the following should print er nesting is not supported,the following should print s-12 Inner:num thds= S-13 Inner: um thds=1 s-14 print t. "Inner:num_thds=",omp_get_num_threads() S-15 !Somp end single OpenMP Examples Version 4.0.2-March 2015
S-25 #pragma omp barrier S-26 omp_set_nested(0); S-27 #pragma omp parallel S-28 { S-29 #pragma omp single S-30 { S-31 /* S-32 * Even if OMP_NUM_THREADS=2,3 was set, the following should S-33 * print, because nesting is disabled: S-34 * Inner: num_thds=1 S-35 * Inner: num_thds=1 S-36 */ S-37 printf ("Inner: num_thds=%d\n", omp_get_num_threads()); S-38 } S-39 } S-40 #pragma omp barrier S-41 #pragma omp single S-42 { S-43 /* S-44 * If OMP_NUM_THREADS=2,3 was set, the following should print: S-45 * Outer: num_thds=2 S-46 */ S-47 printf ("Outer: num_thds=%d\n", omp_get_num_threads()); S-48 } S-49 } S-50 return 0; S-51 } C / C++ Fortran 1 Example nthrs_nesting.1f S-1 program icv S-2 use omp_lib S-3 call omp_set_nested(.true.) S-4 call omp_set_dynamic(.false.) S-5 !$omp parallel S-6 !$omp parallel S-7 !$omp single S-8 ! If OMP_NUM_THREADS=2,3 was set, the following should print: S-9 ! Inner: num_thds= 3 S-10 ! Inner: num_thds= 3 S-11 ! If nesting is not supported, the following should print: S-12 ! Inner: num_thds= 1 S-13 ! Inner: num_thds= 1 S-14 print *, "Inner: num_thds=", omp_get_num_threads() S-15 !$omp end single 18 OpenMP Examples Version 4.0.2 - March 2015
S-16 !Somp end parallel S-17 !Somp barrier s-18 call omp_set_nested(.false.) S-19 !Somp parallel s-20 !Somp single S-21 Even if OMP_NUM_THREADS=2,3 was set,the following should print, s-2 because nesting is disabled: !Inner:num_thds=1 !Inner:num_thds=1 print *"Inner:num_thds=",omp_get_num_threads() !Somp end single S-27 !Somp end parallel !Somp barrier !Somp single s-30 If OMP_NUM_THREADS=2,3 was set,the following should print: S-31 Outer:num_thds=2 print *"outer:num_thds=",omp_get_num_threads() !Somp end single !Somp end parallel Fortran CHAPTER 6.CONTROLLING THE NUMBER OF THREADS ON MULTIPLE NESTING Levels 19
S-16 !$omp end parallel S-17 !$omp barrier S-18 call omp_set_nested(.false.) S-19 !$omp parallel S-20 !$omp single S-21 ! Even if OMP_NUM_THREADS=2,3 was set, the following should print, S-22 ! because nesting is disabled: S-23 ! Inner: num_thds= 1 S-24 ! Inner: num_thds= 1 S-25 print *, "Inner: num_thds=", omp_get_num_threads() S-26 !$omp end single S-27 !$omp end parallel S-28 !$omp barrier S-29 !$omp single S-30 ! If OMP_NUM_THREADS=2,3 was set, the following should print: S-31 ! Outer: num_thds= 2 S-32 print *, "Outer: num_thds=", omp_get_num_threads() S-33 !$omp end single S-34 !$omp end parallel S-35 end Fortran CHAPTER 6. CONTROLLING THE NUMBER OF THREADS ON MULTIPLE NESTING LEVELS 19