blocked_rangeNd.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (c) 2017-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_blocked_rangeNd_H
  14. #define __TBB_blocked_rangeNd_H
  15. #if ! TBB_PREVIEW_BLOCKED_RANGE_ND
  16. #error Set TBB_PREVIEW_BLOCKED_RANGE_ND to include blocked_rangeNd.h
  17. #endif
  18. #include "tbb_config.h"
  19. // tbb::blocked_rangeNd requires C++11 support
  20. #if __TBB_CPP11_PRESENT && __TBB_CPP11_ARRAY_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
  21. #include "internal/_template_helpers.h" // index_sequence, make_index_sequence
  22. #include <array>
  23. #include <algorithm> // std::any_of
  24. #include <type_traits> // std::is_same, std::enable_if
  25. #include "tbb/blocked_range.h"
  26. namespace tbb {
  27. namespace internal {
  28. /*
  29. The blocked_rangeNd_impl uses make_index_sequence<N> to automatically generate a ctor with
  30. exactly N arguments of the type tbb::blocked_range<Value>. Such ctor provides an opportunity
  31. to use braced-init-list parameters to initialize each dimension.
  32. Use of parameters, whose representation is a braced-init-list, but they're not
  33. std::initializer_list or a reference to one, produces a non-deduced context
  34. within template argument deduction.
  35. NOTE: blocked_rangeNd must be exactly a templated alias to the blocked_rangeNd_impl
  36. (and not e.g. a derived class), otherwise it would need to declare its own ctor
  37. facing the same problem that the impl class solves.
  38. */
  39. template<typename Value, unsigned int N, typename = make_index_sequence<N>>
  40. class blocked_rangeNd_impl;
  41. template<typename Value, unsigned int N, std::size_t... Is>
  42. class blocked_rangeNd_impl<Value, N, index_sequence<Is...>> {
  43. public:
  44. //! Type of a value.
  45. using value_type = Value;
  46. private:
  47. //! Helper type to construct range with N tbb::blocked_range<value_type> objects.
  48. template<std::size_t>
  49. using dim_type_helper = tbb::blocked_range<value_type>;
  50. public:
  51. blocked_rangeNd_impl() = delete;
  52. //! Constructs N-dimensional range over N half-open intervals each represented as tbb::blocked_range<Value>.
  53. blocked_rangeNd_impl(const dim_type_helper<Is>&... args) : my_dims{ {args...} } {}
  54. //! Dimensionality of a range.
  55. static constexpr unsigned int ndims() { return N; }
  56. //! Range in certain dimension.
  57. const tbb::blocked_range<value_type>& dim(unsigned int dimension) const {
  58. __TBB_ASSERT(dimension < N, "out of bound");
  59. return my_dims[dimension];
  60. }
  61. //------------------------------------------------------------------------
  62. // Methods that implement Range concept
  63. //------------------------------------------------------------------------
  64. //! True if at least one dimension is empty.
  65. bool empty() const {
  66. return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) {
  67. return d.empty();
  68. });
  69. }
  70. //! True if at least one dimension is divisible.
  71. bool is_divisible() const {
  72. return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) {
  73. return d.is_divisible();
  74. });
  75. }
  76. #if __TBB_USE_PROPORTIONAL_SPLIT_IN_BLOCKED_RANGES
  77. //! Static field to support proportional split.
  78. static const bool is_splittable_in_proportion = true;
  79. blocked_rangeNd_impl(blocked_rangeNd_impl& r, proportional_split proportion) : my_dims(r.my_dims) {
  80. do_split(r, proportion);
  81. }
  82. #endif
  83. blocked_rangeNd_impl(blocked_rangeNd_impl& r, split proportion) : my_dims(r.my_dims) {
  84. do_split(r, proportion);
  85. }
  86. private:
  87. __TBB_STATIC_ASSERT(N != 0, "zero dimensional blocked_rangeNd can't be constructed");
  88. //! Ranges in each dimension.
  89. std::array<tbb::blocked_range<value_type>, N> my_dims;
  90. template<typename split_type>
  91. void do_split(blocked_rangeNd_impl& r, split_type proportion) {
  92. __TBB_STATIC_ASSERT((is_same_type<split_type, split>::value
  93. || is_same_type<split_type, proportional_split>::value),
  94. "type of split object is incorrect");
  95. __TBB_ASSERT(r.is_divisible(), "can't split not divisible range");
  96. 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) {
  97. return (first.size() * second.grainsize() < second.size() * first.grainsize());
  98. });
  99. auto r_it = r.my_dims.begin() + (my_it - my_dims.begin());
  100. my_it->my_begin = tbb::blocked_range<value_type>::do_split(*r_it, proportion);
  101. // (!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin)) equals to
  102. // (my_it->my_begin == r_it->my_end), but we can't use operator== due to Value concept
  103. __TBB_ASSERT(!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin),
  104. "blocked_range has been split incorrectly");
  105. }
  106. };
  107. } // namespace internal
  108. template<typename Value, unsigned int N>
  109. using blocked_rangeNd = internal::blocked_rangeNd_impl<Value, N>;
  110. } // namespace tbb
  111. #endif /* __TBB_CPP11_PRESENT && __TBB_CPP11_ARRAY_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */
  112. #endif /* __TBB_blocked_rangeNd_H */