-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_cuda.bat
More file actions
174 lines (147 loc) · 5.02 KB
/
install_cuda.bat
File metadata and controls
174 lines (147 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem =========================================
rem default setting: CUDA 12.8
rem =========================================
set "TARGET_VER=12.8"
set "TARGET_VER_ALT=12.8.0"
rem =========================================
rem Check for admin rights and elevate if necessary
rem =========================================
>nul 2>&1 net session
if not %errorlevel%==0 (
echo Administratorrechte sind erforderlich
powershell -NoProfile -Command "Start-Process -Verb RunAs -FilePath '%~f0'"
exit /b
)
rem =========================================
rem User selection of the desired CUDA version
rem (Default remains 12.8 if Enter is pressed or input is invalid)
rem =========================================
echo.
echo ===============================================
echo Select CUDA-Version
echo [1] CUDA 12.8 (Default)
echo [2] CUDA 12.4
echo [3] CUDA 12.1
echo ===============================================
echo.
set "USER_SEL="
set /p "USER_SEL=Please enter your selection (1/2/3) [Default: 1] and Enter: "
rem ---- Normalise input (remove spaces/quotation marks)
if not defined USER_SEL set "USER_SEL=1"
set "USER_SEL=%USER_SEL:"=%"
for /f "tokens=1 delims= " %%A in ("%USER_SEL%") do set "USER_SEL=%%A"
rem ---- Optional: map direct version entries to menu numbers
if /I "%USER_SEL%"=="12.8" set "USER_SEL=1"
if /I "%USER_SEL%"=="12.4" set "USER_SEL=2"
if /I "%USER_SEL%"=="12.1" set "USER_SEL=3"
rem ---- Branch selection (without using an else-if cascade)
if "%USER_SEL%"=="1" goto :sel_128
if "%USER_SEL%"=="2" goto :sel_124
if "%USER_SEL%"=="3" goto :sel_121
echo invalid selection "%USER_SEL%". The default (CUDA 12.8) will be used.
goto :sel_128
:sel_128
set "TARGET_VER=12.8"
set "TARGET_VER_ALT=12.8.0"
set "TARGET_LABEL=CUDA 12.8"
goto :after_choice
:sel_124
set "TARGET_VER=12.4"
set "TARGET_VER_ALT=12.4.1"
set "TARGET_LABEL=CUDA 12.4"
goto :after_choice
:sel_121
set "TARGET_VER=12.1"
set "TARGET_VER_ALT=12.1.0"
set "TARGET_LABEL=CUDA 12.1"
goto :after_choice
:after_choice
echo selected version: %TARGET_LABEL%
echo.
rem =========================================
rem Check Winget
rem =========================================
where winget >nul 2>&1
if errorlevel 1 (
echo Winget was not found. Please install Microsoft App Installer.
exit /b 1
)
rem Update Winget sources
winget source update >nul 2>&1
set "CUDA_VER="
call :detect_installed_version
if not defined CUDA_VER (
echo No existing CUDA installation detected.
goto :install
)
echo Detected CUDA version: !CUDA_VER!
call :compare_versions "!CUDA_VER!" "%TARGET_VER%"
if /I "!COMPARE_RESULT!"=="LT" (
echo Older CUDA version found. Attempting uninstallation...
winget uninstall --id NVIDIA.CUDA -e --silent --accept-package-agreements --accept-source-agreements >nul 2>&1
timeout /t 2 >nul
goto :install
) else (
echo Version is equal to or newer than %TARGET_VER%. Nothing will be installed.
goto :done
)
:install
echo Installation of CUDA %TARGET_VER% via winget is starting
winget install --id Nvidia.CUDA --version %TARGET_VER% -e --silent --accept-package-agreements --accept-source-agreements
if errorlevel 1 (
echo First installation attempt failed. Fallback via %TARGET_VER_ALT%
winget install --id Nvidia.CUDA --version %TARGET_VER_ALT% -e --silent --accept-package-agreements --accept-source-agreements
)
:done
echo Process completed.
exit /b 0
rem -------------------------------------------------------------
rem helper functions
rem -------------------------------------------------------------
:detect_installed_version
rem Determines the installed CUDA version EXCLUSIVELY via nvcc
rem If nvcc is not available: do not set a version → proceed with installation.
set "CUDA_VER="
set "REL_PART="
set "REL_CLEAN="
where nvcc >nul 2>&1
if errorlevel 1 (
rem nvcc not found → no installed version detected
exit /b 0
)
for /f "usebackq tokens=2 delims=," %%A in (`nvcc --version 2^>nul ^| findstr /i "release"`) do (
set "REL_PART=%%~A" rem " release 12.4"
)
if defined REL_PART (
for /f "tokens=2" %%B in ("!REL_PART!") do (
set "REL_CLEAN=%%~B" rem "12.4"
)
)
if defined REL_CLEAN (
for /f "tokens=1,2 delims=." %%M in ("!REL_CLEAN!") do (
if "%%N"=="" (
set "CUDA_VER=%%M.0"
) else (
set "CUDA_VER=%%M.%%N"
)
)
)
exit /b 0
:compare_versions
set "COMPARE_RESULT="
set "A=%~1"
set "B=%~2"
for /f "tokens=1,2 delims=." %%a in ("%A%") do (set "A1=%%a"& set "A2=%%b")
for /f "tokens=1,2 delims=." %%a in ("%B%") do (set "B1=%%a"& set "B2=%%b")
if not defined A2 set "A2=0"
if not defined B2 set "B2=0"
set /a MA=A1, MB=B1
if %MA% LSS %MB% (set "COMPARE_RESULT=LT"& exit /b 0)
if %MA% GTR %MB% (set "COMPARE_RESULT=GT"& exit /b 0)
set /a mi=A2, mj=B2
if %mi% LSS %mj% (set "COMPARE_RESULT=LT"& exit /b 0)
if %mi% GTR %mj% (set "COMPARE_RESULT=GT"& exit /b 0)
set "COMPARE_RESULT=EQ"
exit /b 0