Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmake] Remove the use of an internal Eigen3 version and instead use the one installed on the system. #1281

Merged
8 changes: 1 addition & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,18 +410,16 @@ cpack_add_component(resources
GROUP runtime
)

set(CPACK_COMPONENTS_ALL applications headers Eigen_headers GTest_headers libraries resources)
set(CPACK_COMPONENTS_ALL applications headers GTest_headers libraries resources)

set(CPACK_COMPONENT_APPLICATIONS_DISPLAY_NAME "runSofa Application")
set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ Headers")
set(CPACK_COMPONENT_EIGEN_HEADERS_DISPLAY_NAME "Eigen Headers")
set(CPACK_COMPONENT_GTEST_HEADERS_DISPLAY_NAME "GTest Headers")
set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
set(CPACK_COMPONENT_RESOURCES_DISPLAY_NAME "Resources")

set(CPACK_COMPONENT_APPLICATIONS_GROUP "Runtime")
set(CPACK_COMPONENT_HEADERS_GROUP "Development")
set(CPACK_COMPONENT_EIGEN_HEADERS_GROUP "Development")
set(CPACK_COMPONENT_GTEST_HEADERS_GROUP "Development")
set(CPACK_COMPONENT_LIBRARIES_GROUP "Development")
set(CPACK_COMPONENT_RESOURCES_GROUP "Runtime")
Expand Down Expand Up @@ -461,10 +459,6 @@ if(CPACK_BINARY_IFW)
DISPLAY_NAME "C++ Headers"
DEPENDS development
)
cpack_ifw_configure_component(Eigen_headers
DISPLAY_NAME "Eigen Headers"
DEPENDS development
)
cpack_ifw_configure_component(GTest_headers
DISPLAY_NAME "GTest Headers"
DEPENDS development
Expand Down
3 changes: 3 additions & 0 deletions SofaKernel/SofaBase/SofaBaseConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ set(SOFABASE_TARGETS @SOFABASE_TARGETS@)

find_package(SofaSimulation REQUIRED)

# Eigen3 is required by SofaBaseTopology
find_package(Eigen3 QUIET REQUIRED)

foreach(target ${SOFABASE_TARGETS})
if(NOT TARGET ${target})
include("${CMAKE_CURRENT_LIST_DIR}/SofaBaseTargets.cmake")
Expand Down
10 changes: 10 additions & 0 deletions SofaKernel/SofaCommon/SofaCommonConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
@PACKAGE_INIT@

set(SOFACOMMON_TARGETS @SOFACOMMON_TARGETS@)
set(SOFAEIGEN2SOLVER_HAVE_OPENMP @SOFAEIGEN2SOLVER_HAVE_OPENMP@)

find_package(SofaBase REQUIRED SofaComponentBase)

# Eigen3 is required by SofaEigen2Solver and SofaRigid
find_package(Eigen3 QUIET REQUIRED)

# OpenMP might be required by SofaEigen2Solver
if (SOFAEIGEN2SOLVER_HAVE_OPENMP AND NOT TARGET OpenMP::OpenMP_CXX)
find_package(OpenMP QUIET REQUIRED)
endif()


foreach(target ${SOFACOMMON_TARGETS})
if(NOT TARGET ${target})
include("${CMAKE_CURRENT_LIST_DIR}/SofaCommonTargets.cmake")
Expand Down
1 change: 1 addition & 0 deletions SofaKernel/SofaCommon/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@
# define SOFA_EIGEN2_SOLVER_API SOFA_IMPORT_DYNAMIC_LIBRARY
#endif

#cmakedefine01 SOFAEIGEN2SOLVER_HAVE_OPENMP

#endif
3 changes: 3 additions & 0 deletions SofaKernel/SofaFramework/SofaFrameworkConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ if(SOFAHELPER_HAVE_GTEST)
find_package(GTest CONFIG QUIET REQUIRED)
endif()

# Eigen3 is required by SofaDefaultType and SofaHelper
find_package(Eigen3 QUIET REQUIRED)

foreach(target SofaHelper SofaDefaultType SofaCore)
if(NOT TARGET ${target})
include("${CMAKE_CURRENT_LIST_DIR}/SofaFrameworkTargets.cmake")
Expand Down
1 change: 0 additions & 1 deletion SofaKernel/cmake/Modules/FindSOFA.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ list(APPEND SOFA_INCLUDE_OTHER_DIRS
${SOFA_INCLUDE_EXTLIBS}/colladadom
${SOFA_INCLUDE_EXTLIBS}/csparse
${SOFA_INCLUDE_EXTLIBS}/cudpp
${SOFA_INCLUDE_EXTLIBS}/eigen-3.2.1
${SOFA_INCLUDE_EXTLIBS}/ffmpeg
${SOFA_INCLUDE_EXTLIBS}/fftpack
${SOFA_INCLUDE_EXTLIBS}/fishpack
Expand Down
13 changes: 0 additions & 13 deletions SofaKernel/extlibs/eigen/ExternalProjectConfig.cmake.in

This file was deleted.

3 changes: 3 additions & 0 deletions SofaKernel/modules/SofaBaseTopology/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ set(SOURCE_FILES
initBaseTopology.cpp
)

find_package(Eigen3 REQUIRED)

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC SofaSimulationCommon)
target_link_libraries(${PROJECT_NAME} PUBLIC Eigen3::Eigen)
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_BASE_TOPOLOGY")
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${HEADER_FILES}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#define SOFA_COMPONENT_TOPOLOGY_EDGESETGEOMETRYALGORITHMS_INL

#include <fstream>

#include <Eigen/Dense>
#include <Eigen/Jacobi>

#include <SofaBaseTopology/EdgeSetGeometryAlgorithms.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/helper/MatEigen.h>
Expand Down
13 changes: 3 additions & 10 deletions SofaKernel/modules/SofaDefaultType/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ set(HEADER_FILES
${SRC_ROOT}/DataTypeInfo.h
${SRC_ROOT}/Frame.h
${SRC_ROOT}/MapMapSparseMatrix.h
${SRC_ROOT}/MapMapSparseMatrixEigenUtils.h
${SRC_ROOT}/Mat.h
${SRC_ROOT}/MatSym.h
${SRC_ROOT}/Mat_solve_Cholesky.h
${SRC_ROOT}/Mat_solve_LU.h
${SRC_ROOT}/Mat_solve_SVD.h
${SRC_ROOT}/Quat.h
${SRC_ROOT}/Quat.inl
# ${SRC_ROOT}/RigidInertia.h
# ${SRC_ROOT}/RigidInertia.inl
${SRC_ROOT}/RigidTypes.h
${SRC_ROOT}/RigidVec6Types.h
${SRC_ROOT}/SolidTypes.h
${SRC_ROOT}/SolidTypes.inl
# ${SRC_ROOT}/SparseConstraintTypes.h
${SRC_ROOT}/TemplatesAliases.h
${SRC_ROOT}/TopologyTypes.h
${SRC_ROOT}/Vec.h
Expand All @@ -35,25 +33,20 @@ set(HEADER_FILES
${SRC_ROOT}/Color.h
)

if(EIGEN3_FOUND OR Eigen3_FOUND)
list(APPEND HEADER_FILES
${SRC_ROOT}/MapMapSparseMatrixEigenUtils.h
)
endif()

set(SOURCE_FILES
${SRC_ROOT}/BaseMatrix.cpp
${SRC_ROOT}/BoundingBox.cpp
${SRC_ROOT}/Frame.cpp
# ${SRC_ROOT}/RigidInertia.cpp
${SRC_ROOT}/SolidTypes.cpp
${SRC_ROOT}/TemplatesAliases.cpp
${SRC_ROOT}/init.cpp
)

find_package(Eigen3 REQUIRED)

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC SofaHelper)
target_link_libraries(${PROJECT_NAME} PUBLIC Eigen3::Eigen)
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>")
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_DEFAULTTYPE")
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "_d")
Expand Down
17 changes: 15 additions & 2 deletions SofaKernel/modules/SofaEigen2Solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ project(SofaEigen2Solver)

