FindMPI.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. # Try to find the MPI driver program
  55. find_program(MPI_COMPILER
  56. NAMES mpic++ mpicxx mpiCC mpicc
  57. DOC "MPI compiler. Used only to detect MPI compilation flags.")
  58. mark_as_advanced(MPI_COMPILER)
  59. file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles)
  60. find_program(MPIEXEC
  61. NAMES mpiexec mpirun lamexec
  62. PATHS /usr/bin /usr/local/bin /usr/local/mpi/bin
  63. "$ENV{SystemDrive}/Program Files/MPICH/SDK/Bin"
  64. "${ProgramFiles}/MPICH2/Bin
  65. "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Bin"
  66. "$ENV{SystemDrive}/Program Files/Microsoft HPC Pack 2008 SDK/Bin"
  67. DOC "Executable for running MPI programs.")
  68. 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.")
  69. set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
  70. set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
  71. set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
  72. mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
  73. MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
  74. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  75. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  76. # the cache, and we don't want to override those settings.
  77. elseif (MPI_COMPILER)
  78. # Check whether the -showme:compile option works. This indicates
  79. # that we have either Open MPI or a newer version of LAM-MPI, and
  80. # implies that -showme:link will also work.
  81. exec_program(${MPI_COMPILER}
  82. ARGS -showme:compile
  83. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  84. RETURN_VALUE MPI_COMPILER_RETURN)
  85. if (MPI_COMPILER_RETURN EQUAL 0)
  86. # If we appear to have -showme:compile, then we should also have
  87. # -showme:link. Try it.
  88. exec_program(${MPI_COMPILER}
  89. ARGS -showme:link
  90. OUTPUT_VARIABLE MPI_LINK_CMDLINE
  91. RETURN_VALUE MPI_COMPILER_RETURN)
  92. # Note that we probably have -showme:incdirs and -showme:libdirs
  93. # as well.
  94. set(MPI_COMPILER_MAY_HAVE_INCLIBDIRS TRUE)
  95. endif (MPI_COMPILER_RETURN EQUAL 0)
  96. if (MPI_COMPILER_RETURN EQUAL 0)
  97. # Do nothing: we have our command lines now
  98. else (MPI_COMPILER_RETURN EQUAL 0)
  99. # Older versions of LAM-MPI have "-showme". Try it.
  100. exec_program(${MPI_COMPILER}
  101. ARGS -showme
  102. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  103. RETURN_VALUE MPI_COMPILER_RETURN)
  104. endif (MPI_COMPILER_RETURN EQUAL 0)
  105. if (MPI_COMPILER_RETURN EQUAL 0)
  106. # Do nothing: we have our command lines now
  107. else (MPI_COMPILER_RETURN EQUAL 0)
  108. # MPICH uses "-show". Try it.
  109. exec_program(${MPI_COMPILER}
  110. ARGS -show
  111. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  112. RETURN_VALUE MPI_COMPILER_RETURN)
  113. endif (MPI_COMPILER_RETURN EQUAL 0)
  114. if (MPI_COMPILER_RETURN EQUAL 0)
  115. # We have our command lines, but we might need to copy
  116. # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
  117. if (NOT MPI_LINK_CMDLINE)
  118. SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
  119. endif (NOT MPI_LINK_CMDLINE)
  120. else (MPI_COMPILER_RETURN EQUAL 0)
  121. message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
  122. endif (MPI_COMPILER_RETURN EQUAL 0)
  123. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  124. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  125. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  126. # the cache, and we don't want to override those settings.
  127. elseif (MPI_COMPILE_CMDLINE)
  128. # Extract compile flags from the compile command line.
  129. string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
  130. set(MPI_COMPILE_FLAGS_WORK)
  131. foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
  132. if (MPI_COMPILE_FLAGS_WORK)
  133. set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
  134. else(MPI_COMPILE_FLAGS_WORK)
  135. set(MPI_COMPILE_FLAGS_WORK ${FLAG})
  136. endif(MPI_COMPILE_FLAGS_WORK)
  137. endforeach(FLAG)
  138. # Extract include paths from compile command line
  139. string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
  140. set(MPI_INCLUDE_PATH_WORK)
  141. foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
  142. string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
  143. string(REGEX REPLACE "//" "/" IPATH ${IPATH})
  144. list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
  145. endforeach(IPATH)
  146. if (NOT MPI_INCLUDE_PATH_WORK)
  147. if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  148. # The compile command line didn't have any include paths on it,
  149. # but we may have -showme:incdirs. Use it.
  150. exec_program(${MPI_COMPILER}
  151. ARGS -showme:incdirs
  152. OUTPUT_VARIABLE MPI_INCLUDE_PATH_WORK
  153. RETURN_VALUE MPI_COMPILER_RETURN)
  154. separate_arguments(MPI_INCLUDE_PATH_WORK)
  155. endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  156. endif (NOT MPI_INCLUDE_PATH_WORK)
  157. if (NOT MPI_INCLUDE_PATH_WORK)
  158. # If all else fails, just search for mpi.h in the normal include
  159. # paths.
  160. find_path(MPI_INCLUDE_PATH mpi.h)
  161. set(MPI_INCLUDE_PATH_WORK ${MPI_INCLUDE_PATH})
  162. endif (NOT MPI_INCLUDE_PATH_WORK)
  163. # Extract linker paths from the link command line
  164. string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
  165. set(MPI_LINK_PATH)
  166. foreach(LPATH ${MPI_ALL_LINK_PATHS})
  167. string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
  168. string(REGEX REPLACE "//" "/" LPATH ${LPATH})
  169. list(APPEND MPI_LINK_PATH ${LPATH})
  170. endforeach(LPATH)
  171. if (NOT MPI_LINK_PATH)
  172. if (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  173. # The compile command line didn't have any linking paths on it,
  174. # but we may have -showme:libdirs. Use it.
  175. exec_program(${MPI_COMPILER}
  176. ARGS -showme:libdirs
  177. OUTPUT_VARIABLE MPI_LINK_PATH
  178. RETURN_VALUE MPI_COMPILER_RETURN)
  179. separate_arguments(MPI_LINK_PATH)
  180. endif (MPI_COMPILER_MAY_HAVE_INCLIBDIRS)
  181. endif (NOT MPI_LINK_PATH)
  182. # Extract linker flags from the link command line
  183. string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
  184. set(MPI_LINK_FLAGS_WORK)
  185. foreach(FLAG ${MPI_ALL_LINK_FLAGS})
  186. if (MPI_LINK_FLAGS_WORK)
  187. set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
  188. else(MPI_LINK_FLAGS_WORK)
  189. set(MPI_LINK_FLAGS_WORK ${FLAG})
  190. endif(MPI_LINK_FLAGS_WORK)
  191. endforeach(FLAG)
  192. # Extract the set of libraries to link against from the link command
  193. # line
  194. string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
  195. # Determine full path names for all of the libraries that one needs
  196. # to link against in an MPI program
  197. set(MPI_LIBRARIES)
  198. foreach(LIB ${MPI_LIBNAMES})
  199. string(REGEX REPLACE "^-l" "" LIB ${LIB})
  200. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
  201. find_library(MPI_LIB ${LIB} HINTS ${MPI_LINK_PATH})
  202. if (MPI_LIB)
  203. list(APPEND MPI_LIBRARIES ${MPI_LIB})
  204. else (MPI_LIB)
  205. message(SEND_ERROR "Unable to find MPI library ${LIB}")
  206. endif (MPI_LIB)
  207. endforeach(LIB)
  208. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
  209. # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
  210. # MPI_EXTRA_LIBRARY.
  211. list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
  212. list(LENGTH MPI_LIBNAMES MPI_NUMLIBS_EXPECTED)
  213. if (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  214. list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
  215. set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
  216. else (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  217. set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE FILEPATH "MPI library to link against" FORCE)
  218. endif (MPI_NUMLIBS EQUAL MPI_NUMLIBS_EXPECTED)
  219. if (MPI_NUMLIBS GREATER 1)
  220. set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
  221. list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
  222. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
  223. else (MPI_NUMLIBS GREATER 1)
  224. set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
  225. endif (MPI_NUMLIBS GREATER 1)
  226. # Set up all of the appropriate cache entries
  227. set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
  228. set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
  229. set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
  230. else (MPI_COMPILE_CMDLINE)
  231. find_path(MPI_INCLUDE_PATH mpi.h
  232. /usr/local/include
  233. /usr/include
  234. /usr/include/mpi
  235. /usr/local/mpi/include
  236. "$ENV{SystemDrive}/Program Files/MPICH/SDK/Include"
  237. "${ProgramFiles}/MPICH2/include"
  238. "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Include"
  239. "$ENV{SystemDrive}/Program Files/Microsoft HPC Pack 2008 SDK/Include"
  240. )
  241. # Decide between 32-bit and 64-bit libraries for Microsoft's MPI
  242. if (CMAKE_CL_64)
  243. set(MS_MPI_ARCH_DIR amd64)
  244. else (CMAKE_CL_64)
  245. set(MS_MPI_ARCH_DIR i386)
  246. endif (CMAKE_CL_64)
  247. find_library(MPI_LIBRARY
  248. NAMES mpi mpich msmpi
  249. PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
  250. "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
  251. "${ProgramFiles}/MPICH2/Lib
  252. "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Lib/${MS_MPI_ARCH_DIR}"
  253. "$ENV{SystemDrive}/Program Files/Microsoft HPC Pack 2008 SDK/Lib/${MS_MPI_ARCH_DIR}"
  254. )
  255. find_library(MPI_LIBRARY
  256. NAMES mpich2
  257. PATHS
  258. "${ProgramFiles}/MPICH2/Lib")
  259. find_library(MPI_EXTRA_LIBRARY
  260. NAMES mpi++
  261. PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
  262. "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
  263. DOC "Extra MPI libraries to link against.")
  264. set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
  265. set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
  266. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  267. # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
  268. if("${MPI_LIBRARY}" MATCHES "mpich.rts")
  269. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  270. set(MPI_LIBRARY ${MPI_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  271. endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
  272. # Set up extra variables to conform to
  273. if (MPI_EXTRA_LIBRARY)
  274. set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
  275. else (MPI_EXTRA_LIBRARY)
  276. set(MPI_LIBRARIES ${MPI_LIBRARY})
  277. endif (MPI_EXTRA_LIBRARY)
  278. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  279. set(MPI_FOUND TRUE)
  280. else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  281. set(MPI_FOUND FALSE)
  282. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  283. include(FindPackageHandleStandardArgs)
  284. # handle the QUIETLY and REQUIRED arguments
  285. find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
  286. mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY
  287. MPI_EXTRA_LIBRARY)