FindMPI.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. find_program(MPIEXEC
  60. NAMES mpiexec mpirun lamexec
  61. DOC "Executable for running MPI programs.")
  62. 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.")
  63. set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
  64. set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
  65. set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
  66. mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS
  67. MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
  68. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  69. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  70. # the cache, and we don't want to override those settings.
  71. elseif (MPI_COMPILER)
  72. # Check whether the -showme:compile option works. This indicates
  73. # that we have either Open MPI or a newer version of LAM-MPI, and
  74. # implies that -showme:link will also work.
  75. exec_program(${MPI_COMPILER}
  76. ARGS -showme:compile
  77. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  78. RETURN_VALUE MPI_COMPILER_RETURN)
  79. if (MPI_COMPILER_RETURN EQUAL 0)
  80. # If we appear to have -showme:compile, then we should also have
  81. # -showme:link. Try it.
  82. exec_program(${MPI_COMPILER}
  83. ARGS -showme:link
  84. OUTPUT_VARIABLE MPI_LINK_CMDLINE
  85. RETURN_VALUE MPI_COMPILER_RETURN)
  86. endif (MPI_COMPILER_RETURN EQUAL 0)
  87. if (MPI_COMPILER_RETURN EQUAL 0)
  88. # Do nothing: we have our command lines now
  89. else (MPI_COMPILER_RETURN EQUAL 0)
  90. # Older versions of LAM-MPI have "-showme". Try it.
  91. exec_program(${MPI_COMPILER}
  92. ARGS -showme
  93. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  94. RETURN_VALUE MPI_COMPILER_RETURN)
  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. # MPICH uses "-show". Try it.
  100. exec_program(${MPI_COMPILER}
  101. ARGS -show
  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. # We have our command lines, but we might need to copy
  107. # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
  108. if (NOT MPI_LINK_CMDLINE)
  109. SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
  110. endif (NOT MPI_LINK_CMDLINE)
  111. else (MPI_COMPILER_RETURN EQUAL 0)
  112. message(STATUS "Unable to determine MPI from MPI driver ${MPI_COMPILER}")
  113. endif (MPI_COMPILER_RETURN EQUAL 0)
  114. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  115. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  116. # Do nothing: we already have MPI_INCLUDE_PATH and MPI_LIBRARY in
  117. # the cache, and we don't want to override those settings.
  118. elseif (MPI_COMPILE_CMDLINE)
  119. # Extract compile flags from the compile command line.
  120. string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS "${MPI_COMPILE_CMDLINE}")
  121. set(MPI_COMPILE_FLAGS_WORK)
  122. foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
  123. if (MPI_COMPILE_FLAGS_WORK)
  124. set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
  125. else(MPI_COMPILE_FLAGS_WORK)
  126. set(MPI_COMPILE_FLAGS_WORK ${FLAG})
  127. endif(MPI_COMPILE_FLAGS_WORK)
  128. endforeach(FLAG)
  129. # Extract include paths from compile command line
  130. string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
  131. set(MPI_INCLUDE_PATH_WORK)
  132. foreach(IPATH ${MPI_ALL_INCLUDE_PATHS})
  133. string(REGEX REPLACE "^-I" "" IPATH ${IPATH})
  134. string(REGEX REPLACE "//" "/" IPATH ${IPATH})
  135. list(APPEND MPI_INCLUDE_PATH_WORK ${IPATH})
  136. endforeach(IPATH)
  137. # Extract linker paths from the link command line
  138. string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_PATHS "${MPI_LINK_CMDLINE}")
  139. set(MPI_LINK_PATH)
  140. foreach(LPATH ${MPI_ALL_LINK_PATHS})
  141. string(REGEX REPLACE "^-L" "" LPATH ${LPATH})
  142. string(REGEX REPLACE "//" "/" LPATH ${LPATH})
  143. list(APPEND MPI_LINK_PATH ${LPATH})
  144. endforeach(LPATH)
  145. # Extract linker flags from the link command line
  146. string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE}")
  147. set(MPI_LINK_FLAGS_WORK)
  148. foreach(FLAG ${MPI_ALL_LINK_FLAGS})
  149. if (MPI_LINK_FLAGS_WORK)
  150. set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
  151. else(MPI_LINK_FLAGS_WORK)
  152. set(MPI_LINK_FLAGS_WORK ${FLAG})
  153. endif(MPI_LINK_FLAGS_WORK)
  154. endforeach(FLAG)
  155. # Extract the set of libraries to link against from the link command
  156. # line
  157. string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
  158. # Determine full path names for all of the libraries that one needs
  159. # to link against in an MPI program
  160. set(MPI_LIBRARIES)
  161. foreach(LIB ${MPI_LIBNAMES})
  162. string(REGEX REPLACE "^-l" "" LIB ${LIB})
  163. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
  164. find_library(MPI_LIB ${LIB} PATHS ${MPI_LINK_PATH})
  165. if (MPI_LIB)
  166. list(APPEND MPI_LIBRARIES ${MPI_LIB})
  167. else (MPI_LIB)
  168. status(ERROR "Unable to find MPI library ${LIB}")
  169. endif (MPI_LIB)
  170. endforeach(LIB)
  171. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
  172. # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
  173. # MPI_EXTRA_LIBRARY.
  174. list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
  175. if (MPI_NUMLIBS GREATER 0)
  176. list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
  177. set(MPI_LIBRARY ${MPI_LIBRARY_WORK} CACHE FILEPATH "MPI library to link against" FORCE)
  178. else (MPI_NUMLIBS GREATER 0)
  179. set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND" CACHE STRING "MPI library to link against" FORCE)
  180. endif (MPI_NUMLIBS GREATER 0)
  181. if (MPI_NUMLIBS GREATER 1)
  182. set(MPI_EXTRA_LIBRARY_WORK ${MPI_LIBRARIES})
  183. list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
  184. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY_WORK} CACHE STRING "Extra MPI libraries to link against" FORCE)
  185. else (MPI_NUMLIBS GREATER 1)
  186. set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND" CACHE STRING "Extra MPI libraries to link against" FORCE)
  187. endif (MPI_NUMLIBS GREATER 1)
  188. # Set up all of the appropriate cache entries
  189. set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
  190. set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH_WORK} CACHE STRING "MPI include path" FORCE)
  191. set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
  192. else (MPI_COMPILE_CMDLINE)
  193. find_path(MPI_INCLUDE_PATH mpi.h
  194. /usr/local/include
  195. /usr/include
  196. /usr/include/mpi
  197. /usr/local/mpi/include
  198. "C:/Program Files/MPICH/SDK/Include"
  199. "$ENV{SystemDrive}/Program Files/MPICH2/include"
  200. "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Include"
  201. )
  202. # TODO: How do we know whether we're building 32-bit vs. 64-bit for MS-MPI?
  203. find_library(MPI_LIBRARY
  204. NAMES mpi mpich
  205. PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
  206. "C:/Program Files/MPICH/SDK/Lib"
  207. "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
  208. "$ENV{SystemDrive}/Program Files/Microsoft Compute Cluster Pack/Lib/i386"
  209. )
  210. find_library(MPI_LIBRARY
  211. NAMES mpich2
  212. PATHS
  213. "$ENV{SystemDrive}/Program Files/MPICH2/Lib")
  214. find_library(MPI_EXTRA_LIBRARY
  215. NAMES mpi++
  216. PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
  217. "C:/Program Files/MPICH/SDK/Lib"
  218. DOC "Extra MPI libraries to link against.")
  219. set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
  220. set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
  221. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  222. # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
  223. if("${MPI_LIBRARY}" MATCHES "mpich.rts")
  224. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  225. set(MPI_LIBRARY ${MPI_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  226. endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
  227. # Set up extra variables to conform to
  228. if (MPI_EXTRA_LIBRARY)
  229. set(MPI_LIBRARIES ${MPI_LIBRARY} ${MPI_EXTRA_LIBRARY})
  230. else (MPI_EXTRA_LIBRARY)
  231. set(MPI_LIBRARIES ${MPI_LIBRARY})
  232. endif (MPI_EXTRA_LIBRARY)
  233. if (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  234. set(MPI_FOUND TRUE)
  235. else (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  236. set(MPI_FOUND FALSE)
  237. endif (MPI_INCLUDE_PATH AND MPI_LIBRARY)
  238. include(FindPackageHandleStandardArgs)
  239. # handle the QUIETLY and REQUIRED arguments
  240. find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
  241. mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARY
  242. MPI_EXTRA_LIBRARY)