Skip to content

Commit

Permalink
Elimination of raw pointers in BasisReader, BasisGenerator, and SVD c…
Browse files Browse the repository at this point in the history
…lasses.
  • Loading branch information
dylan-copeland committed Aug 1, 2024
1 parent 9b90860 commit ffa9e5a
Show file tree
Hide file tree
Showing 39 changed files with 206 additions and 250 deletions.
5 changes: 3 additions & 2 deletions examples/misc/combine_samples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ int main(int argc, char* argv[])
else {
/*-- load data from hdf5 file to find the mean and subtract it --*/
if (rank==0) std::cout << "Reading snapshots" << std::endl;
CAROM::Matrix* snapshots = (CAROM::Matrix*)
static_basis_generator->getSnapshotMatrix();

CAROM::Matrix *snapshots = (CAROM::Matrix*)
static_basis_generator->getSnapshotMatrix().get();

int num_rows = snapshots->numRows();
int num_cols = snapshots->numColumns();
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/de_parametric_maxwell_greedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ double simulation()
loadBasisName += "_greedy";
}

const CAROM::Matrix* spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
CAROM::Options* options;
CAROM::BasisGenerator *generator;
int numRowRB, numColumnRB;
Expand Down Expand Up @@ -358,7 +358,6 @@ double simulation()

// 24. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 25. Recover the parallel grid function corresponding to X. This is the
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/dg_advection_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ int main(int argc, char *argv[])
bool isIncremental = false;
const std::string basisName = "basis";
const std::string basisFileName = basisName + std::to_string(id);
const CAROM::Matrix* spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
CAROM::Options* options;
CAROM::BasisGenerator *generator;
int numRowRB, numColumnRB;
Expand Down Expand Up @@ -913,7 +913,6 @@ int main(int argc, char *argv[])
if (myid == 0) std::cout << "Relative l2 error of ROM solution " << diffNorm /
fomNorm << std::endl;

delete spatialbasis;
delete M_hat_carom;
delete K_hat_carom;
delete M_hat;
Expand Down
8 changes: 2 additions & 6 deletions examples/prom/dg_advection_local_rom_matrix_interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,13 +722,9 @@ int main(int argc, char *argv[])
{
CAROM::BasisReader reader(basisName);
if (rdim != -1)
{
spatialbasis.reset(reader.getSpatialBasis(rdim));
}
spatialbasis = reader.getSpatialBasis(rdim);
else
{
spatialbasis.reset(reader.getSpatialBasis(ef));
}
spatialbasis = reader.getSpatialBasis(ef);
numRowRB = spatialbasis->numRows();
numColumnRB = spatialbasis->numColumns();
if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB,
Expand Down
8 changes: 1 addition & 7 deletions examples/prom/elliptic_eigenproblem_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,11 @@ int main(int argc, char *argv[])
CAROM::BasisReader reader(basis_filename);

Vector eigenvalues_rom;
const CAROM::Matrix *spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
if (rdim != -1)
{
spatialbasis = reader.getSpatialBasis(rdim);
}
else
{
spatialbasis = reader.getSpatialBasis(ef);
}

const int numRowRB = spatialbasis->numRows();
const int numColumnRB = spatialbasis->numColumns();
Expand Down Expand Up @@ -553,8 +549,6 @@ int main(int argc, char *argv[])
eigenvectors.SetRow(i, eigenvector_carom->getData());
}

delete spatialbasis;

