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

Enable use of external fmt #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions src/polysolve/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "Utils.hpp"

#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

namespace polysolve
{
Expand Down Expand Up @@ -49,7 +53,7 @@ namespace polysolve

void StopWatch::log_msg()
{
const static std::string log_fmt_text =
const static auto log_fmt_text =
fmt::format("[{}] {{}} {{:.3g}}s", fmt::format(fmt::fg(fmt::terminal_color::magenta), "timing"));

if (!m_name.empty())
Expand All @@ -60,7 +64,7 @@ namespace polysolve

void log_and_throw_error(spdlog::logger &logger, const std::string &msg)
{
logger.error(msg);
logger.error("{}", msg);
throw std::runtime_error(msg);
}

Expand All @@ -79,4 +83,4 @@ namespace polysolve
return json[name];
}

} // namespace polysolve
} // namespace polysolve
2 changes: 1 addition & 1 deletion src/polysolve/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace polysolve
template <typename... Args>
[[noreturn]] void log_and_throw_error(spdlog::logger &logger, const std::string &msg, const Args &...args)
{
log_and_throw_error(logger, fmt::format(msg, args...));
log_and_throw_error(logger, fmt::format(fmt::runtime(msg), args...));
}

Eigen::SparseMatrix<double> sparse_identity(int rows, int cols);
Expand Down
51 changes: 23 additions & 28 deletions src/polysolve/nonlinear/Criteria.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

void Criteria::print(std::ostream &os) const
{
os << fmt::format(
os << print_message();

Check warning on line 32 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L32

Added line #L32 was not covered by tests
}
std::string Criteria::print_message() const {
return fmt::format(
"iters={:d} Δf={:g} ‖∇f‖={:g} ‖Δx‖={:g} Δx⋅∇f(x)={:g}",
iterations, fDelta, gradNorm, xDelta, xDeltaDotGrad);
}
Expand Down Expand Up @@ -61,47 +64,39 @@
return Status::Continue;
}

std::ostream &operator<<(std::ostream &os, const Status &s)
{
std::string_view status_message(Status s) {
switch (s)
{
case Status::NotStarted:
os << "Solver not started";
break;
return "Solver not started";

Check warning on line 71 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L71

Added line #L71 was not covered by tests
case Status::Continue:
os << "Convergence criteria not reached";
break;
return "Convergence criteria not reached";

Check warning on line 73 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L73

Added line #L73 was not covered by tests
case Status::IterationLimit:
os << "Iteration limit reached";
break;
return "Iteration limit reached";

Check warning on line 75 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L75

Added line #L75 was not covered by tests
case Status::XDeltaTolerance:
os << "Change in parameter vector too small";
break;
return "Change in parameter vector too small";

Check warning on line 77 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L77

Added line #L77 was not covered by tests
case Status::FDeltaTolerance:
os << "Change in cost function value too small";
break;
return "Change in cost function value too small";

Check warning on line 79 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L79

Added line #L79 was not covered by tests
case Status::GradNormTolerance:
os << "Gradient vector norm too small";
break;
return "Gradient vector norm too small";
case Status::ObjectiveCustomStop:
os << "Objective function specified to stop";
break;
return "Objective function specified to stop";

Check warning on line 83 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L83

Added line #L83 was not covered by tests
case Status::NanEncountered:
os << "Objective or gradient function returned NaN";
break;
return "Objective or gradient function returned NaN";

Check warning on line 85 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L85

Added line #L85 was not covered by tests
case Status::NotDescentDirection:
os << "Search direction not a descent direction";
break;
return "Search direction not a descent direction";
case Status::LineSearchFailed:
os << "Line search failed";
break;
return "Line search failed";

Check warning on line 89 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L89

Added line #L89 was not covered by tests
case Status::UpdateDirectionFailed:
os << "Update direction could not be computed";
break;
return "Update direction could not be computed";
default:
os << "Unknown status";
break;
return "Unknown status";

Check warning on line 93 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L93

Added line #L93 was not covered by tests
}
}

std::ostream &operator<<(std::ostream &os, const Status &s)

Check warning on line 97 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L97

Added line #L97 was not covered by tests
{
os << status_message(s);

Check warning on line 99 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L99

Added line #L99 was not covered by tests
return os;
}

Expand All @@ -110,4 +105,4 @@
c.print(os);
return os;
}
} // namespace polysolve::nonlinear
} // namespace polysolve::nonlinear
7 changes: 6 additions & 1 deletion src/polysolve/nonlinear/Criteria.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstddef>
#include <iostream>
#include <string_view>

namespace polysolve::nonlinear
{
Expand Down Expand Up @@ -43,11 +44,15 @@ namespace polysolve::nonlinear
void reset();

void print(std::ostream &os) const;
std::string print_message() const;
};

Status checkConvergence(const Criteria &stop, const Criteria &current);

std::string_view status_message(Status s);
std::string criteria_message(const Criteria& s);

std::ostream &operator<<(std::ostream &os, const Status &s);

std::ostream &operator<<(std::ostream &os, const Criteria &c);
} // namespace polysolve::nonlinear
} // namespace polysolve::nonlinear
20 changes: 12 additions & 8 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#include <jse/jse.h>

#include <spdlog/spdlog.h>
#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif
#include <spdlog/fmt/ostr.h>

#include <finitediff.hpp>
Expand Down Expand Up @@ -279,7 +283,7 @@

m_logger.debug(
"Starting {} with {} solve f₀={:g} (stopping criteria: {})",
descent_strategy_name(), m_line_search->name(), objFunc(x), m_stop);
descent_strategy_name(), m_line_search->name(), objFunc(x), m_stop.print_message());

update_solver_info(objFunc(x));
objFunc.post_step(PostStepData(m_current.iterations, solver_info, x, grad));
Expand Down Expand Up @@ -350,12 +354,12 @@
m_status = Status::UpdateDirectionFailed;
log_and_throw_error(
m_logger, "[{}][{}] {} on last strategy; stopping",
current_name, m_line_search->name(), m_status);
current_name, m_line_search->name(), status_message(m_status));

Check warning on line 357 in src/polysolve/nonlinear/Solver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Solver.cpp#L357

Added line #L357 was not covered by tests
}

m_logger.debug(
"[{}][{}] {}; reverting to {}", current_name, m_line_search->name(),
Status::UpdateDirectionFailed, descent_strategy_name());
status_message(Status::UpdateDirectionFailed), descent_strategy_name());
m_status = Status::Continue;
continue;
}
Expand All @@ -374,15 +378,15 @@
m_status = Status::NotDescentDirection;
log_and_throw_error(
m_logger, "[{}][{}] {} on last strategy (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); stopping",
current_name, m_line_search->name(), m_status, delta_x.norm(), compute_grad_norm(x, grad),
current_name, m_line_search->name(), status_message(m_status), delta_x.norm(), compute_grad_norm(x, grad),

Check warning on line 381 in src/polysolve/nonlinear/Solver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Solver.cpp#L381

Added line #L381 was not covered by tests
m_current.xDeltaDotGrad);
}
else
{
m_status = Status::Continue;
m_logger.debug(
"[{}][{}] {} (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); reverting to {}",
current_name, m_line_search->name(), Status::NotDescentDirection,
current_name, m_line_search->name(), status_message(Status::NotDescentDirection),
delta_x.norm(), compute_grad_norm(x, grad), m_current.xDeltaDotGrad,
descent_strategy_name());
}
Expand Down Expand Up @@ -473,7 +477,7 @@

m_logger.debug(
"[{}][{}] {} (stopping criteria: {})",
descent_strategy_name(), m_line_search->name(), m_current, m_stop);
descent_strategy_name(), m_line_search->name(), m_current.print_message(), m_stop.print_message());

if (++m_current.iterations >= m_stop.iterations)
m_status = Status::IterationLimit;
Expand All @@ -495,8 +499,8 @@
m_logger.log(
succeeded ? spdlog::level::info : spdlog::level::err,
"[{}][{}] Finished: {} took {:g}s ({}) (stopping criteria: {})",
descent_strategy_name(), m_line_search->name(), m_status, tot_time,
m_current, m_stop);
descent_strategy_name(), m_line_search->name(), status_message(m_status), tot_time,
m_current.print_message(), m_stop.print_message());

log_times();
update_solver_info(objFunc(x));
Expand Down
4 changes: 4 additions & 0 deletions src/polysolve/nonlinear/descent_strategies/Newton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include <polysolve/Utils.hpp>

#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

namespace polysolve::nonlinear
{
Expand Down
4 changes: 4 additions & 0 deletions src/polysolve/nonlinear/line_search/LineSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

#include <polysolve/Types.hpp>

#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

#include <cfenv>

Expand Down