Skip to content

Commit b9d46bd

Browse files
committed
Implementation of the smoke-test + refactor
1 parent 1b167f4 commit b9d46bd

File tree

2 files changed

+187
-31
lines changed

2 files changed

+187
-31
lines changed

tests/smoke/sssp.cpp

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright 2021 Huawei Technologies Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <exception>
18+
#include <iostream>
19+
#include <vector>
20+
#include <cinttypes>
21+
22+
#include <graphblas.hpp>
23+
#include <graphblas/algorithms/sssp.hpp>
24+
#include <graphblas/utils/Timer.hpp>
25+
#include <graphblas/utils/parser.hpp>
26+
#include <utils/output_verification.hpp>
27+
28+
using namespace grb;
29+
30+
template< typename T >
31+
Vector< T > stdToGrbVector( const std::vector< T > & in ) {
32+
Vector< T > out( in.size() );
33+
for( size_t i = 0; i < in.size(); i++ )
34+
setElement( out, in[ i ], i );
35+
return out;
36+
}
37+
38+
template< typename T >
39+
struct input_t {
40+
const Matrix< T >& A;
41+
size_t root;
42+
const Vector< T > & expected_distances;
43+
44+
// Empty constructor necessary for distributed backends
45+
input_t(
46+
const Matrix<T>& A = {0,0},
47+
size_t root = 0,
48+
const Vector< T > & expected_distances = {0}
49+
) : A( A ), root( root ), expected_distances( expected_distances ) {}
50+
};
51+
52+
struct output_t {
53+
RC rc = SUCCESS;
54+
utils::TimerResults times;
55+
};
56+
57+
template< typename T >
58+
void grbProgram( const struct input_t< T > & input, struct output_t & output ) {
59+
std::cout << std::endl << "Running SSSP" << std::endl;
60+
output.rc = SUCCESS;
61+
utils::Timer timer;
62+
63+
timer.reset();
64+
bool explored_all = false;
65+
size_t max_level = 0;
66+
Vector< T > distances( grb::nrows( input.A ) );
67+
Vector< T > x( grb::nrows( input.A ) ), y( grb::nrows( input.A ) );
68+
output.times.preamble = timer.time();
69+
70+
timer.reset();
71+
output.rc = algorithms::sssp( input.A, input.root, explored_all, max_level, distances, x, y );
72+
output.times.useful = timer.time();
73+
74+
// Check distances by comparing it with the expected one
75+
bool expected_equals = std::equal(
76+
input.expected_distances.cbegin(), input.expected_distances.cend(), distances.cbegin()
77+
);
78+
if( expected_equals ) {
79+
std::cout << "SUCCESS: distances are correct" << std::endl;
80+
} else {
81+
std::cerr << "FAILED: distances are incorrect" << std::endl;
82+
std::cerr << "distances != expected_distances" << std::endl;
83+
for( size_t i = 0; i < grb::nrows( input.A ); i++ )
84+
std::cerr << std::string( 3, ' ' ) << distances[ i ] << " | "
85+
<< input.expected_distances[ i ] << std::endl;
86+
output.rc = FAILED;
87+
}
88+
}
89+
90+
int main( int argc, char ** argv ) {
91+
(void)argc;
92+
(void)argv;
93+
94+
Benchmarker< AUTOMATIC > benchmarker;
95+
std::cout << "Test executable: " << argv[ 0 ] << std::endl;
96+
97+
if( argc < 4 ) {
98+
std::cerr << "Usage: \n\t"
99+
<< argv[ 0 ]
100+
<< " <dataset>"
101+
<< " <direct|indirect>"
102+
<< " <root>"
103+
<< " <expected_distances_filepath>"
104+
<< " [ inner_iterations=1 ]"
105+
<< " [ outer_iterations=1 ]"
106+
<< std::endl;
107+
return 1;
108+
}
109+
std::string dataset( argv[ 1 ] );
110+
bool direct = ( strcmp( argv[ 2 ], "direct" ) == 0 );
111+
size_t root = std::stoul( argv[ 3 ] );
112+
std::string expected_distances_filepath( argv[ 4 ] );
113+
size_t inner_iterations = ( argc >= 5 ) ? std::stoul( argv[ 4 ] ) : 1;
114+
size_t outer_iterations = ( argc >= 6 ) ? std::stoul( argv[ 5 ] ) : 1;
115+
116+
std::cout << "-- Running test on file: " << dataset << std::endl;
117+
118+
// Read matrix from file
119+
utils::MatrixFileReader< double > reader( dataset, direct, true );
120+
size_t r = reader.n(), c = reader.m();
121+
assert( r == c );
122+
Matrix< double > A( r, c );
123+
RC rc_build = buildMatrixUnique(
124+
A, reader.cbegin( SEQUENTIAL ), reader.cend( SEQUENTIAL ), SEQUENTIAL
125+
);
126+
if( rc_build != SUCCESS ) {
127+
std::cerr << "ERROR during buildMatrixUnique: rc = "
128+
<< grb::toString( rc_build ) << std::endl;
129+
return 1;
130+
}
131+
std::cout << "Matrix read successfully" << std::endl;
132+
133+
Vector< double > expected_distances( r );
134+
// TODO: Read expected_distances vector from file
135+
136+
// Run the algorithm
137+
input_t< double > input { A, root, expected_distances };
138+
output_t output;
139+
RC bench_rc = benchmarker.exec(
140+
&grbProgram, input, output, inner_iterations, outer_iterations, true
141+
);
142+
if( bench_rc ) {
143+
std::cerr << "ERROR during execution on file " << dataset
144+
<< ": rc = " << grb::toString(bench_rc) << std::endl;
145+
return bench_rc;
146+
} else if( output.rc ) {
147+
std::cerr << "Test failed: rc = " << grb::toString(output.rc) << std::endl;
148+
return output.rc;
149+
}
150+
151+
std::cerr << std::flush;
152+
std::cout << "Test OK" << std::endl << std::flush;
153+
154+
return 0;
155+
}

