block_random_access_matrix.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: sameeragarwal@google.com (Sameer Agarwal)
  30. //
  31. // Interface for matrices that allow block based random access.
  32. #ifndef CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_
  33. #define CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_
  34. #include <mutex>
  35. #include "ceres/internal/export.h"
  36. namespace ceres::internal {
  37. // A matrix implementing the BlockRandomAccessMatrix interface is a
  38. // matrix whose rows and columns are divided into blocks. For example
  39. // the matrix A:
  40. //
  41. // 3 4 5
  42. // A = 5 [c_11 c_12 c_13]
  43. // 4 [c_21 c_22 c_23]
  44. //
  45. // has row blocks of size 5 and 4, and column blocks of size 3, 4 and
  46. // 5. It has six cells corresponding to the six row-column block
  47. // combinations.
  48. //
  49. // BlockRandomAccessMatrix objects provide access to cells c_ij using
  50. // the GetCell method. when a cell is present, GetCell will return a
  51. // CellInfo object containing a pointer to an array which contains the
  52. // cell as a submatrix and a mutex that guards this submatrix. If the
  53. // user is accessing the matrix concurrently, it is his responsibility
  54. // to use the mutex to exclude other writers from writing to the cell
  55. // concurrently.
  56. //
  57. // There is no requirement that all cells be present, i.e. the matrix
  58. // itself can be block sparse. When a cell is not present, the GetCell
  59. // method will return a nullptr pointer.
  60. //
  61. // There is no requirement about how the cells are stored beyond that
  62. // form a dense submatrix of a larger dense matrix. Like everywhere
  63. // else in Ceres, RowMajor storage assumed.
  64. //
  65. // Example usage:
  66. //
  67. // BlockRandomAccessMatrix* A = new BlockRandomAccessMatrixSubClass(...)
  68. //
  69. // int row, col, row_stride, col_stride;
  70. // CellInfo* cell = A->GetCell(row_block_id, col_block_id,
  71. // &row, &col,
  72. // &row_stride, &col_stride);
  73. //
  74. // if (cell != nullptr) {
  75. // MatrixRef m(cell->values, row_stride, col_stride);
  76. // std::lock_guard<std::mutex> l(&cell->m);
  77. // m.block(row, col, row_block_size, col_block_size) = ...
  78. // }
  79. // Structure to carry a pointer to the array containing a cell and the
  80. // mutex guarding it.
  81. struct CERES_NO_EXPORT CellInfo {
  82. CellInfo() = default;
  83. explicit CellInfo(double* values) : values(values) {}
  84. double* values{nullptr};
  85. std::mutex m;
  86. };
  87. class CERES_NO_EXPORT BlockRandomAccessMatrix {
  88. public:
  89. virtual ~BlockRandomAccessMatrix();
  90. // If the cell (row_block_id, col_block_id) is present, then return
  91. // a CellInfo with a pointer to the dense matrix containing it,
  92. // otherwise return nullptr. The dense matrix containing this cell has
  93. // size row_stride, col_stride and the cell is located at position
  94. // (row, col) within this matrix.
  95. //
  96. // The size of the cell is row_block_size x col_block_size is
  97. // assumed known to the caller. row_block_size less than or equal to
  98. // row_stride and col_block_size is upper bounded by col_stride.
  99. virtual CellInfo* GetCell(int row_block_id,
  100. int col_block_id,
  101. int* row,
  102. int* col,
  103. int* row_stride,
  104. int* col_stride) = 0;
  105. // Zero out the values of the array. The structure of the matrix
  106. // (size and sparsity) is preserved.
  107. virtual void SetZero() = 0;
  108. // Number of scalar rows and columns in the matrix, i.e the sum of
  109. // all row blocks and column block sizes respectively.
  110. virtual int num_rows() const = 0;
  111. virtual int num_cols() const = 0;
  112. };
  113. } // namespace ceres::internal
  114. #endif // CERES_INTERNAL_BLOCK_RANDOM_ACCESS_MATRIX_H_