diff --git a/README.md b/README.md index e176737..bdbaa7b 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,20 @@ The example shows the distributed nature of dr data structures. The distributed_ Additionally, a use of a subrange is presented, and `transform()` function, which puts transformed values of input structure to the output structure, element by element. The transforming function is given as lambda `newvalue`. _Please note: after each loop the vector content is printed with `fmt::print()`. The formatter function for `distributed_vector` is rather slow, as it gets the vector element by element, both from local node and remote nodes. You can think about customised, more effective way of results presentation._ - +### Example 4 + +[./src/example4.cpp](src/example4.cpp) + +This example illustrates adding two distributed,multidimensional arrays. Each array has two dimensions and is initialized by an `std::array`. The arrays are populated with sequential values using a distributed version of iota called `mhp::iota`. A for_each loop is the main part of the code, computing the sum on a specified number of nodes. It takes a lambda copy function along with two input arrays (a and b) and an output array (c) as parameters. The result is printed on a node 0. + +### Example 5 + +[./src/example5.cpp](src/example5.cpp) + +Example 5 outlines a method for calculating a 2D 5-point stencil with distributed multidimensional arrays, specifically utilizing `dr::mhp::distributed_mdarray`. Initially, it involves setting up key parameters like the radius for element exchange between nodes through `dr::mhp::halo`, and defining the start and end points of the array slice. The example's core is the `mhp::stencil_for_each` function, which applies a lambda function to two subsets of the array, designated as input and output. The `mdspan_stencil_op` lambda function conducts a simple calculation that involves adding together the values of an element and its adjacent elements and subsequently calculating their average. The `mhp::halo().exchange()` enables values to be shared across distinct nodes, making this process feasible. Ultimately, the outcomes of the calculation are neatly displayed on node 0 using mdspan(), resulting in a clear indication of the modifications made to the 2D array. This example is a practical demonstration of executing stencil operations on distributed arrays. + +### Example 6 + +[./src/example6.cpp](src/example6.cpp) + +This example's code demonstrates a 2D pattern search in a distributed, multidimensional array (`mhp::distributed_mdarray`). It initializes a 2D array, populates it with `mhp::iota`, converts it to binary values using `mhp::transform` and defines a pattern of 2x2. A lambda function is used to scan the array and mark occurrences of the pattern in a separate array. The process is similar to the one demonstrated in example5. diff --git a/src/example4.cpp b/src/example4.cpp index 94932ab..c3cfc73 100644 --- a/src/example4.cpp +++ b/src/example4.cpp @@ -24,11 +24,11 @@ int main(int argc, char **argv) { mhp::iota(a, 100); mhp::iota(b, 200); - auto copy_op = [](auto v) { + auto sum_op = [](auto v) { auto [in1, in2, out] = v; out = in1 + in2; }; - mhp::for_each(copy_op, a, b, c); + mhp::for_each(sum_op, a, b, c); if (mhp::rank() == 0) { fmt::print("A:\n{}\n", a.mdspan()); diff --git a/src/example5.cpp b/src/example5.cpp index 9e56938..b19d9b5 100644 --- a/src/example5.cpp +++ b/src/example5.cpp @@ -19,14 +19,14 @@ int main() { std::array slice_starts{radius, radius}; std::array slice_ends{arr_size - radius, arr_size - radius}; - auto dist = dr::mhp::distribution().halo(radius); + auto dist = 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 in = mhp::views::submdspan(a.view(), slice_starts, slice_ends); + auto out = mhp::views::submdspan(b.view(), slice_starts, slice_ends); auto mdspan_stencil_op = [](auto &&v) { auto [in, out] = v;