Skip to content

Commit f815133

Browse files
committed
threaded jacobi
Change-Id: I96a5d2f719545e14c03d73e71c8c0564e8c1c729
1 parent 7f8e930 commit f815133

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

internal/ceres/block_jacobi_preconditioner.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
#include "ceres/block_structure.h"
3737
#include "ceres/casts.h"
3838
#include "ceres/internal/eigen.h"
39+
#include "ceres/parallel_for.h"
3940
#include "ceres/small_blas.h"
4041

4142
namespace ceres::internal {
4243

4344
BlockSparseJacobiPreconditioner::BlockSparseJacobiPreconditioner(
44-
const BlockSparseMatrix& A) {
45-
m_ = std::make_unique<BlockRandomAccessDiagonalMatrix>(
46-
A.block_structure()->cols);
45+
Preconditioner::Options options, const BlockSparseMatrix& A)
46+
: options_(std::move(options)) {
47+
m_ = std::make_unique<BlockRandomAccessDiagonalMatrix>(A.block_structure());
4748
}
4849

4950
BlockSparseJacobiPreconditioner::~BlockSparseJacobiPreconditioner() = default;
@@ -94,7 +95,8 @@ bool BlockSparseJacobiPreconditioner::UpdateImpl(const BlockSparseMatrix& A,
9495
}
9596

9697
BlockCRSJacobiPreconditioner::BlockCRSJacobiPreconditioner(
97-
const CompressedRowSparseMatrix& A) {
98+
Preconditioner::Options options, const CompressedRowSparseMatrix& A)
99+
: options_(std::move(options)), locks_(A.col_blocks().size()) {
98100
auto& col_blocks = A.col_blocks();
99101

100102
// Compute the number of non-zeros in the preconditioner. This is needed so

internal/ceres/block_jacobi_preconditioner.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class CERES_NO_EXPORT BlockSparseJacobiPreconditioner
5252
: public BlockSparseMatrixPreconditioner {
5353
public:
5454
// A must remain valid while the BlockJacobiPreconditioner is.
55-
explicit BlockSparseJacobiPreconditioner(const BlockSparseMatrix& A);
55+
explicit BlockSparseJacobiPreconditioner(Preconditioner::Options,
56+
const BlockSparseMatrix& A);
5657
~BlockSparseJacobiPreconditioner() override;
5758
void RightMultiplyAndAccumulate(const double* x, double* y) const final {
5859
return m_->RightMultiplyAndAccumulate(x, y);
@@ -64,6 +65,7 @@ class CERES_NO_EXPORT BlockSparseJacobiPreconditioner
6465
private:
6566
bool UpdateImpl(const BlockSparseMatrix& A, const double* D) final;
6667

68+
Preconditioner::Options options_;
6769
std::unique_ptr<BlockRandomAccessDiagonalMatrix> m_;
6870
};
6971

@@ -73,7 +75,8 @@ class CERES_NO_EXPORT BlockCRSJacobiPreconditioner
7375
: public CompressedRowSparseMatrixPreconditioner {
7476
public:
7577
// A must remain valid while the BlockJacobiPreconditioner is.
76-
explicit BlockCRSJacobiPreconditioner(const CompressedRowSparseMatrix& A);
78+
explicit BlockCRSJacobiPreconditioner(Preconditioner::Options options,
79+
const CompressedRowSparseMatrix& A);
7780
~BlockCRSJacobiPreconditioner() override;
7881
void RightMultiplyAndAccumulate(const double* x, double* y) const final {
7982
m_->RightMultiplyAndAccumulate(x, y);
@@ -85,6 +88,8 @@ class CERES_NO_EXPORT BlockCRSJacobiPreconditioner
8588
private:
8689
bool UpdateImpl(const CompressedRowSparseMatrix& A, const double* D) final;
8790

91+
Preconditioner::Options options_;
92+
std::vector<std::mutex> locks_;
8893
std::unique_ptr<CompressedRowSparseMatrix> m_;
8994
};
9095

internal/ceres/cgnr_solver.cc

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,20 @@ LinearSolver::Summary CgnrSolver::SolveImpl(
135135
double* x) {
136136
EventLogger event_logger("CgnrSolver::Solve");
137137
if (!preconditioner_) {
138+
Preconditioner::Options preconditioner_options;
139+
preconditioner_options.type = options_.preconditioner_type;
140+
preconditioner_options.subset_preconditioner_start_row_block =
141+
options_.subset_preconditioner_start_row_block;
142+
preconditioner_options.sparse_linear_algebra_library_type =
143+
options_.sparse_linear_algebra_library_type;
144+
preconditioner_options.ordering_type = options_.ordering_type;
145+
preconditioner_options.num_threads = options_.num_threads;
146+
preconditioner_options.context = options_.context;
147+
138148
if (options_.preconditioner_type == JACOBI) {
139-
preconditioner_ = std::make_unique<BlockSparseJacobiPreconditioner>(*A);
149+
preconditioner_ = std::make_unique<BlockSparseJacobiPreconditioner>(
150+
preconditioner_options, *A);
140151
} else if (options_.preconditioner_type == SUBSET) {
141-
Preconditioner::Options preconditioner_options;
142-
preconditioner_options.type = SUBSET;
143-
preconditioner_options.subset_preconditioner_start_row_block =
144-
options_.subset_preconditioner_start_row_block;
145-
preconditioner_options.sparse_linear_algebra_library_type =
146-
options_.sparse_linear_algebra_library_type;
147-
preconditioner_options.ordering_type = options_.ordering_type;
148-
preconditioner_options.num_threads = options_.num_threads;
149-
preconditioner_options.context = options_.context;
150152
preconditioner_ =
151153
std::make_unique<SubsetPreconditioner>(preconditioner_options, *A);
152154
} else {
@@ -238,9 +240,11 @@ class CERES_NO_EXPORT CudaIdentityPreconditioner final
238240
class CERES_NO_EXPORT CudaJacobiPreconditioner final
239241
: public CudaPreconditioner {
240242
public:
241-
explicit CudaJacobiPreconditioner(ContextImpl* context,
243+
explicit CudaJacobiPreconditioner(Preconditioner::Options options,
242244
const CompressedRowSparseMatrix& A)
243-
: cpu_preconditioner_(A), m_(context, cpu_preconditioner_.matrix()) {}
245+
: options_(std::move(options)),
246+
cpu_preconditioner_(options_, A),
247+
m_(options_->context, cpu_preconditioner_.matrix()) {}
244248
~CudaJacobiPreconditioner() = default;
245249

246250
void Update(const CompressedRowSparseMatrix& A, const double* D) final {
@@ -253,6 +257,7 @@ class CERES_NO_EXPORT CudaJacobiPreconditioner final
253257
}
254258

255259
private:
260+
Preconditioner::Options options_;
256261
BlockCRSJacobiPreconditioner cpu_preconditioner_;
257262
CudaSparseMatrix m_;
258263
};
@@ -297,9 +302,20 @@ void CudaCgnrSolver::CpuToGpuTransfer(const CompressedRowSparseMatrix& A,
297302
Atb_ = std::make_unique<CudaVector>(options_.context, A.num_cols());
298303
Ax_ = std::make_unique<CudaVector>(options_.context, A.num_rows());
299304
D_ = std::make_unique<CudaVector>(options_.context, A.num_cols());
305+
306+
Preconditioner::Options preconditioner_options;
307+
preconditioner_options.type = options_.preconditioner_type;
308+
preconditioner_options.subset_preconditioner_start_row_block =
309+
options_.subset_preconditioner_start_row_block;
310+
preconditioner_options.sparse_linear_algebra_library_type =
311+
options_.sparse_linear_algebra_library_type;
312+
preconditioner_options.ordering_type = options_.ordering_type;
313+
preconditioner_options.num_threads = options_.num_threads;
314+
preconditioner_options.context = options_.context;
315+
300316
if (options_.preconditioner_type == JACOBI) {
301317
preconditioner_ =
302-
std::make_unique<CudaJacobiPreconditioner>(options_.context, A);
318+
std::make_unique<CudaJacobiPreconditioner>(preconditioner_options, A);
303319
} else {
304320
preconditioner_ = std::make_unique<CudaIdentityPreconditioner>();
305321
}

0 commit comments

Comments
 (0)