Difference between revisions of "Programming/OpenMP"
From HPC
m |
m |
||
Line 125: | Line 125: | ||
* module add gcc/4.9.3 (GNU compiler) | * module add gcc/4.9.3 (GNU compiler) | ||
* module add intel/compiler/64/2016.2.181 (Intel compiler) | * module add intel/compiler/64/2016.2.181 (Intel compiler) | ||
− | + | {| | |
− | + | |style="width:5%; border-width: 0;cellpadding=0" | [[File:icon_exclam3.png]] | |
− | + | |style="width:95%; border-width: 0;cellpadding=0" | OpenMP is a library directive within the compiler and does not require any additional module to be loaded. | |
− | + | |- | |
− | + | |} | |
== Usage Examples == | == Usage Examples == | ||
Revision as of 16:02, 3 March 2017
Contents
Programming Details
OpenMP is designed for multi-processor/core, shared memory machines. The underlying architecture can be shared memory UMA or NUMA.
It is an Application Program Interface (API) that may be used to explicitly direct multi-threaded, shared memory parallelism. Comprised of three primary API components:
- Compiler Directives
- Runtime Library Routines
- Environment Variables
OpenMP compiler directives are used for various purposes:
- Spawning a parallel region
- Dividing blocks of code among threads
- Distributing loop iterations between threads
- Serializing sections of code
- Synchronization of work among threads
Programming Example
C Example
#include <stdio.h> #include <stdlib.h> #include <malloc.h> /* compile with gcc -o test2 -fopenmp test2.c */ int main(int argc, char** argv) { int i = 0; int size = 20; int* a = (int*) calloc(size, sizeof(int)); int* b = (int*) calloc(size, sizeof(int)); int* c; for ( i = 0; i < size; i++ ) { a[i] = i; b[i] = size-i; printf("[BEFORE] At %d: a=%d, b=%d\n", i, a[i], b[i]); } #pragma omp parallel shared(a,b) private(c,i) { c = (int*) calloc(3, sizeof(int)); #pragma omp for for ( i = 0; i < size; i++ ) { c[0] = 5*a[i]; c[1] = 2*b[i]; c[2] = -2*i; a[i] = c[0]+c[1]+c[2]; c[0] = 4*a[i]; c[1] = -1*b[i]; c[2] = i; b[i] = c[0]+c[1]+c[2]; } free(c); } for ( i = 0; i < size; i++ ) { printf("[AFTER] At %d: a=%d, b=%d\n", i, a[i], b[i]); } }
Fortran Example
program omp_par_do implicit none integer, parameter :: n = 100 real, dimension(n) :: dat, result integer :: i !$OMP PARALLEL DO do i = 1, n result(i) = my_function(dat(i)) end do !$OMP END PARALLEL DO contains function my_function(d) result(y) real, intent(in) :: d real :: y ! do something complex with data to calculate y end function my_function end program omp_par_do
Compilation
The program would be compiled in the following way, optional Intel compiler available too:
For C [username@login01 ~]$ module add gcc/4.9.3 [username@login01 ~]$ gcc -o test2 -fopenmp test2.c For Fortran [username@login01 ~]$ module add gcc/4.9.3 [username@login01 ~]$ gfortran -o test2 -fopenmp test2.c
Modules Available
The following modules are available for OpenMP:
- module add gcc/4.9.3 (GNU compiler)
- module add intel/compiler/64/2016.2.181 (Intel compiler)
OpenMP is a library directive within the compiler and does not require any additional module to be loaded. |
Usage Examples
Batch example
#!/bin/bash #SBATCH -J openmpi-single-node #SBATCH -N 1 #SBATCH --ntasks-per-node 28 #SBATCH -o %N.%j.%a.out #SBATCH -e %N.%j.%a.err #SBATCH -p compute #SBATCH --exclusive echo $SLURM_JOB_NODELIST module purge module add gcc/4.9.3 export I_MPI_DEBUG=5 export I_MPI_FABRICS=shm:tmi export I_MPI_FALLBACK=no /home/user/CODE_SAMPLES/OPENMP/demo
[username@login01 ~]$ sbatch demo.job Submitted batch job 289552
Further Information
Specific
- https://en.wikipedia.org/wiki/OpenMP
- http://www.openmp.org/
- https://computing.llnl.gov/tutorials/openMP/
- http://fortranwiki.org/fortran/show/OpenMP
- 'Youtube - Intel Training videos'