HIP: Heterogenous-computing Interface for Portability
hsa_helpers.hpp
1 /*
2 Copyright (c) 2015 - 2021 Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #pragma once
23 
24 #include <hsa/hsa.h>
25 
26 #include <cstdint>
27 #include <functional>
28 #include <string>
29 
30 namespace hip_impl {
31 inline void* address(hsa_executable_symbol_t x) {
32  void* r = nullptr;
33  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ADDRESS, &r);
34 
35  return r;
36 }
37 
38 inline hsa_agent_t agent(hsa_executable_symbol_t x) {
39  hsa_agent_t r = {};
40  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_AGENT, &r);
41 
42  return r;
43 }
44 
45 inline std::uint32_t group_size(hsa_executable_symbol_t x) {
46  std::uint32_t r = 0u;
47  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE, &r);
48 
49  return r;
50 }
51 
52 inline hsa_isa_t isa(hsa_agent_t x) {
53  hsa_isa_t r = {};
54  hsa_agent_iterate_isas(x,
55  [](hsa_isa_t i, void* o) {
56  *static_cast<hsa_isa_t*>(o) = i; // Pick the first.
57 
58  return HSA_STATUS_INFO_BREAK;
59  },
60  &r);
61 
62  return r;
63 }
64 
65 inline std::uint64_t kernel_object(hsa_executable_symbol_t x) {
66  std::uint64_t r = 0u;
67  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT, &r);
68 
69  return r;
70 }
71 
72 inline std::string name(hsa_executable_symbol_t x) {
73  std::uint32_t sz = 0u;
74  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_NAME_LENGTH, &sz);
75 
76  std::string r(sz, '\0');
77  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_NAME, &r.front());
78 
79  return r;
80 }
81 
82 inline std::uint32_t private_size(hsa_executable_symbol_t x) {
83  std::uint32_t r = 0u;
84  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE, &r);
85 
86  return r;
87 }
88 
89 inline std::uint32_t size(hsa_executable_symbol_t x) {
90  std::uint32_t r = 0;
91  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_SIZE, &r);
92 
93  return r;
94 }
95 
96 inline hsa_symbol_kind_t type(hsa_executable_symbol_t x) {
97  hsa_symbol_kind_t r = {};
98  hsa_executable_symbol_get_info(x, HSA_EXECUTABLE_SYMBOL_INFO_TYPE, &r);
99 
100  return r;
101 }
102 } // namespace hip_impl