(A) Definition of OpenMP
OpenMP stands for Open Multi Processing and is a standard Application Programming Interface (API) for distributing program tasks across threads of a shared memory computer.
The OpenMP supports multi-platform shared memory multiprocessing architecture using programming languages like C/C++ and Fortran, including Linux/Unix and Microsoft Windows platforms. It consists of a set of compiler directives, library routines, and run-time environment variables.
The OpenMP standards are jointly defined by a group of major computer hardware and software vendors so as to give shared-memory parallel programmers a simple and flexible interface for developing parallel applications.
The advantages of using OpenMP are simple, incremental parallelism and unified code for both serial and parallel applications.
The disadvantages of using OpenMP are currently only running efficiently in shared-memory multiprocessor platforms, requiring a compiler that supports OpenMP, and low parallel efficiency.
The performance expectation of OpenMP is that one may expect to get N times less wall clock execution time (or N times speedup) when running a program parallelized using OpenMP on a N processor platform.
As a reminder, OpenMP can often be used to improve performance on symmetric multi-processor (SMP) machines by simply adding a few compiler directives to the program code.
______________________________________________________________________
(B) History of gfortran with OpenMP
1. Objective
gfortran attempts to be OpenMP Application Program Interface v2.5 compatible when invoked with the -fopenmp option. gfortran then generates parallellized code according to the OpenMP directives used in the source. The OpenMP Fortran runtime library routines are provided both in a form of Fortran 90 module named omp_lib and in a form of a Fortran include file named omp_lib.h.
The GOMP project was GCC's OpenMP implementation project. The code was merged into mainline to become part of GCC 4.2.
The GOMP release will include a support library, libgomp, and extensions to target language parsers. The initial focus is on implementing the basic syntax of GOMP in the C, C++, and Fortran 95 frontends, to be followed by specific implementations for different platforms. A long-term goal is the generation of efficient and small code for OpenMP applications.
2. History
March 9, 2006 Richard Henderson, Jakub Jelinek and Diego Novillo of Red Hat Inc, and Dmitry Kurochkin have contributed an implementation of the OpenMP v2.5 parallel programming interface for C, C++ and Fortran.
February 14, 2006 Jakub Jelinek committed the front end support for OpenMP.
November 21, 2006 Jakub Jelinek updated libgfortran to a thread safe library in preparation for OpenMP.
November 18, 2005 The branch is ready to be merged into mainline. All three front ends are functional and there should not be many corners of the standard left to implement. There are 5 main modules to merge into mainline: (1) runtime library, (2) code generation, (3) C front end, (4) C++ front end, and, (5) Fortran front end.
October 20, 2005 The runtime library is functionally complete. The syntax parsers for C, C++ and Fortran are complete, though there are still dusty corners with respect to semantic translation to be resolved. Adventurous users who don't mind the compiler crashing on every other source file are encouraged to begin filing bugs.
3. Requirements of OpenMP in gfotran (v2.0)
The foremost goal is correctness; programs compiled with OpenMP must operate as expected according to programming language and OpenMP standards.
For Operation with compiler like gfortran, it is expected to follow the requirements below.
- OpenMP directives will act as comments (F95) or unimplemented pragmas (C/C++) unless the -fopenmp option is specified on the compiler command line.
- When -fopenmp is used, the compiler will generate parallel code based on the OpenMP directives encountered.
- By default, GOMP will use the threading model specified by the �óenable-thread=xxx configuration option.
- When -fopenmp=stubs is used, the compiler will generate OpenMP code that links with a minimal stub library.
- The design will consider, but might not implement, link-time selection of a threading model, via an -fopenmp=model syntax.
- By default, the compiler will define the option -fno-openmp.
- If -fopenmp is not specified, the compiler will generate a serial program that operates as if the OpenMP directives were non-existent.
- Linking to the OpenMP support library will be automatic if -fopenmp is specified.
4. Future of OpenMP in gfotran
Straight line code vectorization and Automatic parallelization are undergoing development. Please refer to the presentation "OpenMP and Vectorization in GCC" in October, 2006.
______________________________________________________________________
(C) Compiling and running sample gfortran OpenMP programs (to be verified by contributor soon)
1. Fortran sample program
The program file hello.f contains the following program statements:
PROGRAM HELLO
USE omp_lib
IMPLICIT NONE
INTEGER nthreads, tid, OMP_GET_NUM_THREADS
INTEGER OMP_GET_THREAD_NUM
! Fork a team of threads
!$OMP PARALLEL PRIVATE(nthreads, tid)
! Obtain and print thread id
tid = OMP_GET_THREAD_NUM()
print *, 'Hello World from thread = ', tid
! Only master thread does this
IF (tid .EQ. 0) THEN
nthreads = OMP_GET_NUM_THREADS()
print *, 'Number of threads = ', nthreads
END IF
! All threads join master thread and disband
!$OMP END PARALLEL
END PROGRAM
2. Compile the gfortran OpenMP program
Type the following command:
gfortran hello.f -o hello.exe -fopenmp
or
gfortran hello.f -o hello.exe -fopenmp -static -lgomp -lrt
Only -fopenmp -static -lgomp -lrt should be enough for static linking, though even that ought to be unnecessary, but due to a bug the compiler inserts -lgomp -lrt for static link to early on the command line, will fix.
3. Run the gfortran OpenMP program
Type the following command:
setenv OMP_NUM_THREADS 2
hello.exe
[Note : Sometimes you may need to use "EXPORT OMP_NUM_THREADS=2" instead. If you are using the Windows platform, please use "set" (ie. set OMP_NUM_THREADS=2) instead of "setenv".]
The expected result or output :
Hello World from thread = 0
Number of threads = 2
Hello World from thread = 1
4. Download the gfortran OpenMP under Windows platform from this web site
If you have any problems about setting up the Windows environment, please refer to GCC Wiki gfortran ______________________________________________________________________
(D) How to avoid the common mistakes in writing OpenMP programs
The common mistakes are mainly Race Conditions and Deadlocks. It is good to refer this paper titled Common Mistakes in OpenMP and How To Avoid Them -- a Collection of Best Practices if you are a novice programmer in writing OpenMP programs.
______________________________________________________________________
(E) Runtime library status with OpenMP features
The Input/Output library is not thread-safe. This will have to be fixed because OpenMP allows threaded IO.
______________________________________________________________________
Contributor : Henry Kar Ming Chan, High Performance Computing Specialist in Scientific Computation.
All comments are welcome. Please feel free to contact the contributor at karminghenry@sinaman.com
没有评论:
发表评论