Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / common / mkldnn_thread.hpp
1 /*******************************************************************************
2 * Copyright 2017-2018 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 #ifndef MKLDNN_THREAD_HPP
18 #define MKLDNN_THREAD_HPP
19
20 #include "utils.hpp"
21 #include "z_magic.hpp"
22
23 #define MKLDNN_THR_SEQ 0
24 #define MKLDNN_THR_OMP 1
25 #define MKLDNN_THR_TBB 2
26
27 /* Ideally this condition below should never happen (if the library is built
28  * using regular cmake). For the 3rd-party projects that build the library
29  * from the sources on their own try to guess the right threading... */
30 #if !defined(MKLDNN_THR)
31 #   if defined(_OPENMP)
32 #       define MKLDNN_THR MKLDNN_THR_OMP
33 #   else
34 #       define MKLDNN_THR MKLDNN_THR_SEQ
35 #   endif
36 #endif
37
38 #if MKLDNN_THR == MKLDNN_THR_SEQ
39 #define MKLDNN_THR_SYNC 1
40 inline int mkldnn_get_max_threads() { return 1; }
41 inline int mkldnn_get_num_threads() { return 1; }
42 inline int mkldnn_get_thread_num() { return 0; }
43 inline int mkldnn_in_parallel() { return 0; }
44 inline void mkldnn_thr_barrier() {}
45
46 #define PRAGMA_OMP(...)
47
48 #elif MKLDNN_THR == MKLDNN_THR_OMP
49 #include <omp.h>
50 #define MKLDNN_THR_SYNC 1
51
52 inline int mkldnn_get_max_threads() { return omp_get_max_threads(); }
53 inline int mkldnn_get_num_threads() { return omp_get_num_threads(); }
54 inline int mkldnn_get_thread_num() { return omp_get_thread_num(); }
55 inline int mkldnn_in_parallel() { return omp_in_parallel(); }
56 inline void mkldnn_thr_barrier() {
57 #   pragma omp barrier
58 }
59
60 #define PRAGMA_OMP(...) PRAGMA_MACRO(CHAIN2(omp, __VA_ARGS__))
61
62 #elif MKLDNN_THR == MKLDNN_THR_TBB
63 #include "tbb/task_arena.h"
64 #include "tbb/parallel_for.h"
65 #define MKLDNN_THR_SYNC 0
66
67 inline int mkldnn_get_max_threads()
68 { return tbb::this_task_arena::max_concurrency(); }
69 inline int mkldnn_get_num_threads() { return mkldnn_get_max_threads(); }
70 inline int mkldnn_get_thread_num()
71 { return tbb::this_task_arena::current_thread_index(); }
72 inline int mkldnn_in_parallel() { return 0; }
73 inline void mkldnn_thr_barrier() { assert(!"no barrier in TBB"); }
74
75 #define PRAGMA_OMP(...)
76
77 #endif
78
79 /* MSVC still supports omp 2.0 only */
80 #if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
81 #   define collapse(x)
82 #   define PRAGMA_OMP_SIMD(...)
83 #else
84 #   define PRAGMA_OMP_SIMD(...) PRAGMA_MACRO(CHAIN2(omp, simd __VA_ARGS__))
85 #endif // defined(_MSC_VER) && !defined(__INTEL_COMPILER)
86
87 namespace mkldnn {
88 namespace impl {
89
90 inline bool mkldnn_thr_syncable() { return MKLDNN_THR_SYNC == 1; }
91
92 template <typename T, typename U>
93 inline void balance211(T n, U team, U tid, T &n_start, T &n_end) {
94     T n_min = 1;
95     T &n_my = n_end;
96     if (team <= 1 || n == 0) {
97         n_start = 0;
98         n_my = n;
99     } else if (n_min == 1) {
100         // team = T1 + T2
101         // n = T1*n1 + T2*n2  (n1 - n2 = 1)
102         T n1 = utils::div_up(n, (T)team);
103         T n2 = n1 - 1;
104         T T1 = n - n2 * (T)team;
105         n_my = (T)tid < T1 ? n1 : n2;
106         n_start = (T)tid <= T1 ? tid * n1 : T1 * n1 + ((T)tid - T1) * n2;
107     }
108
109     n_end += n_start;
110 }
111
112 } // namespace impl
113 } // namespace mkldnn
114
115 #include "mkldnn_thread_parallel_nd.hpp"
116
117 #endif
118
119 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s