File: test_expandable_parameters.cpp

package info (click to toggle)
gridtools 2.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,480 kB
  • sloc: cpp: 228,792; python: 17,561; javascript: 9,164; ansic: 4,101; sh: 850; makefile: 231; f90: 201
file content (172 lines) | stat: -rw-r--r-- 5,809 bytes parent folder | download | duplicates (2)
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
/*
 * GridTools
 *
 * Copyright (c) 2014-2023, ETH Zurich
 * All rights reserved.
 *
 * Please, refer to the LICENSE file in the root directory.
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <gtest/gtest.h>

#include <gridtools/stencil/cartesian.hpp>
#include <gridtools/stencil/naive.hpp>

#define GT_STENCIL_NAIVE
#include <stencil_select.hpp>
#include <test_environment.hpp>

namespace {
    using namespace gridtools;
    using namespace stencil;
    using namespace cartesian;
    using namespace expressions;

    struct expandable_parameters : ::testing::Test {
        using env_t = test_environment<>::apply<naive, double, inlined_params<13, 9, 7>>;
        using storages_t = std::vector<env_t::storage_type>;

        template <class Comp, class... Args>
        void run_computation(Comp comp, Args &&...args) const {
            expandable_run<2>(comp, naive(), env_t::make_grid(), std::forward<Args>(args)...);
        }

        void verify(storages_t const &expected, storages_t const &actual) const {
            EXPECT_EQ(expected.size(), actual.size());
            for (size_t i = 0; i != expected.size(); ++i)
                env_t::verify(expected[i], actual[i]);
        }
    };

    struct expandable_parameters_copy : expandable_parameters {

        storages_t out = {env_t::make_storage(1.),
            env_t::make_storage(2.),
            env_t::make_storage(3.),
            env_t::make_storage(4.),
            env_t::make_storage(5.)};
        storages_t in = {env_t::make_storage(-1.),
            env_t::make_storage(-2.),
            env_t::make_storage(-3.),
            env_t::make_storage(-4.),
            env_t::make_storage(-5.)};

        template <class Functor>
        void run_computation() {
            expandable_parameters::run_computation(
                [](auto out, auto in) { return execute_parallel().stage(Functor(), out, in); }, out, in);
        }

        ~expandable_parameters_copy() { verify(in, out); }
    };

    struct copy_functor {
        typedef inout_accessor<0> out;
        typedef in_accessor<1> in;

        typedef make_param_list<out, in> param_list;

        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &eval) {
            eval(out{}) = eval(in{});
        }
    };

    TEST_F(expandable_parameters_copy, copy) { run_computation<copy_functor>(); }

    struct copy_functor_with_expression {
        typedef inout_accessor<0> out;
        typedef in_accessor<1> in;

        typedef make_param_list<out, in> param_list;

        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &eval) {
            // use an expression which is equivalent to a copy to simplify the check
            eval(out{}) = eval(2. * in{} - in{});
        }
    };

    TEST_F(expandable_parameters_copy, copy_with_expression) { run_computation<copy_functor_with_expression>(); }

    struct call_proc_copy_functor {
        typedef inout_accessor<0> out;
        typedef in_accessor<1> in;

        typedef make_param_list<out, in> param_list;

        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &eval) {
            call_proc<copy_functor>::with(eval, out(), in());
        }
    };

    TEST_F(expandable_parameters_copy, call_proc_copy) { run_computation<call_proc_copy_functor>(); }

    struct call_copy_functor {
        typedef inout_accessor<0> out;
        typedef in_accessor<1> in;

        typedef make_param_list<out, in> param_list;

        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &eval) {
            eval(out()) = call<copy_functor>::with(eval, in());
        }
    };

    TEST_F(expandable_parameters_copy, call_copy) { run_computation<call_copy_functor>(); }

    struct shift_functor {
        typedef inout_accessor<0, extent<0, 0, 0, 0, -1, 0>> out;

        typedef make_param_list<out> param_list;

        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &eval) {
            eval(out()) = eval(out(0, 0, -1));
        }
    };

    struct call_shift_functor {
        typedef inout_accessor<0, extent<0, 0, 0, 0, -1, 0>> out;

        typedef make_param_list<out> param_list;

        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &eval, axis<1>::full_interval::modify<1, 0>) {
            call_proc<shift_functor>::with(eval, out());
        }
        template <typename Evaluation>
        GT_FUNCTION static void apply(Evaluation &, axis<1>::full_interval::first_level) {}
    };

    TEST_F(expandable_parameters, call_shift) {
        auto expected = [&](double value) { return env_t::make_storage([=](int_t, int_t, int_t) { return value; }); };
        auto in = [&](double value) {
            return env_t::make_storage([=](int_t, int_t, int_t k) { return k == 0 ? value : -1; });
        };

        storages_t actual = {in(14), in(15), in(16), in(17), in(18)};
        run_computation([](auto x) { return execute_forward().stage(call_shift_functor(), x); }, actual);
        verify({expected(14), expected(15), expected(16), expected(17), expected(18)}, actual);
    }

    TEST_F(expandable_parameters, caches) {
        storages_t out = {env_t::make_storage(1.),
            env_t::make_storage(2.),
            env_t::make_storage(3.),
            env_t::make_storage(4.),
            env_t::make_storage(5.)};
        auto in = env_t::make_storage(42.);
        run_computation(
            [](auto in, auto out) {
                GT_DECLARE_TMP(double, tmp);
                return execute_parallel().ij_cached(tmp).stage(copy_functor(), tmp, in).stage(copy_functor(), out, tmp);
            },
            in,
            out);
        verify({in, in, in, in, in}, out);
    }
} // namespace