nikigoli commited on
Commit
ea78ade
1 Parent(s): 0d11f1e

Create switch_cuda.sh from https://github.com/phohenecker/switch-cuda/blob/master/switch-cuda.sh

Browse files
Files changed (1) hide show
  1. switch_cuda.sh +85 -0
switch_cuda.sh ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ # Copyright (c) 2018 Patrick Hohenecker
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ # author: Patrick Hohenecker <mail@paho.at>
24
+ # version: 2018.1
25
+ # date: May 15, 2018
26
+
27
+
28
+ set -e
29
+
30
+
31
+ # ensure that the script has been sourced rather than just executed
32
+ if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
33
+ echo "Please use 'source' to execute switch-cuda.sh!"
34
+ exit 1
35
+ fi
36
+
37
+ INSTALL_FOLDER="/usr/local" # the location to look for CUDA installations at
38
+ TARGET_VERSION=${1} # the target CUDA version to switch to (if provided)
39
+
40
+ # if no version to switch to has been provided, then just print all available CUDA installations
41
+ if [[ -z ${TARGET_VERSION} ]]; then
42
+ echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
43
+ ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
44
+ echo "* ${line}"
45
+ done
46
+ set +e
47
+ return
48
+ # otherwise, check whether there is an installation of the requested CUDA version
49
+ elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
50
+ echo "No installation of CUDA ${TARGET_VERSION} has been found!"
51
+ set +e
52
+ return
53
+ fi
54
+
55
+ # the path of the installation to use
56
+ cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"
57
+
58
+ # filter out those CUDA entries from the PATH that are not needed anymore
59
+ path_elements=(${PATH//:/ })
60
+ new_path="${cuda_path}/bin"
61
+ for p in "${path_elements[@]}"; do
62
+ if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
63
+ new_path="${new_path}:${p}"
64
+ fi
65
+ done
66
+
67
+ # filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
68
+ ld_path_elements=(${LD_LIBRARY_PATH//:/ })
69
+ new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
70
+ for p in "${ld_path_elements[@]}"; do
71
+ if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
72
+ new_ld_path="${new_ld_path}:${p}"
73
+ fi
74
+ done
75
+
76
+ # update environment variables
77
+ export CUDA_HOME="${cuda_path}"
78
+ export CUDA_ROOT="${cuda_path}"
79
+ export LD_LIBRARY_PATH="${new_ld_path}"
80
+ export PATH="${new_path}"
81
+
82
+ echo "Switched to CUDA ${TARGET_VERSION}."
83
+
84
+ set +e
85
+ return