Skip to content

Commit 359c4a1

Browse files
committed
Change example for sparse matrix usage
1 parent 9d8e567 commit 359c4a1

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

resources/example.mtx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
%%MatrixMarket matrix coordinate real general
2+
25 25 49
3+
1 1 1.000e+00
4+
2 2 2.000e+00
5+
3 3 3.000e+00
6+
4 4 4.000e+00
7+
5 5 5.000e+00
8+
6 6 6.000e+00
9+
7 7 7.000e+00
10+
8 8 8.000e+00
11+
9 9 9.000e+00
12+
10 10 1.000e+01
13+
11 11 2.000e+01
14+
12 12 3.000e+01
15+
13 13 4.000e+01
16+
14 14 5.000e+01
17+
15 15 6.000e+01
18+
16 16 7.000e+01
19+
17 17 8.000e+01
20+
18 18 8.000e+01
21+
19 19 9.000e+01
22+
20 20 1.000e+02
23+
21 21 2.000e+02
24+
22 22 2.000e+02
25+
23 23 3.000e+02
26+
24 24 4.000e+02
27+
25 25 5.000e+02
28+
1 2 1.000e+00
29+
2 3 2.000e+00
30+
3 4 3.000e+00
31+
4 5 4.000e+00
32+
5 6 5.000e+00
33+
6 7 6.000e+00
34+
7 8 7.000e+00
35+
8 9 8.000e+00
36+
9 10 9.000e+00
37+
10 11 1.000e+01
38+
11 12 2.000e+01
39+
12 13 3.000e+01
40+
13 14 4.000e+01
41+
14 15 5.000e+01
42+
15 16 6.000e+01
43+
16 17 7.000e+01
44+
17 18 8.000e+01
45+
18 19 9.000e+01
46+
19 20 1.000e+01
47+
20 21 2.000e+01
48+
21 22 3.000e+01
49+
22 23 4.000e+01
50+
23 24 5.000e+01
51+
24 25 6.000e+01

scripts/build_run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ mpirun -n 3 ./build/src/example4
1717
mpirun -n 3 ./build/src/example5
1818
mpirun -n 3 ./build/src/example6
1919
mpirun -n 3 ./build/src/example7
20+
mpirun -n 3 ./build/src/example8

src/CMakeLists.txt

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

4747
target_compile_definitions(example7 INTERFACE DR_FORMAT)
4848
target_link_libraries(example7 DR::mpi fmt::fmt)
49+
50+
add_executable(example8 example8.cpp)
51+
52+
target_compile_definitions(example8 INTERFACE DR_FORMAT)
53+
target_link_libraries(example8 DR::mpi fmt::fmt)

src/example7.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ int main() {
1010
dr::mp::init(sycl::default_selector_v);
1111
using I = long;
1212
using V = double;
13+
1314
dr::views::csr_matrix_view<V, I> local_data;
1415
auto root = 0;
15-
auto n = 10;
16-
auto up = 1; // number of diagonals above main diagonal
17-
auto down = up; // number of diagonals below main diagonal
1816
if (root == dr::mp::rank()) {
19-
local_data = dr::generate_band_csr<V, I>(n, up, down);
17+
auto source = "./resources/example.mtx";
18+
local_data = dr::read_csr<double, long>(source);
2019
}
2120

2221
dr::mp::distributed_sparse_matrix<
@@ -38,7 +37,7 @@ int main() {
3837
gemv(root, res, matrix, broadcasted_b);
3938

4039
if (root == dr::mp::rank()) {
41-
fmt::print("Band matrix {} x {} with bandwitch {}\n", n, n, up * 2);
40+
fmt::print("Matrix imported from {}\n", "./resources/example.mtx");
4241
fmt::print("Input: ");
4342
for (auto x : b) {
4443
fmt::print("{} ", x);

src/example8.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: Intel Corporation
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
#include <dr/mp.hpp>
6+
#include <fmt/core.h>
7+
#include <random>
8+
9+
/* Sparse band matrix vector multiplication */
10+
int main() {
11+
dr::mp::init(sycl::default_selector_v);
12+
using I = long;
13+
using V = double;
14+
dr::views::csr_matrix_view<V, I> local_data;
15+
auto root = 0;
16+
if (root == dr::mp::rank()) {
17+
auto size = 10;
18+
auto nnz = 20;
19+
auto colInd = new I[nnz];
20+
auto rowInd = new I[size + 1];
21+
auto values = new V[nnz];
22+
std::uniform_real_distribution<double> unif(0, 1);
23+
std::default_random_engine re;
24+
for (auto i = 0; i <= size; i++) {
25+
rowInd[i] = i * 2; // two elements per row
26+
}
27+
for (auto i = 0; i < nnz; i++) {
28+
colInd[i] = (i % 2) * (std::max(i / 2, 1)); // column on 0 and diagonal (with additional entry in first row)
29+
values[i] = unif(re);
30+
}
31+
32+
local_data = dr::views::csr_matrix_view<V, I>(values, rowInd, colInd, {size, size}, nnz, root);
33+
}
34+
35+
dr::mp::distributed_sparse_matrix<
36+
V, I, dr::mp::MpiBackend,
37+
dr::mp::csr_eq_distribution<V, I, dr::mp::MpiBackend>>
38+
matrix(local_data, root);
39+
40+
std::vector<double> b;
41+
b.reserve(matrix.shape().second);
42+
std::vector<double> res(matrix.shape().first);
43+
for (auto i = 0; i < matrix.shape().second; i++) {
44+
b.push_back(i);
45+
}
46+
47+
dr::mp::broadcasted_vector<double> broadcasted_b;
48+
broadcasted_b.broadcast_data(matrix.shape().second, 0, b,
49+
dr::mp::default_comm());
50+
51+
gemv(root, res, matrix, broadcasted_b);
52+
53+
if (root == dr::mp::rank()) {
54+
fmt::print("Matrix with {} x {} and number of non-zero entries equal to {} and entries:\n", matrix.shape().first, matrix.shape().second, matrix.size());
55+
for (auto [i, v]: matrix) {
56+
auto [n, m] = i;
57+
fmt::print("Matrix entry <{}, {}, {}>\n", n, m, v);
58+
}
59+
fmt::print("Input: ");
60+
for (auto x : b) {
61+
fmt::print("{} ", x);
62+
}
63+
fmt::print("\n");
64+
fmt::print("Matrix vector multiplication res: ");
65+
for (auto x : res) {
66+
fmt::print("{} ", x);
67+
}
68+
fmt::print("\n");
69+
}
70+
71+
dr::mp::finalize();
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)