|
3 | 3 | #include <colmap/estimators/cost_functions.h> |
4 | 4 | #include <colmap/estimators/manifold.h> |
5 | 5 | #include <colmap/sensor/models.h> |
| 6 | +#include <colmap/util/cuda.h> |
| 7 | +#include <colmap/util/misc.h> |
6 | 8 |
|
7 | 9 | namespace glomap { |
8 | 10 |
|
@@ -36,6 +38,57 @@ bool BundleAdjuster::Solve(const ViewGraph& view_graph, |
36 | 38 | // Set the solver options. |
37 | 39 | ceres::Solver::Summary summary; |
38 | 40 |
|
| 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 | + |
39 | 92 | // Do not use the iterative solver, as it does not seem to be helpful |
40 | 93 | options_.solver_options.linear_solver_type = ceres::SPARSE_SCHUR; |
41 | 94 | options_.solver_options.preconditioner_type = ceres::CLUSTER_TRIDIAGONAL; |
|
0 commit comments