FindMPI.cmake 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # This module looks for the Message Passing Interface (MPI).
  2. #
  3. # This module will set the following variables:
  4. # MPI_FOUND TRUE if we have found MPI
  5. # MPI_COMPILE_FLAGS Compilation flags for MPI programs
  6. # MPI_INCLUDE_PATH Include path for MPI header
  7. # MPI_LINK_FLAGS Linking flags for MPI programs
  8. # MPI_LIBRARIES Libraries to link MPI programs against
  9. # MPI_LIBRARY Deprecated; first MPI library to link against
  10. # MPI_EXTRA_LIBRARY Deprecated; second MPI library to link against
  11. # MPIEXEC Executable for running MPI programs
  12. #
  13. # This module will attempt to auto-detect these settings, first by
  14. # looking for a C++ MPI driver (e.g., mpic++, mpiCC, or mpicxx; set by
  15. # MPICXX) and later by checking common MPI paths and library names.
  16. # Try to find the MPI driver program
  17. find_program(MPICXX
  18. NAMES mpic++ mpicxx mpiCC
  19. DOC "MPI C++ compiler. Used only to detect MPI compilation flags.")
  20. mark_as_advanced(MPICXX)
  21. find_program(MPIEXEC
  22. NAMES mpiexec mpirun
  23. DOC "Executable for running MPI programs.")
  24. mark_as_advanced(MPIEXEC)
  25. if (NOT OLD_MPICXX STREQUAL MPICXX)
  26. set(MPI_FORCE_RECONFIGURE TRUE)
  27. set(OLD_MPICXX ${MPICXX} CACHE INTERNAL "Previous value of MPICXX" FORCE)
  28. endif (NOT OLD_MPICXX STREQUAL MPICXX)
  29. if (NOT MPICXX)
  30. # If there is no MPI C++ compiler, we force ourselves to configure
  31. # MPI the old way.
  32. set(MPI_FORCE_RECONFIGURE TRUE)
  33. endif (NOT MPICXX)
  34. if (MPICXX)
  35. # Check whether the -showme:compile option works. This indicates
  36. # that we have either Open MPI or a newer version of LAM-MPI, and
  37. # implies that -showme:link will also work.
  38. exec_program(${MPICXX}
  39. ARGS -showme:compile
  40. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  41. RETURN_VALUE MPICXX_RETURN)
  42. if (MPICXX_RETURN EQUAL 0)
  43. # If we appear to have -showme:compile, then we should also have
  44. # -showme:link. Try it.
  45. exec_program(${MPICXX}
  46. ARGS -showme:link
  47. OUTPUT_VARIABLE MPI_LINK_CMDLINE
  48. RETURN_VALUE MPICXX_RETURN)
  49. endif (MPICXX_RETURN EQUAL 0)
  50. if (MPICXX_RETURN EQUAL 0)
  51. # Do nothing: we have our command lines now
  52. else (MPICXX_RETURN EQUAL 0)
  53. # Older versions of LAM-MPI have "-showme". Try it.
  54. exec_program(${MPICXX}
  55. ARGS -showme
  56. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  57. RETURN_VALUE MPICXX_RETURN)
  58. endif (MPICXX_RETURN EQUAL 0)
  59. if (MPICXX_RETURN EQUAL 0)
  60. # Do nothing: we have our command lines now
  61. else (MPICXX_RETURN EQUAL 0)
  62. # MPICH uses "-show". Try it.
  63. exec_program(${MPICXX}
  64. ARGS -show
  65. OUTPUT_VARIABLE MPI_COMPILE_CMDLINE
  66. RETURN_VALUE MPICXX_RETURN)
  67. endif (MPICXX_RETURN EQUAL 0)
  68. if (MPICXX_RETURN EQUAL 0)
  69. # We have our command lines, but we might need to copy
  70. # MPI_COMPILE_CMDLINE into MPI_LINK_CMDLINE, if the underlying
  71. if (NOT MPI_LINK_CMDLINE)
  72. SET(MPI_LINK_CMDLINE ${MPI_COMPILE_CMDLINE})
  73. endif (NOT MPI_LINK_CMDLINE)
  74. else (MPICXX_RETURN EQUAL 0)
  75. message(STATUS "Unable to determine MPI from MPI driver ${MPICXX}")
  76. endif (MPICXX_RETURN EQUAL 0)
  77. endif (MPICXX)
  78. if (NOT MPI_FORCE_RECONFIGURE)
  79. # We don't actually have to reconfigure anything
  80. elseif (MPI_COMPILE_CMDLINE)
  81. # Extract compile flags from the compile command line.
  82. string(REGEX MATCHALL "-D([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_FLAGS ${MPI_COMPILE_CMDLINE})
  83. set(MPI_COMPILE_FLAGS_WORK)
  84. foreach(FLAG ${MPI_ALL_COMPILE_FLAGS})
  85. if (MPI_COMPILE_FLAGS_WORK)
  86. set(MPI_COMPILE_FLAGS_WORK "${MPI_COMPILE_FLAGS_WORK} ${FLAG}")
  87. else(MPI_COMPILE_FLAGS_WORK)
  88. set(MPI_COMPILE_FLAGS_WORK ${FLAG})
  89. endif(MPI_COMPILE_FLAGS_WORK)
  90. endforeach(FLAG)
  91. # Extract include paths from compile command line
  92. string(REGEX MATCH "-I([^\" ]+|\"[^\"]+\")" MPI_INCLUDE_PATH ${MPI_COMPILE_CMDLINE})
  93. string(REGEX REPLACE "^-I" "" MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH})
  94. string(REGEX REPLACE "//" "/" MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH})
  95. # Extract linker paths from the link command line
  96. string(REGEX MATCH "-L([^\" ]+|\"[^\"]+\")" MPI_LINK_PATH ${MPI_LINK_CMDLINE})
  97. string(REGEX REPLACE "^-L" "" MPI_LINK_PATH ${MPI_LINK_PATH})
  98. string(REGEX REPLACE "//" "/" MPI_LINK_PATH ${MPI_LINK_PATH})
  99. # Extract linker flags from the link command line
  100. string(REGEX MATCHALL "-Wl,([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS ${MPI_LINK_CMDLINE})
  101. set(MPI_LINK_FLAGS_WORK)
  102. foreach(FLAG ${MPI_ALL_LINK_FLAGS})
  103. if (MPI_LINK_FLAGS_WORK)
  104. set(MPI_LINK_FLAGS_WORK "${MPI_LINK_FLAGS_WORK} ${FLAG}")
  105. else(MPI_LINK_FLAGS_WORK)
  106. set(MPI_LINK_FLAGS_WORK ${FLAG})
  107. endif(MPI_LINK_FLAGS_WORK)
  108. endforeach(FLAG)
  109. # Extract the set of libraries to link against from the link command
  110. # line
  111. string(REGEX MATCHALL "-l([^\" ]+|\"[^\"]+\")" MPI_LIBNAMES ${MPI_LINK_CMDLINE})
  112. # Determine full path names for all of the libraries that one needs
  113. # to link against in an MPI program
  114. set(MPI_LIBRARIES)
  115. foreach(LIB ${MPI_LIBNAMES})
  116. string(REGEX REPLACE "^-l" "" LIB ${LIB})
  117. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
  118. find_library(MPI_LIB ${LIB} PATHS ${MPI_LINK_PATH})
  119. if (MPI_LIB)
  120. list(APPEND MPI_LIBRARIES ${MPI_LIB})
  121. else (MPI_LIB)
  122. status(ERROR "Unable to find MPI library ${LIB}")
  123. endif (MPI_LIB)
  124. endforeach(LIB)
  125. set(MPI_LIB "MPI_LIB-NOTFOUND" CACHE INTERNAL "Scratch variable for MPI detection" FORCE)
  126. # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and
  127. # MPI_EXTRA_LIBRARY.
  128. list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
  129. if (MPI_NUMLIBS GREATER 0)
  130. list(GET MPI_LIBRARIES 0 MPI_LIBRARY)
  131. else (MPI_NUMLIBS GREATER 0)
  132. set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND")
  133. endif (MPI_NUMLIBS GREATER 0)
  134. if (MPI_NUMLIBS GREATER 1)
  135. cdr(MPI_EXTRA_LIBRARY ${MPI_LIBRARIES})
  136. else (MPI_NUMLIBS GREATER 1)
  137. set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND")
  138. endif (MPI_NUMLIBS GREATER 1)
  139. # Set up all of the appropriate cache entries
  140. set(MPI_FOUND TRUE CACHE INTERNAL "Whether MPI was found" FORCE)
  141. if (NOT MPI_FORCE_RECONFIGURE)
  142. set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags")
  143. set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH} CACHE STRING "MPI include path")
  144. set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags")
  145. set(MPI_LIBRARIES ${MPI_LIBRARIES} CACHE STRING "MPI libraries to link against, separated by semicolons")
  146. else (NOT MPI_FORCE_RECONFIGURE)
  147. set(MPI_COMPILE_FLAGS ${MPI_COMPILE_FLAGS_WORK} CACHE STRING "MPI compilation flags" FORCE)
  148. set(MPI_INCLUDE_PATH ${MPI_INCLUDE_PATH} CACHE STRING "MPI include path" FORCE)
  149. set(MPI_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI linking flags" FORCE)
  150. set(MPI_LIBRARIES ${MPI_LIBRARIES} CACHE STRING "MPI libraries to link against, separated by semicolons" FORCE)
  151. endif (NOT MPI_FORCE_RECONFIGURE)
  152. else (MPI_COMPILE_CMDLINE)
  153. find_path(MPI_INCLUDE_PATH mpi.h
  154. /usr/local/include
  155. /usr/include
  156. /usr/include/mpi
  157. /usr/local/mpi/include
  158. "C:/Program Files/MPICH/SDK/Include"
  159. "$ENV{SystemDrive}/Program Files/MPICH2/include"
  160. "C:/Program Files/Microsoft Compute Cluster Pack/Include"
  161. )
  162. # TODO: How do we know whether we're building 32-bit vs. 64-bit?
  163. find_library(MPI_LIBRARY
  164. NAMES mpi mpich
  165. PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
  166. "C:/Program Files/MPICH/SDK/Lib"
  167. "$ENV{SystemDrive}/Program Files/MPICH/SDK/Lib"
  168. "C:/Program Files/Microsoft Compute Cluster Pack/Lib/i386"
  169. )
  170. find_library(MPI_LIBRARY
  171. NAMES mpich2
  172. PATHS
  173. "$ENV{SystemDrive}/Program Files/MPICH2/Lib")
  174. find_library(MPI_EXTRA_LIBRARY
  175. NAMES mpi++
  176. PATHS /usr/lib /usr/local/lib /usr/local/mpi/lib
  177. "C:/Program Files/MPICH/SDK/Lib"
  178. "C:/Program Files/Microsoft Compute Cluster Pack/Lib/i386"
  179. DOC "If a second MPI library is necessary, specify it here.")
  180. set(MPI_COMPILE_FLAGS "" CACHE STRING "MPI compilation flags")
  181. set(MPI_LINK_FLAGS "" CACHE STRING "MPI linking flags")
  182. if (MPI_EXTRA_LIBRARY)
  183. set(MPI_LIBRARIES "${MPI_LIBRARY};${MPI_EXTRA_LIBRARY}" CACHE STRING "MPI libraries to link against, separated by semicolons")
  184. else (MPI_EXTRA_LIBRARY)
  185. set(MPI_LIBRARIES ${MPI_LIBRARY} CACHE STRING "MPI libraries to link against, separated by semicolons")
  186. endif (MPI_EXTRA_LIBRARY)
  187. if (MPI_LIBRARY)
  188. set(MPI_FOUND TRUE CACHE INTERNAL "Whether MPI was found" FORCE)
  189. else (MPI_LIBRARY)
  190. set(MPI_FOUND FALSE CACHE INTERNAL "Whether MPI was found" FORCE)
  191. endif (MPI_LIBRARY)
  192. endif (NOT MPI_FORCE_RECONFIGURE)
  193. # on BlueGene/L the MPI lib is named libmpich.rts.a, there also these additional libs are required
  194. if("${MPI_LIBRARY}" MATCHES "mpich.rts")
  195. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  196. set(MPI_LIBRARY ${MPI_LIBRARY} msglayer.rts devices.rts rts.rts devices.rts)
  197. endif("${MPI_LIBRARY}" MATCHES "mpich.rts")
  198. set(MPI_LIBRARY ${MPI_LIBRARY} CACHE INTERNAL "MPI library to link against. Deprecated: use MPI_LIBRARIES instead")
  199. set(MPI_EXTRA_LIBRARY ${MPI_EXTRA_LIBRARY} CACHE INTERNAL "Second MPI library to link against. Deprecated: use MPI_LIBRARIES instead")
  200. include(FindPackageHandleStandardArgs)
  201. # handle the QUIETLY and REQUIRED arguments
  202. find_package_handle_standard_args(MPI DEFAULT_MSG MPI_LIBRARY MPI_INCLUDE_PATH)
  203. mark_as_advanced(MPI_INCLUDE_PATH MPI_COMPILE_FLAGS MPI_LINK_FLAGS MPI_LIBRARIES)