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)
62 NAMES mpiexec mpirun lamexec
63 DOC "Executable for running MPI programs.")
65 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.")
66 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
67 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
68 set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
69 mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
70 MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
72 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
73 # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
74 # the cache, and we don't want to override those settings.
76 # Check whether the -showme:compile option works. This indicates
77 # that we have either Open MPI or a newer version of LAM-MPI, and
78 # implies that -showme:link will also work.
79 exec_program(${MPI_COMPILER}
81 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
82 RETURN_VALUE MPI_COMPILER_RETURN)
84 if (MPI_COMPILER_RETURN EQUAL 0)
85 # If we appear to have -showme:compile, then we should also have
86 # -showme:link. Try it.
87 exec_program(${MPI_COMPILER}
89 OUTPUT_VARIABLE MPI_LINK_CMDLINE
90 RETURN_VALUE MPI_COMPILER_RETURN)
91 endif (MPI_COMPILER_RETURN EQUAL 0)
93 if (MPI_COMPILER_RETURN EQUAL 0)
94 # Do nothing: we have our command lines now
95 else (MPI_COMPILER_RETURN EQUAL 0)
96 # Older versions of LAM-MPI have "-showme". Try it.
97 exec_program(${MPI_COMPILER}
99 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
100 RETURN_VALUE MPI_COMPILER_RETURN)
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 # MPICH uses "-show". 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 # We have our command lines, but we might need to copy
115 # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
116 if (NOT MPI_LINK_CMDLINE)
117 SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
118 endif (NOT MPI_LINK_CMDLINE)
119 else (MPI_COMPILER_RETURN EQUAL 0)
120 message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
121 endif (MPI_COMPILER_RETURN EQUAL 0)
122 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
124 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
125 # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
126 # the cache, and we don't want to override those settings.
127 elseif (MPI_COMPILE_CMDLINE)
128 # Extract compile flags from the compile command line.
129 string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
130 set(MPI_COMPILE_FLAGS_WORK)
131 foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
132 if (MPI_COMPILE_FLAGS_WORK)
133 set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
134 else(MPI_COMPILE_FLAGS_WORK)
135 set(MPI_COMPILE_FLAGS_WORK ${FLAG})
136 endif(MPI_COMPILE_FLAGS_WORK)
139 # Extract include paths from compile command line
140 string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
141 set(MPI_INCLUDE_PATH_WORK)
142 foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
143 string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
144 string(REGEX REPLACE "//" "/" IPATH ${IPATH})
145 list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
148 # Extract linker paths from the link command line
149 string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
151 foreach(LPATH ${MPI_ALL_LINK_PATHS})
152 string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
153 string(REGEX REPLACE "//" "/" LPATH ${LPATH})
154 list(APPEND MPI_LINK_PATH ${LPATH})
157 # Extract linker flags from the link command line
158 string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
159 set(MPI_LINK_FLAGS_WORK)
160 foreach(FLAG ${MPI_ALL_LINK_FLAGS})
161 if (MPI_LINK_FLAGS_WORK)
162 set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
163 else(MPI_LINK_FLAGS_WORK)
164 set(MPI_LINK_FLAGS_WORK ${FLAG})
165 endif(MPI_LINK_FLAGS_WORK)
168 # Extract the set of libraries to link against from the link command
170 string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
172 # Determine full path names for all of the libraries that one needs
173 # to link against in an MPI program
175 foreach(LIB ${MPI_LIBNAMES})
176 string(REGEX REPLACE "^-l" "" LIB ${LIB})
177 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
178 find_library(MPI_LIB ${LIB} PATHS ${MPI_LINK_PATH})
180 list(APPEND MPI_LIBRARIES ${MPI_LIB})
182 status(ERROR "Unable to find MPI library ${LIB}")
185 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
187 # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
189 list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
190 if (MPI_NUMLIBS GREATER 0)
191 list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
192 set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
193 else (MPI_NUMLIBS GREATER 0)
194 set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE STRING "MPI library to link against" FORCE)
195 endif (MPI_NUMLIBS GREATER 0)
196 if (MPI_NUMLIBS GREATER 1)
197 set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
198 list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
199 set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
200 else (MPI_NUMLIBS GREATER 1)
201 set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
202 endif (MPI_NUMLIBS GREATER 1)
204 # Set up all of the appropriate cache entries
205 set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
206 set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
207 set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
208 else (MPI_COMPILE_CMDLINE)
209 find_path(MPI_INCLUDE_PATH mpi.h
213 /usr/local/mpi/include
214 "C:/Program Files/MPICH/SDK/Include"
215 "$ENV{SystemDrive}/Program Files/MPICH2/include"
216 "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Include"
219 # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
221 set(MS_MPI_ARCH_DIR amd64)
223 set(MS_MPI_ARCH_DIR i386)
226 find_library(MPI_LIBRARY
227 NAMES mpi mpich msmpi
228 PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
229 "C:/Program Files/MPICH/SDK/Lib"
230 "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
231 "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Lib/${MS_MPI_ARCH_DIR}"
233 find_library(MPI_LIBRARY
236 "$ENV{SystemDrive}/Program Files/MPICH2/Lib")
238 find_library(MPI_EXTRA_LIBRARY
240 PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
241 "C:/Program Files/MPICH/SDK/Lib"
242 DOC "Extra MPI libraries to link against.")
244 set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
245 set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
246 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
248 # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
249 if("${MPI_LIBRARY}" MATCHES "mpich.rts")
250 set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
251 set(MPI_LIBRARY ${MPI_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
252 endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
254 # Set up extra variables to conform to
255 if (MPI_EXTRA_LIBRARY)
256 set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
257 else (MPI_EXTRA_LIBRARY)
258 set(MPI_LIBRARIES ${MPI_LIBRARY})
259 endif (MPI_EXTRA_LIBRARY)
261 if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
263 else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
265 endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
267 include(FindPackageHandleStandardArgs)
268 # handle the QUIETLY and REQUIRED arguments
269 find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
271 mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY