Skip to content

Commit 80aa762

Browse files
Parallel reading of serial basis files (#316)
1 parent 87fed6b commit 80aa762

5 files changed

Lines changed: 58 additions & 18 deletions

File tree

lib/linalg/BasisReader.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ namespace CAROM {
2323
BasisReader::BasisReader(
2424
const std::string& base_file_name,
2525
Database::formats db_format,
26-
const int dim) :
26+
const int dim,
27+
MPI_Comm comm) :
2728
d_dim(dim),
2829
full_file_name(""),
2930
base_file_name_(base_file_name),
3031
d_format(db_format)
3132
{
32-
CAROM_ASSERT(!base_file_name.empty());
33+
CAROM_VERIFY(!base_file_name.empty());
34+
CAROM_VERIFY(comm == MPI_COMM_NULL || comm == MPI_COMM_WORLD);
35+
d_distributed = comm != MPI_COMM_NULL;
3336

3437
int mpi_init;
3538
MPI_Initialized(&mpi_init);
3639
int rank;
37-
if (mpi_init) {
38-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
40+
if (mpi_init && d_distributed) {
41+
MPI_Comm_rank(comm, &rank);
3942
}
4043
else {
4144
rank = 0;
@@ -55,15 +58,15 @@ BasisReader::BasisReader(
5558
We allow 0 local dimension. (global dimension still needs to be positive)
5659
*/
5760
std::vector<int> tmp;
58-
d_global_dim = get_global_offsets(d_dim, tmp, MPI_COMM_WORLD);
61+
d_global_dim = get_global_offsets(d_dim, tmp, comm);
5962
CAROM_VERIFY(d_dim >= 0);
6063
CAROM_VERIFY(d_global_dim > 0);
6164
d_database = new HDFDatabaseMPIO();
6265
}
6366
else
6467
CAROM_ERROR("BasisWriter only supports HDF5/HDF5_MPIO data format!\n");
6568

66-
d_database->open(full_file_name, "r", MPI_COMM_WORLD);
69+
d_database->open(full_file_name, "r", comm);
6770
}
6871

6972
BasisReader::~BasisReader()
@@ -78,12 +81,12 @@ BasisReader::getSpatialBasis()
7881
int num_rows = getDim("basis");
7982
int num_cols = getNumSamples("basis");
8083

81-
Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols, true);
84+
Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols, d_distributed);
8285

8386
d_database->getDoubleArray("spatial_basis",
8487
&spatial_basis_vectors->item(0, 0),
8588
num_rows*num_cols,
86-
true);
89+
d_distributed);
8790
return std::unique_ptr<Matrix>(spatial_basis_vectors);
8891
}
8992

@@ -107,15 +110,16 @@ BasisReader::getSpatialBasis(
107110
CAROM_VERIFY(start_col <= end_col && end_col <= num_cols);
108111
int num_cols_to_read = end_col - start_col + 1;
109112

110-
Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols_to_read, true);
113+
Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols_to_read,
114+
d_distributed);
111115
sprintf(tmp, "spatial_basis");
112116
d_database->getDoubleArray(tmp,
113117
&spatial_basis_vectors->item(0, 0),
114118
num_rows*num_cols_to_read,
115119
start_col - 1,
116120
num_cols_to_read,
117121
num_cols,
118-
true);
122+
d_distributed);
119123
return std::unique_ptr<Matrix>(spatial_basis_vectors);
120124
}
121125

@@ -156,7 +160,7 @@ BasisReader::getTemporalBasis()
156160
sprintf(tmp, "temporal_basis");
157161
d_database->getDoubleArray(tmp,
158162
&temporal_basis_vectors->item(0, 0),
159-
num_rows*num_cols);
163+
num_rows*num_cols, d_distributed);
160164
return std::unique_ptr<Matrix>(temporal_basis_vectors);
161165
}
162166

@@ -187,7 +191,8 @@ BasisReader::getTemporalBasis(
187191
num_rows*num_cols_to_read,
188192
start_col - 1,
189193
num_cols_to_read,
190-
num_cols);
194+
num_cols,
195+
d_distributed);
191196
return std::unique_ptr<Matrix>(temporal_basis_vectors);
192197
}
193198

