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 overhaul #41

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*.elf
*.pyo
*.pyc

# Libraries #
#############
*.lib
Expand All @@ -34,13 +34,13 @@
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
Expand All @@ -62,6 +62,7 @@ Thumbs.db
###################
build/
fortran/build/
dependencies

# dependency directory #
########################
Expand Down
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ sudo: required
before_install:
- sudo apt-get install build-essential
- pip install --user pyyaml cpp-coveralls

# Enable C++ support
language: cpp
# Compiler selection
compiler:
- g++

install:
- source install.sh

# Build steps
script:
- mkdir build
- cd build
- ../deps/cmake/bin/cmake .. -DCMAKE_BUILD_TYPE=COVERAGE -DINPUT_FILE=../test/test_HODLR.cpp -DOUTPUT_EXECUTABLE=test_HODLR
- make
- ./test_HODLR
- cmake .. -DCMAKE_BUILD_TYPE=COVERAGE
- make test_HODLR
- ./test/test_HODLR
- cd ${TRAVIS_BUILD_DIR}

after_success:
Expand Down
191 changes: 114 additions & 77 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
project (HODLRlib)
cmake_minimum_required (VERSION 3.0)
set (CMAKE_CXX_STANDARD 11)
# Provide the input file that you want to solve for:
# This can also be passed as a command line argument:
if(NOT INPUT_FILE)
set (INPUT_FILE "")
endif()
# Here set the name for the output executable:
# This can also be passed as a command line argument:
if(NOT OUTPUT_EXECUTABLE)
set (OUTPUT_EXECUTABLE "")
endif()
cmake_policy(SET CMP0048 NEW)
project(HODLRlib VERSION 3.1415)
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 11)

#################### OPTIONS ####################

option(HODLR_BUILD_TESTS "Build tests for the HODLR library" ON)
option(USE_MKL "Use Intel MKL with Eigen." OFF)

#################### FIND DEPENDENCIES ####################

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

find_package(OpenMP REQUIRED)

if(${USE_MKL})
set(BLA_VENDOR Intel10_64lp)
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
endif ()

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(find_or_download)
find_or_download(Eigen3)

#################### SET DATATYPE TO BE USED ####################

# Set this to: "float", "double", "complex32" and "complex64"
set (DTYPE "double")
# Change this accordingly to point to the Eigen root folder
# By default, it will pick up the environment variable EIGEN_PATH
set(EIGEN_PATH "$ENV{EIGEN_PATH}")

################ FINDING MKL ####################

# Checking for MKL:
# Currently disabling MKL. Sometimes a linker error pops up
# TODO: Look into this
#if (DEFINED ENV{MKLROOT})
# add_compile_definitions(MKL_ENABLED)
# set (MKLROOT "$ENV{MKLROOT}")
# set (LDFLAGS "${LDFLAGS} -DMKL_ENABLED -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group")
# set (MKL_FLAGS "-DMKL_LP64 -DMKL_ENABLED -I${MKLROOT}/include")
#endif ()

################ FINDING EIGEN ####################
if(NOT EIGEN_PATH)
message ( FATAL_ERROR "Please point the environment variable EIGEN_PATH to the root directory of your Eigen3 installation.")
endif()
set(DTYPE
"double" CACHE STRING
"Matrix entry type: \"float\", \"double\", \"complex32\" or \"complex64\""
)
set_property(CACHE DTYPE PROPERTY STRINGS float double complex32 complex64)

################ SETTING DATATYPE TO USE:FLOAT, DOUBLE AND COMPLEX ####################
if(DTYPE STREQUAL "float")
set(DTYPE_FLAG "-DUSE_FLOAT")
set(DTYPE_FLAG "-DUSE_FLOAT")
elseif(DTYPE STREQUAL "double")
set(DTYPE_FLAG "-DUSE_DOUBLE")
set(DTYPE_FLAG "-DUSE_DOUBLE")
elseif(DTYPE STREQUAL "complex32")
set(DTYPE_FLAG "-DUSE_COMPLEX32")
set(DTYPE_FLAG "-DUSE_COMPLEX32")
elseif(DTYPE STREQUAL "complex64")
set(DTYPE_FLAG "-DUSE_COMPLEX64")
set(DTYPE_FLAG "-DUSE_COMPLEX64")
else()
message(FATAL_ERROR "Invalid Choice for Datatype. Exiting...")
message(FATAL_ERROR "Invalid Choice for Datatype. Exiting...")
endif()

