@@ -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