59.3 omp_set_default_device and omp_get_default_device Routines....·.·.·.,·· 246 60 Fortran ASSocIATE Construct 248 A Document Revision History 250 A.1 Changes from 4.0.1 to 4.0.2 ,。。。·,。,,,。··。÷··。·。。。。。·… 250 A.2 Changes from 4.0 to 4.0.1 250 A.3 Changes from 3.1 to 4.0. 。。。。。。 250 Contents
59.3 omp_set_default_device and omp_get_default_device Routines . . . . . . . . . . . . . . . . . . . . . . 246 60 Fortran ASSOCIATE Construct 248 A Document Revision History 250 A.1 Changes from 4.0.1 to 4.0.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 A.2 Changes from 4.0 to 4.0.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 A.3 Changes from 3.1 to 4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Contents v
Introduction 2 This collection of pr 34 mming examples supplments the OpenMPPIor Shad with theopeMincaotheoa conventions used in that document 56 Note-This first release of the OpenMP Examples reflects the OpenMP Version 4.0 specifications. Additional examples are being developed and will be published in future releases of this document. The OpenMP API specification provides a model for parallel programming that is portable across 8 shared memory architectures from different vendors.Compilers from numerous vendors support the OpenMP API. 101 The directives,library routines.and environment variables demonstrated in this document allow users to create and manage parallel programs while permitting portability.The directives extend the 12 C,C++and Fortran base languages with single program multiple data(SPMD)constructs,tasking 13 constructs,device constructs,worksharing constructs,and synchronization constructs,and they 14 provide support for sharing and privatizing data.The functionality to control the runtime 15 167 18 The latest source codes for OpenMP Examples can be downloaded from the sources directory at 9 https://github.com/OpenMP/Examples.The codes for this OpenMP 4.0.2 Examples document have 20 the tag v4.0.2. 222 tion bout the OpenMP API anda list of the comilers that suppor the OpenMP 23 http://www.openmp.org 1
1 Introduction 2 This collection of programming examples supplements the OpenMP API for Shared Memory 3 Parallelization specifications, and is not part of the formal specifications. It assumes familiarity 4 with the OpenMP specifications, and shares the typographical conventions used in that document. 5 Note – This first release of the OpenMP Examples reflects the OpenMP Version 4.0 specifications. 6 Additional examples are being developed and will be published in future releases of this document. 7 The OpenMP API specification provides a model for parallel programming that is portable across 8 shared memory architectures from different vendors. Compilers from numerous vendors support 9 the OpenMP API. 10 The directives, library routines, and environment variables demonstrated in this document allow 11 users to create and manage parallel programs while permitting portability. The directives extend the 12 C, C++ and Fortran base languages with single program multiple data (SPMD) constructs, tasking 13 constructs, device constructs, worksharing constructs, and synchronization constructs, and they 14 provide support for sharing and privatizing data. The functionality to control the runtime 15 environment is provided by library routines and environment variables. Compilers that support the 16 OpenMP API often include a command line option to the compiler that activates and allows 17 interpretation of all OpenMP directives. 18 The latest source codes for OpenMP Examples can be downloaded from the sources directory at 19 https://github.com/OpenMP/Examples. The codes for this OpenMP 4.0.2 Examples document have 20 the tag v4.0.2. 21 Complete information about the OpenMP API and a list of the compilers that support the OpenMP 22 API can be found at the OpenMP.org web site 23 http://www.openmp.org 1
Examples 2 The following are examples of the OpenMP API directives,constructs,and routines. A C/C++ A statement following a directive is compound only when necessary,and a non-compound statement is indented with respect to a directive preceding it. C/C++
1 Examples 2 The following are examples of the OpenMP API directives, constructs, and routines. C / C++ 3 A statement following a directive is compound only when necessary, and a non-compound 4 statement is indented with respect to a directive preceding it. C / C++ 2
1 CHAPTER1 2 A Simple Parallel Loop The following example demonstrates how to parallelize a simple loop using the parallel loop g construct.The loop iteration variable is private by default,so it is not necessary to specify it explicitly in a private clause. C/C++ 6 Example ploop.Ic void simple(int n,float ta,float +b) int i; ttra-P20 ate by defa电 1 C/C++ Fortran Example ploop.If 5 SUBROUTINE SIMPLE(N,A,B) -3 INTEGER I,N REAL B(N),A(N) s-5 S-6 !SOMP PARALLEL DO !I is private by default S-7 DO I=2,N S-8 B(I)=(a(I)+A(I-1)/2.0 S-9 ENDDO S-10 !SOMP END PARALLEL DO S-11 S-12 END SUBROUTINE SIMPLE Fortran 3
1 CHAPTER 1 2 A Simple Parallel Loop 3 The following example demonstrates how to parallelize a simple loop using the parallel loop 4 construct. The loop iteration variable is private by default, so it is not necessary to specify it 5 explicitly in a private clause. C / C++ 6 Example ploop.1c S-1 void simple(int n, float *a, float *b) S-2 { S-3 int i; S-4 S-5 #pragma omp parallel for S-6 for (i=1; i<n; i++) /* i is private by default */ S-7 b[i] = (a[i] + a[i-1]) / 2.0; S-8 } C / C++ Fortran 7 Example ploop.1f S-1 SUBROUTINE SIMPLE(N, A, B) S-2 S-3 INTEGER I, N S-4 REAL B(N), A(N) S-5 S-6 !$OMP PARALLEL DO !I is private by default S-7 DO I=2,N S-8 B(I) = (A(I) + A(I-1)) / 2.0 S-9 ENDDO S-10 !$OMP END PARALLEL DO S-11 S-12 END SUBROUTINE SIMPLE Fortran 3
1 CHAPTER2 The OpenMP Memory Model In the following example,at Print 1,the value of x could be either 2 or 5,depending on the timing g of the threads,and the implementation of the assignment to x.There are two reasons that the value at Print I might not be 5.First,Print I might be executed before the assignment to x is executed. 67 ment.the value5 is not guaranteed to be seen by 8 The barrier after Print I contains implicit flushes on all threads,as well as a thread synchronization. so the programmer is guaranteed that the value 5 will be printed by both Print 2 and Print 3. C/C++ 10 Example mem_model.Io #include <stdio.h> #include <omp.h> int main()( int x; x=2; pragma omp parallel num_threads(2)shared(x) if (omp- -get_thread_num()=-0)( 85 fol ng read Thread#sd:x 819 S-18 s-19 #pragma omp barrier s-20 if (omp_get_thread_num()==0){
1 CHAPTER 2 2 The OpenMP Memory Model 3 In the following example, at Print 1, the value of x could be either 2 or 5, depending on the timing 4 of the threads, and the implementation of the assignment to x. There are two reasons that the value 5 at Print 1 might not be 5. First, Print 1 might be executed before the assignment to x is executed. 6 Second, even if Print 1 is executed after the assignment, the value 5 is not guaranteed to be seen by 7 thread 1 because a flush may not have been executed by thread 0 since the assignment. 8 The barrier after Print 1 contains implicit flushes on all threads, as well as a thread synchronization, 9 so the programmer is guaranteed that the value 5 will be printed by both Print 2 and Print 3. C / C++ 10 Example mem_model.1c S-1 #include <stdio.h> S-2 #include <omp.h> S-3 S-4 int main(){ S-5 int x; S-6 S-7 x = 2; S-8 #pragma omp parallel num_threads(2) shared(x) S-9 { S-10 S-11 if (omp_get_thread_num() == 0) { S-12 x = 5; S-13 } else { S-14 /* Print 1: the following read of x has a race */ S-15 printf("1: Thread# %d: x = %d\n", omp_get_thread_num(),x ); S-16 } S-17 S-18 #pragma omp barrier S-19 S-20 if (omp_get_thread_num() == 0) { 4