Rename trnio.* to trrio.*
[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, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 # Manage the MPI setup, assuming that CMAKE_C_COMPILER is an MPI
36 # (wrapper) compiler.
37 if(GMX_MPI)
38   if(GMX_THREAD_MPI)
39     message(STATUS "MPI is not compatible with thread-MPI. Disabling thread-MPI.")
40     set(GMX_THREAD_MPI OFF CACHE BOOL
41         "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" FORCE)
42   endif()
43
44   # Test the CMAKE_C_COMPILER for being an MPI (wrapper) compiler
45   TRY_COMPILE(MPI_FOUND ${CMAKE_BINARY_DIR}
46     "${CMAKE_SOURCE_DIR}/cmake/TestMPI.c"
47     COMPILE_DEFINITIONS )
48
49   # If CMAKE_C_COMPILER is not a MPI wrapper. Try to find MPI using cmake module as fall-back.
50   if(NOT MPI_FOUND)
51       find_package(MPI)
52       if(MPI_C_FOUND)
53         set(MPI_COMPILE_FLAGS ${MPI_C_COMPILE_FLAGS})
54         set(MPI_LINKER_FLAGS ${MPI_C_LINK_FLAGS})
55         include_directories(${MPI_C_INCLUDE_PATH})
56         list(APPEND GMX_EXTRA_LIBRARIES ${MPI_C_LIBRARIES})
57       endif()
58       set(MPI_FOUND ${MPI_C_FOUND})
59   else()
60       # The following defaults are based on FindMPI.cmake in cmake
61       # 3.1.2. (That package does not actually do any detection of the
62       # flags, but if it ever does then we should re-visit how we use
63       # the package.) If we are compiling with an MPI wrapper
64       # compiler, then MPI_FOUND will be set above, and will mean that
65       # none of these cache variables are populated by the package. We
66       # need to do it manually so that test drivers can work using the
67       # standard machinery for CMake + FindMPI.cmake.  Users will need
68       # to set these to suit their MPI setup in order for tests to
69       # work.
70
71       find_program(MPIEXEC
72           NAMES mpiexec mpirun lamexec srun aprun poe
73           HINTS ${MPI_HOME} $ENV{MPI_HOME}
74           PATH_SUFFIXES bin
75           DOC "Executable for running MPI programs.")
76
77       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.")
78       set(MPIEXEC_PREFLAGS     ""    CACHE STRING "These flags will be directly before the executable that is being run by MPIEXEC.")
79       set(MPIEXEC_POSTFLAGS    ""    CACHE STRING "These flags will come after all flags given to MPIEXEC.")
80       set(MPIEXEC_MAX_NUMPROCS "2"   CACHE STRING "Maximum number of processors available to run MPI applications.")
81       mark_as_advanced(MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
82   endif()
83
84   if(MPI_FOUND)
85     include(gmxTestMPI_IN_PLACE)
86     if (GMX_MPI_IN_PLACE)
87       gmx_test_mpi_in_place(MPI_IN_PLACE_EXISTS)
88     endif()
89
90     # Find path of the mpi compilers
91     if (${MPI_C_FOUND})
92         get_filename_component(_mpi_c_compiler_path "${MPI_C_COMPILER}" PATH)
93         get_filename_component(_mpiexec_path "${MPIEXEC}" PATH)
94     else()
95         get_filename_component(_cmake_c_compiler_path "${CMAKE_C_COMPILER}" PATH)
96         get_filename_component(_cmake_cxx_compiler_path "${CMAKE_CXX_COMPILER}" PATH)
97     endif()
98
99     # Test for and warn about unsuitable MPI versions
100     #
101     # Execute the ompi_info binary with the full path of the compiler wrapper
102     # found, otherwise we run the risk of false positives.
103     find_file(MPI_INFO_BIN ompi_info
104               HINTS ${_mpi_c_compiler_path} ${_mpiexec_path}
105                     ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
106               NO_DEFAULT_PATH
107               NO_SYSTEM_ENVIRONMENT_PATH
108               NO_CMAKE_SYSTEM_PATH)
109     if (MPI_INFO_BIN)
110       exec_program(${MPI_INFO_BIN}
111         ARGS -v ompi full
112         OUTPUT_VARIABLE OPENMPI_TYPE
113         RETURN_VALUE OPENMPI_EXEC_RETURN)
114       if(OPENMPI_EXEC_RETURN EQUAL 0)
115         string(REGEX REPLACE ".*Open MPI: \([0-9]+\\.[0-9]*\\.?[0-9]*\).*" "\\1" OPENMPI_VERSION ${OPENMPI_TYPE})
116         if(OPENMPI_VERSION VERSION_LESS "1.4.1")
117           MESSAGE(WARNING
118              "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
119              "There are known problems with GROMACS and OpenMPI version < 1.4.1. "
120              "Please consider updating your OpenMPI if your MPI wrapper compilers "
121              "are using the above OpenMPI version.")
122         endif()
123         unset(OPENMPI_VERSION)
124         unset(OPENMPI_TYPE)
125         unset(OPENMPI_EXEC_RETURN)
126       endif()
127     endif()
128     unset(MPI_INFO_BIN CACHE)
129
130     # Execute the mpiname binary with the full path of the compiler wrapper
131     # found, otherwise we run the risk of false positives.
132     find_file(MPINAME_BIN mpiname
133               HINTS ${_mpi_c_compiler_path}
134                     ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
135               NO_DEFAULT_PATH
136               NO_SYSTEM_ENVIRONMENT_PATH
137               NO_CMAKE_SYSTEM_PATH)
138     if (MPINAME_BIN)
139       exec_program(${MPINAME_BIN}
140         ARGS -n -v
141         OUTPUT_VARIABLE MVAPICH2_TYPE
142         RETURN_VALUE MVAPICH2_EXEC_RETURN)
143       if(MVAPICH2_EXEC_RETURN EQUAL 0)
144         string(REGEX MATCH "MVAPICH2" MVAPICH2_NAME ${MVAPICH2_TYPE})
145         # Want to check for MVAPICH2 in case some other library supplies mpiname
146         string(REGEX REPLACE "MVAPICH2 \([0-9]+\\.[0-9]*[a-z]?\\.?[0-9]*\)" "\\1" MVAPICH2_VERSION ${MVAPICH2_TYPE})
147         if(${MVAPICH2_NAME} STREQUAL "MVAPICH2" AND MVAPICH2_VERSION VERSION_LESS "1.5")
148           # This test works correctly even with 1.5a1
149           MESSAGE(WARNING
150              "CMake found MVAPICH2 version ${MVAPICH2_VERSION} on your system. "
151              "There are known problems with GROMACS and MVAPICH2 version < 1.5. "
152              "Please consider updating your MVAPICH2 if your MPI wrapper compilers "
153              "are using the above MVAPICH2 version.")
154        endif()
155        unset(MVAPICH2_VERSION)
156        unset(MVAPICH2_NAME)
157        unset(MVAPICH2_TYPE)
158        unset(MVAPICH2_EXEC_RETURN)
159       endif()
160     endif()
161     unset(MPINAME_BIN CACHE)
162
163     # Using find_file() runs the CMake standard module
164     # GetPrerequisites.cmake, which adds the file_cmd
165     # variable to the top-level CMake namespace. This is
166     # fixed in CMake 2.8.10. Meanwhile, clean up for it.
167     if(CMAKE_VERSION VERSION_LESS "2.8.10")
168         mark_as_advanced(file_cmd)
169     endif()
170
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_C above.")
176   endif()
177
178   set(GMX_LIB_MPI 1)
179 endif()