1 //===- AMDGPUArch.cpp - list AMDGPU installed ----------*- C++ -*---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a tool for detecting name of AMDGPU installed in system
10 // using HSA. This tool is used by AMDGPU OpenMP driver.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include <hsa.h>
15 #include <string>
16 #include <vector>
17 
18 static hsa_status_t iterateAgentsCallback(hsa_agent_t Agent, void *Data) {
19   hsa_device_type_t DeviceType;
20   hsa_status_t Status =
21       hsa_agent_get_info(Agent, HSA_AGENT_INFO_DEVICE, &DeviceType);
22 
23   // continue only if device type if GPU
24   if (Status != HSA_STATUS_SUCCESS || DeviceType != HSA_DEVICE_TYPE_GPU) {
25     return Status;
26   }
27 
28   std::vector<std::string> *GPUs =
29       static_cast<std::vector<std::string> *>(Data);
30   char GPUName[64];
31   Status = hsa_agent_get_info(Agent, HSA_AGENT_INFO_NAME, GPUName);
32   if (Status != HSA_STATUS_SUCCESS) {
33     return Status;
34   }
35   GPUs->push_back(GPUName);
36   return HSA_STATUS_SUCCESS;
37 }
38 
39 int main() {
40   hsa_status_t Status = hsa_init();
41   if (Status != HSA_STATUS_SUCCESS) {
42     return 1;
43   }
44 
45   std::vector<std::string> GPUs;
46   Status = hsa_iterate_agents(iterateAgentsCallback, &GPUs);
47   if (Status != HSA_STATUS_SUCCESS) {
48     return 1;
49   }
50 
51   for (const auto &GPU : GPUs)
52     printf("%s\n", GPU.c_str());
53 
54   if (GPUs.size() < 1)
55     return 1;
56 
57   hsa_shut_down();
58   return 0;
59 }
60