Skip to content

Commit

Permalink
Sid: dimension to tuple like (#1750)
Browse files Browse the repository at this point in the history
Adds a sid transformations utility which translates a sid with dimension `D` and element type `T` to a sid with `D` removed and type is `tuple<T>` like, with tuple_size `N` for `sid::dimension_to_tuple_like<D, N>(sid)`.

Additional:
- new regression test for performance comparison of different kinds of tuple-likes
  • Loading branch information
havogt authored Apr 20, 2023
1 parent 43d826c commit e19411f
Show file tree
Hide file tree
Showing 19 changed files with 90,954 additions and 53,268 deletions.
2 changes: 1 addition & 1 deletion include/gridtools/common/tuple_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
* ```
* Also tuple algorithms works with `foo` as expected:
* ```
* auto x = tuple_util::trasnform([](auto x) { return x * 2; }, foo{1, 2.5});
* auto x = tuple_util::transform([](auto x) { return x * 2; }, foo{1, 2.5});
* assert(x.a == 2);
* assert(x.b == 5.);
* ```
Expand Down
12 changes: 3 additions & 9 deletions include/gridtools/sid/composite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <cassert>
#include <functional>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -134,13 +135,6 @@ namespace gridtools {
};

struct ctor_tag {};

struct sum {
template <class Lhs, class Rhs>
GT_FUNCTION constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const {
return std::forward<Lhs>(lhs) + std::forward<Rhs>(rhs);
}
};
} // namespace impl_

/**
Expand Down Expand Up @@ -325,13 +319,13 @@ namespace gridtools {
template <class... Ptrs>
friend constexpr GT_FUNCTION composite_ptr<Ptrs...> operator+(
composite_ptr<Ptrs...> const &lhs, composite_entity const &rhs) {
return tuple_util::host_device::transform(impl_::sum(), lhs, rhs);
return tuple_util::host_device::transform(std::plus<>{}, lhs, rhs);
}

template <class... PtrHolders>
friend composite_ptr_holder<PtrHolders...> operator+(
composite_ptr_holder<PtrHolders...> const &lhs, composite_entity const &rhs) {
return tuple_util::transform(impl_::sum(), lhs, rhs);
return tuple_util::transform(std::plus<>{}, lhs, rhs);
}

template <class... Ptrs, class Offset>
Expand Down
81 changes: 81 additions & 0 deletions include/gridtools/sid/dimension_to_tuple_like.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* GridTools
*
* Copyright (c) 2014-2021, ETH Zurich
* All rights reserved.
*
* Please, refer to the LICENSE file in the root directory.
* SPDX-License-Identifier: BSD-3-Clause
*/

#pragma once

#include <type_traits>
#include <utility>

#include "../common/host_device.hpp"
#include "../common/hymap.hpp"
#include "../meta.hpp"
#include "composite.hpp"
#include "concept.hpp"
#include "delegate.hpp"
#include "sid_shift_origin.hpp"

namespace gridtools {
namespace sid {
namespace dimension_to_tuple_like_impl_ {
template <class Dim, class Sid>
struct remove_dimension_sid : sid::delegate<Sid> {
friend decltype(hymap::canonicalize_and_remove_key<Dim>(std::declval<sid::strides_type<Sid>>()))
sid_get_strides(remove_dimension_sid const &obj) {
return hymap::canonicalize_and_remove_key<Dim>(sid::get_strides(obj.m_impl));
}
friend decltype(hymap::canonicalize_and_remove_key<Dim>(std::declval<sid::lower_bounds_type<Sid>>()))
sid_get_lower_bounds(remove_dimension_sid const &obj) {
return hymap::canonicalize_and_remove_key<Dim>(sid::get_lower_bounds(obj.m_impl));
}
friend decltype(hymap::canonicalize_and_remove_key<Dim>(std::declval<sid::upper_bounds_type<Sid>>()))
sid_get_upper_bounds(remove_dimension_sid const &obj) {
return hymap::canonicalize_and_remove_key<Dim>(sid::get_upper_bounds(obj.m_impl));
}

using sid::delegate<Sid>::delegate;
};

template <class Dim, class Sid>
remove_dimension_sid<Dim, Sid> remove_dimension(Sid &&sid) {
return {std::forward<Sid>(sid)};
}

template <class T, class U>
std::enable_if_t<std::is_reference_v<T>, T> copy_if_rvalue(U &p) {
return p;
}
template <class T, class U>
std::enable_if_t<!std::is_reference_v<T>, T> copy_if_rvalue(U const &p) {
return p;
}

template <class Dim, class Sid, size_t... Is>
constexpr decltype(auto) as_tuple_like_helper(Sid &&sid, std::index_sequence<Is...>) {
using keys = sid::composite::keys<integral_constant<int, Is>...>;
return keys::make_values(remove_dimension<Dim>(sid::shift_sid_origin(
copy_if_rvalue<Sid>(sid), // copy required otherwise we move away from `sid` multiple times
hymap::keys<Dim>::make_values(Is)))...);
}
} // namespace dimension_to_tuple_like_impl_

/**
* Returns a SID, where `Dim` of `sid` is mapped to a tuple-like of size `N`.
*
* TODO(havogt):
* - Currently, no bounds check is implemented.
* - In case bounds are compile-time known we could infer `N`.
*/
template <class Dim, size_t N, class Sid>
decltype(auto) dimension_to_tuple_like(Sid &&sid) {
return dimension_to_tuple_like_impl_::as_tuple_like_helper<Dim>(
std::forward<Sid>(sid), std::make_index_sequence<N>{});
}
} // namespace sid
} // namespace gridtools
5 changes: 5 additions & 0 deletions include/gridtools/stencil/common/intent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace gridtools {
template <intent Intent, class T>
struct apply_intent_type;

template <class T>
struct apply_intent_type<intent::inout, T> {
using type = T; // in case T is wrapping a reference (e.g. sid::composite)
};

template <class T>
struct apply_intent_type<intent::inout, T &> {
using type = T &;
Expand Down
3 changes: 1 addition & 2 deletions jenkins/update_references.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ for domain in 128 256; do
for label in ault dom daint-cn tsa; do
for env in cray hip nvcc_cray nvcc_cray_cxx20 nvcc_gcc; do
current="${label%-*}_$env/$domain.json"
src="https://jenkins-mch.cscs.ch/view/GridTools/job/GridTools_perftest_PR/$1/env=$env,label=$label/artifact/build/pyutils/perftest/results/$current"

src="https://jenkins-mch.cscs.ch/job/GridTools/job/GridTools_perftest_PR/$1/env=$env,label=$label/artifact/build/pyutils/perftest/results/$current"
tmp=$(mktemp)
curl -fs -u "$user:$password" -o "$tmp" "$src"

Expand Down
Loading

0 comments on commit e19411f

Please sign in to comment.