[ 前言 ] 切换不同版本 cuda 前提须安装多个版本的 cuda

cuda 安装可参考CUDA的下载与安装,去Nvidia官网下载安装所需的 cuda 版本


[ 基于Windows 下 切换不同版本 cuda ] :

1. 修改系统环境变量方法

可以参看【CUDA】win10切换不同版本的CUDA,此方法适用于长时间更换cuda 版本,若仅需在某个虚拟环境(env)中切换成对应版本,可参照第二点方法

2. 编写脚本切换 cuda 版本

详细可以参看 conda 官方文档 Windows 下设置环境变量

  CmdPrompt 定位到需要切换 cuda 版本的虚拟环境(env)下:

cd C:\ProgramData\Anaconda3\envs\ env # Anaconda 默认安装在C盘此目录下 env:为所需切换cuda版本的虚拟环境
cd /d D:\Anaconda3\envs\ env   #Anaconda 安装在其他位置

  env 下创建存放脚本的文件夹:

mkdir .\etc\conda\activate.d #创建activate.d文件夹
mkdir .\etc\conda\deactivate.d #创建deactivate.d文件夹

   在新创建的两个文件夹中创建 env_vars.bat 脚本文件,将cuda版本添加到临时系统变量中:

(将CUDA_PATH、CUDA_NVVP、CUDA_lib设置为所需cuda下的bin、libnvvp、lib\x64对应绝对路径)

# activate.d\env_vars.bat 
@set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin
@set CUDA_NVVP=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\libnvvp
@set CUDA_lib=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64
@set OLD_PATH=%PATH%
@set PATH=%CUDA_PATH%;%CUDA_NVVP%;%CUDA_lib%;%PATH%;
# deactivate.d\env_vars.bat 
@set PATH=%OLD_PATH%

   此时激活虚拟环境 conda activate env ,运行 nvcc -V 查看版本,即发现cuda版本已经切换, 此方法重启终端后,cuda 环境会恢复至原来版本

若发现激活env时,修改临时系统变量的脚本并未生效,可以在激活后,手动执行脚本即可


[ 基于Linux下 切换不同版本 cuda ] :

1. 修改系统环境变量方法

详细可以参看【CUDA】Linux切换不同版本的CUDA, 此方法适用于长时间更换cuda 版本,若仅需在临时切换成对应版本,可参照第二点方法

   ls /usr/local | grep cuda列出已安装的 CUDA 版本

   vim ~/.bashrc 将要使用的CUDA版本添加到环境变量,将如下内容添加进最后一行

# <version> 须切换的CUDA版本号
export PATH=/usr/local/cuda-<version>/bin${PATH:+:${PATH}} 
export LD_LIBRARY_PATH=/usr/local/cuda-<version>/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

   source ~/.bashrc 更新环境变量即可
   此时激活虚拟环境 conda activate env ,运行 nvcc -V 查看版本,即发现cuda版本已经切换

2. 编写脚本切换 cuda 版本

详细可以参考 Phohenecker 写的CUDA版本切换脚本,P神在github上有介绍详细的用法

# switch-cuda.sh 脚本代码如下
#!/usr/bin/env bash

# Copyright (c) 2018 Patrick Hohenecker
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# author:   Patrick Hohenecker <mail@paho.at>
# version:  2018.1
# date:     May 15, 2018


set -e


# ensure that the script has been sourced rather than just executed
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
    echo "Please use 'source' to execute switch-cuda.sh!"
    exit 1
fi

INSTALL_FOLDER="/usr/local"  # the location to look for CUDA installations at
TARGET_VERSION=${1}          # the target CUDA version to switch to (if provided)

# if no version to switch to has been provided, then just print all available CUDA installations
if [[ -z ${TARGET_VERSION} ]]; then
    echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
    ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
        echo "* ${line}"
    done
    set +e
    return
# otherwise, check whether there is an installation of the requested CUDA version
elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
    echo "No installation of CUDA ${TARGET_VERSION} has been found!"
    set +e
    return
fi

# the path of the installation to use
cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"

# filter out those CUDA entries from the PATH that are not needed anymore
path_elements=(${PATH//:/ })
new_path="${cuda_path}/bin"
for p in "${path_elements[@]}"; do
    if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
        new_path="${new_path}:${p}"
    fi
done

# filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
ld_path_elements=(${LD_LIBRARY_PATH//:/ })
new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
for p in "${ld_path_elements[@]}"; do
    if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
        new_ld_path="${new_ld_path}:${p}"
    fi
done

# update environment variables
export CUDA_HOME="${cuda_path}"
export CUDA_ROOT="${cuda_path}"
export LD_LIBRARY_PATH="${new_ld_path}"
export PATH="${new_path}"

echo "Switched to CUDA ${TARGET_VERSION}."

set +e
return

   vi switch-cuda.sh 将上述 Phohenecker 编写的脚本内容填入新建文件当中
   source switch-cuda.sh 执行脚本会扫描当前已安装的 cuda版本并显示在终端上
   source switch-cuda.sh version version 为须切换的CUDA版本号,脚本基于export 语句,重启终端后,cuda环境还是会恢复到原先版本

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