1 /* 2 Copyright (c) 2017-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 #ifndef __TBB_blocked_rangeNd_H 18 #define __TBB_blocked_rangeNd_H 19 20 #if !TBB_PREVIEW_BLOCKED_RANGE_ND 21 #error Set TBB_PREVIEW_BLOCKED_RANGE_ND to include blocked_rangeNd.h 22 #endif 23 24 #include <algorithm> // std::any_of 25 #include <array> 26 #include <cstddef> 27 #include <type_traits> // std::is_same, std::enable_if 28 29 #include "detail/_config.h" 30 #include "detail/_template_helpers.h" // index_sequence, make_index_sequence 31 #include "detail/_range_common.h" 32 33 #include "blocked_range.h" 34 35 namespace tbb { 36 namespace detail { 37 namespace d1 { 38 39 /* 40 The blocked_rangeNd_impl uses make_index_sequence<N> to automatically generate a ctor with 41 exactly N arguments of the type tbb::blocked_range<Value>. Such ctor provides an opportunity 42 to use braced-init-list parameters to initialize each dimension. 43 Use of parameters, whose representation is a braced-init-list, but they're not 44 std::initializer_list or a reference to one, produces a non-deduced context 45 within template argument deduction. 46 47 NOTE: blocked_rangeNd must be exactly a templated alias to the blocked_rangeNd_impl 48 (and not e.g. a derived class), otherwise it would need to declare its own ctor 49 facing the same problem that the impl class solves. 50 */ 51 52 template<typename Value, unsigned int N, typename = detail::make_index_sequence<N>> 53 __TBB_requires(blocked_range_value<Value>) 54 class blocked_rangeNd_impl; 55 56 template<typename Value, unsigned int N, std::size_t... Is> __TBB_requires(blocked_range_value<Value>)57 __TBB_requires(blocked_range_value<Value>) 58 class blocked_rangeNd_impl<Value, N, detail::index_sequence<Is...>> { 59 public: 60 //! Type of a value. 61 using value_type = Value; 62 63 private: 64 //! Helper type to construct range with N tbb::blocked_range<value_type> objects. 65 template<std::size_t> 66 using dim_type_helper = tbb::blocked_range<value_type>; 67 68 public: 69 blocked_rangeNd_impl() = delete; 70 71 //! Constructs N-dimensional range over N half-open intervals each represented as tbb::blocked_range<Value>. 72 blocked_rangeNd_impl(const dim_type_helper<Is>&... args) : my_dims{ {args...} } {} 73 74 //! Dimensionality of a range. 75 static constexpr unsigned int ndims() { return N; } 76 77 //! Range in certain dimension. 78 const tbb::blocked_range<value_type>& dim(unsigned int dimension) const { 79 __TBB_ASSERT(dimension < N, "out of bound"); 80 return my_dims[dimension]; 81 } 82 83 //------------------------------------------------------------------------ 84 // Methods that implement Range concept 85 //------------------------------------------------------------------------ 86 87 //! True if at least one dimension is empty. 88 bool empty() const { 89 return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) { 90 return d.empty(); 91 }); 92 } 93 94 //! True if at least one dimension is divisible. 95 bool is_divisible() const { 96 return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) { 97 return d.is_divisible(); 98 }); 99 } 100 101 blocked_rangeNd_impl(blocked_rangeNd_impl& r, proportional_split proportion) : my_dims(r.my_dims) { 102 do_split(r, proportion); 103 } 104 105 blocked_rangeNd_impl(blocked_rangeNd_impl& r, split proportion) : my_dims(r.my_dims) { 106 do_split(r, proportion); 107 } 108 109 private: 110 static_assert(N != 0, "zero dimensional blocked_rangeNd can't be constructed"); 111 112 //! Ranges in each dimension. 113 std::array<tbb::blocked_range<value_type>, N> my_dims; 114 115 template<typename split_type> 116 void do_split(blocked_rangeNd_impl& r, split_type proportion) { 117 static_assert((std::is_same<split_type, split>::value || std::is_same<split_type, proportional_split>::value), "type of split object is incorrect"); 118 __TBB_ASSERT(r.is_divisible(), "can't split not divisible range"); 119 120 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) { 121 return (first.size() * second.grainsize() < second.size() * first.grainsize()); 122 }); 123 124 auto r_it = r.my_dims.begin() + (my_it - my_dims.begin()); 125 126 my_it->my_begin = tbb::blocked_range<value_type>::do_split(*r_it, proportion); 127 128 // (!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin)) equals to 129 // (my_it->my_begin == r_it->my_end), but we can't use operator== due to Value concept 130 __TBB_ASSERT(!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin), 131 "blocked_range has been split incorrectly"); 132 } 133 }; 134 135 template<typename Value, unsigned int N> 136 using blocked_rangeNd = blocked_rangeNd_impl<Value, N>; 137 138 } // namespace d1 139 } // namespace detail 140 141 inline namespace v1 { 142 using detail::d1::blocked_rangeNd; 143 } // namespace v1 144 } // namespace tbb 145 146 #endif /* __TBB_blocked_rangeNd_H */ 147 148