fixed g_tune_pme to pass correctly boolean arguments to mdrun
[gromacs/rigid-bodies.git] / cmake / FindMPI.cmake
bloba0c36ed77af5dd34443a3886296d45098bffda5c
1 # - Message Passing Interface (MPI) module.
3 # The Message Passing Interface (MPI) is a library used to write
4 # high-performance parallel applications that use message passing, and
5 # is typically deployed on a cluster. MPI is a standard interface
6 # (defined by the MPI forum) for which many implementations are
7 # available. All of these implementations have somewhat different
8 # compilation approaches (different include paths, libraries to link
9 # against, etc.), and this module tries to smooth out those differences.
11 # This module will set the following variables:
12 #   MPI_FOUND                  TRUE if we have found MPI
13 #   MPI_COMPILE_FLAGS          Compilation flags for MPI programs
14 #   MPI_INCLUDE_PATH           Include path(s) for MPI header
15 #   MPI_LINK_FLAGS             Linking flags for MPI programs
16 #   MPI_LIBRARY                First MPI library to link against (cached)
17 #   MPI_EXTRA_LIBRARY          Extra MPI libraries to link against (cached)
18 #   MPI_LIBRARIES              All libraries to link MPI programs against
19 #   MPIEXEC                    Executable for running MPI programs
20 #   MPIEXEC_NUMPROC_FLAG       Flag to pass to MPIEXEC before giving it the
21 #                              number of processors to run on
22 #   MPIEXEC_PREFLAGS           Flags to pass to MPIEXEC directly before the
23 #                              executable to run.
24 #   MPIEXEC_POSTFLAGS          Flags to pass to MPIEXEC after all other flags.
26 # This module will attempt to auto-detect these settings, first by
27 # looking for a MPI compiler, which many MPI implementations provide
28 # as a pass-through to the native compiler to simplify the compilation
29 # of MPI programs. The MPI compiler is stored in the cache variable
30 # MPI_COMPILER, and will attempt to look for commonly-named drivers
31 # mpic++, mpicxx, mpiCC, or mpicc. If the compiler driver is found and
32 # recognized, it will be used to set all of the module variables. To
33 # skip this auto-detection, set MPI_LIBRARY and MPI_INCLUDE_PATH in
34 # the CMake cache.
36 # If no compiler driver is found or the compiler driver is not
37 # recognized, this module will then search for common include paths
38 # and library names to try to detect MPI.
40 # If CMake initially finds a different MPI than was intended, and you
41 # want to use the MPI compiler auto-detection for a different MPI
42 # implementation, set MPI_COMPILER to the MPI compiler driver you want
43 # to use (e.g., mpicxx) and then set MPI_LIBRARY to the string
44 # MPI_LIBRARY-NOTFOUND. When you re-configure, auto-detection of MPI
45 # will run again with the newly-specified MPI_COMPILER.
47 # When using MPIEXEC to execute MPI applications, you should typically
48 # use all of the MPIEXEC flags as follows:
49 #   ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} PROCS ${MPIEXEC_PREFLAGS} EXECUTABLE
50 #     ${MPIEXEC_POSTFLAGS} ARGS
51 # where PROCS is the number of processors on which to execute the program,
52 # EXECUTABLE is the MPI program, and ARGS are the arguments to pass to the
53 # MPI program.
55 #=============================================================================
56 # Copyright 2001-2009 Kitware, Inc.
58 # Distributed under the OSI-approved BSD License (the "License");
59 # see accompanying file Copyright.txt for details.
61 # This software is distributed WITHOUT ANY WARRANTY; without even the
62 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
63 # See the License for more information.
64 #=============================================================================
65 # (To distributed this file outside of CMake, substitute the full
66 #  License text for the above reference.)
68 # This module is maintained by David Partyka <dave.partyka@kitware.com>.
70 # A set of directories to search through in addition to the standard system paths
71 # that find_program will search through.
72 # Microsoft HPC SDK is automatically added to the system path
73 # Argonne National Labs MPICH2 sets a registry key that we can use.
75 TRY_COMPILE(MPI_FOUND ${CMAKE_BINARY_DIR}
76   "${CMAKE_SOURCE_DIR}/cmake/TestMPI.c"
77   COMPILE_DEFINITIONS )
79 if(MPI_FOUND)
80   return()
81 endif()
83 set(_MPI_PACKAGE_DIR
84   mpi
85   mpich
86   openmpi
87   lib/mpi
88   lib/mpich
89   lib/openmpi
90   "MPICH/SDK"
91   "Microsoft Compute Cluster Pack"
92   )
94 set(_MPI_PREFIX_PATH)
95 if(WIN32)
96   list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]/..")
97   list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]")
98 endif()
100 foreach(SystemPrefixDir ${CMAKE_SYSTEM_PREFIX_PATH})
101   foreach(MpiPackageDir ${_MPI_PREFIX_PATH})
102     if(EXISTS ${SystemPrefixDir}/${MpiPackageDir})
103       list(APPEND _MPI_PREFIX_PATH "${SystemPrefixDir}/${MpiPackageDir}")
104     endif()
105   endforeach(MpiPackageDir)
106 endforeach(SystemPrefixDir)
108 # Most mpi distros have some form of mpiexec which gives us something we can reliably look for.
109 find_program(MPIEXEC
110   NAMES mpiexec mpirun lamexec
111   PATHS ${_MPI_PREFIX_PATH}
112   PATH_SUFFIXES bin
113   DOC "Executable for running MPI programs."
114   )
116 # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
117 # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
118 get_filename_component(_MPI_BASE_DIR "${MPIEXEC}" PATH)
119 get_filename_component(_MPI_BASE_DIR "${_MPI_BASE_DIR}" PATH)
121 # If there is an mpi compiler find it and interogate (farther below) it for the include
122 # and lib dirs otherwise we will continue to search from ${_MPI_BASE_DIR}.
123 find_program(MPI_COMPILER
124   NAMES mpic++ mpicxx mpiCC mpicc
125   HINTS "${_MPI_BASE_DIR}"
126   PATH_SUFFIXES bin
127   DOC "MPI compiler. Used only to detect MPI compilation flags.")
128 mark_as_advanced(MPI_COMPILER)
130 set(MPIEXEC_NUMPROC_FLAG "-np" CACHE STRING "Flag used by MPI to specify the number of processes for MPIEXEC; the next option will be the number of processes.")
131 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
132 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
133 set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
134 mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
135   MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
137 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
138   # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
139   # the cache, and we don't want to override those settings.
140 elseif (MPI_COMPILER)
141   # Check whether the -showme:compile option works. This indicates
142   # that we have either Open MPI or a newer version of LAM-MPI, and
143   # implies that -showme:link will also work.
144   # Note that Windows distros do not have an mpi compiler to interogate.
145   exec_program(${MPI_COMPILER}
146     ARGS -showme:compile
147     OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
148     RETURN_VALUE MPI_COMPILER_RETURN)
150   if (MPI_COMPILER_RETURN EQUAL 0)
151     # If we appear to have -showme:compile, then we should also have
152     # -showme:link. Try it.
153     exec_program(${MPI_COMPILER}
154       ARGS -showme:link
155       OUTPUT_VARIABLE MPI_LINK_CMDLINE
156       RETURN_VALUE MPI_COMPILER_RETURN)
158     # Note that we probably have -showme:incdirs and -showme:libdirs
159     # as well.
160     set(MPI_COMPILER_MAY_HAVE_INCLIBDIRS TRUE)
161   endif (MPI_COMPILER_RETURN EQUAL 0)
163   if (MPI_COMPILER_RETURN EQUAL 0)
164     # Do nothing: we have our command lines now
165   else (MPI_COMPILER_RETURN EQUAL 0)
166     # Older versions of LAM-MPI have "-showme". Try it.
167     exec_program(${MPI_COMPILER}
168       ARGS -showme
169       OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
170       RETURN_VALUE MPI_COMPILER_RETURN)
171   endif (MPI_COMPILER_RETURN EQUAL 0)
173   if (MPI_COMPILER_RETURN EQUAL 0)
174     # Do nothing: we have our command lines now
175   else (MPI_COMPILER_RETURN EQUAL 0)
176     # MPICH uses "-show". Try it.
177     exec_program(${MPI_COMPILER}
178       ARGS -show
179       OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
180       RETURN_VALUE MPI_COMPILER_RETURN)
181   endif (MPI_COMPILER_RETURN EQUAL 0)
183   if (MPI_COMPILER_RETURN EQUAL 0)
184     # We have our command lines, but we might need to copy
185     # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
186     if (NOT MPI_LINK_CMDLINE)
187       SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
188     endif (NOT MPI_LINK_CMDLINE)
189   else (MPI_COMPILER_RETURN EQUAL 0)
190     message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
191   endif (MPI_COMPILER_RETURN EQUAL 0)
192 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
194 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
195   # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
196   # the cache, and we don't want to override those settings.
197 elseif (MPI_COMPILE_CMDLINE)
198   # Extract compile flags from the compile command line.
199   string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
200   set(MPI_COMPILE_FLAGS_WORK)
201   foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
202     if (MPI_COMPILE_FLAGS_WORK)
203       set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
204     else(MPI_COMPILE_FLAGS_WORK)
205       set(MPI_COMPILE_FLAGS_WORK ${FLAG})
206     endif(MPI_COMPILE_FLAGS_WORK)
207   endforeach(FLAG)
209   # Extract include paths from compile command line
210   string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
211   set(MPI_INCLUDE_PATH_WORK)
212   foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
213     string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
214     string(REGEX REPLACE "//" "/" IPATH ${IPATH})
215     list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
216   endforeach(IPATH)
218   if (NOT MPI_INCLUDE_PATH_WORK)
219     if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
220       # The compile command line didn't have any include paths on it,
221       # but we may have -showme:incdirs. Use it.
222       exec_program(${MPI_COMPILER}
223         ARGS -showme:incdirs
224         OUTPUT_VARIABLE MPI_INCLUDE_PATH_WORK
225         RETURN_VALUE MPI_COMPILER_RETURN)
226       separate_arguments(MPI_INCLUDE_PATH_WORK)
227     endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
228   endif (NOT MPI_INCLUDE_PATH_WORK)
230   if (NOT MPI_INCLUDE_PATH_WORK)
231     # If all else fails, just search for mpi.h in the normal include
232     # paths.
233     find_path(MPI_INCLUDE_PATH mpi.h
234   HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
235   PATH_SUFFIXES include
236     )
237     set(MPI_INCLUDE_PATH_WORK ${MPI_INCLUDE_PATH})
238   endif (NOT MPI_INCLUDE_PATH_WORK)
240   # Extract linker paths from the link command line
241   string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
242   set(MPI_LINK_PATH)
243   foreach(LPATH ${MPI_ALL_LINK_PATHS})
244     string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
245     string(REGEX REPLACE "//" "/" LPATH ${LPATH})
246     list(APPEND MPI_LINK_PATH ${LPATH})
247   endforeach(LPATH)
249   if (NOT MPI_LINK_PATH)
250     if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
251       # The compile command line didn't have any linking paths on it,
252       # but we may have -showme:libdirs. Use it.
253       exec_program(${MPI_COMPILER}
254         ARGS -showme:libdirs
255         OUTPUT_VARIABLE MPI_LINK_PATH
256         RETURN_VALUE MPI_COMPILER_RETURN)
257       separate_arguments(MPI_LINK_PATH)
258     endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
259   endif (NOT MPI_LINK_PATH)
261   # Extract linker flags from the link command line
262   string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
263   set(MPI_LINK_FLAGS_WORK)
264   foreach(FLAG ${MPI_ALL_LINK_FLAGS})
265     if (MPI_LINK_FLAGS_WORK)
266       set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
267     else(MPI_LINK_FLAGS_WORK)
268       set(MPI_LINK_FLAGS_WORK ${FLAG})
269     endif(MPI_LINK_FLAGS_WORK)
270   endforeach(FLAG)
272   # Extract the set of libraries to link against from the link command
273   # line
274   string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
276   # Determine full path names for all of the libraries that one needs
277   # to link against in an MPI program
278   set(MPI_LIBRARIES)
279   foreach(LIB ${MPI_LIBNAMES})
280     string(REGEX REPLACE "^-l" "" LIB ${LIB})
281     set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
282     find_library(MPI_LIB ${LIB} HINTS ${MPI_LINK_PATH})
283     if (MPI_LIB)
284       list(APPEND MPI_LIBRARIES ${MPI_LIB})
285     else (MPI_LIB)
286       message(SEND_ERROR "Unable to find MPI library ${LIB}")
287     endif (MPI_LIB)
288   endforeach(LIB)
289   set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
291   # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
292   # MPI_EXTRA_LIBRARY.
293   list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
294   list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
295   if (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
296     list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
297     set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
298   else (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
299     set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
300   endif (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
301   if (MPI_NUMLIBS GREATER 1)
302     set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
303     list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
304     set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
305   else (MPI_NUMLIBS GREATER 1)
306     set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
307   endif (MPI_NUMLIBS GREATER 1)
309   # Set up all of the appropriate cache entries
310   set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
311   set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
312   set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
313 else (MPI_COMPILE_CMDLINE)
314 # No MPI compiler to interogate so attempt to find everything with find functions.
315   find_path(MPI_INCLUDE_PATH mpi.h
316     HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
317     PATH_SUFFIXES include
318     )
320   # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
321   if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
322     set(MS_MPI_ARCH_DIR amd64)
323   else()
324     set(MS_MPI_ARCH_DIR i386)
325   endif()
327   find_library(MPI_LIBRARY
328     NAMES mpi mpich msmpi
329     HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
330     PATH_SUFFIXES lib lib/${MS_MPI_ARCH_DIR} Lib Lib/${MS_MPI_ARCH_DIR}
331     )
333   find_library(MPI_EXTRA_LIBRARY
334     NAMES mpi++
335     HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
336     PATH_SUFFIXES lib
337     DOC "Extra MPI libraries to link against.")
339   set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
340   set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
341 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
343 # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
344 if("${MPI_LIBRARY}" MATCHES "mpich.rts")
345    set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
346    set(MPI_LIBRARY ${MPI_LIBRARY}  msglayer.rts devices.rts rts.rts devices.rts)
347 endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
349 # Set up extra variables to conform to
350 if (MPI_EXTRA_LIBRARY)
351   set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
352 else (MPI_EXTRA_LIBRARY)
353   set(MPI_LIBRARIES ${MPI_LIBRARY})
354 endif (MPI_EXTRA_LIBRARY)
356 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
357   set(MPI_FOUND TRUE)
358 else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
359   set(MPI_FOUND FALSE)
360 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
362 include(FindPackageHandleStandardArgs)
363 # handle the QUIETLY and REQUIRED arguments
364 find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
366 mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY
367   MPI_EXTRA_LIBRARY)
369 # unset to cleanup namespace
370 unset(_MPI_PACKAGE_DIR)
371 unset(_MPI_PREFIX_PATH)
372 unset(_MPI_BASE_DIR)