FindMPI.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # - Message Passing Interface (MPI) module.
  2. #
  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.
  10. #
  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.
  25. #
  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.
  35. #
  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.
  39. #
  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.
  46. #
  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.
  54. #=============================================================================
  55. # Copyright 2001-2009 Kitware, Inc.
  56. #
  57. # Distributed under the OSI-approved BSD License (the "License");
  58. # see accompanying file Copyright.txt for details.
  59. #
  60. # This software is distributed WITHOUT ANY WARRANTY; without even the
  61. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  62. # See the License for more information.
  63. #=============================================================================
  64. # (To distribute this file outside of CMake, substitute the full
  65. # License text for the above reference.)
  66. # This module is maintained by David Partyka <[email protected]>.
  67. # A set of directories to search through in addition to the standard system paths
  68. # that find_program will search through.
  69. # Microsoft HPC SDK is automatically added to the system path
  70. # Argonne National Labs MPICH2 sets a registry key that we can use.
  71. set(_MPI_PACKAGE_DIR
  72. mpi
  73. mpich
  74. openmpi
  75. lib/mpi
  76. lib/mpich
  77. lib/openmpi
  78. "MPICH/SDK"
  79. "Microsoft Compute Cluster Pack"
  80. )
  81. set(_MPI_PREFIX_PATH)
  82. if(WIN32)
  83. list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]/..")
  84. list(APPEND _MPI_PREFIX_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]")
  85. endif()
  86. foreach(SystemPrefixDir ${CMAKE_SYSTEM_PREFIX_PATH})
  87. foreach(MpiPackageDir ${_MPI_PREFIX_PATH})
  88. if(EXISTS ${SystemPrefixDir}/${MpiPackageDir})
  89. list(APPEND _MPI_PREFIX_PATH "${SystemPrefixDir}/${MpiPackageDir}")
  90. endif()
  91. endforeach(MpiPackageDir)
  92. endforeach(SystemPrefixDir)
  93. # Most mpi distros have some form of mpiexec which gives us something we can reliably look for.
  94. find_program(MPIEXEC
  95. NAMES mpiexec mpirun lamexec
  96. PATHS ${_MPI_PREFIX_PATH}
  97. PATH_SUFFIXES bin
  98. DOC "Executable for running MPI programs."
  99. )
  100. # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
  101. # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
  102. get_filename_component(_MPI_BASE_DIR "${MPIEXEC}" PATH)
  103. get_filename_component(_MPI_BASE_DIR "${_MPI_BASE_DIR}" PATH)
  104. # If there is an mpi compiler find it and interogate (farther below) it for the include
  105. # and lib dirs otherwise we will continue to search from ${_MPI_BASE_DIR}.
  106. find_program(MPI_COMPILER
  107. NAMES mpic++ mpicxx mpiCC mpicc
  108. HINTS "${_MPI_BASE_DIR}"
  109. PATH_SUFFIXES bin
  110. DOC "MPI compiler. Used only to detect MPI compilation flags.")
  111. mark_as_advanced(MPI_COMPILER)
  112. 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.")
  113. set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
  114. set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
  115. set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
  116. mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
  117. MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
  118. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  119. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  120. # the cache, and we don't want to override those settings.
  121. elseif (MPI_COMPILER)
  122. # Check whether the -showme:compile option works. This indicates
  123. # that we have either Open MPI or a newer version of LAM-MPI, and
  124. # implies that -showme:link will also work.
  125. # Note that Windows distros do not have an mpi compiler to interogate.
  126. exec_program(${MPI_COMPILER}
  127. ARGS -showme:compile
  128. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  129. RETURN_VALUE MPI_COMPILER_RETURN)
  130. if (MPI_COMPILER_RETURN EQUAL 0)
  131. # If we appear to have -showme:compile, then we should also have
  132. # -showme:link. Try it.
  133. exec_program(${MPI_COMPILER}
  134. ARGS -showme:link
  135. OUTPUT_VARIABLE MPI_LINK_CMDLINE
  136. RETURN_VALUE MPI_COMPILER_RETURN)
  137. # Note that we probably have -showme:incdirs and -showme:libdirs
  138. # as well.
  139. set(MPI_COMPILER_MAY_HAVE_INCLIBDIRS TRUE)
  140. endif (MPI_COMPILER_RETURN EQUAL 0)
  141. if (MPI_COMPILER_RETURN EQUAL 0)
  142. # Do nothing: we have our command lines now
  143. else (MPI_COMPILER_RETURN EQUAL 0)
  144. # Older versions of LAM-MPI have "-showme". Try it.
  145. exec_program(${MPI_COMPILER}
  146. ARGS -showme
  147. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  148. RETURN_VALUE MPI_COMPILER_RETURN)
  149. endif (MPI_COMPILER_RETURN EQUAL 0)
  150. if (MPI_COMPILER_RETURN EQUAL 0)
  151. # Do nothing: we have our command lines now
  152. else (MPI_COMPILER_RETURN EQUAL 0)
  153. # MPICH uses "-show". Try it.
  154. exec_program(${MPI_COMPILER}
  155. ARGS -show
  156. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  157. RETURN_VALUE MPI_COMPILER_RETURN)
  158. endif (MPI_COMPILER_RETURN EQUAL 0)
  159. if (MPI_COMPILER_RETURN EQUAL 0)
  160. # We have our command lines, but we might need to copy
  161. # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
  162. if (NOT MPI_LINK_CMDLINE)
  163. SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
  164. endif (NOT MPI_LINK_CMDLINE)
  165. else (MPI_COMPILER_RETURN EQUAL 0)
  166. message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
  167. endif (MPI_COMPILER_RETURN EQUAL 0)
  168. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  169. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  170. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  171. # the cache, and we don't want to override those settings.
  172. elseif (MPI_COMPILE_CMDLINE)
  173. # Extract compile flags from the compile command line.
  174. string(REGEX MATCHALL "(^| )-[Df]([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
  175. set(MPI_COMPILE_FLAGS_WORK)
  176. foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
  177. if (MPI_COMPILE_FLAGS_WORK)
  178. set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
  179. else(MPI_COMPILE_FLAGS_WORK)
  180. set(MPI_COMPILE_FLAGS_WORK ${FLAG})
  181. endif(MPI_COMPILE_FLAGS_WORK)
  182. endforeach(FLAG)
  183. # Extract include paths from compile command line
  184. string(REGEX MATCHALL "(^| )-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
  185. set(MPI_INCLUDE_PATH_WORK)
  186. foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
  187. string(REGEX REPLACE "^ ?-I" "" IPATH ${IPATH})
  188. string(REGEX REPLACE "//" "/" IPATH ${IPATH})
  189. list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
  190. endforeach(IPATH)
  191. if (NOT MPI_INCLUDE_PATH_WORK)
  192. if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  193. # The compile command line didn't have any include paths on it,
  194. # but we may have -showme:incdirs. Use it.
  195. exec_program(${MPI_COMPILER}
  196. ARGS -showme:incdirs
  197. OUTPUT_VARIABLE MPI_INCLUDE_PATH_WORK
  198. RETURN_VALUE MPI_COMPILER_RETURN)
  199. separate_arguments(MPI_INCLUDE_PATH_WORK)
  200. endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  201. endif (NOT MPI_INCLUDE_PATH_WORK)
  202. if (NOT MPI_INCLUDE_PATH_WORK)
  203. # If all else fails, just search for mpi.h in the normal include
  204. # paths.
  205. find_path(MPI_INCLUDE_PATH mpi.h
  206. HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
  207. PATH_SUFFIXES include
  208. )
  209. set(MPI_INCLUDE_PATH_WORK ${MPI_INCLUDE_PATH})
  210. endif (NOT MPI_INCLUDE_PATH_WORK)
  211. # Extract linker paths from the link command line
  212. string(REGEX MATCHALL "(^| |-Wl,)-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
  213. set(MPI_LINK_PATH)
  214. foreach(LPATH ${MPI_ALL_LINK_PATHS})
  215. string(REGEX REPLACE "^(| |-Wl,)-L" "" LPATH ${LPATH})
  216. string(REGEX REPLACE "//" "/" LPATH ${LPATH})
  217. list(APPEND MPI_LINK_PATH ${LPATH})
  218. endforeach(LPATH)
  219. if (NOT MPI_LINK_PATH)
  220. if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  221. # The compile command line didn't have any linking paths on it,
  222. # but we may have -showme:libdirs. Use it.
  223. exec_program(${MPI_COMPILER}
  224. ARGS -showme:libdirs
  225. OUTPUT_VARIABLE MPI_LINK_PATH
  226. RETURN_VALUE MPI_COMPILER_RETURN)
  227. separate_arguments(MPI_LINK_PATH)
  228. endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  229. endif (NOT MPI_LINK_PATH)
  230. # Extract linker flags from the link command line
  231. string(REGEX MATCHALL "(^| )-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
  232. set(MPI_LINK_FLAGS_WORK)
  233. foreach(FLAG ${MPI_ALL_LINK_FLAGS})
  234. if (MPI_LINK_FLAGS_WORK)
  235. set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
  236. else(MPI_LINK_FLAGS_WORK)
  237. set(MPI_LINK_FLAGS_WORK ${FLAG})
  238. endif(MPI_LINK_FLAGS_WORK)
  239. endforeach(FLAG)
  240. # Extract the set of libraries to link against from the link command
  241. # line
  242. string(REGEX MATCHALL "(^| )-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
  243. # Determine full path names for all of the libraries that one needs
  244. # to link against in an MPI program
  245. set(MPI_LIBRARIES)
  246. foreach(LIB ${MPI_LIBNAMES})
  247. string(REGEX REPLACE "^ ?-l" "" LIB ${LIB})
  248. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
  249. find_library(MPI_LIB ${LIB} HINTS ${MPI_LINK_PATH})
  250. if (MPI_LIB)
  251. list(APPEND MPI_LIBRARIES ${MPI_LIB})
  252. elseif (NOT MPI_FIND_QUIETLY)
  253. message(WARNING "Unable to find MPI library ${LIB}")
  254. endif ()
  255. endforeach(LIB)
  256. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
  257. # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
  258. # MPI_EXTRA_LIBRARY.
  259. list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
  260. list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
  261. if (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  262. list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
  263. set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
  264. else (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  265. set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
  266. endif (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  267. if (MPI_NUMLIBS GREATER 1)
  268. set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
  269. list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
  270. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
  271. else (MPI_NUMLIBS GREATER 1)
  272. set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
  273. endif (MPI_NUMLIBS GREATER 1)
  274. # Set up all of the appropriate cache entries
  275. set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
  276. set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
  277. set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
  278. else (MPI_COMPILE_CMDLINE)
  279. # No MPI compiler to interogate so attempt to find everything with find functions.
  280. find_path(MPI_INCLUDE_PATH mpi.h
  281. HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
  282. PATH_SUFFIXES include
  283. )
  284. # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
  285. if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
  286. set(MS_MPI_ARCH_DIR amd64)
  287. else()
  288. set(MS_MPI_ARCH_DIR i386)
  289. endif()
  290. find_library(MPI_LIBRARY
  291. NAMES mpi mpich msmpi
  292. HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
  293. PATH_SUFFIXES lib lib/${MS_MPI_ARCH_DIR} Lib Lib/${MS_MPI_ARCH_DIR}
  294. )
  295. find_library(MPI_EXTRA_LIBRARY
  296. NAMES mpi++
  297. HINTS ${_MPI_BASE_DIR} ${_MPI_PREFIX_PATH}
  298. PATH_SUFFIXES lib
  299. DOC "Extra MPI libraries to link against.")
  300. set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
  301. set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
  302. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  303. # Set up extra variables to conform to
  304. if (MPI_EXTRA_LIBRARY)
  305. set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
  306. else (MPI_EXTRA_LIBRARY)
  307. set(MPI_LIBRARIES ${MPI_LIBRARY})
  308. endif (MPI_EXTRA_LIBRARY)
  309. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  310. set(MPI_FOUND TRUE)
  311. else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  312. set(MPI_FOUND FALSE)
  313. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  314. include(FindPackageHandleStandardArgs)
  315. # handle the QUIETLY and REQUIRED arguments
  316. find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
  317. mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY
  318. MPI_EXTRA_LIBRARY)
  319. # unset to cleanup namespace
  320. unset(_MPI_PACKAGE_DIR)
  321. unset(_MPI_PREFIX_PATH)
  322. unset(_MPI_BASE_DIR)