Debian Codegraph @

rust-interface/examples/amdsmi_get_gpu_info.rs

raw
1 // Copyright (C) 2024 Advanced Micro Devices. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy of
4 // this software and associated documentation files (the "Software"), to deal in
5 // the Software without restriction, including without limitation the rights to
6 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7 // the Software, and to permit persons to whom the Software is furnished to do so,
8 // subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20
21 use amdsmi::*;
22
23 fn main() {
24 // Initialize the AMD SMI library
25 if let Err(e) = amdsmi_init(AmdsmiInitFlagsT::AmdsmiInitAmdGpus) {
26 eprintln!("Failed to initialize AMD SMI: {}", e);
27 return;
28 }
29
30 // Get socket handles
31 let socket_handles = match amdsmi_get_socket_handles() {
32 Ok(handles) => handles,
33 Err(e) => {
34 eprintln!("Failed to get socket handles: {}", e);
35 amdsmi_shut_down().expect("Failed to shutdown AMD SMI");
36 return;
37 }
38 };
39
40 for socket_handle in socket_handles {
41 // Get processor handles for each socket handle
42 let processor_handles = match amdsmi_get_processor_handles(socket_handle) {
43 Ok(handles) => handles,
44 Err(e) => {
45 eprintln!(
46 "Failed to get processor handles for socket {:?}: {}",
47 socket_handle, e
48 );
49 continue;
50 }
51 };
52
53 for processor_handle in processor_handles {
54 // Get GPU ID using the processor handle
55 match amdsmi_get_gpu_id(processor_handle) {
56 Ok(gpu_id) => println!("GPU ID: {}", gpu_id),
57 Err(e) => eprintln!("Failed to get GPU ID: {}", e),
58 }
59
60 // Get GPU revision using the processor handle
61 match amdsmi_get_gpu_revision(processor_handle) {
62 Ok(gpu_revision) => println!("GPU Revision: {}", gpu_revision),
63 Err(e) => eprintln!("Failed to get GPU revision: {}", e),
64 }
65
66 // Get GPU vendor name using the processor handle
67 match amdsmi_get_gpu_vendor_name(processor_handle) {
68 Ok(gpu_vendor_name) => println!("GPU Vendor Name: {}", gpu_vendor_name),
69 Err(e) => eprintln!("Failed to get GPU vendor name: {}", e),
70 }
71
72 // Get GPU VRAM vendor using the processor handle
73 match amdsmi_get_gpu_vram_vendor(processor_handle) {
74 Ok(gpu_vram_vendor) => println!("GPU VRAM Vendor: {}", gpu_vram_vendor),
75 Err(e) => eprintln!("Failed to get GPU VRAM vendor: {}", e),
76 }
77
78 // Get GPU subsystem ID using the processor handle
79 match amdsmi_get_gpu_subsystem_id(processor_handle) {
80 Ok(gpu_subsystem_id) => println!("GPU Subsystem ID: {}", gpu_subsystem_id),
81 Err(e) => eprintln!("Failed to get GPU subsystem ID: {}", e),
82 }
83
84 // Get GPU subsystem name using the processor handle
85 match amdsmi_get_gpu_subsystem_name(processor_handle) {
86 Ok(gpu_subsystem_name) => println!("GPU Subsystem Name: {}", gpu_subsystem_name),
87 Err(e) => eprintln!("Failed to get GPU subsystem name: {}", e),
88 }
89
90 // Get GPU BDF using the processor handle
91 match amdsmi_get_gpu_device_bdf(processor_handle) {
92 Ok(gpu_bdf) => println!("GPU BDF: {}", gpu_bdf),
93 Err(e) => eprintln!("Failed to get GPU BDF: {}", e),
94 }
95
96 // Get GPU BDF ID using the processor handle
97 match amdsmi_get_gpu_bdf_id(processor_handle) {
98 Ok(gpu_bdf_id) => println!("GPU BDF ID: {}", gpu_bdf_id),
99 Err(e) => eprintln!("Failed to get GPU BDF ID: {}", e),
100 }
101
102 println!();
103 }
104 }
105
106 // Shutdown the AMD SMI library
107 if let Err(e) = amdsmi_shut_down() {
108 eprintln!("Failed to shutdown AMD SMI: {}", e);
109 }
110 }