Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created and tested bindings for hyperreduction #7

Merged
merged 12 commits into from
Aug 30, 2023
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ jobs:
pytest test_pyBasisReader.py --verbose
echo run pyBasisWriter unit test
pytest test_pyBasisWriter.py --verbose
echo run pyDEIM unit test
pytest test_pyDEIM.py --verbose
echo run pyGNAT unit test
pytest test_pyGNAT.py --verbose
echo run pyQDEIM unit test
pytest test_pyQDEIM.py --verbose
echo run pyS_OPT unit test
pytest test_pyS_OPT.py --verbose
echo run pySTSampling unit test
pytest test_pySTSampling.py --verbose
echo run pyUtilities unit test
pytest test_pyUtilities.py --verbose
cmake-with-librom:
runs-on: ubuntu-latest
needs: [docker-librom-image]
Expand Down Expand Up @@ -115,6 +127,18 @@ jobs:
pytest test_pyBasisReader.py --verbose
echo run pyBasisWriter unit test
pytest test_pyBasisWriter.py --verbose
echo run pyDEIM unit test
pytest test_pyDEIM.py --verbose
echo run pyGNAT unit test
pytest test_pyGNAT.py --verbose
echo run pyQDEIM unit test
pytest test_pyQDEIM.py --verbose
echo run pyS_OPT unit test
pytest test_pyS_OPT.py --verbose
echo run pySTSampling unit test
pytest test_pySTSampling.py --verbose
echo run pyUtilities unit test
pytest test_pyUtilities.py --verbose
baseline:
runs-on: ubuntu-latest
needs: [docker-base-image]
Expand Down Expand Up @@ -164,4 +188,16 @@ jobs:
pytest test_pyBasisReader.py --verbose
echo run pyBasisWriter unit test
pytest test_pyBasisWriter.py --verbose
echo run pyDEIM unit test
pytest test_pyDEIM.py --verbose
echo run pyGNAT unit test
pytest test_pyGNAT.py --verbose
echo run pyQDEIM unit test
pytest test_pyQDEIM.py --verbose
echo run pyS_OPT unit test
pytest test_pyS_OPT.py --verbose
echo run pySTSampling unit test
pytest test_pySTSampling.py --verbose
echo run pyUtilities unit test
pytest test_pyUtilities.py --verbose