tests/unit/sssp.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
#include <exception>
1818
#include <iostream>
1919
#include <vector>
20+
#include <cinttypes>
2021

21-
#include <inttypes.h>
22-
22+
#include <graphblas.hpp>
2323
#include <graphblas/algorithms/sssp.hpp>
2424
#include <graphblas/utils/Timer.hpp>
2525
#include <graphblas/utils/parser.hpp>
26-
27-
#include <graphblas.hpp>
2826
#include <utils/output_verification.hpp>
2927

3028
using namespace grb;
@@ -43,12 +41,18 @@ struct input_t {
4341
Matrix< T > A;
4442
size_t root;
4543
const Vector< T > & expected_distances;
44+
45+
// Empty constructor necessary for distributed backends
46+
input_t(
47+
const Matrix<T>& A = {0,0},
48+
size_t root = 0,
49+
const Vector< T > & expected_distances = {0}
50+
) : A( A ), root( root ), expected_distances( expected_distances ) {}
4651
};
4752

4853
struct output_t {
4954
RC rc = SUCCESS;
5055
utils::TimerResults times;
51-
size_t data_in_local;
5256
};
5357

5458
template< typename T >
@@ -83,17 +87,14 @@ void grbProgram( const struct input_t< T > & input, struct output_t & output ) {
8387
int main( int argc, char ** argv ) {
8488
(void)argc;
8589
(void)argv;
86-
constexpr size_t niterations = 1;
8790

88-
grb::Benchmarker< grb::EXEC_MODE::AUTOMATIC > benchmarker;
91+
Launcher< AUTOMATIC > launcher;
8992
std::cout << "Test executable: " << argv[ 0 ] << std::endl;
9093

91-
// Check if we are testing on a file
92-
if( argc != 1 && argc != 4 ) {
93-
std::cerr << "Usage: \n\t" << argv[ 0 ] << " [ <graph_filepath> <root> <expected_distances_filepath> ]" << std::endl;
94+
if( argc != 1 ) {
95+
std::cerr << "Usage: \n\t" << argv[ 0 ] << std::endl;
9496
return 1;
9597
}
96-
bool test_on_file = ( argc == 4 );
9798

9899
/** Matrix A0: Fully connected acyclic graph
99100
*
@@ -116,10 +117,10 @@ int main( int argc, char ** argv ) {
116117
grb::buildMatrixUnique( A, A_rows.data(), A_cols.data(), A_values.data(), A_rows.size(), PARALLEL );
117118
input_t< weight_t > input { A, root, stdToGrbVector( expected_distances ) };
118119
output_t output;
119-
RC bench_rc = benchmarker.exec( &grbProgram, input, output, niterations, 1, true );
120-
if( bench_rc != SUCCESS ) {
121-
std::cerr << "ERROR during execution: rc = " << bench_rc << std::endl;
122-
return bench_rc;
120+
RC rc = launcher.exec( &grbProgram, input, output, true );
121+
if( rc != SUCCESS ) {
122+
std::cerr << "ERROR during execution: rc = " << rc << std::endl;
123+
return rc;
123124
} else if( output.rc ) {
124125
std::cerr << "Test failed: rc = " << output.rc << std::endl;
125126
return output.rc;
@@ -148,10 +149,10 @@ int main( int argc, char ** argv ) {
148149
grb::buildMatrixUnique( A, A_rows.data(), A_cols.data(), A_values.data(), A_rows.size(), PARALLEL );
149150
input_t< weight_t > input { A, root, stdToGrbVector( expected_distances ) };
150151
output_t output;
151-
RC bench_rc = benchmarker.exec( &grbProgram, input, output, niterations, 1, true );
152-
if( bench_rc != SUCCESS ) {
153-
std::cerr << "ERROR during execution: rc = " << bench_rc << std::endl;
154-
return bench_rc;
152+
RC rc = launcher.exec( &grbProgram, input, output, true );
153+
if( rc != SUCCESS ) {
154+
std::cerr << "ERROR during execution: rc = " << rc << std::endl;
155+
return rc;
155156
} else if( output.rc ) {
156157
std::cerr << "Test failed: rc = " << output.rc << std::endl;
157158
return output.rc;
@@ -180,10 +181,10 @@ int main( int argc, char ** argv ) {
180181
grb::buildMatrixUnique( A, A_rows.data(), A_cols.data(), A_values.data(), A_rows.size(), PARALLEL );
181182
input_t< weight_t > input { A, root, stdToGrbVector( expected_distances ) };
182183
output_t output;
183-
RC bench_rc = benchmarker.exec( &grbProgram, input, output, niterations, 1, true );
184-
if( bench_rc != SUCCESS ) {
185-
std::cerr << "ERROR during execution: rc = " << bench_rc << std::endl;
186-
return bench_rc;
184+
RC rc = launcher.exec( &grbProgram, input, output, true );
185+
if( rc != SUCCESS ) {
186+
std::cerr << "ERROR during execution: rc = " << rc << std::endl;
187+
return rc;
187188
} else if( output.rc ) {
188189
std::cerr << "Test failed: rc = " << output.rc << std::endl;
189190
return output.rc;
@@ -209,10 +210,10 @@ int main( int argc, char ** argv ) {
209210
grb::buildMatrixUnique( A, A_rows.data(), A_cols.data(), A_values.data(), A_rows.size(), PARALLEL );
210211
input_t< weight_t > input { A, root, stdToGrbVector( expected_distances ) };
211212
output_t output;
212-
RC bench_rc = benchmarker.exec( &grbProgram, input, output, niterations, 1, true );
213-
if( bench_rc != SUCCESS ) {
214-
std::cerr << "ERROR during execution: rc = " << bench_rc << std::endl;
215-
return bench_rc;
213+
RC rc = launcher.exec( &grbProgram, input, output, true );
214+
if( rc != SUCCESS ) {
215+
std::cerr << "ERROR during execution: rc = " << rc << std::endl;
216+
return rc;
216217
} else if( output.rc ) {
217218
std::cerr << "Test failed: rc = " << output.rc << std::endl;
218219
return output.rc;
@@ -230,10 +231,10 @@ int main( int argc, char ** argv ) {
230231
grb::buildMatrixUnique( A, A_rows.data(), A_cols.data(), A_values.data(), A_rows.size(), PARALLEL );
231232
input_t< weight_t > input { A, root, stdToGrbVector( expected_distances ) };
232233
output_t output;
233-
RC bench_rc = benchmarker.exec( &grbProgram, input, output, niterations, 1, true );
234-
if( bench_rc != SUCCESS ) {
235-
std::cerr << "ERROR during execution: rc = " << bench_rc << std::endl;
236-
return bench_rc;
234+
RC rc = launcher.exec( &grbProgram, input, output, true );
235+
if( rc != SUCCESS ) {
236+
std::cerr << "ERROR during execution: rc = " << rc << std::endl;
237+
return rc;
237238
} else if( output.rc ) {
238239
std::cerr << "Test failed: rc = " << output.rc << std::endl;
239240
return output.rc;

0 commit comments

Comments
 (0)