set(HEADER_FILES
EigenBaseSparseMatrix.h
EigenBaseSparseMatrix_MT.h
EigenMatrixManipulator.h
EigenSparseMatrix.h
# EigenSparseSquareMatrix.h
EigenVector.h
EigenVectorWrapper.h
SVDLinearSolver.h
Expand All @@ -21,9 +19,24 @@ set(SOURCE_FILES
initEigen2Solver.cpp
)

find_package(Eigen3 REQUIRED)

if (SOFA_OPENMP AND "${Eigen3_VERSION}" VERSION_LESS 3.2.9)
sofa_find_package(OpenMP BOTH_SCOPES) # will set/update SOFAEIGEN2SOLVER_HAVE_OPENMP
endif()
Copy link
Contributor

Choose a reason for hiding this comment

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

Without the else, if you decide to disable SOFA_OPENMP after having it enabled, you will still have SOFAEIGEN2SOLVER_HAVE_OPENMP = 1 in CMake cache.

Copy link
Contributor Author

@jnbrunet jnbrunet Mar 31, 2020

Choose a reason for hiding this comment

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

Not if #cmakedefine is used instead of #cmakedefine01

Copy link
Contributor Author

@jnbrunet jnbrunet Mar 31, 2020

Choose a reason for hiding this comment

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

Sorry, I've misread you.

SOFAEIGEN2SOLVER_HAVE_OPENMP isn't a cmake cached variable. It is unset at the beginning of each cmake call. I've tested it:

$ sed -i 's/SOFA_OPENMP:BOOL=ON/SOFA_OPENMP:BOOL=OFF/' CMakeCache.txt
$ cmake .
$ grep SOFAEIGEN2SOLVER_HAVE_OPENMP include/SofaCommon/config.h
/* #undef SOFAEIGEN2SOLVER_HAVE_OPENMP */
$ sed -i 's/SOFA_OPENMP:BOOL=OFF/SOFA_OPENMP:BOOL=ON/' CMakeCache.txt
$ cmake .
$ grep SOFAEIGEN2SOLVER_HAVE_OPENMP include/SofaCommon/config.h
#define SOFAEIGEN2SOLVER_HAVE_OPENMP
$ sed -i 's/SOFA_OPENMP:BOOL=ON/SOFA_OPENMP:BOOL=OFF/' CMakeCache.txt
$ cmake .
$ grep SOFAEIGEN2SOLVER_HAVE_OPENMP include/SofaCommon/config.h
/* #undef SOFAEIGEN2SOLVER_HAVE_OPENMP */

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, thanks for the tests 👍
What happens if #cmakedefine01 is used instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OFF : #define SOFAEIGEN2SOLVER_HAVE_OPENMP 0
ON : #define SOFAEIGEN2SOLVER_HAVE_OPENMP 1
OFF : #define SOFAEIGEN2SOLVER_HAVE_OPENMP 0

and in cpp files, #ifdef SOFAEIGEN2SOLVER_HAVE_OPENMP will always be true since it is always defined

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes because the idea behind #cmakedefine01 is to permit the usage of #if and if(). #ifdef surely becomes useless though.


if (SOFAEIGEN2SOLVER_HAVE_OPENMP)
list(APPEND HEADER_FILES EigenBaseSparseMatrix_MT.h)
endif()

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC SofaBaseLinearSolver)
target_link_libraries(${PROJECT_NAME} PUBLIC Eigen3::Eigen)
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_EIGEN2_SOLVER")
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${HEADER_FILES}")

