@@ -41,31 +41,26 @@ public static SystemInfo Get()
41
41
42
42
return new SystemInfo ( platform , GetCudaMajorVersion ( ) , GetVulkanVersion ( ) ) ;
43
43
}
44
-
45
44
#region Vulkan version
46
45
private static string ? GetVulkanVersion ( )
47
46
{
48
47
// Get Vulkan Summary
49
48
string ? vulkanSummary = GetVulkanSummary ( ) ;
50
-
51
49
// If we have a Vulkan summary
52
50
if ( vulkanSummary != null )
53
51
{
54
52
// Extract Vulkan version from summary
55
53
string ? vulkanVersion = ExtractVulkanVersionFromSummary ( vulkanSummary ) ;
56
-
57
54
// If we have a Vulkan version
58
55
if ( vulkanVersion != null )
59
56
{
60
57
// Return the Vulkan version
61
58
return vulkanVersion ;
62
59
}
63
60
}
64
-
65
61
// Return null if we failed to get the Vulkan version
66
62
return null ;
67
63
}
68
-
69
64
private static string ? GetVulkanSummary ( )
70
65
{
71
66
// Note: on Linux, this requires `vulkan-tools` to be installed. (`sudo apt install vulkan-tools`)
@@ -85,7 +80,6 @@ public static SystemInfo Get()
85
80
}
86
81
} ;
87
82
var ( exitCode , output , error , ok ) = process . SafeRun ( TimeSpan . FromSeconds ( 12 ) ) ;
88
-
89
83
// If ok return the output else return null
90
84
return ok ? output :
91
85
null ;
@@ -96,38 +90,30 @@ public static SystemInfo Get()
96
90
return null ;
97
91
}
98
92
}
99
-
100
93
static string ? ExtractVulkanVersionFromSummary ( string vulkanSummary )
101
94
{
102
95
// We have three ways of parsing the Vulkan version from the summary (output is a different between Windows and Linux)
103
96
// For now, I have decided to go with the full version number, and leave it up to the user to parse it further if needed
104
97
// I have left the other patterns in, in case we need them in the future
105
-
106
98
// Output on linux : 4206847 (1.3.255)
107
99
// Output on windows : 1.3.255
108
100
string pattern = @"apiVersion\s*=\s*([^\r\n]+)" ;
109
-
110
101
// Output on linux : 4206847
111
102
// Output on windows : 1.3.255
112
103
//string pattern = @"apiVersion\s*=\s*([\d\.]+)";
113
-
114
104
// Output on linux : 1.3.255
115
105
// Output on windows : 1.3.255
116
106
//string pattern = @"apiVersion\s*=\s*(?:\d+\s*)?(?:\(\s*)?([\d]+\.[\d]+\.[\d]+)(?:\s*\))?";
117
-
118
107
// Create a Regex object to match the pattern
119
108
Regex regex = new Regex ( pattern ) ;
120
-
121
109
// Match the pattern in the input string
122
110
Match match = regex . Match ( vulkanSummary ) ;
123
-
124
111
// If a match is found
125
112
if ( match . Success )
126
113
{
127
114
// Return the version number
128
115
return match . Groups [ 1 ] . Value ;
129
116
}
130
-
131
117
// Return null if no match is found
132
118
return null ;
133
119
}
@@ -145,37 +131,37 @@ private static int GetCudaMajorVersion()
145
131
{
146
132
return - 1 ;
147
133
}
148
-
149
134
//Ensuring cuda bin path is reachable. Especially for MAUI environment.
150
135
string cudaBinPath = Path . Combine ( cudaPath , "bin" ) ;
151
-
152
136
if ( Directory . Exists ( cudaBinPath ) )
153
137
{
154
138
AddDllDirectory ( cudaBinPath ) ;
155
139
}
156
-
157
140
version = GetCudaVersionFromPath ( cudaPath ) ;
158
141
}
159
142
else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
160
143
{
144
+ string ? env_version = Environment . GetEnvironmentVariable ( "CUDA_VERSION" ) ;
145
+ if ( env_version is not null )
146
+ {
147
+ return ExtractMajorVersion ( ref env_version ) ;
148
+ }
161
149
// List of default cuda paths
162
150
string [ ] defaultCudaPaths =
163
151
[
164
152
"/usr/local/bin/cuda" ,
165
153
"/usr/local/cuda" ,
166
154
] ;
167
-
168
155
// Loop through every default path to find the version
169
156
foreach ( var path in defaultCudaPaths )
170
157
{
171
158
// Attempt to get the version from the path
172
159
version = GetCudaVersionFromPath ( path ) ;
173
-
160
+
174
161
// If a CUDA version is found, break the loop
175
162
if ( ! string . IsNullOrEmpty ( version ) )
176
163
break ;
177
164
}
178
-
179
165
if ( string . IsNullOrEmpty ( version ) )
180
166
{
181
167
cudaPath = Environment . GetEnvironmentVariable ( "LD_LIBRARY_PATH" ) ;
@@ -193,17 +179,18 @@ private static int GetCudaMajorVersion()
193
179
}
194
180
}
195
181
}
196
-
197
182
if ( string . IsNullOrEmpty ( version ) )
198
183
return - 1 ;
199
184
185
+ return ExtractMajorVersion ( ref version ) ;
186
+ }
187
+ private static int ExtractMajorVersion ( ref string version )
188
+ {
200
189
version = version . Split ( '.' ) [ 0 ] ;
201
190
if ( int . TryParse ( version , out var majorVersion ) )
202
191
return majorVersion ;
203
-
204
192
return - 1 ;
205
193
}
206
-
207
194
private static string GetCudaVersionFromPath ( string cudaPath )
208
195
{
209
196
try
@@ -226,12 +213,10 @@ private static string GetCudaVersionFromPath(string cudaPath)
226
213
return string . Empty ;
227
214
}
228
215
}
229
-
230
216
// Put it here to avoid calling NativeApi when getting the cuda version.
231
217
[ DllImport ( "kernel32.dll" , CharSet = CharSet . Unicode , SetLastError = true ) ]
232
218
internal static extern int AddDllDirectory ( string NewDirectory ) ;
233
-
234
219
private const string cudaVersionFile = "version.json" ;
235
220
#endregion
236
221
}
237
- }
222
+ }
0 commit comments