From 01847d683505b02e4dce53656e15c01285603854 Mon Sep 17 00:00:00 2001 From: Abhay D Date: Wed, 10 Jan 2024 14:29:48 -0800 Subject: [PATCH 1/3] Add CMake build support --- CMakeLists.txt | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9e4b285 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,71 @@ +cmake_minimum_required(VERSION 3.13) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +project(optim LANGUAGES CXX) + +set(ARCHITECTURE "x86" CACHE STRING "CPU Architecture [x86, ARM]") +set(OPENMP ON CACHE BOOL "Enable OpenMP parallelization") +set(FP_TYPE "double" CACHE STRING "Default floating point type") +set(LINALG_LIB "eigen" CACHE STRING "Choice of linear algebra library [arma, eigen]") +set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The build type") + +include(FetchContent) + +FetchContent_Declare( + Eigen + GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git + GIT_TAG 3.4.0 + GIT_SHALLOW TRUE + GIT_PROGRESS TRUE) +FetchContent_MakeAvailable(Eigen) + +# Optimization flags + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + add_compile_options(-O0 -g) +else() # default to release + add_compile_options( + -O3 -ffp-contract=fast -flto -DNDEBUG -fPIC + ) + + if(ARCHITECTURE STREQUAL "ARM") + add_compile_options(-mcpu=native) + else() # default to x86 + add_compile_options(-march=native) + endif() + + if(OPENMP) + add_compile_options(-fopenmp) + endif() +endif() + +# Other flags + +if(LINALG_LIB STREQUAL "arma") + add_compile_options(-DOPTIM_ENABLE_ARMA_WRAPPERS -DARMA_NO_DEBUG) +else() # default to eigen + add_compile_options(-DOPTIM_ENABLE_EIGEN_WRAPPERS) +endif() + +add_compile_options(-Wall -DOPTIM_FPN_TYPE=${FP_TYPE} -lblas -llapack) + +add_library(optim SHARED + src/line_search/more_thuente.cpp + src/unconstrained/de.cpp + src/unconstrained/nm.cpp + src/unconstrained/newton.cpp + src/unconstrained/cg.cpp + src/unconstrained/bfgs.cpp + src/unconstrained/de_prmm.cpp + src/unconstrained/pso.cpp + src/unconstrained/pso_dv.cpp + src/unconstrained/gd.cpp + src/unconstrained/lbfgs.cpp + src/zeros/broyden.cpp + src/zeros/broyden_df.cpp + src/constrained/sumt.cpp +) +target_include_directories(optim PUBLIC include) +target_link_libraries(optim Eigen3::Eigen) From 8579e64ebfdecaa00acaaab18d98dd31e657046d Mon Sep 17 00:00:00 2001 From: Abhay D Date: Wed, 10 Jan 2024 14:57:04 -0800 Subject: [PATCH 2/3] Improve openmp linking --- CMakeLists.txt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e4b285..f3dcdf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The build type") include(FetchContent) +# Dependencies + FetchContent_Declare( Eigen GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git @@ -21,6 +23,10 @@ FetchContent_Declare( GIT_PROGRESS TRUE) FetchContent_MakeAvailable(Eigen) +if(OPENMP) + find_package(OpenMP REQUIRED) +endif() + # Optimization flags if(CMAKE_BUILD_TYPE STREQUAL "Debug") @@ -35,10 +41,6 @@ else() # default to release else() # default to x86 add_compile_options(-march=native) endif() - - if(OPENMP) - add_compile_options(-fopenmp) - endif() endif() # Other flags @@ -51,6 +53,8 @@ endif() add_compile_options(-Wall -DOPTIM_FPN_TYPE=${FP_TYPE} -lblas -llapack) +# Set up targets + add_library(optim SHARED src/line_search/more_thuente.cpp src/unconstrained/de.cpp @@ -68,4 +72,7 @@ add_library(optim SHARED src/constrained/sumt.cpp ) target_include_directories(optim PUBLIC include) -target_link_libraries(optim Eigen3::Eigen) +target_link_libraries(optim PRIVATE Eigen3::Eigen) +if(OPENMP) + target_link_libraries(optim PRIVATE OpenMP::OpenMP_CXX) +endif() From 198fdb68965bf8996334834ad980ba6462c0df12 Mon Sep 17 00:00:00 2001 From: Abhay D Date: Wed, 10 Jan 2024 15:19:06 -0800 Subject: [PATCH 3/3] Add arma to build --- CMakeLists.txt | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3dcdf2..b4322ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,13 +15,17 @@ include(FetchContent) # Dependencies -FetchContent_Declare( - Eigen - GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git - GIT_TAG 3.4.0 - GIT_SHALLOW TRUE - GIT_PROGRESS TRUE) -FetchContent_MakeAvailable(Eigen) +if(LINALG_LIB STREQUAL "arma") + find_package(Armadillo REQUIRED) +else() + FetchContent_Declare( + Eigen + GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git + GIT_TAG 3.4.0 + GIT_SHALLOW TRUE + GIT_PROGRESS TRUE) + FetchContent_MakeAvailable(Eigen) +endif() if(OPENMP) find_package(OpenMP REQUIRED) @@ -72,7 +76,12 @@ add_library(optim SHARED src/constrained/sumt.cpp ) target_include_directories(optim PUBLIC include) -target_link_libraries(optim PRIVATE Eigen3::Eigen) +if(LINALG_LIB STREQUAL "arma") + target_include_directories(optim PRIVATE ${ARMADILLO_INCLUDE_DIRS}) + target_link_libraries(optim PRIVATE ${ARMADILLO_LIBRARIES}) +else() + target_link_libraries(optim PRIVATE Eigen3::Eigen) +endif() if(OPENMP) target_link_libraries(optim PRIVATE OpenMP::OpenMP_CXX) endif()