Skip to content

Commit

Permalink
Add option to compile Eigen with alignment enabled
Browse files Browse the repository at this point in the history
Eigen is most optimal when using SIMD instructions which require
overaligned buffers. In C++14 this is a problem because there's no
built-in support for correctly aligned allocations, so this Eigen
feature is currently disabled in AliceVision. C++17 solves Eigen
alignment issues completely because there now exist additional overloads
of operator new() which support correct allocation alignment. We can't
upgrade to C++17 yet, but certain compilers added support for this
feature earlier, such as -faligned-new in GCC 7.1. Therefore it makes
sense to add a configuration option to allow enabling alignment support
in Eigen in cases when the compiler is good enough.
  • Loading branch information
p12tic committed Jul 25, 2022
1 parent 4dbf0c2 commit 8c9b081
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option(AV_BUILD_LAPACK "Enable building an embedded Lapack" ON)
option(AV_BUILD_SUITESPARSE "Enable building an embedded SuiteSparse" ON)
option(AV_BUILD_FFMPEG "Enable building an embedded FFMpeg" ON)
option(AV_BUILD_ALICEVISION "Enable building of AliceVision" ON)
option(AV_EIGEN_NO_ALIGN "Disable Eigen alignment" ON)

option(AV_USE_CUDA "Enable CUDA" ON)

Expand Down
18 changes: 16 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ if(CMAKE_COMPILER_IS_GNUCXX)
AddCompilerFlag("-Werror=return-local-addr")
endif()

# Eigen requires overaligned buffers for maximum efficiency (e.g. on AVX512 buffers may need to
# be aligned to 64 bytes). AliceVision currently does not support this. Fortunately this is fixed
# in C++17. While we can't upgrade to C++17 just yet, some compilers support overaligned
# allocation feature with a separate flag, so use it if alignment is enabled in Eigen.
# See https://eigen.tuxfamily.org/dox/group__TopicUnalignedArrayAssert.html
if(NOT AV_EIGEN_NO_ALIGN)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.1)
AddCompilerFlag("-faligned-new")
endif()
endif()

# ==============================================================================
# Enable code coverage generation (only with GCC)
# ==============================================================================
Expand Down Expand Up @@ -346,8 +357,11 @@ endif()
find_package(Eigen3 3.3 REQUIRED)
if(Eigen3_FOUND OR EIGEN3_FOUND)
# message(STATUS "EIGEN_INCLUDE_DIR: ${EIGEN_INCLUDE_DIR}")
# See https://eigen.tuxfamily.org/dox/group__TopicUnalignedArrayAssert.html
set(AV_EIGEN_DEFINITIONS -DEIGEN_MAX_ALIGN_BYTES=0 -DEIGEN_MAX_STATIC_ALIGN_BYTES=0)
if(AV_EIGEN_NO_ALIGN)
set(AV_EIGEN_DEFINITIONS -DEIGEN_MAX_ALIGN_BYTES=0 -DEIGEN_MAX_STATIC_ALIGN_BYTES=0)
else()
set(AV_EIGEN_DEFINITIONS -DALICEVISION_EIGEN_REQUIRE_ALIGNMENT=1)
endif()
else()
message(FATAL_ERROR " EIGEN NOT FOUND. EIGEN_INCLUDE_DIR: ${EIGEN_INCLUDE_DIR}")
endif()
Expand Down
24 changes: 13 additions & 11 deletions src/aliceVision/numeric/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@

#pragma once

// AliceVision does not support Eigen with alignment,
// AliceVision does not support Eigen with alignment, unless C++17 aligned new feature is enabled.
// So ensure Eigen is used with the correct flags.
#ifndef EIGEN_MAX_ALIGN_BYTES
#error "EIGEN_MAX_ALIGN_BYTES is not defined"
#elif EIGEN_MAX_ALIGN_BYTES != 0
#error "EIGEN_MAX_ALIGN_BYTES is defined but not 0"
#endif

#ifndef EIGEN_MAX_STATIC_ALIGN_BYTES
#error "EIGEN_MAX_STATIC_ALIGN_BYTES is not defined"
#elif EIGEN_MAX_STATIC_ALIGN_BYTES != 0
#error "EIGEN_MAX_STATIC_ALIGN_BYTES is defined but not 0"
#ifndef ALICEVISION_EIGEN_REQUIRE_ALIGNMENT
#ifndef EIGEN_MAX_ALIGN_BYTES
#error "EIGEN_MAX_ALIGN_BYTES is not defined"
#elif EIGEN_MAX_ALIGN_BYTES != 0
#error "EIGEN_MAX_ALIGN_BYTES is defined but not 0"
#endif

#ifndef EIGEN_MAX_STATIC_ALIGN_BYTES
#error "EIGEN_MAX_STATIC_ALIGN_BYTES is not defined"
#elif EIGEN_MAX_STATIC_ALIGN_BYTES != 0
#error "EIGEN_MAX_STATIC_ALIGN_BYTES is defined but not 0"
#endif
#endif


Expand Down

0 comments on commit 8c9b081

Please sign in to comment.