我有两个 Cuda 安装,分别是 11.8 和 12,并且想在它们之间切换,而无需手动编辑 .bashrc 文件并再次获取它。
为此,我想出了在 .bashrc 中放置以下函数
export CUDA_11=/usr/local/cuda-11.8/
export CUDA_12=/usr/local/cuda-12.0/
#Default Cuda12.0 will be loaded, use this to unload 12 and load 11.8
function switchCuda {
local pathToUnset=$1"bin/"
echo $pathToUnset
local lib64ToUnset=$1"lib64/"
local pathToSet=$2"bin/"
local ldPathToSet=$2"lib64/"
local currentPath=:$PATH:
unset PATH
local removedPath=${currentPath/:$pathToUnset:/:}
removedPath=${removedPath%:}
removedPath=${removedPath#:}
PATH=$removedPath
export $PATH
currentPath=:$LD_LIBRARY_PATH:
unset LD_LIBRARY_PATH
local modifiedLDPath=${currentPath/:$lib64ToUnset:/:}
modifiedLDPath=${modifiedLDPath%:}
$modifiedLDPath=${modifiedLDPath#:}
LD_LIBRARY_PATH=$modifiedLDPath
export $LD_LIBRARY_PATH
#echo "I AM DONE UNSETTING THE PATH"
if [$2 == "" ]; then
echo "Simply unsetting cuda path, as provided cuda path is empty. use loadCuda to load the desired path"; return;
fi
export PATH=$PATH:$2
export PATH=$PATH:$pathToSet
export LD_LIBRARY_PATH=$ldPathToSet${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
}
export -f switchCuda
function loadCuda {
export PATH=$PATH:$1
export PATH=$PATH:$1bin
export LD_LIBRARY_PATH=$1lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
}
export -f loadCuda
上述函数的预期结果应如下 -
#in a fresh terminal
nvcc --version (should result in 12, as by default cuda 12 is added to path)
switchCuda ${CUDA_12} ${CUDA_11}
nvcc --version (should be 11.8, as cuda 11.8 will now be in path, nobody should be able to find 12)
但是,当我从命令行调用此函数时,出现第一个错误如下 -
bash: export: `/opt/ros/noetic/bin:/home/atharva/ros_catkin_pkg/install_isolated/bin:/opt/intel/oneapi/vtune/2022.2.0/bin64:/opt/intel/oneapi/vpl/2022.1.0/bin:/opt/intel/oneapi/mpi/2021.6.0//libfabric/bin:/opt/intel/oneapi/mpi/2021.6.0//bin:/opt/intel/oneapi/mkl/2022.1.0/bin/intel64:/opt/intel/oneapi/itac/2021.6.0/bin:/opt/intel/oneapi/intelpython/latest/bin:/opt/intel/oneapi/intelpython/latest/condabin:/opt/intel/oneapi/inspector/2022.1.0/bin64:/opt/intel/oneapi/dpcpp-ct/2022.1.0/bin:/opt/intel/oneapi/dev-
utilities/2021.6.0/bin:/opt/intel/oneapi/debugger/2021.6.0/gdb/intel64/bin:/opt/intel/oneapi/compiler/2022.1.0/linux/lib/oclfpga/bin:/opt/intel/oneapi/compiler/2022.1.0/linux/bin/intel64:/opt/intel/oneapi/compiler/2022.1.0/linux/bin:/opt/intel/oneapi/clck/2021.6.0/bin/intel64:/opt/intel/oneapi/advisor/2022.1.0/bin64:/home/atharva/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:
/usr/local/cuda-12.0:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/:/usr/local/cuda-11.8/:/usr/local/cuda-11.8/+bin/:/usr/local/cuda-12.0/:/usr/local/cuda-12.0/bin:/home/atharva/TensorRT/TensorRT/bin/': **not a valid identifier**
上述错误信息的最后一行如下 -
12.0/bin:/home/atharva/TensorRT/TensorRT/bin/': **not a valid identifier**
我认为存在多个问题 -
- 字符串替换不正确
- 即使删除后,cuda_12 仍然会出现在路径中。
- 另外,还有另一个问题,假设我运行 switchCuda {CUDA_12}“”。它应该完全删除 cuda,并且执行类似操作
nvcc --version
应该会导致command not found
。但是,它仍然能够找到 nvcc。为什么会这样?我该如何实现我想要的结果?
短暂性脑缺血发作