forked from oneapi-src/unified-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathur_lib.cpp
More file actions
209 lines (181 loc) · 5.92 KB
/
ur_lib.cpp
File metadata and controls
209 lines (181 loc) · 5.92 KB
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
/*
*
* Copyright (C) 2022-2023 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file ur_lib.cpp
*
*/
#include "ur_lib.hpp"
#include "logger/ur_logger.hpp"
#include "ur_loader.hpp"
#include <cstring>
namespace ur_lib {
///////////////////////////////////////////////////////////////////////////////
context_t *context;
///////////////////////////////////////////////////////////////////////////////
context_t::context_t() {
for (auto l : layers) {
if (l->isAvailable()) {
for (auto &layerName : l->getNames()) {
availableLayers += layerName + ";";
}
}
}
// Remove the trailing ";"
availableLayers.pop_back();
parseEnvEnabledLayers();
}
///////////////////////////////////////////////////////////////////////////////
context_t::~context_t() {}
bool context_t::layerExists(const std::string &layerName) const {
return availableLayers.find(layerName) != std::string::npos;
}
void context_t::parseEnvEnabledLayers() {
auto maybeEnableEnvVarMap = getenv_to_map("UR_ENABLE_LAYERS", false);
if (!maybeEnableEnvVarMap.has_value()) {
return;
}
auto enableEnvVarMap = maybeEnableEnvVarMap.value();
for (auto &key : enableEnvVarMap) {
enabledLayerNames.insert(key.first);
}
}
void context_t::initLayers() const {
for (auto &l : layers) {
if (l->isAvailable()) {
l->init(&context->urDdiTable, enabledLayerNames, codelocData);
}
}
}
void context_t::tearDownLayers() const {
for (auto &l : layers) {
if (l->isAvailable()) {
l->tearDown();
}
}
}
//////////////////////////////////////////////////////////////////////////
__urdlllocal ur_result_t context_t::Init(
ur_device_init_flags_t, ur_loader_config_handle_t hLoaderConfig) {
ur_result_t result;
const char *logger_name = "loader";
logger::init(logger_name);
logger::debug("Logger {} initialized successfully!", logger_name);
result = ur_loader::context->init();
if (UR_RESULT_SUCCESS == result) {
result = urLoaderInit();
}
if (hLoaderConfig) {
codelocData = hLoaderConfig->codelocData;
enabledLayerNames.merge(hLoaderConfig->getEnabledLayerNames());
}
if (!enabledLayerNames.empty()) {
initLayers();
}
return result;
}
ur_result_t urLoaderConfigCreate(ur_loader_config_handle_t *phLoaderConfig) {
if (!phLoaderConfig) {
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}
*phLoaderConfig = new ur_loader_config_handle_t_;
return UR_RESULT_SUCCESS;
}
ur_result_t urLoaderConfigRetain(ur_loader_config_handle_t hLoaderConfig) {
if (!hLoaderConfig) {
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
hLoaderConfig->incrementReferenceCount();
return UR_RESULT_SUCCESS;
}
ur_result_t urLoaderConfigRelease(ur_loader_config_handle_t hLoaderConfig) {
if (!hLoaderConfig) {
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
if (hLoaderConfig->decrementReferenceCount() == 0) {
delete hLoaderConfig;
}
return UR_RESULT_SUCCESS;
}
ur_result_t urLoaderConfigGetInfo(ur_loader_config_handle_t hLoaderConfig,
ur_loader_config_info_t propName,
size_t propSize, void *pPropValue,
size_t *pPropSizeRet) {
if (!hLoaderConfig) {
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
if (!pPropValue && !pPropSizeRet) {
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}
switch (propName) {
case UR_LOADER_CONFIG_INFO_AVAILABLE_LAYERS: {
if (pPropSizeRet) {
*pPropSizeRet = context->availableLayers.size() + 1;
}
if (pPropValue) {
char *outString = static_cast<char *>(pPropValue);
if (propSize != context->availableLayers.size() + 1) {
return UR_RESULT_ERROR_INVALID_SIZE;
}
std::memcpy(outString, context->availableLayers.data(),
propSize - 1);
outString[propSize - 1] = '\0';
}
break;
}
case UR_LOADER_CONFIG_INFO_REFERENCE_COUNT: {
auto refCount = hLoaderConfig->getReferenceCount();
auto truePropSize = sizeof(refCount);
if (pPropSizeRet) {
*pPropSizeRet = truePropSize;
}
if (pPropValue) {
if (propSize != truePropSize) {
return UR_RESULT_ERROR_INVALID_SIZE;
}
std::memcpy(pPropValue, &refCount, truePropSize);
}
break;
}
default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
return UR_RESULT_SUCCESS;
}
ur_result_t urLoaderConfigEnableLayer(ur_loader_config_handle_t hLoaderConfig,
const char *pLayerName) {
if (!hLoaderConfig) {
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
if (!pLayerName) {
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}
if (!context->layerExists(std::string(pLayerName))) {
return UR_RESULT_ERROR_LAYER_NOT_PRESENT;
}
hLoaderConfig->enabledLayers.insert(pLayerName);
return UR_RESULT_SUCCESS;
}
ur_result_t urLoaderTearDown() {
context->tearDownLayers();
return UR_RESULT_SUCCESS;
}
ur_result_t
urLoaderConfigSetCodeLocationCallback(ur_loader_config_handle_t hLoaderConfig,
ur_code_location_callback_t pfnCodeloc,
void *pUserData) {
if (!hLoaderConfig) {
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
if (!pfnCodeloc) {
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
}
hLoaderConfig->codelocData.codelocCb = pfnCodeloc;
hLoaderConfig->codelocData.codelocUserdata = pUserData;
return UR_RESULT_SUCCESS;
}
} // namespace ur_lib