Skip to content

Commit 445e6e6

Browse files
raphael-s-steinerChristos Konstantinos Matzoros
andauthored
Eigen tests
* More Eigen Tests * more tests * more tests * more tests * fix iterator comparison * removed comment * valid iterator check to Eigen Matrix graph iterator * Making bool explicit * Add static casts --------- Co-authored-by: Christos Konstantinos Matzoros <christos.konstantinos.matzoros@h-partners.com>
1 parent 42c0ece commit 445e6e6

3 files changed

Lines changed: 177 additions & 10 deletions

File tree

include/osp/graph_implementations/eigen_matrix_adapter/eigen_sparse_iterator.hpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ class EigenCSRRange {
7272
return temp;
7373
}
7474

75-
bool operator==(const Iterator &other) const {return it_ == other.it_;}
76-
bool operator!=(const Iterator &other) const { return it_ != other.it_;}
75+
inline bool operator==(const Iterator &other) const {
76+
return (it_ && other.it_ && it_.index() == other.it_.index()) || ((not it_) && (not other.it_));
77+
}
78+
79+
inline bool operator!=(const Iterator &other) const { return not(*this == other); }
80+
81+
inline explicit operator bool() const { return it_; }
7782
};
7883

7984
EigenCSRRange(const Graph &graph, EigenIdxType idx) : graph_(graph), index_(idx) {}
@@ -123,7 +128,6 @@ class EigenCSCRange {
123128

124129
Iterator &operator++() {
125130
++it_;
126-
SkipDiagonal();
127131
return *this;
128132
}
129133

@@ -133,8 +137,13 @@ class EigenCSCRange {
133137
return temp;
134138
}
135139

136-
bool operator==(const Iterator &other) const {return it_ == other.it_;}
137-
bool operator!=(const Iterator &other) const { return it_ != other.it_;}
140+
inline bool operator==(const Iterator &other) const {
141+
return (it_ && other.it_ && it_.index() == other.it_.index()) || ((not it_) && (not other.it_));
142+
}
143+
144+
inline bool operator!=(const Iterator &other) const { return not(*this == other); }
145+
146+
inline explicit operator bool() const { return it_; }
138147
};
139148

140149
EigenCSCRange(const Graph &graph, EigenIdxType idx) : graph_(graph), index_(idx) {}

tests/sparse_matrix_impl.cpp

Lines changed: 151 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ limitations under the License.
2424
# include <iostream>
2525
# include <vector>
2626

27+
# include "osp/auxiliary/io/general_file_reader.hpp"
28+
# include "osp/auxiliary/io/hdag_graph_file_reader.hpp"
2729
# include "osp/graph_algorithms/directed_graph_path_util.hpp"
2830
# include "osp/graph_algorithms/directed_graph_util.hpp"
31+
# include "osp/graph_implementations/adj_list_impl/compact_sparse_graph.hpp"
2932
# include "osp/graph_implementations/eigen_matrix_adapter/sparse_matrix.hpp"
33+
# include "test_graphs.hpp"
34+
# include "osp/graph_implementations/adj_list_impl/computational_dag_vector_impl.hpp"
3035

3136
using namespace osp;
3237

38+
using SmCsr = Eigen::SparseMatrix<double, Eigen::RowMajor, int32_t>;
39+
using SmCsc = Eigen::SparseMatrix<double, Eigen::ColMajor, int32_t>;
40+
using Triplet = Eigen::Triplet<double>;
41+
42+
using VImpl1 = CDagVertexImpl<std::size_t, unsigned, unsigned, unsigned, unsigned>;
43+
3344
BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
3445
/*
3546
@@ -57,9 +68,6 @@ BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
5768
6 | 0.0 10.0 11.0 0 0 12.0 0
5869
5970
*/
60-
using SmCsr = Eigen::SparseMatrix<double, Eigen::RowMajor, int32_t>;
61-
using SmCsc = Eigen::SparseMatrix<double, Eigen::ColMajor, int32_t>;
62-
using Triplet = Eigen::Triplet<double>;
6371
const int size = 7;
6472
std::vector<Triplet> triplets;
6573

@@ -132,29 +140,42 @@ BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
132140
BOOST_CHECK_EQUAL(v, vertices[idx++]);
133141

134142
size_t i = 0;
143+
size_t cntr = 0;
135144
const size_t vi = static_cast<size_t>(v);
136145

