Skip to content

Commit fcf6420

Browse files
add gpu to bundle adjustment
1 parent e8ceece commit fcf6420

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

glomap/estimators/bundle_adjustment.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <colmap/estimators/cost_functions.h>
44
#include <colmap/estimators/manifold.h>
55
#include <colmap/sensor/models.h>
6+
#include <colmap/util/cuda.h>
7+
#include <colmap/util/misc.h>
68

79
namespace glomap {
810

@@ -36,6 +38,57 @@ bool BundleAdjuster::Solve(const ViewGraph& view_graph,
3638
// Set the solver options.
3739
ceres::Solver::Summary summary;
3840

41+
int num_images = images.size();
42+
#ifdef GLOMAP_CUDA_ENABLED
43+
bool cuda_solver_enabled = false;
44+
45+
#if (CERES_VERSION_MAJOR >= 3 || \
46+
(CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2)) && \
47+
!defined(CERES_NO_CUDA)
48+
if (options_.use_gpu && num_images >= options_.min_num_images_gpu_solver) {
49+
cuda_solver_enabled = true;
50+
options_.solver_options.dense_linear_algebra_library_type = ceres::CUDA;
51+
}
52+
#else
53+
if (options_.use_gpu) {
54+
LOG_FIRST_N(WARNING, 1)
55+
<< "Requested to use GPU for bundle adjustment, but Ceres was "
56+
"compiled without CUDA support. Falling back to CPU-based dense "
57+
"solvers.";
58+
}
59+
#endif
60+
61+
#if (CERES_VERSION_MAJOR >= 3 || \
62+
(CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 3)) && \
63+
!defined(CERES_NO_CUDSS)
64+
if (options_.use_gpu && num_images >= options_.min_num_images_gpu_solver) {
65+
cuda_solver_enabled = true;
66+
options_.solver_options.sparse_linear_algebra_library_type =
67+
ceres::CUDA_SPARSE;
68+
}
69+
#else
70+
if (options_.use_gpu) {
71+
LOG_FIRST_N(WARNING, 1)
72+
<< "Requested to use GPU for bundle adjustment, but Ceres was "
73+
"compiled without cuDSS support. Falling back to CPU-based sparse "
74+
"solvers.";
75+
}
76+
#endif
77+
78+
if (cuda_solver_enabled) {
79+
const std::vector<int> gpu_indices = colmap::CSVToVector<int>(options_.gpu_index);
80+
THROW_CHECK_GT(gpu_indices.size(), 0);
81+
colmap::SetBestCudaDevice(gpu_indices[0]);
82+
}
83+
#else
84+
if (options_.use_gpu) {
85+
LOG_FIRST_N(WARNING, 1)
86+
<< "Requested to use GPU for bundle adjustment, but COLMAP was "
87+
"compiled without CUDA support. Falling back to CPU-based "
88+
"solvers.";
89+
}
90+
#endif // GLOMAP_CUDA_ENABLED
91+
3992
// Do not use the iterative solver, as it does not seem to be helpful
4093
options_.solver_options.linear_solver_type = ceres::SPARSE_SCHUR;
4194
options_.solver_options.preconditioner_type = ceres::CLUSTER_TRIDIAGONAL;

glomap/estimators/bundle_adjustment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct BundleAdjusterOptions : public OptimizationBaseOptions {
1616
bool optimize_intrinsics = true;
1717
bool optimize_points = true;
1818

19-
bool use_gpu = false;
19+
bool use_gpu = true;
2020
std::string gpu_index = "-1";
2121
int min_num_images_gpu_solver = 300;
2222

glomap/estimators/global_positioning.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ struct GlobalPositionerOptions : public OptimizationBaseOptions {
2929
bool optimize_points = true;
3030
bool optimize_scales = true;
3131

32-
bool use_gpu = false;
32+
bool use_gpu = true;
3333
std::string gpu_index = "-1";
34-
int min_num_images_gpu_solver = 2000;
34+
int min_num_images_gpu_solver = 1000;
3535

3636
// Constrain the minimum number of views per track
3737
int min_num_view_per_track = 3;

glomap/glomap_gpu.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ int ShowHelp(
1313
<< std::endl;
1414

1515
#ifdef GLOMAP_CUDA_ENABLED
16-
std::cout << "\nThis version was compiled with CUDA!\n" << std::endl;
16+
std::cout << "This version was compiled with CUDA!" << std::endl
17+
<< std::endl;
1718
#else
18-
std::cout << "\nThis version was NOT compiled CUDA!\n" << std::endl;
19+
std::cout << "This version was NOT compiled CUDA!" << std::endl
20+
<< std::endl;
1921
#endif
2022

2123
std::cout << "Usage:" << std::endl;

0 commit comments

Comments
 (0)