Debian Codegraph @

cmake/modules/FindMPI.cmake

raw
1 # - Find a Message Passing Interface (MPI) implementation
2 # The Message Passing Interface (MPI) is a library used to write
3 # high-performance distributed-memory parallel applications, and
4 # is typically deployed on a cluster. MPI is a standard interface
5 # (defined by the MPI forum) for which many implementations are
6 # available. All of them have somewhat different include paths,
7 # libraries to link against, etc., and this module tries to smooth
8 # out those differences.
9 #
10 # === Variables ===
11 #
12 # This module will set the following variables per language in your project,
13 # where <lang> is one of C, CXX, or Fortran:
14 # MPI_<lang>_FOUND TRUE if FindMPI found MPI flags for <lang>
15 # MPI_<lang>_COMPILER MPI Compiler wrapper for <lang>
16 # MPI_<lang>_COMPILE_FLAGS Compilation flags for MPI programs
17 # MPI_<lang>_INCLUDE_PATH Include path(s) for MPI header
18 # MPI_<lang>_LINK_FLAGS Linking flags for MPI programs
19 # MPI_<lang>_LIBRARIES All libraries to link MPI programs against
20 # Additionally, FindMPI sets the following variables for running MPI
21 # programs from the command line:
22 # MPIEXEC Executable for running MPI programs
23 # MPIEXEC_NUMPROC_FLAG Flag to pass to MPIEXEC before giving
24 # it the number of processors to run on
25 # MPIEXEC_PREFLAGS Flags to pass to MPIEXEC directly
26 # before the executable to run.
27 # MPIEXEC_POSTFLAGS Flags to pass to MPIEXEC after other flags
28 # === Usage ===
29 #
30 # To use this module, simply call FindMPI from a CMakeLists.txt file, or
31 # run find_package(MPI), then run CMake. If you are happy with the auto-
32 # detected configuration for your language, then you're done. If not, you
33 # have two options:
34 # 1. Set MPI_<lang>_COMPILER to the MPI wrapper (mpicc, etc.) of your
35 # choice and reconfigure. FindMPI will attempt to determine all the
36 # necessary variables using THAT compiler's compile and link flags.
37 # 2. If this fails, or if your MPI implementation does not come with
38 # a compiler wrapper, then set both MPI_<lang>_LIBRARIES and
39 # MPI_<lang>_INCLUDE_PATH. You may also set any other variables
40 # listed above, but these two are required. This will circumvent
41 # autodetection entirely.
42 # When configuration is successful, MPI_<lang>_COMPILER will be set to the
43 # compiler wrapper for <lang>, if it was found. MPI_<lang>_FOUND and other
44 # variables above will be set if any MPI implementation was found for <lang>,
45 # regardless of whether a compiler was found.
46 #
47 # When using MPIEXEC to execute MPI applications, you should typically use
48 # all of the MPIEXEC flags as follows:
49 # ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} PROCS
50 # ${MPIEXEC_PREFLAGS} EXECUTABLE ${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.
54 #
55 # === Backward Compatibility ===
56 #
57 # For backward compatibility with older versions of FindMPI, these
58 # variables are set, but deprecated:
59 # MPI_FOUND MPI_COMPILER MPI_LIBRARY
60 # MPI_COMPILE_FLAGS MPI_INCLUDE_PATH MPI_EXTRA_LIBRARY
61 # MPI_LINK_FLAGS MPI_LIBRARIES
62 # In new projects, please use the MPI_<lang>_XXX equivalents.
63
64 #=============================================================================
65 # Copyright 2001-2011 Kitware, Inc.
66 # Copyright 2010-2011 Todd Gamblin [email protected]
67 # Copyright 2001-2009 Dave Partyka
68 #
69 # Redistribution and use in source and binary forms, with or without
70 # modification, are permitted provided that the following conditions
71 # are met:
72 #
73 # 1. Redistributions of source code must retain the copyright
74 # notice, this list of conditions and the following disclaimer.
75 # 2. Redistributions in binary form must reproduce the copyright
76 # notice, this list of conditions and the following disclaimer in the
77 # documentation and/or other materials provided with the distribution.
78 # 3. The name of the author may not be used to endorse or promote products
79 # derived from this software without specific prior written permission.
80 #
81 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
82 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
85 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
86 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
87 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
88 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
89 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
90 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91 #=============================================================================
92
93 # include this to handle the QUIETLY and REQUIRED arguments
94 include(FindPackageHandleStandardArgs)
95 include(GetPrerequisites)
96
97 #
98 # This part detects MPI compilers, attempting to wade through the mess of compiler names in
99 # a sensible way.
100 #
101 # The compilers are detected in this order:
102 #
103 # 1. Try to find the most generic availble MPI compiler, as this is usually set up by
104 # cluster admins. e.g., if plain old mpicc is available, we'll use it and assume it's
105 # the right compiler.
106 #
107 # 2. If a generic mpicc is NOT found, then we attempt to find one that matches
108 # CMAKE_<lang>_COMPILER_ID. e.g. if you are using XL compilers, we'll try to find mpixlc
109 # and company, but not mpiicc. This hopefully prevents toolchain mismatches.
110 #
111 # If you want to force a particular MPI compiler other than what we autodetect (e.g. if you
112 # want to compile regular stuff with GNU and parallel stuff with Intel), you can always set
113 # your favorite MPI_<lang>_COMPILER explicitly and this stuff will be ignored.
114 #
115
116 # Start out with the generic MPI compiler names, as these are most commonly used.
117 set(_MPI_C_COMPILER_NAMES mpicc mpcc mpicc_r mpcc_r)
118 set(_MPI_CXX_COMPILER_NAMES mpicxx mpiCC mpcxx mpCC mpic++ mpc++
119 mpicxx_r mpiCC_r mpcxx_r mpCC_r mpic++_r mpc++_r)
120 set(_MPI_Fortran_COMPILER_NAMES mpif95 mpif95_r mpf95 mpf95_r
121 mpif90 mpif90_r mpf90 mpf90_r
122 mpif77 mpif77_r mpf77 mpf77_r)
123
124 # GNU compiler names
125 set(_MPI_GNU_C_COMPILER_NAMES mpigcc mpgcc mpigcc_r mpgcc_r)
126 set(_MPI_GNU_CXX_COMPILER_NAMES mpig++ mpg++ mpig++_r mpg++_r)
127 set(_MPI_GNU_Fortran_COMPILER_NAMES mpigfortran mpgfortran mpigfortran_r mpgfortran_r
128 mpig77 mpig77_r mpg77 mpg77_r)
129
130 # Intel MPI compiler names
131 set(_MPI_Intel_C_COMPILER_NAMES mpiicc)
132 set(_MPI_Intel_CXX_COMPILER_NAMES mpiicpc mpiicxx mpiic++ mpiiCC)
133 set(_MPI_Intel_Fortran_COMPILER_NAMES mpiifort mpiif95 mpiif90 mpiif77)
134
135 # PGI compiler names
136 set(_MPI_PGI_C_COMPILER_NAMES mpipgcc mppgcc)
137 set(_MPI_PGI_CXX_COMPILER_NAMES mpipgCC mppgCC)
138 set(_MPI_PGI_Fortran_COMPILER_NAMES mpipgf95 mpipgf90 mppgf95 mppgf90 mpipgf77 mppgf77)
139
140 # XLC MPI Compiler names
141 set(_MPI_XL_C_COMPILER_NAMES mpxlc mpxlc_r mpixlc mpixlc_r)
142 set(_MPI_XL_CXX_COMPILER_NAMES mpixlcxx mpixlC mpixlc++ mpxlcxx mpxlc++ mpixlc++ mpxlCC
143 mpixlcxx_r mpixlC_r mpixlc++_r mpxlcxx_r mpxlc++_r mpixlc++_r mpxlCC_r)
144 set(_MPI_XL_Fortran_COMPILER_NAMES mpixlf95 mpixlf95_r mpxlf95 mpxlf95_r
145 mpixlf90 mpixlf90_r mpxlf90 mpxlf90_r
146 mpixlf77 mpixlf77_r mpxlf77 mpxlf77_r
147 mpixlf mpixlf_r mpxlf mpxlf_r)
148
149 # append vendor-specific compilers to the list if we either don't know the compiler id,
150 # or if we know it matches the regular compiler.
151 foreach (lang C CXX Fortran)
152 foreach (id GNU Intel PGI XL)
153 if (NOT CMAKE_${lang}_COMPILER_ID OR "${CMAKE_${lang}_COMPILER_ID}" STREQUAL "${id}")
154 list(APPEND _MPI_${lang}_COMPILER_NAMES ${_MPI_${id}_${lang}_COMPILER_NAMES})
155 endif()
156 unset(_MPI_${id}_${lang}_COMPILER_NAMES) # clean up the namespace here
157 endforeach()
158 endforeach()
159
160
161 # Names to try for MPI exec
162 set(_MPI_EXEC_NAMES mpiexec mpirun lamexec srun)
163
164 # Grab the path to MPI from the registry if we're on windows.
165 set(_MPI_PREFIX_PATH)
166 if(WIN32)
167 list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]/..")
168 list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]")
169 list(APPEND _MPI_PREFIX_PATH "$ENV{ProgramW6432}/MPICH2/")
170 endif()
171
172 # Build a list of prefixes to search for MPI.
173 foreach(SystemPrefixDir ${CMAKE_SYSTEM_PREFIX_PATH})
174 foreach(MpiPackageDir ${_MPI_PREFIX_PATH})
175 if(EXISTS ${SystemPrefixDir}/${MpiPackageDir})
176 list(APPEND _MPI_PREFIX_PATH "${SystemPrefixDir}/${MpiPackageDir}")
177 endif()
178 endforeach()
179 endforeach()
180
181
182 #
183 # interrogate_mpi_compiler(lang try_libs)
184 #
185 # Attempts to extract compiler and linker args from an MPI compiler. The arguments set
186 # by this function are:
187 #
188 # MPI_<lang>_INCLUDE_PATH MPI_<lang>_LINK_FLAGS MPI_<lang>_FOUND
189 # MPI_<lang>_COMPILE_FLAGS MPI_<lang>_LIBRARIES
190 #
191 # MPI_<lang>_COMPILER must be set beforehand to the absolute path to an MPI compiler for
192 # <lang>. Additionally, MPI_<lang>_INCLUDE_PATH and MPI_<lang>_LIBRARIES may be set
193 # to skip autodetection.
194 #
195 # If try_libs is TRUE, this will also attempt to find plain MPI libraries in the usual
196 # way. In general, this is not as effective as interrogating the compilers, as it
197 # ignores language-specific flags and libraries. However, some MPI implementations
198 # (Windows implementations) do not have compiler wrappers, so this approach must be used.
199 #
200 function (interrogate_mpi_compiler lang try_libs)
201 # MPI_${lang}_NO_INTERROGATE will be set to a compiler name when the *regular* compiler was
202 # discovered to be the MPI compiler. This happens on machines like the Cray XE6 that use
203 # modules to set cc, CC, and ftn to the MPI compilers. If the user force-sets another MPI
204 # compiler, MPI_${lang}_COMPILER won't be equal to MPI_${lang}_NO_INTERROGATE, and we'll
205 # inspect that compiler anew. This allows users to set new compilers w/o rm'ing cache.
206 string(COMPARE NOTEQUAL "${MPI_${lang}_NO_INTERROGATE}" "${MPI_${lang}_COMPILER}" interrogate)
207
208 # If MPI is set already in the cache, don't bother with interrogating the compiler.
209 if (interrogate AND ((NOT MPI_${lang}_INCLUDE_PATH) OR (NOT MPI_${lang}_LIBRARIES)))
210 if (MPI_${lang}_COMPILER)
211 # Check whether the -showme:compile option works. This indicates that we have either OpenMPI
212 # or a newer version of LAM-MPI, and implies that -showme:link will also work.
213 execute_process(
214 COMMAND ${MPI_${lang}_COMPILER} -showme:compile
215 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE OUTPUT_STRIP_TRAILING_WHITESPACE
216 ERROR_VARIABLE MPI_COMPILE_CMDLINE ERROR_STRIP_TRAILING_WHITESPACE
217 RESULT_VARIABLE MPI_COMPILER_RETURN)
218
219 if (MPI_COMPILER_RETURN EQUAL 0)
220 # If we appear to have -showme:compile, then we should
221 # also have -showme:link. Try it.
222 execute_process(
223 COMMAND ${MPI_${lang}_COMPILER} -showme:link
224 OUTPUT_VARIABLE MPI_LINK_CMDLINE OUTPUT_STRIP_TRAILING_WHITESPACE
225 ERROR_VARIABLE MPI_LINK_CMDLINE ERROR_STRIP_TRAILING_WHITESPACE
226 RESULT_VARIABLE MPI_COMPILER_RETURN)
227
228 if (MPI_COMPILER_RETURN EQUAL 0)
229 # We probably have -showme:incdirs and -showme:libdirs as well,
230 # so grab that while we're at it.
231 execute_process(
232 COMMAND ${MPI_${lang}_COMPILER} -showme:incdirs
233 OUTPUT_VARIABLE MPI_INCDIRS OUTPUT_STRIP_TRAILING_WHITESPACE
234 ERROR_VARIABLE MPI_INCDIRS ERROR_STRIP_TRAILING_WHITESPACE)
235
236 execute_process(
237 COMMAND ${MPI_${lang}_COMPILER} -showme:libdirs
238 OUTPUT_VARIABLE MPI_LIBDIRS OUTPUT_STRIP_TRAILING_WHITESPACE
239 ERROR_VARIABLE MPI_LIBDIRS ERROR_STRIP_TRAILING_WHITESPACE)
240
241 else()
242 # reset things here if something went wrong.
243 set(MPI_COMPILE_CMDLINE)
244 set(MPI_LINK_CMDLINE)
245 endif()
246 endif ()
247
248 # Older versions of LAM-MPI have "-showme". Try to find that.
249 if (NOT MPI_COMPILER_RETURN EQUAL 0)
250 execute_process(
251 COMMAND ${MPI_${lang}_COMPILER} -showme
252 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE OUTPUT_STRIP_TRAILING_WHITESPACE
253 ERROR_VARIABLE MPI_COMPILE_CMDLINE ERROR_STRIP_TRAILING_WHITESPACE
254 RESULT_VARIABLE MPI_COMPILER_RETURN)
255 endif()
256
257 # MVAPICH uses -compile-info and -link-info. Try them.
258 if (NOT MPI_COMPILER_RETURN EQUAL 0)
259 execute_process(
260 COMMAND ${MPI_${lang}_COMPILER} -compile-info
261 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE OUTPUT_STRIP_TRAILING_WHITESPACE
262 ERROR_VARIABLE MPI_COMPILE_CMDLINE ERROR_STRIP_TRAILING_WHITESPACE
263 RESULT_VARIABLE MPI_COMPILER_RETURN)
264
265 # If we have compile-info, also have link-info.
266 if (MPI_COMPILER_RETURN EQUAL 0)
267 execute_process(
268 COMMAND ${MPI_${lang}_COMPILER} -link-info
269 OUTPUT_VARIABLE MPI_LINK_CMDLINE OUTPUT_STRIP_TRAILING_WHITESPACE
270 ERROR_VARIABLE MPI_LINK_CMDLINE ERROR_STRIP_TRAILING_WHITESPACE
271 RESULT_VARIABLE MPI_COMPILER_RETURN)
272 endif()
273
274 # make sure we got compile and link. Reset vars if something's wrong.
275 if (NOT MPI_COMPILER_RETURN EQUAL 0)
276 set(MPI_COMPILE_CMDLINE)
277 set(MPI_LINK_CMDLINE)
278 endif()
279 endif()
280
281 # MPICH just uses "-show". Try it.
282 if (NOT MPI_COMPILER_RETURN EQUAL 0)
283 execute_process(
284 COMMAND ${MPI_${lang}_COMPILER} -show
285 OUTPUT_VARIABLE MPI_COMPILE_CMDLINE OUTPUT_STRIP_TRAILING_WHITESPACE
286 ERROR_VARIABLE MPI_COMPILE_CMDLINE ERROR_STRIP_TRAILING_WHITESPACE
287 RESULT_VARIABLE MPI_COMPILER_RETURN)
288 endif()
289
290 if (MPI_COMPILER_RETURN EQUAL 0)
291 # We have our command lines, but we might need to copy MPI_COMPILE_CMDLINE
292 # into MPI_LINK_CMDLINE, if we didn't find the link line.
293 if (NOT MPI_LINK_CMDLINE)
294 set(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
295 endif()
296 else()
297 message(STATUS "Unable to determine MPI from MPI driver ${MPI_${lang}_COMPILER}")
298 set(MPI_COMPILE_CMDLINE)
299 set(MPI_LINK_CMDLINE)
300 endif()
301
302 # Here, we're done with the interrogation part, and we'll try to extract args we care
303 # about from what we learned from the compiler wrapper scripts.
304
305 # If interrogation came back with something, extract our variable from the MPI command line
306 if (MPI_COMPILE_CMDLINE OR MPI_LINK_CMDLINE)
307 # Extract compile flags from the compile command line.
308 string(REGEX MATCHALL "(^| )-[Df]([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
309 set(MPI_COMPILE_FLAGS_WORK)
310
311 foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
312 if (MPI_COMPILE_FLAGS_WORK)
313 set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
314 else()
315 set(MPI_COMPILE_FLAGS_WORK ${FLAG})
316 endif()
317 endforeach()
318
319 # Extract include paths from compile command line
320 string(REGEX MATCHALL "(^| )-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
321 foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
322 string(REGEX REPLACE "^ ?-I" "" IPATH ${IPATH})
323 string(REGEX REPLACE "//" "/" IPATH ${IPATH})
324 list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
325 endforeach()
326
327 # try using showme:incdirs if extracting didn't work.
328 if (NOT MPI_INCLUDE_PATH_WORK)
329 set(MPI_INCLUDE_PATH_WORK ${MPI_INCDIRS})
330 separate_arguments(MPI_INCLUDE_PATH_WORK)
331 endif()
332
333 # If all else fails, just search for mpi.h in the normal include paths.
334 if (NOT MPI_INCLUDE_PATH_WORK)
335 set(MPI_HEADER_PATH "MPI_HEADER_PATH-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
336 find_path(MPI_HEADER_PATH mpi.h
337 HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
338 PATH_SUFFIXES include)
339 set(MPI_INCLUDE_PATH_WORK ${MPI_HEADER_PATH})
340 endif()
341
342 # Extract linker paths from the link command line
343 string(REGEX MATCHALL "(^| |-Wl,)-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
344 set(MPI_LINK_PATH)
345 foreach(LPATH ${MPI_ALL_LINK_PATHS})
346 string(REGEX REPLACE "^(| |-Wl,)-L" "" LPATH ${LPATH})
347 string(REGEX REPLACE "//" "/" LPATH ${LPATH})
348 list(APPEND MPI_LINK_PATH ${LPATH})
349 endforeach()
350
351 # try using showme:libdirs if extracting didn't work.
352 if (NOT MPI_LINK_PATH)
353 set(MPI_LINK_PATH ${MPI_LIBDIRS})
354 separate_arguments(MPI_LINK_PATH)
355 endif()
356
357 # Extract linker flags from the link command line
358 string(REGEX MATCHALL "(^| )-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
359 set(MPI_LINK_FLAGS_WORK)
360 foreach(FLAG ${MPI_ALL_LINK_FLAGS})
361 if (MPI_LINK_FLAGS_WORK)
362 set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
363 else()
364 set(MPI_LINK_FLAGS_WORK ${FLAG})
365 endif()
366 endforeach()
367
368 # Extract the set of libraries to link against from the link command
369 # line
370 string(REGEX MATCHALL "(^| )-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
371 # add the compiler implicit directories because some compilers
372 # such as the intel compiler have libraries that show up
373 # in the showme list that can only be found in the implicit
374 # link directories of the compiler. Do this for C++ and C
375 # compilers if the implicit link directories are defined.
376 if (DEFINED CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES)
377 set(MPI_LINK_PATH
378 "${MPI_LINK_PATH};${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES}")
379 endif ()
380
381 if (DEFINED CMAKE_C_IMPLICIT_LINK_DIRECTORIES)
382 set(MPI_LINK_PATH
383 "${MPI_LINK_PATH};${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
384 endif ()
385
386 # Determine full path names for all of the libraries that one needs
387 # to link against in an MPI program
388 foreach(LIB ${MPI_LIBNAMES})
389 string(REGEX REPLACE "^ ?-l" "" LIB ${LIB})
390 # MPI_LIB is cached by find_library, but we don't want that.
391 # Clear it first.
392 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
393 find_library(MPI_LIB NAMES ${LIB}
394 HINTS ${MPI_LINK_PATH}
395 NO_DEFAULT_PATH
396 )
397 find_library(MPI_LIB NAMES ${LIB})
398
399 if (MPI_LIB)
400 list(APPEND MPI_LIBRARIES_WORK ${MPI_LIB})
401 elseif (NOT MPI_FIND_QUIETLY)
402 message(WARNING "Unable to find MPI library ${LIB}")
403 endif()
404 endforeach()
405
406 # Sanity check MPI_LIBRARIES to make sure there are enough libraries
407 list(LENGTH MPI_LIBRARIES_WORK MPI_NUMLIBS)
408 list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
409 if (NOT MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
410 set(MPI_LIBRARIES_WORK "MPI_${lang}_LIBRARIES-NOTFOUND")
411 endif()
412 endif()
413
414 elseif(try_libs)
415 # If we didn't have an MPI compiler script to interrogate, attempt to find everything
416 # with plain old find functions. This is nasty because MPI implementations have LOTS of
417 # different library names, so this section isn't going to be very generic. We need to
418 # make sure it works for MS MPI, though, since there are no compiler wrappers for that.
419 find_path(MPI_HEADER_PATH mpi.h
420 HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
421 PATH_SUFFIXES include Inc)
422 set(MPI_INCLUDE_PATH_WORK ${MPI_HEADER_PATH})
423
424 # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
425 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
426 set(MS_MPI_ARCH_DIR amd64)
427 else()
428 set(MS_MPI_ARCH_DIR i386)
429 endif()
430
431 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
432 find_library(MPI_LIB
433 NAMES mpi mpich mpich2 msmpi
434 HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
435 PATH_SUFFIXES lib lib/${MS_MPI_ARCH_DIR} Lib Lib/${MS_MPI_ARCH_DIR}
436 NO_DEFAULT_PATH
437 )
438 find_library(MPI_LIB
439 NAMES mpi mpich mpich2 msmpi
440 )
441 set(MPI_LIBRARIES_WORK ${MPI_LIB})
442
443 # Right now, we only know about the extra libs for C++.
444 # We could add Fortran here (as there is usually libfmpich, etc.), but
445 # this really only has to work with MS MPI on Windows.
446 # Assume that other MPI's are covered by the compiler wrappers.
447 if (${lang} STREQUAL CXX)
448 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
449 find_library(MPI_LIB
450 NAMES mpi++ mpicxx cxx mpi_cxx
451 HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
452 PATH_SUFFIXES lib
453 NO_DEFAULT_PATH
454 )
455 find_library(MPI_LIB
456 NAMES mpi++ mpicxx cxx mpi_cxx
457 )
458 if (MPI_LIBRARIES_WORK AND MPI_LIB)
459 list(APPEND MPI_LIBRARIES_WORK ${MPI_LIB})
460 endif()
461 endif()
462
463 if (NOT MPI_LIBRARIES_WORK)
464 set(MPI_LIBRARIES_WORK "MPI_${lang}_LIBRARIES-NOTFOUND")
465 endif()
466 endif()
467
468 # If we found MPI, set up all of the appropriate cache entries
469 set(MPI_${lang}_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI ${lang} compilation flags" FORCE)
470 set(MPI_${lang}_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI ${lang} include path" FORCE)
471 set(MPI_${lang}_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI ${lang} linking flags" FORCE)
472 set(MPI_${lang}_LIBRARIES ${MPI_LIBRARIES_WORK} CACHE STRING "MPI ${lang} libraries to link against" FORCE)
473 mark_as_advanced(MPI_${lang}_COMPILE_FLAGS MPI_${lang}_INCLUDE_PATH MPI_${lang}_LINK_FLAGS MPI_${lang}_LIBRARIES)
474
475 # clear out our temporary lib/header detectionv variable here.
476 set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI lib detection" FORCE)
477 set(MPI_HEADER_PATH "MPI_HEADER_PATH-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI header detection" FORCE)
478 endif()
479
480 # finally set a found variable for each MPI language
481 if (MPI_${lang}_INCLUDE_PATH AND MPI_${lang}_LIBRARIES)
482 set(MPI_${lang}_FOUND TRUE PARENT_SCOPE)
483 else()
484 set(MPI_${lang}_FOUND FALSE PARENT_SCOPE)
485 endif()
486 endfunction()
487
488
489 # This function attempts to compile with the regular compiler, to see if MPI programs
490 # work with it. This is a last ditch attempt after we've tried interrogating mpicc and
491 # friends, and after we've tried to find generic libraries. Works on machines like
492 # Cray XE6, where the modules environment changes what MPI version cc, CC, and ftn use.
493 function(try_regular_compiler lang success)
494 set(scratch_directory ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
495 if (${lang} STREQUAL Fortran)
496 set(test_file ${scratch_directory}/cmake_mpi_test.f90)
497 file(WRITE ${test_file}
498 "program hello\n"
499 "include 'mpif.h'\n"
500 "integer ierror\n"
501 "call MPI_INIT(ierror)\n"
502 "call MPI_FINALIZE(ierror)\n"
503 "end\n")
504 else()
505 if (${lang} STREQUAL CXX)
506 set(test_file ${scratch_directory}/cmake_mpi_test.cpp)
507 else()
508 set(test_file ${scratch_directory}/cmake_mpi_test.c)
509 endif()
510 file(WRITE ${test_file}
511 "#include <mpi.h>\n"
512 "int main(int argc, char **argv) {\n"
513 " MPI_Init(&argc, &argv);\n"
514 " MPI_Finalize();\n"
515 "}\n")
516 endif()
517 try_compile(compiler_has_mpi ${scratch_directory} ${test_file})
518 if (compiler_has_mpi)
519 set(MPI_${lang}_NO_INTERROGATE ${CMAKE_${lang}_COMPILER} CACHE STRING "Whether to interrogate MPI ${lang} compiler" FORCE)
520 set(MPI_${lang}_COMPILER ${CMAKE_${lang}_COMPILER} CACHE STRING "MPI ${lang} compiler" FORCE)
521 set(MPI_${lang}_COMPILE_FLAGS "" CACHE STRING "MPI ${lang} compilation flags" FORCE)
522 set(MPI_${lang}_INCLUDE_PATH "" CACHE STRING "MPI ${lang} include path" FORCE)
523 set(MPI_${lang}_LINK_FLAGS "" CACHE STRING "MPI ${lang} linking flags" FORCE)
524 set(MPI_${lang}_LIBRARIES "" CACHE STRING "MPI ${lang} libraries to link against" FORCE)
525 endif()
526 set(${success} ${compiler_has_mpi} PARENT_SCOPE)
527 unset(compiler_has_mpi CACHE)
528 endfunction()
529
530 # End definitions, commence real work here.
531
532 # Most mpi distros have some form of mpiexec which gives us something we can reliably look for.
533 find_program(MPIEXEC
534 NAMES ${_MPI_EXEC_NAMES}
535 PATHS ${_MPI_PREFIX_PATH}
536 PATH_SUFFIXES bin
537 DOC "Executable for running MPI programs.")
538
539 # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
540 # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
541 get_filename_component(_MPI_BASE_DIR "${MPIEXEC}" PATH)
542 get_filename_component(_MPI_BASE_DIR "${_MPI_BASE_DIR}" PATH)
543
544 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.")
545 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
546 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
547 set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
548 mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
549
550
551 #=============================================================================
552 # Backward compatibility input hacks. Propagate the FindMPI hints to C and
553 # CXX if the respective new versions are not defined. Translate the old
554 # MPI_LIBRARY and MPI_EXTRA_LIBRARY to respective MPI_${lang}_LIBRARIES.
555 #
556 # Once we find the new variables, we translate them back into their old
557 # equivalents below.
558 foreach (lang C CXX)
559 # Old input variables.
560 set(_MPI_OLD_INPUT_VARS COMPILER COMPILE_FLAGS INCLUDE_PATH LINK_FLAGS)
561
562 # Set new vars based on their old equivalents, if the new versions are not already set.
563 foreach (var ${_MPI_OLD_INPUT_VARS})
564 if (NOT MPI_${lang}_${var} AND MPI_${var})
565 set(MPI_${lang}_${var} "${MPI_${var}}")
566 endif()
567 endforeach()
568
569 # Special handling for MPI_LIBRARY and MPI_EXTRA_LIBRARY, which we nixed in the
570 # new FindMPI. These need to be merged into MPI_<lang>_LIBRARIES
571 if (NOT MPI_${lang}_LIBRARIES AND (MPI_LIBRARY OR MPI_EXTRA_LIBRARY))
572 set(MPI_${lang}_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
573 endif()
574 endforeach()
575 #=============================================================================
576
577
578 # This loop finds the compilers and sends them off for interrogation.
579 foreach (lang C CXX Fortran)
580 if (CMAKE_${lang}_COMPILER_WORKS)
581 # If the user supplies a compiler *name* instead of an absolute path, assume that we need to find THAT compiler.
582 if (MPI_${lang}_COMPILER)
583 is_file_executable(MPI_${lang}_COMPILER MPI_COMPILER_IS_EXECUTABLE)
584 if (NOT MPI_COMPILER_IS_EXECUTABLE)
585 # Get rid of our default list of names and just search for the name the user wants.
586 set(_MPI_${lang}_COMPILER_NAMES ${MPI_${lang}_COMPILER})
587 set(MPI_${lang}_COMPILER "MPI_${lang}_COMPILER-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
588 # If the user specifies a compiler, we don't want to try to search libraries either.
589 set(try_libs FALSE)
590 endif()
591 else()
592 set(try_libs TRUE)
593 endif()
594
595 find_program(MPI_${lang}_COMPILER
596 NAMES ${_MPI_${lang}_COMPILER_NAMES}
597 PATHS "${MPI_HOME}/bin" "$ENV{MPI_HOME}/bin" ${_MPI_PREFIX_PATH})
598 interrogate_mpi_compiler(${lang} ${try_libs})
599 mark_as_advanced(MPI_${lang}_COMPILER)
600
601 # last ditch try -- if nothing works so far, just try running the regular compiler and
602 # see if we can create an MPI executable.
603 set(regular_compiler_worked 0)
604 if (NOT MPI_${lang}_LIBRARIES OR NOT MPI_${lang}_INCLUDE_PATH)
605 try_regular_compiler(${lang} regular_compiler_worked)
606 endif()
607
608 if (regular_compiler_worked)
609 find_package_handle_standard_args(MPI_${lang} DEFAULT_MSG MPI_${lang}_COMPILER)
610 else()
611 find_package_handle_standard_args(MPI_${lang} DEFAULT_MSG MPI_${lang}_LIBRARIES MPI_${lang}_INCLUDE_PATH)
612 endif()
613 endif()
614 endforeach()
615
616
617 #=============================================================================
618 # More backward compatibility stuff
619 #
620 # Bare MPI sans ${lang} vars are set to CXX then C, depending on what was found.
621 # This mimics the behavior of the old language-oblivious FindMPI.
622 set(_MPI_OLD_VARS FOUND COMPILER INCLUDE_PATH COMPILE_FLAGS LINK_FLAGS LIBRARIES)
623 if (MPI_CXX_FOUND)
624 foreach (var ${_MPI_OLD_VARS})
625 set(MPI_${var} ${MPI_CXX_${var}})
626 endforeach()
627 elseif (MPI_C_FOUND)
628 foreach (var ${_MPI_OLD_VARS})
629 set(MPI_${var} ${MPI_C_${var}})
630 endforeach()
631 else()
632 # Note that we might still have found Fortran, but you'll need to use MPI_Fortran_FOUND
633 set(MPI_FOUND FALSE)
634 endif()
635
636 # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and MPI_EXTRA_LIBRARY, and set them in cache.
637 if (MPI_LIBRARIES)
638 list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
639 set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
640 else()
641 set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
642 endif()
643
644 list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
645 if (MPI_NUMLIBS GREATER 1)
646 set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
647 list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
648 set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
649 else()
650 set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
651 endif()
652 #=============================================================================
653
654 # unset these vars to cleanup namespace
655 unset(_MPI_OLD_VARS)
656 unset(_MPI_PREFIX_PATH)
657 unset(_MPI_BASE_DIR)
658 foreach (lang C CXX Fortran)
659 unset(_MPI_${lang}_COMPILER_NAMES)
660 endforeach()