Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.28)

project(glomap VERSION 1.0.0)
project(glomap_gpu VERSION 1.0.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the original project name.

We should add an option to enable/disable CUDA support (similar to how colmap works).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I was fixed the naming back to glomap. The commit has the GlobalPositioning.use_gpu and BundleAdjustment.use_gpu options in option_manager.cc


set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
68 changes: 66 additions & 2 deletions cmake/FindDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ FetchContent_Declare(PoseLib
SYSTEM
)
message(STATUS "Configuring PoseLib...")
if (FETCH_POSELIB)
if (FETCH_POSELIB)
FetchContent_MakeAvailable(PoseLib)
else()
find_package(PoseLib REQUIRED)
Expand All @@ -42,9 +42,73 @@ FetchContent_Declare(COLMAP
)
message(STATUS "Configuring COLMAP...")
set(UNINSTALL_ENABLED OFF CACHE INTERNAL "")
if (FETCH_COLMAP)
if (FETCH_COLMAP)
FetchContent_MakeAvailable(COLMAP)
else()
find_package(COLMAP REQUIRED)
endif()
message(STATUS "Configuring COLMAP... done")

set(CUDA_MIN_VERSION "7.0")
if(CUDA_ENABLED)
if(CMAKE_VERSION VERSION_LESS 3.17)
find_package(CUDA QUIET)
if(CUDA_FOUND)
message(STATUS "Found CUDA version ${CUDA_VERSION} installed in "
"${CUDA_TOOLKIT_ROOT_DIR} via legacy CMake (<3.17) module. "
"Using the legacy CMake module means that any installation of "
"COLMAP will require that the CUDA libraries are "
"available under LD_LIBRARY_PATH.")
message(STATUS "Found CUDA ")
message(STATUS " Includes : ${CUDA_INCLUDE_DIRS}")
message(STATUS " Libraries : ${CUDA_LIBRARIES}")

enable_language(CUDA)

macro(declare_imported_cuda_target module)
add_library(CUDA::${module} INTERFACE IMPORTED)
target_include_directories(
CUDA::${module} INTERFACE ${CUDA_INCLUDE_DIRS})
target_link_libraries(
CUDA::${module} INTERFACE ${CUDA_${module}_LIBRARY} ${ARGN})
endmacro()

declare_imported_cuda_target(cudart ${CUDA_LIBRARIES})
declare_imported_cuda_target(curand ${CUDA_LIBRARIES})

set(CUDAToolkit_VERSION "${CUDA_VERSION_STRING}")
set(CUDAToolkit_BIN_DIR "${CUDA_TOOLKIT_ROOT_DIR}/bin")
else()
message(STATUS "CUDA not found")
endif()
else()
find_package(CUDAToolkit QUIET)
if(CUDAToolkit_FOUND)
set(CUDA_FOUND ON)
enable_language(CUDA)
else()
message(STATUS "CUDA not found")
endif()
endif()
endif()

if(CUDA_ENABLED AND CUDA_FOUND)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES "native")
endif()

add_definitions("-DGLOMAP_CUDA_ENABLED")

# Do not show warnings if the architectures are deprecated.
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
# Explicitly set PIC flags for CUDA targets.
if(NOT IS_MSVC)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --compiler-options -fPIC")
endif()

message(STATUS "Enabling CUDA support (version: ${CUDAToolkit_VERSION}, "
"archs: ${CMAKE_CUDA_ARCHITECTURES})")
else()
set(CUDA_ENABLED OFF)
message(STATUS "Disabling CUDA support")
endif()
26 changes: 13 additions & 13 deletions glomap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,33 @@ set(HEADERS
scene/types.h
)

add_library(glomap ${SOURCES} ${HEADERS})
add_library(glomap_gpu ${SOURCES} ${HEADERS})
if(NOT FETCH_COLMAP)
target_link_libraries(glomap PUBLIC colmap::colmap)
target_link_libraries(glomap_gpu PUBLIC colmap::colmap)
else()
target_link_libraries(glomap PUBLIC colmap)
target_link_libraries(glomap_gpu PUBLIC colmap)
endif()

if(NOT FETCH_POSELIB)
target_link_libraries(glomap PUBLIC PoseLib::PoseLib)
target_link_libraries(glomap_gpu PUBLIC PoseLib::PoseLib)
else()
target_link_libraries(glomap PUBLIC PoseLib)
target_link_libraries(glomap_gpu PUBLIC PoseLib)
endif()