################ COMPILERS: APPROPRIATELY USE INTEL OR GNU ####################
#################### COMPILER SPECIFIC SETTINGS ####################

# Toggle this to 1 when Intel Compilers are available:
set(CMAKE_CXX_FLAGS)
add_compile_options(
--std=c++11
$<$<CONFIG:>:-O3>
-funroll-loops -ffast-math -ffinite-math-only
)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DTYPE_FLAG} ${MKL_FLAGS} --std=c++11 -O3 -ipo -qopenmp -xHost -funroll-loops -ffast-math -ffinite-math-only -g -liomp5 -lpthread -ldl")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DTYPE_FLAG} ${MKL_FLAGS} --std=c++11 -O3 -funroll-loops -ffast-math -ffinite-math-only -g -fopenmp -lpthread -ldl")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo -xHost -liomp5")
endif ()

# Flags for testing coverage information
Expand All @@ -62,48 +65,82 @@ set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS} --coverage")
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS} --coverage")

################ ADDING OF THE NECESSARY HEADER FOLDERS ####################
# Adding folders which contain necessary header files:
include_directories("${EIGEN_PATH}"
"include"
)

################ MENTIONING C++ FILES TO LINK WITH HEADERS + FILE TO BUILD ####################

set(HODLRlib_SRCS
${PROJECT_SOURCE_DIR}/src/HODLR_Matrix.cpp
${PROJECT_SOURCE_DIR}/src/LowRank.cpp
${PROJECT_SOURCE_DIR}/src/HODLR_Node.cpp
${PROJECT_SOURCE_DIR}/src/HODLR.cpp
${PROJECT_SOURCE_DIR}/src/HODLR_NonSPD.cpp
${PROJECT_SOURCE_DIR}/src/HODLR_SPD.cpp
${PROJECT_SOURCE_DIR}/src/KDTree.cpp
)

if(NOT("${CMAKE_BUILD_TYPE}" STREQUAL "COVERAGE"))
add_subdirectory(examples)
add_subdirectory(test)
endif()
#################### MAIN BUILD INSTRUCTIONS ####################

if(INPUT_FILE)
add_executable(${OUTPUT_EXECUTABLE}
${INPUT_FILE}
${HODLRlib_SRCS}
)
add_library(HODLR
${PROJECT_SOURCE_DIR}/src/HODLR_Matrix.cpp
${PROJECT_SOURCE_DIR}/src/LowRank.cpp
${PROJECT_SOURCE_DIR}/src/HODLR_Node.cpp
${PROJECT_SOURCE_DIR}/src/HODLR.cpp
${PROJECT_SOURCE_DIR}/src/HODLR_NonSPD.cpp
${PROJECT_SOURCE_DIR}/src/HODLR_SPD.cpp
${PROJECT_SOURCE_DIR}/src/KDTree.cpp
)
target_link_libraries(HODLR
PUBLIC Eigen3::Eigen $<$<BOOL:USE_MKL>:${BLAS_LIBRARIES} ${LAPACK_LIBRARIES}>
PRIVATE Threads::Threads OpenMP::OpenMP_CXX
)
target_include_directories(HODLR
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_compile_definitions(HODLR
PUBLIC ${DTYPE_FLAG} $<$<BOOL:${USE_MKL}>:MKL_ENABLED>
)

if(${HODLR_BUILD_TESTS})
add_subdirectory(examples)
add_subdirectory(test)
endif()

################ SUMMARY OF BUILD INFORMATION ################
#################### SUMMARY OF BUILD INFORMATION ####################

message("")
message("#################")
message("#####################")
message("# Build options #")
message("#################")
message("#####################")
message("")
message("Input File : " ${INPUT_FILE})
message("Data Type : " ${DTYPE})
message("CXX Compiler : " ${CMAKE_CXX_COMPILER})
message("CXX_FLAGS : " ${CMAKE_CXX_FLAGS})
message("Eigen dir : " ${EIGEN_PATH})
# message("MKL dir : " ${MKLROOT})
message("Eigen dir : " ${EIGEN3_ROOT_DIR})
message("MKL dir : " $ENV{MKLROOT})
message("Build dir : " ${CMAKE_BINARY_DIR})
message("")

#################### Install instruction ####################
# Copy headers to install directory
install(
DIRECTORY ${CMAKE_SOURCE_DIR}/include/HODLR
DESTINATION ${CMAKE_INSTALL_PREFIX}/include
)
# Create targets file of HODLR
# No public dependencies so a Config file with the targets is enough
install(TARGETS HODLR EXPORT HODLRTargets DESTINATION lib)
install(EXPORT HODLRTargets
FILE HODLRTargets.cmake
NAMESPACE HODLR::
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/HODLR
)
# Create config file that organizes dependencies
include(CMakePackageConfigHelpers)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/HODLRConfig.cmake.in
${CMAKE_BINARY_DIR}/HODLRConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/HODLR
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/HODLRConfig.cmake"
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/HODLR
)
# Create version file for cmake package
write_basic_package_version_file(
HODLRConfigVersion.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/HODLRConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/HODLR
)
18 changes: 18 additions & 0 deletions cmake/Eigen3_download.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.5)
include(ExternalProject)
project(Eigen3)
ExternalProject_Add(${PROJECT_NAME}
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG master
@ADDITIONAL_GIT_SETTINGS@
SOURCE_DIR "@CMAKE_SOURCE_DIR@/dependencies/sources/${PROJECT_NAME}"
BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@/${PROJECT_NAME}_build"
INSTALL_DIR @DEPENDENCY_INSTALL_PREFIX@
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=Release
-DEIGEN_BUILD_BTL=OFF
-DEIGEN_BUILD_DOC=OFF
-DBUILD_TESTING=OFF
-DCMAKEPACKAGE_INSTALL_DIR=lib/cmake/${PROJECT_NAME}
-DPKGCONFIG_INSTALL_DIR=lib/pkgconfig
)
12 changes: 12 additions & 0 deletions cmake/HODLRConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include(CMakeFindDependencyMacro)
find_dependency(Eigen3)

