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

Add implicit residual log to solvers and benchmarks #714

Merged
merged 4 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions benchmark/solver/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ void solve_system(const std::string &solver_name,
rapidjson::Value(rapidjson::kArrayType), allocator);
add_or_set_member(solver_json, "true_residuals",
rapidjson::Value(rapidjson::kArrayType), allocator);
add_or_set_member(solver_json, "implicit_residuals",
rapidjson::Value(rapidjson::kArrayType), allocator);
add_or_set_member(solver_json, "iteration_timestamps",
rapidjson::Value(rapidjson::kArrayType), allocator);
if (b->get_size()[1] == 1 && !FLAGS_overhead) {
Expand Down Expand Up @@ -457,9 +459,13 @@ void solve_system(const std::string &solver_name,
exec, lend(system_matrix), b,
solver_json["recurrent_residuals"],
solver_json["true_residuals"],
solver_json["implicit_residuals"],
solver_json["iteration_timestamps"], allocator);
solver->add_logger(res_logger);
solver->apply(lend(b), lend(x_clone));
if (!res_logger->has_implicit_res_norms()) {
solver_json.RemoveMember("implicit_residuals");
}
}
exec->synchronize();
}
Expand Down
30 changes: 29 additions & 1 deletion benchmark/utils/loggers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <chrono>
#include <cmath>
#include <mutex>
#include <regex>
#include <unordered_map>
Expand Down Expand Up @@ -202,10 +203,21 @@ template <typename ValueType>
struct ResidualLogger : gko::log::Logger {
using rc_vtype = gko::remove_complex<ValueType>;

void on_iteration_complete(const gko::LinOp *, const gko::size_type &,
// TODO2.0: Remove when deprecating simple overload
void on_iteration_complete(const gko::LinOp *solver,
const gko::size_type &it,
const gko::LinOp *residual,
const gko::LinOp *solution,
const gko::LinOp *residual_norm) const override
{
on_iteration_complete(solver, it, residual, solution, residual_norm,
nullptr);
}

void on_iteration_complete(
const gko::LinOp *, const gko::size_type &, const gko::LinOp *residual,
const gko::LinOp *solution, const gko::LinOp *residual_norm,
const gko::LinOp *implicit_sq_residual_norm) const override
{
timestamps.PushBack(std::chrono::duration<double>(
std::chrono::steady_clock::now() - start)
Expand All @@ -226,12 +238,22 @@ struct ResidualLogger : gko::log::Logger {
} else {
true_res_norms.PushBack(-1.0, alloc);
}
if (implicit_sq_residual_norm) {
implicit_res_norms.PushBack(
std::sqrt(get_norm(
gko::as<vec<rc_vtype>>(implicit_sq_residual_norm))),
alloc);
has_implicit_res_norm = true;
} else {
implicit_res_norms.PushBack(-1.0, alloc);
}
}

ResidualLogger(std::shared_ptr<const gko::Executor> exec,
const gko::LinOp *matrix, const vec<ValueType> *b,
rapidjson::Value &rec_res_norms,
rapidjson::Value &true_res_norms,
rapidjson::Value &implicit_res_norms,
rapidjson::Value &timestamps,
rapidjson::MemoryPoolAllocator<> &alloc)
: gko::log::Logger(exec, gko::log::Logger::iteration_complete_mask),
Expand All @@ -240,16 +262,22 @@ struct ResidualLogger : gko::log::Logger {
start{std::chrono::steady_clock::now()},
rec_res_norms{rec_res_norms},
true_res_norms{true_res_norms},
has_implicit_res_norm{},
implicit_res_norms{implicit_res_norms},
timestamps{timestamps},
alloc{alloc}
{}

bool has_implicit_res_norms() const { return has_implicit_res_norm; }

private:
const gko::LinOp *matrix;
const vec<ValueType> *b;
std::chrono::steady_clock::time_point start;
rapidjson::Value &rec_res_norms;
rapidjson::Value &true_res_norms;
mutable bool has_implicit_res_norm;
rapidjson::Value &implicit_res_norms;
rapidjson::Value &timestamps;
rapidjson::MemoryPoolAllocator<> &alloc;
};
Expand Down
4 changes: 2 additions & 2 deletions core/solver/bicg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ void Bicg<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
z->compute_dot(r2.get(), rho.get());

++iter;
this->template log<log::Logger::iteration_complete>(this, iter, r.get(),
dense_x);
this->template log<log::Logger::iteration_complete>(
this, iter, r.get(), dense_x, nullptr, rho.get());
if (stop_criterion->update()
.num_iterations(iter)
.residual(r.get())
Expand Down
4 changes: 2 additions & 2 deletions core/solver/bicgstab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ void Bicgstab<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
*/
while (true) {
++iter;
this->template log<log::Logger::iteration_complete>(this, iter, r.get(),
dense_x);
this->template log<log::Logger::iteration_complete>(
this, iter, r.get(), dense_x, nullptr, rho.get());
rr->compute_dot(r.get(), rho.get());

if (stop_criterion->update()
Expand Down
4 changes: 2 additions & 2 deletions core/solver/cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ void Cg<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
r->compute_dot(z.get(), rho.get());

++iter;
this->template log<log::Logger::iteration_complete>(this, iter, r.get(),
dense_x);
this->template log<log::Logger::iteration_complete>(
this, iter, r.get(), dense_x, nullptr, rho.get());
if (stop_criterion->update()
.num_iterations(iter)
.residual(r.get())
Expand Down
4 changes: 2 additions & 2 deletions core/solver/cgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ void Cgs<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
alpha.get(), &stop_status));

++iter;
this->template log<log::Logger::iteration_complete>(this, iter, r.get(),
dense_x);
this->template log<log::Logger::iteration_complete>(
this, iter, r.get(), dense_x, nullptr, rho.get());
if (stop_criterion->update()
.num_iterations(iter)
.residual(r.get())
Expand Down
4 changes: 2 additions & 2 deletions core/solver/fcg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ void Fcg<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
t->compute_dot(z.get(), rho_t.get());

++iter;
this->template log<log::Logger::iteration_complete>(this, iter, r.get(),
dense_x);
this->template log<log::Logger::iteration_complete>(
this, iter, r.get(), dense_x, nullptr, rho.get());
if (stop_criterion->update()
.num_iterations(iter)
.residual(r.get())
Expand Down