Skip to content

Commit

Permalink
add example5 - simple stencil2d (#6)
Browse files Browse the repository at this point in the history
* add example5 - simple stencil2d

* review fixes

change to a floating point number
remove unused variables
  • Loading branch information
haichangsi authored Nov 13, 2023
1 parent 2a0c52d commit 633ad92
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/docker_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
cmake --build build -j
- name: Run examples
run: |
mpirun -n 2 ./build/src/example1
mpirun -n 2 ./build/src/example2
mpirun -n 2 ./build/src/example3
mpirun -n 3 ./build/src/example1
mpirun -n 3 ./build/src/example2
mpirun -n 3 ./build/src/example3
mpirun -n 3 ./build/src/example4
mpirun -n 3 ./build/src/example5
2 changes: 2 additions & 0 deletions scripts/build_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ cmake --build build -j
mpirun -n 2 ./build/src/example1
mpirun -n 2 ./build/src/example2
mpirun -n 2 ./build/src/example3
mpirun -n 2 ./build/src/example4
mpirun -n 2 ./build/src/example5
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ add_executable(example4 example4.cpp)

target_compile_definitions(example4 INTERFACE DR_FORMAT)
target_link_libraries(example4 DR::mpi fmt::fmt)

add_executable(example5 example5.cpp)

target_compile_definitions(example5 INTERFACE DR_FORMAT)
target_link_libraries(example5 DR::mpi fmt::fmt)
49 changes: 49 additions & 0 deletions src/example5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: Intel Corporation
//
// SPDX-License-Identifier: BSD-3-Clause

#include <dr/mhp.hpp>
#include <fmt/core.h>

namespace mhp = dr::mhp;

using T = float;
using MDA = dr::mhp::distributed_mdarray<T, 2>;

/* 2d stencil - simple operation on multi-dimensional array */
int main() {
mhp::init(sycl::default_selector_v);

std::size_t arr_size = 4;
std::size_t radius = 1;
std::array slice_starts{radius, radius};
std::array slice_ends{arr_size - radius, arr_size - radius};

auto dist = dr::mhp::distribution().halo(radius);
MDA a({arr_size, arr_size}, dist);
MDA b({arr_size, arr_size}, dist);
mhp::iota(a, 1);
mhp::iota(b, 1);

auto in = dr::mhp::views::submdspan(a.view(), slice_starts, slice_ends);
auto out = dr::mhp::views::submdspan(b.view(), slice_starts, slice_ends);

auto mdspan_stencil_op = [](auto &&v) {
auto [in, out] = v;
out(0, 0) = (in(-1, 0) + in(0, -1) + in(0, 0) + in(0, 1) + in(1, 0)) / 4;
};

mhp::halo(a).exchange();
mhp::stencil_for_each(mdspan_stencil_op, in, out);

if (mhp::rank() == 0) {
fmt::print("a: \n{} \n", a.mdspan());
fmt::print("b: \n{} \n", b.mdspan());
fmt::print("in: \n{} \n", in.mdspan());
fmt::print("out: \n{} \n", out.mdspan());
}

mhp::finalize();

return 0;
}

0 comments on commit 633ad92

Please sign in to comment.