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
|
/*
* 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 <stencil_select.hpp>
#include <test_environment.hpp>
namespace {
using namespace gridtools;
using namespace stencil;
using namespace cartesian;
using axis_t = axis<3, axis_config::offset_limit<3>>;
using kfull = axis_t::full_interval;
double in(int i, int j, int k) { return i + j + k + 1; }
GT_ENVIRONMENT_TEST_SUITE(test_kcache_local,
(vertical_test_environment<0, axis_t>),
stencil_backend_t,
(double, inlined_params<6, 6, 2, 6, 2>));
struct shift_acc_forward {
using in = in_accessor<0>;
using out = inout_accessor<1>;
using buff = inout_accessor<2, extent<0, 0, 0, 0, -1, 0>>;
using param_list = make_param_list<in, out, buff>;
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::first_level) {
eval(buff()) = eval(in());
eval(out()) = eval(buff());
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::modify<1, 0>) {
eval(buff()) = eval(buff(0, 0, -1)) + eval(in());
eval(out()) = eval(buff());
}
};
GT_ENVIRONMENT_TYPED_TEST(test_kcache_local, forward) {
auto out = TypeParam::make_storage();
auto spec = [](auto in, auto out) {
GT_DECLARE_TMP(double, tmp);
return execute_forward().k_cached(tmp).stage(shift_acc_forward(), in, out, tmp);
};
run(spec, stencil_backend_t(), TypeParam::make_grid(), TypeParam::make_storage(in), out);
auto ref = TypeParam::make_storage();
auto refv = ref->host_view();
for (int i = 0; i < TypeParam::d(0); ++i)
for (int j = 0; j < TypeParam::d(1); ++j) {
refv(i, j, 0) = in(i, j, 0);
for (int k = 1; k < 10; ++k) {
refv(i, j, k) = refv(i, j, k - 1) + in(i, j, k);
}
}
TypeParam::verify(ref, out);
}
struct shift_acc_backward {
using in = in_accessor<0>;
using out = inout_accessor<1>;
using buff = inout_accessor<2, extent<0, 0, 0, 0, 0, 1>>;
using param_list = make_param_list<in, out, buff>;
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::last_level) {
eval(buff()) = eval(in());
eval(out()) = eval(buff());
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::modify<0, -1>) {
eval(buff()) = eval(buff(0, 0, 1)) + eval(in());
eval(out()) = eval(buff());
}
};
GT_ENVIRONMENT_TYPED_TEST(test_kcache_local, backward) {
auto out = TypeParam::make_storage();
auto spec = [](auto in, auto out) {
GT_DECLARE_TMP(double, tmp);
return execute_backward().k_cached(tmp).stage(shift_acc_backward(), in, out, tmp);
};
run(spec, stencil_backend_t(), TypeParam::make_grid(), TypeParam::make_storage(in), out);
auto ref = TypeParam::make_storage();
auto refv = ref->host_view();
for (int i = 0; i < TypeParam::d(0); ++i)
for (int j = 0; j < TypeParam::d(1); ++j) {
refv(i, j, 9) = in(i, j, 9);
for (int k = 8; k >= 0; --k) {
refv(i, j, k) = refv(i, j, k + 1) + in(i, j, k);
}
}
TypeParam::verify(ref, out);
}
struct biside_large_kcache_forward {
using in = in_accessor<0>;
using out = inout_accessor<1>;
using buff = inout_accessor<2, extent<0, 0, 0, 0, -2, 1>>;
using param_list = make_param_list<in, out, buff>;
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::first_level) {
eval(buff()) = eval(in());
eval(buff(0, 0, 1)) = eval(in()) / 2;
eval(out()) = eval(buff());
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::first_level::shift<1>) {
eval(buff(0, 0, 1)) = eval(in()) / 2;
eval(out()) = eval(buff()) + eval(buff(0, 0, -1)) / 4;
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::modify<2, -1>) {
eval(buff(0, 0, 1)) = eval(in()) / 2;
eval(out()) = eval(buff()) + eval(buff(0, 0, -1)) / 4 + eval(buff(0, 0, -2)) * .12;
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::last_level) {
eval(out()) = eval(buff()) + eval(buff(0, 0, -1)) / 4 + eval(buff(0, 0, -2)) * .12;
}
};
GT_ENVIRONMENT_TYPED_TEST(test_kcache_local, biside_forward) {
auto out = TypeParam::make_storage();
auto spec = [](auto in, auto out) {
GT_DECLARE_TMP(double, tmp);
return execute_forward().k_cached(tmp).stage(biside_large_kcache_forward(), in, out, tmp);
};
run(spec, stencil_backend_t(), TypeParam::make_grid(), TypeParam::make_storage(in), out);
auto ref = TypeParam::make_storage();
auto refv = ref->host_view();
auto buff = TypeParam::make_storage();
auto buffv = buff->host_view();
for (int i = 0; i < TypeParam::d(0); ++i)
for (int_t j = 0; j < TypeParam::d(1); ++j) {
buffv(i, j, 0) = in(i, j, 0);
buffv(i, j, 1) = in(i, j, 0) / 2;
refv(i, j, 0) = in(i, j, 0);
buffv(i, j, 2) = in(i, j, 1) / 2;
refv(i, j, 1) = buffv(i, j, 1) + buffv(i, j, 0) / 4;
for (int k = 2; k < 10; ++k) {
if (k != 9)
buffv(i, j, k + 1) = in(i, j, k) / 2;
refv(i, j, k) = buffv(i, j, k) + buffv(i, j, k - 1) / 4 + .12 * buffv(i, j, k - 2);
}
}
TypeParam::verify(ref, out);
}
struct biside_large_kcache_backward {
using in = in_accessor<0>;
using out = inout_accessor<1>;
using buff = inout_accessor<2, extent<0, 0, 0, 0, -1, 2>>;
using param_list = make_param_list<in, out, buff>;
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::last_level) {
eval(buff()) = eval(in());
eval(buff(0, 0, -1)) = eval(in()) / 2;
eval(out()) = eval(buff());
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::last_level::shift<-1>) {
eval(buff(0, 0, -1)) = eval(in()) / 2;
eval(out()) = eval(buff()) + eval(buff(0, 0, 1)) / 4;
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::modify<1, -2>) {
eval(buff(0, 0, -1)) = eval(in()) / 2;
eval(out()) = eval(buff()) + eval(buff(0, 0, 1)) / 4 + eval(buff(0, 0, 2)) * .12;
}
template <class Eval>
GT_FUNCTION static void apply(Eval &&eval, kfull::first_level) {
eval(out()) = eval(buff()) + eval(buff(0, 0, 1)) / 4 + eval(buff(0, 0, 2)) * .12;
}
};
GT_ENVIRONMENT_TYPED_TEST(test_kcache_local, biside_backward) {
auto out = TypeParam::make_storage();
auto spec = [](auto in, auto out) {
GT_DECLARE_TMP(double, tmp);
return execute_backward().k_cached(tmp).stage(biside_large_kcache_backward(), in, out, tmp);
};
run(spec, stencil_backend_t(), TypeParam::make_grid(), TypeParam::make_storage(in), out);
auto ref = TypeParam::make_storage();
auto refv = ref->host_view();
auto buff = TypeParam::make_storage();
auto buffv = buff->host_view();
for (int i = 0; i < TypeParam::d(0); ++i)
for (int j = 0; j < TypeParam::d(1); ++j) {
buffv(i, j, 9) = in(i, j, 9);
buffv(i, j, 8) = in(i, j, 9) / 2;
refv(i, j, 9) = in(i, j, 9);
buffv(i, j, 7) = in(i, j, 8) / 2;
refv(i, j, 8) = buffv(i, j, 8) + buffv(i, j, 9) / 4;
for (int_t k = 7; k >= 0; --k) {
if (k != 0)
buffv(i, j, k - 1) = in(i, j, k) / 2;
refv(i, j, k) = buffv(i, j, k) + buffv(i, j, k + 1) / 4 + .12 * buffv(i, j, k + 2);
}
}
TypeParam::verify(ref, out);
}
} // namespace
|