Easiest way to test for existence of cuda-capable GPU from cmake?

前端 未结 5 2005
一个人的身影
一个人的身影 2020-12-30 06:10

We have some nightly build machines that have the cuda libraries installed, but which do not have a cuda-capable GPU installed. These machines are capable of building cuda-

5条回答
  •  一整个雨季
    2020-12-30 06:54

    One useful approach is to run programs that CUDA has installed, such as nvidia-smi, to see what they return.

            find_program(_nvidia_smi "nvidia-smi")
            if (_nvidia_smi)
                set(DETECT_GPU_COUNT_NVIDIA_SMI 0)
                # execute nvidia-smi -L to get a short list of GPUs available
                exec_program(${_nvidia_smi_path} ARGS -L
                    OUTPUT_VARIABLE _nvidia_smi_out
                    RETURN_VALUE    _nvidia_smi_ret)
                # process the stdout of nvidia-smi
                if (_nvidia_smi_ret EQUAL 0)
                    # convert string with newlines to list of strings
                    string(REGEX REPLACE "\n" ";" _nvidia_smi_out "${_nvidia_smi_out}")
                    foreach(_line ${_nvidia_smi_out})
                        if (_line MATCHES "^GPU [0-9]+:")
                            math(EXPR DETECT_GPU_COUNT_NVIDIA_SMI "${DETECT_GPU_COUNT_NVIDIA_SMI}+1")
                            # the UUID is not very useful for the user, remove it
                            string(REGEX REPLACE " \\(UUID:.*\\)" "" _gpu_info "${_line}")
                            if (NOT _gpu_info STREQUAL "")
                                list(APPEND DETECT_GPU_INFO "${_gpu_info}")
                            endif()
                        endif()
                    endforeach()
    
                    check_num_gpu_info(${DETECT_GPU_COUNT_NVIDIA_SMI} DETECT_GPU_INFO)
                    set(DETECT_GPU_COUNT ${DETECT_GPU_COUNT_NVIDIA_SMI})
                endif()
            endif()
    

    One might also query linux /proc or lspci. See fully-worked CMake example at https://github.com/gromacs/gromacs/blob/master/cmake/gmxDetectGpu.cmake

提交回复
热议问题