|
| 1 | +// SPDX-FileCopyrightText: Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +#include <dr/mhp.hpp> |
| 6 | +#include <fmt/core.h> |
| 7 | + |
| 8 | +namespace mhp = dr::mhp; |
| 9 | + |
| 10 | +using T = uint16_t; |
| 11 | +using MDA = dr::mhp::distributed_mdarray<T, 2>; |
| 12 | + |
| 13 | +int main() { |
| 14 | +#ifdef SYCL_LANGUAGE_VERSION |
| 15 | + mhp::init(sycl::default_selector_v); |
| 16 | +#else |
| 17 | + mhp::init(); |
| 18 | +#endif |
| 19 | + std::size_t arr_size = 7; |
| 20 | + const std::size_t pattern_size = 2; |
| 21 | + const std::size_t radius = pattern_size - 1; |
| 22 | + std::array slice_starts{radius - 1, radius - 1}; |
| 23 | + std::array slice_ends{arr_size - radius, arr_size - radius}; |
| 24 | + |
| 25 | + auto dist = dr::mhp::distribution().halo(radius); |
| 26 | + MDA a({arr_size, arr_size}, dist); |
| 27 | + MDA occurrences_coords({arr_size, arr_size}); |
| 28 | + |
| 29 | + mhp::iota(a, 1); |
| 30 | + mhp::transform(a, a.begin(), [](auto &&v) { return v % 2; }); |
| 31 | + mhp::fill(occurrences_coords, 0); |
| 32 | + |
| 33 | + auto a_submdspan = |
| 34 | + dr::mhp::views::submdspan(a.view(), slice_starts, slice_ends); |
| 35 | + auto occurrences = dr::mhp::views::submdspan(occurrences_coords.view(), |
| 36 | + slice_starts, slice_ends); |
| 37 | + int pattern[pattern_size][pattern_size] = {{1, 0}, {0, 1}}; |
| 38 | + |
| 39 | + auto mdspan_pattern_op = [pattern](auto &&v) { |
| 40 | + auto [a_submdspan, occurrences] = v; |
| 41 | + if (pattern[0][0] == a_submdspan(0, 0) && |
| 42 | + pattern[0][1] == a_submdspan(0, 1) && |
| 43 | + pattern[1][0] == a_submdspan(1, 0) && |
| 44 | + pattern[1][1] == a_submdspan(1, 1)) { |
| 45 | + occurrences(0, 0)++; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + mhp::halo(a).exchange(); |
| 50 | + mhp::stencil_for_each(mdspan_pattern_op, a_submdspan, occurrences); |
| 51 | + |
| 52 | + if (mhp::rank() == 0) { |
| 53 | + fmt::print("a: \n{} \n", a.mdspan()); |
| 54 | + fmt::print("pattern: \n{} \n", pattern); |
| 55 | + fmt::print("occurrences: \n{} \n", occurrences_coords.mdspan()); |
| 56 | + } |
| 57 | + |
| 58 | + mhp::finalize(); |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
0 commit comments