OpenMP (The GNU Fortran Compiler) (original) (raw)
5.1.18 OpenMP ¶
OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming in C/C++ and Fortran on many architectures, including Unix and Microsoft Windows platforms. It consists of a set of compiler directives, library routines, and environment variables that influence run-time behavior.
GNU Fortran implements all of the OpenMP Application Program Interface v4.5, and many features from later versions of the OpenMP specification. See OpenMP Implementation Status in GNU Offloading and Multi Processing Runtime Library, for more details about currently supported OpenMP features.
To enable the processing of the OpenMP directive !$omp
in free-form source code; the c$omp
, *$omp
and !$omp
directives in fixed form; the !$
conditional compilation sentinels in free form; and the c$
, *$
and !$
sentinels in fixed form, gfortran
needs to be invoked with the-fopenmp option. This option also arranges for automatic linking of the OpenMP runtime library. See GNU Offloading and Multi Processing Runtime Library.
The OpenMP Fortran runtime library routines are provided both in a form of a Fortran 90 module named omp_lib
and in a form of a Fortran include
file named omp_lib.h.
An example of a parallelized loop taken from Appendix A.1 of the OpenMP Application Program Interface v2.5:
SUBROUTINE A1(N, A, B) INTEGER I, N REAL B(N), A(N) !$OMP PARALLEL DO !I is private by default DO I=2,N B(I) = (A(I) + A(I-1)) / 2.0 ENDDO !$OMP END PARALLEL DO END SUBROUTINE A1
See OpenMP and OpenACC Options in Using the GNU Compiler Collection (GCC), for additional options useful with-fopenmp.
Please note:
- -fopenmp implies -frecursive, i.e., all local arrays are allocated on the stack. When porting existing code to OpenMP, this may lead to surprising results, especially segmentation faults if the stack size is limited.
- On glibc-based systems, OpenMP-enabled applications cannot be statically linked due to limitations of the underlying pthreads implementation. It might be possible to get a working solution if
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive
is added to the command line. However, this is not supported by GCC and thus not recommended.