diff --git a/include/gridtools/sid/rename_dimension.hpp b/include/gridtools/sid/rename_dimension.hpp new file mode 100644 index 0000000000..a4e4652616 --- /dev/null +++ b/include/gridtools/sid/rename_dimension.hpp @@ -0,0 +1,61 @@ +/* + * GridTools + * + * Copyright (c) 2014-2019, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include + +#include "../common/hymap.hpp" +#include "../meta.hpp" +#include "concept.hpp" +#include "delegate.hpp" + +namespace gridtools { + namespace sid { + namespace rename_dimension_impl_ { + template + auto remap(Map map) { + return hymap::convert_to, OldKey, NewKey>>(std::move(map)); + } + + template + struct renamed_sid : delegate { + template + using remapped_t = decltype(remap(std::declval())); + + template + renamed_sid(T &&obj) : delegate(std::forward(obj)) {} + + friend remapped_t> sid_get_strides(renamed_sid const &obj) { + return remap(sid_get_strides(obj.impl())); + } + friend remapped_t> sid_get_lower_bounds(renamed_sid const &obj) { + return remap(sid_get_lower_bounds(obj.impl())); + } + friend remapped_t> sid_get_upper_bounds(renamed_sid const &obj) { + return remap(sid_get_upper_bounds(obj.impl())); + } + }; + + template + struct stride_kind_wrapper {}; + + template + stride_kind_wrapper> sid_get_strides_kind( + renamed_sid const &); + + template + renamed_sid rename_dimension(Sid &&sid) { + return renamed_sid{std::forward(sid)}; + } + } // namespace rename_dimension_impl_ + using rename_dimension_impl_::rename_dimension; + } // namespace sid +} // namespace gridtools diff --git a/tests/regression/CMakeLists.txt b/tests/regression/CMakeLists.txt index b7ab11f932..5fb874337e 100644 --- a/tests/regression/CMakeLists.txt +++ b/tests/regression/CMakeLists.txt @@ -114,6 +114,7 @@ gridtools_add_cartesian_regression_test(extended_4D SOURCES extended_4D.cpp) gridtools_add_cartesian_regression_test(expandable_parameters SOURCES expandable_parameters.cpp) gridtools_add_cartesian_regression_test(expandable_parameters_single_kernel SOURCES expandable_parameters_single_kernel.cpp) gridtools_add_cartesian_regression_test(horizontal_diffusion_functions SOURCES horizontal_diffusion_functions.cpp) +gridtools_add_cartesian_regression_test(whole_axis_access SOURCES whole_axis_access.cpp) gridtools_add_layout_transformation_test() gridtools_add_boundary_conditions_test() diff --git a/tests/regression/whole_axis_access.cpp b/tests/regression/whole_axis_access.cpp new file mode 100644 index 0000000000..455f51be5e --- /dev/null +++ b/tests/regression/whole_axis_access.cpp @@ -0,0 +1,59 @@ +/* + * GridTools + * + * Copyright (c) 2014-2019, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ +#include + +#include +#include +#include +#include + +#include +#include + +namespace { + using namespace gridtools; + using namespace stencil; + using namespace cartesian; + + struct functor { + using out = inout_accessor<0>; + using in = in_accessor<1, extent<>, 4>; + using k_pos = in_accessor<2>; + using param_list = make_param_list; + + template + GT_FUNCTION static void apply(Eval &&eval) { + auto k = eval(k_pos()); + std::decay_t res = 0; + for (int kk = 0; kk < k; ++kk) + res += eval(in(0, 0, 0, kk)); + eval(out()) = res; + } + }; + + GT_REGRESSION_TEST(whole_axis_access, test_environment<>, stencil_backend_t) { + auto in = [](int i, int j, int k) { return i + j + k; }; + auto out = TypeParam::make_storage(); + run_single_stage(functor(), + stencil_backend_t(), + TypeParam::make_grid(), + out, + sid::rename_dimension>(TypeParam::make_storage(in)), + positional()); + TypeParam::verify( + [in](int i, int j, int k) { + int res = 0; + for (int kk = 0; kk < k; ++kk) + res += in(i, j, kk); + return res; + }, + out); + } +} // namespace diff --git a/tests/unit_tests/sid/CMakeLists.txt b/tests/unit_tests/sid/CMakeLists.txt index bcf78ec002..cca77822c8 100644 --- a/tests/unit_tests/sid/CMakeLists.txt +++ b/tests/unit_tests/sid/CMakeLists.txt @@ -8,3 +8,4 @@ gridtools_add_unit_test(test_sid_loop SOURCES test_sid_loop.cpp) gridtools_add_unit_test(test_sid_multi_shift SOURCES test_sid_multi_shift.cpp) gridtools_add_unit_test(test_sid_shift_sid_origin SOURCES test_sid_shift_sid_origin.cpp) gridtools_add_unit_test(test_sid_synthetic SOURCES test_sid_synthetic.cpp) +gridtools_add_unit_test(test_sid_rename_dimension SOURCES test_sid_rename_dimension.cpp) diff --git a/tests/unit_tests/sid/test_sid_rename_dimension.cpp b/tests/unit_tests/sid/test_sid_rename_dimension.cpp new file mode 100644 index 0000000000..6ef339c2d1 --- /dev/null +++ b/tests/unit_tests/sid/test_sid_rename_dimension.cpp @@ -0,0 +1,56 @@ +/* + * GridTools + * + * Copyright (c) 2014-2019, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include + +#include +#include +#include +#include +#include + +namespace gridtools { + namespace { + using sid::property; + using namespace literals; + namespace tu = tuple_util; + + struct a {}; + struct b {}; + struct c {}; + struct d {}; + + TEST(rename_dimensions, smoke) { + double data[3][5][7]; + + auto src = sid::synthetic() + .set(sid::make_simple_ptr_holder(&data[0][0][0])) + .set(tu::make::values>(5_c * 7_c, 7_c, 1_c)) + .set(tu::make::values>(3, 5)); + + auto testee = sid::rename_dimension(src); + using testee_t = decltype(testee); + + auto strides = sid::get_strides(testee); + EXPECT_EQ(35, sid::get_stride(strides)); + EXPECT_EQ(0, sid::get_stride(strides)); + EXPECT_EQ(1, sid::get_stride(strides)); + EXPECT_EQ(7, sid::get_stride(strides)); + + static_assert(meta::is_empty>>(), ""); + + auto u_bound = sid::get_upper_bounds(testee); + EXPECT_EQ(3, at_key(u_bound)); + EXPECT_EQ(5, at_key(u_bound)); + } + } // namespace +} // namespace gridtools