Added software-related quote
[gromacs/rigid-bodies.git] / cmake / FindMPI.cmake
blob481b0e994dfa5d6c73c3a888444c90637a869a19
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 set(_MPI_PACKAGE_DIR
76   mpi
77   mpich
78   openmpi
79   lib/mpi
80   lib/mpich
81   lib/openmpi
82   "MPICH/SDK"
83   "Microsoft Compute Cluster Pack"
84   )
86 set(_MPI_PREFIX_PATH)
87 if(WIN32)
88   list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]/..")
89   list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]")
90 endif()
92 foreach(SystemPrefixDir ${CMAKE_SYSTEM_PREFIX_PATH})
93   foreach(MpiPackageDir ${_MPI_PREFIX_PATH})
94     if(EXISTS ${SystemPrefixDir}/${MpiPackageDir})
95       list(APPEND _MPI_PREFIX_PATH "${SystemPrefixDir}/${MpiPackageDir}")
96     endif()
97   endforeach(MpiPackageDir)
98 endforeach(SystemPrefixDir)
100 # Most mpi distros have some form of mpiexec which gives us something we can reliably look for.
101 find_program(MPIEXEC
102   NAMES mpiexec mpirun lamexec
103   PATHS ${_MPI_PREFIX_PATH}
104   PATH_SUFFIXES bin
105   DOC "Executable for running MPI programs."
106   )
108 # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
109 # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
110 get_filename_component(_MPI_BASE_DIR "${MPIEXEC}" PATH)
111 get_filename_component(_MPI_BASE_DIR "${_MPI_BASE_DIR}" PATH)
113 # If there is an mpi compiler find it and interogate (farther below) it for the include
114 # and lib dirs otherwise we will continue to search from ${_MPI_BASE_DIR}.
115 find_program(MPI_COMPILER
116   NAMES mpic++ mpicxx mpiCC mpicc
117   HINTS "${_MPI_BASE_DIR}"
118   PATH_SUFFIXES bin
119   DOC "MPI compiler. Used only to detect MPI compilation flags.")
120 mark_as_advanced(MPI_COMPILER)
122 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.")
123 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
124 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
125 set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
126 mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
127   MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
129 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
130   # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
131   # the cache, and we don't want to override those settings.
132 elseif (MPI_COMPILER)
133   # Check whether the -showme:compile option works. This indicates
134   # that we have either Open MPI or a newer version of LAM-MPI, and
135   # implies that -showme:link will also work.
136   # Note that Windows distros do not have an mpi compiler to interogate.
137   exec_program(${MPI_COMPILER}
138     ARGS -showme:compile
139     OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
140     RETURN_VALUE MPI_COMPILER_RETURN)
142   if (MPI_COMPILER_RETURN EQUAL 0)
143     # If we appear to have -showme:compile, then we should also have
144     # -showme:link. Try it.
145     exec_program(${MPI_COMPILER}
146       ARGS -showme:link
147       OUTPUT_VARIABLE MPI_LINK_CMDLINE
148       RETURN_VALUE MPI_COMPILER_RETURN)
150     # Note that we probably have -showme:incdirs and -showme:libdirs
151     # as well.
152     set(MPI_COMPILER_MAY_HAVE_INCLIBDIRS TRUE)
153   endif (MPI_COMPILER_RETURN EQUAL 0)
155   if (MPI_COMPILER_RETURN EQUAL 0)
156     # Do nothing: we have our command lines now
157   else (MPI_COMPILER_RETURN EQUAL 0)
158     # Older versions of LAM-MPI have "-showme". Try it.
159     exec_program(${MPI_COMPILER}
160       ARGS -showme
161       OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
162       RETURN_VALUE MPI_COMPILER_RETURN)
163   endif (MPI_COMPILER_RETURN EQUAL 0)
165   if (MPI_COMPILER_RETURN EQUAL 0)
166     # Do nothing: we have our command lines now
167   else (MPI_COMPILER_RETURN EQUAL 0)
168     # MPICH uses "-show". Try it.
169     exec_program(${MPI_COMPILER}
170       ARGS -show
171       OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
172       RETURN_VALUE MPI_COMPILER_RETURN)
173   endif (MPI_COMPILER_RETURN EQUAL 0)
175   if (MPI_COMPILER_RETURN EQUAL 0)
176     # We have our command lines, but we might need to copy
177     # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
178     if (NOT MPI_LINK_CMDLINE)
179       SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
180     endif (NOT MPI_LINK_CMDLINE)
181   else (MPI_COMPILER_RETURN EQUAL 0)
182     message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
183   endif (MPI_COMPILER_RETURN EQUAL 0)
184 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
186 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
187   # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
188   # the cache, and we don't want to override those settings.
189 elseif (MPI_COMPILE_CMDLINE)
190   # Extract compile flags from the compile command line.
191   string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
192   set(MPI_COMPILE_FLAGS_WORK)
193   foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
194     if (MPI_COMPILE_FLAGS_WORK)
195       set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
196     else(MPI_COMPILE_FLAGS_WORK)
197       set(MPI_COMPILE_FLAGS_WORK ${FLAG})
198     endif(MPI_COMPILE_FLAGS_WORK)
199   endforeach(FLAG)
201   # Extract include paths from compile command line
202   string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
203   set(MPI_INCLUDE_PATH_WORK)
204   foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
205     string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
206     string(REGEX REPLACE "//" "/" IPATH ${IPATH})
207     list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
208   endforeach(IPATH)
210   if (NOT MPI_INCLUDE_PATH_WORK)
211     if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
212       # The compile command line didn't have any include paths on it,
213       # but we may have -showme:incdirs. Use it.
214       exec_program(${MPI_COMPILER}
215         ARGS -showme:incdirs
216         OUTPUT_VARIABLE MPI_INCLUDE_PATH_WORK
217         RETURN_VALUE MPI_COMPILER_RETURN)
218       separate_arguments(MPI_INCLUDE_PATH_WORK)
219     endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
220   endif (NOT MPI_INCLUDE_PATH_WORK)
222   if (NOT MPI_INCLUDE_PATH_WORK)
223     # If all else fails, just search for mpi.h in the normal include
224     # paths.
225     find_path(MPI_INCLUDE_PATH mpi.h
226   HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
227   PATH_SUFFIXES include
228     )
229     set(MPI_INCLUDE_PATH_WORK ${MPI_INCLUDE_PATH})
230   endif (NOT MPI_INCLUDE_PATH_WORK)
232   # Extract linker paths from the link command line
233   string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
234   set(MPI_LINK_PATH)
235   foreach(LPATH ${MPI_ALL_LINK_PATHS})
236     string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
237     string(REGEX REPLACE "//" "/" LPATH ${LPATH})
238     list(APPEND MPI_LINK_PATH ${LPATH})
239   endforeach(LPATH)
241   if (NOT MPI_LINK_PATH)
242     if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
243       # The compile command line didn't have any linking paths on it,
244       # but we may have -showme:libdirs. Use it.
245       exec_program(${MPI_COMPILER}
246         ARGS -showme:libdirs
247         OUTPUT_VARIABLE MPI_LINK_PATH
248         RETURN_VALUE MPI_COMPILER_RETURN)
249       separate_arguments(MPI_LINK_PATH)
250     endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
251   endif (NOT MPI_LINK_PATH)
253   # Extract linker flags from the link command line
254   string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
255   set(MPI_LINK_FLAGS_WORK)
256   foreach(FLAG ${MPI_ALL_LINK_FLAGS})
257     if (MPI_LINK_FLAGS_WORK)
258       set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
259     else(MPI_LINK_FLAGS_WORK)
260       set(MPI_LINK_FLAGS_WORK ${FLAG})
261     endif(MPI_LINK_FLAGS_WORK)
262   endforeach(FLAG)
264   # Extract the set of libraries to link against from the link command
265   # line
266   string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
268   # Determine full path names for all of the libraries that one needs
269   # to link against in an MPI program
270   set(MPI_LIBRARIES)
271   foreach(LIB ${MPI_LIBNAMES})
272     string(REGEX REPLACE "^-l" "" LIB ${LIB})
273     set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
274     find_library(MPI_LIB ${LIB} HINTS ${MPI_LINK_PATH})
275     if (MPI_LIB)
276       list(APPEND MPI_LIBRARIES ${MPI_LIB})
277     else (MPI_LIB)
278       message(SEND_ERROR "Unable to find MPI library ${LIB}")
279     endif (MPI_LIB)
280   endforeach(LIB)
281   set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
283   # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
284   # MPI_EXTRA_LIBRARY.
285   list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
286   list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
287   if (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
288     list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
289     set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
290   else (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
291     set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
292   endif (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
293   if (MPI_NUMLIBS GREATER 1)
294     set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
295     list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
296     set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
297   else (MPI_NUMLIBS GREATER 1)
298     set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
299   endif (MPI_NUMLIBS GREATER 1)
301   # Set up all of the appropriate cache entries
302   set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
303   set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
304   set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
305 else (MPI_COMPILE_CMDLINE)
306 # No MPI compiler to interogate so attempt to find everything with find functions.
307   find_path(MPI_INCLUDE_PATH mpi.h
308     HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
309     PATH_SUFFIXES include
310     )
312   # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
313   if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
314     set(MS_MPI_ARCH_DIR amd64)
315   else()
316     set(MS_MPI_ARCH_DIR i386)
317   endif()
319   find_library(MPI_LIBRARY
320     NAMES mpi mpich msmpi
321     HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
322     PATH_SUFFIXES lib lib/${MS_MPI_ARCH_DIR} Lib Lib/${MS_MPI_ARCH_DIR}
323     )
325   find_library(MPI_EXTRA_LIBRARY
326     NAMES mpi++
327     HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
328     PATH_SUFFIXES lib
329     DOC "Extra MPI libraries to link against.")
331   set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
332   set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
333 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
335 # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
336 if("${MPI_LIBRARY}" MATCHES "mpich.rts")
337    set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
338    set(MPI_LIBRARY ${MPI_LIBRARY}  msglayer.rts devices.rts rts.rts devices.rts)
339 endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
341 # Set up extra variables to conform to
342 if (MPI_EXTRA_LIBRARY)
343   set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
344 else (MPI_EXTRA_LIBRARY)
345   set(MPI_LIBRARIES ${MPI_LIBRARY})
346 endif (MPI_EXTRA_LIBRARY)
348 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
349   set(MPI_FOUND TRUE)
350 else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
351   set(MPI_FOUND FALSE)
352 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
354 include(FindPackageHandleStandardArgs)
355 # handle the QUIETLY and REQUIRED arguments
356 find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
358 mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY
359   MPI_EXTRA_LIBRARY)
361 # unset to cleanup namespace
362 unset(_MPI_PACKAGE_DIR)
363 unset(_MPI_PREFIX_PATH)
364 unset(_MPI_BASE_DIR)