delete A_mat;
delete M_mat;
}
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/grad_div_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ int main(int argc, char *argv[])
// 20. read the reduced basis
assembleTimer.Start();
CAROM::BasisReader reader(basisName);
const CAROM::Matrix* spatialbasis = reader.getSpatialBasis();
std::unique_ptr<const CAROM::Matrix> spatialbasis = reader.getSpatialBasis();
const int numRowRB = spatialbasis->numRows();
const int numColumnRB = spatialbasis->numColumns();
if (myid == 0) printf("spatial basis dimension is %d x %d\n", numRowRB,
Expand All @@ -395,7 +395,6 @@ int main(int argc, char *argv[])

// 23. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 24. Recover the parallel grid function corresponding to X. This is the
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/linear_elasticity_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ int main(int argc, char* argv[])
// 20. read the reduced basis
assembleTimer.Start();
CAROM::BasisReader reader(basisName);
const CAROM::Matrix* spatialbasis = reader.getSpatialBasis();
std::unique_ptr<const CAROM::Matrix> spatialbasis = reader.getSpatialBasis();
numRowRB = spatialbasis->numRows();
numColumnRB = spatialbasis->numColumns();
printf("On rank %d, spatial basis dimension is %d x %d\n", myid,
Expand All @@ -405,7 +405,6 @@ int main(int argc, char* argv[])

// 23. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 24. Recover the parallel grid function corresponding to X. This is the
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/maxwell_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int main(int argc, char *argv[])
bool isIncremental = false;
const std::string basisName = "basis";
const std::string basisFileName = basisName + std::to_string(id);
const CAROM::Matrix* spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
CAROM::Options* options;
CAROM::BasisGenerator *generator;
int numRowRB, numColumnRB;
Expand Down Expand Up @@ -386,7 +386,6 @@ int main(int argc, char *argv[])

// 23. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 24. Recover the parallel grid function corresponding to X. This is the
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/maxwell_local_rom_greedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ int main(int argc, char *argv[])
loadBasisName += "_greedy";
}

const CAROM::Matrix* spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
CAROM::Options* options;
CAROM::BasisGenerator *generator;
int numRowRB, numColumnRB;
Expand Down Expand Up @@ -508,7 +508,6 @@ int main(int argc, char *argv[])

// 24. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 25. Recover the parallel grid function corresponding to X. This is the
Expand Down
8 changes: 4 additions & 4 deletions examples/prom/mixed_nonlinear_diffusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ int main(int argc, char *argv[])
if (online)
{
CAROM::BasisReader readerR("basisR");
BR_librom.reset(readerR.getSpatialBasis());
BR_librom = readerR.getSpatialBasis();
if (rrdim == -1)
rrdim = BR_librom->numColumns();
else
Expand All @@ -871,7 +871,7 @@ int main(int argc, char *argv[])
printf("reduced R dim = %d\n",rrdim);

CAROM::BasisReader readerW("basisW");
BW_librom.reset(readerW.getSpatialBasis());
BW_librom = readerW.getSpatialBasis();
if (rwdim == -1)
rwdim = BW_librom->numColumns();
else
Expand All @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
*/

CAROM::BasisReader readerFR("basisFR");
FR_librom.reset(readerFR.getSpatialBasis());
FR_librom = readerFR.getSpatialBasis();

if (nldim == -1)
{
Expand Down Expand Up @@ -981,7 +981,7 @@ int main(int argc, char *argv[])
if (hyperreduce_source)
{
readerS = new CAROM::BasisReader("basisS");
S_librom.reset(readerS->getSpatialBasis());
S_librom = readerS->getSpatialBasis();

if (nsdim == -1)
{
Expand Down
12 changes: 5 additions & 7 deletions examples/prom/nonlinear_elasticity_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,16 +815,14 @@ int main(int argc, char *argv[])

if (x_base_only)
{
readerV = new
CAROM::BasisReader("basisX"); // The basis for v uses the x basis instead.
// The basis for v uses the x basis instead.
readerV = new CAROM::BasisReader("basisX");
rvdim = rxdim;
}
else
{
readerV = new CAROM::BasisReader("basisV");
}

BV_librom.reset(readerV->getSpatialBasis());
BV_librom = readerV->getSpatialBasis();

if (rvdim == -1) // Change rvdim
rvdim = BV_librom->numColumns();
Expand All @@ -837,7 +835,7 @@ int main(int argc, char *argv[])
printf("reduced V dim = %d\n", rvdim);

CAROM::BasisReader readerX("basisX");
BX_librom.reset(readerX.getSpatialBasis());
BX_librom = readerX.getSpatialBasis();

if (rxdim == -1) // Change rxdim
rxdim = BX_librom->numColumns();
Expand All @@ -851,7 +849,7 @@ int main(int argc, char *argv[])

// Hyper reduce H
CAROM::BasisReader readerH("basisH");
H_librom.reset(readerH.getSpatialBasis());
H_librom = readerH.getSpatialBasis();

// Compute sample points
if (hdim == -1)
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/poisson_global_rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ int main(int argc, char *argv[])
bool isIncremental = false;
const std::string basisName = "basis";
const std::string basisFileName = basisName + std::to_string(id);
const CAROM::Matrix* spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
CAROM::Options* options;
CAROM::BasisGenerator *generator;
int numRowRB, numColumnRB;
Expand Down Expand Up @@ -382,7 +382,6 @@ int main(int argc, char *argv[])

// 23. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 24. Recover the parallel grid function corresponding to X. This is the
Expand Down
3 changes: 1 addition & 2 deletions examples/prom/poisson_local_rom_greedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ int main(int argc, char *argv[])
loadBasisName += "_greedy";
}

const CAROM::Matrix* spatialbasis;
std::unique_ptr<const CAROM::Matrix> spatialbasis;
CAROM::Options* options;
CAROM::BasisGenerator *generator;
int numRowRB, numColumnRB;
Expand Down Expand Up @@ -492,7 +492,6 @@ int main(int argc, char *argv[])

// 24. reconstruct FOM state
spatialbasis->mult(reducedSol, X_carom);
delete spatialbasis;
}

// 25. Recover the parallel grid function corresponding to X. This is the
Expand Down
25 changes: 9 additions & 16 deletions lib/linalg/BasisGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,17 @@ BasisGenerator::BasisGenerator(
if (options.fast_update_brand) {
d_svd.reset(
new IncrementalSVDBrand(
options,
basis_file_name));
options, basis_file_name));
}
else if (options.fast_update) {
d_svd.reset(
new IncrementalSVDFastUpdate(
options,
basis_file_name));
options, basis_file_name));
}
else {
d_svd.reset(
new IncrementalSVDStandard(
options,
basis_file_name));
options, basis_file_name));
}

