Skip to content

Commit eff4d21

Browse files
authored
add example6 - find a 2d pattern in a distributed_mdarray (#9)
* add example6 - find a 2d pattern in a distributed_mdarray * add a new example to the workflow and the script * small review fixes removed ifdef, added a comment describing the example * remove an unnecessary submdspan
1 parent 717acdd commit eff4d21

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

.github/workflows/docker_workflow.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ jobs:
5252
mpirun -n 3 ./build/src/example3
5353
mpirun -n 3 ./build/src/example4
5454
mpirun -n 3 ./build/src/example5
55+
mpirun -n 3 ./build/src/example6

scripts/build_run.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export CC=icx
1010
cmake -B build
1111
cmake --build build -j
1212

13-
mpirun -n 2 ./build/src/example1
14-
mpirun -n 2 ./build/src/example2
15-
mpirun -n 2 ./build/src/example3
16-
mpirun -n 2 ./build/src/example4
17-
mpirun -n 2 ./build/src/example5
13+
mpirun -n 3 ./build/src/example1
14+
mpirun -n 3 ./build/src/example2
15+
mpirun -n 3 ./build/src/example3
16+
mpirun -n 3 ./build/src/example4
17+
mpirun -n 3 ./build/src/example5
18+
mpirun -n 3 ./build/src/example6

src/CMakeLists.txt

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

3737
target_compile_definitions(example5 INTERFACE DR_FORMAT)
3838
target_link_libraries(example5 DR::mpi fmt::fmt)
39+
40+
add_executable(example6 example6.cpp)
41+
42+
target_compile_definitions(example6 INTERFACE DR_FORMAT)
43+
target_link_libraries(example6 DR::mpi fmt::fmt)

src/example6.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
/* 2D pattern search in a distributed multidimensional (2D) array */
14+
int main() {
15+
mhp::init(sycl::default_selector_v);
16+
17+
std::size_t arr_size = 7;
18+
// keep in mind that if you change the pattern size, you have to also change
19+
// the pattern array
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+
int pattern[pattern_size][pattern_size] = {{1, 0}, {0, 1}};
36+
37+
auto mdspan_pattern_op = [pattern](auto &&v) {
38+
auto [a_submdspan, occurrences] = v;
39+
if (pattern[0][0] == a_submdspan(0, 0) &&
40+
pattern[0][1] == a_submdspan(0, 1) &&
41+
pattern[1][0] == a_submdspan(1, 0) &&
42+
pattern[1][1] == a_submdspan(1, 1)) {
43+
occurrences(0, 0) = 1;
44+
}
45+
};
46+
47+
mhp::halo(a).exchange();
48+
mhp::stencil_for_each(mdspan_pattern_op, a_submdspan, occurrences_coords);
49+
50+
if (mhp::rank() == 0) {
51+
fmt::print("a: \n{} \n", a.mdspan());
52+
fmt::print("pattern: \n{} \n", pattern);
53+
fmt::print("occurrences: \n{} \n", occurrences_coords.mdspan());
54+
}
55+
56+
mhp::finalize();
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)