Skip to content

Commit 0c75471

Browse files
committed
Add requested changes
1 parent c08620c commit 0c75471

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/example7.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <dr/mp.hpp>
66
#include <fmt/core.h>
7+
#include <ranges>
78

89
/* Sparse band matrix vector multiplication */
910
int main() {
@@ -14,6 +15,10 @@ int main() {
1415
dr::views::csr_matrix_view<V, I> local_data;
1516
auto root = 0;
1617
if (root == dr::mp::rank()) {
18+
// x x 0 0 ... 0
19+
// 0 x x 0 ... 0
20+
// .............
21+
// 0 ... 0 0 x x
1722
auto source = "./resources/example.mtx";
1823
local_data = dr::read_csr<double, long>(source);
1924
}
@@ -23,17 +28,20 @@ int main() {
2328
dr::mp::csr_eq_distribution<V, I, dr::mp::MpiBackend>>
2429
matrix(local_data, root);
2530

31+
dr::mp::broadcasted_vector<double> broadcasted_b;
2632
std::vector<double> b;
27-
b.reserve(matrix.shape().second);
28-
std::vector<double> res(matrix.shape().first);
29-
for (auto i = 0; i < matrix.shape().second; i++) {
30-
b.push_back(i);
31-
}
33+
if (root == dr::mp::rank()) {
34+
b.resize(matrix.shape().second);
35+
std::iota(b.begin(), b.end(), 1);
3236

33-
dr::mp::broadcasted_vector<double> broadcasted_b;
3437
broadcasted_b.broadcast_data(matrix.shape().second, 0, b,
3538
dr::mp::default_comm());
36-
39+
}
40+
else {
41+
broadcasted_b.broadcast_data(matrix.shape().second, 0, std::ranges::empty_view<V>(),
42+
dr::mp::default_comm());
43+
}
44+
std::vector<double> res(matrix.shape().first);
3745
gemv(root, res, matrix, broadcasted_b);
3846

3947
if (root == dr::mp::rank()) {

src/example8.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <dr/mp.hpp>
66
#include <fmt/core.h>
77
#include <random>
8+
#include <ranges>
89

910
/* Sparse band matrix vector multiplication */
1011
int main() {
@@ -21,6 +22,12 @@ int main() {
2122
auto values = new V[nnz];
2223
std::uniform_real_distribution<double> unif(0, 1);
2324
std::default_random_engine re;
25+
// x x 0 0 ... 0
26+
// x x 0 0 ... 0
27+
// x 0 x 0 ... 0
28+
// x 0 0 x ... 0
29+
// .............
30+
// x ... 0 0 0 x
2431
for (auto i = 0; i <= size; i++) {
2532
rowInd[i] = i * 2; // two elements per row
2633
}
@@ -40,17 +47,21 @@ int main() {
4047
dr::mp::csr_eq_distribution<V, I, dr::mp::MpiBackend>>
4148
matrix(local_data, root);
4249

50+
dr::mp::broadcasted_vector<double> broadcasted_b;
4351
std::vector<double> b;
44-
b.reserve(matrix.shape().second);
45-
std::vector<double> res(matrix.shape().first);
46-
for (auto i = 0; i < matrix.shape().second; i++) {
47-
b.push_back(i);
48-
}
52+
if (root == dr::mp::rank()) {
53+
b.resize(matrix.shape().second);
54+
std::iota(b.begin(), b.end(), 1);
4955

50-
dr::mp::broadcasted_vector<double> broadcasted_b;
5156
broadcasted_b.broadcast_data(matrix.shape().second, 0, b,
5257
dr::mp::default_comm());
58+
}
59+
else {
60+
broadcasted_b.broadcast_data(matrix.shape().second, 0, std::ranges::empty_view<V>(),
61+
dr::mp::default_comm());
62+
}
5363

64+
std::vector<double> res(matrix.shape().first);
5465
gemv(root, res, matrix, broadcasted_b);
5566

5667
if (root == dr::mp::rank()) {

0 commit comments

Comments
 (0)