1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
Copyright (c) 2017-2024 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "common/test.h"
#include "common/utils.h"
#include "common/utils_assert.h"
#include "common/utils_concurrency_limit.h"
//! \file conformance_blocked_rangeNd.cpp
//! \brief Test for [preview] functionality
#define TBB_PREVIEW_BLOCKED_RANGE_ND 1
#include "oneapi/tbb/blocked_rangeNd.h"
#include "oneapi/tbb/parallel_for.h"
#include "oneapi/tbb/global_control.h"
#include <algorithm> // std::for_each
#include <array>
// AbstractValueType class represents Value concept's requirements in the most abstract way
class AbstractValueType {
int value;
AbstractValueType() {}
public:
friend AbstractValueType MakeAbstractValue(int i);
friend int GetValueOf(const AbstractValueType& v);
};
int GetValueOf(const AbstractValueType& v) { return v.value; }
AbstractValueType MakeAbstractValue(int i) {
AbstractValueType x;
x.value = i;
return x;
}
// operator- returns amount of elements of AbstractValueType between u and v
std::size_t operator-(const AbstractValueType& u, const AbstractValueType& v) {
return GetValueOf(u) - GetValueOf(v);
}
bool operator<(const AbstractValueType& u, const AbstractValueType& v) {
return GetValueOf(u) < GetValueOf(v);
}
AbstractValueType operator+(const AbstractValueType& u, std::size_t offset) {
return MakeAbstractValue(GetValueOf(u) + int(offset));
}
template<typename range_t, unsigned int N>
struct range_utils {
using val_t = typename range_t::value_type;
template<typename EntityType, std::size_t DimSize>
using data_type = std::array<typename range_utils<range_t, N - 1>::template data_type<EntityType, DimSize>, DimSize>;
template<typename EntityType, std::size_t DimSize>
static void init_data(data_type<EntityType, DimSize>& data) {
std::for_each(data.begin(), data.end(), range_utils<range_t, N - 1>::template init_data<EntityType, DimSize>);
}
template<typename EntityType, std::size_t DimSize>
static void increment_data(const range_t& range, data_type<EntityType, DimSize>& data) {
auto begin = data.begin() + range.dim(N - 1).begin();
// same as "auto end = out.begin() + range.dim(N - 1).end();"
auto end = begin + range.dim(N - 1).size();
for (auto i = begin; i != end; ++i) {
range_utils<range_t, N - 1>::template increment_data<EntityType, DimSize>(range, *i);
}
}
template<typename EntityType, std::size_t DimSize>
static void check_data(const range_t& range, data_type<EntityType, DimSize>& data) {
auto begin = data.begin() + range.dim(N - 1).begin();
// same as "auto end = out.begin() + range.dim(N - 1).end();"
auto end = begin + range.dim(N - 1).size();
for (auto i = begin; i != end; ++i) {
range_utils<range_t, N - 1>::template check_data<EntityType, DimSize>(range, *i);
}
}
// BullseyeCoverage Compile C++ with GCC 5.4 warning suppression
// Sequence points error in braced initializer list
#if __GNUC__ && !defined(__clang__) && !defined(__INTEL_COMPILER)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsequence-point"
#endif
template<typename input_t, std::size_t... Is>
static range_t make_range(std::size_t shift, bool negative, val_t(*gen)(input_t), oneapi::tbb::detail::index_sequence<Is...>) {
return range_t( { { gen(negative ? -input_t(Is + shift) : 0), gen(input_t(Is + shift)), Is + 1} ... } );
}
#if __GNUC__ && !defined(__clang__) && !defined(__INTEL_COMPILER)
#pragma GCC diagnostic pop
#endif
static bool is_empty(const range_t& range) {
if (range.dim(N - 1).empty()) { return true; }
return range_utils<range_t, N - 1>::is_empty(range);
}
static bool is_divisible(const range_t& range) {
if (range.dim(N - 1).is_divisible()) { return true; }
return range_utils<range_t, N - 1>::is_divisible(range);
}
static void check_splitting(const range_t& range_split, const range_t& range_new, int(*get)(const val_t&), bool split_checker = false) {
if (get(range_split.dim(N - 1).begin()) == get(range_new.dim(N - 1).begin())) {
REQUIRE(get(range_split.dim(N - 1).end()) == get(range_new.dim(N - 1).end()));
}
else {
REQUIRE((get(range_split.dim(N - 1).end()) == get(range_new.dim(N - 1).begin()) && !split_checker));
split_checker = true;
}
range_utils<range_t, N - 1>::check_splitting(range_split, range_new, get, split_checker);
}
};
template<typename range_t>
struct range_utils<range_t, 0> {
using val_t = typename range_t::value_type;
template<typename EntityType, std::size_t DimSize>
using data_type = EntityType;
template<typename EntityType, std::size_t DimSize>
static void init_data(data_type<EntityType, DimSize>& data) { data = 0; }
template<typename EntityType, std::size_t DimSize>
static void increment_data(const range_t&, data_type<EntityType, DimSize>& data) { ++data; }
template<typename EntityType, std::size_t DimSize>
static void check_data(const range_t&, data_type<EntityType, DimSize>& data) {
REQUIRE(data == 1);
}
static bool is_empty(const range_t&) { return false; }
static bool is_divisible(const range_t&) { return false; }
static void check_splitting(const range_t&, const range_t&, int(*)(const val_t&), bool) {}
};
// We need MakeInt function to pass it into make_range as factory function
// because of matching make_range with AbstractValueType and other types too
int MakeInt(int i) { return i; }
template<unsigned int DimAmount>
void SerialTest() {
static_assert((oneapi::tbb::blocked_rangeNd<int, DimAmount>::ndims() == oneapi::tbb::blocked_rangeNd<AbstractValueType, DimAmount>::ndims()),
"different amount of dimensions");
using range_t = oneapi::tbb::blocked_rangeNd<AbstractValueType, DimAmount>;
using utils_t = range_utils<range_t, DimAmount>;
// Generate empty range
range_t r = utils_t::make_range(0, true, &MakeAbstractValue, oneapi::tbb::detail::make_index_sequence<DimAmount>());
utils::AssertSameType(r.is_divisible(), bool());
utils::AssertSameType(r.empty(), bool());
utils::AssertSameType(range_t::ndims(), 0U);
REQUIRE((r.empty() == utils_t::is_empty(r) && r.empty()));
REQUIRE(r.is_divisible() == utils_t::is_divisible(r));
// Generate not-empty range divisible range
r = utils_t::make_range(1, true, &MakeAbstractValue, oneapi::tbb::detail::make_index_sequence<DimAmount>());
REQUIRE((r.empty() == utils_t::is_empty(r) && !r.empty()));
REQUIRE((r.is_divisible() == utils_t::is_divisible(r) && r.is_divisible()));
range_t r_new(r, oneapi::tbb::split());
utils_t::check_splitting(r, r_new, &GetValueOf);
SerialTest<DimAmount - 1>();
}
template<> void SerialTest<0>() {}
template<unsigned int DimAmount>
void ParallelTest() {
using range_t = oneapi::tbb::blocked_rangeNd<int, DimAmount>;
using utils_t = range_utils<range_t, DimAmount>;
// Max size is 1 << 20 - 1 bytes
// Thus size of one dimension's elements is 1 << (20 / DimAmount - 1) bytes
typename utils_t::template data_type<unsigned char, 1 << (20 / DimAmount - 1)> data;
utils_t::init_data(data);
range_t r = utils_t::make_range((1 << (20 / DimAmount - 1)) - DimAmount, false, &MakeInt, oneapi::tbb::detail::make_index_sequence<DimAmount>());
oneapi::tbb::parallel_for(r, [&data](const range_t& range) {
utils_t::increment_data(range, data);
});
utils_t::check_data(r, data);
ParallelTest<DimAmount - 1>();
}
template<> void ParallelTest<0>() {}
//! Testing blocked_rangeNd construction
//! \brief \ref interface
TEST_CASE("Construction") {
oneapi::tbb::blocked_rangeNd<int, 1>{ { 0,13,3 } };
oneapi::tbb::blocked_rangeNd<int, 1>{ oneapi::tbb::blocked_range<int>{ 0,13,3 } };
oneapi::tbb::blocked_rangeNd<int, 2>(oneapi::tbb::blocked_range<int>(-8923, 8884, 13), oneapi::tbb::blocked_range<int>(-8923, 5, 13));
oneapi::tbb::blocked_rangeNd<int, 2>({ -8923, 8884, 13 }, { -8923, 8884, 13 });
oneapi::tbb::blocked_range<int> r1(0, 13);
oneapi::tbb::blocked_range<int> r2(-12, 23);
oneapi::tbb::blocked_rangeNd<int, 2>({ { -8923, 8884, 13 }, r1});
oneapi::tbb::blocked_rangeNd<int, 2>({ r2, r1 });
oneapi::tbb::blocked_rangeNd<int, 2>(r1, r2);
oneapi::tbb::blocked_rangeNd<AbstractValueType, 4>({ MakeAbstractValue(-3), MakeAbstractValue(13), 8 },
{ MakeAbstractValue(-53), MakeAbstractValue(23), 2 },
{ MakeAbstractValue(-23), MakeAbstractValue(33), 1 },
{ MakeAbstractValue(-13), MakeAbstractValue(43), 7 });
}
static const std::size_t N = 4;
//! Testing blocked_rangeNd interface
//! \brief \ref interface \ref requirement
TEST_CASE("Serial test") {
SerialTest<N>();
}
#if !EMSCRIPTEN
//! Testing blocked_rangeNd interface with parallel_for
//! \brief \ref requirement
TEST_CASE("Parallel test") {
for ( auto concurrency_level : utils::concurrency_range() ) {
oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level);
ParallelTest<N>();
}
}
#endif
//! Testing blocked_rangeNd with proportional splitting
//! \brief \ref interface \ref requirement
TEST_CASE("blocked_rangeNd proportional splitting") {
oneapi::tbb::blocked_rangeNd<int, 2> original{{0, 100}, {0, 100}};
oneapi::tbb::blocked_rangeNd<int, 2> first(original);
oneapi::tbb::proportional_split ps(3, 1);
oneapi::tbb::blocked_rangeNd<int, 2> second(first, ps);
int expected_first_end = static_cast<int>(
original.dim(0).begin() + ps.left() * (original.dim(0).end() - original.dim(0).begin()) / (ps.left() + ps.right())
);
if (first.dim(0).size() == second.dim(0).size()) {
// Splitting was made by cols
utils::check_range_bounds_after_splitting(original.dim(1), first.dim(1), second.dim(1), expected_first_end);
} else {
// Splitting was made by rows
utils::check_range_bounds_after_splitting(original.dim(0), first.dim(0), second.dim(0), expected_first_end);
}
}
|