9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ pybind11_add_module(_pylibROM

bindings/pylibROM/algo/pyDMD.cpp
bindings/pylibROM/algo/pyParametricDMD.cpp


bindings/pylibROM/hyperreduction/pyDEIM.cpp
bindings/pylibROM/hyperreduction/pyGNAT.cpp
bindings/pylibROM/hyperreduction/pyQDEIM.cpp
bindings/pylibROM/hyperreduction/pyS_OPT.cpp
bindings/pylibROM/hyperreduction/pySTSampling.cpp
bindings/pylibROM/hyperreduction/pyUtilities.cpp

bindings/pylibROM/utils/mpi_utils.cpp
bindings/pylibROM/utils/pyDatabase.hpp
bindings/pylibROM/utils/pyDatabase.cpp
Expand Down
4 changes: 4 additions & 0 deletions bindings/pylibROM/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# from . import linalg
# from . import algo
# from . import utils
# from . import hyperreduction
# To add pure python routines to this module,
# either define/import the python routine in this file.
# This will combine both c++ bindings/pure python routines into this module.
Expand Down
1 change: 1 addition & 0 deletions bindings/pylibROM/hyperreduction/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from _pylibROM.hyperreduction import *
22 changes: 22 additions & 0 deletions bindings/pylibROM/hyperreduction/pyDEIM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include "hyperreduction/DEIM.h"
#include <vector>
#include "linalg/Matrix.h"

namespace py = pybind11;
using namespace CAROM;


void init_DEIM(pybind11::module_ &m) {
m.def("DEIM", [](const Matrix* f_basis,int num_f_basis_vectors_used,
Matrix& f_basis_sampled_inv,int myid,int num_procs) {
int num_basis_vectors = std::min(num_f_basis_vectors_used, f_basis->numColumns());
std::vector<int> f_sampled_row(num_basis_vectors);
std::vector<int> f_sampled_rows_per_proc(num_procs);
DEIM(f_basis, num_f_basis_vectors_used,f_sampled_row, f_sampled_rows_per_proc,f_basis_sampled_inv, myid, num_procs);
return std::make_tuple(std::move(f_sampled_row), std::move(f_sampled_rows_per_proc));
});
}
34 changes: 34 additions & 0 deletions bindings/pylibROM/hyperreduction/pyGNAT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include "hyperreduction/GNAT.h"
#include "linalg/Matrix.h"

namespace py = pybind11;
using namespace CAROM;

void init_GNAT(pybind11::module_ &m) {
m.def("GNAT", [](const Matrix* f_basis,
const int num_f_basis_vectors_used,
Matrix& f_basis_sampled_inv,
const int myid,
const int num_procs,
const int num_samples_req,
std::vector<int>& init_samples){

const int num_basis_vectors = std::min(num_f_basis_vectors_used, f_basis->numColumns());
const int num_samples = num_samples_req > 0 ? num_samples_req : num_basis_vectors;
std::vector<int> f_sampled_row(num_samples);
std::vector<int> f_sampled_rows_per_proc(num_procs);
GNAT(f_basis, num_f_basis_vectors_used, f_sampled_row, f_sampled_rows_per_proc,
f_basis_sampled_inv, myid, num_procs, num_samples_req,&init_samples);
return std::make_tuple(std::move(f_sampled_row), std::move(f_sampled_rows_per_proc));
},py::arg("f_basis"),
py::arg("num_f_basis_vectors_used"),
py::arg("f_basis_sampled_inv"),
py::arg("myid"),
py::arg("num_procs"),
py::arg("num_samples_req") = -1,
py::arg("init_samples") = std::vector<int>(0));
}
21 changes: 21 additions & 0 deletions bindings/pylibROM/hyperreduction/pyQDEIM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include "hyperreduction/QDEIM.h"
#include <vector>
#include "linalg/Matrix.h"

namespace py = pybind11;
using namespace CAROM;

void init_QDEIM(pybind11::module_ &m) {
m.def("QDEIM", [](const Matrix* f_basis,int num_f_basis_vectors_used,
Matrix& f_basis_sampled_inv,const int myid,const int num_procs,const int num_samples_req) {
std::vector<int> f_sampled_row(num_samples_req);
std::vector<int> f_sampled_rows_per_proc(num_procs);
QDEIM(f_basis, num_f_basis_vectors_used,f_sampled_row, f_sampled_rows_per_proc,f_basis_sampled_inv, myid, num_procs,num_samples_req);
return std::make_tuple(std::move(f_sampled_row), std::move(f_sampled_rows_per_proc));
});
}

39 changes: 39 additions & 0 deletions bindings/pylibROM/hyperreduction/pySTSampling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include "librom.h"
#include "python_utils/cpp_utils.hpp"

namespace py = pybind11;
using namespace CAROM;


void init_STSampling(pybind11::module_ &m) {
m.def("SpaceTimeSampling", [](const Matrix* s_basis,
const Matrix* t_basis,
const int num_f_basis_vectors_used,
py::array_t<int>& f_sampled_row,
py::array_t<int>& f_sampled_rows_per_proc,
Matrix& s_basis_sampled,
const int myid,
const int num_procs,
const int num_t_samples_req = -1,
const int num_s_samples_req = -1,
const bool excludeFinalTime = false) {
std::vector<int> t_samples(num_t_samples_req);
SpaceTimeSampling(s_basis, t_basis,num_f_basis_vectors_used,t_samples,getVectorPointer(f_sampled_row), getVectorPointer(f_sampled_rows_per_proc), s_basis_sampled, myid, num_procs, num_t_samples_req,num_s_samples_req,excludeFinalTime);
return std::move(t_samples);
}, py::arg("s_basis"), py::arg("t_basis"), py::arg("num_f_basis_vectors_used"),
py::arg("f_sampled_row"), py::arg("f_sampled_rows_per_proc"),
py::arg("s_basis_sampled"), py::arg("myid"), py::arg("num_procs"),
py::arg("num_t_samples_req") = -1, py::arg("num_s_samples_req") = -1,
py::arg("excludeFinalTime") = false);

m.def("GetSampledSpaceTimeBasis", [](std::vector<int> const& t_samples,
const Matrix* t_basis,
Matrix const& s_basis_sampled,
Matrix& f_basis_sampled_inv) {
GetSampledSpaceTimeBasis(t_samples,t_basis,s_basis_sampled,f_basis_sampled_inv);
});
}
47 changes: 47 additions & 0 deletions bindings/pylibROM/hyperreduction/pyS_OPT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include <vector>
#include "hyperreduction/S_OPT.h"
#include "linalg/Matrix.h"

namespace py = pybind11;
using namespace CAROM;

void init_S_OPT(pybind11::module_ &m) {
m.def("S_OPT", [](
const Matrix* f_basis,
int num_f_basis_vectors_used,
Matrix& f_basis_sampled_inv,
const int myid,
const int num_procs,
const int num_samples_req,
std::vector<int> &init_samples,
bool qr_factorize) {

// S_OPT checks if f_sample_row and f_sample_rows_per_proc have the right sizes.
// Initialize them with the right size.
const int num_basis_vectors =
std::min(num_f_basis_vectors_used, f_basis->numColumns());
const int num_samples = num_samples_req > 0 ? num_samples_req :
num_basis_vectors;
std::vector<int> f_sampled_row(num_samples);
std::vector<int> f_sampled_rows_per_proc(num_procs);

S_OPT(f_basis, num_f_basis_vectors_used, f_sampled_row,
f_sampled_rows_per_proc, f_basis_sampled_inv, myid,
num_procs, num_samples_req, &init_samples, qr_factorize);

return std::make_tuple(std::move(f_sampled_row),
std::move(f_sampled_rows_per_proc));
},
py::arg("f_basis"),
py::arg("num_f_basis_vectors_used"),
py::arg("f_basis_sampled_inv"),
py::arg("myid"),
py::arg("num_procs"),
py::arg("num_samples_req") = -1,
py::arg("init_samples") = std::vector<int>(0),
py::arg("qr_factorize") = false);
}
18 changes: 18 additions & 0 deletions bindings/pylibROM/hyperreduction/pyUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include "hyperreduction/Utilities.h"
#include "mpi.h"


namespace py = pybind11;
using namespace CAROM;

void init_Utilities(pybind11::module_ &m) {
py::class_<RowInfo>(m, "RowInfo")
.def(py::init<>())
.def_readwrite("row_val", &RowInfo::row_val)
.def_readwrite("row", &RowInfo::row)
.def_readwrite("proc", &RowInfo::proc);
}
15 changes: 15 additions & 0 deletions bindings/pylibROM/pylibROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ void init_IncrementalSVD(pybind11::module_ &m);
void init_DMD(pybind11::module_ &);
void init_ParametricDMD(pybind11::module_ &m);

//hyperreduction
void init_DEIM(pybind11::module_ &m);
void init_GNAT(pybind11::module_ &m);
void init_QDEIM(pybind11::module_ &m);
void init_S_OPT(pybind11::module_ &m);
void init_STSampling(pybind11::module_ &m);
void init_Utilities(pybind11::module_ &m);

//utils
void init_mpi_utils(pybind11::module_ &m);
void init_Database(pybind11::module_ &m);
Expand Down Expand Up @@ -57,6 +65,13 @@ PYBIND11_MODULE(_pylibROM, m) {
init_DMD(algo);
init_ParametricDMD(algo);

py::module hyperreduction = m.def_submodule("hyperreduction");
init_DEIM(hyperreduction);
init_GNAT(hyperreduction);
init_QDEIM(hyperreduction);
init_S_OPT(hyperreduction);
init_STSampling(hyperreduction);
init_Utilities(hyperreduction);
// py::module mfem = m.def_submodule("mfem");
// init_mfem_Utilities(mfem);

Expand Down
1 change: 0 additions & 1 deletion bindings/pylibROM/python_utils/cpp_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ T* getVectorPointer(py::array_t<T> &u_in)

return static_cast<T*>(buf_info.ptr);
}

#endif
18 changes: 14 additions & 4 deletions tests/test_BasisGenerator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import sys

sys.path.append("../../build")
import pylibROM.linalg as libROM
from pylibROM.utils import Database
import pytest
try:
# import pip-installed package
import pylibROM
import pylibROM.linalg as libROM
from pylibROM.utils import Database
except ModuleNotFoundError:
# If pip-installed package is not found, import cmake-built package
sys.path.append("../build")
import _pylibROM as pylibROM
import _pylibROM.linalg as libROM
from _pylibROM.utils import Database
import numpy as np
import h5py


# Create an instance of BasisGenerator
options = libROM.Options(4, 20, 3, True, True)
Expand Down
Loading