Skip to content

Commit 876ca99

Browse files
committed
add example6 - find a 2d pattern in a distributed_mdarray
1 parent 7db7b6c commit 876ca99

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ add_executable(example3 example3.cpp)
2626

2727
target_compile_definitions(example3 INTERFACE DR_FORMAT)
2828
target_link_libraries(example3 DR::mpi fmt::fmt)
29+
30+
add_executable(example6 example6.cpp)
31+
32+
target_compile_definitions(example6 INTERFACE DR_FORMAT)
33+
target_link_libraries(example6 DR::mpi fmt::fmt)

src/example6.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)