compressed_row_sparse_matrix.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #include "ceres/compressed_row_sparse_matrix.h"
  31. #include <algorithm>
  32. #include <functional>
  33. #include <memory>
  34. #include <numeric>
  35. #include <random>
  36. #include <vector>
  37. #include "ceres/context_impl.h"
  38. #include "ceres/crs_matrix.h"
  39. #include "ceres/internal/export.h"
  40. #include "ceres/parallel_for.h"
  41. #include "ceres/triplet_sparse_matrix.h"
  42. #include "glog/logging.h"
  43. namespace ceres::internal {
  44. namespace {
  45. // Helper functor used by the constructor for reordering the contents
  46. // of a TripletSparseMatrix. This comparator assumes that there are no
  47. // duplicates in the pair of arrays rows and cols, i.e., there is no
  48. // indices i and j (not equal to each other) s.t.
  49. //
  50. // rows[i] == rows[j] && cols[i] == cols[j]
  51. //
  52. // If this is the case, this functor will not be a StrictWeakOrdering.
  53. struct RowColLessThan {
  54. RowColLessThan(const int* rows, const int* cols) : rows(rows), cols(cols) {}
  55. bool operator()(const int x, const int y) const {
  56. if (rows[x] == rows[y]) {
  57. return (cols[x] < cols[y]);
  58. }
  59. return (rows[x] < rows[y]);
  60. }
  61. const int* rows;
  62. const int* cols;
  63. };
  64. void TransposeForCompressedRowSparseStructure(const int num_rows,
  65. const int num_cols,
  66. const int num_nonzeros,
  67. const int* rows,
  68. const int* cols,
  69. const double* values,
  70. int* transpose_rows,
  71. int* transpose_cols,
  72. double* transpose_values) {
  73. // Explicitly zero out transpose_rows.
  74. std::fill(transpose_rows, transpose_rows + num_cols + 1, 0);
  75. // Count the number of entries in each column of the original matrix
  76. // and assign to transpose_rows[col + 1].
  77. for (int idx = 0; idx < num_nonzeros; ++idx) {
  78. ++transpose_rows[cols[idx] + 1];
  79. }
  80. // Compute the starting position for each row in the transpose by
  81. // computing the cumulative sum of the entries of transpose_rows.
  82. for (int i = 1; i < num_cols + 1; ++i) {
  83. transpose_rows[i] += transpose_rows[i - 1];
  84. }
  85. // Populate transpose_cols and (optionally) transpose_values by
  86. // walking the entries of the source matrices. For each entry that
  87. // is added, the value of transpose_row is incremented allowing us
  88. // to keep track of where the next entry for that row should go.
  89. //
  90. // As a result transpose_row is shifted to the left by one entry.
  91. for (int r = 0; r < num_rows; ++r) {
  92. for (int idx = rows[r]; idx < rows[r + 1]; ++idx) {
  93. const int c = cols[idx];
  94. const int transpose_idx = transpose_rows[c]++;
  95. transpose_cols[transpose_idx] = r;
  96. if (values != nullptr && transpose_values != nullptr) {
  97. transpose_values[transpose_idx] = values[idx];
  98. }
  99. }
  100. }
  101. // This loop undoes the left shift to transpose_rows introduced by
  102. // the previous loop.
  103. for (int i = num_cols - 1; i > 0; --i) {
  104. transpose_rows[i] = transpose_rows[i - 1];
  105. }
  106. transpose_rows[0] = 0;
  107. }
  108. template <class RandomNormalFunctor>
  109. void AddRandomBlock(const int num_rows,
  110. const int num_cols,
  111. const int row_block_begin,
  112. const int col_block_begin,
  113. RandomNormalFunctor&& randn,
  114. std::vector<int>* rows,
  115. std::vector<int>* cols,
  116. std::vector<double>* values) {
  117. for (int r = 0; r < num_rows; ++r) {
  118. for (int c = 0; c < num_cols; ++c) {
  119. rows->push_back(row_block_begin + r);
  120. cols->push_back(col_block_begin + c);
  121. values->push_back(randn());
  122. }
  123. }
  124. }
  125. template <class RandomNormalFunctor>
  126. void AddSymmetricRandomBlock(const int num_rows,
  127. const int row_block_begin,
  128. RandomNormalFunctor&& randn,
  129. std::vector<int>* rows,
  130. std::vector<int>* cols,
  131. std::vector<double>* values) {
  132. for (int r = 0; r < num_rows; ++r) {
  133. for (int c = r; c < num_rows; ++c) {
  134. const double v = randn();
  135. rows->push_back(row_block_begin + r);
  136. cols->push_back(row_block_begin + c);
  137. values->push_back(v);
  138. if (r != c) {
  139. rows->push_back(row_block_begin + c);
  140. cols->push_back(row_block_begin + r);
  141. values->push_back(v);
  142. }
  143. }
  144. }
  145. }
  146. } // namespace
  147. // This constructor gives you a semi-initialized CompressedRowSparseMatrix.
  148. CompressedRowSparseMatrix::CompressedRowSparseMatrix(int num_rows,
  149. int num_cols,
  150. int max_num_nonzeros) {
  151. num_rows_ = num_rows;
  152. num_cols_ = num_cols;
  153. storage_type_ = StorageType::UNSYMMETRIC;
  154. rows_.resize(num_rows + 1, 0);
  155. cols_.resize(max_num_nonzeros, 0);
  156. values_.resize(max_num_nonzeros, 0.0);
  157. VLOG(1) << "# of rows: " << num_rows_ << " # of columns: " << num_cols_
  158. << " max_num_nonzeros: " << cols_.size() << ". Allocating "
  159. << (num_rows_ + 1) * sizeof(int) + // NOLINT
  160. cols_.size() * sizeof(int) + // NOLINT
  161. cols_.size() * sizeof(double); // NOLINT
  162. }
  163. std::unique_ptr<CompressedRowSparseMatrix>
  164. CompressedRowSparseMatrix::FromTripletSparseMatrix(
  165. const TripletSparseMatrix& input) {
  166. return CompressedRowSparseMatrix::FromTripletSparseMatrix(input, false);
  167. }
  168. std::unique_ptr<CompressedRowSparseMatrix>
  169. CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(
  170. const TripletSparseMatrix& input) {
  171. return CompressedRowSparseMatrix::FromTripletSparseMatrix(input, true);
  172. }
  173. std::unique_ptr<CompressedRowSparseMatrix>
  174. CompressedRowSparseMatrix::FromTripletSparseMatrix(
  175. const TripletSparseMatrix& input, bool transpose) {
  176. int num_rows = input.num_rows();
  177. int num_cols = input.num_cols();
  178. const int* rows = input.rows();
  179. const int* cols = input.cols();
  180. const double* values = input.values();
  181. if (transpose) {
  182. std::swap(num_rows, num_cols);
  183. std::swap(rows, cols);
  184. }
  185. // index is the list of indices into the TripletSparseMatrix input.
  186. std::vector<int> index(input.num_nonzeros(), 0);
  187. for (int i = 0; i < input.num_nonzeros(); ++i) {
  188. index[i] = i;
  189. }
  190. // Sort index such that the entries of m are ordered by row and ties
  191. // are broken by column.
  192. std::sort(index.begin(), index.end(), RowColLessThan(rows, cols));
  193. VLOG(1) << "# of rows: " << num_rows << " # of columns: " << num_cols
  194. << " num_nonzeros: " << input.num_nonzeros() << ". Allocating "
  195. << ((num_rows + 1) * sizeof(int) + // NOLINT
  196. input.num_nonzeros() * sizeof(int) + // NOLINT
  197. input.num_nonzeros() * sizeof(double)); // NOLINT
  198. auto output = std::make_unique<CompressedRowSparseMatrix>(
  199. num_rows, num_cols, input.num_nonzeros());
  200. if (num_rows == 0) {
  201. // No data to copy.
  202. return output;
  203. }
  204. // Copy the contents of the cols and values array in the order given
  205. // by index and count the number of entries in each row.
  206. int* output_rows = output->mutable_rows();
  207. int* output_cols = output->mutable_cols();
  208. double* output_values = output->mutable_values();
  209. output_rows[0] = 0;
  210. for (int i = 0; i < index.size(); ++i) {
  211. const int idx = index[i];
  212. ++output_rows[rows[idx] + 1];
  213. output_cols[i] = cols[idx];
  214. output_values[i] = values[idx];
  215. }
  216. // Find the cumulative sum of the row counts.
  217. for (int i = 1; i < num_rows + 1; ++i) {
  218. output_rows[i] += output_rows[i - 1];
  219. }
  220. CHECK_EQ(output->num_nonzeros(), input.num_nonzeros());
  221. return output;
  222. }
  223. CompressedRowSparseMatrix::CompressedRowSparseMatrix(const double* diagonal,
  224. int num_rows) {
  225. CHECK(diagonal != nullptr);
  226. num_rows_ = num_rows;
  227. num_cols_ = num_rows;
  228. storage_type_ = StorageType::UNSYMMETRIC;
  229. rows_.resize(num_rows + 1);
  230. cols_.resize(num_rows);
  231. values_.resize(num_rows);
  232. rows_[0] = 0;
  233. for (int i = 0; i < num_rows_; ++i) {
  234. cols_[i] = i;
  235. values_[i] = diagonal[i];
  236. rows_[i + 1] = i + 1;
  237. }
  238. CHECK_EQ(num_nonzeros(), num_rows);
  239. }
  240. CompressedRowSparseMatrix::~CompressedRowSparseMatrix() = default;
  241. void CompressedRowSparseMatrix::SetZero() {
  242. std::fill(values_.begin(), values_.end(), 0);
  243. }
  244. // TODO(sameeragarwal): Make RightMultiplyAndAccumulate and
  245. // LeftMultiplyAndAccumulate block-aware for higher performance.
  246. void CompressedRowSparseMatrix::RightMultiplyAndAccumulate(
  247. const double* x, double* y, ContextImpl* context, int num_threads) const {
  248. if (storage_type_ != StorageType::UNSYMMETRIC) {
  249. RightMultiplyAndAccumulate(x, y);
  250. return;
  251. }
  252. auto values = values_.data();
  253. auto rows = rows_.data();
  254. auto cols = cols_.data();
  255. ParallelFor(
  256. context, 0, num_rows_, num_threads, [values, rows, cols, x, y](int row) {
  257. for (int idx = rows[row]; idx < rows[row + 1]; ++idx) {
  258. const int c = cols[idx];
  259. const double v = values[idx];
  260. y[row] += v * x[c];
  261. }
  262. });
  263. }
  264. void CompressedRowSparseMatrix::RightMultiplyAndAccumulate(const double* x,
  265. double* y) const {
  266. CHECK(x != nullptr);
  267. CHECK(y != nullptr);
  268. if (storage_type_ == StorageType::UNSYMMETRIC) {
  269. RightMultiplyAndAccumulate(x, y, nullptr, 1);
  270. } else if (storage_type_ == StorageType::UPPER_TRIANGULAR) {
  271. // Because of their block structure, we will have entries that lie
  272. // above (below) the diagonal for lower (upper) triangular matrices,
  273. // so the loops below need to account for this.
  274. for (int r = 0; r < num_rows_; ++r) {
  275. int idx = rows_[r];
  276. const int idx_end = rows_[r + 1];
  277. // For upper triangular matrices r <= c, so skip entries with r
  278. // > c.
  279. while (idx < idx_end && r > cols_[idx]) {
  280. ++idx;
  281. }
  282. for (; idx < idx_end; ++idx) {
  283. const int c = cols_[idx];
  284. const double v = values_[idx];
  285. y[r] += v * x[c];
  286. // Since we are only iterating over the upper triangular part
  287. // of the matrix, add contributions for the strictly lower
  288. // triangular part.
  289. if (r != c) {
  290. y[c] += v * x[r];
  291. }
  292. }
  293. }
  294. } else if (storage_type_ == StorageType::LOWER_TRIANGULAR) {
  295. for (int r = 0; r < num_rows_; ++r) {
  296. int idx = rows_[r];
  297. const int idx_end = rows_[r + 1];
  298. // For lower triangular matrices, we only iterate till we are r >=
  299. // c.
  300. for (; idx < idx_end && r >= cols_[idx]; ++idx) {
  301. const int c = cols_[idx];
  302. const double v = values_[idx];
  303. y[r] += v * x[c];
  304. // Since we are only iterating over the lower triangular part
  305. // of the matrix, add contributions for the strictly upper
  306. // triangular part.
  307. if (r != c) {
  308. y[c] += v * x[r];
  309. }
  310. }
  311. }
  312. } else {
  313. LOG(FATAL) << "Unknown storage type: " << storage_type_;
  314. }
  315. }
  316. void CompressedRowSparseMatrix::LeftMultiplyAndAccumulate(const double* x,
  317. double* y) const {
  318. CHECK(x != nullptr);
  319. CHECK(y != nullptr);
  320. if (storage_type_ == StorageType::UNSYMMETRIC) {
  321. for (int r = 0; r < num_rows_; ++r) {
  322. for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
  323. y[cols_[idx]] += values_[idx] * x[r];
  324. }
  325. }
  326. } else {
  327. // Since the matrix is symmetric, LeftMultiplyAndAccumulate =
  328. // RightMultiplyAndAccumulate.
  329. RightMultiplyAndAccumulate(x, y);
  330. }
  331. }
  332. void CompressedRowSparseMatrix::SquaredColumnNorm(double* x) const {
  333. CHECK(x != nullptr);
  334. std::fill(x, x + num_cols_, 0.0);
  335. if (storage_type_ == StorageType::UNSYMMETRIC) {
  336. for (int idx = 0; idx < rows_[num_rows_]; ++idx) {
  337. x[cols_[idx]] += values_[idx] * values_[idx];
  338. }
  339. } else if (storage_type_ == StorageType::UPPER_TRIANGULAR) {
  340. // Because of their block structure, we will have entries that lie
  341. // above (below) the diagonal for lower (upper) triangular
  342. // matrices, so the loops below need to account for this.
  343. for (int r = 0; r < num_rows_; ++r) {
  344. int idx = rows_[r];
  345. const int idx_end = rows_[r + 1];
  346. // For upper triangular matrices r <= c, so skip entries with r
  347. // > c.
  348. while (idx < idx_end && r > cols_[idx]) {
  349. ++idx;
  350. }
  351. for (; idx < idx_end; ++idx) {
  352. const int c = cols_[idx];
  353. const double v2 = values_[idx] * values_[idx];
  354. x[c] += v2;
  355. // Since we are only iterating over the upper triangular part
  356. // of the matrix, add contributions for the strictly lower
  357. // triangular part.
  358. if (r != c) {
  359. x[r] += v2;
  360. }
  361. }
  362. }
  363. } else if (storage_type_ == StorageType::LOWER_TRIANGULAR) {
  364. for (int r = 0; r < num_rows_; ++r) {
  365. int idx = rows_[r];
  366. const int idx_end = rows_[r + 1];
  367. // For lower triangular matrices, we only iterate till we are r >=
  368. // c.
  369. for (; idx < idx_end && r >= cols_[idx]; ++idx) {
  370. const int c = cols_[idx];
  371. const double v2 = values_[idx] * values_[idx];
  372. x[c] += v2;
  373. // Since we are only iterating over the lower triangular part
  374. // of the matrix, add contributions for the strictly upper
  375. // triangular part.
  376. if (r != c) {
  377. x[r] += v2;
  378. }
  379. }
  380. }
  381. } else {
  382. LOG(FATAL) << "Unknown storage type: " << storage_type_;
  383. }
  384. }
  385. void CompressedRowSparseMatrix::ScaleColumns(const double* scale) {
  386. CHECK(scale != nullptr);
  387. for (int idx = 0; idx < rows_[num_rows_]; ++idx) {
  388. values_[idx] *= scale[cols_[idx]];
  389. }
  390. }
  391. void CompressedRowSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
  392. CHECK(dense_matrix != nullptr);
  393. dense_matrix->resize(num_rows_, num_cols_);
  394. dense_matrix->setZero();
  395. for (int r = 0; r < num_rows_; ++r) {
  396. for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
  397. (*dense_matrix)(r, cols_[idx]) = values_[idx];
  398. }
  399. }
  400. }
  401. void CompressedRowSparseMatrix::DeleteRows(int delta_rows) {
  402. CHECK_GE(delta_rows, 0);
  403. CHECK_LE(delta_rows, num_rows_);
  404. CHECK_EQ(storage_type_, StorageType::UNSYMMETRIC);
  405. num_rows_ -= delta_rows;
  406. rows_.resize(num_rows_ + 1);
  407. // The rest of the code updates the block information. Immediately
  408. // return in case of no block information.
  409. if (row_blocks_.empty()) {
  410. return;
  411. }
  412. // Walk the list of row blocks until we reach the new number of rows
  413. // and the drop the rest of the row blocks.
  414. int num_row_blocks = 0;
  415. int num_rows = 0;
  416. while (num_row_blocks < row_blocks_.size() && num_rows < num_rows_) {
  417. num_rows += row_blocks_[num_row_blocks].size;
  418. ++num_row_blocks;
  419. }
  420. row_blocks_.resize(num_row_blocks);
  421. }
  422. void CompressedRowSparseMatrix::AppendRows(const CompressedRowSparseMatrix& m) {
  423. CHECK_EQ(storage_type_, StorageType::UNSYMMETRIC);
  424. CHECK_EQ(m.num_cols(), num_cols_);
  425. CHECK((row_blocks_.empty() && m.row_blocks().empty()) ||
  426. (!row_blocks_.empty() && !m.row_blocks().empty()))
  427. << "Cannot append a matrix with row blocks to one without and vice versa."
  428. << "This matrix has : " << row_blocks_.size() << " row blocks."
  429. << "The matrix being appended has: " << m.row_blocks().size()
  430. << " row blocks.";
  431. if (m.num_rows() == 0) {
  432. return;
  433. }
  434. if (cols_.size() < num_nonzeros() + m.num_nonzeros()) {
  435. cols_.resize(num_nonzeros() + m.num_nonzeros());
  436. values_.resize(num_nonzeros() + m.num_nonzeros());
  437. }
  438. // Copy the contents of m into this matrix.
  439. DCHECK_LT(num_nonzeros(), cols_.size());
  440. if (m.num_nonzeros() > 0) {
  441. std::copy(m.cols(), m.cols() + m.num_nonzeros(), &cols_[num_nonzeros()]);
  442. std::copy(
  443. m.values(), m.values() + m.num_nonzeros(), &values_[num_nonzeros()]);
  444. }
  445. rows_.resize(num_rows_ + m.num_rows() + 1);
  446. // new_rows = [rows_, m.row() + rows_[num_rows_]]
  447. std::fill(rows_.begin() + num_rows_,
  448. rows_.begin() + num_rows_ + m.num_rows() + 1,
  449. rows_[num_rows_]);
  450. for (int r = 0; r < m.num_rows() + 1; ++r) {
  451. rows_[num_rows_ + r] += m.rows()[r];
  452. }
  453. num_rows_ += m.num_rows();
  454. // The rest of the code updates the block information. Immediately
  455. // return in case of no block information.
  456. if (row_blocks_.empty()) {
  457. return;
  458. }
  459. row_blocks_.insert(
  460. row_blocks_.end(), m.row_blocks().begin(), m.row_blocks().end());
  461. }
  462. void CompressedRowSparseMatrix::ToTextFile(FILE* file) const {
  463. CHECK(file != nullptr);
  464. for (int r = 0; r < num_rows_; ++r) {
  465. for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
  466. fprintf(file, "% 10d % 10d %17f\n", r, cols_[idx], values_[idx]);
  467. }
  468. }
  469. }
  470. void CompressedRowSparseMatrix::ToCRSMatrix(CRSMatrix* matrix) const {
  471. matrix->num_rows = num_rows_;
  472. matrix->num_cols = num_cols_;
  473. matrix->rows = rows_;
  474. matrix->cols = cols_;
  475. matrix->values = values_;
  476. // Trim.
  477. matrix->rows.resize(matrix->num_rows + 1);
  478. matrix->cols.resize(matrix->rows[matrix->num_rows]);
  479. matrix->values.resize(matrix->rows[matrix->num_rows]);
  480. }
  481. void CompressedRowSparseMatrix::SetMaxNumNonZeros(int num_nonzeros) {
  482. CHECK_GE(num_nonzeros, 0);
  483. cols_.resize(num_nonzeros);
  484. values_.resize(num_nonzeros);
  485. }
  486. std::unique_ptr<CompressedRowSparseMatrix>
  487. CompressedRowSparseMatrix::CreateBlockDiagonalMatrix(
  488. const double* diagonal, const std::vector<Block>& blocks) {
  489. const int num_rows = NumScalarEntries(blocks);
  490. int num_nonzeros = 0;
  491. for (auto& block : blocks) {
  492. num_nonzeros += block.size * block.size;
  493. }
  494. auto matrix = std::make_unique<CompressedRowSparseMatrix>(
  495. num_rows, num_rows, num_nonzeros);
  496. int* rows = matrix->mutable_rows();
  497. int* cols = matrix->mutable_cols();
  498. double* values = matrix->mutable_values();
  499. std::fill(values, values + num_nonzeros, 0.0);
  500. int idx_cursor = 0;
  501. int col_cursor = 0;
  502. for (auto& block : blocks) {
  503. for (int r = 0; r < block.size; ++r) {
  504. *(rows++) = idx_cursor;
  505. if (diagonal != nullptr) {
  506. values[idx_cursor + r] = diagonal[col_cursor + r];
  507. }
  508. for (int c = 0; c < block.size; ++c, ++idx_cursor) {
  509. *(cols++) = col_cursor + c;
  510. }
  511. }
  512. col_cursor += block.size;
  513. }
  514. *rows = idx_cursor;
  515. *matrix->mutable_row_blocks() = blocks;
  516. *matrix->mutable_col_blocks() = blocks;
  517. CHECK_EQ(idx_cursor, num_nonzeros);
  518. CHECK_EQ(col_cursor, num_rows);
  519. return matrix;
  520. }
  521. std::unique_ptr<CompressedRowSparseMatrix>
  522. CompressedRowSparseMatrix::Transpose() const {
  523. auto transpose = std::make_unique<CompressedRowSparseMatrix>(
  524. num_cols_, num_rows_, num_nonzeros());
  525. switch (storage_type_) {
  526. case StorageType::UNSYMMETRIC:
  527. transpose->set_storage_type(StorageType::UNSYMMETRIC);
  528. break;
  529. case StorageType::LOWER_TRIANGULAR:
  530. transpose->set_storage_type(StorageType::UPPER_TRIANGULAR);
  531. break;
  532. case StorageType::UPPER_TRIANGULAR:
  533. transpose->set_storage_type(StorageType::LOWER_TRIANGULAR);
  534. break;
  535. default:
  536. LOG(FATAL) << "Unknown storage type: " << storage_type_;
  537. };
  538. TransposeForCompressedRowSparseStructure(num_rows(),
  539. num_cols(),
  540. num_nonzeros(),
  541. rows(),
  542. cols(),
  543. values(),
  544. transpose->mutable_rows(),
  545. transpose->mutable_cols(),
  546. transpose->mutable_values());
  547. // The rest of the code updates the block information. Immediately
  548. // return in case of no block information.
  549. if (row_blocks_.empty()) {
  550. return transpose;
  551. }
  552. *(transpose->mutable_row_blocks()) = col_blocks_;
  553. *(transpose->mutable_col_blocks()) = row_blocks_;
  554. return transpose;
  555. }
  556. std::unique_ptr<CompressedRowSparseMatrix>
  557. CompressedRowSparseMatrix::CreateRandomMatrix(
  558. CompressedRowSparseMatrix::RandomMatrixOptions options,
  559. std::mt19937& prng) {
  560. CHECK_GT(options.num_row_blocks, 0);
  561. CHECK_GT(options.min_row_block_size, 0);
  562. CHECK_GT(options.max_row_block_size, 0);
  563. CHECK_LE(options.min_row_block_size, options.max_row_block_size);
  564. if (options.storage_type == StorageType::UNSYMMETRIC) {
  565. CHECK_GT(options.num_col_blocks, 0);
  566. CHECK_GT(options.min_col_block_size, 0);
  567. CHECK_GT(options.max_col_block_size, 0);
  568. CHECK_LE(options.min_col_block_size, options.max_col_block_size);
  569. } else {
  570. // Symmetric matrices (LOWER_TRIANGULAR or UPPER_TRIANGULAR);
  571. options.num_col_blocks = options.num_row_blocks;
  572. options.min_col_block_size = options.min_row_block_size;
  573. options.max_col_block_size = options.max_row_block_size;
  574. }
  575. CHECK_GT(options.block_density, 0.0);
  576. CHECK_LE(options.block_density, 1.0);
  577. std::vector<Block> row_blocks;
  578. row_blocks.reserve(options.num_row_blocks);
  579. std::vector<Block> col_blocks;
  580. col_blocks.reserve(options.num_col_blocks);
  581. std::uniform_int_distribution<int> col_distribution(
  582. options.min_col_block_size, options.max_col_block_size);
  583. std::uniform_int_distribution<int> row_distribution(
  584. options.min_row_block_size, options.max_row_block_size);
  585. std::uniform_real_distribution<double> uniform01(0.0, 1.0);
  586. std::normal_distribution<double> standard_normal_distribution;
  587. // Generate the row block structure.
  588. int row_pos = 0;
  589. for (int i = 0; i < options.num_row_blocks; ++i) {
  590. // Generate a random integer in [min_row_block_size, max_row_block_size]
  591. row_blocks.emplace_back(row_distribution(prng), row_pos);
  592. row_pos += row_blocks.back().size;
  593. }
  594. if (options.storage_type == StorageType::UNSYMMETRIC) {
  595. // Generate the col block structure.
  596. int col_pos = 0;
  597. for (int i = 0; i < options.num_col_blocks; ++i) {
  598. // Generate a random integer in [min_col_block_size, max_col_block_size]
  599. col_blocks.emplace_back(col_distribution(prng), col_pos);
  600. col_pos += col_blocks.back().size;
  601. }
  602. } else {
  603. // Symmetric matrices (LOWER_TRIANGULAR or UPPER_TRIANGULAR);
  604. col_blocks = row_blocks;
  605. }
  606. std::vector<int> tsm_rows;
  607. std::vector<int> tsm_cols;
  608. std::vector<double> tsm_values;
  609. // For ease of construction, we are going to generate the
  610. // CompressedRowSparseMatrix by generating it as a
  611. // TripletSparseMatrix and then converting it to a
  612. // CompressedRowSparseMatrix.
  613. // It is possible that the random matrix is empty which is likely
  614. // not what the user wants, so do the matrix generation till we have
  615. // at least one non-zero entry.
  616. while (tsm_values.empty()) {
  617. tsm_rows.clear();
  618. tsm_cols.clear();
  619. tsm_values.clear();
  620. int row_block_begin = 0;
  621. for (int r = 0; r < options.num_row_blocks; ++r) {
  622. int col_block_begin = 0;
  623. for (int c = 0; c < options.num_col_blocks; ++c) {
  624. if (((options.storage_type == StorageType::UPPER_TRIANGULAR) &&
  625. (r > c)) ||
  626. ((options.storage_type == StorageType::LOWER_TRIANGULAR) &&
  627. (r < c))) {
  628. col_block_begin += col_blocks[c].size;
  629. continue;
  630. }
  631. // Randomly determine if this block is present or not.
  632. if (uniform01(prng) <= options.block_density) {
  633. auto randn = [&standard_normal_distribution, &prng] {
  634. return standard_normal_distribution(prng);
  635. };
  636. // If the matrix is symmetric, then we take care to generate
  637. // symmetric diagonal blocks.
  638. if (options.storage_type == StorageType::UNSYMMETRIC || r != c) {
  639. AddRandomBlock(row_blocks[r].size,
  640. col_blocks[c].size,
  641. row_block_begin,
  642. col_block_begin,
  643. randn,
  644. &tsm_rows,
  645. &tsm_cols,
  646. &tsm_values);
  647. } else {
  648. AddSymmetricRandomBlock(row_blocks[r].size,
  649. row_block_begin,
  650. randn,
  651. &tsm_rows,
  652. &tsm_cols,
  653. &tsm_values);
  654. }
  655. }
  656. col_block_begin += col_blocks[c].size;
  657. }
  658. row_block_begin += row_blocks[r].size;
  659. }
  660. }
  661. const int num_rows = NumScalarEntries(row_blocks);
  662. const int num_cols = NumScalarEntries(col_blocks);
  663. const bool kDoNotTranspose = false;
  664. auto matrix = CompressedRowSparseMatrix::FromTripletSparseMatrix(
  665. TripletSparseMatrix(num_rows, num_cols, tsm_rows, tsm_cols, tsm_values),
  666. kDoNotTranspose);
  667. (*matrix->mutable_row_blocks()) = row_blocks;
  668. (*matrix->mutable_col_blocks()) = col_blocks;
  669. matrix->set_storage_type(options.storage_type);
  670. return matrix;
  671. }
  672. } // namespace ceres::internal