Skip to content

Commit

Permalink
Merge pull request #3076 from E3SM-Project/mahf708/field-utils/views-…
Browse files Browse the repository at this point in the history
…are-equal-0

support views_are_equal for rank-0
  • Loading branch information
mahf708 authored Oct 31, 2024
2 parents d487f27 + aa1dcd3 commit cb30379
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions components/eamxx/src/share/field/field_utils_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ bool views_are_equal(const Field& f1, const Field& f2, const ekat::Comm* comm)
bool same_locally = true;
const auto& dims = l1.dims();
switch (l1.rank()) {
case 0:
{
auto v1 = f1.template get_strided_view<ST,Host>();
auto v2 = f2.template get_strided_view<ST,Host>();
if (v1() != v2()) {
same_locally = false;
break;
}
break;
}
case 1:
{
auto v1 = f1.template get_strided_view<ST*,Host>();
Expand Down
25 changes: 25 additions & 0 deletions components/eamxx/src/share/tests/field_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ TEST_CASE("utils") {
f1.get_header().get_alloc_properties().request_allocation(P8::n);
f1.allocate_view();

SECTION("compare-rank-0") {
// create two fields with rank-0 and get their views
std::vector<FieldTag> tags_0 = {};
std::vector<int> dims_0 = {};
FieldIdentifier fid_01("field_01", {tags_0, dims_0}, m / s, "some_grid");
FieldIdentifier fid_02("field_02", {tags_0, dims_0}, m / s, "some_grid");
Field f01(fid_01);
Field f02(fid_02);
f01.allocate_view();
f02.allocate_view();
auto f01v = f01.get_view<Real>();
auto f02v = f02.get_view<Real>();
// fill the views with the same values
Real val = 54321;
Kokkos::deep_copy(f01v, val);
Kokkos::deep_copy(f02v, val);
// check that the views are equal
REQUIRE(views_are_equal(f01, f02));

// fill the views with different values
Kokkos::deep_copy(f02v, 1 / val);
// check that the views are not equal
REQUIRE(not views_are_equal(f01, f02));
}

SECTION ("compare") {

Field f2(fid);
Expand Down

0 comments on commit cb30379

Please sign in to comment.