Linux上的CUDA发行版曾经有一个名为version.txt的文件,该文件的内容如下:
CUDA Version 10.2.89这是非常有用的。但是,从CUDA 11.1开始,此文件不再存在。
如何在Linux上从命令行检查/path/to/cuda/toolkit,确定我正在查看的确切版本?包括subversion吗?
发布于 2021-02-02 07:56:53
(根据@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-例如:您将获得用于下载CUDA11.2的11.2.67,这是本周在NVIDIA网站上提供的。
完整的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发布于 2021-02-02 19:16:36
下面的python代码在Windows和Linux上都能很好地工作,我已经用各种CUDA (大多数是8-11.2)测试了它。
它通过一系列猜测(检查环境变量、nvcc位置或默认安装路径)搜索cuda_path,然后从nvcc --version的输出中获取CUDA版本。没有使用@einpoklum的样式regexp,它只是假设在nvcc --version的输出中只有一个release字符串,但这可以简单地进行检查。
如果您有一个已知的路径要查询,您也可以只使用第一个函数。
将它添加为@einpoklum answer的一个额外部分,只是在python中做了同样的事情。
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()https://stackoverflow.com/questions/66001729
复制相似问题