don't execute any mpi_info/mpiname in the path
[alexxy/gromacs.git] / cmake / gmxManageMPI.cmake
1 # Manage the MPI setup, assuming that CMAKE_C_COMPILER is an MPI
2 # (wrapper) compiler.
3 if(GMX_MPI)
4   if(GMX_THREAD_MPI)
5     message(STATUS "MPI is not compatible with thread-MPI. Disabling thread-MPI.")
6     set(GMX_THREAD_MPI OFF CACHE BOOL
7         "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" FORCE)
8   endif(GMX_THREAD_MPI)
9
10   # Test the CMAKE_C_COMPILER for being an MPI (wrapper) compiler
11   TRY_COMPILE(MPI_FOUND ${CMAKE_BINARY_DIR}
12     "${CMAKE_SOURCE_DIR}/cmake/TestMPI.c"
13     COMPILE_DEFINITIONS )
14
15   # If CMAKE_C_COMPILER is not a MPI wrapper. Try to find MPI using cmake module as fall-back.
16   # cmake < 2.8.5 not recommended for fall-back because of unreliability (redmine #851)
17   if(NOT MPI_FOUND)
18       if(CMAKE_VERSION VERSION_LESS "2.8.5")
19           set(MPI_PREFIX MPI)
20       else()
21           set(MPI_PREFIX MPI_C)
22       endif()
23       find_package(MPI)
24       if(${${MPI_PREFIX}_FOUND})
25         set(GROMACS_C_FLAGS ${GROMACS_FLAGS} ${${MPI_PREFIX}_COMPILE_FLAGS})
26         set(GROMACS_LINKER_FLAGS ${GROMACS_LINKER_FLAGS} ${${MPI_PREFIX}_LINK_FLAGS})
27         include_directories(${${MPI_PREFIX}_INCLUDE_PATH})
28         list(APPEND GMX_EXTRA_LIBRARIES ${${MPI_PREFIX}_LIBRARIES})
29       endif()
30       set(MPI_FOUND ${${MPI_PREFIX}_FOUND})
31   endif()
32
33   if(MPI_FOUND)
34     include(gmxTestMPI_IN_PLACE)
35     if (GMX_MPI_IN_PLACE)
36       gmx_test_mpi_in_place(MPI_IN_PLACE_EXISTS)
37     endif()
38
39     # Find path of the mpi compilers
40     if (${${MPI_PREFIX}_FOUND})
41         get_filename_component(_mpi_c_compiler_path "${MPI_C_COMPILER}" PATH)
42         get_filename_component(_mpiexec_path "${MPIEXEC}" PATH)
43     else()
44         get_filename_component(_cmake_c_compiler_path "${CMAKE_C_COMPILER}" PATH)
45         get_filename_component(_cmake_cxx_compiler_path "${CMAKE_CXX_COMPILER}" PATH)
46     endif()
47
48     # Test for and warn about unsuitable MPI versions
49     #
50     # Execute the ompi_info binary with the full path of the compiler wrapper
51     # found, otherwise we run the risk of false positives.
52     find_file(MPI_INFO_BIN ompi_info
53               HINTS ${_mpi_c_compiler_path} ${_mpiexec_path}
54                     ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
55               NO_DEFAULT_PATH
56               NO_SYSTEM_ENVIRONMENT_PATH
57               NO_CMAKE_SYSTEM_PATH)
58     if (MPI_INFO_BIN)
59       exec_program(${MPI_INFO_BIN}
60         ARGS -v ompi full
61         OUTPUT_VARIABLE OPENMPI_TYPE
62         RETURN_VALUE OPENMPI_EXEC_RETURN)
63       if(OPENMPI_EXEC_RETURN EQUAL 0)
64         string(REGEX REPLACE ".*Open MPI: \([0-9]+\\.[0-9]*\\.?[0-9]*\).*" "\\1" OPENMPI_VERSION ${OPENMPI_TYPE})
65         if(OPENMPI_VERSION VERSION_LESS "1.4.1")
66           MESSAGE(WARNING
67              "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
68              "There are known problems with GROMACS and OpenMPI version < 1.4.1. "
69              "Please consider updating your OpenMPI if your MPI wrapper compilers "
70              "are using the above OpenMPI version.")
71         endif()
72         unset(OPENMPI_VERSION)
73         unset(OPENMPI_TYPE)
74         unset(OPENMPI_EXEC_RETURN)
75       endif()
76     endif()
77     unset(MPI_INFO_BIN)
78
79     # Execute the mpiname binary with the full path of the compiler wrapper
80     # found, otherwise we run the risk of false positives.
81     find_file(MPINAME_BIN mpiname
82               HINTS ${_mpi_c_compiler_path}
83                     ${_cmake_c_compiler_path} ${_cmake_cxx_compiler_path}
84               NO_DEFAULT_PATH
85               NO_SYSTEM_ENVIRONMENT_PATH
86               NO_CMAKE_SYSTEM_PATH)
87     if (MPINAME_BIN)
88       exec_program(${MPINAME_BIN}
89         ARGS -n -v
90         OUTPUT_VARIABLE MVAPICH2_TYPE
91         RETURN_VALUE MVAPICH2_EXEC_RETURN)
92       if(MVAPICH2_EXEC_RETURN EQUAL 0)
93         string(REGEX MATCH "MVAPICH2" MVAPICH2_NAME ${MVAPICH2_TYPE})
94         # Want to check for MVAPICH2 in case some other library supplies mpiname
95         string(REGEX REPLACE "MVAPICH2 \([0-9]+\\.[0-9]*[a-z]?\\.?[0-9]*\)" "\\1" MVAPICH2_VERSION ${MVAPICH2_TYPE})
96         if(${MVAPICH2_NAME} STREQUAL "MVAPICH2" AND MVAPICH2_VERSION VERSION_LESS "1.5")
97           # This test works correctly even with 1.5a1
98           MESSAGE(WARNING
99              "CMake found MVAPICH2 version ${MVAPICH2_VERSION} on your system. "
100              "There are known problems with GROMACS and MVAPICH2 version < 1.5. "
101              "Please consider updating your MVAPICH2 if your MPI wrapper compilers "
102              "are using the above MVAPICH2 version.")
103        endif()
104        unset(MVAPICH2_VERSION)
105        unset(MVAPICH2_NAME)
106        unset(MVAPICH2_TYPE)
107        unset(MVAPICH2_EXEC_RETURN)
108       endif()
109     endif()
110     unset(MPINAME_BIN)
111
112   else(MPI_FOUND)
113     if (CMAKE_VERSION VERSION_LESS "2.8.5")
114       message(FATAL_ERROR
115         "MPI support requested, but no MPI compiler found. Either set the "
116         "C-compiler (CMAKE_C_COMPILER) to the MPI compiler (often called mpicc), "
117         "or use a newer cmake version (>=2.8.5) which has improved MPI detection.")
118     else()
119       message(FATAL_ERROR
120         "MPI support requested, but no MPI compiler found. Either set the "
121         "C-compiler (CMAKE_C_COMPILER) to the MPI compiler (often called mpicc), "
122         "or set the variables reported missing for MPI_C above.")
123     endif()
124
125   endif(MPI_FOUND)
126
127   include(gmxTestCatamount)
128   gmx_test_catamount(GMX_CRAY_XT3)
129   if(GMX_CRAY_XT3)
130     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_CRAY_XT3")
131     set(GMX_NO_SYSTEM 1)
132     set(GMX_NO_NICE 1)
133   endif(GMX_CRAY_XT3)
134
135   set(GMX_LIB_MPI 1)
136   set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_LIB_MPI")
137 endif(GMX_MPI)