Skip to content

Commit

Permalink
Merge pull request #198 from jumonatr/memory
Browse files Browse the repository at this point in the history
Simplified Memory Ownership
  • Loading branch information
RainerKuemmerle authored Sep 2, 2017
2 parents dd0b67b + 4494f8a commit 5572908
Show file tree
Hide file tree
Showing 46 changed files with 675 additions and 617 deletions.
100 changes: 60 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
SET(CMAKE_LEGACY_CYGWIN_WIN32 0)

PROJECT(g2o)
Expand Down Expand Up @@ -74,33 +74,6 @@ LIST(APPEND CMAKE_MODULE_PATH ${g2o_SOURCE_DIR}/cmake_modules)
IF(WIN32)
ADD_DEFINITIONS(-DWINDOWS)
MESSAGE(STATUS "Compiling on Windows")
IF(MSVC)
MESSAGE(STATUS "Compiling with MSVC")
ADD_DEFINITIONS(-DNOMINMAX)
ADD_DEFINITIONS(-D_USE_MATH_DEFINES)

# exception handling
add_definitions("/EHsc")

# SSE2 optimizations
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) # No need to specify if building for x64 (actually, it generates an annoying warning)
ADD_DEFINITIONS("/arch:SSE2")
endif()

IF (BUILD_SHARED_LIBS)
# disable warning on missing DLL interfaces
ADD_DEFINITIONS("/wd4251")
ENDIF()
# Fix issue: https://github.com/RainerKuemmerle/g2o/issues/66
# Link error LNK2005 due to duplicated symbols
add_definitions("/Ob2")
# Fix other stupid warnings:
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) # Avoid deprecated fprintf(), etc.
add_definitions("/nologo")
add_definitions("/wd4244") # Conversion from double -> int
add_definitions("/wd4267") # Conversion during return
add_definitions("/wd4522") # Duplicated operator=() in Eigen headers
ENDIF(MSVC)
ELSEIF(CYGWIN)
MESSAGE(STATUS "Compiling on Cygwin")
ADD_DEFINITIONS(-DCYGWIN)
Expand Down Expand Up @@ -189,6 +162,9 @@ IF(G2O_BUILD_EXAMPLES)
MESSAGE(STATUS "Compiling g2o examples")
ENDIF(G2O_BUILD_EXAMPLES)

OPTION(G2O_FAST_MATH "Enable fast math operations" OFF)
OPTION(G2O_NO_IMPLICIT_OWNERSHIP_OF_OBJECTS "Disables memory management in the graph types, this requires the callers to manager the memory of edges and nodes" OFF)

# Compiler specific options for gcc
IF(CMAKE_COMPILER_IS_GNUCXX)
OPTION (BUILD_WITH_MARCH_NATIVE "Build with \"-march native\"" ON)
Expand All @@ -199,6 +175,11 @@ IF(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -msse4.2")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -msse4.2")
ENDIF()

IF(G2O_FAST_MATH)
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ffast-math")
ENDIF()

# switch off optimization for debug builds
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
Expand Down Expand Up @@ -229,19 +210,58 @@ IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
#SET(g2o_CXX_FLAGS "${g2o_CXX_FLAGS} -Wall -stdlib=libc++")
ENDIF()

# C++11 support
IF(NOT MSVC)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
IF(MSVC)
MESSAGE(STATUS "Compiling with MSVC")

IF (CMAKE_GENERATOR MATCHES "ARM(64)?$")
SET(MSVC_ARM ON)
ENDIF()

ADD_DEFINITIONS(-DNOMINMAX)
ADD_DEFINITIONS(-D_USE_MATH_DEFINES)

# exception handling
ADD_DEFINITIONS("/EHsc")

IF (G2O_FAST_MATH)
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /fp:fast")
ENDIF()

SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /Oi")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /Oi")

SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")

# SSE2 optimizations
# No need to specify if building for x64 (actually, it generates an annoying warning)
if (NOT MSVC_ARM)
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
ADD_DEFINITIONS("/arch:SSE2")
endif()
endif()
endif()

IF (BUILD_SHARED_LIBS)
# disable warning on missing DLL interfaces
ADD_DEFINITIONS("/wd4251")
ENDIF()

# Fix issue: https://github.com/RainerKuemmerle/g2o/issues/66
# Link error LNK2005 due to duplicated symbols
add_definitions("/Ob2")
# Fix other stupid warnings:
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) # Avoid deprecated fprintf(), etc.
add_definitions("/nologo")
# TODO not sure this should be a thing
add_definitions("/wd4244") # Conversion from number_t -> int
add_definitions("/wd4267") # Conversion during return
add_definitions("/wd4522") # Duplicated operator=() in Eigen headers

ENDIF(MSVC)

# C++11 support
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# specifying compiler flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${g2o_CXX_FLAGS}")
Expand Down
8 changes: 8 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#cmakedefine G2O_HAVE_CHOLMOD 1
#cmakedefine G2O_HAVE_CSPARSE 1

#cmakedefine G2O_NO_IMPLICIT_OWNERSHIP_OF_OBJECTS

#ifdef G2O_NO_IMPLICIT_OWNERSHIP_OF_OBJECTS
#define G2O_DELETE_IMPLICITLY_OWNED_OBJECTS 0
#else
#define G2O_DELETE_IMPLICITLY_OWNED_OBJECTS 1
#endif