// Get the number of processors.
Expand All @@ -100,14 +97,10 @@ BasisGenerator::BasisGenerator(
else
{
if (options.randomized) {
d_svd.reset(
new RandomizedSVD(
options));
d_svd.reset(new RandomizedSVD(options));
}
else {
d_svd.reset(
new StaticSVD(
options));
d_svd.reset(new StaticSVD(options));
}
}
}
Expand Down Expand Up @@ -159,8 +152,8 @@ BasisGenerator::loadSampleRange(const std::string& base_file_name,
if (d_basis_reader) delete d_basis_reader;

d_basis_reader = new BasisReader(base_file_name, db_format, d_dim);
const Matrix* mat;
const Vector* singular_vals;
std::unique_ptr<const Matrix> mat;
std::unique_ptr<const Vector> singular_vals;

if (kind == "basis") {
mat = d_basis_reader->getSpatialBasis();
Expand Down Expand Up @@ -221,7 +214,7 @@ BasisGenerator::computeNextSampleTime(
}

// Get the current basis vectors.
const Matrix* basis = getSpatialBasis();
std::shared_ptr<const Matrix> basis = getSpatialBasis();

// Compute l = basis' * u
Vector l(basis->numColumns(), false);
Expand Down Expand Up @@ -309,7 +302,7 @@ BasisGenerator::finalSummary(
const int first_sv)
{
const int rom_dim = getSpatialBasis()->numColumns();
const Vector* sing_vals = getSingularValues();
std::shared_ptr<const Vector> sing_vals = getSingularValues();

CAROM_VERIFY(rom_dim <= sing_vals->dim());

Expand Down
8 changes: 4 additions & 4 deletions lib/linalg/BasisGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BasisGenerator
*
* @return The basis vectors for the current time interval.
*/
const Matrix*
std::shared_ptr<const Matrix>
getSpatialBasis()
{
return d_svd->getSpatialBasis();
Expand All @@ -208,7 +208,7 @@ class BasisGenerator
*
* @return The temporal basis vectors for the current time interval.
*/
const Matrix*
std::shared_ptr<const Matrix>
getTemporalBasis()
{
return d_svd->getTemporalBasis();
Expand All @@ -220,7 +220,7 @@ class BasisGenerator
*
* @return The singular values for the current time interval.
*/
const Vector*
std::shared_ptr<const Vector>
getSingularValues()
{
return d_svd->getSingularValues();
Expand All @@ -231,7 +231,7 @@ class BasisGenerator
*
* @return The snapshot matrix for the current time interval.
*/
const Matrix*
std::shared_ptr<const Matrix>
getSnapshotMatrix()
{
return d_svd->getSnapshotMatrix();
Expand Down
Loading

0 comments on commit ffa9e5a

Please sign in to comment.