Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
blocked_rangeNd.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2017-2019 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 
18 
19 */
20 
21 #ifndef __TBB_blocked_rangeNd_H
22 #define __TBB_blocked_rangeNd_H
23 
24 #if ! TBB_PREVIEW_BLOCKED_RANGE_ND
25  #error Set TBB_PREVIEW_BLOCKED_RANGE_ND to include blocked_rangeNd.h
26 #endif
27 
28 #include "tbb_config.h"
29 
30 // tbb::blocked_rangeNd requires C++11 support
31 #if __TBB_CPP11_PRESENT && __TBB_CPP11_ARRAY_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
32 
33 #include "internal/_template_helpers.h" // index_sequence, make_index_sequence
34 
35 #include <array>
36 #include <algorithm> // std::any_of
37 #include <type_traits> // std::is_same, std::enable_if
38 
39 #include "tbb/blocked_range.h"
40 
41 namespace tbb {
42 namespace internal {
43 
44 /*
45  The blocked_rangeNd_impl uses make_index_sequence<N> to automatically generate a ctor with
46  exactly N arguments of the type tbb::blocked_range<Value>. Such ctor provides an opportunity
47  to use braced-init-list parameters to initialize each dimension.
48  Use of parameters, whose representation is a braced-init-list, but they're not
49  std::initializer_list or a reference to one, produces a non-deduced context
50  within template argument deduction.
51 
52  NOTE: blocked_rangeNd must be exactly a templated alias to the blocked_rangeNd_impl
53  (and not e.g. a derived class), otherwise it would need to declare its own ctor
54  facing the same problem that the impl class solves.
55 */
56 
57 template<typename Value, unsigned int N, typename = make_index_sequence<N>>
58 class blocked_rangeNd_impl;
59 
60 template<typename Value, unsigned int N, std::size_t... Is>
61 class blocked_rangeNd_impl<Value, N, index_sequence<Is...>> {
62 public:
64  using value_type = Value;
65 
66 private:
67 
69  template<std::size_t>
70  using dim_type_helper = tbb::blocked_range<value_type>;
71 
72 public:
73  blocked_rangeNd_impl() = delete;
74 
76  blocked_rangeNd_impl(const dim_type_helper<Is>&... args) : my_dims{ {args...} } {}
77 
79  static constexpr unsigned int ndims() { return N; }
80 
82  const tbb::blocked_range<value_type>& dim(unsigned int dimension) const {
83  __TBB_ASSERT(dimension < N, "out of bound");
84  return my_dims[dimension];
85  }
86 
87  //------------------------------------------------------------------------
88  // Methods that implement Range concept
89  //------------------------------------------------------------------------
90 
92  bool empty() const {
93  return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) {
94  return d.empty();
95  });
96  }
97 
99  bool is_divisible() const {
100  return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) {
101  return d.is_divisible();
102  });
103  }
104 
105 #if __TBB_USE_PROPORTIONAL_SPLIT_IN_BLOCKED_RANGES
106  static const bool is_splittable_in_proportion = true;
108 
109  blocked_rangeNd_impl(blocked_rangeNd_impl& r, proportional_split proportion) : my_dims(r.my_dims) {
110  do_split(r, proportion);
111  }
112 #endif
113 
114  blocked_rangeNd_impl(blocked_rangeNd_impl& r, split proportion) : my_dims(r.my_dims) {
115  do_split(r, proportion);
116  }
117 
118 private:
119  __TBB_STATIC_ASSERT(N != 0, "zero dimensional blocked_rangeNd can't be constructed");
120 
122  std::array<tbb::blocked_range<value_type>, N> my_dims;
123 
124  template<typename split_type>
125  void do_split(blocked_rangeNd_impl& r, split_type proportion) {
128  "type of split object is incorrect");
129  __TBB_ASSERT(r.is_divisible(), "can't split not divisible range");
130 
131  auto my_it = std::max_element(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& first, const tbb::blocked_range<value_type>& second) {
132  return (first.size() * second.grainsize() < second.size() * first.grainsize());
133  });
134 
135  auto r_it = r.my_dims.begin() + (my_it - my_dims.begin());
136 
137  my_it->my_begin = tbb::blocked_range<value_type>::do_split(*r_it, proportion);
138 
139  // (!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin)) equals to
140  // (my_it->my_begin == r_it->my_end), but we can't use operator== due to Value concept
141  __TBB_ASSERT(!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin),
142  "blocked_range has been split incorrectly");
143  }
144 };
145 
146 } // namespace internal
147 
148 template<typename Value, unsigned int N>
149 using blocked_rangeNd = internal::blocked_rangeNd_impl<Value, N>;
150 
151 } // namespace tbb
152 
153 #endif /* __TBB_CPP11_PRESENT && __TBB_CPP11_ARRAY_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */
154 #endif /* __TBB_blocked_rangeNd_H */
A range over which to iterate.
Definition: blocked_range.h:49
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:536
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
auto first(Container &c) -> decltype(begin(c))
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d
static Value do_split(blocked_range &r, split)
Auxiliary function used by the splitting constructor.
The graph class.

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.