Skip to content

Optimizations for temporaries with non-scalar datatype #1662

@havogt

Description

@havogt

To represent (small) matrices and vectors per gridpoint we allow higher (than 3) dimensional storages (with backend specific) layout (typically structure-of-array like, i.e. the matrix/vector dimensions are outermost). For temporaries, we can only express 3D temporaries, but we allow non-scalar element type. Temporaries with non-scalar element type have a natural API, but will have non-optimal data layout (array-of-structure).

If the user expresses the non-scalar datatype (see example below) with (nested) arrays we can do custom allocation with backend specific layout, without changing the API.

/*
 * 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
 */

#include "gridtools/stencil/frontend/cartesian/tmp_arg.hpp"
#include "gridtools/stencil/frontend/run.hpp"
#include <gtest/gtest.h>

#include <gridtools/common/array.hpp>
#include <gridtools/stencil/cartesian.hpp>

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

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

    struct copy_functor {
        using in = in_accessor<0>;
        using out = inout_accessor<1>;
        using tmp = inout_accessor<2>;

        using param_list = make_param_list<in, out, tmp>;

        template <class Eval>
        GT_FUNCTION static void apply(Eval &&eval) {
            eval(tmp())[0] = eval(in());
            eval(out()) = eval(tmp())[0];
        }
    };

    GT_REGRESSION_TEST(copy_stencil_higher_dim, test_environment<>, stencil_backend_t) {
        auto in = [](int i, int j, int k) { return i + j + k; };
        auto out = TypeParam::make_storage();
        run(
            [](auto out, auto in) {
                GT_DECLARE_TMP((array<typename TypeParam::float_t, 2>), tmp);
                return execute_parallel().stage(copy_functor(), in, out, tmp);
            },
            stencil_backend_t(),
            TypeParam::make_grid(),
            out,
            TypeParam::make_storage(in));
        TypeParam::verify(in, out);
    }
} // namespace

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions