Skip to content

Commit 8f0210a

Browse files
committed
Merge remote-tracking branch 'upstream/main' into trilinos
2 parents a376e0d + d65d1b1 commit 8f0210a

File tree

19 files changed

+433
-390
lines changed

19 files changed

+433
-390
lines changed

CMakeLists.txt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ target_include_directories(polysolve PUBLIC ${PROJECT_SOURCE_DIR}/src)
171171
################################################################################
172172

173173
if(POLYSOLVE_LARGE_INDEX)
174-
target_compile_definitions(polysolve_linear PUBLIC -DPOLYSOLVE_LARGE_INDEX)
174+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_LARGE_INDEX)
175175
endif()
176176

177177
target_compile_definitions(polysolve_linear PRIVATE POLYSOLVE_LINEAR_SPEC="${PROJECT_SOURCE_DIR}/linear-solver-spec.json")
@@ -200,7 +200,7 @@ if(POLYSOLVE_WITH_ACCELERATE)
200200
find_package(BLAS REQUIRED)
201201
find_package(LAPACK REQUIRED)
202202
target_link_libraries(polysolve_linear PRIVATE BLAS::BLAS LAPACK::LAPACK)
203-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_ACCELERATE)
203+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_ACCELERATE)
204204
endif()
205205

206206
include(eigen)
@@ -222,7 +222,7 @@ target_link_libraries(polysolve_linear PUBLIC jse::jse)
222222
if(POLYSOLVE_WITH_HYPRE AND NOT POLYSOLVE_WITH_TRILINOS)
223223
include(hypre)
224224
target_link_libraries(polysolve_linear PUBLIC HYPRE::HYPRE)
225-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_HYPRE)
225+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_HYPRE)
226226
if(HYPRE_WITH_MPI)
227227
target_compile_definitions(polysolve_linear PUBLIC HYPRE_WITH_MPI)
228228
endif()
@@ -232,22 +232,22 @@ endif()
232232
if(POLYSOLVE_WITH_CHOLMOD)
233233
include(suitesparse)
234234
target_link_libraries(polysolve_linear PRIVATE SuiteSparse::CHOLMOD)
235-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_CHOLMOD)
235+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_CHOLMOD)
236236
endif()
237237

238238
# MKL library
239239
if(POLYSOLVE_WITH_MKL)
240240
include(mkl)
241241
target_link_libraries(polysolve_linear PRIVATE mkl::mkl)
242-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_MKL)
242+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_MKL)
243243
endif()
244244

245245
# Pardiso solver
246246
if(POLYSOLVE_WITH_PARDISO)
247247
include(pardiso)
248248
if(TARGET Pardiso::Pardiso)
249249
target_link_libraries(polysolve_linear PRIVATE Pardiso::Pardiso)
250-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_PARDISO)
250+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_PARDISO)
251251
else()
252252
message(WARNING "Pardiso not found, solver will not be available.")
253253
endif()
@@ -257,15 +257,15 @@ endif()
257257
if(POLYSOLVE_WITH_UMFPACK)
258258
include(suitesparse)
259259
target_link_libraries(polysolve_linear PRIVATE SuiteSparse::UMFPACK)
260-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_UMFPACK)
260+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_UMFPACK)
261261
endif()
262262

263263
# SuperLU solver
264264
if(POLYSOLVE_WITH_SUPERLU)
265265
include(superlu)
266266
if(TARGET SuperLU::SuperLU)
267267
target_link_libraries(polysolve_linear PRIVATE SuperLU::SuperLU)
268-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_SUPERLU)
268+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_SUPERLU)
269269
else()
270270
message(WARNING "SuperLU not found, solver will not be available.")
271271
endif()
@@ -275,14 +275,14 @@ endif()
275275
if(POLYSOLVE_WITH_AMGCL)
276276
include(amgcl)
277277
target_link_libraries(polysolve_linear PUBLIC amgcl::amgcl)
278-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_AMGCL)
278+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_AMGCL)
279279
endif()
280280

281281
# Spectra (MPL 2.0)
282282
if(POLYSOLVE_WITH_SPECTRA)
283283
include(spectra)
284284
target_link_libraries(polysolve_linear PUBLIC Spectra::Spectra)
285-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_SPECTRA)
285+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_SPECTRA)
286286
endif()
287287

