|
| 1 | +/** |
| 2 | +# Copyright (c) Advanced Micro Devices, Inc. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package list |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/ROCm/container-toolkit/internal/amdgpu" |
| 24 | + "github.com/urfave/cli/v2" |
| 25 | +) |
| 26 | + |
| 27 | +func AddNewCommand() *cli.Command { |
| 28 | + gpuListCmd := cli.Command{ |
| 29 | + Name: "list", |
| 30 | + Usage: "List AMD GPUs with their UUIDs", |
| 31 | + UsageText: "amd-ctk gpu list", |
| 32 | + Action: func(c *cli.Context) error { |
| 33 | + return performAction(c) |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + return &gpuListCmd |
| 38 | +} |
| 39 | + |
| 40 | +func performAction(c *cli.Context) error { |
| 41 | + devs, err := amdgpu.GetAMDGPUs() |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to list AMD devices: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + uuidToGPUIdMap, err := amdgpu.GetUniqueIdToDeviceIndexMap() |
| 47 | + if err != nil { |
| 48 | + uuidToGPUIdMap = make(map[string][]int) |
| 49 | + } |
| 50 | + |
| 51 | + gpuIdToUUIDMap := make(map[int]string) |
| 52 | + for uuid, gpuIds := range uuidToGPUIdMap { |
| 53 | + if strings.HasPrefix(uuid, "0x") || strings.HasPrefix(uuid, "0X") { |
| 54 | + uuid = uuid[2:] |
| 55 | + } |
| 56 | + uuid = "0x" + strings.ToUpper(uuid) |
| 57 | + for _, gpuId := range gpuIds { |
| 58 | + gpuIdToUUIDMap[gpuId] = uuid |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + suffix := "devices" |
| 63 | + if len(devs) == 1 { |
| 64 | + suffix = "device" |
| 65 | + } |
| 66 | + fmt.Printf("Found %v AMD GPU %s\n", len(devs), suffix) |
| 67 | + |
| 68 | + fmt.Println(strings.Repeat("-", 75)) |
| 69 | + fmt.Printf("%-10s%-25s%-40s\n", "GPU Id", "UUID", "DRM Devices") |
| 70 | + fmt.Println(strings.Repeat("-", 75)) |
| 71 | + for idx, dev := range devs { |
| 72 | + uuid := gpuIdToUUIDMap[idx] |
| 73 | + if uuid == "" { |
| 74 | + uuid = "N/A" |
| 75 | + } |
| 76 | + |
| 77 | + var renderDevs []string |
| 78 | + for _, dd := range dev.DrmDevices { |
| 79 | + if !strings.HasPrefix(dd, "/dev/dri/card") { |
| 80 | + renderDevs = append(renderDevs, dd) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + drmStr := strings.Join(renderDevs, ", ") |
| 85 | + fmt.Printf("%-10v%-25s%-40s\n", idx, uuid, drmStr) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
0 commit comments