5a8558176447a0c276e0e0158a0d1571db21414a
[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     # Test for and warn about unsuitable MPI versions
40     exec_program(ompi_info
41       ARGS -v ompi full
42       OUTPUT_VARIABLE OPENMPI_TYPE
43       RETURN_VALUE OPENMPI_EXEC_RETURN)
44     if(OPENMPI_EXEC_RETURN EQUAL 0)
45       string(REGEX REPLACE ".*Open MPI: \([0-9]+\\.[0-9]*\\.?[0-9]*\).*" "\\1" OPENMPI_VERSION ${OPENMPI_TYPE})
46       if(OPENMPI_VERSION VERSION_LESS "1.4.1")
47         MESSAGE(WARNING
48              "CMake found OpenMPI version ${OPENMPI_VERSION} on your system. "
49              "There are known problems with GROMACS and OpenMPI version < 1.4.1. "
50              "Please consider updating your OpenMPI if your MPI wrapper compilers "
51              "are using the above OpenMPI version.")
52       endif()
53       unset(OPENMPI_VERSION)
54       unset(OPENMPI_TYPE)
55       unset(OPENMPI_EXEC_RETURN)
56     endif()
57     exec_program(mpiname
58       ARGS -n -v
59       OUTPUT_VARIABLE MVAPICH2_TYPE
60       RETURN_VALUE MVAPICH2_EXEC_RETURN)
61     if(MVAPICH2_EXEC_RETURN EQUAL 0)
62       string(REGEX MATCH "MVAPICH2" MVAPICH2_NAME ${MVAPICH2_TYPE})
63       # Want to check for MVAPICH2 in case some other library supplies mpiname
64       string(REGEX REPLACE "MVAPICH2 \([0-9]+\\.[0-9]*[a-z]?\\.?[0-9]*\)" "\\1" MVAPICH2_VERSION ${MVAPICH2_TYPE})
65       if(${MVAPICH2_NAME} STREQUAL "MVAPICH2" AND MVAPICH2_VERSION VERSION_LESS "1.5")
66         # This test works correctly even with 1.5a1
67         MESSAGE(WARNING
68              "CMake found MVAPICH2 version ${MVAPICH2_VERSION} on your system. "
69              "There are known problems with GROMACS and MVAPICH2 version < 1.5. "
70              "Please consider updating your MVAPICH2 if your MPI wrapper compilers "
71              "are using the above MVAPICH2 version.")
72       endif()
73       unset(MVAPICH2_VERSION)
74       unset(MVAPICH2_NAME)
75       unset(MVAPICH2_TYPE)
76       unset(MVAPICH2_EXEC_RETURN)
77     endif()
78
79   else(MPI_FOUND)
80     if (CMAKE_VERSION VERSION_LESS "2.8.5")
81       message(FATAL_ERROR
82         "MPI support requested, but no MPI compiler found. Either set the "
83         "C-compiler (CMAKE_C_COMPILER) to the MPI compiler (often called mpicc), "
84         "or use a newer cmake version (>=2.8.5) which has improved MPI detection.")
85     else()
86       message(FATAL_ERROR
87         "MPI support requested, but no MPI compiler found. Either set the "
88         "C-compiler (CMAKE_C_COMPILER) to the MPI compiler (often called mpicc), "
89         "or set the variables reported missing for MPI_C above.")
90     endif()
91
92   endif(MPI_FOUND)
93
94   include(gmxTestCatamount)
95   gmx_test_catamount(GMX_CRAY_XT3)
96   if(GMX_CRAY_XT3)
97     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_CRAY_XT3")
98     set(GMX_NO_SYSTEM 1)
99     set(GMX_NO_NICE 1)
100   endif(GMX_CRAY_XT3)
101
102   set(GMX_LIB_MPI 1)
103   set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_LIB_MPI")
104 endif(GMX_MPI)