288288
# Trilinos
@@ -297,7 +297,7 @@ if(POLYSOLVE_WITH_CUSOLVER)
297297
include(cusolverdn)
298298
if(TARGET CUDA::cusolver)
299299
target_link_libraries(polysolve_linear PUBLIC CUDA::cusolver)
300-
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_CUSOLVER)
300+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_CUSOLVER)
301301
else()
302302
message(WARNING "cuSOLVER not found, solver will not be available.")
303303
endif()
@@ -320,17 +320,13 @@ target_link_libraries(polysolve_linear PRIVATE polysolve::warnings)
320320
# polysolve::linear
321321
target_link_libraries(polysolve PUBLIC polysolve::linear)
322322

323-
# CppNumericalSolvers
324-
include(cppoptlib)
325-
target_link_libraries(polysolve PUBLIC cppoptlib)
326-
327323
# LBFGSpp
328324
include(LBFGSpp)
329-
target_link_libraries(polysolve PUBLIC LBFGSpp::LBFGSpp)
325+
target_link_libraries(polysolve PRIVATE LBFGSpp::LBFGSpp)
330326

331327
# finite-diff (include this after eigen)
332328
include(finite-diff)
333-
target_link_libraries(polysolve PUBLIC finitediff::finitediff)
329+
target_link_libraries(polysolve PRIVATE finitediff::finitediff)
334330

335331
# Sanitizers
336332
if(POLYSOLVE_WITH_SANITIZERS)

cmake/recipes/cppoptlib.cmake

Lines changed: 0 additions & 19 deletions
This file was deleted.

codecov.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: auto
6+
threshold: 1%
7+
patch:
8+
default:
9+
target: 75%
10+
threshold: 5%
11+
only_pulls: true