137146
for (const auto &e : graph.Children(v)) {
147+
++cntr;
138148
BOOST_CHECK_EQUAL(e, outNeighbors[vi][i++]);
139149
}
150+
BOOST_CHECK_EQUAL(cntr, outNeighbors[vi].size());
151+
BOOST_CHECK_EQUAL(graph.OutDegree(v), outNeighbors[vi].size());
140152

141153
i = 0;
154+
cntr = 0;
142155
for (const auto &e : graph.Parents(v)) {
156+
++cntr;
143157
BOOST_CHECK_EQUAL(e, inNeighbors[vi][i++]);
144158
}
159+
BOOST_CHECK_EQUAL(cntr, inNeighbors[vi].size());
160+
BOOST_CHECK_EQUAL(graph.InDegree(v), inNeighbors[vi].size());
145161

146162
i = 0;
163+
cntr = 0;
147164
for (const auto &e : OutEdges(v, graph)) {
165+
++cntr;
148166
BOOST_CHECK_EQUAL(Target(e, graph), outNeighbors[vi][i++]);
149167
}
168+
BOOST_CHECK_EQUAL(cntr, outNeighbors[vi].size());
169+
BOOST_CHECK_EQUAL(graph.OutDegree(v), outNeighbors[vi].size());
150170

151171
i = 0;
172+
cntr = 0;
152173
for (const auto &e : InEdges(v, graph)) {
174+
++cntr;
153175
BOOST_CHECK_EQUAL(Source(e, graph), inNeighbors[vi][i++]);
154176
}
155-
177+
BOOST_CHECK_EQUAL(cntr, inNeighbors[vi].size());
156178
BOOST_CHECK_EQUAL(graph.InDegree(v), inNeighbors[vi].size());
157-
BOOST_CHECK_EQUAL(graph.OutDegree(v), outNeighbors[vi].size());
158179
}
159180

160181
unsigned count = 0;
@@ -165,4 +186,129 @@ BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
165186
BOOST_CHECK_EQUAL(count, 11);
166187
}
167188