target_link_libraries(
glomap
glomap_gpu
PUBLIC
Eigen3::Eigen
Ceres::ceres
SuiteSparse::CHOLMOD
${BOOST_LIBRARIES}
)
target_include_directories(glomap PUBLIC ..)
target_include_directories(glomap_gpu PUBLIC ..)

if(MSVC)
target_compile_options(glomap PRIVATE /bigobj)
target_compile_options(glomap_gpu PRIVATE /bigobj)
else()
target_compile_options(glomap PRIVATE
target_compile_options(glomap_gpu PRIVATE
-Wall
-Werror
-Wno-sign-compare
Expand All @@ -99,12 +99,12 @@ endif()


add_executable(glomap_main
glomap.cc
glomap_gpu.cc
exe/global_mapper.h
exe/global_mapper.cc)
target_link_libraries(glomap_main glomap)
target_link_libraries(glomap_main glomap_gpu)

set_target_properties(glomap_main PROPERTIES OUTPUT_NAME glomap)
set_target_properties(glomap_main PROPERTIES OUTPUT_NAME glomap_gpu)
install(TARGETS glomap_main DESTINATION bin)


Expand All @@ -115,7 +115,7 @@ if(TESTS_ENABLED)
target_link_libraries(
glomap_test
PRIVATE
glomap
glomap_gpu
GTest::gtest
GTest::gtest_main)
add_test(NAME glomap_test COMMAND glomap_test)
Expand Down
6 changes: 6 additions & 0 deletions glomap/controllers/option_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ void OptionManager::AddGlobalPositionerOptions() {
return;
}
added_global_positioning_options_ = true;
AddAndRegisterDefaultOption("GlobalPositioning.use_gpu",
&mapper->opt_gp.use_gpu);
AddAndRegisterDefaultOption("GlobalPositioning.optimize_positions",
&mapper->opt_gp.optimize_positions);
AddAndRegisterDefaultOption("GlobalPositioning.optimize_points",
Expand All @@ -199,6 +201,10 @@ void OptionManager::AddBundleAdjusterOptions() {
return;
}
added_bundle_adjustment_options_ = true;
AddAndRegisterDefaultOption("BundleAdjustment.use_gpu",
&mapper->opt_ba.use_gpu);
AddAndRegisterDefaultOption("BundleAdjustment.gpu_index",
&mapper->opt_ba.gpu_index);
AddAndRegisterDefaultOption("BundleAdjustment.optimize_rotations",
&mapper->opt_ba.optimize_rotations);
AddAndRegisterDefaultOption("BundleAdjustment.optimize_translation",
Expand Down
53 changes: 53 additions & 0 deletions glomap/estimators/bundle_adjustment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <colmap/estimators/cost_functions.h>
#include <colmap/estimators/manifold.h>
#include <colmap/sensor/models.h>
#include <colmap/util/cuda.h>
#include <colmap/util/misc.h>

namespace glomap {

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

int num_images = images.size();
#ifdef GLOMAP_CUDA_ENABLED
bool cuda_solver_enabled = false;

#if (CERES_VERSION_MAJOR >= 3 || \
(CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2)) && \
!defined(CERES_NO_CUDA)
if (options_.use_gpu && num_images >= options_.min_num_images_gpu_solver) {
cuda_solver_enabled = true;
options_.solver_options.dense_linear_algebra_library_type = ceres::CUDA;
}
#else
if (options_.use_gpu) {
LOG_FIRST_N(WARNING, 1)
<< "Requested to use GPU for bundle adjustment, but Ceres was "
"compiled without CUDA support. Falling back to CPU-based dense "
"solvers.";
}
#endif

#if (CERES_VERSION_MAJOR >= 3 || \
(CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 3)) && \
!defined(CERES_NO_CUDSS)
if (options_.use_gpu && num_images >= options_.min_num_images_gpu_solver) {
cuda_solver_enabled = true;
options_.solver_options.sparse_linear_algebra_library_type =
ceres::CUDA_SPARSE;
}
#else
if (options_.use_gpu) {
LOG_FIRST_N(WARNING, 1)
<< "Requested to use GPU for bundle adjustment, but Ceres was "
"compiled without cuDSS support. Falling back to CPU-based sparse "
"solvers.";
}
#endif

if (cuda_solver_enabled) {
const std::vector<int> gpu_indices = colmap::CSVToVector<int>(options_.gpu_index);
THROW_CHECK_GT(gpu_indices.size(), 0);
colmap::SetBestCudaDevice(gpu_indices[0]);
}
#else
if (options_.use_gpu) {
LOG_FIRST_N(WARNING, 1)
<< "Requested to use GPU for bundle adjustment, but COLMAP was "
"compiled without CUDA support. Falling back to CPU-based "
"solvers.";
}
#endif // GLOMAP_CUDA_ENABLED

// Do not use the iterative solver, as it does not seem to be helpful
options_.solver_options.linear_solver_type = ceres::SPARSE_SCHUR;
options_.solver_options.preconditioner_type = ceres::CLUSTER_TRIDIAGONAL;
Expand Down
4 changes: 4 additions & 0 deletions glomap/estimators/bundle_adjustment.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ struct BundleAdjusterOptions : public OptimizationBaseOptions {
bool optimize_intrinsics = true;
bool optimize_points = true;

bool use_gpu = true;
std::string gpu_index = "-1";
int min_num_images_gpu_solver = 300;

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

Expand Down
53 changes: 53 additions & 0 deletions glomap/estimators/global_positioning.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "glomap/estimators/global_positioning.h"

#include "glomap/estimators/cost_function.h"
#include <colmap/util/cuda.h>
#include <colmap/util/misc.h>

namespace glomap {
namespace {
Expand Down Expand Up @@ -361,6 +363,57 @@ void GlobalPositioner::ParameterizeVariables(
}
}

int num_images = images.size();
#ifdef GLOMAP_CUDA_ENABLED
bool cuda_solver_enabled = false;

#if (CERES_VERSION_MAJOR >= 3 || \
(CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 2)) && \
!defined(CERES_NO_CUDA)
if (options_.use_gpu && num_images >= options_.min_num_images_gpu_solver) {
cuda_solver_enabled = true;
options_.solver_options.dense_linear_algebra_library_type = ceres::CUDA;
}
#else
if (options_.use_gpu) {
LOG_FIRST_N(WARNING, 1)
<< "Requested to use GPU for bundle adjustment, but Ceres was "
"compiled without CUDA support. Falling back to CPU-based dense "
"solvers.";
}
#endif

#if (CERES_VERSION_MAJOR >= 3 || \
(CERES_VERSION_MAJOR == 2 && CERES_VERSION_MINOR >= 3)) && \
!defined(CERES_NO_CUDSS)
if (options_.use_gpu && num_images >= options_.min_num_images_gpu_solver) {
cuda_solver_enabled = true;
options_.solver_options.sparse_linear_algebra_library_type =
ceres::CUDA_SPARSE;
}
#else
if (options_.use_gpu) {
LOG_FIRST_N(WARNING, 1)
<< "Requested to use GPU for bundle adjustment, but Ceres was "
"compiled without cuDSS support. Falling back to CPU-based sparse "
"solvers.";
}
#endif

if (cuda_solver_enabled) {
const std::vector<int> gpu_indices = colmap::CSVToVector<int>(options_.gpu_index);
THROW_CHECK_GT(gpu_indices.size(), 0);
colmap::SetBestCudaDevice(gpu_indices[0]);
}
#else
if (options_.use_gpu) {
LOG_FIRST_N(WARNING, 1)
<< "Requested to use GPU for bundle adjustment, but COLMAP was "
"compiled without CUDA support. Falling back to CPU-based "
"solvers.";
}
#endif // GLOMAP_CUDA_ENABLED

// Set up the options for the solver
// Do not use iterative solvers, for its suboptimal performance.
if (tracks.size() > 0) {
Expand Down
4 changes: 4 additions & 0 deletions glomap/estimators/global_positioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct GlobalPositionerOptions : public OptimizationBaseOptions {
bool optimize_points = true;
bool optimize_scales = true;

bool use_gpu = true;
std::string gpu_index = "-1";
int min_num_images_gpu_solver = 1000;

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

Expand Down
8 changes: 8 additions & 0 deletions glomap/glomap.cc → glomap/glomap_gpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ int ShowHelp(
std::cout << "GLOMAP -- Global Structure-from-Motion" << std::endl
<< std::endl;

#ifdef GLOMAP_CUDA_ENABLED
std::cout << "This version was compiled with CUDA!" << std::endl
<< std::endl;
#else
std::cout << "This version was NOT compiled CUDA!" << std::endl
<< std::endl;
#endif

std::cout << "Usage:" << std::endl;
std::cout << " glomap mapper --database_path DATABASE --output_path MODEL"
<< std::endl;
Expand Down
Loading