if (SOFAEIGEN2SOLVER_HAVE_OPENMP)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()

sofa_install_targets(SofaCommon ${PROJECT_NAME} "SofaCommon/${PROJECT_NAME}")
18 changes: 5 additions & 13 deletions SofaKernel/modules/SofaEigen2Solver/EigenBaseSparseMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef SOFA_COMPONENT_LINEARSOLVER_EigenBaseSparseMatrix_H
#define SOFA_COMPONENT_LINEARSOLVER_EigenBaseSparseMatrix_H

#include <SofaEigen2Solver/config.h>
#include <sofa/defaulttype/BaseMatrix.h>
#include <sofa/defaulttype/Mat.h>
#include <sofa/helper/SortedPermutation.h>
Expand All @@ -30,13 +31,10 @@
#include <map>
#include <Eigen/Sparse>

#ifdef _OPENMP
#if (SOFAEIGEN2SOLVER_HAVE_OPENMP == 1)
#include "EigenBaseSparseMatrix_MT.h"
#endif




namespace sofa
{

Expand All @@ -46,12 +44,6 @@ namespace component
namespace linearsolver
{

//#define EigenBaseSparseMatrix_CHECK
//#define EigenBaseSparseMatrix_VERBOSE




/** Sparse matrix based on the Eigen library.

An Eigen::SparseMatrix<Real, RowMajor> matrix is used to store the data in Compressed Row Storage mode.
Expand Down Expand Up @@ -311,7 +303,7 @@ class EigenBaseSparseMatrix : public defaulttype::BaseMatrix
void mult_MT( VectorEigen& result, const VectorEigen& data )
{
compress();
#ifdef _OPENMP
#if (SOFAEIGEN2SOLVER_HAVE_OPENMP == 1)
result = linearsolver::mul_EigenSparseDenseMatrix_MT( compressedMatrix, data );
#else
result = compressedMatrix * data;
Expand Down Expand Up @@ -434,7 +426,7 @@ class EigenBaseSparseMatrix : public defaulttype::BaseMatrix
/// @warning res MUST NOT be the same variable as this or rhs
void mul_MT(EigenBaseSparseMatrix<Real>& res, const EigenBaseSparseMatrix<Real>& rhs) const
{
#ifdef _OPENMP
#if (SOFAEIGEN2SOLVER_HAVE_OPENMP == 1)
assert( &res != this );
assert( &res != &rhs );
((EigenBaseSparseMatrix<Real>*)this)->compress(); /// \warning this violates the const-ness of the method
Expand All @@ -455,7 +447,7 @@ class EigenBaseSparseMatrix : public defaulttype::BaseMatrix
void mul_MT( Eigen::Matrix<Real,Eigen::Dynamic,Eigen::Dynamic>& res, const Eigen::Matrix<Real,Eigen::Dynamic,Eigen::Dynamic>& rhs )
{
compress();
#ifdef _OPENMP
#if (SOFAEIGEN2SOLVER_HAVE_OPENMP == 1)
res = linearsolver::mul_EigenSparseDenseMatrix_MT( compressedMatrix, rhs );
#else
res = compressedMatrix * rhs;
Expand Down
Loading