New module 'openmp'.
[gnulib.git] / m4 / openmp.m4
1 # openmp.m4 serial 4
2 dnl Copyright (C) 2006-2007 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl This file can be removed once we assume autoconf >= 2.62.
8
9 # _AC_LANG_OPENMP
10 # ---------------
11 # Expands to some language dependent source code for testing the presence of
12 # OpenMP.
13 AC_DEFUN([_AC_LANG_OPENMP],
14 [_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])
15
16 # _AC_LANG_OPENMP(C)
17 # ------------------
18 m4_define([_AC_LANG_OPENMP(C)],
19 [
20 #ifndef _OPENMP
21  choke me
22 #endif
23 #include <omp.h>
24 int main () { return omp_get_num_threads (); }
25 ])
26
27 # _AC_LANG_OPENMP(C++)
28 # --------------------
29 m4_copy([_AC_LANG_OPENMP(C)], [_AC_LANG_OPENMP(C++)])
30
31 # _AC_LANG_OPENMP(Fortran 77)
32 # ---------------------------
33 m4_define([_AC_LANG_OPENMP(Fortran 77)],
34 [AC_LANG_FUNC_LINK_TRY([omp_get_num_threads])])
35
36 # _AC_LANG_OPENMP(Fortran)
37 # ---------------------------
38 m4_copy([_AC_LANG_OPENMP(Fortran 77)], [_AC_LANG_OPENMP(Fortran)])
39
40 # AC_OPENMP
41 # ---------
42 # Check which options need to be passed to the C compiler to support OpenMP.
43 # Set the OPENMP_CFLAGS / OPENMP_CXXFLAGS / OPENMP_FFLAGS variable to these
44 # options.
45 # The options are necessary at compile time (so the #pragmas are understood)
46 # and at link time (so the appropriate library is linked with).
47 # This macro takes care to not produce redundant options if $CC $CFLAGS already
48 # supports OpenMP. It also is careful to not pass options to compilers that
49 # misinterpret them; for example, most compilers accept "-openmp" and create
50 # an output file called 'penmp' rather than activating OpenMP support.
51 AC_DEFUN([AC_OPENMP],
52 [
53   OPENMP_[]_AC_LANG_PREFIX[]FLAGS=
54   AC_ARG_ENABLE([openmp],
55     [AS_HELP_STRING([--disable-openmp], [do not use OpenMP])])
56   if test "$enable_openmp" != no; then
57     AC_CACHE_CHECK([for $CC option to support OpenMP],
58       [ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp],
59       [AC_LINK_IFELSE([_AC_LANG_OPENMP],
60          [ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp='none needed'],
61          [ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp='unsupported'
62           dnl Try these flags:
63           dnl   GCC >= 4.2           -fopenmp
64           dnl   SunPRO C             -xopenmp
65           dnl   Intel C              -openmp
66           dnl   SGI C, PGI C         -mp
67           dnl   Tru64 Compaq C       -omp
68           dnl   IBM C (AIX, Linux)   -qsmp=omp
69           dnl If in this loop a compiler is passed an option that it doesn't
70           dnl understand or that it misinterprets, the AC_LINK_IFELSE test
71           dnl will fail (since we know that it failed without the option),
72           dnl therefore the loop will continue searching for an option, and
73           dnl no output file called 'penmp' or 'mp' is created.
74           for ac_option in -fopenmp -xopenmp -openmp -mp -omp -qsmp=omp; do
75             ac_save_[]_AC_LANG_PREFIX[]FLAGS=$[]_AC_LANG_PREFIX[]FLAGS
76             _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $ac_option"
77             AC_LINK_IFELSE([_AC_LANG_OPENMP],
78               [ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp=$ac_option])
79             _AC_LANG_PREFIX[]FLAGS=$ac_save_[]_AC_LANG_PREFIX[]FLAGS
80             if test "$ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp" != unsupported; then
81               break
82             fi
83           done])])
84     case $ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp in #(
85       "none needed" | unsupported)
86         ;; #(
87       *)
88         OPENMP_[]_AC_LANG_PREFIX[]FLAGS=$ac_cv_prog_[]_AC_LANG_ABBREV[]_openmp ;;
89     esac
90   fi
91   AC_SUBST([OPENMP_]_AC_LANG_PREFIX[FLAGS])
92 ])