This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Add sparse matrix usage to example #13
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7e874d8
Add sparse matrix usage to example
9d8e567
Fix formatting
359c4a1
Change example for sparse matrix usage
c08620c
Fix formatting
0c75471
Add requested changes
e40f0c2
Fix formatting
6e2ad08
Modify README and release resources before ending program
4777d58
Fix spelling
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // SPDX-FileCopyrightText: Intel Corporation | ||
| // | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| #include <dr/mp.hpp> | ||
| #include <fmt/core.h> | ||
| #include <ranges> | ||
|
|
||
| /* 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<V, I> 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<double, long>(source); | ||
| } | ||
|
|
||
| dr::mp::distributed_sparse_matrix< | ||
| V, I, dr::mp::MpiBackend, | ||
| dr::mp::csr_eq_distribution<V, I, dr::mp::MpiBackend>> | ||
| matrix(local_data, root); | ||
|
|
||
| dr::mp::broadcasted_vector<double> broadcasted_b; | ||
| std::vector<double> b; | ||
| 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<V>(), | ||
| dr::mp::default_comm()); | ||
| } | ||
| std::vector<double> res(matrix.shape().first); | ||
| gemv(root, res, matrix, broadcasted_b); | ||
|
|
||
| if (root == dr::mp::rank()) { | ||
| fmt::print("Matrix imported from {}\n", "./resources/example.mtx"); | ||
| 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"); | ||
| } | ||
|
|
||
| if (root == dr::mp::default_comm().rank()) { | ||
| dr::__detail::destroy_csr_matrix_view(local_data, std::allocator<V>{}); | ||
| } | ||
|
|
||
| dr::mp::finalize(); | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // SPDX-FileCopyrightText: Intel Corporation | ||
| // | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| #include <dr/mp.hpp> | ||
| #include <fmt/core.h> | ||
| #include <random> | ||
| #include <ranges> | ||
|
|
||
| /* 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<V, I> 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<double> 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 | ||
| } | ||
| for (auto i = 0; i < nnz; i++) { | ||
| colInd[i] = | ||
| (i % 2) * (std::max(i / 2, 1)); // column on 0 and diagonal (with | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we draw that matrix in a comment? |
||
| // additional entry in first row) | ||
| values[i] = unif(re); | ||
| } | ||
|
|
||
| local_data = dr::views::csr_matrix_view<V, I>(values, rowInd, colInd, | ||
| {size, size}, nnz, root); | ||
| } | ||
|
|
||
| dr::mp::distributed_sparse_matrix< | ||
| V, I, dr::mp::MpiBackend, | ||
| dr::mp::csr_eq_distribution<V, I, dr::mp::MpiBackend>> | ||
| matrix(local_data, root); | ||
|
|
||
| dr::mp::broadcasted_vector<double> broadcasted_b; | ||
| std::vector<double> b; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments an in the first example in this code |
||
| 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<V>(), | ||
| dr::mp::default_comm()); | ||
| } | ||
|
|
||
| std::vector<double> res(matrix.shape().first); | ||
| 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"); | ||
| } | ||
|
|
||
| if (root == dr::mp::default_comm().rank()) { | ||
| dr::__detail::destroy_csr_matrix_view(local_data, std::allocator<double>{}); | ||
| } | ||
| dr::mp::finalize(); | ||
|
|
||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In lines 26-31 all ranks already computed their vector B
What is the step with "broadcasted_vector" for?