Use cxx mpi
[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 # Manage the MPI setup, assuming that CMAKE_C_COMPILER is an MPI
37 # (wrapper) compiler.
38 if(GMX_MPI)
39   if(GMX_THREAD_MPI)
40     message(STATUS "MPI is not compatible with thread-MPI. Disabling thread-MPI.")
41     set(GMX_THREAD_MPI OFF CACHE BOOL
42         "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" FORCE)
43   endif()
44
45   # Test the CMAKE_C_COMPILER for being an MPI (wrapper) compiler
46   TRY_COMPILE(MPI_FOUND ${CMAKE_BINARY_DIR}
47     "${CMAKE_SOURCE_DIR}/cmake/TestMPI.cpp"
48     COMPILE_DEFINITIONS )
49
50   # If CMAKE_C_COMPILER is not a MPI wrapper. Try to find MPI using cmake module as fall-back.
51   if(NOT MPI_FOUND)
52       find_package(MPI)
53       if(MPI_CXX_FOUND)
54           set(MPI_COMPILE_FLAGS ${MPI_CXX_COMPILE_FLAGS})
55           separate_arguments(MPI_COMPILE_FLAGS)
56           set(MPI_LINKER_FLAGS ${MPI_CXX_LINK_FLAGS})
57           separate_arguments(MPI_CXX_LINK_FLAGS)
58           include_directories(SYSTEM ${MPI_CXX_INCLUDE_PATH})
59           list(APPEND GMX_COMMON_LIBRARIES ${MPI_CXX_LIBRARIES})
60       endif()
61       set(MPI_FOUND ${MPI_CXX_FOUND})
62   else()
63       # The following defaults are based on FindMPI.cmake in cmake
64       # 3.1.2. (That package does not actually do any detection of the
65       # flags, but if it ever does then we should re-visit how we use
66       # the package.) If we are compiling with an MPI wrapper
67       # compiler, then MPI_FOUND will be set above, and will mean that
68       # none of these cache variables are populated by the package. We
69       # need to do it manually so that test drivers can work using the
70       # standard machinery for CMake + FindMPI.cmake.  Users will need
71       # to set these to suit their MPI setup in order for tests to
72       # work.
73
74       find_program(MPIEXEC
75           NAMES mpiexec mpirun lamexec srun aprun poe
76           HINTS ${MPI_HOME} $ENV{MPI_HOME}
77           PATH_SUFFIXES bin
78           DOC "Executable for running MPI programs.")
79
80       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.")
81       set(MPIEXEC_PREFLAGS     ""    CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
82       set(MPIEXEC_POSTFLAGS    ""    CACHE STRING "These flags will come after all flags given to MPIEXEC.")
83       set(MPIEXEC_MAX_NUMPROCS "2"   CACHE STRING "Maximum number of processors available to run MPI applications.")
84       mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
85   endif()
86
87   if(MPI_FOUND)
88     include(gmxTestMPI_IN_PLACE)
89     if (GMX_MPI_IN_PLACE)
90       gmx_test_mpi_in_place(MPI_IN_PLACE_EXISTS)
91     endif()
92
93     # Find path of the mpi compilers
94     if (${MPI_CXX_FOUND})
95         get_filename_component(_mpi_c_compiler_path "${MPI_CXX_COMPILER}" PATH)
96         get_filename_component(_mpiexec_path "${MPIEXEC}" PATH)
97     else()
98         get_filename_component(_cmake_c_compiler_path "${CMAKE_C_COMPILER}" PATH)
99         get_filename_component(_cmake_cxx_compiler_path "${CMAKE_CXX_COMPILER}" PATH)
100     endif()
101
102     # Test for and warn about unsuitable MPI versions
103     #
104     # Execute the ompi_info binary with the full path of the compiler wrapper
105     # found, otherwise we run the risk of false positives.
106     find_file(MPI_INFO_BIN ompi_info
107               HINTS ${_mpi_c_compiler_path} ${_mpiexec_path}
108                     ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
109               NO_DEFAULT_PATH
110               NO_SYSTEM_ENVIRONMENT_PATH
111               NO_CMAKE_SYSTEM_PATH)
112     if (MPI_INFO_BIN)
113       exec_program(${MPI_INFO_BIN}
114         ARGS -v ompi full
115         OUTPUT_VARIABLE OPENMPI_TYPE
116         RETURN_VALUE OPENMPI_EXEC_RETURN)
117       if(OPENMPI_EXEC_RETURN EQUAL 0)
118         string(REGEX REPLACE ".*Open MPI: \([0-9]+\\.[0-9]*\\.?[0-9]*\).*" "\\1" OPENMPI_VERSION ${OPENMPI_TYPE})
119         if(OPENMPI_VERSION VERSION_LESS "1.4.1")
120           MESSAGE(WARNING
121              "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
122              "There are known problems with GROMACS and OpenMPI version < 1.4.1. "
123              "Please consider updating your OpenMPI if your MPI wrapper compilers "
124              "are using the above OpenMPI version.")
125         endif()
126         if(OPENMPI_VERSION VERSION_EQUAL "1.8.6")
127           MESSAGE(WARNING
128              "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
129              "This OpenMPI version is known to leak memory with GROMACS,"
130              "please update to a more recent version. ")
131         endif()
132         unset(OPENMPI_VERSION)
133         unset(OPENMPI_TYPE)
134         unset(OPENMPI_EXEC_RETURN)
135       endif()
136     endif()
137     unset(MPI_INFO_BIN CACHE)
138
139     # Execute the mpiname binary with the full path of the compiler wrapper
140     # found, otherwise we run the risk of false positives.
141     find_file(MPINAME_BIN mpiname
142               HINTS ${_mpi_c_compiler_path}
143                     ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
144               NO_DEFAULT_PATH
145               NO_SYSTEM_ENVIRONMENT_PATH
146               NO_CMAKE_SYSTEM_PATH)
147     if (MPINAME_BIN)
148       exec_program(${MPINAME_BIN}
149         ARGS -n -v
150         OUTPUT_VARIABLE MVAPICH2_TYPE
151         RETURN_VALUE MVAPICH2_EXEC_RETURN)
152       if(MVAPICH2_EXEC_RETURN EQUAL 0)
153         string(REGEX MATCH "MVAPICH2" MVAPICH2_NAME ${MVAPICH2_TYPE})
154         # Want to check for MVAPICH2 in case some other library supplies mpiname
155         string(REGEX REPLACE "MVAPICH2 \([0-9]+\\.[0-9]*[a-z]?\\.?[0-9]*\)" "\\1" MVAPICH2_VERSION ${MVAPICH2_TYPE})
156         if(${MVAPICH2_NAME} STREQUAL "MVAPICH2" AND MVAPICH2_VERSION VERSION_LESS "1.5")
157           # This test works correctly even with 1.5a1
158           MESSAGE(WARNING
159              "CMake found MVAPICH2 version ${MVAPICH2_VERSION} on your system. "
160              "There are known problems with GROMACS and MVAPICH2 version < 1.5. "
161              "Please consider updating your MVAPICH2 if your MPI wrapper compilers "
162              "are using the above MVAPICH2 version.")
163        endif()
164        unset(MVAPICH2_VERSION)
165        unset(MVAPICH2_NAME)
166        unset(MVAPICH2_TYPE)
167        unset(MVAPICH2_EXEC_RETURN)
168       endif()
169     endif()
170     unset(MPINAME_BIN CACHE)
171   else()
172       message(FATAL_ERROR
173         "MPI support requested, but no MPI compiler found. Either set the "
174         "C-compiler (CMAKE_C_COMPILER) to the MPI compiler (often called mpicc), "
175         "or set the variables reported missing for MPI_CXX above.")
176   endif()
177
178   set(GMX_LIB_MPI 1)
179 endif()