Skip to content

Commit 448c3ab

Browse files
committed
Broadcast input
1 parent 2138f5e commit 448c3ab

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
@@ -164,42 +164,71 @@ struct output {
164164

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

205234
template< typename NonzeroT, typename IType, typename VType >
@@ -208,50 +237,66 @@ void read_matrix_data_from_array(
208237
std::vector<NonzeroT> &data
209238
) {
210239
// Implementation for reading matrix data from array
211-
try {
212-
for (const auto &entry : array) {
213-
data.emplace_back(
214-
NonzeroT( entry.first.first, entry.first.second, entry.second )
215-
);
240+
try {
241+
for (const auto &entry : array) {
242+
data.emplace_back(
243+
NonzeroT( entry.first.first, entry.first.second, entry.second )
244+
);
216245
#ifdef DEBUG_SARE
217246
if( spmd<>::pid() < 2 ){
218247
// print last data element from std::vector<NonzeroT> data
219248
std::cout << "read_matrix_data_from_array: " << data.back().first.first << ", "
220249
<< data.back().first.second << ", " << data.back().second << "\n";
221250
}
222251
#endif
223-
}
224-
std::get<0>(Storage::getData()) = test_data::n;
225-
std::get<1>(Storage::getData()) = data.size();
226-
} catch (const std::exception &e) {
227-
std::cerr << "Failed to read matrix data from array: " << e.what() << "\n";
228-
return;
229-
}
252+
}
253+
std::get<0>(Storage::getData()) = test_data::n;
254+
std::get<1>(Storage::getData()) = data.size();
255+
} catch (const std::exception &e) {
256+
std::cerr << "Failed to read matrix data from array: " << e.what() << "\n";
257+
return;
258+
}
230259
}
231260

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

257302
template< typename Dtype >

0 commit comments

Comments
 (0)