10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: keir@google.com (Keir Mierle) 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// A jacobian writer that directly writes to compressed row sparse matrices. 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_COMPRESSED_ROW_JACOBIAN_WRITER_H_ 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_COMPRESSED_ROW_JACOBIAN_WRITER_H_ 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/evaluator.h" 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/scratch_evaluate_preparer.h" 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal { 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 4279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass CompressedRowSparseMatrix; 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass Program; 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass SparseMatrix; 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass CompressedRowJacobianWriter { 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public: 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CompressedRowJacobianWriter(Evaluator::Options /* ignored */, 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Program* program) 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong : program_(program) { 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 5379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // PopulateJacobianRowAndColumnBlockVectors sets col_blocks and 5479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // row_blocks for a CompressedRowSparseMatrix, based on the 5579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // parameter block sizes and residual sizes respectively from the 5679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // program. This is useful when Solver::Options::use_block_amd = 5779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // true; 5879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // 5979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // This function is static so that it is available to other jacobian 6079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // writers which use CompressedRowSparseMatrix (or derived types). 6179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // (Jacobian writers do not fall under any type hierarchy; they only 6279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // have to provide an interface as specified in program_evaluator.h). 6379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez static void PopulateJacobianRowAndColumnBlockVectors( 6479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez const Program* program, 6579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez CompressedRowSparseMatrix* jacobian); 6679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 6779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // It is necessary to determine the order of the jacobian blocks 6879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // before copying them into a CompressedRowSparseMatrix (or derived 6979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // type). Just because a cost function uses parameter blocks 1 7079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // after 2 in its arguments does not mean that the block 1 occurs 7179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // before block 2 in the column layout of the jacobian. Thus, 7279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // GetOrderedParameterBlocks determines the order by sorting the 7379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // jacobian blocks by their position in the state vector. 7479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // 7579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // This function is static so that it is available to other jacobian 7679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // writers which use CompressedRowSparseMatrix (or derived types). 7779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // (Jacobian writers do not fall under any type hierarchy; they only 7879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // have to provide an interface as specified in 7979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // program_evaluator.h). 8079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez static void GetOrderedParameterBlocks( 8179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez const Program* program, 8279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez int residual_id, 8379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez vector<pair<int, int> >* evaluated_jacobian_blocks); 8479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // JacobianWriter interface. 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 8779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // Since the compressed row matrix has different layout than that 8879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // assumed by the cost functions, use scratch space to store the 8979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // jacobians temporarily then copy them over to the larger jacobian 9079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez // in the Write() function. 910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ScratchEvaluatePreparer* CreateEvaluatePreparers(int num_threads) { 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return ScratchEvaluatePreparer::Create(*program_, num_threads); 930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong SparseMatrix* CreateJacobian() const; 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong void Write(int residual_id, 980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int residual_offset, 990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double **jacobians, 1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong SparseMatrix* base_jacobian); 1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private: 1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Program* program_; 1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}; 1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace internal 1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_INTERNAL_COMPRESSED_ROW_JACOBIAN_WRITER_H_ 110