46f39ef480ccfde20d6df8f8967b786079e8a013
[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 set(GMX_REQUIRED_MPI_COMPONENTS)
51 if (GMX_LIB_MPI OR GMXAPI)
52     # If we are building GROMACS against an MPI library, we need the CXX component.
53     # If the gmxapi interfaces are to be installed, we want to try to help client
54     # software to find a compatible MPI toolchain, regardless of the libgromacs configuration.
55     list(APPEND GMX_REQUIRED_MPI_COMPONENTS "CXX")
56 endif()
57 if (GMX_LIB_MPI AND GMX_CP2K)
58     list(APPEND GMX_REQUIRED_MPI_COMPONENTS "Fortran")
59 endif ()
60 # We can perform the find_package quietly because we report errors elsewhere
61 # when we can't find a required component, but the MPI target is optional
62 # in some build configurations (e.g. thread-MPI gmxapi installations).
63 find_package(MPI QUIET COMPONENTS ${GMX_REQUIRED_MPI_COMPONENTS})
64 if (GMX_LIB_MPI)
65     if (NOT MPI_CXX_FOUND)
66         message(FATAL_ERROR
67                 "MPI support requested, but no suitable MPI compiler found. Either set the "
68                 "MPI_CXX_COMPILER to the MPI compiler wrapper (often called mpicxx or mpic++), "
69                 "set CMAKE_CXX_COMPILER to a default-MPI-enabled compiler, "
70                 "or set the variables reported missing for MPI_CXX above.")
71     elseif (MPI_CXX_VERSION VERSION_LESS 2.0)
72         message(FATAL_ERROR "MPI version 2.0 or higher is required. Please update your MPI library.")
73     endif ()
74     #TODO(#3672, #3776): These should be acquired through the MPI::MPI_CXX target.
75     include_directories(SYSTEM ${MPI_CXX_INCLUDE_PATH})
76     list(APPEND GMX_COMMON_LIBRARIES ${MPI_CXX_LIBRARIES})
77 endif ()
78
79 if (GMX_LIB_MPI)
80     # Test for and warn about unsuitable MPI versions
81     # Find path of the mpi compilers
82     if (${MPI_CXX_FOUND})
83         get_filename_component(_mpi_c_compiler_path "${MPI_CXX_COMPILER}" PATH)
84         get_filename_component(_mpiexec_path "${MPIEXEC_EXECUTABLE}" PATH)
85     else ()
86         get_filename_component(_cmake_c_compiler_path "${CMAKE_C_COMPILER}" PATH)
87         get_filename_component(_cmake_cxx_compiler_path "${CMAKE_CXX_COMPILER}" PATH)
88     endif ()
89
90     # Execute the ompi_info binary with the full path of the compiler wrapper
91     # found, otherwise we run the risk of false positives.
92     find_file(MPI_INFO_BIN ompi_info
93               HINTS ${_mpi_c_compiler_path} ${_mpiexec_path}
94               ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
95               NO_DEFAULT_PATH
96               NO_SYSTEM_ENVIRONMENT_PATH
97               NO_CMAKE_SYSTEM_PATH)
98     if (MPI_INFO_BIN)
99         exec_program(${MPI_INFO_BIN}
100                      ARGS -v ompi full
101                      OUTPUT_VARIABLE OPENMPI_TYPE
102                      RETURN_VALUE OPENMPI_EXEC_RETURN)
103         if (OPENMPI_EXEC_RETURN EQUAL 0)
104             string(REGEX REPLACE ".*Open MPI: \([0-9]+\\.[0-9]*\\.?[0-9]*\).*" "\\1" OPENMPI_VERSION ${OPENMPI_TYPE})
105             if (OPENMPI_VERSION VERSION_LESS "1.4.1")
106                 MESSAGE(WARNING
107                         "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
108                         "There are known problems with GROMACS and OpenMPI version < 1.4.1. "
109                         "Please consider updating your OpenMPI if your MPI wrapper compilers "
110                         "are using the above OpenMPI version.")
111             endif ()
112             if (OPENMPI_VERSION VERSION_EQUAL "1.8.6")
113                 MESSAGE(WARNING
114                         "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
115                         "This OpenMPI version is known to leak memory with GROMACS,"
116                         "please update to a more recent version. ")
117             endif ()
118             unset(OPENMPI_VERSION)
119             unset(OPENMPI_TYPE)
120             unset(OPENMPI_EXEC_RETURN)
121         endif ()
122     endif ()
123     unset(MPI_INFO_BIN CACHE)
124
125     # Execute the mpiname binary with the full path of the compiler wrapper
126     # found, otherwise we run the risk of false positives.
127     find_file(MPINAME_BIN mpiname
128               HINTS ${_mpi_c_compiler_path}
129               ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
130               NO_DEFAULT_PATH
131               NO_SYSTEM_ENVIRONMENT_PATH
132               NO_CMAKE_SYSTEM_PATH)
133     if (MPINAME_BIN)
134         exec_program(${MPINAME_BIN}
135                      ARGS -n -v
136                      OUTPUT_VARIABLE MVAPICH2_TYPE
137                      RETURN_VALUE MVAPICH2_EXEC_RETURN)
138         if (MVAPICH2_EXEC_RETURN EQUAL 0)
139             string(REGEX MATCH "MVAPICH2" MVAPICH2_NAME ${MVAPICH2_TYPE})
140             # Want to check for MVAPICH2 in case some other library supplies mpiname
141             string(REGEX REPLACE "MVAPICH2 \([0-9]+\\.[0-9]*[a-z]?\\.?[0-9]*\)" "\\1" MVAPICH2_VERSION ${MVAPICH2_TYPE})
142             if (${MVAPICH2_NAME} STREQUAL "MVAPICH2" AND MVAPICH2_VERSION VERSION_LESS "1.5")
143                 # This test works correctly even with 1.5a1
144                 MESSAGE(WARNING
145                         "CMake found MVAPICH2 version ${MVAPICH2_VERSION} on your system. "
146                         "There are known problems with GROMACS and MVAPICH2 version < 1.5. "
147                         "Please consider updating your MVAPICH2 if your MPI wrapper compilers "
148                         "are using the above MVAPICH2 version.")
149             endif ()
150             unset(MVAPICH2_VERSION)
151             unset(MVAPICH2_NAME)
152             unset(MVAPICH2_TYPE)
153             unset(MVAPICH2_EXEC_RETURN)
154         endif ()
155     endif ()
156     unset(MPINAME_BIN CACHE)
157 endif () # end if(GMX_LIB_MPI)
158
159 # Look for MPI process launchers that may be missed, especially if we didn't
160 # need to find the full MPI library build system support.
161 if (NOT MPIEXEC_EXECUTABLE)
162     find_program(MPIEXEC
163                  NAMES mpiexec mpirun lamexec srun aprun poe
164                  HINTS ${MPI_HOME} $ENV{MPI_HOME}
165                  PATH_SUFFIXES bin
166                  DOC "Executable for running MPI programs.")
167
168     set(MPIEXEC_EXECUTABLE "$MPIEXEC")
169     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.")
170     set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
171     set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will come after all flags given to MPIEXEC.")
172     set(MPIEXEC_MAX_NUMPROCS "2" CACHE STRING "Maximum number of processors available to run MPI applications.")
173     mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
174 endif ()