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