Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension of Nd range #1449

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions include/oneapi/tbb/blocked_rangeNd.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2021 Intel Corporation
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.
Expand All @@ -17,17 +17,14 @@
#ifndef __TBB_blocked_rangeNd_H
#define __TBB_blocked_rangeNd_H

#if !TBB_PREVIEW_BLOCKED_RANGE_ND
#error Set TBB_PREVIEW_BLOCKED_RANGE_ND to include blocked_rangeNd.h
#endif

#include <algorithm> // std::any_of
#include <array>
#include <cstddef>
#include <type_traits> // std::is_same, std::enable_if

#include "detail/_config.h"
#include "detail/_template_helpers.h" // index_sequence, make_index_sequence
akukanov marked this conversation as resolved.
Show resolved Hide resolved
#include "detail/_namespace_injection.h"
#include "detail/_range_common.h"

#include "blocked_range.h"
Expand Down Expand Up @@ -60,22 +57,25 @@ class blocked_rangeNd_impl<Value, N, detail::index_sequence<Is...>> {
//! Type of a value.
using value_type = Value;

private:
//! Helper type to construct range with N tbb::blocked_range<value_type> objects.
template<std::size_t>
using dim_type_helper = tbb::blocked_range<value_type>;
//! Type of a dimension range.
using dim_range_type = tbb::blocked_range<value_type>;
akukanov marked this conversation as resolved.
Show resolved Hide resolved

//! Type for the size of a range.
using size_type = typename dim_range_type::size_type;

public:
blocked_rangeNd_impl() = delete;

//! Constructs N-dimensional range over N half-open intervals each represented as tbb::blocked_range<Value>.
blocked_rangeNd_impl(const dim_type_helper<Is>&... args) : my_dims{ {args...} } {}
blocked_rangeNd_impl(const indexed_t<dim_range_type, Is>&... args) : my_dims{ {args...} } {}

blocked_rangeNd_impl(const value_type (&size)[N], size_type grainsize = 1) :
my_dims { dim_range_type(0, size[Is], grainsize)... } {}

//! Dimensionality of a range.
static constexpr unsigned int ndims() { return N; }
static constexpr unsigned int dim_count() { return N; }

//! Range in certain dimension.
const tbb::blocked_range<value_type>& dim(unsigned int dimension) const {
const dim_range_type& dim(unsigned int dimension) const {
__TBB_ASSERT(dimension < N, "out of bound");
return my_dims[dimension];
}
Expand All @@ -86,14 +86,14 @@ class blocked_rangeNd_impl<Value, N, detail::index_sequence<Is...>> {

//! True if at least one dimension is empty.
bool empty() const {
return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) {
return std::any_of(my_dims.begin(), my_dims.end(), [](const dim_range_type& d) {
return d.empty();
});
}

//! True if at least one dimension is divisible.
bool is_divisible() const {
return std::any_of(my_dims.begin(), my_dims.end(), [](const tbb::blocked_range<value_type>& d) {
return std::any_of(my_dims.begin(), my_dims.end(), [](const dim_range_type& d) {
return d.is_divisible();
});
}
Expand All @@ -110,20 +110,21 @@ class blocked_rangeNd_impl<Value, N, detail::index_sequence<Is...>> {
static_assert(N != 0, "zero dimensional blocked_rangeNd can't be constructed");

//! Ranges in each dimension.
std::array<tbb::blocked_range<value_type>, N> my_dims;
std::array<dim_range_type, N> my_dims;

template<typename split_type>
void do_split(blocked_rangeNd_impl& r, split_type proportion) {
static_assert((std::is_same<split_type, split>::value || std::is_same<split_type, proportional_split>::value), "type of split object is incorrect");
static_assert((std::is_same<split_type, split>::value || std::is_same<split_type, proportional_split>::value),
"type of split object is incorrect");
__TBB_ASSERT(r.is_divisible(), "can't split not divisible range");

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) {
return (first.size() * second.grainsize() < second.size() * first.grainsize());
auto my_it = std::max_element(my_dims.begin(), my_dims.end(), [](const dim_range_type& first, const dim_range_type& second) {
return (first.size() * double(second.grainsize()) < second.size() * double(first.grainsize()));
pavelkumbrasev marked this conversation as resolved.
Show resolved Hide resolved
});

auto r_it = r.my_dims.begin() + (my_it - my_dims.begin());

my_it->my_begin = tbb::blocked_range<value_type>::do_split(*r_it, proportion);
my_it->my_begin = dim_range_type::do_split(*r_it, proportion);

// (!(my_it->my_begin < r_it->my_end) && !(r_it->my_end < my_it->my_begin)) equals to
// (my_it->my_begin == r_it->my_end), but we can't use operator== due to Value concept
Expand All @@ -144,4 +145,3 @@ using detail::d1::blocked_rangeNd;
} // namespace tbb

#endif /* __TBB_blocked_rangeNd_H */

6 changes: 5 additions & 1 deletion include/oneapi/tbb/detail/_template_helpers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2005-2023 Intel Corporation
Copyright (c) 2005-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.
Expand Down Expand Up @@ -106,6 +106,10 @@ using make_index_sequence = typename make_index_sequence_impl<N>::type;

#endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */

//! Attach an index to a type to use it with an index sequence
template<typename T, std::size_t>
using indexed_t = T;

#if __TBB_CPP17_LOGICAL_OPERATIONS_PRESENT
using std::conjunction;
using std::disjunction;
Expand Down
22 changes: 19 additions & 3 deletions test/conformance/conformance_blocked_rangeNd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
//! \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"
Expand Down Expand Up @@ -160,7 +159,7 @@ 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()),
static_assert((oneapi::tbb::blocked_rangeNd<int, DimAmount>::dim_count() == oneapi::tbb::blocked_rangeNd<AbstractValueType, DimAmount>::dim_count()),
"different amount of dimensions");

using range_t = oneapi::tbb::blocked_rangeNd<AbstractValueType, DimAmount>;
Expand All @@ -171,7 +170,7 @@ void SerialTest() {

utils::AssertSameType(r.is_divisible(), bool());
utils::AssertSameType(r.empty(), bool());
utils::AssertSameType(range_t::ndims(), 0U);
utils::AssertSameType(range_t::dim_count(), 0U);

REQUIRE((r.empty() == utils_t::is_empty(r) && r.empty()));
REQUIRE(r.is_divisible() == utils_t::is_divisible(r));
Expand Down Expand Up @@ -231,6 +230,23 @@ TEST_CASE("Construction") {

oneapi::tbb::blocked_rangeNd<int, 2>(r1, r2);

int sizes[] = {174, 39, 2481, 93};
oneapi::tbb::blocked_rangeNd<int, 4> rNd_1(sizes, /*grainsize*/7);

oneapi::tbb::blocked_rangeNd<int, 4> rNd_2({174, 39, 2481, 93}, /*grainsize*/11);

for (unsigned i = 0; i < rNd_1.dim_count(); ++i) {
oneapi::tbb::blocked_rangeNd<int, 4>::dim_range_type dim1 = rNd_1.dim(i);
oneapi::tbb::blocked_rangeNd<int, 4>::dim_range_type dim2 = rNd_2.dim(i);
REQUIRE(dim1.begin()==0);
REQUIRE(dim2.begin()==0);
unsigned int szi = sizes[i]; // to compare with unsigned integrals without warnings
REQUIRE(dim1.size()==szi);
REQUIRE(dim2.size()==szi);
REQUIRE(dim1.grainsize()==7);
REQUIRE(dim2.grainsize()==11);
}

oneapi::tbb::blocked_rangeNd<AbstractValueType, 4>({ MakeAbstractValue(-3), MakeAbstractValue(13), 8 },
{ MakeAbstractValue(-53), MakeAbstractValue(23), 2 },
{ MakeAbstractValue(-23), MakeAbstractValue(33), 1 },
Expand Down
Loading