#cmakedefine G2O_CXX_COMPILER "@G2O_CXX_COMPILER@"

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions g2o/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ADD_LIBRARY(core ${G2O_LIB_TYPE}
dynamic_aligned_buffer.hpp
ownership.h
base_edge.h
base_binary_edge.h hyper_graph_action.cpp
base_binary_edge.hpp hyper_graph_action.h
Expand Down
46 changes: 28 additions & 18 deletions g2o/core/block_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@

#ifndef G2O_BLOCK_SOLVER_H
#define G2O_BLOCK_SOLVER_H

#include <Eigen/Core>
#include "solver.h"
#include "linear_solver.h"
#include "sparse_block_matrix.h"
#include "sparse_block_matrix_diagonal.h"
#include "openmp_mutex.h"
#include "g2o/config.h"
#include "dynamic_aligned_buffer.hpp"

#include <memory>

namespace g2o {

Expand Down Expand Up @@ -93,9 +97,9 @@ namespace g2o {
* \brief Implementation of a solver operating on the blocks of the Hessian
*/
template <typename Traits>
class BlockSolver: public BlockSolverBase {
class BlockSolver: public BlockSolverBase
{
public:

static const int PoseDim = Traits::PoseDim;
static const int LandmarkDim = Traits::LandmarkDim;
typedef typename Traits::PoseMatrixType PoseMatrixType;
Expand All @@ -116,7 +120,7 @@ namespace g2o {
* NOTE: The BlockSolver assumes exclusive access to the linear solver and will therefore free the pointer
* in its destructor.
*/
BlockSolver(LinearSolverType* linearSolver);
BlockSolver(std::unique_ptr<LinearSolverType> linearSolver);
~BlockSolver();

virtual bool init(SparseOptimizer* optmizer, bool online = false);
Expand All @@ -131,7 +135,7 @@ namespace g2o {
virtual bool schur() { return _doSchur;}
virtual void setSchur(bool s) { _doSchur = s;}

LinearSolver<PoseMatrixType>* linearSolver() const { return _linearSolver;}
LinearSolver<PoseMatrixType>& linearSolver() const { return *_linearSolver;}

virtual void setWriteDebug(bool writeDebug);
virtual bool writeDebug() const {return _linearSolver->writeDebug();}
Expand All @@ -146,17 +150,17 @@ namespace g2o {

void deallocate();

SparseBlockMatrix<PoseMatrixType>* _Hpp;
SparseBlockMatrix<LandmarkMatrixType>* _Hll;
SparseBlockMatrix<PoseLandmarkMatrixType>* _Hpl;
std::unique_ptr<SparseBlockMatrix<PoseMatrixType>> _Hpp;
std::unique_ptr<SparseBlockMatrix<LandmarkMatrixType>> _Hll;
std::unique_ptr<SparseBlockMatrix<PoseLandmarkMatrixType>> _Hpl;

SparseBlockMatrix<PoseMatrixType>* _Hschur;
SparseBlockMatrixDiagonal<LandmarkMatrixType>* _DInvSchur;
std::unique_ptr<SparseBlockMatrix<PoseMatrixType>> _Hschur;
std::unique_ptr<SparseBlockMatrixDiagonal<LandmarkMatrixType>> _DInvSchur;

SparseBlockMatrixCCS<PoseLandmarkMatrixType>* _HplCCS;
SparseBlockMatrixCCS<PoseMatrixType>* _HschurTransposedCCS;
std::unique_ptr<SparseBlockMatrixCCS<PoseLandmarkMatrixType>> _HplCCS;
std::unique_ptr<SparseBlockMatrixCCS<PoseMatrixType>> _HschurTransposedCCS;

LinearSolver<PoseMatrixType>* _linearSolver;
std::unique_ptr<LinearSolverType> _linearSolver;

std::vector<PoseVectorType, Eigen::aligned_allocator<PoseVectorType> > _diagonalBackupPose;
std::vector<LandmarkVectorType, Eigen::aligned_allocator<LandmarkVectorType> > _diagonalBackupLandmark;
Expand All @@ -167,22 +171,28 @@ namespace g2o {

bool _doSchur;

double* _coefficients;
double* _bschur;
std::unique_ptr<double[], aligned_deleter<double>> _coefficients;
std::unique_ptr<double[], aligned_deleter<double>> _bschur;

int _numPoses, _numLandmarks;
int _sizePoses, _sizeLandmarks;
};


template<int p, int l>
using BlockSolverPL = BlockSolver< BlockSolverTraits<p, l> >;

//variable size solver
typedef BlockSolver< BlockSolverTraits<Eigen::Dynamic, Eigen::Dynamic> > BlockSolverX;
using BlockSolverX = BlockSolverPL<Eigen::Dynamic, Eigen::Dynamic>;

// solver for BA/3D SLAM
typedef BlockSolver< BlockSolverTraits<6, 3> > BlockSolver_6_3;
using BlockSolver_6_3 = BlockSolverPL<6, 3>;

// solver fo BA with scale
typedef BlockSolver< BlockSolverTraits<7, 3> > BlockSolver_7_3;
using BlockSolver_7_3 = BlockSolverPL<7, 3>;

// 2Dof landmarks 3Dof poses
typedef BlockSolver< BlockSolverTraits<3, 2> > BlockSolver_3_2;
using BlockSolver_3_2 = BlockSolverPL<3, 2>;

} // end namespace

Expand Down
Loading

0 comments on commit 5572908

Please sign in to comment.