1/*******************************************************************************
2* Copyright 2020-2021 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#include "ittnotify.hpp"
18#include "utils.hpp"
19
20#if defined(DNNL_ENABLE_ITT_TASKS)
21#include "common/ittnotify/ittnotify.h"
22#include "dnnl_debug.h"
23#endif
24
25namespace dnnl {
26namespace impl {
27namespace itt {
28
29static setting_t<int> itt_task_level {__itt_task_level_high};
30
31bool get_itt(__itt_task_level level) {
32 if (!itt_task_level.initialized()) {
33 // Assumes that all threads see the same environment
34 static int val
35 = getenv_int_user("ITT_TASK_LEVEL", itt_task_level.get());
36 itt_task_level.set(val);
37 }
38 return level <= itt_task_level.get();
39}
40
41#if defined(DNNL_ENABLE_ITT_TASKS)
42
43namespace {
44
45thread_local primitive_kind_t thread_primitive_kind;
46
47__itt_domain *itt_domain() {
48 static __itt_domain *d = __itt_domain_create("dnnl::primitive::execute");
49 return d;
50}
51
52} // namespace
53
54void primitive_task_start(primitive_kind_t kind) {
55 if (kind == primitive_kind::undefined) return;
56
57#define CASE(x) \
58 __itt_string_handle_create(dnnl_prim_kind2str(primitive_kind::x))
59 static __itt_string_handle *prim_kind_itt_strings[] = {
60 CASE(undefined),
61 CASE(reorder),
62 CASE(shuffle),
63 CASE(concat),
64 CASE(sum),
65 CASE(convolution),
66 CASE(deconvolution),
67 CASE(eltwise),
68 CASE(lrn),
69 CASE(batch_normalization),
70 CASE(inner_product),
71 CASE(rnn),
72 CASE(gemm),
73 CASE(binary),
74 CASE(matmul),
75 CASE(resampling),
76 CASE(pooling),
77 CASE(reduction),
78 CASE(prelu),
79 CASE(softmax),
80 CASE(layer_normalization),
81 };
82#undef CASE
83 int kind_idx = (int)kind;
84 assert(kind_idx >= 0);
85 assert((size_t)kind_idx
86 < sizeof(prim_kind_itt_strings) / sizeof(prim_kind_itt_strings[0]));
87 __itt_task_begin(itt_domain(), __itt_null, __itt_null,
88 prim_kind_itt_strings[kind_idx]);
89 thread_primitive_kind = kind;
90}
91
92primitive_kind_t primitive_task_get_current_kind() {
93 return thread_primitive_kind;
94}
95
96void primitive_task_end() {
97 if (thread_primitive_kind != primitive_kind::undefined) {
98 __itt_task_end(itt_domain());
99 thread_primitive_kind = primitive_kind::undefined;
100 }
101}
102#else
103void primitive_task_start(primitive_kind_t kind) {
104 UNUSED(kind);
105}
106primitive_kind_t primitive_task_get_current_kind() {
107 return primitive_kind::undefined;
108}
109void primitive_task_end() {}
110#endif
111
112} // namespace itt
113} // namespace impl
114} // namespace dnnl
115