Skip to content

Commit 75f2493

Browse files
vfisikopTolisChal
authored andcommitted
Enable c++17 support in tests and fix saxpy calls (GeomScale#292)
* Enable c++17 support in tests and fix saxpy calls * Fix structure, code style and tests in autodiff
1 parent 3ebe941 commit 75f2493

14 files changed

+146
-114
lines changed

examples/autopoint/CMakeLists.txt

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ project( VolEsti )
1111

1212
option(DISABLE_NLP_ORACLES "Disable non-linear oracles (used in collocation)" ON)
1313
option(BUILTIN_EIGEN "Use eigen from ../external" OFF)
14-
option(BUILTIN_AUTODIFF "Use eigen from ../external" OFF)
15-
option(USE_MKL "Use MKL library to build eigen" ON)
14+
option(USE_MKL "Use MKL library to build eigen" OFF)
1615

1716
CMAKE_MINIMUM_REQUIRED(VERSION 3.17)
1817

@@ -61,8 +60,15 @@ else()
6160

6261
endif(DISABLE_NLP_ORACLES)
6362

63+
option(BUILTIN_AUTODIFF "Use autodiff from ../external" OFF)
6464
include("../../external/cmake-files/Autodiff.cmake")
6565
GetAutodiff()
66+
if (BUILTIN_AUTODIFF)
67+
include_directories (BEFORE ../../external/_deps/Autodiff)
68+
else ()
69+
include_directories(BEFORE /usr/local/include)
70+
endif(BUILTIN_AUTODIFF)
71+
6672
include("../../external/cmake-files/Eigen.cmake")
6773
GetEigen()
6874

@@ -77,11 +83,7 @@ if (BUILTIN_EIGEN)
7783
else ()
7884
include_directories(BEFORE /usr/include/eigen3)
7985
endif(BUILTIN_EIGEN)
80-
if (BUILTIN_AUTODIFF)
81-
include_directories (BEFORE ../../external/_deps/Autodiff)
82-
else ()
83-
include_directories(BEFORE /usr/local/include)
84-
endif(BUILTIN_AUTODIFF)
86+
8587

8688
if (USE_MKL)
8789
find_library(BLAS
@@ -141,7 +143,7 @@ endif()
141143
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++17") # enable C++14 standard
142144
add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler
143145
add_definitions(${CMAKE_CXX_FLAGS} "-pthread") # optimization of the compiler
144-
146+
145147
#add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl")
146148
add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm")
147149
add_definitions(${CMAKE_CXX_FLAGS} "-DMKL_ILP64")

examples/autopoint/Gaussian_mixture_autopoint.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "Eigen/Eigen"
2727

2828
#include "ode_solvers/ode_solvers.hpp"
29+
#include "ode_solvers/oracle_autodiff_functors.hpp"
2930
#include "random.hpp"
3031
#include "random/uniform_int.hpp"
3132
#include "random/normal_distribution.hpp"

examples/autopoint/MultidimensionalGaussian_mixture_autopoint.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "Eigen/Eigen"
2525

2626
#include "ode_solvers/ode_solvers.hpp"
27+
#include "ode_solvers/oracle_autodiff_functors.hpp"
2728
#include "random.hpp"
2829
#include "random/uniform_int.hpp"
2930
#include "random/normal_distribution.hpp"
@@ -79,7 +80,7 @@ void run_main()
7980
}
8081
start = std::chrono::high_resolution_clock::now();
8182
std::cerr << (long)std::chrono::duration_cast<std::chrono::microseconds>(start - stop).count();
82-
for (int i = 0; i < max_actual_draws; i++) {
83+
for (int i = 0; i < max_actual_draws; i++) {
8384
std::cout << hmc.x.getCoefficients().transpose() << std::endl;
8485
hmc.apply(rng, 3);
8586
samples.col(i) = hmc.x.getCoefficients();

examples/autopoint/userDefinedFunction_autopoint.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Eigen/Eigen"
2323

2424
#include "ode_solvers/ode_solvers.hpp"
25+
#include "ode_solvers/oracle_autodiff_functors.hpp"
2526
#include "random.hpp"
2627
#include "random/uniform_int.hpp"
2728
#include "random/normal_distribution.hpp"
@@ -77,7 +78,7 @@ void run_main()
7778
hmc.apply(rng, 3);
7879
}
7980
start = std::chrono::high_resolution_clock::now();
80-
for (int i = 0; i < max_actual_draws; i++) {
81+
for (int i = 0; i < max_actual_draws; i++) {
8182
std::cout << hmc.x.getCoefficients().transpose() << std::endl;
8283
hmc.apply(rng, 3);
8384
samples.col(i) = hmc.x.getCoefficients();

examples/autopoint/userDefinedFunction_manual_gradient.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Eigen/Eigen"
2222

2323
#include "ode_solvers/ode_solvers.hpp"
24+
#include "ode_solvers/oracle_autodiff_functors.hpp"
2425
#include "random.hpp"
2526
#include "random/uniform_int.hpp"
2627
#include "random/normal_distribution.hpp"
@@ -130,7 +131,7 @@ void run_main() {
130131
hmc.apply(rng, 3);
131132
}
132133
start = std::chrono::high_resolution_clock::now();
133-
for (int i = 0; i < max_actual_draws; i++) {
134+
for (int i = 0; i < max_actual_draws; i++) {
134135
std::cout << hmc.x.getCoefficients().transpose() << std::endl;
135136
hmc.apply(rng, 3);
136137
samples.col(i) = hmc.x.getCoefficients();

examples/correlation_matrices/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ else()
6262

6363
endif(DISABLE_NLP_ORACLES)
6464

65+
option(BUILTIN_AUTODIFF "Use autodiff from ../external" OFF)
66+
include("../../external/cmake-files/Autodiff.cmake")
67+
GetAutodiff()
68+
if (BUILTIN_AUTODIFF)
69+
include_directories (BEFORE ../../external/_deps/Autodiff)
70+
else ()
71+
include_directories(BEFORE /usr/local/include)
72+
endif(BUILTIN_AUTODIFF)
73+
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++17") #enable the c++17 support needed by autodiff
74+
6575
include("../../external/cmake-files/Eigen.cmake")
6676
GetEigen()
6777

@@ -125,7 +135,6 @@ else ()
125135
add_compile_definitions("EIGEN_NO_DEBUG")
126136
endif ()
127137

128-
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++11") # enable C++11 standard
129138
add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler
130139
#add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl")
131140
add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm")

examples/count-linear-extensions-using-hpolytope/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ else ()
9898
add_compile_definitions("EIGEN_NO_DEBUG")
9999
endif ()
100100

101-
102-
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++11") # enable C++11 standard
103101
add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler
104102
#add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl")
105103
add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm")

include/cartesian_geom/autopoint.h

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
#ifndef AB9D4FD9_C418_44AC_8276_CC42540EF027
2-
#define AB9D4FD9_C418_44AC_8276_CC42540EF027
1+
// VolEsti (volume computation and sampling library)
2+
3+
// Copyright (c) 2024 Vissarion Fisikopoulos
4+
5+
// Licensed under GNU LGPL.3, see LICENCE file
6+
7+
#ifndef CARTESIAN_KERNEL_AUTOPOINT_H
8+
#define CARTESIAN_KERNEL_AUTOPOINT_H
9+
310
#include <iostream>
411
#include <Eigen/Eigen>
512

13+
/// This class manipulates a point used for automatic differentation
14+
/// parameterized by a number type e.g. double
15+
/// \tparam T Numerical Type
616
template <typename T>
717
class autopoint
818
{
@@ -133,7 +143,7 @@ class autopoint
133143
temp.coeffs=coeffs.array().log().matrix();
134144
return temp;
135145
}
136-
146+
137147
autopoint exp() const {
138148
autopoint temp;
139149
temp.d = d;
@@ -217,20 +227,20 @@ class autopoint
217227
{
218228
return autopoint((Coeff)(coeffs * ((FT)k)));
219229
}
220-
230+
221231
autopoint operator* (T k)
222232
{
223233
return autopoint((Coeff)(coeffs * ((FT)k)));
224234
}
225235
// matrix multiplication
226236
autopoint operator* (const autopoint& autopoint_)
227237
{
228-
return autopoint((Coeff)(coeffs * autopoint_.getCoefficients()));
238+
return autopoint((Coeff)(coeffs * autopoint_.getCoefficients()));
229239
}
230240
// matrix multiplication with normal matrix
231241
autopoint operator* (const coeff& matrix_)
232242
{
233-
return autopoint(( autopoint(matrix_) * autopoint(coeffs) ));
243+
return autopoint(( autopoint(matrix_) * autopoint(coeffs) ));
234244
}
235245

236246
void operator*= (const FT k)
@@ -283,7 +293,7 @@ class autopoint
283293
std::cout<<"\n";
284294
}
285295

286-
static autopoint all_ones(int dim)
296+
static autopoint all_ones(int dim)
287297
{
288298
autopoint p(dim);
289299
for (int i = 0; i < dim; i++) p.set_coord(i, 1.0);
@@ -298,7 +308,7 @@ autopoint<K> operator* ( K k, autopoint<K> const& p)
298308
return p * k;
299309
}
300310

301-
// matrix times autopoint
311+
// matrix times autopoint
302312
template<typename K>
303313
autopoint<K> operator* ( Eigen::Matrix<K, Eigen::Dynamic,Eigen::Dynamic> matrix_, autopoint<K> const& p)
304314
{
@@ -308,4 +318,4 @@ autopoint<K> operator* ( Eigen::Matrix<K, Eigen::Dynamic,Eigen::Dynamic> matrix
308318

309319

310320

311-
#endif /* AB9D4FD9_C418_44AC_8276_CC42540EF027 */
321+
#endif // CARTESIAN_KERNEL_AUTOPOINT_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// VolEsti (volume computation and sampling library)
2+
3+
// Copyright (c) 2012-2024 Vissarion Fisikopoulos
4+
// Copyright (c) 2018-2020 Apostolos Chalkis
5+
6+
// Licensed under GNU LGPL.3, see LICENCE file
7+
8+
#ifndef ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP
9+
#define ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP
10+
11+
#include "Eigen/Eigen"
12+
#include <autodiff/forward/real.hpp>
13+
#include <autodiff/forward/real/eigen.hpp>
14+
#include "cartesian_geom/cartesian_kernel.h"
15+
#include "cartesian_geom/autopoint.h"
16+
17+
struct AutoDiffFunctor {
18+
19+
template <typename NT>
20+
struct parameters {
21+
unsigned int order;
22+
NT L; // Lipschitz constant for gradient
23+
NT m; // Strong convexity constant
24+
NT kappa; // Condition number
25+
Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> data;
26+
parameters() : order(2), L(4), m(4), kappa(1){};
27+
};
28+
29+
template <typename NT>
30+
struct FunctionFunctor_internal {
31+
using Autopoint = autopoint<NT>;
32+
using Coeff = typename autopoint<NT>::Coeff;
33+
using FT = typename autopoint<NT>::FT;
34+
using Point = typename Cartesian<NT>::Point;
35+
36+
static std::function<FT(const Autopoint &, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &)> pdf;
37+
38+
FT static result_internal(const Coeff &x, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &data){
39+
return pdf(x, data); //
40+
}
41+
42+
// external interface
43+
Point static differentiate(Point const &x0, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &data) {
44+
Autopoint x = Autopoint(x0.getCoefficients()); // cast into autopoint
45+
auto x1 = x.getCoefficients();
46+
Coeff y = autodiff::gradient(result_internal, autodiff::wrt(x1), autodiff::at(x1, data));
47+
auto result = y.template cast<NT>();
48+
return -1 * Point(result);
49+
}
50+
51+
NT static result(Point const &x0, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &data) {
52+
Autopoint x = Autopoint(x0.getCoefficients()); // cast to autopoint
53+
auto x1 = x.getCoefficients();
54+
return result_internal(x1, data).val();
55+
}
56+
};
57+
58+
template <typename Point>
59+
struct GradientFunctor {
60+
using NT = typename Point::FT;
61+
62+
FunctionFunctor_internal<NT> F;
63+
parameters<NT> &params;
64+
65+
GradientFunctor(parameters<NT> &params_) : params(params_){};
66+
67+
// The index i represents the state vector index
68+
Point operator()(unsigned int const &i, std::vector<Point> const &xs, NT const &t) const {
69+
// std::cout<<"calling gradient functor"<<std::flush;
70+
if (i == params.order - 1) {
71+
return F.differentiate(xs[0], params.data);
72+
}
73+
else {
74+
return xs[i + 1]; // returns derivative
75+
}
76+
}
77+
};
78+
79+
template <typename Point>
80+
struct FunctionFunctor {
81+
using NT = typename Point::FT;
82+
parameters<NT> &params;
83+
84+
FunctionFunctor(parameters<NT> &params_) : params(params_){};
85+
// The index i represents the state vector index
86+
FunctionFunctor_internal<NT> F;
87+
88+
NT operator()(Point const &x) const {
89+
return F.result(x, params.data);
90+
}
91+
};
92+
};
93+
94+
#endif //ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP

0 commit comments

Comments
 (0)