-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtcm_adaptor.cpp
More file actions
327 lines (266 loc) · 11.3 KB
/
tcm_adaptor.cpp
File metadata and controls
327 lines (266 loc) · 11.3 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Copyright (c) 2023-2024 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "oneapi/tbb/detail/_intrusive_list_node.h"
#include "oneapi/tbb/detail/_template_helpers.h"
#include "oneapi/tbb/task_arena.h"
#include "pm_client.h"
#include "dynamic_link.h"
#include "misc.h"
#include "tcm.h"
#include "tcm_adaptor.h"
#include <iostream>
namespace tbb {
namespace detail {
namespace r1 {
namespace {
#if __TBB_WEAK_SYMBOLS_PRESENT
#pragma weak tcmConnect
#pragma weak tcmDisconnect
#pragma weak tcmRequestPermit
#pragma weak tcmGetPermitData
#pragma weak tcmReleasePermit
#pragma weak tcmIdlePermit
#pragma weak tcmDeactivatePermit
#pragma weak tcmActivatePermit
#pragma weak tcmRegisterThread
#pragma weak tcmUnregisterThread
#pragma weak tcmGetVersionInfo
#endif /* __TBB_WEAK_SYMBOLS_PRESENT */
tcm_result_t(*tcm_connect)(tcm_callback_t callback, tcm_client_id_t* client_id){nullptr};
tcm_result_t(*tcm_disconnect)(tcm_client_id_t client_id){ nullptr };
tcm_result_t(*tcm_request_permit)(tcm_client_id_t client_id, tcm_permit_request_t request,
void* callback_arg, tcm_permit_handle_t* permit_handle, tcm_permit_t* permit){nullptr};
tcm_result_t(*tcm_get_permit_data)(tcm_permit_handle_t permit_handle, tcm_permit_t* permit){nullptr};
tcm_result_t(*tcm_release_permit)(tcm_permit_handle_t permit){nullptr};
tcm_result_t(*tcm_idle_permit)(tcm_permit_handle_t permit_handle){nullptr};
tcm_result_t(*tcm_deactivate_permit)(tcm_permit_handle_t permit_handle){nullptr};
tcm_result_t(*tcm_activate_permit)(tcm_permit_handle_t permit_handle){nullptr};
tcm_result_t(*tcm_register_thread)(tcm_permit_handle_t permit_handle){nullptr};
tcm_result_t(*tcm_unregister_thread)(){nullptr};
tcm_result_t (*tcm_get_version_info)(char* buffer, uint32_t buffer_size){nullptr};
static const dynamic_link_descriptor tcm_link_table[] = {
DLD(tcmConnect, tcm_connect),
DLD(tcmDisconnect, tcm_disconnect),
DLD(tcmRequestPermit, tcm_request_permit),
DLD(tcmGetPermitData, tcm_get_permit_data),
DLD(tcmReleasePermit, tcm_release_permit),
DLD(tcmIdlePermit, tcm_idle_permit),
DLD(tcmDeactivatePermit, tcm_deactivate_permit),
DLD(tcmActivatePermit, tcm_activate_permit),
DLD(tcmRegisterThread, tcm_register_thread),
DLD(tcmUnregisterThread, tcm_unregister_thread),
DLD(tcmGetVersionInfo, tcm_get_version_info)
};
#if TBB_USE_DEBUG
#define DEBUG_SUFFIX "_debug"
#else
#define DEBUG_SUFFIX
#endif /* TBB_USE_DEBUG */
#if _WIN32 || _WIN64
#define LIBRARY_EXTENSION ".dll"
#define LIBRARY_PREFIX
#elif __unix__
#define LIBRARY_EXTENSION ".so.1"
#define LIBRARY_PREFIX "lib"
#else
#define LIBRARY_EXTENSION
#define LIBRARY_PREFIX
#endif /* __unix__ */
#define TCMLIB_NAME LIBRARY_PREFIX "tcm" DEBUG_SUFFIX LIBRARY_EXTENSION
static bool tcm_functions_loaded{ false };
}
class tcm_client : public pm_client {
using tcm_client_mutex_type = d1::mutex;
public:
tcm_client(tcm_adaptor& adaptor, arena& a) : pm_client(a), my_tcm_adaptor(adaptor) {}
~tcm_client() {
if (my_permit_handle) {
__TBB_ASSERT(tcm_release_permit, nullptr);
auto res = tcm_release_permit(my_permit_handle);
__TBB_ASSERT_EX(res == TCM_RESULT_SUCCESS, nullptr);
}
}
int update_concurrency(uint32_t concurrency) {
return my_arena.update_concurrency(concurrency);
}
unsigned priority_level() {
return my_arena.priority_level();
}
tcm_permit_request_t& permit_request() {
return my_permit_request;
}
tcm_permit_handle_t& permit_handle() {
return my_permit_handle;
}
void actualize_permit() {
__TBB_ASSERT(tcm_get_permit_data, nullptr);
int delta{};
{
tcm_client_mutex_type::scoped_lock lock(my_permit_mutex);
uint32_t new_concurrency{};
tcm_permit_t new_permit{ &new_concurrency, nullptr, 1, TCM_PERMIT_STATE_VOID, {} };
auto res = tcm_get_permit_data(my_permit_handle, &new_permit);
__TBB_ASSERT_EX(res == TCM_RESULT_SUCCESS, nullptr);
// The permit has changed during the reading, so the callback will be invoked soon one more time and
// we can just skip this renegotiation iteration.
if (!new_permit.flags.stale) {
// If there is no other demand in TCM, the permit may still have granted concurrency but
// be in the deactivated state thus we enforce 0 allotment to preserve arena invariants.
delta = update_concurrency(new_permit.state != TCM_PERMIT_STATE_INACTIVE ? new_concurrency : 0);
}
}
if (delta) {
my_tcm_adaptor.notify_thread_request(delta);
}
}
void request_permit(tcm_client_id_t client_id) {
__TBB_ASSERT(tcm_request_permit, nullptr);
my_permit_request.max_sw_threads = max_workers();
my_permit_request.min_sw_threads = my_permit_request.max_sw_threads == 0 ? 0 : min_workers();
if (my_permit_request.constraints_size > 0) {
my_permit_request.cpu_constraints->min_concurrency = my_permit_request.min_sw_threads;
my_permit_request.cpu_constraints->max_concurrency = my_permit_request.max_sw_threads;
}
__TBB_ASSERT(my_permit_request.max_sw_threads >= my_permit_request.min_sw_threads, nullptr);
tcm_result_t res = tcm_request_permit(client_id, my_permit_request, this, &my_permit_handle, nullptr);
__TBB_ASSERT_EX(res == TCM_RESULT_SUCCESS, nullptr);
}
void deactivate_permit() {
__TBB_ASSERT(tcm_deactivate_permit, nullptr);
tcm_result_t res = tcm_deactivate_permit(my_permit_handle);
__TBB_ASSERT_EX(res == TCM_RESULT_SUCCESS, nullptr);
}
void init(tcm_client_id_t client_id, d1::constraints& constraints) {
__TBB_ASSERT(tcm_request_permit, nullptr);
__TBB_ASSERT(tcm_deactivate_permit, nullptr);
if (constraints.core_type != d1::task_arena::automatic ||
constraints.numa_id != d1::task_arena::automatic ||
constraints.max_threads_per_core != d1::task_arena::automatic)
{
my_permit_constraints.max_concurrency = constraints.max_concurrency;
my_permit_constraints.min_concurrency = 0;
my_permit_constraints.core_type_id = constraints.core_type;
my_permit_constraints.numa_id = constraints.numa_id;
my_permit_constraints.threads_per_core = constraints.max_threads_per_core;
my_permit_request.cpu_constraints = &my_permit_constraints;
my_permit_request.constraints_size = 1;
}
my_permit_request.min_sw_threads = 0;
my_permit_request.max_sw_threads = 0;
my_permit_request.flags.request_as_inactive = 1;
tcm_result_t res = tcm_request_permit(client_id, my_permit_request, this, &my_permit_handle, nullptr);
__TBB_ASSERT_EX(res == TCM_RESULT_SUCCESS, nullptr);
my_permit_request.flags.request_as_inactive = 0;
}
void register_thread() override {
__TBB_ASSERT(tcm_register_thread, nullptr);
auto return_code = tcm_register_thread(my_permit_handle);
__TBB_ASSERT_EX(return_code == TCM_RESULT_SUCCESS, nullptr);
}
void unregister_thread() override {
__TBB_ASSERT(tcm_unregister_thread, nullptr);
auto return_code = tcm_unregister_thread();
__TBB_ASSERT_EX(return_code == TCM_RESULT_SUCCESS, nullptr);
}
private:
tcm_cpu_constraints_t my_permit_constraints = TCM_PERMIT_REQUEST_CONSTRAINTS_INITIALIZER;
tcm_permit_request_t my_permit_request = TCM_PERMIT_REQUEST_INITIALIZER;
tcm_permit_handle_t my_permit_handle{};
tcm_client_mutex_type my_permit_mutex;
tcm_adaptor& my_tcm_adaptor;
};
//------------------------------------------------------------------------
// tcm_adaptor_impl
//------------------------------------------------------------------------
struct tcm_adaptor_impl {
using demand_mutex_type = d1::mutex;
demand_mutex_type my_demand_mutex;
tcm_client_id_t client_id{};
tcm_adaptor_impl(tcm_client_id_t id) : client_id(id)
{}
};
//------------------------------------------------------------------------
// tcm_adaptor
//------------------------------------------------------------------------
tcm_result_t renegotiation_callback(tcm_permit_handle_t, void* client_ptr, tcm_callback_flags_t) {
__TBB_ASSERT(client_ptr, nullptr);
static_cast<tcm_client*>(client_ptr)->actualize_permit();
return TCM_RESULT_SUCCESS;
}
void tcm_adaptor::initialize() {
tcm_functions_loaded = dynamic_link(TCMLIB_NAME, tcm_link_table, /* tcm_link_table size = */ 11);
}
bool tcm_adaptor::is_initialized() {
return tcm_functions_loaded;
}
void tcm_adaptor::print_version() {
if (is_initialized()) {
__TBB_ASSERT(tcm_get_version_info, nullptr);
char buffer[1024];
tcm_get_version_info(buffer, 1024);
std::fprintf(stderr, "%.*s", 1024, buffer);
}
}
tcm_adaptor::tcm_adaptor() {
__TBB_ASSERT(tcm_connect, nullptr);
tcm_client_id_t client_id{};
auto return_code = tcm_connect(renegotiation_callback, &client_id);
if (return_code == TCM_RESULT_SUCCESS) {
my_impl = make_cache_aligned_unique<tcm_adaptor_impl>(client_id);
}
}
tcm_adaptor::~tcm_adaptor() {
if (my_impl) {
__TBB_ASSERT(tcm_disconnect, nullptr);
auto return_code = tcm_disconnect(my_impl->client_id);
__TBB_ASSERT_EX(return_code == TCM_RESULT_SUCCESS, nullptr);
my_impl = nullptr;
}
}
bool tcm_adaptor::is_connected() {
return my_impl != nullptr;
}
pm_client* tcm_adaptor::create_client(arena& a) {
return new (cache_aligned_allocate(sizeof(tcm_client))) tcm_client(*this, a);
}
void tcm_adaptor::register_client(pm_client* c, d1::constraints& constraints) {
static_cast<tcm_client*>(c)->init(my_impl->client_id, constraints);
}
void tcm_adaptor::unregister_and_destroy_client(pm_client& c) {
auto& client = static_cast<tcm_client&>(c);
{
tcm_adaptor_impl::demand_mutex_type::scoped_lock lock(my_impl->my_demand_mutex);
client.~tcm_client();
}
cache_aligned_deallocate(&client);
}
void tcm_adaptor::set_active_num_workers(int) {}
void tcm_adaptor::adjust_demand(pm_client& c, int mandatory_delta, int workers_delta) {
__TBB_ASSERT(-1 <= mandatory_delta && mandatory_delta <= 1, nullptr);
auto& client = static_cast<tcm_client&>(c);
{
tcm_adaptor_impl::demand_mutex_type::scoped_lock lock(my_impl->my_demand_mutex);
// Update client's state
workers_delta = client.update_request(mandatory_delta, workers_delta);
if (workers_delta == 0) return;
if (client.max_workers() == 0) {
client.deactivate_permit();
} else {
client.request_permit(my_impl->client_id);
}
}
client.actualize_permit();
}
} // namespace r1
} // namespace detail
} // namespace tbb