Debian Codegraph @

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