Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SIDs as neighbour tables in functional API #1730

Merged
merged 27 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a68f595
added neighbor table and a test
petiaccja Aug 17, 2022
9c5a26b
proper reading of elements
petiaccja Aug 18, 2022
9f27022
includes and size_t
petiaccja Aug 24, 2022
d93bf1f
more includes and size_t
petiaccja Aug 24, 2022
813a41f
includes
petiaccja Aug 24, 2022
ae473c2
includes
petiaccja Aug 24, 2022
47c8393
test namespaces
petiaccja Aug 24, 2022
d1f99ef
use sid::shift
petiaccja Aug 24, 2022
dd74069
use C arrays instead of std::array
petiaccja Aug 24, 2022
fa40976
code style
petiaccja Aug 24, 2022
6fb6a86
changed implementation to use ptr holder and strides instead of whole…
petiaccja Aug 24, 2022
0bbbdc7
make ADL function a friend
petiaccja Aug 25, 2022
ab55a32
test on cuda
petiaccja Aug 25, 2022
fa522e3
remove unused includes
petiaccja Aug 25, 2022
d9569af
cuda playing funny games
petiaccja Aug 25, 2022
b67bdf8
use dynamic device memory
petiaccja Aug 25, 2022
9afdaae
to trailing return type
petiaccja Aug 25, 2022
f47a4ec
using neighbor_table::neighbors wrapper fun
petiaccja Aug 25, 2022
af63168
removed superfluous template params
petiaccja Aug 26, 2022
fbde942
consistent const placement
petiaccja Aug 26, 2022
63eb832
i don't know, just trying to get it compile...
petiaccja Aug 26, 2022
37742bd
ints
petiaccja Aug 26, 2022
33bbd76
removed friend function to fix old gcc builds
petiaccja Aug 26, 2022
9e58c84
use integral constant expressions
petiaccja Aug 29, 2022
95f088a
use decay_t to support const ptr holders
petiaccja Aug 30, 2022
4497a48
use const array in tests
petiaccja Aug 30, 2022
907d43c
Update AUTHORS
havogt Oct 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions include/gridtools/fn/sid_neighbor_table.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* GridTools
*
* Copyright (c) 2014-2022, ETH Zurich
* All rights reserved.
*
* Please, refer to the LICENSE file in the root directory.
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once

#include <cstdint>
#include <gridtools/common/array.hpp>
#include <gridtools/fn/unstructured.hpp>
#include <gridtools/sid/concept.hpp>
#include <type_traits>
petiaccja marked this conversation as resolved.
Show resolved Hide resolved

namespace gridtools::fn::sid_neighbor_table {
namespace sid_neighbor_table_impl_ {
template <class IndexDimension, class NeighborDimension, int32_t MaxNumNeighbors, class Sid>
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
struct sid_neighbor_table {
Sid sid;
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
};

template <class IndexDimension, class NeighborDimension, int32_t MaxNumNeighbors, class Sid>
auto neighbor_table_neighbors(
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
sid_neighbor_table<IndexDimension, NeighborDimension, MaxNumNeighbors, Sid> const &table, size_t index) {
using element_type = sid::element_type<Sid>;

const auto ptr = sid_get_origin(table.sid);
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
const auto strides = sid_get_strides(table.sid);

const auto index_stride = at_key<IndexDimension>(strides);
const auto neighbour_stride = at_key<NeighborDimension>(strides);

petiaccja marked this conversation as resolved.
Show resolved Hide resolved
gridtools::array<element_type, MaxNumNeighbors> neighbors;
for (int32_t elementIdx = 0; elementIdx < MaxNumNeighbors; ++elementIdx) {
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
const auto element_ptr = ptr + index * index_stride + elementIdx * neighbour_stride;
neighbors[elementIdx] = *element_ptr();
}
return neighbors;
}

template <class IndexDimension, class NeighborDimension, int32_t MaxNumNeighbors, class Sid>
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
auto as_neighbor_table(Sid sid) {
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
static_assert(gridtools::tuple_util::size<decltype(sid_get_strides(std::declval<Sid>()))>::value == 2,
"Neighbor tables must have exactly two dimensions: the index dimension and the neighbor dimension");
static_assert(!std::is_same_v<IndexDimension, NeighborDimension>,
"The index dimension and the neighbor dimension must be different.");

return sid_neighbor_table<IndexDimension, NeighborDimension, MaxNumNeighbors, Sid>{std::move(sid)};
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
}
} // namespace sid_neighbor_table_impl_

using sid_neighbor_table_impl_::as_neighbor_table;

} // namespace gridtools::fn::sid_neighbor_table
havogt marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions tests/unit_tests/fn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gridtools_add_unit_test(test_fn_run SOURCES test_fn_run.cpp)
gridtools_add_unit_test(test_fn_column_stage SOURCES test_fn_column_stage.cpp)
gridtools_add_unit_test(test_fn_stencil_stage SOURCES test_fn_stencil_stage.cpp LABELS fn)
gridtools_add_unit_test(test_fn_unstructured SOURCES test_fn_unstructured.cpp LABELS fn)
gridtools_add_unit_test(test_fn_sid_neighbor_table SOURCES test_fn_sid_neighbor_table.cpp LABELS fn)

if(TARGET _gridtools_cuda)
gridtools_add_unit_test(test_fn_backend_gpu_cuda
Expand Down
49 changes: 49 additions & 0 deletions tests/unit_tests/fn/test_fn_sid_neighbor_table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* GridTools
*
* Copyright (c) 2014-2022, 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/fn/unstructured.hpp>
#include <gridtools/sid/allocator.hpp>
#include <gridtools/sid/composite.hpp>
#include <gridtools/sid/synthetic.hpp>

#include <array>
#include <cstddef>
#include <cstdint>
#include <gridtools/fn/sid_neighbor_table.hpp>
petiaccja marked this conversation as resolved.
Show resolved Hide resolved

using namespace gridtools;
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
using namespace fn;
using sid_neighbor_table::as_neighbor_table;

using edge_dim_t = unstructured::dim::horizontal;
using edge_to_cell_dim_t = struct {};

TEST(sid_neighbor_table, correctness) {
constexpr size_t numElements = 3;
constexpr size_t numNeighbors = 2;
std::array<int32_t, numElements *numNeighbors> data = {0, 1, 10, 11, 20, 21};
petiaccja marked this conversation as resolved.
Show resolved Hide resolved
using dim_hymap_t = hymap::keys<edge_dim_t, edge_to_cell_dim_t>;
auto contents = sid::synthetic()
.set<sid::property::origin>(sid::host_device::simple_ptr_holder(data.data()))
.set<sid::property::strides>(dim_hymap_t::make_values(numNeighbors, 1));
const auto table = as_neighbor_table<edge_dim_t, edge_to_cell_dim_t, numNeighbors>(contents);

auto [n00, n01] = neighbor_table_neighbors(table, 0);
auto [n10, n11] = neighbor_table_neighbors(table, 1);
auto [n20, n21] = neighbor_table_neighbors(table, 2);
EXPECT_EQ(n00, 0);
EXPECT_EQ(n01, 1);
EXPECT_EQ(n10, 10);
EXPECT_EQ(n11, 11);
EXPECT_EQ(n20, 20);
EXPECT_EQ(n21, 21);
}