FindMPI.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 distributed 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. endif()
  85. foreach(SystemPrefixDir ${CMAKE_SYSTEM_PREFIX_PATH})
  86. foreach(MpiPackageDir ${MPI_PREFIX_PATH})
  87. if(EXISTS ${SystemPrefixDir}/${MpiPackageDir})
  88. list(APPEND MPI_PREFIX_PATH "${SystemPrefixDir}/${MpiPackageDir}")
  89. endif()
  90. endforeach(MpiPackageDir)
  91. endforeach(SystemPrefixDir)
  92. # Most mpi distros have some form of mpiexec which gives us something we can reliably look for.
  93. find_program(MPIEXEC
  94. NAMES mpiexec mpirun lamexec
  95. PATHS ${MPI_PREFIX_PATH}
  96. PATH_SUFFIXES bin
  97. DOC "Executable for running MPI programs."
  98. )
  99. # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
  100. # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
  101. get_filename_component(MPI_BASE_DIR "${MPIEXEC}" PATH)
  102. get_filename_component(MPI_BASE_DIR "${MPI_BASE_DIR}" PATH)
  103. # If there is an mpi compiler find it and interogate (farther below) it for the include
  104. # and lib dirs otherwise we will continue to search from ${MPI_BASE_DIR}.
  105. find_program(MPI_COMPILER
  106. NAMES mpic++ mpicxx mpiCC mpicc
  107. HINTS "${MPI_BASE_DIR}"
  108. PATH_SUFFIXES bin
  109. DOC "MPI compiler. Used only to detect MPI compilation flags.")
  110. mark_as_advanced(MPI_COMPILER)
  111. 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.")
  112. set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
  113. set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
  114. set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
  115. mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
  116. MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
  117. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  118. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  119. # the cache, and we don't want to override those settings.
  120. elseif (MPI_COMPILER)
  121. # Check whether the -showme:compile option works. This indicates
  122. # that we have either Open MPI or a newer version of LAM-MPI, and
  123. # implies that -showme:link will also work.
  124. # Note that Windows distros do not have an mpi compiler to interogate.
  125. exec_program(${MPI_COMPILER}
  126. ARGS -showme:compile
  127. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  128. RETURN_VALUE MPI_COMPILER_RETURN)
  129. if (MPI_COMPILER_RETURN EQUAL 0)
  130. # If we appear to have -showme:compile, then we should also have
  131. # -showme:link. Try it.
  132. exec_program(${MPI_COMPILER}
  133. ARGS -showme:link
  134. OUTPUT_VARIABLE MPI_LINK_CMDLINE
  135. RETURN_VALUE MPI_COMPILER_RETURN)
  136. # Note that we probably have -showme:incdirs and -showme:libdirs
  137. # as well.
  138. set(MPI_COMPILER_MAY_HAVE_INCLIBDIRS TRUE)
  139. endif (MPI_COMPILER_RETURN EQUAL 0)
  140. if (MPI_COMPILER_RETURN EQUAL 0)
  141. # Do nothing: we have our command lines now
  142. else (MPI_COMPILER_RETURN EQUAL 0)
  143. # Older versions of LAM-MPI have "-showme". Try it.
  144. exec_program(${MPI_COMPILER}
  145. ARGS -showme
  146. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  147. RETURN_VALUE MPI_COMPILER_RETURN)
  148. endif (MPI_COMPILER_RETURN EQUAL 0)
  149. if (MPI_COMPILER_RETURN EQUAL 0)
  150. # Do nothing: we have our command lines now
  151. else (MPI_COMPILER_RETURN EQUAL 0)
  152. # MPICH uses "-show". Try it.
  153. exec_program(${MPI_COMPILER}
  154. ARGS -show
  155. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  156. RETURN_VALUE MPI_COMPILER_RETURN)
  157. endif (MPI_COMPILER_RETURN EQUAL 0)
  158. if (MPI_COMPILER_RETURN EQUAL 0)
  159. # We have our command lines, but we might need to copy
  160. # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
  161. if (NOT MPI_LINK_CMDLINE)
  162. SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
  163. endif (NOT MPI_LINK_CMDLINE)
  164. else (MPI_COMPILER_RETURN EQUAL 0)
  165. message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
  166. endif (MPI_COMPILER_RETURN EQUAL 0)
  167. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  168. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  169. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  170. # the cache, and we don't want to override those settings.
  171. elseif (MPI_COMPILE_CMDLINE)
  172. # Extract compile flags from the compile command line.
  173. string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
  174. set(MPI_COMPILE_FLAGS_WORK)
  175. foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
  176. if (MPI_COMPILE_FLAGS_WORK)
  177. set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
  178. else(MPI_COMPILE_FLAGS_WORK)
  179. set(MPI_COMPILE_FLAGS_WORK ${FLAG})
  180. endif(MPI_COMPILE_FLAGS_WORK)
  181. endforeach(FLAG)
  182. # Extract include paths from compile command line
  183. string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
  184. set(MPI_INCLUDE_PATH_WORK)
  185. foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
  186. string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
  187. string(REGEX REPLACE "//" "/" IPATH ${IPATH})
  188. list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
  189. endforeach(IPATH)
  190. if (NOT MPI_INCLUDE_PATH_WORK)
  191. if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  192. # The compile command line didn't have any include paths on it,
  193. # but we may have -showme:incdirs. Use it.
  194. exec_program(${MPI_COMPILER}
  195. ARGS -showme:incdirs
  196. OUTPUT_VARIABLE MPI_INCLUDE_PATH_WORK
  197. RETURN_VALUE MPI_COMPILER_RETURN)
  198. separate_arguments(MPI_INCLUDE_PATH_WORK)
  199. endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  200. endif (NOT MPI_INCLUDE_PATH_WORK)
  201. if (NOT MPI_INCLUDE_PATH_WORK)
  202. # If all else fails, just search for mpi.h in the normal include
  203. # paths.
  204. find_path(MPI_INCLUDE_PATH mpi.h
  205. HINTS ${MPI_BASE_DIR} ${MPI_PREFIX_PATH}
  206. PATH_SUFFIXES include
  207. )
  208. set(MPI_INCLUDE_PATH_WORK ${MPI_INCLUDE_PATH})
  209. endif (NOT MPI_INCLUDE_PATH_WORK)
  210. # Extract linker paths from the link command line
  211. string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
  212. set(MPI_LINK_PATH)
  213. foreach(LPATH ${MPI_ALL_LINK_PATHS})
  214. string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
  215. string(REGEX REPLACE "//" "/" LPATH ${LPATH})
  216. list(APPEND MPI_LINK_PATH ${LPATH})
  217. endforeach(LPATH)
  218. if (NOT MPI_LINK_PATH)
  219. if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  220. # The compile command line didn't have any linking paths on it,
  221. # but we may have -showme:libdirs. Use it.
  222. exec_program(${MPI_COMPILER}
  223. ARGS -showme:libdirs
  224. OUTPUT_VARIABLE MPI_LINK_PATH
  225. RETURN_VALUE MPI_COMPILER_RETURN)
  226. separate_arguments(MPI_LINK_PATH)
  227. endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  228. endif (NOT MPI_LINK_PATH)
  229. # Extract linker flags from the link command line
  230. string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
  231. set(MPI_LINK_FLAGS_WORK)
  232. foreach(FLAG ${MPI_ALL_LINK_FLAGS})
  233. if (MPI_LINK_FLAGS_WORK)
  234. set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
  235. else(MPI_LINK_FLAGS_WORK)
  236. set(MPI_LINK_FLAGS_WORK ${FLAG})
  237. endif(MPI_LINK_FLAGS_WORK)
  238. endforeach(FLAG)
  239. # Extract the set of libraries to link against from the link command
  240. # line
  241. string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
  242. # Determine full path names for all of the libraries that one needs
  243. # to link against in an MPI program
  244. set(MPI_LIBRARIES)
  245. foreach(LIB ${MPI_LIBNAMES})
  246. string(REGEX REPLACE "^-l" "" LIB ${LIB})
  247. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
  248. find_library(MPI_LIB ${LIB} HINTS ${MPI_LINK_PATH})
  249. if (MPI_LIB)
  250. list(APPEND MPI_LIBRARIES ${MPI_LIB})
  251. else (MPI_LIB)
  252. message(SEND_ERROR "Unable to find MPI library ${LIB}")
  253. endif (MPI_LIB)
  254. endforeach(LIB)
  255. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
  256. # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
  257. # MPI_EXTRA_LIBRARY.
  258. list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
  259. list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
  260. if (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  261. list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
  262. set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
  263. else (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  264. set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
  265. endif (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  266. if (MPI_NUMLIBS GREATER 1)
  267. set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
  268. list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
  269. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
  270. else (MPI_NUMLIBS GREATER 1)
  271. set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
  272. endif (MPI_NUMLIBS GREATER 1)
  273. # Set up all of the appropriate cache entries
  274. set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
  275. set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
  276. set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
  277. else (MPI_COMPILE_CMDLINE)
  278. # No MPI compiler to interogate so attempt to find everything with find functions.
  279. find_path(MPI_INCLUDE_PATH mpi.h
  280. HINTS ${MPI_BASE_DIR} ${MPI_PREFIX_PATH}
  281. PATH_SUFFIXES include
  282. )
  283. # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
  284. if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
  285. set(MS_MPI_ARCH_DIR amd64)
  286. else()
  287. set(MS_MPI_ARCH_DIR i386)
  288. endif()
  289. find_library(MPI_LIBRARY
  290. NAMES mpi mpich msmpi
  291. HINTS ${MPI_BASE_DIR} ${MPI_PREFIX_PATH}
  292. PATH_SUFFIXES lib lib/${MS_MPI_ARCH_DIR} Lib Lib/${MS_MPI_ARCH_DIR}
  293. )
  294. find_library(MPI_EXTRA_LIBRARY
  295. NAMES mpi++
  296. HINTS ${MPI_BASE_DIR} ${MPI_PREFIX_PATH}
  297. PATH_SUFFIXES lib
  298. DOC "Extra MPI libraries to link against.")
  299. set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
  300. set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
  301. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  302. # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
  303. if("${MPI_LIBRARY}" MATCHES "mpich.rts")
  304. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  305. set(MPI_LIBRARY ${MPI_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  306. endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
  307. # Set up extra variables to conform to
  308. if (MPI_EXTRA_LIBRARY)
  309. set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
  310. else (MPI_EXTRA_LIBRARY)
  311. set(MPI_LIBRARIES ${MPI_LIBRARY})
  312. endif (MPI_EXTRA_LIBRARY)
  313. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  314. set(MPI_FOUND TRUE)
  315. else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  316. set(MPI_FOUND FALSE)
  317. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  318. include(FindPackageHandleStandardArgs)
  319. # handle the QUIETLY and REQUIRED arguments
  320. find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
  321. mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY
  322. MPI_EXTRA_LIBRARY)
  323. # unset to cleanup namespace
  324. unset(MPI_PACKAGE_DIR)
  325. unset(MPI_PREFIX_PATH)
  326. unset(MPI_BASE_DIR)