Skip to content

Commit f46d4a0

Browse files
more tests
1 parent 0839092 commit f46d4a0

2 files changed

Lines changed: 129 additions & 3 deletions

File tree

tests/sparse_matrix_impl.cpp

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@ 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_edge_idx_vector_impl.hpp"
35+
# include "osp/graph_implementations/adj_list_impl/computational_dag_vector_impl.hpp"
3036

3137
using namespace osp;
3238

39+
using SmCsr = Eigen::SparseMatrix<double, Eigen::RowMajor, int32_t>;
40+
using SmCsc = Eigen::SparseMatrix<double, Eigen::ColMajor, int32_t>;
41+
using Triplet = Eigen::Triplet<double>;
42+
43+
using VImpl1 = CDagVertexImpl<std::size_t, unsigned, unsigned, unsigned, unsigned>;
44+
3345
BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
3446
/*
3547
@@ -57,9 +69,6 @@ BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
5769
6 | 0.0 10.0 11.0 0 0 12.0 0
5870
5971
*/
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>;
6372
const int size = 7;
6473
std::vector<Triplet> triplets;
6574

@@ -178,4 +187,109 @@ BOOST_AUTO_TEST_CASE(TestSparseMatrixAdapter1) {
178187
BOOST_CHECK_EQUAL(count, 11);
179188
}
180189

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