Skip to content

Commit fd280c4

Browse files
committed
Broadcast input
1 parent 975b44d commit fd280c4

1 file changed

Lines changed: 109 additions & 64 deletions

File tree

tests/smoke/simulated_annealing_re_from_mpi.cpp

Lines changed: 109 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -168,42 +168,71 @@ struct output {
168168

169169
template< typename Dtype >
170170
void read_matrix_data(const std::string &filename, std::vector<Dtype> &data, bool direct) {
171-
// Implementation for reading matrix data from file
172-
try {
173-
Parser parser( filename, direct );
174-
assert( parser.m() == parser.n() );
175-
std::get<0>(Storage::getData()) = parser.n();
171+
const size_t s = grb::spmd<>::pid();
172+
if( s == 0 ){
173+
// Implementation for reading matrix data from file
176174
try {
177-
std::get<1>(Storage::getData()) = parser.nz();
178-
} catch( ... ) {
179-
std::get<1>(Storage::getData()) = parser.entries();
180-
}
181-
/* Once internal issue #342 is resolved this can be re-enabled
182-
for(
183-
auto it = parser.begin( PARALLEL );
184-
it != parser.end( PARALLEL );
185-
++it
186-
) {
187-
data.push_back( *it );
188-
}*/
189-
for(
190-
auto it = parser.begin( SEQUENTIAL );
191-
it != parser.end( SEQUENTIAL );
192-
++it
193-
) {
194-
data.push_back( Dtype( *it ) );
195-
#ifdef DEBUG_SARE
196-
if( spmd<>::pid() == 0 ){
197-
// print last data element from std::vector<NonzeroT> data
198-
std::cout << "readmatrix_data: " << data.back().first.first << ", "
199-
<< data.back().first.second << ", " << data.back().second << "\n";
175+
Parser parser( filename, direct );
176+
assert( parser.m() == parser.n() );
177+
std::get<0>(Storage::getData()) = parser.n();
178+
try {
179+
std::get<1>(Storage::getData()) = parser.nz();
180+
} catch( ... ) {
181+
std::get<1>(Storage::getData()) = parser.entries();
200182
}
183+
/* Once internal issue #342 is resolved this can be re-enabled
184+
for(
185+
auto it = parser.begin( PARALLEL );
186+
it != parser.end( PARALLEL );
187+
++it
188+
) {
189+
data.push_back( *it );
190+
}*/
191+
for(
192+
auto it = parser.begin( SEQUENTIAL );
193+
it != parser.end( SEQUENTIAL );
194+
++it
195+
) {
196+
data.push_back( Dtype( *it ) );
197+
#ifdef DEBUG_SARE
198+
if( s == 0 ){
199+
// print last data element from std::vector<NonzeroT> data
200+
std::cout << "readmatrix_data: " << data.back().first.first << ", "
201+
<< data.back().first.second << ", " << data.back().second << "\n";
202+
}
201203
#endif
204+
}
205+
} catch( std::exception &e ) {
206+
std::cerr << "I/O program failed: " << e.what() << "\n";
207+
return;
202208
}
203-
} catch( std::exception &e ) {
204-
std::cerr << "I/O program failed: " << e.what() << "\n";
205-
return;
206209
}
210+
211+
grb::RC rc = grb::SUCCESS;
212+
213+
size_t n = std::get<0>(Storage::getData());
214+
size_t nz = std::get<0>(Storage::getData());
215+
216+
rc = rc ? rc : grb::collectives<>::broadcast( n, 0 );
217+
rc = rc ? rc : grb::collectives<>::broadcast( nz, 0 );
218+
219+
220+
std::get<0>(Storage::getData()) = n;
221+
std::get<1>(Storage::getData()) = nz;
222+
223+
size_t sz = data.size();
224+
rc = rc ? rc : grb::collectives<>::broadcast( sz, 0 );
225+
assert( rc == grb::SUCCESS );
226+
data.resize( sz );
227+
228+
assert( data.size() >= sz );
229+
for(size_t i = 0 ; i < sz ; ++i){
230+
rc = rc ? rc : grb::collectives<>::broadcast( &(data[i].first), 0 );
231+
rc = rc ? rc : grb::collectives<>::broadcast( &(data[i].second), 0 );
232+
}
233+
std::cerr << s << " afjkdl " << std::endl;
234+
assert( rc == grb::SUCCESS );
235+
207236
}
208237

209238
template< typename NonzeroT, typename IType, typename VType >
@@ -212,50 +241,66 @@ void read_matrix_data_from_array(
212241
std::vector<NonzeroT> &data
213242
) {
214243
// Implementation for reading matrix data from array
215-
try {
216-
for (const auto &entry : array) {
217-
data.emplace_back(
218-
NonzeroT( entry.first.first, entry.first.second, entry.second )
219-
);
244+
try {
245+
for (const auto &entry : array) {
246+
data.emplace_back(
247+
NonzeroT( entry.first.first, entry.first.second, entry.second )
248+
);
220249
#ifdef DEBUG_SARE
221250
if( spmd<>::pid() < 2 ){
222251
// print last data element from std::vector<NonzeroT> data
223252
std::cout << "read_matrix_data_from_array: " << data.back().first.first << ", "
224253
<< data.back().first.second << ", " << data.back().second << "\n";
225254
}
226255
#endif
227-
}
228-
std::get<0>(Storage::getData()) = test_data::n;
229-
std::get<1>(Storage::getData()) = data.size();
230-
} catch (const std::exception &e) {
231-
std::cerr << "Failed to read matrix data from array: " << e.what() << "\n";
232-
return;
233-
}
256+
}
257+
std::get<0>(Storage::getData()) = test_data::n;
258+
std::get<1>(Storage::getData()) = data.size();
259+
} catch (const std::exception &e) {
260+
std::cerr << "Failed to read matrix data from array: " << e.what() << "\n";
261+
return;
262+
}
234263
}
235264

236265
template< typename Dtype >
237266
void read_vector_data(const std::string &filename, std::vector<Dtype> &data) {
238-
// Implementation for reading vector data from file
239-
try {
240-
std::ifstream file( filename );
241-
if( !file.is_open() ) {
242-
std::cerr << "Failed to open vector file: " << filename << "\n";
243-
return;
244-
}
245-
std::string line;
246-
while( std::getline( file, line ) ) {
247-
if( line.empty() ) continue; // skip empty lines
248-
std::istringstream iss( line );
249-
Dtype v;
250-
if( !(iss >> v) ) {
251-
throw std::runtime_error( "Failed to parse line in vector file" );
252-
}
253-
data.push_back( v );
254-
}
255-
} catch( std::exception &e ) {
256-
std::cerr << "I/O program failed: " << e.what() << "\n";
257-
return;
258-
}
267+
const size_t s = grb::spmd<>::pid();
268+
if( s == 0 ){
269+
// Implementation for reading vector data from file
270+
try {
271+
std::ifstream file( filename );
272+
if( !file.is_open() ) {
273+
std::cerr << "Failed to open vector file: " << filename << "\n";
274+
return;
275+
}
276+
std::string line;
277+
while( std::getline( file, line ) ) {
278+
if( line.empty() ) continue; // skip empty lines
279+
std::istringstream iss( line );
280+
Dtype v;
281+
if( !(iss >> v) ) {
282+
throw std::runtime_error( "Failed to parse line in vector file" );
283+
}
284+
data.push_back( v );
285+
}
286+
} catch( std::exception &e ) {
287+
std::cerr << "I/O program failed: " << e.what() << "\n";
288+
return;
289+
}
290+
}
291+
292+
grb::RC rc = grb::SUCCESS;
293+
size_t sz = data.size();
294+
295+
rc = rc ? rc : grb::collectives<>::broadcast( sz, 0 );
296+
assert( rc == grb::SUCCESS );
297+
data.resize( sz );
298+
299+
for(size_t i = 0 ; i < sz ; ++i){
300+
rc = rc ? rc : grb::collectives<>::broadcast( data[i], 0 );
301+
}
302+
assert( rc == grb::SUCCESS );
303+
259304
}
260305

261306
template< typename Dtype >

0 commit comments

Comments
 (0)