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 34c9707
Show file tree
Hide file tree
Showing 3 changed files with 28 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
16 changes: 14 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ if(CMAKE_COMPILER_IS_GNUCXX)
AddCompilerFlag("-Werror=return-type")
AddCompilerFlag("-Werror=switch")
AddCompilerFlag("-Werror=return-local-addr")
# 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()

# ==============================================================================
Expand Down Expand Up @@ -346,8 +355,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 34c9707

Please sign in to comment.