@@ -229,7 +234,8 @@ BasisReader::getSingularValues()
229234
sprintf(tmp, "singular_value");
230235
d_database->getDoubleArray(tmp,
231236
&singular_values->item(0),
232-
size);
237+
size,
238+
d_distributed);
233239
return std::unique_ptr<Vector>(singular_values);
234240
}
235241

@@ -324,12 +330,12 @@ BasisReader::getSnapshotMatrix()
324330
int num_cols = getNumSamples("snapshot");
325331

326332
char tmp[100];
327-
Matrix* snapshots = new Matrix(num_rows, num_cols, true);
333+
Matrix* snapshots = new Matrix(num_rows, num_cols, d_distributed);
328334
sprintf(tmp, "snapshot_matrix");
329335
d_database->getDoubleArray(tmp,
330336
&snapshots->item(0, 0),
331337
num_rows*num_cols,
332-
true);
338+
d_distributed);
333339
return std::unique_ptr<Matrix>(snapshots);
334340
}
335341

@@ -353,15 +359,15 @@ BasisReader::getSnapshotMatrix(
353359
int num_cols_to_read = end_col - start_col + 1;
354360

355361
char tmp[100];
356-
Matrix* snapshots = new Matrix(num_rows, num_cols_to_read, true);
362+
Matrix* snapshots = new Matrix(num_rows, num_cols_to_read, d_distributed);
357363
sprintf(tmp, "snapshot_matrix");
358364
d_database->getDoubleArray(tmp,
359365
&snapshots->item(0, 0),
360366
num_rows*num_cols_to_read,
361367
start_col - 1,
362368
num_cols_to_read,
363369
num_cols,
364-
true);
370+
d_distributed);
365371
return std::unique_ptr<Matrix>(snapshots);
366372
}
367373
}

lib/linalg/BasisReader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class BasisReader {
5151
BasisReader(
5252
const std::string& base_file_name,
5353
Database::formats db_format = Database::formats::HDF5,
54-
const int dim = -1);
54+
const int dim = -1,
55+
MPI_Comm comm = MPI_COMM_WORLD);
5556

5657
/**
5758
* @brief Destructor.
@@ -314,6 +315,8 @@ class BasisReader {
314315
* If negative, use the dimension from the rank-specific local file.
315316
*/
316317
int d_global_dim;
318+
319+
bool d_distributed;
317320
};
318321

319322
}

unit_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ endforeach()
8888
# Copy testing inputs and scripts to build/tests/
8989
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/baselines/basis_conversion/
9090
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/baselines/basis_conversion/")
91+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/basis_data/
92+
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/basis_data/")
9193
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/s_opt_data/
9294
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/s_opt_data/")
9395

unit_tests/basis_data/basis.000000

4.7 KB
Binary file not shown.

unit_tests/test_HDFDatabase.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,35 @@ TEST(HDF5, Test_selective_parallel_reading)
543543

544544
#endif
545545

546+
TEST(HDF5, Test_serial_file_parallel_reading)
547+
{
548+
// Read the matrix on all ranks.
549+
CAROM::BasisReader reader("./basis_data/basis.000000",
550+
CAROM::Database::formats::HDF5, -1,
551+
MPI_COMM_NULL);
552+
std::unique_ptr<CAROM::Matrix> B = reader.getSpatialBasis();
553+
554+
EXPECT_EQ(B->numRows(), 4);
555+
EXPECT_EQ(B->numColumns(), 2);
556+
EXPECT_FALSE(B->distributed());
557+
558+
double error = 0.0;
559+
for (int i=0; i<B->numRows(); ++i)
560+
for (int j=0; j<B->numColumns(); ++j)
561+
{
562+
const double B_ij = (i == 1 && j == 0) || (i == 2 && j == 1) ? -1.0 : 0.0;
563+
error = std::max(error, std::abs(B_ij - (*B)(i, j)));
564+
}
565+
566+
std::unique_ptr<CAROM::Vector> sv = reader.getSingularValues();
567+
EXPECT_EQ(sv->dim(), 2);
568+
569+
for (int i=0; i<sv->dim(); ++i)
570+
error = std::max(error, std::abs((*sv)(i) - 1.0));
571+
572+
EXPECT_NEAR(error, 0.0, threshold);
573+
}
574+
546575
TEST(BasisGeneratorIO, HDFDatabase)
547576
{
548577
// Get the rank of this process, and the number of processors.

0 commit comments

Comments
 (0)