|
5 | 5 | import time |
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
8 | | -from ..detector import Devices, detect_devices |
| 8 | +from ..detector import ( |
| 9 | + Devices, |
| 10 | + Topology, |
| 11 | + detect_devices, |
| 12 | + get_devices_topologies, |
| 13 | + group_devices_by_manufacturer, |
| 14 | +) |
9 | 15 | from .__types__ import SubCommand |
10 | 16 |
|
11 | 17 | if TYPE_CHECKING: |
@@ -49,27 +55,67 @@ def __init__(self, args: Namespace): |
49 | 55 | self.watch = args.watch |
50 | 56 |
|
51 | 57 | def run(self): |
52 | | - try: |
53 | | - while True: |
54 | | - devs: Devices = detect_devices(fast=False) |
55 | | - print("\033[2J\033[H", end="") |
56 | | - match self.format.lower(): |
57 | | - case "json": |
58 | | - print(format_devices_json(devs)) |
59 | | - case _: |
60 | | - # Group devs by manufacturer |
61 | | - grouped_devs: dict[str, Devices] = {} |
62 | | - for dev in devs: |
63 | | - if dev.manufacturer not in grouped_devs: |
64 | | - grouped_devs[dev.manufacturer] = [] |
65 | | - grouped_devs[dev.manufacturer].append(dev) |
66 | | - for manu in sorted(grouped_devs.keys()): |
67 | | - print(format_devices_table(grouped_devs[manu])) |
68 | | - if not self.watch: |
69 | | - break |
70 | | - time.sleep(self.watch) |
71 | | - except KeyboardInterrupt: |
| 58 | + while True: |
| 59 | + devs: Devices = detect_devices(fast=False) |
72 | 60 | print("\033[2J\033[H", end="") |
| 61 | + match self.format.lower(): |
| 62 | + case "json": |
| 63 | + print(format_devices_json(devs)) |
| 64 | + case _: |
| 65 | + # Group devices by manufacturer. |
| 66 | + group_devs = group_devices_by_manufacturer(devs) |
| 67 | + if not group_devs: |
| 68 | + print("No GPUs detected.") |
| 69 | + else: |
| 70 | + # Print each group separately. |
| 71 | + for devs in group_devs.values(): |
| 72 | + print(format_devices_table(devs)) |
| 73 | + if not self.watch: |
| 74 | + break |
| 75 | + time.sleep(self.watch) |
| 76 | + |
| 77 | + |
| 78 | +class GetDevicesTopologySubCommand(SubCommand): |
| 79 | + """ |
| 80 | + Command to detect GPUs topology. |
| 81 | + """ |
| 82 | + |
| 83 | + format: str = "table" |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def register(parser: _SubParsersAction): |
| 87 | + topo_parser = parser.add_parser( |
| 88 | + "topology", |
| 89 | + help="Detect GPUs topology", |
| 90 | + aliases=["topo"], |
| 91 | + ) |
| 92 | + |
| 93 | + topo_parser.add_argument( |
| 94 | + "--format", |
| 95 | + type=str, |
| 96 | + choices=["table", "json"], |
| 97 | + default="table", |
| 98 | + help="Output format", |
| 99 | + ) |
| 100 | + |
| 101 | + topo_parser.set_defaults(func=GetDevicesTopologySubCommand) |
| 102 | + |
| 103 | + def __init__(self, args: Namespace): |
| 104 | + self.format = args.format |
| 105 | + |
| 106 | + def run(self): |
| 107 | + topologies = get_devices_topologies(fast=False) |
| 108 | + print("\033[2J\033[H", end="") |
| 109 | + if not topologies: |
| 110 | + print("No GPU topology information available.") |
| 111 | + return |
| 112 | + |
| 113 | + match self.format.lower(): |
| 114 | + case "json": |
| 115 | + print(format_topologies_json(topologies)) |
| 116 | + case _: |
| 117 | + for topo in topologies: |
| 118 | + print(format_topology_table(topo)) |
73 | 119 |
|
74 | 120 |
|
75 | 121 | def format_devices_json(devs: Devices) -> str: |
@@ -151,3 +197,84 @@ def format_devices_table(devs: Devices) -> str: |
151 | 197 |
|
152 | 198 | # Combine all parts |
153 | 199 | return os.linesep.join(header_lines + device_lines + footer_lines) |
| 200 | + |
| 201 | + |
| 202 | +def format_topology_table(topo: Topology) -> str: |
| 203 | + content = topo.stringify() |
| 204 | + |
| 205 | + # Column headers |
| 206 | + col_headers = [str(topo.manufacturer).upper()] |
| 207 | + col_headers += ["Device " + str(idx) for idx in range(len(content))] |
| 208 | + col_headers += [ |
| 209 | + "CPU Affinity", |
| 210 | + "NUMA Affinity", |
| 211 | + ] |
| 212 | + # Gather all rows to determine max width for each column |
| 213 | + rows = [] |
| 214 | + for row_idx, row_devs in enumerate(content): |
| 215 | + row = ["Device " + str(row_idx), *row_devs] |
| 216 | + rows.append(row) |
| 217 | + |
| 218 | + # Calculate max width for each column |
| 219 | + col_widths = [len(header) for header in col_headers] |
| 220 | + for row in rows: |
| 221 | + for i, cell in enumerate(row): |
| 222 | + col_widths[i] = max(col_widths[i], len(str(cell))) |
| 223 | + |
| 224 | + # Add padding |
| 225 | + col_widths = [w + 2 for w in col_widths] |
| 226 | + |
| 227 | + # Calculate table width |
| 228 | + width = sum(col_widths) + len(col_widths) + 1 |
| 229 | + |
| 230 | + # Header section |
| 231 | + header_lines = [ |
| 232 | + "+" + "-" * (width - 2) + "+", |
| 233 | + ] |
| 234 | + |
| 235 | + # Column header line |
| 236 | + col_header_line = "|" |
| 237 | + for i, header in enumerate(col_headers): |
| 238 | + col_header_line += f" {header.center(col_widths[i] - 2)} |" |
| 239 | + header_lines.append(col_header_line) |
| 240 | + |
| 241 | + # Separator line |
| 242 | + separator = "|" + "|".join(["-" * w for w in col_widths]) + "|" |
| 243 | + header_lines.append(separator) |
| 244 | + |
| 245 | + # Topology rows |
| 246 | + topology_lines = [] |
| 247 | + for row in rows: |
| 248 | + row_line = "|" |
| 249 | + for j, data in enumerate(row): |
| 250 | + cell = str(data) |
| 251 | + # Truncate if too long |
| 252 | + if len(cell) > col_widths[j] - 2: |
| 253 | + cell = cell[: col_widths[j] - 5] + "..." |
| 254 | + row_line += f" {cell.ljust(col_widths[j] - 2)} |" |
| 255 | + topology_lines.append(row_line) |
| 256 | + |
| 257 | + # Footer section |
| 258 | + footer_lines = [ |
| 259 | + "+" + "-" * (width - 2) + "+", |
| 260 | + ] |
| 261 | + |
| 262 | + # Legend |
| 263 | + legend_lines = [ |
| 264 | + "", |
| 265 | + "Legend (from nearest to farthest):", |
| 266 | + " X = Self", |
| 267 | + " LINK = Connection traversing with High-Speed Link (e.g., NVIDIA NVLink, Ascend HCCS)", |
| 268 | + " PIX = Connection traversing at most a single PCIe bridge", |
| 269 | + " PXB = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge)", |
| 270 | + " PHB = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU)", |
| 271 | + " NODE = Connection traversing PCIe and the interconnect between NUMA nodes", |
| 272 | + " SYS = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI)", |
| 273 | + ] |
| 274 | + |
| 275 | + # Combine all parts |
| 276 | + return os.linesep.join(header_lines + topology_lines + footer_lines + legend_lines) |
| 277 | + |
| 278 | + |
| 279 | +def format_topologies_json(topologies: list[Topology]) -> str: |
| 280 | + return json.dumps([topo.to_dict() for topo in topologies], indent=2) |
0 commit comments