if(@USE_MKL@)
set(BLA_VENDOR Intel10_64lp)
find_dependency(BLAS REQUIRED)
find_dependency(LAPACK REQUIRED)
endif()

if(NOT TARGET HODLR::HODLR)
include("${CMAKE_CURRENT_LIST_DIR}/HODLRTargets.cmake")
endif()
51 changes: 51 additions & 0 deletions cmake/find_or_download.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
macro(find_or_download PACKAGE)
set(DEPENDENCY_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/dependencies/${PACKAGE})
# Update search path and use regular find_package to add dependency
find_package(
${PACKAGE} QUIET
HINTS ${DEPENDENCY_INSTALL_PREFIX} ${CMAKE_INSTALL_PREFIX}
)

if(${${PACKAGE}_FOUND})
message(STATUS "Found dependency ${PACKAGE} installed in system.")
else()
message(STATUS "Suitable version of ${PACKAGE} not found in system.")
message(STATUS "Downloading ${PACKAGE} and building from source.")
# Prepare download instructions for dependency
configure_file(
${CMAKE_SOURCE_DIR}/cmake/${PACKAGE}_download.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}_download/CMakeLists.txt
@ONLY
)

# Configure step for download instructions
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}_download
OUTPUT_QUIET
)
if(result)
message(FATAL_ERROR "Download of dependency ${PACKAGE} failed: ${result}")
endif()

# Download, build and install dependency according to instructions
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}_download
)
if(result)
message(FATAL_ERROR "Build of dependency ${PACKAGE} failed: ${result}.")
endif()
# Update search path and use regular find_package to add dependency
find_package(${PACKAGE}
REQUIRED NO_DEFAULT_PATH
PATHS "${DEPENDENCY_INSTALL_PREFIX}"
)
message(STATUS "Using ${PACKAGE} from ${DEPENDENCY_INSTALL_PREFIX}.")
install(
DIRECTORY ${DEPENDENCY_INSTALL_PREFIX}/
DESTINATION ${CMAKE_INSTALL_PREFIX})
endif()
endmacro()
13 changes: 5 additions & 8 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
foreach(TARGET
foreach(TARGET
tutorial
example_lowrank_factorization
example
example_RPY
example_matern
example_RPY
example_matern
example_conducting_sphere_plate
)

add_executable(${TARGET}
${TARGET}.cpp
${HODLRlib_SRCS}
)

add_executable(${TARGET} ${TARGET}.cpp)
target_link_libraries(${TARGET} HODLR)
endforeach()
Loading