From 7e874d8cd6f4c1d0b0ed00722f45c274fc13c98f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Fri, 17 Jan 2025 13:34:27 +0000 Subject: [PATCH 1/8] Add sparse matrix usage to example --- scripts/build_run.sh | 1 + src/CMakeLists.txt | 5 ++++ src/example7.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/example7.cpp diff --git a/scripts/build_run.sh b/scripts/build_run.sh index f5c0099..65400c9 100755 --- a/scripts/build_run.sh +++ b/scripts/build_run.sh @@ -16,3 +16,4 @@ mpirun -n 3 ./build/src/example3 mpirun -n 3 ./build/src/example4 mpirun -n 3 ./build/src/example5 mpirun -n 3 ./build/src/example6 +mpirun -n 3 ./build/src/example7 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 11b3150..7e03f09 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,3 +41,8 @@ add_executable(example6 example6.cpp) target_compile_definitions(example6 INTERFACE DR_FORMAT) target_link_libraries(example6 DR::mpi fmt::fmt) + +add_executable(example7 example7.cpp) + +target_compile_definitions(example7 INTERFACE DR_FORMAT) +target_link_libraries(example7 DR::mpi fmt::fmt) diff --git a/src/example7.cpp b/src/example7.cpp new file mode 100644 index 0000000..3b6b43e --- /dev/null +++ b/src/example7.cpp @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: Intel Corporation +// +// SPDX-License-Identifier: BSD-3-Clause + +#include +#include + + +/* Sparse band matrix vector multiplication */ +int main() { + dr::mp::init(sycl::default_selector_v); + using I = long; + using V = double; + dr::views::csr_matrix_view local_data; + auto root = 0; + auto n = 10; + auto up = 1; // number of diagonals above main diagonal + auto down = up; // number of diagonals below main diagonal + if (root == dr::mp::rank()) { + local_data = dr::generate_band_csr(n, up, down); + } + + dr::mp::distributed_sparse_matrix< + V, I, dr::mp::MpiBackend, + dr::mp::csr_eq_distribution> + matrix(local_data, root); + + std::vector b; + b.reserve(matrix.shape().second); + std::vector res(matrix.shape().first); + for (auto i = 0; i < matrix.shape().second; i++) { + b.push_back(i); + } + + dr::mp::broadcasted_vector broadcasted_b; + broadcasted_b.broadcast_data(matrix.shape().second, 0, b, + dr::mp::default_comm()); + + gemv(root, res, matrix, broadcasted_b); + + if (root == dr::mp::rank()) { + fmt::print("Band matrix {} x {} with bandwitch {}\n", n, n, up * 2); + fmt::print("Input: "); + for (auto x: b) { + fmt::print("{} ", x); + } + fmt::print("\n"); + fmt::print("Matrix vector multiplication res: "); + for (auto x: res) { + fmt::print("{} ", x); + } + fmt::print("\n"); + + } + + dr::mp::finalize(); + + return 0; +} From 9d8e5679d9f936d1050022110b41465c4be146ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Fri, 17 Jan 2025 13:46:20 +0000 Subject: [PATCH 2/8] Fix formatting --- src/example7.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/example7.cpp b/src/example7.cpp index 3b6b43e..7330319 100644 --- a/src/example7.cpp +++ b/src/example7.cpp @@ -5,7 +5,6 @@ #include #include - /* Sparse band matrix vector multiplication */ int main() { dr::mp::init(sycl::default_selector_v); @@ -14,10 +13,10 @@ int main() { dr::views::csr_matrix_view local_data; auto root = 0; auto n = 10; - auto up = 1; // number of diagonals above main diagonal + auto up = 1; // number of diagonals above main diagonal auto down = up; // number of diagonals below main diagonal if (root == dr::mp::rank()) { - local_data = dr::generate_band_csr(n, up, down); + local_data = dr::generate_band_csr(n, up, down); } dr::mp::distributed_sparse_matrix< @@ -34,23 +33,22 @@ int main() { dr::mp::broadcasted_vector broadcasted_b; broadcasted_b.broadcast_data(matrix.shape().second, 0, b, - dr::mp::default_comm()); + dr::mp::default_comm()); gemv(root, res, matrix, broadcasted_b); if (root == dr::mp::rank()) { fmt::print("Band matrix {} x {} with bandwitch {}\n", n, n, up * 2); fmt::print("Input: "); - for (auto x: b) { - fmt::print("{} ", x); + for (auto x : b) { + fmt::print("{} ", x); } fmt::print("\n"); fmt::print("Matrix vector multiplication res: "); - for (auto x: res) { - fmt::print("{} ", x); + for (auto x : res) { + fmt::print("{} ", x); } fmt::print("\n"); - } dr::mp::finalize(); From 359c4a1dfa9cc2504b9876c9af2a0b0b02707977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Mon, 20 Jan 2025 09:00:30 +0000 Subject: [PATCH 3/8] Change example for sparse matrix usage --- resources/example.mtx | 51 +++++++++++++++++++++++++++++ scripts/build_run.sh | 1 + src/CMakeLists.txt | 5 +++ src/example7.cpp | 9 +++--- src/example8.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 resources/example.mtx create mode 100644 src/example8.cpp diff --git a/resources/example.mtx b/resources/example.mtx new file mode 100644 index 0000000..428295f --- /dev/null +++ b/resources/example.mtx @@ -0,0 +1,51 @@ +%%MatrixMarket matrix coordinate real general +25 25 49 +1 1 1.000e+00 +2 2 2.000e+00 +3 3 3.000e+00 +4 4 4.000e+00 +5 5 5.000e+00 +6 6 6.000e+00 +7 7 7.000e+00 +8 8 8.000e+00 +9 9 9.000e+00 +10 10 1.000e+01 +11 11 2.000e+01 +12 12 3.000e+01 +13 13 4.000e+01 +14 14 5.000e+01 +15 15 6.000e+01 +16 16 7.000e+01 +17 17 8.000e+01 +18 18 8.000e+01 +19 19 9.000e+01 +20 20 1.000e+02 +21 21 2.000e+02 +22 22 2.000e+02 +23 23 3.000e+02 +24 24 4.000e+02 +25 25 5.000e+02 +1 2 1.000e+00 +2 3 2.000e+00 +3 4 3.000e+00 +4 5 4.000e+00 +5 6 5.000e+00 +6 7 6.000e+00 +7 8 7.000e+00 +8 9 8.000e+00 +9 10 9.000e+00 +10 11 1.000e+01 +11 12 2.000e+01 +12 13 3.000e+01 +13 14 4.000e+01 +14 15 5.000e+01 +15 16 6.000e+01 +16 17 7.000e+01 +17 18 8.000e+01 +18 19 9.000e+01 +19 20 1.000e+01 +20 21 2.000e+01 +21 22 3.000e+01 +22 23 4.000e+01 +23 24 5.000e+01 +24 25 6.000e+01 diff --git a/scripts/build_run.sh b/scripts/build_run.sh index 65400c9..ae8895e 100755 --- a/scripts/build_run.sh +++ b/scripts/build_run.sh @@ -17,3 +17,4 @@ mpirun -n 3 ./build/src/example4 mpirun -n 3 ./build/src/example5 mpirun -n 3 ./build/src/example6 mpirun -n 3 ./build/src/example7 +mpirun -n 3 ./build/src/example8 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e03f09..fcda0fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,3 +46,8 @@ add_executable(example7 example7.cpp) target_compile_definitions(example7 INTERFACE DR_FORMAT) target_link_libraries(example7 DR::mpi fmt::fmt) + +add_executable(example8 example8.cpp) + +target_compile_definitions(example8 INTERFACE DR_FORMAT) +target_link_libraries(example8 DR::mpi fmt::fmt) diff --git a/src/example7.cpp b/src/example7.cpp index 7330319..5a2fbb1 100644 --- a/src/example7.cpp +++ b/src/example7.cpp @@ -10,13 +10,12 @@ int main() { dr::mp::init(sycl::default_selector_v); using I = long; using V = double; + dr::views::csr_matrix_view local_data; auto root = 0; - auto n = 10; - auto up = 1; // number of diagonals above main diagonal - auto down = up; // number of diagonals below main diagonal if (root == dr::mp::rank()) { - local_data = dr::generate_band_csr(n, up, down); + auto source = "./resources/example.mtx"; + local_data = dr::read_csr(source); } dr::mp::distributed_sparse_matrix< @@ -38,7 +37,7 @@ int main() { gemv(root, res, matrix, broadcasted_b); if (root == dr::mp::rank()) { - fmt::print("Band matrix {} x {} with bandwitch {}\n", n, n, up * 2); + fmt::print("Matrix imported from {}\n", "./resources/example.mtx"); fmt::print("Input: "); for (auto x : b) { fmt::print("{} ", x); diff --git a/src/example8.cpp b/src/example8.cpp new file mode 100644 index 0000000..391cdaa --- /dev/null +++ b/src/example8.cpp @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: Intel Corporation +// +// SPDX-License-Identifier: BSD-3-Clause + +#include +#include +#include + +/* Sparse band matrix vector multiplication */ +int main() { + dr::mp::init(sycl::default_selector_v); + using I = long; + using V = double; + dr::views::csr_matrix_view local_data; + auto root = 0; + if (root == dr::mp::rank()) { + auto size = 10; + auto nnz = 20; + auto colInd = new I[nnz]; + auto rowInd = new I[size + 1]; + auto values = new V[nnz]; + std::uniform_real_distribution unif(0, 1); + std::default_random_engine re; + for (auto i = 0; i <= size; i++) { + rowInd[i] = i * 2; // two elements per row + } + for (auto i = 0; i < nnz; i++) { + colInd[i] = (i % 2) * (std::max(i / 2, 1)); // column on 0 and diagonal (with additional entry in first row) + values[i] = unif(re); + } + + local_data = dr::views::csr_matrix_view(values, rowInd, colInd, {size, size}, nnz, root); + } + + dr::mp::distributed_sparse_matrix< + V, I, dr::mp::MpiBackend, + dr::mp::csr_eq_distribution> + matrix(local_data, root); + + std::vector b; + b.reserve(matrix.shape().second); + std::vector res(matrix.shape().first); + for (auto i = 0; i < matrix.shape().second; i++) { + b.push_back(i); + } + + dr::mp::broadcasted_vector broadcasted_b; + broadcasted_b.broadcast_data(matrix.shape().second, 0, b, + dr::mp::default_comm()); + + gemv(root, res, matrix, broadcasted_b); + + if (root == dr::mp::rank()) { + fmt::print("Matrix with {} x {} and number of non-zero entries equal to {} and entries:\n", matrix.shape().first, matrix.shape().second, matrix.size()); + for (auto [i, v]: matrix) { + auto [n, m] = i; + fmt::print("Matrix entry <{}, {}, {}>\n", n, m, v); + } + fmt::print("Input: "); + for (auto x : b) { + fmt::print("{} ", x); + } + fmt::print("\n"); + fmt::print("Matrix vector multiplication res: "); + for (auto x : res) { + fmt::print("{} ", x); + } + fmt::print("\n"); + } + + dr::mp::finalize(); + + return 0; +} From c08620c093739d07ecf81a7ec43ffc0275315d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Mon, 20 Jan 2025 09:01:02 +0000 Subject: [PATCH 4/8] Fix formatting --- src/example8.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/example8.cpp b/src/example8.cpp index 391cdaa..b40b280 100644 --- a/src/example8.cpp +++ b/src/example8.cpp @@ -25,11 +25,14 @@ int main() { rowInd[i] = i * 2; // two elements per row } for (auto i = 0; i < nnz; i++) { - colInd[i] = (i % 2) * (std::max(i / 2, 1)); // column on 0 and diagonal (with additional entry in first row) + colInd[i] = + (i % 2) * (std::max(i / 2, 1)); // column on 0 and diagonal (with + // additional entry in first row) values[i] = unif(re); } - local_data = dr::views::csr_matrix_view(values, rowInd, colInd, {size, size}, nnz, root); + local_data = dr::views::csr_matrix_view(values, rowInd, colInd, + {size, size}, nnz, root); } dr::mp::distributed_sparse_matrix< @@ -51,8 +54,10 @@ int main() { gemv(root, res, matrix, broadcasted_b); if (root == dr::mp::rank()) { - fmt::print("Matrix with {} x {} and number of non-zero entries equal to {} and entries:\n", matrix.shape().first, matrix.shape().second, matrix.size()); - for (auto [i, v]: matrix) { + fmt::print("Matrix with {} x {} and number of non-zero entries equal to {} " + "and entries:\n", + matrix.shape().first, matrix.shape().second, matrix.size()); + for (auto [i, v] : matrix) { auto [n, m] = i; fmt::print("Matrix entry <{}, {}, {}>\n", n, m, v); } From 0c75471c186ea51665ff7bc9f12f54c0d85c1b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Mon, 20 Jan 2025 10:06:48 +0000 Subject: [PATCH 5/8] Add requested changes --- src/example7.cpp | 22 +++++++++++++++------- src/example8.cpp | 23 +++++++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/example7.cpp b/src/example7.cpp index 5a2fbb1..885c383 100644 --- a/src/example7.cpp +++ b/src/example7.cpp @@ -4,6 +4,7 @@ #include #include +#include /* Sparse band matrix vector multiplication */ int main() { @@ -14,6 +15,10 @@ int main() { dr::views::csr_matrix_view local_data; auto root = 0; if (root == dr::mp::rank()) { + // x x 0 0 ... 0 + // 0 x x 0 ... 0 + // ............. + // 0 ... 0 0 x x auto source = "./resources/example.mtx"; local_data = dr::read_csr(source); } @@ -23,17 +28,20 @@ int main() { dr::mp::csr_eq_distribution> matrix(local_data, root); + dr::mp::broadcasted_vector broadcasted_b; std::vector b; - b.reserve(matrix.shape().second); - std::vector res(matrix.shape().first); - for (auto i = 0; i < matrix.shape().second; i++) { - b.push_back(i); - } + if (root == dr::mp::rank()) { + b.resize(matrix.shape().second); + std::iota(b.begin(), b.end(), 1); - dr::mp::broadcasted_vector broadcasted_b; broadcasted_b.broadcast_data(matrix.shape().second, 0, b, dr::mp::default_comm()); - + } + else { + broadcasted_b.broadcast_data(matrix.shape().second, 0, std::ranges::empty_view(), + dr::mp::default_comm()); + } + std::vector res(matrix.shape().first); gemv(root, res, matrix, broadcasted_b); if (root == dr::mp::rank()) { diff --git a/src/example8.cpp b/src/example8.cpp index b40b280..f66ab80 100644 --- a/src/example8.cpp +++ b/src/example8.cpp @@ -5,6 +5,7 @@ #include #include #include +#include /* Sparse band matrix vector multiplication */ int main() { @@ -21,6 +22,12 @@ int main() { auto values = new V[nnz]; std::uniform_real_distribution unif(0, 1); std::default_random_engine re; + // x x 0 0 ... 0 + // x x 0 0 ... 0 + // x 0 x 0 ... 0 + // x 0 0 x ... 0 + // ............. + // x ... 0 0 0 x for (auto i = 0; i <= size; i++) { rowInd[i] = i * 2; // two elements per row } @@ -40,17 +47,21 @@ int main() { dr::mp::csr_eq_distribution> matrix(local_data, root); + dr::mp::broadcasted_vector broadcasted_b; std::vector b; - b.reserve(matrix.shape().second); - std::vector res(matrix.shape().first); - for (auto i = 0; i < matrix.shape().second; i++) { - b.push_back(i); - } + if (root == dr::mp::rank()) { + b.resize(matrix.shape().second); + std::iota(b.begin(), b.end(), 1); - dr::mp::broadcasted_vector broadcasted_b; broadcasted_b.broadcast_data(matrix.shape().second, 0, b, dr::mp::default_comm()); + } + else { + broadcasted_b.broadcast_data(matrix.shape().second, 0, std::ranges::empty_view(), + dr::mp::default_comm()); + } + std::vector res(matrix.shape().first); gemv(root, res, matrix, broadcasted_b); if (root == dr::mp::rank()) { From e40f0c292f57c41b34b265dcaa08eb0b06a4bc70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Mon, 20 Jan 2025 10:07:21 +0000 Subject: [PATCH 6/8] Fix formatting --- src/example7.cpp | 16 ++++++++-------- src/example8.cpp | 18 +++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/example7.cpp b/src/example7.cpp index 885c383..80e9b71 100644 --- a/src/example7.cpp +++ b/src/example7.cpp @@ -31,15 +31,15 @@ int main() { dr::mp::broadcasted_vector broadcasted_b; std::vector b; if (root == dr::mp::rank()) { - b.resize(matrix.shape().second); - std::iota(b.begin(), b.end(), 1); + b.resize(matrix.shape().second); + std::iota(b.begin(), b.end(), 1); - broadcasted_b.broadcast_data(matrix.shape().second, 0, b, - dr::mp::default_comm()); - } - else { - broadcasted_b.broadcast_data(matrix.shape().second, 0, std::ranges::empty_view(), - dr::mp::default_comm()); + broadcasted_b.broadcast_data(matrix.shape().second, 0, b, + dr::mp::default_comm()); + } else { + broadcasted_b.broadcast_data(matrix.shape().second, 0, + std::ranges::empty_view(), + dr::mp::default_comm()); } std::vector res(matrix.shape().first); gemv(root, res, matrix, broadcasted_b); diff --git a/src/example8.cpp b/src/example8.cpp index f66ab80..b63a0f9 100644 --- a/src/example8.cpp +++ b/src/example8.cpp @@ -49,16 +49,16 @@ int main() { dr::mp::broadcasted_vector broadcasted_b; std::vector b; - if (root == dr::mp::rank()) { - b.resize(matrix.shape().second); - std::iota(b.begin(), b.end(), 1); + if (root == dr::mp::rank()) { + b.resize(matrix.shape().second); + std::iota(b.begin(), b.end(), 1); - broadcasted_b.broadcast_data(matrix.shape().second, 0, b, - dr::mp::default_comm()); - } - else { - broadcasted_b.broadcast_data(matrix.shape().second, 0, std::ranges::empty_view(), - dr::mp::default_comm()); + broadcasted_b.broadcast_data(matrix.shape().second, 0, b, + dr::mp::default_comm()); + } else { + broadcasted_b.broadcast_data(matrix.shape().second, 0, + std::ranges::empty_view(), + dr::mp::default_comm()); } std::vector res(matrix.shape().first); From 6e2ad086d41d8857dbc2755a38a39f4b8c4d7436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Mon, 20 Jan 2025 11:24:41 +0000 Subject: [PATCH 7/8] Modify README and release resources before ending program --- README.md | 28 ++++++++++++++++++++-------- src/example7.cpp | 4 ++++ src/example8.cpp | 3 +++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 40d5b90..03ed7f5 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ The distributed-ranges library provides data-structures, algorithms and views de Algorithms and data structures are designed to take the user off the need to worry about the technical details of their parallelism. An example would be the definition of a distributed vector in memory of multiple nodes connected using MPI. ```cpp -dr::mhp::distributed_vector dv(N); +dr::mp::distributed_vector dv(N); ``` Such a vector, containing N elements, is automatically distributed among all the nodes involved in the calculation, with individual nodes storing an equal (if possible) amount of data. @@ -82,12 +82,12 @@ In this way, many of the technical details related to the parallel execution of ### Namespaces General namespace used in the library is `dr::` -For program using a single node with shared memory available for multiple CPUs and one or more GPUs, data structures and algorithms from `dr::shp::` namespace are provided. -For distributed memory model, use the `dr::mhp::` namespace. +For program using a single node with shared memory available for multiple CPUs and one or more GPUs, data structures and algorithms from `dr::sp::` namespace are provided. +For distributed memory model, use the `dr::mp::` namespace. ### Data structures -Content of distributes-ranges' data structures is distributed over available nodes. For example, segments of `dr::mhp::distributed_vector` are located in memory of different nodes (mpi processes). Still, global view of the `distributed_vector` is uniform, with contiguous indices. +Content of distributes-ranges' data structures is distributed over available nodes. For example, segments of `dr::mp::distributed_vector` are located in memory of different nodes (mpi processes). Still, global view of the `distributed_vector` is uniform, with contiguous indices. #### Halo concept @@ -98,7 +98,7 @@ To support this situation, the concept of halo was introduced. A halo is an area ### Algorithms -Following algorithms are included in distributed-ranges, both in mhp and shp versions: +Following algorithms are included in distributed-ranges, both in mp and sp versions: ```cpp copy() @@ -151,16 +151,28 @@ The example shows the distributed nature of dr data structures. The distributed_ [./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. +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 `mp::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 5 outlines a method for calculating a 2D 5-point stencil with distributed multidimensional arrays, specifically utilizing `dr::mp::distributed_mdarray`. Initially, it involves setting up key parameters like the radius for element exchange between nodes through `dr::mp::halo`, and defining the start and end points of the array slice. The example's core is the `mp::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 `mp::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. +This example's code demonstrates a 2D pattern search in a distributed, multidimensional array (`mp::distributed_mdarray`). It initializes a 2D array, populates it with `mp::iota`, converts it to binary values using `mp::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. + +### Example 7 + +[./src/example7.cpp](src/example7.cpp) + +This example showcases usage of `mp::distributed_sparse_matrix`. It retrieves data from `resources/example.mtx` file in root node, and distributes it between all nodes. The root node initializes vector and broadcasts it to every other node. After that, the `mp::gemv` operation is performed and result is returned to `std::vector` in the root. Finally, the root prints the multiplied vector and the result. + +### Example 8 + +[./src/example8.cpp](src/example8.cpp) + +The example 8 is exactly the same as example 7, the only thing that is different is the initialization of the matrix data. Here the matrix is generated inside the code, has different shape and uses random values. Additionaly, we print matrix data together with vector and result. \ No newline at end of file diff --git a/src/example7.cpp b/src/example7.cpp index 80e9b71..257fa75 100644 --- a/src/example7.cpp +++ b/src/example7.cpp @@ -58,6 +58,10 @@ int main() { fmt::print("\n"); } + if (root == dr::mp::default_comm().rank()) { + dr::__detail::destroy_csr_matrix_view(local_data, std::allocator{}); + } + dr::mp::finalize(); return 0; diff --git a/src/example8.cpp b/src/example8.cpp index b63a0f9..fb4ef14 100644 --- a/src/example8.cpp +++ b/src/example8.cpp @@ -84,6 +84,9 @@ int main() { fmt::print("\n"); } + if (root == dr::mp::default_comm().rank()) { + dr::__detail::destroy_csr_matrix_view(local_data, std::allocator{}); + } dr::mp::finalize(); return 0; From 4777d58fc996e5e8ebe859cd045213e37cb29dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Komar?= Date: Mon, 20 Jan 2025 11:25:31 +0000 Subject: [PATCH 8/8] Fix spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03ed7f5..432d49a 100644 --- a/README.md +++ b/README.md @@ -175,4 +175,4 @@ This example showcases usage of `mp::distributed_sparse_matrix`. It retrieves da [./src/example8.cpp](src/example8.cpp) -The example 8 is exactly the same as example 7, the only thing that is different is the initialization of the matrix data. Here the matrix is generated inside the code, has different shape and uses random values. Additionaly, we print matrix data together with vector and result. \ No newline at end of file +The example 8 is exactly the same as example 7, the only thing that is different is the initialization of the matrix data. Here the matrix is generated inside the code, has different shape and uses random values. Additionally, we print matrix data together with vector and result.