threads 2.3 execute on p3 with the place partition p3 threads 4.5 execute on p4 with the place partition p4 34 .threads 6.7 execute on p5 with the place partition p5 .threads 8.9 execute on p6 with the place partition p6 ⊙ threads 10,11 execute on p7 with the place partition p7 67 .threads 12.13 execute on po with the place partition po .threads 14,15 execute on pl with the place partition pl 88.2 Close Affinity Policy 8 The following example shows the result of policy on the partition list when th number of threads is less than or equal to the number of places in parent's place partition,for the 11 machine architecture depicted above.The place partition is not changed by the close policy. C/C++ 12 Example affinity.3c void work(); int main ( #pragma omp parallel proc_bind(close)num_threads(4) work ( return 0; C/C++ Fortran 13 Example affinity.3f 82 LLEL PROC_BIND (CLOSE)NUM_THREADS(4) CALL WORK() ISOMP END PARALLE END PROGRAM EXAMPLE CHAPTER 8.THE PROC_BIND CLAUSE 25
1 • threads 2,3 execute on p3 with the place partition p3 2 • threads 4,5 execute on p4 with the place partition p4 3 • threads 6,7 execute on p5 with the place partition p5 4 • threads 8,9 execute on p6 with the place partition p6 5 • threads 10,11 execute on p7 with the place partition p7 6 • threads 12,13 execute on p0 with the place partition p0 7 • threads 14,15 execute on p1 with the place partition p1 8 8.2 Close Affinity Policy 9 The following example shows the result of the close affinity policy on the partition list when the 10 number of threads is less than or equal to the number of places in parent’s place partition, for the 11 machine architecture depicted above. The place partition is not changed by the close policy. C / C++ 12 Example affinity.3c S-1 void work(); S-2 int main() S-3 { S-4 #pragma omp parallel proc_bind(close) num_threads(4) S-5 { S-6 work(); S-7 } S-8 return 0; S-9 } C / C++ Fortran 13 Example affinity.3f S-1 PROGRAM EXAMPLE S-2 !$OMP PARALLEL PROC_BIND(CLOSE) NUM_THREADS(4) S-3 CALL WORK() S-4 !$OMP END PARALLEL S-5 END PROGRAM EXAMPLE CHAPTER 8. THE PROC_BIND CLAUSE 25
Fortran It is u thread 0 executes on po with the place partition po-p7 g thread 1 executes on pI with the place partition pO-p7 5 .thread 2 executes on p2 with the place partition po-p7 6 thread 3 executes on p3 with the place partition po-p7 7 If the master thread would initially be started on p2,the placement of threads and distribution of the 8 place partition would be as follows: 9 .thread executes on p2 with the place partition po-p7 .thread 1 executes on p3 with the place partition po-p7 11 thread 2 executes on p4 with the place partition p0-p7 .thread 3 executes on p5 with the place partition po-p7 Let T be the number of threads in the team,and P be the number of places in the parent's place partition.The first T/P threads of the team (including the master thread)execute on the parent's place.The next T/P threads execute on the next place in the place partition,and so on,with wrap 18 around.The place partition is not changed by the close policy. C/C++ Example affinity.4c S-1 void work(); S-2 void foo() S-3 S-4 #pragma omp parallel num_threads(16)proc_bind(close) S-5 S-6 work () s-7 S-8 C/C++ Fortran 9 Example affinity.4f S-1 subroutine foo S-2 !Somp parallel num_threads(16)proc_bind(close) S-3 call work() S-4 !Somp end parallel S-5 end subroutine 26 OpenMP Examples Version 4.0.2-March 2015
Fortran 1 It is unspecified on which place the master thread is initially started. If the master thread is initially 2 started on p0, the following placement of threads will be applied in the parallel region: 3 • thread 0 executes on p0 with the place partition p0-p7 4 • thread 1 executes on p1 with the place partition p0-p7 5 • thread 2 executes on p2 with the place partition p0-p7 6 • thread 3 executes on p3 with the place partition p0-p7 7 If the master thread would initially be started on p2, the placement of threads and distribution of the 8 place partition would be as follows: 9 • thread 0 executes on p2 with the place partition p0-p7 10 • thread 1 executes on p3 with the place partition p0-p7 11 • thread 2 executes on p4 with the place partition p0-p7 12 • thread 3 executes on p5 with the place partition p0-p7 13 The following example illustrates the close thread affinity policy when the number of threads is 14 greater than the number of places in the parent’s place partition. 15 Let T be the number of threads in the team, and P be the number of places in the parent’s place 16 partition. The first T/P threads of the team (including the master thread) execute on the parent’s 17 place. The next T/P threads execute on the next place in the place partition, and so on, with wrap 18 around. The place partition is not changed by the close policy. C / C++ 19 Example affinity.4c S-1 void work(); S-2 void foo() S-3 { S-4 #pragma omp parallel num_threads(16) proc_bind(close) S-5 { S-6 work(); S-7 } S-8 } C / C++ Fortran 20 Example affinity.4f S-1 subroutine foo S-2 !$omp parallel num_threads(16) proc_bind(close) S-3 call work() S-4 !$omp end parallel S-5 end subroutine 26 OpenMP Examples Version 4.0.2 - March 2015
Fortran 12 It is unspecified on which place the master thread is initially started.If the master thread is initially running on po,the following placement of threads will be applied in the parallel region: 345 .threads 0.1 execute on po with the place partition po-p7 .threads 23 execute on pl with the place partition po-p7 .threads 4.5 execute on p2 with the place partition po-p7 6 threads 6.7 execute on p3 with the place partition po-p7 .threads 8,9 execute on p4 with the place partition po-p7 89 .threads 10.11 execute on p5 with the place partition po-p7 .threads 12,13 execute on p6 with the place partition po-p7 10 threads 14,15 execute on p7 with the place partition po-p7 2 If the master thread would initially be started on p2,the placement of threads and distribution of the place partition would be as follows: 13 .threads 0.1 execute on p2 with the place partition po-p7 threads 2.3 execute on p3 with the place partition po-p7 15 .threads 4.5 execute on p4 with the place partition po-p7 167 threads 6.7 execute on p5 with the place partition po-p7 .threads 8.9 execute on p6 with the place partition po-p7 threads 10,11 execute on p7 with the place partition po-p7 1 .threads 12.13 execute on po with the place partition po-p7 20 .threads 14.15 execute on pl with the place partition po-p7 21 8.3 Master Affinity Policy The following example shows the result of the master affinity policy on the partition list for the machine architecture depicted above.The place partition is not changed by the master policy. CHAPTER 8.THE PROC_BIND CLAUSE 27
Fortran 1 It is unspecified on which place the master thread is initially started. If the master thread is initially 2 running on p0, the following placement of threads will be applied in the parallel region: 3 • threads 0,1 execute on p0 with the place partition p0-p7 4 • threads 2,3 execute on p1 with the place partition p0-p7 5 • threads 4,5 execute on p2 with the place partition p0-p7 6 • threads 6,7 execute on p3 with the place partition p0-p7 7 • threads 8,9 execute on p4 with the place partition p0-p7 8 • threads 10,11 execute on p5 with the place partition p0-p7 9 • threads 12,13 execute on p6 with the place partition p0-p7 10 • threads 14,15 execute on p7 with the place partition p0-p7 11 If the master thread would initially be started on p2, the placement of threads and distribution of the 12 place partition would be as follows: 13 • threads 0,1 execute on p2 with the place partition p0-p7 14 • threads 2,3 execute on p3 with the place partition p0-p7 15 • threads 4,5 execute on p4 with the place partition p0-p7 16 • threads 6,7 execute on p5 with the place partition p0-p7 17 • threads 8,9 execute on p6 with the place partition p0-p7 18 • threads 10,11 execute on p7 with the place partition p0-p7 19 • threads 12,13 execute on p0 with the place partition p0-p7 20 • threads 14,15 execute on p1 with the place partition p0-p7 21 8.3 Master Affinity Policy 22 The following example shows the result of the master affinity policy on the partition list for the 23 machine architecture depicted above. The place partition is not changed by the master policy. CHAPTER 8. THE PROC_BIND CLAUSE 27
C/C++ Example affinity.5c void work(); int main ( #pragma omp parallel proc_bind(master)num_threads(4) 1 89 work ( return 0; C/C++ Fortran 2 Example affinity.5f PROGRAM EXAMPLE PROC_BIND(MASTER)NUM_THREADS(4) I SOMP END PAR ORK ( END PROGRAM EXAMPLE Fortran It is unspecified on which place the master thread is initially started.If the master thread is initially 4 running on po,the following placement of threads will be applied in the parallel region: threads 0-3 execute on po with the place partition po-p7 67 If the maste thread would initially be started on p2,the placement of threads and distribution of the place partition would bes follows: 8 threads 0-3 execute on p2 with the place partition po-p7 28 OpenMP Examples Version 4.0.2-March 2015
C / C++ 1 Example affinity.5c S-1 void work(); S-2 int main() S-3 { S-4 #pragma omp parallel proc_bind(master) num_threads(4) S-5 { S-6 work(); S-7 } S-8 return 0; S-9 } C / C++ Fortran 2 Example affinity.5f S-1 PROGRAM EXAMPLE S-2 !$OMP PARALLEL PROC_BIND(MASTER) NUM_THREADS(4) S-3 CALL WORK() S-4 !$OMP END PARALLEL S-5 END PROGRAM EXAMPLE Fortran 3 It is unspecified on which place the master thread is initially started. If the master thread is initially 4 running on p0, the following placement of threads will be applied in the parallel region: 5 • threads 0-3 execute on p0 with the place partition p0-p7 6 If the master thread would initially be started on p2, the placement of threads and distribution of the 7 place partition would be as follows: 8 • threads 0-3 execute on p2 with the place partition p0-p7 28 OpenMP Examples Version 4.0.2 - March 2015
1 CHAPTER 9 Fortran Restrictions on the do 3 Construct Fortran 456 If ano directive followsadn which several Do statements shareaDo termination 7 Example fort_do.If s-1 SUBROUTINE WORK(I,J) INTEGER I,J END SUBROUTINE WORK 88 SUBROUTINE DO_GOOD() INTEGER I J REAL A(1000) S-g D01001=1,10 s-10 SOMP DO s-11 D0100J=1,10 S-12 CALL WORK (I,J) S-13 100 CONTINUE !SOMP ENDDO implied here S-14 S-15 !SOMP S-16 D0200J=1,10 S-17 200 S-18 SOMP 8工+1 动的 SOMP =1,10
1 CHAPTER 9 2 Fortran Restrictions on the do 3 Construct Fortran 4 If an end do directive follows a do-construct in which several DO statements share a DO 5 termination statement, then a do directive can only be specified for the outermost of these DO 6 statements. The following example contains correct usages of loop constructs: 7 Example fort_do.1f S-1 SUBROUTINE WORK(I, J) S-2 INTEGER I,J S-3 END SUBROUTINE WORK S-4 S-5 SUBROUTINE DO_GOOD() S-6 INTEGER I, J S-7 REAL A(1000) S-8 S-9 DO 100 I = 1,10 S-10 !$OMP DO S-11 DO 100 J = 1,10 S-12 CALL WORK(I,J) S-13 100 CONTINUE ! !$OMP ENDDO implied here S-14 S-15 !$OMP DO S-16 DO 200 J = 1,10 S-17 200 A(I) = I + 1 S-18 !$OMP ENDDO S-19 S-20 !$OMP DO S-21 DO 300 I = 1,10 S-22 DO 300 J = 1,10 S-23 CALL WORK(I,J) 29