Unverified Commit 83ba2dbd authored by greole's avatar greole Committed by GitHub
Browse files

Reuse init guess (#19)

* create separate IOExecutorHandler class

* add IOExecutorHandler files

* fix compilation issue

* create separate IOSortingIdxHandler class

* Implement separate IOGKOMatrixHandler class

* move host matrix handling to separate class

* fix matrix sorting/updating host matrix

* implement reusable initial guess

* fix host executor call
parent ab78a11e
......@@ -41,12 +41,9 @@ void IOGKOMatrixHandler::init_device_matrix(
return;
}
bool sparsity_pattern_stored =
db.foundObject<regIOobject>(sparsity_pattern_name_cols_);
std::shared_ptr<idx_array> col_idx;
std::shared_ptr<idx_array> row_idx;
if (sparsity_pattern_stored) {
if (sparsity_pattern_stored_) {
io_col_idxs_ptr_ =
&db.lookupObjectRef<GKOIDXIOPtr>(sparsity_pattern_name_cols_);
io_row_idxs_ptr_ =
......@@ -95,4 +92,6 @@ void IOGKOMatrixHandler::init_device_matrix(
defineTemplateTypeNameWithName(GKOIDXIOPtr, "IDXIOPtr");
defineTemplateTypeNameWithName(GKOCOOIOPtr, "COOIOPtr");
defineTemplateTypeNameWithName(GKOVECIOPtr, "VECIOPtr");
} // namespace Foam
......@@ -36,6 +36,7 @@ SourceFiles
namespace Foam {
using mtx = gko::matrix::Coo<scalar>;
using vec = gko::matrix::Dense<scalar>;
using val_array = gko::Array<scalar>;
using idx_array = gko::Array<label>;
......@@ -45,11 +46,19 @@ private:
const bool sys_matrix_stored_;
const word sparsity_pattern_name_cols_ = "gko_sparsity_pattern_cols";
const word update_sysMatrix_;
const word sparsity_pattern_name_rows_ = "gko_sparsity_pattern_rows";
const word sparsity_pattern_name_cols_;
const word update_sysMatrix_;
const word sparsity_pattern_name_rows_;
const bool sparsity_pattern_stored_;
const word init_guess_vector_name_;
const bool init_guess_vector_stored_;
const bool update_init_guess_vector_;
const bool export_;
......@@ -61,6 +70,10 @@ private:
mutable GKOIDXIOPtr *io_row_idxs_ptr_ = NULL;
mutable std::shared_ptr<vec> init_guess_ = NULL;
mutable GKOVECIOPtr *io_init_guess_ptr_ = NULL;
public:
IOGKOMatrixHandler(const objectRegistry &db, const dictionary &controlDict,
const word sys_matrix_name)
......@@ -69,6 +82,16 @@ public:
sys_matrix_stored_(db.foundObject<regIOobject>(sys_matrix_name_)),
update_sysMatrix_(
controlDict.lookupOrDefault("updateSysMatrix", word("yes"))),
sparsity_pattern_name_cols_("gko_sparsity_pattern_cols"),
sparsity_pattern_name_rows_("gko_sparsity_pattern_rows"),
sparsity_pattern_stored_(
db.foundObject<regIOobject>(sparsity_pattern_name_cols_)),
init_guess_vector_name_(sys_matrix_name_ + "_init_guess"),
init_guess_vector_stored_(
db.foundObject<regIOobject>(init_guess_vector_name_)),
update_init_guess_vector_(
controlDict.lookupOrDefault<Switch>("updateInitVector", false)),
export_(controlDict.lookupOrDefault<Switch>("export", false)){};
bool get_sys_matrix_stored() const { return sys_matrix_stored_; };
......@@ -88,6 +111,67 @@ public:
return gkomatrix_ptr_->get_ptr();
};
void init_initial_guess(const scalarField &psi, const objectRegistry &db,
const label nCells) const
{
std::shared_ptr<gko::Executor> device_exec = get_device_executor();
if (init_guess_vector_stored_ && !update_init_guess_vector_) {
std::cout << " init_initial_guess do nothing" << std::endl;
io_init_guess_ptr_ =
&db.lookupObjectRef<GKOVECIOPtr>(init_guess_vector_name_);
return;
}
std::cout << " init_initial_guess create psi_view" << std::endl;
auto psi_view = val_array::view(gko::ReferenceExecutor::create(),
nCells, const_cast<scalar *>(&psi[0]));
std::cout << " init_initial_guess create x" << std::endl;
auto x = gko::share(
vec::create(device_exec, gko::dim<2>(nCells, 1), psi_view, 1));
const fileName path_init_guess = init_guess_vector_name_;
std::cout << " init_initial_guess create io_init_guess_ptr_"
<< std::endl;
io_init_guess_ptr_ = new GKOVECIOPtr(IOobject(path_init_guess, db), x);
}
std::shared_ptr<vec> get_initial_guess() const
{
std::cout << "get_initial_guess" << std::endl;
if (io_init_guess_ptr_ == NULL) {
std::cout << "is Null" << std::endl;
return init_guess_;
}
return io_init_guess_ptr_->get_ptr();
}
void copy_result_back(const scalarField &psi, const label nCells) const
{
std::cout << "copy back" << std::endl;
auto device_x = vec::create(ref_exec(), nCells);
std::cout << "copy back 2" << std::endl;
device_x->copy_from(gko::lend(get_initial_guess()));
std::cout << "create x_view back" << std::endl;
auto x_view =
val_array::view(ref_exec(), nCells, device_x->get_values());
// for (label i = 0; i < nCells; i++) {
// std::cout << x_view[i] << std::endl;
// }
// move frome device
std::cout << "create psi_view " << std::endl;
auto psi_view =
val_array::view(ref_exec(), nCells, const_cast<scalar *>(&psi[0]));
std::cout << "copy x_view to psi_view " << std::endl;
psi_view = x_view;
}
bool get_update_sys_matrix() const { return (update_sysMatrix_ == "yes"); }
bool get_export() const { return export_; }
......
......@@ -62,6 +62,8 @@ typedef IOPtr<gko::OmpExecutor> GKOOmpExecPtr;
typedef IOPtr<gko::HipExecutor> GKOHipExecPtr;
typedef IOPtr<idx_array> GKOIDXIOPtr;
typedef IOPtr<gko::matrix::Coo<scalar>> GKOCOOIOPtr;
typedef IOPtr<gko::matrix::Dense<scalar>> GKOVECIOPtr;
} // namespace Foam
......
......@@ -127,14 +127,16 @@ public:
matrix_.Amul(wA, psi, interfaceBouCoeffs_, interfaces_, cmpt);
scalar norm_factor = this->normFactor(psi, source, wA, pA);
auto psi_view = val_array::view(ref_exec(), nCells(),
const_cast<scalar *>(&psi[0]));
auto source_view = val_array::view(ref_exec(), nCells(),
const_cast<scalar *>(&source[0]));
auto b =
vec::create(ref_exec(), gko::dim<2>(nCells(), 1), source_view, 1);
auto x = vec::create(ref_exec(), gko::dim<2>(nCells(), 1), psi_view, 1);
std::cout << "solve_impl init_initial_guess" << std::endl;
init_initial_guess(psi, matrix().mesh().thisDb(), nCells());
std::cout << "solve_impl init_initial_guess done" << std::endl;
auto x = this->get_initial_guess();
std::cout << "solve_impl get_initial_guess done" << std::endl;
std::vector<std::shared_ptr<const gko::stop::CriterionFactory>>
criterion_vec{};
......@@ -153,7 +155,8 @@ public:
// from this factory.
solver_gen->add_logger(logger);
auto b_clone = gko::clone(b);
// auto b_clone = gko::clone(b);
std::shared_ptr<mtx> gkomatrix = get_gkomatrix();
if (get_export())
......@@ -168,11 +171,9 @@ public:
// Solve system
SIMPLE_TIME(solve, solver->apply(gko::lend(b), gko::lend(x));)
auto x_view = val_array::view(ref_exec(), nCells(), x->get_values());
auto x_clone =
vec::create(ref_exec(), gko::dim<2>(nCells(), 1), x_view, 1);
psi_view = x_view;
b_clone->copy_from(gko::lend(b));
this->copy_result_back(psi, nCells());
// b_clone->copy_from(gko::lend(b));
solverPerf.finalResidual() = 0.0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment