|
| 1 | +/* |
| 2 | + * Copyright (C) 2018, 2019 David Cattermole. |
| 3 | + * |
| 4 | + * This file is part of mmSolver. |
| 5 | + * |
| 6 | + * mmSolver is free software: you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Lesser General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * mmSolver is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with mmSolver. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + * ==================================================================== |
| 19 | + * |
| 20 | + * Logging functions for the solver to the terminal/Maya Output |
| 21 | + * Window. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "adjust_solveFunc.h" |
| 25 | + |
| 26 | +// Maya |
| 27 | +#include <maya/MStreamUtils.h> |
| 28 | + |
| 29 | +// MM Solver |
| 30 | +#include "adjust_data.h" |
| 31 | +#include "adjust_results.h" |
| 32 | +#include "mmSolver/utilities/assert_utils.h" |
| 33 | +#include "mmSolver/utilities/debug_utils.h" |
| 34 | +#include "mmSolver/utilities/number_utils.h" |
| 35 | +#include "mmSolver/utilities/string_utils.h" |
| 36 | + |
| 37 | +namespace mmsolver { |
| 38 | + |
| 39 | +const char *const LOG_SOLVER_RETURN_SUCCESS = "Solver returned SUCCESS | "; |
| 40 | +const char *const LOG_SOLVER_RETURN_FAILURE = "Solver returned FAILURE | "; |
| 41 | +const char *const LOG_SOLVER_ITERATION_RESIDUAL_ERROR_FORMAT = |
| 42 | + " | error avg %8.4f min %8.4f max %8.4f"; |
| 43 | +const char *const LOG_SOLVER_END_RESIDUAL_ERROR_FORMAT = |
| 44 | + "error avg %8.4f min %8.4f max %8.4f iterations %03u (%s evals/sec)"; |
| 45 | + |
| 46 | +void log_solver_iteration_pre_solve(const LogLevel log_level, |
| 47 | + const bool is_normal_call, |
| 48 | + const bool is_jacobian_call, |
| 49 | + const bool do_calc_jacobian, |
| 50 | + const int32_t iter_num, |
| 51 | + const int32_t func_eval_num, |
| 52 | + const int32_t jac_iter_num) { |
| 53 | + const bool verbose = false; |
| 54 | + MMSOLVER_MAYA_VRB("adjust_logging log_solver_iteration_pre_solve"); |
| 55 | + |
| 56 | + if (is_normal_call) { |
| 57 | + if (log_level >= LOG_LEVEL_PRINT_NORMAL_ITERATIONS) { |
| 58 | + MStreamUtils::stdErrorStream() << "Iteration "; |
| 59 | + MStreamUtils::stdErrorStream() |
| 60 | + << std::right << std::setfill('0') << std::setw(4) << iter_num; |
| 61 | + MStreamUtils::stdErrorStream() << " | Eval "; |
| 62 | + MStreamUtils::stdErrorStream() << std::right << std::setfill('0') |
| 63 | + << std::setw(4) << func_eval_num; |
| 64 | + } |
| 65 | + } else if (is_jacobian_call && !do_calc_jacobian) { |
| 66 | + if (log_level >= LOG_LEVEL_PRINT_JACOBIAN_ITERATIONS) { |
| 67 | + MStreamUtils::stdErrorStream() << "Jacobian "; |
| 68 | + MStreamUtils::stdErrorStream() << std::right << std::setfill('0') |
| 69 | + << std::setw(4) << jac_iter_num; |
| 70 | + MStreamUtils::stdErrorStream() << " | Eval "; |
| 71 | + MStreamUtils::stdErrorStream() << std::right << std::setfill('0') |
| 72 | + << std::setw(4) << func_eval_num; |
| 73 | + if (do_calc_jacobian) { |
| 74 | + MStreamUtils::stdErrorStream() << "\n"; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void log_solver_iteration_post_solve( |
| 81 | + const LogLevel log_level, const bool is_normal_call, |
| 82 | + const bool is_jacobian_call, const bool do_calc_jacobian, |
| 83 | + const double error_avg, const double error_min, const double error_max) { |
| 84 | + const bool verbose = false; |
| 85 | + MMSOLVER_MAYA_VRB("adjust_logging log_solver_iteration_post_solve"); |
| 86 | + |
| 87 | + if (is_normal_call) { |
| 88 | + if (log_level >= LOG_LEVEL_PRINT_NORMAL_ITERATIONS) { |
| 89 | + char formatBuffer[128]; |
| 90 | + sprintf(formatBuffer, LOG_SOLVER_ITERATION_RESIDUAL_ERROR_FORMAT, |
| 91 | + error_avg, error_min, error_max); |
| 92 | + MStreamUtils::stdErrorStream() << std::string(formatBuffer) << "\n"; |
| 93 | + } |
| 94 | + } else { |
| 95 | + if (log_level >= LOG_LEVEL_PRINT_JACOBIAN_ITERATIONS) { |
| 96 | + if (!do_calc_jacobian) { |
| 97 | + MStreamUtils::stdErrorStream() << "\n"; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void log_solver_results(const SolverResult &solverResult, |
| 104 | + const SolverTimer &timer) { |
| 105 | + if (solverResult.success) { |
| 106 | + MStreamUtils::stdErrorStream() << LOG_SOLVER_RETURN_SUCCESS; |
| 107 | + } else { |
| 108 | + MStreamUtils::stdErrorStream() << LOG_SOLVER_RETURN_FAILURE; |
| 109 | + } |
| 110 | + |
| 111 | + double seconds = mmsolver::debug::timestamp_as_seconds( |
| 112 | + mmsolver::debug::get_timestamp() - timer.startTimestamp); |
| 113 | + seconds = std::max(1e-9, seconds); |
| 114 | + auto evals_per_sec = static_cast<size_t>( |
| 115 | + static_cast<double>(solverResult.functionEvals) / seconds); |
| 116 | + std::string evals_per_sec_string = |
| 117 | + mmstring::numberToStringWithCommas(evals_per_sec); |
| 118 | + |
| 119 | + const auto solverResult_iterations = |
| 120 | + static_cast<uint32_t>(solverResult.iterations); |
| 121 | + |
| 122 | + const size_t buffer_size = 128; |
| 123 | + char formatBuffer[buffer_size]; |
| 124 | + std::snprintf(formatBuffer, buffer_size, |
| 125 | + LOG_SOLVER_END_RESIDUAL_ERROR_FORMAT, solverResult.errorAvg, |
| 126 | + solverResult.errorMin, solverResult.errorMax, |
| 127 | + solverResult_iterations, &evals_per_sec_string[0]); |
| 128 | + // Note: We use std::endl to flush the stream, and ensure an |
| 129 | + // update for the user. |
| 130 | + MStreamUtils::stdErrorStream() << formatBuffer << std::endl; |
| 131 | +} |
| 132 | + |
| 133 | +void log_solver_timer(const SolverTimer &timer, |
| 134 | + const uint32_t total_iteration_count) { |
| 135 | + static std::ostream &stream = MStreamUtils::stdErrorStream(); |
| 136 | + timer.solveBenchTimer.print(stream, "Solve Time", 1); |
| 137 | + timer.funcBenchTimer.print(stream, "Func Time", 1); |
| 138 | + timer.jacBenchTimer.print(stream, "Jacobian Time", 1); |
| 139 | + timer.paramBenchTimer.print(stream, "Param Time", total_iteration_count); |
| 140 | + timer.errorBenchTimer.print(stream, "Error Time", total_iteration_count); |
| 141 | + timer.funcBenchTimer.print(stream, "Func Time", total_iteration_count); |
| 142 | + |
| 143 | + timer.solveBenchTicks.print(stream, "Solve Ticks", 1); |
| 144 | + timer.funcBenchTicks.print(stream, "Func Ticks", 1); |
| 145 | + timer.jacBenchTicks.print(stream, "Jacobian Ticks", 1); |
| 146 | + timer.paramBenchTicks.print(stream, "Param Ticks", total_iteration_count); |
| 147 | + timer.errorBenchTicks.print(stream, "Error Ticks", total_iteration_count); |
| 148 | + timer.funcBenchTicks.print(stream, "Func Ticks", total_iteration_count); |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace mmsolver |
0 commit comments