Skip to content

Commit ab7938f

Browse files
committed
gtests, benchdnn: explicitly finalize TBB scheduler
1 parent 31c9e7b commit ab7938f

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

tests/benchdnn/dnnl_common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ void get_gpu_profiling_info(uint64_t &nsec, double &freq, int mode) {
358358

359359
void finalize() {
360360
reset_gpu_profiling();
361+
finalize_tbb();
361362
}
362363

363364
inline bool should_stop(const timer::timer_t &t) {

tests/gtests/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright 2016-2020 Intel Corporation
2+
* Copyright 2016-2022 Intel Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,6 +91,7 @@ int main(int argc, char *argv[]) {
9191
system("where /q umdh && del post_cpu.txt");
9292
#endif
9393

94+
finalize_tbb();
9495
return result;
9596
}
9697

tests/test_thread.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ struct thr_ctx_t {
8080
#error "src/common/dnnl_thread.hpp" has an unexpected header guard
8181
#endif
8282

83+
#ifdef TBB_INTERFACE_VERSION
8384
// tbb constraints on core type appear in 2021.2
8485
// tbb constraints on max_concurrency appear in 2020
8586
// we check only for 2021.2 to enable thread context knobs
86-
#ifdef TBB_INTERFACE_VERSION
8787
#define DNNL_TBB_CONSTRAINTS_ENABLED (TBB_INTERFACE_VERSION >= 12020)
88+
// API to do explicit finalization was introduced in 2021.6.
89+
#define DNNL_TBB_NEED_EXPLICIT_FINALIZE (TBB_INTERFACE_VERSION >= 12060)
8890
#else
8991
#define DNNL_TBB_CONSTRAINTS_ENABLED 0
92+
#define DNNL_TBB_NEED_EXPLICIT_FINALIZE 0
9093
#endif
9194

9295
#define DNNL_TBB_THREADING_WITH_CONSTRAINTS \
@@ -318,6 +321,21 @@ auto execute_in_thr_ctx(const thr_ctx_t &ctx, F &&f, Args_t &...args)
318321
#error __FILE__"(" __LINE__ ")" "unsupported threading runtime!"
319322
#endif
320323

324+
// TBB runtime may crash when it is used under CTest. This is a known TBB
325+
// limitation that can be worked around by doing explicit finalization.
326+
// The API to do that was introduced in 2021.6.0. When using an older TBB
327+
// runtime the crash may still happen.
328+
#if DNNL_TBB_NEED_EXPLICIT_FINALIZE
329+
#include "tbb/global_control.h"
330+
inline void finalize_tbb() {
331+
oneapi::tbb::task_scheduler_handle handle
332+
= oneapi::tbb::task_scheduler_handle {oneapi::tbb::attach {}};
333+
oneapi::tbb::finalize(handle);
334+
}
335+
#else
336+
inline void finalize_tbb() {};
337+
#endif
338+
321339
#undef ALIAS_TO_RUN_IN_THR_CTX
322340
#undef THR_CTX_ASSERT
323341
#undef DNNL_TBB_THREADING_WITHOUT_CONSTRAINTS

0 commit comments

Comments
 (0)