11using System ;
2+ using System . Diagnostics ;
23using System . IO ;
34using System . Runtime . InteropServices ;
45using System . Text . Json ;
6+ using System . Text . RegularExpressions ;
57
68namespace LLama . Native
79{
@@ -10,7 +12,8 @@ namespace LLama.Native
1012 /// </summary>
1113 /// <param name="OSPlatform"></param>
1214 /// <param name="CudaMajorVersion"></param>
13- public record class SystemInfo ( OSPlatform OSPlatform , int CudaMajorVersion )
15+ /// <param name="VulkanVersion"></param>
16+ public record class SystemInfo ( OSPlatform OSPlatform , int CudaMajorVersion , string ? VulkanVersion )
1417 {
1518 /// <summary>
1619 /// Get the system information of the current machine.
@@ -37,8 +40,108 @@ public static SystemInfo Get()
3740 throw new PlatformNotSupportedException ( ) ;
3841 }
3942
40- return new SystemInfo ( platform , GetCudaMajorVersion ( ) ) ;
43+ return new SystemInfo ( platform , GetCudaMajorVersion ( ) , GetVulkanVersion ( ) ) ;
4144 }
45+
46+ #region Vulkan version
47+ private static string ? GetVulkanVersion ( )
48+ {
49+ // Get Vulkan Summary
50+ string ? vulkanSummary = GetVulkanSummary ( ) ;
51+
52+ // If we have a Vulkan summary
53+ if ( vulkanSummary != null )
54+ {
55+ // Extract Vulkan version from summary
56+ string ? vulkanVersion = ExtractVulkanVersionFromSummary ( vulkanSummary ) ;
57+
58+ // If we have a Vulkan version
59+ if ( vulkanVersion != null )
60+ {
61+ // Return the Vulkan version
62+ return vulkanVersion ;
63+ }
64+ }
65+
66+ // Return null if we failed to get the Vulkan version
67+ return null ;
68+ }
69+
70+ private static string ? GetVulkanSummary ( )
71+ {
72+ // Note: on Linux, this requires `vulkan-tools` to be installed. (`sudo apt install vulkan-tools`)
73+ try
74+ {
75+ // Set up the process start info
76+ ProcessStartInfo start = new ( )
77+ {
78+ FileName = "vulkaninfo" ,
79+ Arguments = "--summary" ,
80+ RedirectStandardOutput = true ,
81+ UseShellExecute = false ,
82+ CreateNoWindow = true
83+ } ;
84+
85+ // Start the process
86+ Process process = new ( )
87+ {
88+ StartInfo = start
89+ } ;
90+ process . Start ( ) ;
91+
92+ // Read the output to a string
93+ string output = process . StandardOutput . ReadToEnd ( ) ;
94+
95+ // Wait for the process to exit
96+ process . WaitForExit ( ) ;
97+
98+ // Return the output
99+ return output ;
100+ }
101+ catch ( Exception e )
102+ {
103+ //Console.WriteLine(e);
104+
105+ // Return null if we failed to get the Vulkan version
106+ return null ;
107+ }
108+ }
109+
110+ static string ? ExtractVulkanVersionFromSummary ( string vulkanSummary )
111+ {
112+ // We have three ways of parsing the Vulkan version from the summary (output is a different between Windows and Linux)
113+ // 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
114+ // I have left the other patterns in, in case we need them in the future
115+
116+ // Output on linux : 4206847 (1.3.255)
117+ // Output on windows : 1.3.255
118+ string pattern = @"apiVersion\s*=\s*([^\r\n]+)" ;
119+
120+ // Output on linux : 4206847
121+ // Output on windows : 1.3.255
122+ //string pattern = @"apiVersion\s*=\s*([\d\.]+)";
123+
124+ // Output on linux : 1.3.255
125+ // Output on windows : 1.3.255
126+ //string pattern = @"apiVersion\s*=\s*(?:\d+\s*)?(?:\(\s*)?([\d]+\.[\d]+\.[\d]+)(?:\s*\))?";
127+
128+ // Create a Regex object to match the pattern
129+ Regex regex = new Regex ( pattern ) ;
130+
131+ // Match the pattern in the input string
132+ Match match = regex . Match ( vulkanSummary ) ;
133+
134+ // If a match is found
135+ if ( match . Success )
136+ {
137+ // Return the version number
138+ return match . Groups [ 1 ] . Value ;
139+ }
140+
141+ // Return null if no match is found
142+ return null ;
143+ }
144+ #endregion
42145
43146 #region CUDA version
44147 private static int GetCudaMajorVersion ( )
0 commit comments