23#include <gtsam/dllexport.h>
29#include <gtsam/linear/internal/BatchHessianMapping.h>
39#include <unordered_map>
40#include <unordered_set>
56template <
typename KeyRange>
59 for (
Key key : keys) {
60 auto it = dims.find(key);
61 if (it != dims.end()) dim += it->second;
73 using shared_ptr = std::shared_ptr<MultifrontalClique>;
74 using Children = std::vector<shared_ptr>;
80 std::weak_ptr<MultifrontalClique>
parent;
97 const std::weak_ptr<MultifrontalClique>&
parent,
99 const KeySet& separatorKeys,
102 const std::unordered_set<Key>* fixedKeys,
103 size_t numEliminatedFrontals);
162 double minDiagonal,
double maxDiagonal);
184 std::shared_ptr<GaussianConditional> conditional()
const;
193 std::shared_ptr<HessianFactor> remainingFactor()
const;
202 bool useQR()
const {
return solveMode_ == SolveMode::QrLeaf; }
206 return solveMode_ == SolveMode::CompactCholeskyLeaf ||
207 solveMode_ == SolveMode::FusedStarCandidate ||
208 solveMode_ == SolveMode::FusedStarCholeskyLeaf;
216 void print(
const std::string& s =
"",
231 void eliminateInPlace();
240 void eliminateInPlace(
double lambda,
const LMDampingParams& dampingParams,
251 void updateSolution();
260 double constantTermError()
const;
263 friend std::ostream& operator<<(std::ostream& os,
267 enum class SolveMode {
272 FusedStarCholeskyLeaf
276 void cacheSolutionPointers(
VectorValues* delta,
const KeyVector& frontals,
277 const KeyVector& separatorKeys);
280 DenseIndex blockIndex(Key key)
const;
282 enum class FactorLoadKind : uint8_t { Empty, Jacobian, Batch };
284 struct FactorLoadPlan {
285 FactorLoadKind kind = FactorLoadKind::Empty;
286 size_t factorIndex = 0;
287 SharedDiagonal model;
290 size_t abRowOffset = 0;
293 std::vector<DenseIndex> blockIndices;
295 internal::BatchHessianMapping localMapping;
297 internal::BatchHessianMapping parentMapping;
299 internal::BatchHessianMapping separatorMapping;
300 bool canDirectUpdate =
false;
303 static FactorLoadPlan forNullFactor(
size_t factorIndex);
306 static FactorLoadPlan forJacobian(
size_t factorIndex,
307 const JacobianFactor& factor,
308 const MultifrontalClique& clique);
311 static FactorLoadPlan forBatch(
size_t factorIndex,
312 const BatchJacobianFactorBase& factor,
313 const MultifrontalClique& clique);
316 bool isDirectBatch()
const {
317 return kind == FactorLoadKind::Batch && canDirectUpdate;
321 bool needsMaterializedRows(
const MultifrontalClique& clique)
const;
324 void assignMaterializedRows(
size_t* nextRow);
327 void assertInvariants(
const GaussianFactor* factor,
328 const MultifrontalClique& clique)
const;
331 bool supportsFusedStarLeaf()
const;
334 void buildSolveMappings(
const BatchJacobianFactorBase& factor,
335 const MultifrontalClique& clique);
339 void mapKeys(
const KeyVector& keys,
const MultifrontalClique& clique);
342 void buildLocalBatchMapping(
const BatchJacobianFactorBase& factor,
343 const MultifrontalClique& clique);
346 internal::BatchHessianMapping buildRetainedMapping(
347 const BatchJacobianFactorBase& factor, DenseIndex numFrontals,
348 const std::vector<DenseIndex>& targetIndices,
349 const std::vector<DenseIndex>& targetScalarOffsets)
const;
353 void buildLoadPlans(
const GaussianFactorGraph& graph);
356 void allocateSolveStorage();
359 void resolveLeafSolveMode();
362 void buildFusedStarMappings();
366 void updateParentInfo(SymmetricBlockMatrix& parentInfo)
const;
369 void updateParentMaterializedColumn(SymmetricBlockMatrix& parentInfo,
370 DenseIndex sourceSeparatorBlock)
const;
373 void updateParentQrColumnScratch(DenseIndex sourceSeparatorBlock,
374 Matrix* scratch)
const;
377 double parentRhsDiagonal()
const;
380 void updateSeparatorInfo(SymmetricBlockMatrix& separatorInfo)
const;
383 void prepareCompactCholesky();
386 void factorizeCompactCholesky();
389 void factorizeFusedStarCholesky();
392 void updateFusedStarInfo(SymmetricBlockMatrix& targetInfo,
393 const std::vector<DenseIndex>& targetIndices,
394 const std::vector<DenseIndex>& targetScalarOffsets,
395 bool useParentMappedSlots)
const;
398 void updateCholeskyInfo(SymmetricBlockMatrix& targetInfo,
399 const std::vector<DenseIndex>& targetIndices,
400 const std::vector<DenseIndex>& targetBlockOffsets,
401 bool useParentMappedSlots)
const;
404 void updateDirectFactors(SymmetricBlockMatrix& targetInfo,
405 const std::vector<DenseIndex>& targetIndices,
406 bool useParentMappedSlots)
const;
410 void gatherUpdatesSequential();
414 void gatherUpdatesParallel(
size_t numThreads);
417 void gatherSameSeparatorUpdates();
420 std::vector<size_t> blockDims(
const KeyDimMap& dims,
421 const KeyVector& frontals,
422 const KeySet& separatorKeys)
const;
425 void applyDampingQR(
double lambda,
const LMDampingParams& dampingParams,
426 const VectorValues& exactHessianDiagonal);
429 void applyDampingCholesky(
double lambda,
const LMDampingParams& dampingParams,
430 const VectorValues& exactHessianDiagonal);
436 size_t addJacobianFactor(
const JacobianFactor& factor,
size_t rowOffset,
437 const FactorLoadPlan& plan);
443 size_t addBatchJacobianFactor(
const BatchJacobianFactorBase& factor,
444 size_t rowOffset,
const FactorLoadPlan& plan);
446 void setParentIndices(
const std::vector<DenseIndex>& indices,
447 const std::vector<DenseIndex>& scalarOffsets) {
448 parentIndices_ = indices;
449 parentScalarOffsets_ = scalarOffsets;
453 std::vector<size_t> factorIndices_;
455 size_t totalFrontals_ = 0;
456 const std::unordered_set<Key>* fixedKeys_ =
nullptr;
457 std::unordered_map<Key, DenseIndex> blockIndexCache_;
458 std::vector<Vector*> frontalPtrs_;
459 std::vector<const Vector*> separatorPtrs_;
460 std::vector<size_t> blockDims_;
461 size_t factorRows_ = 0;
462 mutable const GaussianFactorGraph* activeLoadGraph_ =
nullptr;
463 mutable bool allBatchFactors_ =
false;
464 mutable bool hasDirectBatchFactors_ =
false;
465 mutable std::vector<FactorLoadPlan> loadPlans_;
466 mutable bool loadPlansBuilt_ =
false;
467 mutable size_t materializedRows_ = 0;
468 mutable size_t fillRows_ = 0;
471 std::vector<DenseIndex>
473 std::vector<DenseIndex>
474 parentScalarOffsets_;
475 std::vector<DenseIndex> separatorIndices_;
476 std::vector<DenseIndex>
477 separatorScalarOffsets_;
478 SolveMode solveMode_ = SolveMode::Cholesky;
479 SolveMode starFallbackMode_ = SolveMode::Cholesky;
481 bool deferredSingleFactorAutoQr_ =
false;
482 std::vector<DenseIndex> parentSchurScalarOffsets_;
483 std::vector<DenseIndex> separatorSchurScalarOffsets_;
485 std::vector<std::vector<size_t>> sameSeparatorChildGroups_;
486 std::vector<std::vector<DenseIndex>> sameSeparatorParentIndices_;
487 std::vector<std::vector<DenseIndex>> sameSeparatorParentScalarOffsets_;
488 std::vector<SymmetricBlockMatrix> sameSeparatorInfos_;
489 std::vector<uint8_t> childInSameSeparatorGroup_;
491 struct ParentGatherPlan;
493 std::shared_ptr<ParentGatherPlan> parentGatherPlan_;
497 VerticalBlockMatrix Ab_;
500 mutable VerticalBlockMatrix RSd_;
501 mutable SymmetricBlockMatrix info_;
504 bool RSdReady_ =
false;
505 bool infoReady_ =
false;
509 Vector separatorScratch_;
512 double lastOldError_ = 0.0;
513 double lastNewError_ = 0.0;
A matrix with column blocks of pre-defined sizes.
Access to matrices via blocks of pre-defined sizes.
size_t sumDims(const KeyDimMap &dims, const KeyRange &keys)
Sum variable dimensions for a key range, skipping unknown keys.
Definition MultifrontalClique.h:57
Linear Factor Graph where all factors are Gaussians.
Parameters for the imperative multifrontal solver.
A factor with a quadratic error function - a Gaussian.
Parameters controlling LM-style damping for NonlinearMultifrontalSolver.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
std::map< Key, size_t > KeyDimMap
Map from variable key to dimension.
Definition MultifrontalClique.h:51
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
Common interface for compact batch Jacobian factors.
Definition BatchJacobianFactor.h:78
A GaussianConditional functions as the node in a Bayes network.
Definition GaussianConditional.h:43
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition GaussianFactorGraph.h:77
A Gaussian factor using the canonical parameters (information form).
Definition HessianFactor.h:101
A Gaussian factor in the squared-error form.
Definition JacobianFactor.h:92
Imperative multifrontal clique structure used by MultifrontalSolver.
Definition MultifrontalClique.h:71
const SymmetricBlockMatrix & info() const
Get the information matrix (const).
Definition MultifrontalClique.h:199
void addExactDiagonalDamping(double lambda, const VectorValues &hessianDiagonal, double minDiagonal, double maxDiagonal)
Add diagonal damping to the frontal block using an externally provided Hessian diagonal diag(J^T J) k...
Definition MultifrontalClique.cpp:1064
int problemSize() const
Return the clique dimension used for traversal scheduling.
Definition MultifrontalClique.h:170
void addIdentityDamping(double lambda)
Add identity damping to the frontal block.
Definition MultifrontalClique.cpp:1027
Children children
Child cliques used for traversal.
Definition MultifrontalClique.h:81
const VerticalBlockMatrix & Ab() const
Get the vertical block matrix Ab.
Definition MultifrontalClique.h:196
const KeyVector & orderedKeys() const
Return keys ordered by block index (frontals followed by separators).
Definition MultifrontalClique.h:181
size_t separatorDim
Separator dimension.
Definition MultifrontalClique.h:83
void fillAb(const GaussianFactorGraph &graph)
Load factor values into a lazily allocated, reusable Ab matrix.
Definition MultifrontalClique.cpp:688
bool useCompactCholesky() const
Check if this leaf avoids materializing its separator Hessian.
Definition MultifrontalClique.h:205
size_t numFrontals() const
Return the number of frontal keys in this clique.
Definition MultifrontalClique.h:175
bool useQR() const
Check if this clique is using QR elimination.
Definition MultifrontalClique.h:202
bool fullyEliminated() const
Return whether every symbolic frontal in this clique is eliminated.
Definition MultifrontalClique.h:178
size_t frontalDim
Frontal dimension.
Definition MultifrontalClique.h:82
double lastOldError() const
Access the last old error computed during updateSolution().
Definition MultifrontalClique.h:254
void prepareForElimination()
Zero out the info matrix, re-add Hessians, accumulate Jacobians and children.
Definition MultifrontalClique.cpp:788
MultifrontalClique(std::vector< size_t > factorIndices, const std::weak_ptr< MultifrontalClique > &parent, const KeyVector &frontals, const KeySet &separatorKeys, const KeyDimMap &dims, size_t vbmRows, VectorValues *solution, const std::unordered_set< Key > *fixedKeys, size_t numEliminatedFrontals)
Construct a clique from factor indices and cache static structure.
Definition MultifrontalClique.cpp:164
void addDiagonalDamping(double lambda, double minDiagonal, double maxDiagonal)
Add diagonal damping to the frontal block.
Definition MultifrontalClique.cpp:1043
std::weak_ptr< MultifrontalClique > parent
Parent clique.
Definition MultifrontalClique.h:80
void finalize(std::vector< ChildInfo > children, const MultifrontalParameters ¶ms)
Cache the children list, compute parent indices, and lock in QR usage.
Definition MultifrontalClique.cpp:236
double lastNewError() const
Access the last new error computed during updateSolution().
Definition MultifrontalClique.h:257
void factorize()
Perform Cholesky factorization on the frontal block.
Definition MultifrontalClique.cpp:902
Definition MultifrontalClique.h:75
Parameters for gtsam::MultifrontalSolver.
Definition MultifrontalParameters.h:37
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
Parameters controlling LM-style damping as applied by gtsam::NonlinearMultifrontalSolver.
Definition LMDampingParams.h:31
The Factor::error simply extracts the.