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
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
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
55 # Try to find the MPI driver program
56 find_program(MPI_COMPILER
57 NAMES mpic++ mpicxx mpiCC mpicc
58 DOC "MPI compiler. Used only to detect MPI compilation flags.")
59 mark_as_advanced(MPI_COMPILER)
61 file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles)
63 NAMES mpiexec mpirun lamexec
64 PATHS /usr/bin /usr/local/bin /usr/local/mpi/bin
65 "$ENV{SystemDrive}/Program Files/MPICH/SDK/Bin"
66 "${ProgramFiles}/MPICH2/Bin"
67 "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Bin"
68 "$ENV{SystemDrive}/Program Files/Microsoft HPC Pack 2008 SDK/Bin"
69 DOC "Executable for running MPI programs.")
71 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.")
72 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
73 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
74 set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
75 mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
76 MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
78 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
79 # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
80 # the cache, and we don't want to override those settings.
82 # Check whether the -showme:compile option works. This indicates
83 # that we have either Open MPI or a newer version of LAM-MPI, and
84 # implies that -showme:link will also work.
85 exec_program(${MPI_COMPILER}
87 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
88 RETURN_VALUE MPI_COMPILER_RETURN)
90 if (MPI_COMPILER_RETURN EQUAL 0)
91 # If we appear to have -showme:compile, then we should also have
92 # -showme:link. Try it.
93 exec_program(${MPI_COMPILER}
95 OUTPUT_VARIABLE MPI_LINK_CMDLINE
96 RETURN_VALUE MPI_COMPILER_RETURN)
98 # Note that we probably have -showme:incdirs and -showme:libdirs
100 set(MPI_COMPILER_MAY_HAVE_INCLIBDIRS TRUE)
101 endif (MPI_COMPILER_RETURN EQUAL 0)
103 if (MPI_COMPILER_RETURN EQUAL 0)
104 # Do nothing: we have our command lines now
105 else (MPI_COMPILER_RETURN EQUAL 0)
106 # Older versions of LAM-MPI have "-showme". Try it.
107 exec_program(${MPI_COMPILER}
109 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
110 RETURN_VALUE MPI_COMPILER_RETURN)
111 endif (MPI_COMPILER_RETURN EQUAL 0)
113 if (MPI_COMPILER_RETURN EQUAL 0)
114 # Do nothing: we have our command lines now
115 else (MPI_COMPILER_RETURN EQUAL 0)
116 # MPICH uses "-show". Try it.
117 exec_program(${MPI_COMPILER}
119 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
120 RETURN_VALUE MPI_COMPILER_RETURN)
121 endif (MPI_COMPILER_RETURN EQUAL 0)
123 if (MPI_COMPILER_RETURN EQUAL 0)
124 # We have our command lines, but we might need to copy
125 # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
126 if (NOT MPI_LINK_CMDLINE)
127 SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
128 endif (NOT MPI_LINK_CMDLINE)
129 else (MPI_COMPILER_RETURN EQUAL 0)
130 message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
131 endif (MPI_COMPILER_RETURN EQUAL 0)
132 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
134 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
135 # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
136 # the cache, and we don't want to override those settings.
137 elseif (MPI_COMPILE_CMDLINE)
138 # Extract compile flags from the compile command line.
139 string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
140 set(MPI_COMPILE_FLAGS_WORK)
141 foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
142 if (MPI_COMPILE_FLAGS_WORK)
143 set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
144 else(MPI_COMPILE_FLAGS_WORK)
145 set(MPI_COMPILE_FLAGS_WORK ${FLAG})
146 endif(MPI_COMPILE_FLAGS_WORK)
149 # Extract include paths from compile command line
150 string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
151 set(MPI_INCLUDE_PATH_WORK)
152 foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
153 string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
154 string(REGEX REPLACE "//" "/" IPATH ${IPATH})
155 list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
158 if (NOT MPI_INCLUDE_PATH_WORK)
159 if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
160 # The compile command line didn't have any include paths on it,
161 # but we may have -showme:incdirs. Use it.
162 exec_program(${MPI_COMPILER}
164 OUTPUT_VARIABLE MPI_INCLUDE_PATH_WORK
165 RETURN_VALUE MPI_COMPILER_RETURN)
166 separate_arguments(MPI_INCLUDE_PATH_WORK)
167 endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
168 endif (NOT MPI_INCLUDE_PATH_WORK)
170 if (NOT MPI_INCLUDE_PATH_WORK)
171 # If all else fails, just search for mpi.h in the normal include
173 find_path(MPI_INCLUDE_PATH mpi.h)
174 set(MPI_INCLUDE_PATH_WORK ${MPI_INCLUDE_PATH})
175 endif (NOT MPI_INCLUDE_PATH_WORK)
177 # Extract linker paths from the link command line
178 string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
180 foreach(LPATH ${MPI_ALL_LINK_PATHS})
181 string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
182 string(REGEX REPLACE "//" "/" LPATH ${LPATH})
183 list(APPEND MPI_LINK_PATH ${LPATH})
186 if (NOT MPI_LINK_PATH)
187 if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
188 # The compile command line didn't have any linking paths on it,
189 # but we may have -showme:libdirs. Use it.
190 exec_program(${MPI_COMPILER}
192 OUTPUT_VARIABLE MPI_LINK_PATH
193 RETURN_VALUE MPI_COMPILER_RETURN)
194 separate_arguments(MPI_LINK_PATH)
195 endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
196 endif (NOT MPI_LINK_PATH)
198 # Extract linker flags from the link command line
199 string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
200 set(MPI_LINK_FLAGS_WORK)
201 foreach(FLAG ${MPI_ALL_LINK_FLAGS})
202 if (MPI_LINK_FLAGS_WORK)
203 set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
204 else(MPI_LINK_FLAGS_WORK)
205 set(MPI_LINK_FLAGS_WORK ${FLAG})
206 endif(MPI_LINK_FLAGS_WORK)
209 # Extract the set of libraries to link against from the link command
211 string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
213 # Determine full path names for all of the libraries that one needs
214 # to link against in an MPI program
216 foreach(LIB ${MPI_LIBNAMES})
217 string(REGEX REPLACE "^-l" "" LIB ${LIB})
218 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
219 find_library(MPI_LIB ${LIB} HINTS ${MPI_LINK_PATH})
221 list(APPEND MPI_LIBRARIES ${MPI_LIB})
223 message(SEND_ERROR "Unable to find MPI library ${LIB}")
226 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
228 # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
230 list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
231 list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
232 if (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
233 list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
234 set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
235 else (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
236 set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
237 endif (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
238 if (MPI_NUMLIBS GREATER 1)
239 set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
240 list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
241 set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
242 else (MPI_NUMLIBS GREATER 1)
243 set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
244 endif (MPI_NUMLIBS GREATER 1)
246 # Set up all of the appropriate cache entries
247 set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
248 set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
249 set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
250 else (MPI_COMPILE_CMDLINE)
251 find_path(MPI_INCLUDE_PATH mpi.h
255 /usr/local/mpi/include
256 "$ENV{SystemDrive}/Program Files/MPICH/SDK/Include"
257 "${ProgramFiles}/MPICH2/include"
258 "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Include"
259 "$ENV{SystemDrive}/Program Files/Microsoft HPC Pack 2008 SDK/Include"
262 # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
264 set(MS_MPI_ARCH_DIR amd64)
266 set(MS_MPI_ARCH_DIR i386)
269 find_library(MPI_LIBRARY
270 NAMES mpi mpich msmpi
271 PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
272 "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
273 "${ProgramFiles}/MPICH2/Lib"
274 "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Lib/${MS_MPI_ARCH_DIR}"
275 "$ENV{SystemDrive}/Program Files/Microsoft HPC Pack 2008 SDK/Lib/${MS_MPI_ARCH_DIR}"
277 find_library(MPI_LIBRARY
280 "${ProgramFiles}/MPICH2/Lib")
282 find_library(MPI_EXTRA_LIBRARY
284 PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
285 "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
286 DOC "Extra MPI libraries to link against.")
288 set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
289 set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
290 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
292 # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
293 if("${MPI_LIBRARY}" MATCHES "mpich.rts")
294 set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
295 set(MPI_LIBRARY ${MPI_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
296 endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
298 # Set up extra variables to conform to
299 if (MPI_EXTRA_LIBRARY)
300 set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
301 else (MPI_EXTRA_LIBRARY)
302 set(MPI_LIBRARIES ${MPI_LIBRARY})
303 endif (MPI_EXTRA_LIBRARY)
305 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
307 else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
309 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
311 include(FindPackageHandleStandardArgs)
312 # handle the QUIETLY and REQUIRED arguments
313 find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
315 mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY