如何确定完整的 CUDA 版本 + 颠覆?

How can I determine the full CUDA version + subversion?

Linux 上的 CUDA 发行版曾经有一个名为 version.txt 的文件,其中读取,例如:

CUDA Version 10.2.89

这很有用。但是,自 CUDA 11.1 起,此文件不再存在。

如何在 Linux 和命令行中确定并检查 /path/to/cuda/toolkit 我正在查看的确切版本?包括颠覆?

(根据@RobertCrovella 的评论回答)

这样做就可以了:

/path/to/cuda/toolkit/bin/nvcc --version | egrep -o "V[0-9]+.[0-9]+.[0-9]+" | cut -c2-

当然,对于当前选择和配置使用的 CUDA 版本,只需采用路径上的 nvcc

nvcc --version | egrep -o "V[0-9]+.[0-9]+.[0-9]+" | cut -c2-

例如:本周可在 NVIDIA 网站上下载 CUDA 11.2,您将获得 11.2.67

完整的 nvcc --version 输出将是:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Nov_30_19:08:53_PST_2020
Cuda compilation tools, release 11.2, V11.2.67
Build cuda_11.2.r11.2/compiler.29373293_0

以下 python 代码适用于 Windows 和 Linux,我已经用各种 CUDA(8-11.2,其中大部分)对其进行了测试。

它通过一系列猜测(检查环境变量、nvcc 位置或默认安装路径)搜索 cuda_path,然后从 nvcc --version 的输出中获取 CUDA 版本。不使用@einpoklum 的样式正则表达式,它只是假设 nvcc --version 的输出中只有一个 release 字符串,但可以简单地检查一下。

如果您有已知的查询路径,您也可以只使用第一个函数。

将它添加为@einpoklum 答案的额外内容,做同样的事情,只是在 python.

From TIGRE.

import glob
import os
from os.path import join as pjoin
import subprocess
import sys


def get_cuda_version(cuda_home):
    """Locate the CUDA version
    """
    version_file = os.path.join(cuda_home, "version.txt")
    try:
        if os.path.isfile(version_file):
            with open(version_file) as f:
                version_str = f.readline().replace('\n', '').replace('\r', '')
                return version_str.split(" ")[2][:4]
        else:
            version_str = subprocess.check_output([os.path.join(cuda_home,"bin","nvcc"),"--version"])
            version_str=str(version_str).replace('\n', '').replace('\r', '')
            idx=version_str.find("release")
            return version_str[idx+len("release "):idx+len("release ")+4]
    except:
        raise RuntimeError("Cannot read cuda version file") 
def locate_cuda():
    """Locate the CUDA environment on the system

    Returns a dict with keys 'home', 'include' and 'lib64'
    and values giving the absolute path to each directory.

    Starts by looking for the CUDA_HOME or CUDA_PATH env variable. If not found, everything
    is based on finding 'nvcc' in the PATH.
    """
    # Guess #1
    cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')
    if cuda_home is None:
        # Guess #2
        try:
            which = 'where' if IS_WINDOWS else 'which'
            nvcc = subprocess.check_output(
                [which, 'nvcc']).decode().rstrip('\r\n')
            cuda_home = os.path.dirname(os.path.dirname(nvcc))
        except subprocess.CalledProcessError:
            # Guess #3
            if IS_WINDOWS:
                cuda_homes = glob.glob(
                    'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v*.*')
                if len(cuda_homes) == 0:
                    cuda_home = ''
                else:
                    cuda_home = cuda_homes[0]
            else:
                cuda_home = '/usr/local/cuda'
            if not os.path.exists(cuda_home):
                cuda_home = None
    version = get_cuda_version(cuda_home)
    cudaconfig = {'home': cuda_home,
                  'include': pjoin(cuda_home, 'include'),
                  'lib64': pjoin(cuda_home, pjoin('lib', 'x64') if IS_WINDOWS else 'lib64')}
    if not all([os.path.exists(v) for v in cudaconfig.values()]):
        raise EnvironmentError(
            'The CUDA  path could not be located in $PATH, $CUDA_HOME or $CUDA_PATH. '
            'Either add it to your path, or set $CUDA_HOME or $CUDA_PATH.')

    return cudaconfig, version


CUDA, CUDA_VERSION = locate_cuda()