1 /*******************************************************************************
2 * Copyright 2017-2018 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *******************************************************************************/
17 #ifndef MKLDNN_THREAD_HPP
18 #define MKLDNN_THREAD_HPP
21 #include "z_magic.hpp"
23 #define MKLDNN_THR_SEQ 0
24 #define MKLDNN_THR_OMP 1
25 #define MKLDNN_THR_TBB 2
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)
32 # define MKLDNN_THR MKLDNN_THR_OMP
34 # define MKLDNN_THR MKLDNN_THR_SEQ
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() {}
46 #define PRAGMA_OMP(...)
48 #elif MKLDNN_THR == MKLDNN_THR_OMP
50 #define MKLDNN_THR_SYNC 1
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() {
60 #define PRAGMA_OMP(...) PRAGMA_MACRO(CHAIN2(omp, __VA_ARGS__))
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
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"); }
75 #define PRAGMA_OMP(...)
79 /* MSVC still supports omp 2.0 only */
80 #if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
82 # define PRAGMA_OMP_SIMD(...)
84 # define PRAGMA_OMP_SIMD(...) PRAGMA_MACRO(CHAIN2(omp, simd __VA_ARGS__))
85 #endif // defined(_MSC_VER) && !defined(__INTEL_COMPILER)
90 inline bool mkldnn_thr_syncable() { return MKLDNN_THR_SYNC == 1; }
92 template <typename T, typename U>
93 inline void balance211(T n, U team, U tid, T &n_start, T &n_end) {
96 if (team <= 1 || n == 0) {
99 } else if (n_min == 1) {
101 // n = T1*n1 + T2*n2 (n1 - n2 = 1)
102 T n1 = utils::div_up(n, (T)team);
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;
113 } // namespace mkldnn
115 #include "mkldnn_thread_parallel_nd.hpp"
119 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s