2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012, 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.
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.
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.
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.
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.
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.
35 # The gmx_detect_gpu() macro aims to detect GPUs available in the build machine
36 # and provide the number, names, and compute-capabilities of these devices.
38 # The current version is limited to checking the availability of NVIDIA GPUs
39 # without compute-capability information.
41 # The current detection relies on the following checks in the order of listing:
42 # - output of nvidia-smi (if available);
43 # - presence and content of of /proc/driver/nvidia/gpus/*/information (Linux)
44 # - output of lspci (Linux)
46 # If any of the checks succeeds in finding devices, consecutive checks will not
47 # be carried out. Additionally, when lspci is used and a device with unknown
48 # PCI ID is encountered, lspci tries to check the online PCI ID database. If
49 # this is not possible or the device is simply not recognized, no device names
52 # The following advanced variables are defined:
53 # - GMX_DETECT_GPU_AVAILABLE - TRUE if any GPUs were detected, otherwise FALSE
54 # - GMX_DETECT_GPU_COUNT - # of GPUs detected
55 # - GMX_DETECT_GPU_INFO - list of information strings of the detected GPUs
57 # NOTE: The proper solution is to detect hardware compatible with the native
58 # GPU acceleration. However, this requires checking the compute capability
59 # of the device which is not possible with the current checks and requires
60 # interfacing with the CUDA driver API.
63 # check whether the number of GPUs machetes the number of elements in the GPU info list
64 macro(check_num_gpu_info NGPU GPU_INFO)
65 list(LENGTH ${GPU_INFO} _len)
66 if (NOT NGPU EQUAL _len)
67 list(APPEND ${GMX_DETECT_GPU_INFO} "NOTE: information about some GPU(s) missing!")
73 if (NOT DEFINED GMX_DETECT_GPU_COUNT OR NOT DEFINED GMX_DETECT_GPU_INFO)
75 set(GMX_DETECT_GPU_COUNT 0)
76 set(GMX_DETECT_GPU_INFO "")
78 message(STATUS "Looking for NVIDIA GPUs present in the system")
80 # nvidia-smi-based detection.
81 # Requires the nvidia-smi tool to be installed and available in the path
82 # or in one of the default search locations
83 if (NOT DEFINED GMX_DETECT_GPU_COUNT_NVIDIA_SMI)
84 # try to find the nvidia-smi binary
85 # TODO add location hints
86 find_program(_nvidia_smi "nvidia-smi")
88 set(GMX_DETECT_GPU_COUNT_NVIDIA_SMI 0)
89 # execute nvidia-smi -L to get a short list of GPUs available
90 exec_program(${_nvidia_smi_path} ARGS -L
91 OUTPUT_VARIABLE _nvidia_smi_out
92 RETURN_VALUE _nvidia_smi_ret)
93 # process the stdout of nvidia-smi
94 if (_nvidia_smi_ret EQUAL 0)
95 # convert string with newlines to list of strings
96 string(REGEX REPLACE "\n" ";" _nvidia_smi_out "${_nvidia_smi_out}")
97 foreach(_line ${_nvidia_smi_out})
98 if (_line MATCHES "^GPU [0-9]+:")
99 math(EXPR GMX_DETECT_GPU_COUNT_NVIDIA_SMI "${GMX_DETECT_GPU_COUNT_NVIDIA_SMI}+1")
100 # the UUID is not very useful for the user, remove it
101 string(REGEX REPLACE " \\(UUID:.*\\)" "" _gpu_info "${_line}")
102 if (NOT _gpu_info STREQUAL "")
103 list(APPEND GMX_DETECT_GPU_INFO "${_gpu_info}")
108 check_num_gpu_info(${GMX_DETECT_GPU_COUNT_NVIDIA_SMI} GMX_DETECT_GPU_INFO)
109 set(GMX_DETECT_GPU_COUNT ${GMX_DETECT_GPU_COUNT_NVIDIA_SMI})
113 unset(_nvidia_smi CACHE)
114 unset(_nvidia_smi_ret)
115 unset(_nvidia_smi_out)
120 if (UNIX AND NOT (APPLE OR CYGWIN))
121 # /proc/driver/nvidia/gpus/*/information-based detection.
122 # Requires the NVDIA closed source driver to be installed and loaded
123 if (NOT DEFINED GMX_DETECT_GPU_COUNT_PROC AND GMX_DETECT_GPU_COUNT EQUAL 0)
124 set(GMX_DETECT_GPU_COUNT_PROC 0)
125 file(GLOB _proc_nv_gpu_info "/proc/driver/nvidia/gpus/*/information")
126 foreach (_file ${_proc_nv_gpu_info})
127 math(EXPR GMX_DETECT_GPU_COUNT_PROC "${GMX_DETECT_GPU_COUNT_PROC}+1")
128 # assemble information strings similar to the nvidia-smi output
129 # GPU ID = directory name on /proc/driver/nvidia/gpus/
130 string(REGEX REPLACE "/proc/driver/nvidia/gpus.*([0-9]+).*information" "\\1" _gpu_id ${_file})
132 file(STRINGS ${_file} _gpu_name LIMIT_COUNT 1 REGEX "^Model:.*" NO_HEX_CONVERSION)
133 string(REGEX REPLACE "^Model:[ \t]*(.*)" "\\1" _gpu_name "${_gpu_name}")
134 if (NOT _gpu_id STREQUAL "" AND NOT _gpu_name STREQUAL "")
135 list(APPEND GMX_DETECT_GPU_INFO "GPU ${_gpu_id}: ${_gpu_name}")
139 check_num_gpu_info(${GMX_DETECT_GPU_COUNT_PROC} GMX_DETECT_GPU_INFO)
140 set(GMX_DETECT_GPU_COUNT ${GMX_DETECT_GPU_COUNT_PROC})
142 unset(_proc_nv_gpu_info)
148 # lspci-based detection (does not provide GPU information).
149 # Requires lspci and for GPU names to be fetched from the central
150 # PCI ID db if not available locally.
151 if (NOT DEFINED GMX_DETECT_GPU_COUNT_LSPCI AND GMX_DETECT_GPU_COUNT EQUAL 0)
152 set(GMX_DETECT_GPU_COUNT_LSPCI 0)
153 exec_program(lspci ARGS -q
154 OUTPUT_VARIABLE _lspci_out
155 RETURN_VALUE _lspci_ret)
156 # prehaps -q is not supported, try running without
157 if (NOT RETURN_VALUE EQUAL 0)
159 OUTPUT_VARIABLE _lspci_out
160 RETURN_VALUE _lspci_ret)
162 if (_lspci_ret EQUAL 0)
163 # convert string with newlines to list of strings
164 STRING(REGEX REPLACE ";" "\\\\;" _lspci_out "${_lspci_out}")
165 string(REGEX REPLACE "\n" ";" _lspci_out "${_lspci_out}")
166 foreach(_line ${_lspci_out})
167 string(TOUPPER "${_line}" _line_upper)
168 if (_line_upper MATCHES ".*VGA.*NVIDIA.*" OR _line_upper MATCHES ".*3D.*NVIDIA.*")
169 math(EXPR GMX_DETECT_GPU_COUNT_LSPCI "${GMX_DETECT_GPU_COUNT_LSPCI}+1")
170 # Try to parse out the device name which should be
171 # included in the lspci -q output between []-s
172 string(REGEX REPLACE ".*\\[(.*)\\].*" "\\1" _gpu_name "${_line}")
173 if (NOT _gpu_name EQUAL "")
174 list(APPEND GMX_DETECT_GPU_INFO "${_gpu_name}")
179 check_num_gpu_info(${GMX_DETECT_GPU_COUNT_LSPCI} GMX_DETECT_GPU_INFO)
180 set(GMX_DETECT_GPU_COUNT ${GMX_DETECT_GPU_COUNT_LSPCI})
191 if (GMX_DETECT_GPU_COUNT GREATER 0)
192 set(GMX_DETECT_GPU_AVAILABLE YES)
194 set(GMX_DETECT_GPU_AVAILABLE NO)
196 set(GMX_DETECT_GPU_AVAILABLE ${GMX_DETECT_GPU_AVAILABLE} CACHE BOOL "Whether any NVIDIA GPU was detected" FORCE)
198 set(GMX_DETECT_GPU_COUNT ${GMX_DETECT_GPU_COUNT}
199 CACHE STRING "Number of NVIDIA GPUs detected")
200 set(GMX_DETECT_GPU_INFO ${GMX_DETECT_GPU_INFO}
201 CACHE STRING "basic information on the detected NVIDIA GPUs")
203 set(GMX_DETECT_GPU_COUNT_NVIDIA_SMI ${GMX_DETECT_GPU_COUNT_NVIDIA_SMI}
204 CACHE INTERNAL "Number of NVIDIA GPUs detected using nvidia-smi")
205 set(GMX_DETECT_GPU_COUNT_PROC ${GMX_DETECT_GPU_COUNT_PROC}
206 CACHE INTERNAL "Number of NVIDIA GPUs detected in /proc/driver/nvidia/gpus")
207 set(GMX_DETECT_GPU_COUNT_LSPCI ${GMX_DETECT_GPU_COUNT_LSPCI}
208 CACHE INTERNAL "Number of NVIDIA GPUs detected using lspci")
210 mark_as_advanced(GMX_DETECT_GPU_AVAILABLE
214 if (GMX_DETECT_GPU_AVAILABLE)
215 message(STATUS "Number of NVIDIA GPUs detected: ${GMX_DETECT_GPU_COUNT} ")
217 message(STATUS "Could not detect NVIDIA GPUs")
220 endif (NOT DEFINED GMX_DETECT_GPU_COUNT OR NOT DEFINED GMX_DETECT_GPU_INFO)
221 endmacro(gmx_detect_gpu)