1ae95db1a1cc6ff15f9ec291d2b70ca11c500a17
[alexxy/gromacs.git] / cmake / gmxManageMPI.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 # Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 if (GMX_MPI)
37     if (GMX_THREAD_MPI)
38         message(STATUS "MPI is not compatible with thread-MPI. Disabling thread-MPI.")
39         set(GMX_THREAD_MPI OFF CACHE BOOL
40             "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" FORCE)
41     endif ()
42     set(GMX_LIB_MPI 1)
43 else ()
44     set(GMX_LIB_MPI 0)
45 endif ()
46
47 # Manage the MPI setup.
48 # Note that we may want to execute tests or Python with MPI,
49 # even if we are not using an MPI-enabled GROMACS build.
50 #TODO(#3672): find_package(MPI COMPONENTS ...)
51 find_package(MPI)
52 if (GMX_LIB_MPI)
53     if (NOT MPI_CXX_FOUND)
54         message(FATAL_ERROR
55                 "MPI support requested, but no suitable MPI compiler found. Either set the "
56                 "MPI_CXX_COMPILER to the MPI compiler wrapper (often called mpicxx or mpic++), "
57                 "set CMAKE_CXX_COMPILER to a default-MPI-enabled compiler, "
58                 "or set the variables reported missing for MPI_CXX above.")
59     elseif (MPI_CXX_VERSION VERSION_LESS 2.0)
60         message(FATAL_ERROR "MPI version 2.0 or higher is required. Please update your MPI library.")
61     endif ()
62     # TODO(#3672): Replace usage of `MPI_LINKER_FLAGS` with MPI_LINK_FLAGS,
63     #   MPI_<lang>_LINK_FLAGS, or target_link_libraries(... MPI::MPI_CXX)
64     set(MPI_LINKER_FLAGS ${MPI_CXX_LINK_FLAGS})
65     separate_arguments(MPI_CXX_LINK_FLAGS)
66     #TODO(#3672, #3776): These should be acquired through the MPI::MPI_CXX target.
67     include_directories(SYSTEM ${MPI_CXX_INCLUDE_PATH})
68     list(APPEND GMX_COMMON_LIBRARIES ${MPI_CXX_LIBRARIES})
69 endif ()
70
71 if (GMX_LIB_MPI)
72     # Test for and warn about unsuitable MPI versions
73     # Find path of the mpi compilers
74     if (${MPI_CXX_FOUND})
75         get_filename_component(_mpi_c_compiler_path "${MPI_CXX_COMPILER}" PATH)
76         get_filename_component(_mpiexec_path "${MPIEXEC_EXECUTABLE}" PATH)
77     else ()
78         get_filename_component(_cmake_c_compiler_path "${CMAKE_C_COMPILER}" PATH)
79         get_filename_component(_cmake_cxx_compiler_path "${CMAKE_CXX_COMPILER}" PATH)
80     endif ()
81
82     # Execute the ompi_info binary with the full path of the compiler wrapper
83     # found, otherwise we run the risk of false positives.
84     find_file(MPI_INFO_BIN ompi_info
85               HINTS ${_mpi_c_compiler_path} ${_mpiexec_path}
86               ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
87               NO_DEFAULT_PATH
88               NO_SYSTEM_ENVIRONMENT_PATH
89               NO_CMAKE_SYSTEM_PATH)
90     if (MPI_INFO_BIN)
91         exec_program(${MPI_INFO_BIN}
92                      ARGS -v ompi full
93                      OUTPUT_VARIABLE OPENMPI_TYPE
94                      RETURN_VALUE OPENMPI_EXEC_RETURN)
95         if (OPENMPI_EXEC_RETURN EQUAL 0)
96             string(REGEX REPLACE ".*Open MPI: \([0-9]+\\.[0-9]*\\.?[0-9]*\).*" "\\1" OPENMPI_VERSION ${OPENMPI_TYPE})
97             if (OPENMPI_VERSION VERSION_LESS "1.4.1")
98                 MESSAGE(WARNING
99                         "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
100                         "There are known problems with GROMACS and OpenMPI version < 1.4.1. "
101                         "Please consider updating your OpenMPI if your MPI wrapper compilers "
102                         "are using the above OpenMPI version.")
103             endif ()
104             if (OPENMPI_VERSION VERSION_EQUAL "1.8.6")
105                 MESSAGE(WARNING
106                         "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
107                         "This OpenMPI version is known to leak memory with GROMACS,"
108                         "please update to a more recent version. ")
109             endif ()
110             unset(OPENMPI_VERSION)
111             unset(OPENMPI_TYPE)
112             unset(OPENMPI_EXEC_RETURN)
113         endif ()
114     endif ()
115     unset(MPI_INFO_BIN CACHE)
116
117     # Execute the mpiname binary with the full path of the compiler wrapper
118     # found, otherwise we run the risk of false positives.
119     find_file(MPINAME_BIN mpiname
120               HINTS ${_mpi_c_compiler_path}
121               ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
122               NO_DEFAULT_PATH
123               NO_SYSTEM_ENVIRONMENT_PATH
124               NO_CMAKE_SYSTEM_PATH)
125     if (MPINAME_BIN)
126         exec_program(${MPINAME_BIN}
127                      ARGS -n -v
128                      OUTPUT_VARIABLE MVAPICH2_TYPE
129                      RETURN_VALUE MVAPICH2_EXEC_RETURN)
130         if (MVAPICH2_EXEC_RETURN EQUAL 0)
131             string(REGEX MATCH "MVAPICH2" MVAPICH2_NAME ${MVAPICH2_TYPE})
132             # Want to check for MVAPICH2 in case some other library supplies mpiname
133             string(REGEX REPLACE "MVAPICH2 \([0-9]+\\.[0-9]*[a-z]?\\.?[0-9]*\)" "\\1" MVAPICH2_VERSION ${MVAPICH2_TYPE})
134             if (${MVAPICH2_NAME} STREQUAL "MVAPICH2" AND MVAPICH2_VERSION VERSION_LESS "1.5")
135                 # This test works correctly even with 1.5a1
136                 MESSAGE(WARNING
137                         "CMake found MVAPICH2 version ${MVAPICH2_VERSION} on your system. "
138                         "There are known problems with GROMACS and MVAPICH2 version < 1.5. "
139                         "Please consider updating your MVAPICH2 if your MPI wrapper compilers "
140                         "are using the above MVAPICH2 version.")
141             endif ()
142             unset(MVAPICH2_VERSION)
143             unset(MVAPICH2_NAME)
144             unset(MVAPICH2_TYPE)
145             unset(MVAPICH2_EXEC_RETURN)
146         endif ()
147     endif ()
148     unset(MPINAME_BIN CACHE)
149 endif () # end if(GMX_LIB_MPI)
150
151 # Look for MPI process launchers that may be missed, especially if we didn't
152 # need to find the full MPI library build system support.
153 if (NOT MPIEXEC_EXECUTABLE)
154     find_program(MPIEXEC
155                  NAMES mpiexec mpirun lamexec srun aprun poe
156                  HINTS ${MPI_HOME} $ENV{MPI_HOME}
157                  PATH_SUFFIXES bin
158                  DOC "Executable for running MPI programs.")
159
160     set(MPIEXEC_EXECUTABLE "$MPIEXEC")
161     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.")
162     set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
163     set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
164     set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
165     mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
166 endif ()