1AC_DEFUN([MPI_CONFIG_HELPER],[
2
3mpi_trybuild () {
4  $MPICC ${1} > /dev/null 2>&1 || return 1
5  return 0
6}
7
8mpi_trybuild_run () {
9  mpi_trybuild "-o conftest ${1}" || return 1
10  ./conftest 2>&1 || return 1
11  return 0
12}
13
14mpi_preprocess () {
15  $MPICC -E -c ${1} 2> /dev/null
16}
17
18mpi_getflags () {
19  # -- call mpiCC, remove compiler name
20  # compiler-name is first word in line _if_ it doesn't start with a dash!
21  # needed because mpiCC sometimes does not include compiler (newer LAM)
22
23  # the additional brackets keep m4 from interpreting the brackets
24  # in the sed-command...
25  retval=`$MPICC ${1} ${2} 2>/dev/null | head -1`
26  # remove compiler name
27  retval=`echo $retval | sed -e 's/^[[^-]][[^ ]][[^ ]]* //'`
28  # remove dummy-parameter (if existing)
29  retval=`echo $retval | sed -e "s/ ${1} / /"`
30  if test ${#} = 2 ; then
31    retval=`echo $retval | sed -e "s/ ${2} / /"`
32  fi
33  # remove optimization, warning, etc paramters
34  retval=`echo " $retval " | sed -e 's/ -g / /g' -e 's/ -W[[a-z0-9]]\+ / /g' -e 's/ -O[[0-9]]\+ / /g'`
35  # strip leading and trailing spaces
36  retval=`echo "$retval" | sed 's/^ *//g;s/ *$//g'`
37}
38
39# removes regexp $2 from string $1
40mpi_remove () {
41  retval=`echo ${1} | sed -e "s/ ${2} / /"`
42  # string to remove might be on the beginning of the line
43  retval=`echo ${retval} | sed -e "s/^${2} / /"`
44}
45
46test_lam () {
47  AC_MSG_CHECKING([for lam])
48  cat >conftest.c <<_EOF
49#include <mpi.h>
50#include <stdio.h>
51
52int main() {
53  printf ("%i%i\n", LAM_MAJOR_VERSION, LAM_MINOR_VERSION);
54  return 0;
55 }
56_EOF
57
58  if mpi_trybuild "-c conftest.c"; then
59    # try new -showme:xxx function
60    mpi_getflags "-showme:compile"
61    if test x"$retval" != x ; then
62      # seems like LAM >= 7.1 which supports extraction of parameters without
63      # dummy files
64      dune_MPI_VERSION="LAM >= 7.1"
65      if test x"$DUNEMPICPPFLAGS" = x; then
66        DUNEMPICPPFLAGS="$retval"
67      fi
68      if test x"$DUNEMPILIBS" = x; then
69        mpi_getflags "-showme:link"
70        DUNEMPILIBS="$retval"
71      fi
72    else
73      dune_MPI_VERSION="LAM < 7.1"
74      # use -showme and dummy parameters to extract flags
75      if test x"$DUNEMPICPPFLAGS" = x; then
76        mpi_getflags "-showme" "-c $MPISOURCE"
77        DUNEMPICPPFLAGS="$retval"
78      fi
79      if test x"$DUNEMPILIBS" = x; then
80        mpi_getflags "-showme" "dummy.o -o dummy"
81        DUNEMPILIBS="$retval"
82      fi
83    fi
84    # hack in option to disable LAM-C++-bindings...
85    # we fake to have mpicxx.h read already
86    MPI_NOCXXFLAGS="-DMPIPP_H"
87    AC_MSG_RESULT([yes])
88    rm -f conftest*
89    return 0
90  fi
91
92  rm -f conftest*
93  AC_MSG_RESULT([no])
94  return 1
95}
96
97mpi_getmpichflags() {
98  if test x"$DUNEMPICPPFLAGS" = x; then
99    # use special commands to extract options
100    mpi_getflags "-compile_info"
101    DUNEMPICPPFLAGS="$retval"
102    # remove implicitly set -c
103    mpi_remove "$DUNEMPICPPFLAGS" '-c'
104    DUNEMPICPPFLAGS="$retval"
105  fi
106
107  if test x"$DUNEMPILIBS" = x; then
108    # get linker options
109    mpi_getflags "-link_info"
110    DUNEMPILIBS="$retval"
111    # strip -o option
112    mpi_remove "$DUNEMPILIBS" "-o"
113    DUNEMPILIBS="$retval"
114    #strip DUNEMPICPPFLAGS (which are included for mpich2 on jugene)
115    enc=`echo "$DUNEMPICPPFLAGS" | sed -e 's/\\//\\\\\\//g'`
116    DUNEMPILIBS=`echo "$retval" | sed -e "s/$enc / /"`
117  fi
118
119  # hack in option to disable MPICH-C++-bindings...
120  MPI_NOCXXFLAGS="-DMPICH_SKIP_MPICXX"
121}
122
123mpi_getmpich2flags() {
124  if test x"$DUNEMPICPPFLAGS" = x; then
125    # use special commands to extract options
126    mpi_getflags "-show" "-c"
127    DUNEMPICPPFLAGS="$retval"
128    # remove implicitly set -c
129    mpi_remove "$DUNEMPICPPFLAGS" '-c'
130    DUNEMPICPPFLAGS="$retval"
131  fi
132
133  if test x"$DUNEMPILIBS" = x; then
134    # get linker options
135    mpi_getflags "-show" "-o"
136    DUNEMPILIBS="$retval"
137    # strip -o option
138    mpi_remove "$DUNEMPILIBS" "-o"
139    DUNEMPILIBS="$retval"
140    #strip DUNEMPICPPFLAGS (which are included for mpich2 on jugene)
141    enc=`echo "$DUNEMPICPPFLAGS" | sed -e 's/\\//\\\\\\//g'`
142    DUNEMPILIBS=`echo "$retval" | sed -e "s/$enc / /"`
143  fi
144
145  # hack in option to disable MPICH-C++-bindings...
146  MPI_NOCXXFLAGS="-DMPICH_SKIP_MPICXX"
147}
148
149test_mpich () {
150  AC_MSG_CHECKING([for mpich])
151  cat >conftest.c <<_EOF
152#include <mpi.h>
153
154int main() { return 0; }
155_EOF
156
157  if (mpi_preprocess conftest.c \
158      | grep -q MPICHX_PARALLELSOCKETS_PARAMETERS); then
159    dune_MPI_VERSION="MPICH"
160    mpi_getmpichflags
161
162    AC_MSG_RESULT([yes])
163    rm -f conftest*
164    return 0
165  fi
166
167  rm -f conftest*
168  AC_MSG_RESULT([no])
169  return 1
170}
171
172test_mpich2 () {
173  AC_MSG_CHECKING([for mpich2])
174  cat >conftest.c <<_EOF
175#include <mpi.h>
176#include <stdio.h>
177int main() { printf ("%s\n", MPICH2_VERSION); return 0; }
178_EOF
179
180  if mpi_trybuild "-c conftest.c"; then
181    dune_MPI_VERSION="MPICH2"
182    mpi_getmpich2flags
183
184    AC_MSG_RESULT([yes])
185    rm -f conftest*
186    return 0
187  fi
188
189  rm -f conftest*
190  AC_MSG_RESULT([no])
191  return 1
192}
193
194test_mpich3 () {
195  AC_MSG_CHECKING([for mpich 3.x])
196  cat >conftest.c <<_EOF
197#include <mpi.h>
198#include <stdio.h>
199int main() { printf ("%s\n", MPICH_VERSION); return 0; }
200_EOF
201
202  if (mpi_trybuild_run "conftest.c" | grep -q "^3\.") ; then
203    dune_MPI_VERSION="MPICH2"
204    mpi_getmpich2flags
205
206    AC_MSG_RESULT([yes])
207    rm -f conftest*
208    return 0
209  fi
210
211  rm -f conftest*
212  AC_MSG_RESULT([no])
213  return 1
214}
215
216test_openmpi () {
217  AC_MSG_CHECKING([for OpenMPI])
218  cat >conftest.c <<_EOF
219#include <mpi.h>
220
221int main() { return 0; }
222_EOF
223
224  if (mpi_preprocess conftest.c | grep -q ompi_communicator_t); then
225    dune_MPI_VERSION="OpenMPI"
226
227    if test x"$DUNEMPICPPFLAGS" = x; then
228      mpi_getflags "-showme:compile"
229      DUNEMPICPPFLAGS="$retval"
230    fi
231    if test x"$DUNEMPILIBS" = x; then
232      mpi_getflags "-showme:link"
233      DUNEMPILIBS="$retval"
234    fi
235    MPI_NOCXXFLAGS="-DMPIPP_H"
236
237    AC_MSG_RESULT([yes])
238    rm -f conftest*
239    return 0
240  fi
241
242  rm -f conftest*
243  AC_MSG_RESULT([no])
244  return 1
245}
246
247test_mvapich() {
248  AC_MSG_CHECKING([for MVAPICH])
249
250  mpi_getflags "-v" "-c dummy.c"
251  if (echo $dune_MPI_VERSION | grep ^MVAPICH>/dev/null);then
252      get_mpichflags
253
254      AC_MSG_RESULT([yes])
255      return 0
256  fi
257
258  AC_MSG_RESULT([no])
259  return 1
260}
261
262test_mvapich2() {
263  AC_MSG_CHECKING([for MVAPICH2])
264  cat >conftest.c <<_EOF
265#define _OSU_MVAPICH_
266#include <mpi.h>
267/* MVAPICH2_VERSION is only defined for MVAPICH2 1.4+
268 * MVAPICH_VERSION is only defined for MVAPICH2 1.2.*
269 * We can thus fall back to MVAPICH_VERSION if MVAPICH2_VERSION
270 * is not defined.
271 */
272#ifndef MVAPICH2_VERSION
273#define MVAPICH2_VERSION MVAPICH_VERSION
274#endif
275#include <stdio.h>
276int main() { printf("%s\n",MVAPICH2_VERSION); return 0; }
277_EOF
278
279  if mpi_trybuild "-c conftest.c"; then
280    dune_MPI_VERSION="MVAPICH2"
281    mpi_getmpich2flags
282
283    AC_MSG_RESULT([yes])
284    rm -f conftest*
285    return 0
286  fi
287
288  rm -f conftest*
289  AC_MSG_RESULT([no])
290  return 1
291}
292
293test_ibmmpi() {
294  AC_MSG_CHECKING([for IBM MPI])
295  if $MPICC -v -c conftest.c > /dev/null 2>&1; then
296    mpi_getflags "-v" "-c dummy.c"
297    if (echo $retval | grep '^xl[[cC]]'); then
298      dune_MPI_VERSION="IBM MPI"
299
300      if test x"$DUNEMPICPPFLAGS" = x; then
301        DUNEMPICPPFLAGS="$retval"
302      fi
303
304      if test x"$DUNEMPILIBS" = x; then
305        mpi_getflags "-v" "dummy.o -o dummy"
306        DUNEMPILIBS="$retval"
307      fi
308
309      AC_MSG_RESULT([yes])
310      rm -f conftest*
311      return 0
312    fi
313  fi
314
315  AC_MSG_RESULT([no])
316  return 1
317}
318
319test_intelmpi() {
320  AC_MSG_CHECKING([for Intel MPI])
321  mpi_getflags "-v"
322  if (echo $retval | grep 'Intel(R) MPI Library'); then
323    dune_MPI_VERSION="Intel MPI"
324    mpi_getmpich2flags
325
326    AC_MSG_RESULT([yes])
327    return 0
328  fi
329
330  AC_MSG_RESULT([no])
331  return 1
332}
333
334get_mpiparameters() {
335  AC_MSG_NOTICE([Trying to identify the version of MPI compiler $MPICC])
336
337  if test x"$dune_MPI_VERSION" != x; then
338    return
339  fi
340
341  test_lam && return
342  test_mpich && return
343  test_openmpi && return
344  test_mvapich && return
345  test_mvapich2 && return
346  test_mpich2 && return
347  test_mpich3 && return
348  test_ibmmpi && return
349  test_intelmpi && return
350
351  dune_MPI_VERSION="unknown"
352  AC_MSG_ERROR([Could not identify MPI-package! Please send a bugreport and tell us what MPI-package you're using.])
353}
354])
355
356AC_DEFUN([MPI_CONFIG],[
357  AC_REQUIRE([MPI_CONFIG_HELPER])
358  get_mpiparameters;
359])
360