189+
BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter2) {
190+
std::vector<std::string> filenamesGraph = TestMTXGraphs();
191+
192+
// Getting root git directory
193+
std::filesystem::path cwd = std::filesystem::current_path();
194+
std::cout << cwd << std::endl;
195+
while ((!cwd.empty()) && (cwd.filename() != "OneStopParallel")) {
196+
cwd = cwd.parent_path();
197+
std::cout << cwd << std::endl;
198+
}
199+
200+
for (auto &filenameGraph : filenamesGraph) {
201+
std::string nameGraph = filenameGraph.substr(filenameGraph.find_last_of("/\\") + 1);
202+
nameGraph = nameGraph.substr(0, nameGraph.find_last_of("."));
203+
204+
std::cout << "Graph: " << nameGraph << std::endl;
205+
206+
ComputationalDagVectorImpl<VImpl1> graph1;
207+
208+
const bool statusGraph = file_reader::ReadGraph((cwd / filenameGraph).string(), graph1);
209+
210+
BOOST_CHECK(statusGraph);
211+
if (!statusGraph) {
212+
std::cout << "Reading files failed." << std::endl;
213+
}
214+
215+
CompactSparseGraph<true, true, true, true, true> graph2(graph1);
216+
217+
std::vector<Triplet> triplets;
218+
// Diagonal entries
219+
for (const auto &vert : graph1.Vertices()) {
220+
triplets.emplace_back(vert, vert, 1.0);
221+
}
222+
223+
// Below Diagonal
224+
for (const auto &vert : graph1.Vertices()) {
225+
for (const auto &child : graph1.Children(vert)) {
226+
triplets.emplace_back(child, vert, 2.0);
227+
}
228+
}
229+
230+
const int32_t nVert = static_cast<int32_t>(graph1.NumVertices());
231+
SmCsr lCsr(nVert, nVert);
232+
lCsr.setFromTriplets(triplets.begin(), triplets.end());
233+
SmCsc lCsc{};
234+
lCsc = lCsr;
235+
236+
SparseMatrixImp<int32_t> graph;
237+
graph.SetCsr(&lCsr);
238+
graph.SetCsc(&lCsc);
239+
240+
BOOST_CHECK_EQUAL(static_cast<std::size_t>(graph.NumVertices()), graph1.NumVertices());
241+
BOOST_CHECK_EQUAL(static_cast<std::size_t>(graph.NumVertices()), graph2.NumVertices());
242+
243+
BOOST_CHECK_EQUAL(static_cast<std::size_t>(graph.NumEdges()), graph1.NumEdges());
244+
BOOST_CHECK_EQUAL(static_cast<std::size_t>(graph.NumEdges()), graph2.NumEdges());
245+
246+
for (const auto &vert : graph2.Vertices()) {
247+
auto chldren = graph.Children(vert);
248+
auto chldren2 = graph2.Children(vert);
249+
auto it = chldren.begin();
250+
auto it_other = chldren.begin();
251+
const auto begin = chldren.begin();
252+
auto it2 = chldren2.begin();
253+
const auto end = chldren.end();
254+
const auto end_other = chldren.end();
255+
const auto end2 = chldren2.end();
256+
257+
BOOST_CHECK(end == end_other);
258+
259+
std::size_t cntr = 0;
260+
while ((it != end) && (it2 != end2)) {
261+
BOOST_CHECK_EQUAL(*it, *it2);
262+
BOOST_CHECK(it == it_other);
263+
BOOST_CHECK(cntr == 0U || it != begin);
264+
BOOST_CHECK(cntr == 0U || (not (it == begin)));
265+
BOOST_CHECK_EQUAL(static_cast<bool>(it), it != end);
266+
267+
++cntr;
268+
++it;
269+
++it_other;
270+
++it2;
271+
}
272+
BOOST_CHECK_EQUAL(cntr, graph.OutDegree(vert));
273+
BOOST_CHECK_EQUAL(cntr, graph1.OutDegree(vert));
274+
BOOST_CHECK_EQUAL(cntr, graph2.OutDegree(vert));
275+
BOOST_CHECK(it == end);
276+
BOOST_CHECK(it2 == end2);
277+
BOOST_CHECK_EQUAL(static_cast<bool>(it), it != end);
278+
}
279+
280+
for (const auto &vert : graph2.Vertices()) {
281+
auto parents = graph.Parents(vert);
282+
auto parents2 = graph2.Parents(vert);
283+
auto it = parents.begin();
284+
auto it_other = parents.begin();
285+
const auto begin = parents.begin();
286+
auto it2 = parents2.begin();
287+
const auto end = parents.end();
288+
const auto end_other = parents.end();
289+
const auto end2 = parents2.end();
290+
291+
BOOST_CHECK(end == end_other);
292+
293+
std::size_t cntr = 0;
294+
while ((it != end) && (it2 != end2)) {
295+
BOOST_CHECK_EQUAL(*it, *it2);
296+
BOOST_CHECK(cntr == 0U || it != begin);
297+
BOOST_CHECK(cntr == 0U || (not (it == begin)));
298+
BOOST_CHECK_EQUAL(static_cast<bool>(it), it != end);
299+
300+
++cntr;
301+
++it;
302+
++it2;
303+
}
304+
BOOST_CHECK_EQUAL(cntr, graph.InDegree(vert));
305+
BOOST_CHECK_EQUAL(cntr, graph1.InDegree(vert));
306+
BOOST_CHECK_EQUAL(cntr, graph2.InDegree(vert));
307+
BOOST_CHECK(it == end);
308+
BOOST_CHECK(it2 == end2);
309+
BOOST_CHECK_EQUAL(static_cast<bool>(it), it != end);
310+
}
311+
}
312+
}
313+
168314
#endif

tests/test_graphs.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ std::vector<std::string> TestGraphs() {
5757
"data/spaa/tiny/instance_CG_N3_K1_nzP0d5.hdag"};
5858
}
5959

60+
std::vector<std::string> TestMTXGraphs() {
61+
return {"data/mtx_tests/ErdosRenyi_100_1k_A.mtx",
62+
"data/mtx_tests/ErdosRenyi_200_5k_A.mtx",
63+
"data/mtx_tests/ErdosRenyi_2k_14k_A.mtx",
64+
"data/mtx_tests/ErdosRenyi_500_8k_A.mtx",
65+
"data/mtx_tests/ErdosRenyi_8_19_A.mtx",
66+
"data/mtx_tests/RandomBand_p40_b30_2k_23k_A.mtx",
67+
"data/mtx_tests/RandomBand_p70_b12_500_4k_A.mtx",
68+
"data/mtx_tests/RandomBand_p80_b14_200_2k_A.mtx",
69+
"data/mtx_tests/RandomBand_p80_b5_100_419_A.mtx"};
70+
}
71+
6072
/**
6173
* @brief Constructs a DAG with multiple identical, parallel pipelines.
6274
*

0 commit comments

Comments
 (0)