nonlinear-solver-spec.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
[
2-
{
1+
[{
32
"pointer": "/",
43
"default": null,
54
"type": "object",
@@ -562,10 +561,8 @@
562561
"type": "string",
563562
"options": [
564563
"Armijo",
565-
"ArmijoAlt",
566564
"RobustArmijo",
567565
"Backtracking",
568-
"MoreThuente",
569566
"None"
570567
],
571568
"doc": "Line-search type"

src/polysolve/linear/Solver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace polysolve::linear
113113
}
114114
}
115115
if (!accepted_solver.empty())
116-
logger.info("Solver {} is the highest priority availble solver; using it.", accepted_solver);
116+
logger.info("Solver {} is the highest priority available solver; using it.", accepted_solver);
117117
else
118118
logger.warn("No valid solver found in the list of specified solvers!");
119119
params[lin_solver_ptr] = accepted_solver;

src/polysolve/nonlinear/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
set(SOURCES
22
BoxConstraintSolver.cpp
33
BoxConstraintSolver.hpp
4-
Solver.hpp
5-
Solver.cpp
6-
Problem.hpp
7-
Problem.cpp
8-
PostStepData.hpp
4+
Criteria.cpp
5+
Criteria.hpp
96
PostStepData.cpp
7+
PostStepData.hpp
8+
Problem.cpp
9+
Problem.hpp
10+
Solver.cpp
11+
Solver.hpp
1012
)
1113

1214
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "Source Files" FILES ${SOURCES})
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Source: https://github.com/PatWie/CppNumericalSolvers/blob/7eddf28fa5a8872a956d3c8666055cac2f5a535d/include/cppoptlib/meta.h
2+
// License: MIT
3+
#include "Criteria.hpp"
4+
5+
namespace polysolve::nonlinear
6+
{
7+
bool is_converged_status(const Status s)
8+
{
9+
return s == Status::XDeltaTolerance || s == Status::FDeltaTolerance || s == Status::GradNormTolerance;
10+
}
11+
12+
Criteria::Criteria()
13+
{
14+
reset();
15+
}
16+
17+
void Criteria::reset()
18+
{
19+
iterations = 0;
20+
xDelta = 0;
21+
fDelta = 0;
22+
gradNorm = 0;
23+
firstGradNorm = 0;
24+
fDeltaCount = 0;
25+
xDeltaDotGrad = 0;
26+
}
27+
28+
void Criteria::print(std::ostream &os) const
29+
{
30+
os << "Iterations: " << iterations << std::endl;
31+
os << "xDelta: " << xDelta << std::endl;
32+
os << "fDelta: " << fDelta << std::endl;
33+
os << "GradNorm: " << gradNorm << std::endl;
34+
os << "xDeltaDotGrad: " << xDeltaDotGrad << std::endl;
35+
os << "FirstGradNorm: " << firstGradNorm << std::endl;
36+
os << "fDeltaCount: " << fDeltaCount << std::endl;
37+
}
38+
39+
Status checkConvergence(const Criteria &stop, const Criteria &current)
40+
{
41+
if (stop.iterations > 0 && current.iterations > stop.iterations)
42+
{
43+
return Status::IterationLimit;
44+
}
45+
if (stop.xDelta > 0 && current.xDelta < stop.xDelta)
46+
{
47+
return Status::XDeltaTolerance;
48+
}
49+
if (stop.fDelta > 0 && current.fDelta < stop.fDelta && current.fDeltaCount >= stop.fDeltaCount)
50+
{
51+
return Status::FDeltaTolerance;
52+
}
53+
const double stopGradNorm = current.iterations == 0 ? stop.firstGradNorm : stop.gradNorm;
54+
if (stopGradNorm > 0 && current.gradNorm < stopGradNorm)
55+
{
56+
return Status::GradNormTolerance;
57+
}
58+
// Δx⋅∇f ≥ 0 means that the search direction is not a descent direction
59+
if (stop.xDeltaDotGrad < 0 && current.xDeltaDotGrad > stop.xDeltaDotGrad)
60+
{
61+
return Status::NotDescentDirection;
62+
}
63+
return Status::Continue;
64+
}
65+
66+
std::ostream &operator<<(std::ostream &os, const Status &s)
67+
{
68+
switch (s)
69+
{
70+
case Status::NotStarted:
71+
os << "Solver not started.";
72+
break;
73+
case Status::Continue:
74+
os << "Convergence criteria not reached.";
75+
break;
76+
case Status::IterationLimit:
77+
os << "Iteration limit reached.";
78+
break;
79+
case Status::XDeltaTolerance:
80+
os << "Change in parameter vector too small.";
81+
break;
82+
case Status::FDeltaTolerance:
83+
os << "Change in cost function value too small.";
84+
break;
85+
case Status::GradNormTolerance:
86+
os << "Gradient vector norm too small.";
87+
break;
88+
case Status::ObjectiveCustomStop:
89+
os << "Objective function specified to stop.";
90+
break;
91+
case Status::NanEncountered:
92+
os << "Objective or gradient function returned NaN.";
93+
break;
94+
case Status::NotDescentDirection:
95+
os << "Search direction not a descent direction.";
96+
break;
97+
case Status::LineSearchFailed:
98+
os << "Line search failed.";
99+
break;
100+
default:
101+
os << "Unknown status.";
102+
break;
103+
}
104+
return os;
105+
}
106+
107+
std::ostream &operator<<(std::ostream &os, const Criteria &c)
108+
{
109+
c.print(os);
110+
return os;
111+
}
112+
} // namespace polysolve::nonlinear
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <iostream>
5+
6+
namespace polysolve::nonlinear
7+
{
8+
// Source: https://github.com/PatWie/CppNumericalSolvers/blob/7eddf28fa5a8872a956d3c8666055cac2f5a535d/include/cppoptlib/meta.h
9+
// License: MIT
10+
11+
enum class Status
12+
{
13+
NotStarted = -1, ///< The solver has not been started
14+
Continue = 0, ///< The solver should continue
15+
// Success cases
16+
IterationLimit, ///< The maximum number of iterations has been reached
17+
XDeltaTolerance, ///< The change in the parameter vector is below the tolerance
18+
FDeltaTolerance, ///< The change in the cost function is below the tolerance
19+
GradNormTolerance, ///< The norm of the gradient vector is below the tolerance
20+
ObjectiveCustomStop, ///< The objective function specified to stop
21+
// Failure cases
22+
NanEncountered, ///< The objective function returned NaN
23+
NotDescentDirection, ///< The search direction is not a descent direction
24+
LineSearchFailed, ///< The line search failed
25+
};
26+
27+
bool is_converged_status(const Status s);
28+
29+
class Criteria
30+
{
31+
public:
32+
size_t iterations; ///< Maximum number of iterations
33+
double xDelta; ///< Minimum change in parameter vector
34+
double fDelta; ///< Minimum change in cost function
35+
double gradNorm; ///< Minimum norm of gradient vector
36+
double firstGradNorm; ///< Initial norm of gradient vector
37+
double xDeltaDotGrad; ///< Dot product of parameter vector and gradient vector
38+
unsigned fDeltaCount; ///< Number of steps where fDelta is satisfied
39+
40+
Criteria();
41+
42+
void reset();
43+
44+
void print(std::ostream &os) const;
45+
};
46+
47+
Status checkConvergence(const Criteria &stop, const Criteria &current);
48+
49+
std::ostream &operator<<(std::ostream &os, const Status &s);
50+
51+
std::ostream &operator<<(std::ostream &os, const Criteria &c);
52+
} // namespace polysolve::nonlinear

0 commit comments

Comments
 (0)