From 44a92001d0514ddda5d176caa6c5a81998890979 Mon Sep 17 00:00:00 2001 From: vfisikop <160006479+vfisikop@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:02:29 +0200 Subject: [PATCH] Enable c++17 support in tests and fix saxpy calls (#292) * Enable c++17 support in tests and fix saxpy calls * Fix structure, code style and tests in autodiff --- examples/autopoint/CMakeLists.txt | 18 ++-- .../autopoint/Gaussian_mixture_autopoint.cpp | 1 + ...idimensionalGaussian_mixture_autopoint.cpp | 3 +- .../userDefinedFunction_autopoint.cpp | 3 +- .../userDefinedFunction_manual_gradient.cpp | 3 +- examples/correlation_matrices/CMakeLists.txt | 11 ++- .../CMakeLists.txt | 2 - include/cartesian_geom/autopoint.h | 28 ++++-- .../ode_solvers/oracle_autodiff_functors.hpp | 94 +++++++++++++++++++ include/ode_solvers/oracle_functors.hpp | 86 ----------------- include/preprocess/crhmc/crhmc_utils.h | 2 +- include/preprocess/crhmc/two_sided_barrier.h | 4 +- .../crhmc/weighted_two_sided_barrier.h | 4 +- test/CMakeLists.txt | 1 + 14 files changed, 146 insertions(+), 114 deletions(-) create mode 100644 include/ode_solvers/oracle_autodiff_functors.hpp diff --git a/examples/autopoint/CMakeLists.txt b/examples/autopoint/CMakeLists.txt index d96e6dac2..77e592dd0 100644 --- a/examples/autopoint/CMakeLists.txt +++ b/examples/autopoint/CMakeLists.txt @@ -11,8 +11,7 @@ project( VolEsti ) option(DISABLE_NLP_ORACLES "Disable non-linear oracles (used in collocation)" ON) option(BUILTIN_EIGEN "Use eigen from ../external" OFF) -option(BUILTIN_AUTODIFF "Use eigen from ../external" OFF) -option(USE_MKL "Use MKL library to build eigen" ON) +option(USE_MKL "Use MKL library to build eigen" OFF) CMAKE_MINIMUM_REQUIRED(VERSION 3.17) @@ -61,8 +60,15 @@ else() endif(DISABLE_NLP_ORACLES) +option(BUILTIN_AUTODIFF "Use autodiff from ../external" OFF) include("../../external/cmake-files/Autodiff.cmake") GetAutodiff() +if (BUILTIN_AUTODIFF) + include_directories (BEFORE ../../external/_deps/Autodiff) +else () + include_directories(BEFORE /usr/local/include) +endif(BUILTIN_AUTODIFF) + include("../../external/cmake-files/Eigen.cmake") GetEigen() @@ -77,11 +83,7 @@ if (BUILTIN_EIGEN) else () include_directories(BEFORE /usr/include/eigen3) endif(BUILTIN_EIGEN) -if (BUILTIN_AUTODIFF) - include_directories (BEFORE ../../external/_deps/Autodiff) -else () - include_directories(BEFORE /usr/local/include) -endif(BUILTIN_AUTODIFF) + if (USE_MKL) find_library(BLAS @@ -141,7 +143,7 @@ endif() add_definitions(${CMAKE_CXX_FLAGS} "-std=c++17") # enable C++14 standard add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler add_definitions(${CMAKE_CXX_FLAGS} "-pthread") # optimization of the compiler - + #add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl") add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm") add_definitions(${CMAKE_CXX_FLAGS} "-DMKL_ILP64") diff --git a/examples/autopoint/Gaussian_mixture_autopoint.cpp b/examples/autopoint/Gaussian_mixture_autopoint.cpp index b0d4f2b96..44dbbd539 100644 --- a/examples/autopoint/Gaussian_mixture_autopoint.cpp +++ b/examples/autopoint/Gaussian_mixture_autopoint.cpp @@ -26,6 +26,7 @@ #include "Eigen/Eigen" #include "ode_solvers/ode_solvers.hpp" +#include "ode_solvers/oracle_autodiff_functors.hpp" #include "random.hpp" #include "random/uniform_int.hpp" #include "random/normal_distribution.hpp" diff --git a/examples/autopoint/MultidimensionalGaussian_mixture_autopoint.cpp b/examples/autopoint/MultidimensionalGaussian_mixture_autopoint.cpp index 0db40c544..432b7141a 100644 --- a/examples/autopoint/MultidimensionalGaussian_mixture_autopoint.cpp +++ b/examples/autopoint/MultidimensionalGaussian_mixture_autopoint.cpp @@ -24,6 +24,7 @@ #include "Eigen/Eigen" #include "ode_solvers/ode_solvers.hpp" +#include "ode_solvers/oracle_autodiff_functors.hpp" #include "random.hpp" #include "random/uniform_int.hpp" #include "random/normal_distribution.hpp" @@ -79,7 +80,7 @@ void run_main() } start = std::chrono::high_resolution_clock::now(); std::cerr << (long)std::chrono::duration_cast(start - stop).count(); - for (int i = 0; i < max_actual_draws; i++) { + for (int i = 0; i < max_actual_draws; i++) { std::cout << hmc.x.getCoefficients().transpose() << std::endl; hmc.apply(rng, 3); samples.col(i) = hmc.x.getCoefficients(); diff --git a/examples/autopoint/userDefinedFunction_autopoint.cpp b/examples/autopoint/userDefinedFunction_autopoint.cpp index 9762bd751..f0f7a734a 100755 --- a/examples/autopoint/userDefinedFunction_autopoint.cpp +++ b/examples/autopoint/userDefinedFunction_autopoint.cpp @@ -22,6 +22,7 @@ #include "Eigen/Eigen" #include "ode_solvers/ode_solvers.hpp" +#include "ode_solvers/oracle_autodiff_functors.hpp" #include "random.hpp" #include "random/uniform_int.hpp" #include "random/normal_distribution.hpp" @@ -77,7 +78,7 @@ void run_main() hmc.apply(rng, 3); } start = std::chrono::high_resolution_clock::now(); - for (int i = 0; i < max_actual_draws; i++) { + for (int i = 0; i < max_actual_draws; i++) { std::cout << hmc.x.getCoefficients().transpose() << std::endl; hmc.apply(rng, 3); samples.col(i) = hmc.x.getCoefficients(); diff --git a/examples/autopoint/userDefinedFunction_manual_gradient.cpp b/examples/autopoint/userDefinedFunction_manual_gradient.cpp index d4d40b10d..5224b3759 100644 --- a/examples/autopoint/userDefinedFunction_manual_gradient.cpp +++ b/examples/autopoint/userDefinedFunction_manual_gradient.cpp @@ -21,6 +21,7 @@ #include "Eigen/Eigen" #include "ode_solvers/ode_solvers.hpp" +#include "ode_solvers/oracle_autodiff_functors.hpp" #include "random.hpp" #include "random/uniform_int.hpp" #include "random/normal_distribution.hpp" @@ -130,7 +131,7 @@ void run_main() { hmc.apply(rng, 3); } start = std::chrono::high_resolution_clock::now(); - for (int i = 0; i < max_actual_draws; i++) { + for (int i = 0; i < max_actual_draws; i++) { std::cout << hmc.x.getCoefficients().transpose() << std::endl; hmc.apply(rng, 3); samples.col(i) = hmc.x.getCoefficients(); diff --git a/examples/correlation_matrices/CMakeLists.txt b/examples/correlation_matrices/CMakeLists.txt index 7871ba5fa..bfc4b513c 100644 --- a/examples/correlation_matrices/CMakeLists.txt +++ b/examples/correlation_matrices/CMakeLists.txt @@ -62,6 +62,16 @@ else() endif(DISABLE_NLP_ORACLES) +option(BUILTIN_AUTODIFF "Use autodiff from ../external" OFF) +include("../../external/cmake-files/Autodiff.cmake") +GetAutodiff() +if (BUILTIN_AUTODIFF) + include_directories (BEFORE ../../external/_deps/Autodiff) +else () + include_directories(BEFORE /usr/local/include) +endif(BUILTIN_AUTODIFF) +add_definitions(${CMAKE_CXX_FLAGS} "-std=c++17") #enable the c++17 support needed by autodiff + include("../../external/cmake-files/Eigen.cmake") GetEigen() @@ -125,7 +135,6 @@ else () add_compile_definitions("EIGEN_NO_DEBUG") endif () - add_definitions(${CMAKE_CXX_FLAGS} "-std=c++11") # enable C++11 standard add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler #add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl") add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm") diff --git a/examples/count-linear-extensions-using-hpolytope/CMakeLists.txt b/examples/count-linear-extensions-using-hpolytope/CMakeLists.txt index d42d2b7a8..f3e66b634 100644 --- a/examples/count-linear-extensions-using-hpolytope/CMakeLists.txt +++ b/examples/count-linear-extensions-using-hpolytope/CMakeLists.txt @@ -98,8 +98,6 @@ else () add_compile_definitions("EIGEN_NO_DEBUG") endif () - - add_definitions(${CMAKE_CXX_FLAGS} "-std=c++11") # enable C++11 standard add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler #add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl") add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm") diff --git a/include/cartesian_geom/autopoint.h b/include/cartesian_geom/autopoint.h index e1d73f222..41dccf47e 100644 --- a/include/cartesian_geom/autopoint.h +++ b/include/cartesian_geom/autopoint.h @@ -1,8 +1,18 @@ -#ifndef AB9D4FD9_C418_44AC_8276_CC42540EF027 -#define AB9D4FD9_C418_44AC_8276_CC42540EF027 +// VolEsti (volume computation and sampling library) + +// Copyright (c) 2024 Vissarion Fisikopoulos + +// Licensed under GNU LGPL.3, see LICENCE file + +#ifndef CARTESIAN_KERNEL_AUTOPOINT_H +#define CARTESIAN_KERNEL_AUTOPOINT_H + #include #include +/// This class manipulates a point used for automatic differentation +/// parameterized by a number type e.g. double +/// \tparam T Numerical Type template class autopoint { @@ -133,7 +143,7 @@ class autopoint temp.coeffs=coeffs.array().log().matrix(); return temp; } - + autopoint exp() const { autopoint temp; temp.d = d; @@ -217,7 +227,7 @@ class autopoint { return autopoint((Coeff)(coeffs * ((FT)k))); } - + autopoint operator* (T k) { return autopoint((Coeff)(coeffs * ((FT)k))); @@ -225,12 +235,12 @@ class autopoint // matrix multiplication autopoint operator* (const autopoint& autopoint_) { - return autopoint((Coeff)(coeffs * autopoint_.getCoefficients())); + return autopoint((Coeff)(coeffs * autopoint_.getCoefficients())); } // matrix multiplication with normal matrix autopoint operator* (const coeff& matrix_) { - return autopoint(( autopoint(matrix_) * autopoint(coeffs) )); + return autopoint(( autopoint(matrix_) * autopoint(coeffs) )); } void operator*= (const FT k) @@ -283,7 +293,7 @@ class autopoint std::cout<<"\n"; } - static autopoint all_ones(int dim) + static autopoint all_ones(int dim) { autopoint p(dim); for (int i = 0; i < dim; i++) p.set_coord(i, 1.0); @@ -298,7 +308,7 @@ autopoint operator* ( K k, autopoint const& p) return p * k; } -// matrix times autopoint +// matrix times autopoint template autopoint operator* ( Eigen::Matrix matrix_, autopoint const& p) { @@ -308,4 +318,4 @@ autopoint operator* ( Eigen::Matrix matrix -#endif /* AB9D4FD9_C418_44AC_8276_CC42540EF027 */ +#endif // CARTESIAN_KERNEL_AUTOPOINT_H diff --git a/include/ode_solvers/oracle_autodiff_functors.hpp b/include/ode_solvers/oracle_autodiff_functors.hpp new file mode 100644 index 000000000..20500141f --- /dev/null +++ b/include/ode_solvers/oracle_autodiff_functors.hpp @@ -0,0 +1,94 @@ +// VolEsti (volume computation and sampling library) + +// Copyright (c) 2012-2024 Vissarion Fisikopoulos +// Copyright (c) 2018-2020 Apostolos Chalkis + +// Licensed under GNU LGPL.3, see LICENCE file + +#ifndef ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP +#define ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP + +#include "Eigen/Eigen" +#include +#include +#include "cartesian_geom/cartesian_kernel.h" +#include "cartesian_geom/autopoint.h" + +struct AutoDiffFunctor { + + template + struct parameters { + unsigned int order; + NT L; // Lipschitz constant for gradient + NT m; // Strong convexity constant + NT kappa; // Condition number + Eigen::Matrix data; + parameters() : order(2), L(4), m(4), kappa(1){}; + }; + + template + struct FunctionFunctor_internal { + using Autopoint = autopoint; + using Coeff = typename autopoint::Coeff; + using FT = typename autopoint::FT; + using Point = typename Cartesian::Point; + + static std::function &)> pdf; + + FT static result_internal(const Coeff &x, const Eigen::Matrix &data){ + return pdf(x, data); // + } + + // external interface + Point static differentiate(Point const &x0, const Eigen::Matrix &data) { + Autopoint x = Autopoint(x0.getCoefficients()); // cast into autopoint + auto x1 = x.getCoefficients(); + Coeff y = autodiff::gradient(result_internal, autodiff::wrt(x1), autodiff::at(x1, data)); + auto result = y.template cast(); + return -1 * Point(result); + } + + NT static result(Point const &x0, const Eigen::Matrix &data) { + Autopoint x = Autopoint(x0.getCoefficients()); // cast to autopoint + auto x1 = x.getCoefficients(); + return result_internal(x1, data).val(); + } + }; + + template + struct GradientFunctor { + using NT = typename Point::FT; + + FunctionFunctor_internal F; + parameters ¶ms; + + GradientFunctor(parameters ¶ms_) : params(params_){}; + + // The index i represents the state vector index + Point operator()(unsigned int const &i, std::vector const &xs, NT const &t) const { + // std::cout<<"calling gradient functor"< + struct FunctionFunctor { + using NT = typename Point::FT; + parameters ¶ms; + + FunctionFunctor(parameters ¶ms_) : params(params_){}; + // The index i represents the state vector index + FunctionFunctor_internal F; + + NT operator()(Point const &x) const { + return F.result(x, params.data); + } + }; +}; + +#endif //ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP \ No newline at end of file diff --git a/include/ode_solvers/oracle_functors.hpp b/include/ode_solvers/oracle_functors.hpp index 80da6d425..35fc8887b 100644 --- a/include/ode_solvers/oracle_functors.hpp +++ b/include/ode_solvers/oracle_functors.hpp @@ -10,12 +10,6 @@ #ifndef ODE_SOLVERS_ORACLE_FUNCTORS_HPP #define ODE_SOLVERS_ORACLE_FUNCTORS_HPP -#include "Eigen/Eigen" -#include -#include -#include "cartesian_geom/cartesian_kernel.h" -#include "cartesian_geom/autopoint.h" - struct OptimizationFunctor { template < @@ -400,84 +394,4 @@ struct HessianFunctor { }; -struct AutoDiffFunctor { -template < - typename NT> -struct parameters { - unsigned int order; - NT L; - // Lipschitz constant for gradient - NT m; - // Strong convexity constant - NT kappa; // Condition number - Eigen::Matrix data; - parameters() : order(2), L(4), m(4), kappa(1){}; -}; - -template < - typename NT> -struct FunctionFunctor_internal { - using Autopoint =autopoint; - using Coeff=typename autopoint::Coeff; - using FT =typename autopoint::FT; - using Kernel=Cartesian; - using Point=typename Kernel::Point; - static std::function &)> pdf; - // static std::function f; - FT static result_internal(const Coeff &x, const Eigen::Matrix &data){ - return pdf(x, data); // - } - // external interface - Point static differentiate(Point const &x0, const Eigen::Matrix &data) { - Autopoint x = Autopoint(x0.getCoefficients()); // cast into autopoint - auto x1 = x.getCoefficients(); - Coeff y = autodiff::gradient(result_internal, autodiff::wrt(x1), autodiff::at(x1, data)); - auto result = y.template cast(); - return -1 * Point(result); - } - - NT static result(Point const &x0, const Eigen::Matrix &data) { - Autopoint x = Autopoint(x0.getCoefficients()); // cast to autopoint - auto x1 = x.getCoefficients(); - return result_internal(x1, data).val(); - } -}; - -template < - typename Point> -struct GradientFunctor { - typedef typename Point::FT NT; - typedef std::vector pts; - typedef FunctionFunctor_internal NegativeLogprobFunctor; - NegativeLogprobFunctor F; - parameters ¶ms; - GradientFunctor(parameters ¶ms_) : params(params_){}; - // The index i represents the state vector index - Point operator()(unsigned int const &i, pts const &xs, NT const &t) const { - // std::cout<<"calling gradient functor"< -struct FunctionFunctor { - typedef typename Point::FT NT; - parameters ¶ms; - FunctionFunctor(parameters ¶ms_) : params(params_){}; - // The index i represents the state vector index - typedef FunctionFunctor_internal NegativeLogprobFunctor; - NegativeLogprobFunctor F; - NT operator()(Point const &x) const { - NT temp = F.result(x, params.data); - // std::cout<const & idx, const Type c){ } } template -void saxpy(MatrixType &a,MatrixType const &b,MatrixType const& c, std::vectorconst & a_idx, std::vectorconst & b_idx){ +void volesti_saxpy(MatrixType &a,MatrixType const &b,MatrixType const& c, std::vectorconst & a_idx, std::vectorconst & b_idx){ for(int i=0;i class two_sided_barrier { VT c = (ub + lb) / 2; VT bias1=VT::Ones(x2, 1) * unbounded_center_coord; - saxpy(c,lb,bias1,lowerIdx,lowerIdx); + volesti_saxpy(c,lb,bias1,lowerIdx,lowerIdx); VT bias2=-VT::Ones(x1, 1) * unbounded_center_coord; - saxpy(c,ub,bias2,upperIdx,upperIdx); + volesti_saxpy(c,ub,bias2,upperIdx,upperIdx); set(c, freeIdx, 0.0); center = c; diff --git a/include/preprocess/crhmc/weighted_two_sided_barrier.h b/include/preprocess/crhmc/weighted_two_sided_barrier.h index 09f5eb798..4df8cc967 100644 --- a/include/preprocess/crhmc/weighted_two_sided_barrier.h +++ b/include/preprocess/crhmc/weighted_two_sided_barrier.h @@ -143,9 +143,9 @@ template class weighted_two_sided_barrier { VT c = (ub + lb) / 2; VT bias1=VT::Ones(x2, 1) * unbounded_center_coord; - saxpy(c,lb,bias1,lowerIdx,lowerIdx); + volesti_saxpy(c,lb,bias1,lowerIdx,lowerIdx); VT bias2=-VT::Ones(x1, 1) * unbounded_center_coord; - saxpy(c,ub,bias2,upperIdx,upperIdx); + volesti_saxpy(c,ub,bias2,upperIdx,upperIdx); set(c, freeIdx, 0.0); center = c; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4482d3b9d..3bde869d2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -191,6 +191,7 @@ add_definitions(${CMAKE_CXX_FLAGS} "-g") # enable debuger #add_definitions(${CMAKE_CXX_FLAGS} "-Wall") add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler +add_definitions(${CMAKE_CXX_FLAGS} "-std=c++17") #enable the c++17 support needed by autodiff #add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl") add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm") add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-ldl")