1dnl @synopsis ACX_MPI([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2dnl
3dnl This macro tries to find out how to compile programs that
4dnl use MPI (Message Passing Interface), a standard API for
5dnl parallel process communication (see http://www-unix.mcs.anl.gov/mpi/)
6dnl
7dnl On success, it sets the MPICC, MPICXX, or MPIF77 output variable to
8dnl the name of the MPI compiler, depending upon the current language.
9dnl (This may just be $CC/$CXX/$F77, but is more often something like
10dnl mpicc/mpiCC/mpif77.)  It also sets MPILIBS to any libraries that are
11dnl needed for linking MPI (e.g. -lmpi, if a special MPICC/MPICXX/MPIF77
12dnl was not found).
13dnl
14dnl If you want to compile everything with MPI, you should set:
15dnl
16dnl     CC="$MPICC" #OR# CXX="$MPICXX" #OR# F77="$MPIF77"
17dnl     LIBS="$MPILIBS $LIBS"
18dnl
19dnl The user can force a particular library/compiler by setting the
20dnl MPICC/MPICXX/MPIF77 and/or MPILIBS environment variables.
21dnl
22dnl ACTION-IF-FOUND is a list of shell commands to run if an MPI
23dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
24dnl to run it if it is not found.  If ACTION-IF-FOUND is not specified,
25dnl the default action will define HAVE_MPI.
26dnl
27dnl @version $Id: acx_mpi.m4,v 1.2 2002/10/24 05:11:22 stevenj Exp $
28dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
29
30AC_DEFUN([ACX_MPI], [
31AC_PREREQ(2.50) dnl for AC_LANG_CASE
32
33AC_LANG_CASE([C], [
34	AC_REQUIRE([AC_PROG_CC])
35	AC_CHECK_PROGS(MPICC, mpicc hcc mpcc mpcc_r mpxlc, $CC)
36	acx_mpi_save_CC="$CC"
37	CC="$MPICC"
38	AC_SUBST(MPICC)
39],
40[C++], [
41	AC_REQUIRE([AC_PROG_CXX])
42	AC_CHECK_PROGS(MPICXX, mpiCC mpCC, $CXX)
43	acx_mpi_save_CXX="$CXX"
44	CXX="$MPICXX"
45	AC_SUBST(MPICXX)
46],
47[Fortran 77], [
48	AC_REQUIRE([AC_PROG_F77])
49	AC_CHECK_PROGS(MPIF77, mpif77 hf77 mpxlf mpf77 mpif90 mpf90 mpxlf90 mpxlf95 mpxlf_r, $F77)
50	acx_mpi_save_F77="$F77"
51	F77="$MPIF77"
52	AC_SUBST(MPIF77)
53])
54
55if test x = x"$MPILIBS"; then
56	AC_LANG_CASE([C], [AC_CHECK_FUNC(MPI_Init, [MPILIBS=" "])],
57		[C++], [AC_CHECK_FUNC(MPI_Init, [MPILIBS=" "])],
58		[Fortran 77], [AC_MSG_CHECKING([for MPI_Init])
59			AC_TRY_LINK([],[      call MPI_Init], [MPILIBS=" "
60				AC_MSG_RESULT(yes)], [AC_MSG_RESULT(no)])])
61fi
62if test x = x"$MPILIBS"; then
63	AC_CHECK_LIB(mpi, MPI_Init, [MPILIBS="-lmpi"])
64fi
65if test x = x"$MPILIBS"; then
66	AC_CHECK_LIB(mpich, MPI_Init, [MPILIBS="-lmpich"])
67fi
68
69dnl We have to use AC_TRY_COMPILE and not AC_CHECK_HEADER because the
70dnl latter uses $CPP, not $CC (which may be mpicc).
71AC_LANG_CASE([C], [if test x != x"$MPILIBS"; then
72	AC_MSG_CHECKING([for mpi.h])
73	AC_TRY_COMPILE([#include <mpi.h>],[],[AC_MSG_RESULT(yes)], [MPILIBS=""
74		AC_MSG_RESULT(no)])
75fi],
76[C++], [if test x != x"$MPILIBS"; then
77	AC_MSG_CHECKING([for mpi.h])
78	AC_TRY_COMPILE([#include <mpi.h>],[],[AC_MSG_RESULT(yes)], [MPILIBS=""
79		AC_MSG_RESULT(no)])
80fi])
81
82AC_LANG_CASE([C], [CC="$acx_mpi_save_CC"],
83	[C++], [CXX="$acx_mpi_save_CXX"],
84	[Fortran 77], [F77="$acx_mpi_save_F77"])
85
86AC_SUBST(MPILIBS)
87
88# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
89if test x = x"$MPILIBS"; then
90        $2
91        :
92else
93        ifelse([$1],,[AC_DEFINE(HAVE_MPI,1,[Define if you have the MPI library.])],[$1])
94        :
95fi
96])dnl ACX_MPI
97