automation for setting GMX_GPU & cmake GPU detection
[alexxy/gromacs.git] / cmake / gmxManageGPU.cmake
1 # If the user did not set GMX_GPU we'll consider this option to be
2 # in "auto" mode meaning that we will:
3 # - search for CUDA and set GMX_GPU=ON we find it
4 # - check whether GPUs are present
5 # - if CUDA is not found but GPUs were detected issue a warning
6 if (NOT DEFINED GMX_GPU)
7     set(GMX_GPU_AUTO TRUE CACHE INTERNAL "GPU acceleration will be selected automatically")
8 endif()
9 option(GMX_GPU "Enable GPU acceleration" OFF)
10
11 if(GMX_GPU AND GMX_DOUBLE)
12     message(FATAL_ERROR "GPU acceleration is not available in double precision!")
13 endif()
14 if(GMX_GPU_AUTO AND GMX_DOUBLE)
15     message(WARNING "GPU acceleration is not available in double precision, disabled!")
16     set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
17     set_property(CACHE GMX_GPU_AUTO PROPERTY VALUE OFF)
18 endif()
19
20 # detect GPUs in the build host machine
21 if (GMX_GPU OR GMX_GPU_AUTO AND NOT GMX_GPU_DETECTION_DONE)
22     include(gmxDetectGpu)
23     gmx_detect_gpu()
24 endif()
25
26 # Depending on the current vale of GMX_GPU and GMX_GPU_AUTO:
27 # - OFF, FALSE: Will skip this detection.
28 # - OFF, TRUE : Will keep GMX_GPU=OFF if no CUDA is detected, but will assemble
29 #               a warning message which will be issued at the end of the
30 #               configuration if GPU(s) were found in the build system.
31 # - ON , FALSE: The user requested GPU builds, will require CUDA and will fail
32 #               if it is not available.
33 # - ON , TRUE : Can't happen (GMX_GPU=ON can only be user-set at this point)
34 if(GMX_GPU OR GMX_GPU_AUTO AND NOT GMX_GPU_DETECTION_DONE)
35     # We support CUDA >=v3.2 on *nix, but <= v4.1 doesn't work with MSVC
36     if(MSVC)
37         find_package(CUDA 4.1)
38     else()
39         find_package(CUDA 3.2)
40     endif()
41
42     if (EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
43         set(CUDA_FOUND TRUE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
44     else()
45         set(CUDA_FOUND FALSE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
46     endif()
47
48     # assemble warning/error message
49     if (GMX_DETECT_GPU_AVAILABLE)
50         set(_msg "
51     ${GMX_DETECT_GPU_COUNT} NVIDIA GPU(s) found in the system")
52
53         # append GPU names
54         if (NOT GMX_DETECT_GPU_INFO STREQUAL "")
55             set(_msg "${_msg}:")
56             foreach(gpu ${GMX_DETECT_GPU_INFO})
57                 set(_msg "${_msg}
58                 ${gpu}")
59             endforeach()
60         endif()
61
62         # TODO remove the second part of the message when we'll have compute
63         # capability information from the detection.
64         set(_msg "${_msg}
65     Compute capability information not available, consult the NVIDIA website:
66     https://developer.nvidia.com/cuda-gpus
67             ")
68     endif()
69
70         set(CUDA_NOTFOUND_MESSAGE "
71     mdrun supports native GPU acceleration on NVIDIA hardware with compute
72     capability >=2.0. This requires the NVIDIA CUDA library, which was not
73     found; the location can be hinted by setting CUDA_TOOLKIT_ROOT_DIR as
74     a CMake option (It does not work as an environment variable).
75     The typical location would be /usr/local/cuda[-version].
76     Note that CPU or GPU acceleration can be selected at runtime!
77
78     ${_msg}")
79         unset(_msg)
80
81     if (NOT CUDA_FOUND)
82         if (GMX_GPU_AUTO)
83             # Disable GPU acceleration in auto mode
84             message(STATUS "Disabling native GPU acceleration")
85             set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
86             set(CUDA_NOTFOUND_AUTO ON)
87         else ()
88             # the user requested CUDA, but it wasn't found
89             message(FATAL_ERROR "${CUDA_NOTFOUND_MESSAGE}")
90         endif()
91     else()
92         if (GMX_GPU_AUTO)
93             message(STATUS "Enabling native GPU acceleration")
94             set_property(CACHE GMX_GPU PROPERTY VALUE ON)
95         endif()
96     endif() # NOT CUDA_FOUND
97 endif()
98 # Annoyingly enough, FindCUDA leaves a few variables behind as non-advanced.
99 # We need to mark these advanced outside the conditional, otherwise, if the
100 # user turns GMX_GPU=OFF after a failed cmake pass, these variables will be
101 # left behind in the cache.
102 mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_SDK_ROOT_DIR CUDA_VERBOSE_BUILD)
103
104 macro(gmx_gpu_setup)
105     # set up nvcc options
106     include(gmxManageNvccConfig)
107
108     # Version info (semicolon used as line separator) for nvcc.
109     get_nvcc_version_info()
110
111     # Check whether we can use atomic operations needed for polling wait for GPU
112     # (to avoid the cudaStreamSynchronize + ECC bug).
113     # With thread-MPI testing atomics has already been carried out, but without
114     # thread-MPI we need to invoke the atomics test independently.
115     if (NOT GMX_THREAD_MPI)
116         set(TEST_TMPI_ATOMICS_ONLY ON CACHE INTERNAL
117             "Test only the atomic operations of thread-MPI.")
118         include(ThreadMPI)
119     endif()
120
121     # no OpenMP is no good!
122     if(NOT GMX_OPENMP)
123         message(WARNING "
124     To use GPU acceleration efficiently, mdrun requires OpenMP multi-threading.
125     With no OpenMP a single CPU core can be used with a GPU which is not optimal.
126     Note that with MPI multiple processes can be forced to use a single GPU, but this
127     typically inefficient. Note that you need to set both C and C++ compilers that
128     support OpenMP (CC and CXX environment variables, respectively) when using GPUs.")
129     endif()
130 endmacro()