From f9fde04644b61fcac548c7877dbd7fefc977afc2 Mon Sep 17 00:00:00 2001 From: Huangzizhou Date: Thu, 7 Dec 2023 14:43:49 -0500 Subject: [PATCH 1/4] cache hessian in RegularizedNewton --- src/polysolve/nonlinear/descent_strategies/Newton.cpp | 9 +++++++-- src/polysolve/nonlinear/descent_strategies/Newton.hpp | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/polysolve/nonlinear/descent_strategies/Newton.cpp b/src/polysolve/nonlinear/descent_strategies/Newton.cpp index 8beee993..f3bdc143 100644 --- a/src/polysolve/nonlinear/descent_strategies/Newton.cpp +++ b/src/polysolve/nonlinear/descent_strategies/Newton.cpp @@ -243,8 +243,13 @@ namespace polysolve::nonlinear polysolve::StiffnessMatrix &hessian) { - objFunc.set_project_to_psd(true); - objFunc.hessian(x, hessian); + if (x.size() != x_cache.size() || x != x_cache) + { + objFunc.set_project_to_psd(true); + objFunc.hessian(x, hessian_cache); + x_cache = x; + } + hessian = hessian_cache; if (reg_weight > 0) { hessian += reg_weight * sparse_identity(hessian.rows(), hessian.cols()); diff --git a/src/polysolve/nonlinear/descent_strategies/Newton.hpp b/src/polysolve/nonlinear/descent_strategies/Newton.hpp index 74e809c9..5cb7a8de 100644 --- a/src/polysolve/nonlinear/descent_strategies/Newton.hpp +++ b/src/polysolve/nonlinear/descent_strategies/Newton.hpp @@ -114,6 +114,9 @@ namespace polysolve::nonlinear double reg_weight_max; double reg_weight_inc; + TVector x_cache; + polysolve::StiffnessMatrix hessian_cache; + double reg_weight; ///< Regularization Coefficients protected: void compute_hessian(Problem &objFunc, From 6be499c570b06be50ca048325deafe4c131d26f6 Mon Sep 17 00:00:00 2001 From: Huangzizhou Date: Thu, 7 Dec 2023 16:28:40 -0500 Subject: [PATCH 2/4] add allow_out_of_iterations --- src/polysolve/nonlinear/Solver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/polysolve/nonlinear/Solver.cpp b/src/polysolve/nonlinear/Solver.cpp index daf8811e..25d9540e 100644 --- a/src/polysolve/nonlinear/Solver.cpp +++ b/src/polysolve/nonlinear/Solver.cpp @@ -119,8 +119,8 @@ namespace polysolve::nonlinear this->setStopCriteria(criteria); use_grad_norm_tol = solver_params["line_search"]["use_grad_norm_tol"]; - first_grad_norm_tol = solver_params["first_grad_norm_tol"]; + allow_out_of_iterations = solver_params["allow_out_of_iterations"]; use_grad_norm_tol *= characteristic_length; first_grad_norm_tol *= characteristic_length; From eab76b4f5fbf67823d7491a3fd6818635032f32e Mon Sep 17 00:00:00 2001 From: teseoch Date: Thu, 7 Dec 2023 13:56:35 -0800 Subject: [PATCH 3/4] added class for post step (#54) * added class for post step --- src/polysolve/nonlinear/CMakeLists.txt | 2 ++ src/polysolve/nonlinear/PostStepData.cpp | 11 +++++++++++ src/polysolve/nonlinear/PostStepData.hpp | 19 +++++++++++++++++++ src/polysolve/nonlinear/Problem.hpp | 4 +++- src/polysolve/nonlinear/Solver.cpp | 6 ++++-- 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/polysolve/nonlinear/PostStepData.cpp create mode 100644 src/polysolve/nonlinear/PostStepData.hpp diff --git a/src/polysolve/nonlinear/CMakeLists.txt b/src/polysolve/nonlinear/CMakeLists.txt index fc46887d..c842dbe3 100644 --- a/src/polysolve/nonlinear/CMakeLists.txt +++ b/src/polysolve/nonlinear/CMakeLists.txt @@ -5,6 +5,8 @@ set(SOURCES Solver.cpp Problem.hpp Problem.cpp + PostStepData.hpp + PostStepData.cpp ) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "Source Files" FILES ${SOURCES}) diff --git a/src/polysolve/nonlinear/PostStepData.cpp b/src/polysolve/nonlinear/PostStepData.cpp new file mode 100644 index 00000000..a411dce4 --- /dev/null +++ b/src/polysolve/nonlinear/PostStepData.cpp @@ -0,0 +1,11 @@ +#include "PostStepData.hpp" + +namespace polysolve::nonlinear +{ + PostStepData::PostStepData(const int iter_num, + const Eigen::VectorXd &x, + const Eigen::VectorXd &grad) + : iter_num(iter_num), x(x), grad(grad) + { + } +} // namespace polysolve::nonlinear diff --git a/src/polysolve/nonlinear/PostStepData.hpp b/src/polysolve/nonlinear/PostStepData.hpp new file mode 100644 index 00000000..dc5ccdba --- /dev/null +++ b/src/polysolve/nonlinear/PostStepData.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace polysolve::nonlinear +{ + + class PostStepData + { + public: + PostStepData(const int iter_num, + const Eigen::VectorXd &x, + const Eigen::VectorXd &grad); + + const int iter_num; + const Eigen::VectorXd &x; + const Eigen::VectorXd &grad; + }; +} // namespace polysolve::nonlinear diff --git a/src/polysolve/nonlinear/Problem.hpp b/src/polysolve/nonlinear/Problem.hpp index 6cb15a3e..33e535c1 100644 --- a/src/polysolve/nonlinear/Problem.hpp +++ b/src/polysolve/nonlinear/Problem.hpp @@ -2,6 +2,8 @@ #include +#include "PostStepData.hpp" + #include #include @@ -33,7 +35,7 @@ namespace polysolve::nonlinear virtual void line_search_begin(const TVector &x0, const TVector &x1) {} virtual void line_search_end() {} - virtual void post_step(const int iter_num, const TVector &x) {} + virtual void post_step(const PostStepData &data) {} virtual void set_project_to_psd(bool val) {} diff --git a/src/polysolve/nonlinear/Solver.cpp b/src/polysolve/nonlinear/Solver.cpp index 5e995f99..38d34f87 100644 --- a/src/polysolve/nonlinear/Solver.cpp +++ b/src/polysolve/nonlinear/Solver.cpp @@ -1,6 +1,8 @@ #include "Solver.hpp" +#include "PostStepData.hpp" + #include "descent_strategies/BFGS.hpp" #include "descent_strategies/Newton.hpp" #include "descent_strategies/GradientDescent.hpp" @@ -191,7 +193,7 @@ namespace polysolve::nonlinear objFunc.solution_changed(x); } - objFunc.post_step(this->m_current.iterations, x); + objFunc.post_step(PostStepData(this->m_current.iterations, x, grad)); const auto g_norm_tol = this->m_stop.gradNorm; this->m_stop.gradNorm = first_grad_norm_tol; @@ -371,7 +373,7 @@ namespace polysolve::nonlinear m_logger.debug("[{}][{}] Objective decided to stop", name(), m_line_search->name()); } - objFunc.post_step(this->m_current.iterations, x); + objFunc.post_step(PostStepData(this->m_current.iterations, x, grad)); if (f_delta < this->m_stop.fDelta) f_delta_step_cnt++; From 287071bcae77f0f8daf4ec114a5acb10c51a52c4 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Thu, 7 Dec 2023 22:39:49 -0500 Subject: [PATCH 4/4] Fix CMake deps order --- CMakeLists.txt | 105 +++++++++++++++++++++------------------- cmake/recipes/jse.cmake | 2 +- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9601a499..641871b3 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,8 +157,6 @@ add_subdirectory(src/polysolve/nonlinear) target_link_libraries(polysolve_linear PUBLIC polysolve_coverage_config) target_link_libraries(polysolve PUBLIC polysolve_coverage_config) - - target_compile_features(polysolve_linear PUBLIC cxx_std_17) target_compile_features(polysolve PUBLIC cxx_std_17) @@ -183,33 +181,11 @@ target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_JSON_SPEC_DIR="${PR # Dependencies ################################################################################ -# polysolve_linear -target_link_libraries(polysolve PUBLIC polysolve::linear) - -# CppNumericalSolvers -include(cppoptlib) -target_link_libraries(polysolve PUBLIC cppoptlib) - -# LBFGSpp -include(LBFGSpp) -target_link_libraries(polysolve PUBLIC LBFGSpp::LBFGSpp) - - - - -# JSON Specification Engine library -include(jse) -target_link_libraries(polysolve_linear PUBLIC jse::jse) - -# spdlog -include(spdlog) -target_link_libraries(polysolve_linear PUBLIC spdlog::spdlog) - -# finite-diff -include(finite-diff) -target_link_libraries(polysolve PUBLIC finitediff::finitediff) +# ------ +# Linear +# ------ -# Accelerate solver +# Accelerate solver (Include before Eigen) if(POLYSOLVE_WITH_ACCELERATE) include(CPM) CPMAddPackage( @@ -222,23 +198,24 @@ if(POLYSOLVE_WITH_ACCELERATE) find_package(BLAS REQUIRED) find_package(LAPACK REQUIRED) target_link_libraries(polysolve_linear PRIVATE BLAS::BLAS LAPACK::LAPACK) -endif() - -# Extra warnings -include(polysolve_warnings) -target_link_libraries(polysolve_linear PRIVATE polysolve::warnings) -target_link_libraries(polysolve PRIVATE polysolve::warnings) - -# Sanitizers -if(POLYSOLVE_WITH_SANITIZERS) - include(sanitizers) - add_sanitizers(polysolve_linear) - add_sanitizers(polysolve) + target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_ACCELERATE) endif() include(eigen) target_link_libraries(polysolve_linear PUBLIC Eigen3::Eigen) +# spdlog +include(spdlog) +target_link_libraries(polysolve_linear PUBLIC spdlog::spdlog) + +# JSON (MIT) +include(json) +target_link_libraries(polysolve_linear PUBLIC nlohmann_json::nlohmann_json) + +# JSON Specification Engine library +include(jse) +target_link_libraries(polysolve_linear PUBLIC jse::jse) + # Hypre (GNU Lesser General Public License) if(POLYSOLVE_WITH_HYPRE) include(hypre) @@ -249,15 +226,6 @@ if(POLYSOLVE_WITH_HYPRE) endif() endif() -# Json (MIT) -include(json) -target_link_libraries(polysolve_linear PUBLIC nlohmann_json::nlohmann_json) - -# Accelerate solvers -if(POLYSOLVE_WITH_ACCELERATE) - target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_ACCELERATE) -endif() - # CHOLMOD solver if(POLYSOLVE_WITH_CHOLMOD) include(suitesparse) @@ -326,6 +294,45 @@ if(POLYSOLVE_WITH_CUSOLVER) endif() endif() +# Sanitizers +if(POLYSOLVE_WITH_SANITIZERS) + include(sanitizers) + add_sanitizers(polysolve_linear) +endif() + +# Extra warnings (include last for highest priority) +include(polysolve_warnings) +target_link_libraries(polysolve_linear PRIVATE polysolve::warnings) + +# --------- +# Nonlinear +# --------- + +# polysolve::linear +target_link_libraries(polysolve PUBLIC polysolve::linear) + +# CppNumericalSolvers +include(cppoptlib) +target_link_libraries(polysolve PUBLIC cppoptlib) + +# LBFGSpp +include(LBFGSpp) +target_link_libraries(polysolve PUBLIC LBFGSpp::LBFGSpp) + +# finite-diff (include this after eigen) +include(finite-diff) +target_link_libraries(polysolve PUBLIC finitediff::finitediff) + +# Sanitizers +if(POLYSOLVE_WITH_SANITIZERS) + include(sanitizers) + add_sanitizers(polysolve) +endif() + +# Extra warnings (include last for highest priority) +include(polysolve_warnings) +target_link_libraries(polysolve PRIVATE polysolve::warnings) + ################################################################################ # Compiler options diff --git a/cmake/recipes/jse.cmake b/cmake/recipes/jse.cmake index 4b67c02e..e998d760 100644 --- a/cmake/recipes/jse.cmake +++ b/cmake/recipes/jse.cmake @@ -8,4 +8,4 @@ endif() message(STATUS "Third-party: creating target 'jse::jse'") include(CPM) -CPMAddPackage("gh:geometryprocessing/json-spec-engine#1261dc89478c7646ff99cbed8bc5357c2813565d") \ No newline at end of file +CPMAddPackage("gh:geometryprocessing/json-spec-engine#49f1a30f8c2912814916ec3d6108a649b23cb243") \ No newline at end of file