0


[解決方案] conda 虚拟环境中 cuda不同版本進行切換(含Linux 和 Windows)


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

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


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

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

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

第二点方法 

2. 编写脚本切换 cuda 版本

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

Cmd

Prompt

定位到需要切换 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版本号exportPATH=/usr/local/cuda-<version>/bin${PATH:+:${PATH}}exportLD_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 <[email protected]># version:  2018.1# date:     May 15, 2018set -e

# ensure that the script has been sourced rather than just executedif[["${BASH_SOURCE[0]}"="${0}"]];thenecho"Please use 'source' to execute switch-cuda.sh!"exit1fiINSTALL_FOLDER="/usr/local"# the location to look for CUDA installations atTARGET_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 installationsif[[ -z ${TARGET_VERSION}]];thenecho"The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"ls -l "${INSTALL_FOLDER}"|egrep -o "cuda-[0-9]+\\.[0-9]+$"|whileread -r line;doecho"* ${line}"doneset +e
    return# otherwise, check whether there is an installation of the requested CUDA versionelif[[! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"]];thenecho"No installation of CUDA ${TARGET_VERSION} has been found!"set +e
    returnfi# the path of the installation to usecuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"# filter out those CUDA entries from the PATH that are not needed anymorepath_elements=(${PATH//:/ })new_path="${cuda_path}/bin"forpin"${path_elements[@]}";doif[[!${p}=~ ^${INSTALL_FOLDER}/cuda ]];thennew_path="${new_path}:${p}"fidone# filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymoreld_path_elements=(${LD_LIBRARY_PATH//:/ })new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"forpin"${ld_path_elements[@]}";doif[[!${p}=~ ^${INSTALL_FOLDER}/cuda ]];thennew_ld_path="${new_ld_path}:${p}"fidone# update environment variablesexportCUDA_HOME="${cuda_path}"exportCUDA_ROOT="${cuda_path}"exportLD_LIBRARY_PATH="${new_ld_path}"exportPATH="${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环境还是会恢复到原先版本

标签: conda linux windows

本文转载自: https://blog.csdn.net/weixin_43305485/article/details/130413708
版权归原作者 叫我蘑菇先生 所有, 如有侵权,请联系我们删除。

“[解決方案] conda 虚拟环境中 cuda不同版本進行切換(含Linux 和 Windows)”的评论:

还没有评论