Skip to content

Commit 8f3959d

Browse files
authored
Merge pull request certik#42 from cphyc/omp_mpi_recommendations
add mpi and omp best practices
2 parents 6b58521 + 78c75b8 commit 8f3959d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/best-practices.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,3 +1212,53 @@ The ``transfer()`` method is here for completeness only (before Fortran 2003,
12121212
it was the only way) and it is a little cumbersome, because the user needs to
12131213
create auxiliary conversion functions for each of his types.
12141214
As such, the ``type(c_ptr)`` method should be used instead.
1215+
1216+
.. _parallel:
1217+
1218+
Parallel programming
1219+
--------------------
1220+
1221+
OpenMP
1222+
~~~~~~
1223+
1224+
`OpenMP <http://www.openmp.org/>`_ should be compatible with
1225+
non-openMP compilers. This can be enforced by prepending all
1226+
OpenMP-specific calls by ``!\$``. Regular compilers will consider these
1227+
lines as comments and ignore them. For OpenMP compilers, these lines
1228+
will be considered as regular Fortran code. The following code ::
1229+
1230+
program test_openmpi
1231+
!\$ use omp_lib
1232+
implicit none
1233+
1234+
integer :: nthreads
1235+
1236+
nthreads = -1
1237+
!\$ nthreads = omp_get_num_threads()
1238+
1239+
! will print the number of running threads when compiled with OpenMP, else will print -1
1240+
print*, "nthreads=", nthreads
1241+
end program
1242+
1243+
will print the number of threads used when compiled with OpenMP. It will print by default -1 if compiled without OpenMP.
1244+
1245+
MPI
1246+
~~~
1247+
1248+
There are three ways of including MPI in a fortran program:
1249+
1250+
=============== ==================== ====================================================================
1251+
Fortran version Method Comments
1252+
=============== ==================== ====================================================================
1253+
Fortran 08 ``use mpi_f08`` Consistent with F08 standard, good type-checking; highly recommended
1254+
Fortran 90 ``use mpi`` Not consistent with standard, so-so type-checking; not recommended
1255+
Fortran 77 ``include "mpif.h"`` Not consistent with standard, no type-checking; strongly discouraged
1256+
=============== ==================== ====================================================================
1257+
1258+
On infrastructures where ``use mpi_f08`` is not available, one should
1259+
fallback to ``use mpi``. The use of ``include "mpif.h"`` is strongly
1260+
discouraged, as it does not check at all the types of the argument or
1261+
that the function calls provide the good arguments. For example, you
1262+
don’t get any compiler warnings if you call a subroutine and forget a
1263+
parameter, add an extra parameter, or pass a parameter of the wrong
1264+
type. It may also lead to silent data corruption.

0 commit comments

Comments
 (0)