-
Notifications
You must be signed in to change notification settings - Fork 5
/
HSAModule.cpp
executable file
·227 lines (191 loc) · 8.12 KB
/
HSAModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//==============================================================================
// Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools
/// \file
/// \brief This class manages the dynamic loading of HSA entry points hsa-runtime{32,64}.dll and libhsa-runtime{32,64}.so
//==============================================================================
#include <string>
#include <cstring>
#include "HSAModule.h"
#if defined(_WIN64) || defined(__LP64__)
#if defined(_WIN32) || defined(__CYGWIN__)
const char* HSAModule::s_defaultModuleName = "hsa-runtime64.dll";
#else // LINUX
const char* HSAModule::s_defaultModuleName = "libhsa-runtime64.so.1";
#endif
#else
#pragma message("HSA Foundation runtime does not support 32-bit builds")
#if defined(_WIN32) || defined(__CYGWIN__)
const char* HSAModule::s_defaultModuleName = "hsa-runtime.dll";
#else // LINUX
const char* HSAModule::s_defaultModuleName = "libhsa-runtime.so.1";
#endif
#endif
#define ROCM_1_2_AMD_VEN_LOADER_EXTENSION 3
HSAModule::HSAModule(void) : m_isModuleLoaded(false)
{
Initialize();
LoadModule();
}
HSAModule::~HSAModule(void)
{
UnloadModule();
}
void HSAModule::Initialize()
{
#define X(SYM) SYM = nullptr;
HSA_RUNTIME_API_TABLE;
HSA_EXT_FINALIZE_API_TABLE;
HSA_EXT_IMAGE_API_TABLE;
HSA_EXT_AMD_API_TABLE;
HSA_VEN_AMD_LOADER_API_TABLE;
HSA_VEN_AMD_AQL_PROFILE_API_TABLE;
HSA_NON_INTERCEPTABLE_RUNTIME_API_TABLE;
#undef X
m_isModuleLoaded = false;
m_finalizeExtTableLoaded = false;
m_imageExtTableLoaded = false;
m_amdExtTableLoaded = false;
m_amdVenLoaderTableLoaded = false;
m_nonInterceptableTableLoaded = false;
}
void HSAModule::UnloadModule()
{
m_dynamicLibraryHelper.UnloadModule();
Initialize();
}
bool HSAModule::LoadModule(const std::string& moduleName)
{
// Load from specified module
bool bLoaded = m_dynamicLibraryHelper.LoadModule(moduleName);
if (!bLoaded)
{
// Load from deafult module
bLoaded = m_dynamicLibraryHelper.LoadModule(s_defaultModuleName);
}
if (bLoaded)
{
#define MAKE_STRING(s) "hsa_"#s
#define X(SYM) SYM = reinterpret_cast<decltype(::hsa_##SYM)*>(m_dynamicLibraryHelper.GetProcAddress(MAKE_STRING(SYM)));
HSA_RUNTIME_API_TABLE;
HSA_EXT_AMD_API_TABLE;
HSA_NON_INTERCEPTABLE_RUNTIME_API_TABLE;
#undef X
#undef MAKE_STRING
// Check if we were able to initialize the various sets of function pointers
// We will consider the module to have successfully loaded if:
// 1) all core runtime functions are non-nullptr or
// 2) all functions shared by ROCM 1.2 and ROCM 1.2 are non-null
// All extension functions are considered optional for the m_isModuleLoaded flag
#define X(SYM) && SYM != nullptr
bool isCommonTableLoaded = true COMMON_1_2_AND_1_3;
bool isRuntimeTableLoaded = true HSA_RUNTIME_API_TABLE;
m_amdExtTableLoaded = true HSA_EXT_AMD_API_TABLE;
m_nonInterceptableTableLoaded = true HSA_NON_INTERCEPTABLE_RUNTIME_API_TABLE;
#undef X
m_isModuleLoaded = isRuntimeTableLoaded;
#define X(SYM) || SYM != nullptr
bool isAnyCommonFunctionLoaded = false COMMON_1_2_AND_1_3;
#undef X
if (!m_isModuleLoaded && isAnyCommonFunctionLoaded)
{
m_isModuleLoaded = isCommonTableLoaded;
}
// initialize the extension functions
if (m_isModuleLoaded)
{
bool extensionSupported = false;
bool mustCallShutdown = false;
hsa_status_t status = system_extension_supported(HSA_EXTENSION_FINALIZER, 1, 0, &extensionSupported);
if (HSA_STATUS_ERROR_NOT_INITIALIZED == status)
{
// hsa runtime not initialized yet, initialize it now
status = init();
if (HSA_STATUS_SUCCESS == status)
{
mustCallShutdown = true;
uint16_t finalizerMinorVersion = 0;
status = system_major_extension_supported(HSA_EXTENSION_FINALIZER, 1, &finalizerMinorVersion, &extensionSupported);
}
else
{
m_isModuleLoaded = false;
}
}
if (m_isModuleLoaded)
{
if ((HSA_STATUS_SUCCESS == status) && extensionSupported)
{
hsa_ext_finalizer_1_00_pfn_t finalizerTable;
memset(&finalizerTable, 0, sizeof(hsa_ext_finalizer_1_00_pfn_t));
status = system_get_major_extension_table(HSA_EXTENSION_FINALIZER, 1, sizeof(hsa_ext_finalizer_1_00_pfn_t), &finalizerTable);
if (HSA_STATUS_SUCCESS == status)
{
#define X(SYM) SYM = finalizerTable.hsa_##SYM;
HSA_EXT_FINALIZE_API_TABLE;
m_finalizeExtTableLoaded = true;
#undef X
}
}
uint16_t minorVer = 0;
status = system_major_extension_supported(HSA_EXTENSION_IMAGES, 1, &minorVer, &extensionSupported);
if ((HSA_STATUS_SUCCESS == status) && extensionSupported)
{
hsa_ext_images_1_pfn_t imagesTable;
memset(&imagesTable, 0, sizeof(hsa_ext_images_1_pfn_t));
status = system_get_major_extension_table(HSA_EXTENSION_IMAGES, 1, sizeof(hsa_ext_images_1_pfn_t), &imagesTable);
if (HSA_STATUS_SUCCESS == status)
{
#define X(SYM) SYM = imagesTable.hsa_##SYM;
HSA_EXT_IMAGE_API_TABLE;
m_imageExtTableLoaded = true;
#undef X
}
}
uint16_t amdLoaderExtension = HSA_EXTENSION_AMD_LOADER;
uint16_t amdLoaderMinorVersion;
status = system_major_extension_supported(amdLoaderExtension, 1, &amdLoaderMinorVersion, &extensionSupported);
if ((HSA_STATUS_SUCCESS != status) || !extensionSupported)
{
amdLoaderExtension = ROCM_1_2_AMD_VEN_LOADER_EXTENSION;
status = system_major_extension_supported(amdLoaderExtension, 1, &amdLoaderMinorVersion, &extensionSupported);
}
if ((HSA_STATUS_SUCCESS == status) && extensionSupported)
{
hsa_ven_amd_loader_1_01_pfn_t loaderTable;
memset(&loaderTable, 0, sizeof(hsa_ven_amd_loader_1_01_pfn_t));
status = system_get_major_extension_table(amdLoaderExtension, 1, sizeof(hsa_ven_amd_loader_1_01_pfn_t), &loaderTable);
if (HSA_STATUS_SUCCESS == status)
{
#define X(SYM) SYM = loaderTable.hsa_##SYM;
HSA_VEN_AMD_LOADER_API_TABLE;
m_amdVenLoaderTableLoaded = true;
#undef X
}
}
uint16_t amdAqlProfileExtension = HSA_EXTENSION_AMD_AQLPROFILE;
uint16_t amdAqlProfileMinorVersion;
status = system_major_extension_supported(amdAqlProfileExtension, 1, &amdAqlProfileMinorVersion, &extensionSupported);
if ((HSA_STATUS_SUCCESS == status) && extensionSupported)
{
hsa_ven_amd_aqlprofile_pfn_t aqlprofileTable;
memset(&aqlprofileTable, 0, sizeof(hsa_ven_amd_aqlprofile_pfn_t));
status = system_get_major_extension_table(amdAqlProfileExtension, 1, sizeof(hsa_ven_amd_aqlprofile_pfn_t), &aqlprofileTable);
if (HSA_STATUS_SUCCESS == status)
{
#define X(SYM) SYM = aqlprofileTable.hsa_##SYM;
HSA_VEN_AMD_AQL_PROFILE_API_TABLE;
m_amdVenAqlprofileTableLoaded = true;
#undef X
}
}
if (mustCallShutdown)
{
// if we initialzed the runtime, then shut it down now
shut_down();
}
}
}
}
return m_isModuleLoaded;
}