diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b10938ff..1ee67fb76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,9 +156,16 @@ if(TBB_FOUND AND GTSAM_WITH_TBB) else() set(GTSAM_TBB_LIBRARIES ${TBB_LIBRARIES}) endif() +else() + set(GTSAM_USE_TBB 0) # This will go into config.h endif() +############################################################################### +# Find Google perftools +find_package(GooglePerfTools) + + ############################################################################### # Option for using system Eigen or GTSAM-bundled Eigen option(GTSAM_USE_SYSTEM_EIGEN "Find and use system-installed Eigen. If 'off', use the one bundled with GTSAM" OFF) @@ -190,6 +197,46 @@ install(FILES ${CMAKE_BINARY_DIR}/gtsam/3rdparty/gtsam_eigen_includes.h DESTINAT ############################################################################### # Global compile options +# Build list of possible allocators +set(possible_allocators "") +if(GTSAM_USE_TBB) + list(APPEND possible_allocators TBB) + set(preferred_allocator TBB) +else() + list(APPEND possible_allocators BoostPool STL) + set(preferred_allocator BoostPool) + if(GOOGLE_PERFTOOLS_FOUND) + list(APPEND possible_allocators tcmalloc) + endif() +endif() + +# Check if current allocator choice is valid +list(FIND possible_allocators "${GTSAM_DEFAULT_ALLOCATOR}" allocator_valid) +if(allocator_valid EQUAL -1) + set(force_allocator true) +endif() + +# Set allocator cache option +if(force_allocator) + set(GTSAM_DEFAULT_ALLOCATOR ${preferred_allocator} CACHE STRING "Default allocator" FORCE) +else() + set(GTSAM_DEFAULT_ALLOCATOR ${preferred_allocator} CACHE STRING "Default allocator") +endif() +set_property(CACHE GTSAM_DEFAULT_ALLOCATOR PROPERTY STRINGS ${possible_allocators}) +mark_as_advanced(GTSAM_DEFAULT_ALLOCATOR) + +# Define compile flags depending on allocator +if("${GTSAM_DEFAULT_ALLOCATOR}" STREQUAL "BoostPool") + set(GTSAM_ALLOCATOR_BOOSTPOOL 1) +elseif("${GTSAM_DEFAULT_ALLOCATOR}" STREQUAL "STL") + set(GTSAM_ALLOCATOR_STL 1) +elseif("${GTSAM_DEFAULT_ALLOCATOR}" STREQUAL "TBB") + set(GTSAM_ALLOCATOR_TBB 1) +elseif("${GTSAM_DEFAULT_ALLOCATOR}" STREQUAL "tcmalloc") + set(GTSAM_ALLOCATOR_STL 1) # tcmalloc replaces malloc, so to use it we use the STL allocator + list(APPEND GTSAM_ADDITIONAL_LIBRARIES "tcmalloc") +endif() + # Include boost - use 'BEFORE' so that a specific boost specified to CMake # takes precedence over a system-installed one. include_directories(BEFORE ${TBB_INCLUDE_DIRS} ${Boost_INCLUDE_DIR}) @@ -325,6 +372,7 @@ elseif(TBB_FOUND) else() message(STATUS " Use Intel TBB : TBB not found") endif() +message(STATUS " Default allocator : ${GTSAM_DEFAULT_ALLOCATOR}") message(STATUS "Packaging flags ") @@ -343,7 +391,7 @@ print_config_flag(${GTSAM_INSTALL_WRAP} "Install wrap utility message(STATUS "===============================================================") if(GTSAM_WITH_TBB AND NOT TBB_FOUND) - message(WARNING "GTSAM_USE_TBB is set to 'On' but TBB was not found. This is ok, but GTSAM parallelization will be disabled. Set GTSAM_USE_TBB to 'Off' to avoid this warning.") + message(WARNING "GTSAM_USE_TBB is set to 'On' but TBB was not found. This is ok, but GTSAM parallelization will be disabled. Set GTSAM_WITH_TBB to 'Off' to avoid this warning.") endif() # Include CPack *after* all flags diff --git a/gtsam/CMakeLists.txt b/gtsam/CMakeLists.txt index 6f949aa5d..77d415d86 100644 --- a/gtsam/CMakeLists.txt +++ b/gtsam/CMakeLists.txt @@ -96,7 +96,7 @@ message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") if (GTSAM_BUILD_STATIC_LIBRARY) message(STATUS "Building GTSAM - static") add_library(gtsam-static STATIC ${gtsam_srcs}) - target_link_libraries(gtsam-static ${GTSAM_BOOST_LIBRARIES} ${GTSAM_TBB_LIBRARIES}) + target_link_libraries(gtsam-static ${GTSAM_BOOST_LIBRARIES} ${GTSAM_TBB_LIBRARIES} ${GTSAM_ADDITIONAL_LIBRARIES}) set_target_properties(gtsam-static PROPERTIES OUTPUT_NAME gtsam CLEAN_DIRECT_OUTPUT 1 @@ -115,7 +115,7 @@ endif () if (GTSAM_BUILD_SHARED_LIBRARY) message(STATUS "Building GTSAM - shared") add_library(gtsam-shared SHARED ${gtsam_srcs}) - target_link_libraries(gtsam-shared ${GTSAM_BOOST_LIBRARIES} ${GTSAM_TBB_LIBRARIES}) + target_link_libraries(gtsam-shared ${GTSAM_BOOST_LIBRARIES} ${GTSAM_TBB_LIBRARIES} ${GTSAM_ADDITIONAL_LIBRARIES}) set_target_properties(gtsam-shared PROPERTIES OUTPUT_NAME gtsam CLEAN_DIRECT_OUTPUT 1 diff --git a/gtsam/base/FastList.h b/gtsam/base/FastList.h index 589d0a689..4b5d1caf1 100644 --- a/gtsam/base/FastList.h +++ b/gtsam/base/FastList.h @@ -54,14 +54,16 @@ public: /** Copy constructor from the base list class */ FastList(const Base& x) : Base(x) {} +#ifdef GTSAM_ALLOCATOR_BOOSTPOOL /** Copy constructor from a standard STL container */ - FastList(const std::list& x, typename boost::disable_if_c::isSTL>::type* = 0) { + FastList(const std::list& x) { // This if statement works around a bug in boost pool allocator and/or // STL vector where if the size is zero, the pool allocator will allocate // huge amounts of memory. if(x.size() > 0) Base::assign(x.begin(), x.end()); } +#endif /** Conversion to a standard STL container */ operator std::list() const { diff --git a/gtsam/base/FastSet.h b/gtsam/base/FastSet.h index 25f012201..7ed6c7cd7 100644 --- a/gtsam/base/FastSet.h +++ b/gtsam/base/FastSet.h @@ -78,14 +78,16 @@ public: Base(x) { } +#ifdef GTSAM_ALLOCATOR_BOOSTPOOL /** Copy constructor from a standard STL container */ - FastSet(const std::set& x, typename boost::disable_if_c::isSTL>::type* = 0) { + FastSet(const std::set& x) { // This if statement works around a bug in boost pool allocator and/or // STL vector where if the size is zero, the pool allocator will allocate // huge amounts of memory. if(x.size() > 0) Base::insert(x.begin(), x.end()); } +#endif /** Conversion to a standard STL container */ operator std::set() const { diff --git a/gtsam/config.h.in b/gtsam/config.h.in index 4ff9a499e..db1c30e85 100644 --- a/gtsam/config.h.in +++ b/gtsam/config.h.in @@ -41,3 +41,8 @@ // Whether we are using TBB (if TBB was found and GTSAM_WITH_TBB is enabled in CMake) #cmakedefine GTSAM_USE_TBB + +// The default allocator to use +#cmakedefine GTSAM_ALLOCATOR_BOOSTPOOL +#cmakedefine GTSAM_ALLOCATOR_TBB +#cmakedefine GTSAM_ALLOCATOR_STL diff --git a/gtsam_extra.cmake.in b/gtsam_extra.cmake.in index 8b7060b14..70330fbb7 100644 --- a/gtsam_extra.cmake.in +++ b/gtsam_extra.cmake.in @@ -6,6 +6,5 @@ set (GTSAM_VERSION_PATCH @GTSAM_VERSION_PATCH@) set (GTSAM_VERSION_NUMERIC @GTSAM_VERSION_NUMERIC@) set (GTSAM_VERSION_STRING "@GTSAM_VERSION_STRING@") -if(@GTSAM_USE_TBB@) - set (GTSAM_USE_TBB @GTSAM_USE_TBB@) -endif() +set (GTSAM_USE_TBB @GTSAM_USE_TBB@) +set (GTSAM_DEFAULT_ALLOCATOR @GTSAM_DEFAULT_ALLOCATOR@)