Home | History | Annotate | Line # | Download | only in amdgpu-arch
      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 #if defined(__has_include)
     15 #if __has_include("hsa.h")
     16 #define HSA_HEADER_FOUND 1
     17 #include "hsa.h"
     18 #elif __has_include("hsa/hsa.h")
     19 #define HSA_HEADER_FOUND 1
     20 #include "hsa/hsa.h"
     21 #else
     22 #define HSA_HEADER_FOUND 0
     23 #endif
     24 #else
     25 #define HSA_HEADER_FOUND 0
     26 #endif
     27 
     28 #if !HSA_HEADER_FOUND
     29 int main() { return 1; }
     30 #else
     31 
     32 #include <string>
     33 #include <vector>
     34 
     35 static hsa_status_t iterateAgentsCallback(hsa_agent_t Agent, void *Data) {
     36   hsa_device_type_t DeviceType;
     37   hsa_status_t Status =
     38       hsa_agent_get_info(Agent, HSA_AGENT_INFO_DEVICE, &DeviceType);
     39 
     40   // continue only if device type if GPU
     41   if (Status != HSA_STATUS_SUCCESS || DeviceType != HSA_DEVICE_TYPE_GPU) {
     42     return Status;
     43   }
     44 
     45   std::vector<std::string> *GPUs =
     46       static_cast<std::vector<std::string> *>(Data);
     47   char GPUName[64];
     48   Status = hsa_agent_get_info(Agent, HSA_AGENT_INFO_NAME, GPUName);
     49   if (Status != HSA_STATUS_SUCCESS) {
     50     return Status;
     51   }
     52   GPUs->push_back(GPUName);
     53   return HSA_STATUS_SUCCESS;
     54 }
     55 
     56 int main() {
     57   hsa_status_t Status = hsa_init();
     58   if (Status != HSA_STATUS_SUCCESS) {
     59     return 1;
     60   }
     61 
     62   std::vector<std::string> GPUs;
     63   Status = hsa_iterate_agents(iterateAgentsCallback, &GPUs);
     64   if (Status != HSA_STATUS_SUCCESS) {
     65     return 1;
     66   }
     67 
     68   for (const auto &GPU : GPUs)
     69     printf("%s\n", GPU.c_str());
     70 
     71   if (GPUs.size() < 1)
     72     return 1;
     73 
     74   hsa_shut_down();
     75   return 0;
     76 }
     77 
     78 #endif
     79