Skip to content

Commit eab24dd

Browse files
committed
Merge remote-tracking branch 'origin/master' into constify-vec_jac
2 parents 04f0c56 + 2c94e79 commit eab24dd

File tree

133 files changed

+17870
-7716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+17870
-7716
lines changed

.gitattributes

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ ADOL-C/include/adolc/adolc.h -text
212212
ADOL-C/include/adolc/adolc_fatalerror.h -text
213213
ADOL-C/include/adolc/adolc_openmp.h -text
214214
ADOL-C/include/adolc/adolc_sparse.h -text
215-
ADOL-C/include/adolc/adouble.h -text
215+
ADOL-C/include/adolc/adtb_types.h -text
216216
ADOL-C/include/adolc/adoublecuda.h -text
217217
ADOL-C/include/adolc/adtl.h -text
218218
ADOL-C/include/adolc/adtl_hov.h -text
@@ -246,7 +246,7 @@ ADOL-C/include/adolc/lie/Makefile.am -text
246246
ADOL-C/include/adolc/lie/Makefile.in -text
247247
ADOL-C/include/adolc/lie/drivers.h -text
248248
ADOL-C/include/adolc/medipacksupport.h -text
249-
ADOL-C/include/adolc/param.h -text
249+
ADOL-C/include/adolc/adtb_types.h -text
250250
ADOL-C/include/adolc/revolve.h -text
251251
ADOL-C/include/adolc/sparse/Makefile.am -text
252252
ADOL-C/include/adolc/sparse/Makefile.in -text

ADOL-C/boost-test/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ endif()
1616
set(SOURCE_FILES
1717
adouble.cpp
1818
main.cpp
19+
pdouble.cpp
1920
traceCompositeTests.cpp
2021
tracelessCompositeTests.cpp
2122
tracelessOperatorScalar.cpp
@@ -25,8 +26,16 @@ set(SOURCE_FILES
2526
traceSecOrderScalar.cpp
2627
traceSecOrderVector.cpp
2728
traceFixedPointScalarTests.cpp
28-
uni5_for.cpp
2929
)
30+
31+
# Add all source files from uni5_for
32+
file(GLOB UNI5_FOR_FILES "uni5_for/*.cpp")
33+
list(APPEND SOURCE_FILES ${UNI5_FOR_FILES})
34+
35+
# Add all source files from integration_tests
36+
file(GLOB INTEGRATION_TESTS_FILES "integration_tests/*.cpp")
37+
list(APPEND SOURCE_FILES ${INTEGRATION_TESTS_FILES})
38+
3039
add_executable(boost-test-adolc ${SOURCE_FILES})
3140
target_include_directories(boost-test-adolc PRIVATE "${ADOLC_INCLUDE_DIR}")
3241
target_link_libraries(boost-test-adolc PRIVATE
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#define BOOST_TEST_DYN_LINK
2+
#include <boost/test/unit_test.hpp>
3+
4+
namespace tt = boost::test_tools;
5+
6+
#include "../const.h"
7+
#include <adolc/adolc.h>
8+
#include <array>
9+
#include <numeric>
10+
#include <vector>
11+
12+
// Euler step (double version)
13+
int euler_step(size_t n, double *y) {
14+
y[0] = y[0] + 0.01 * y[0];
15+
y[1] = y[1] + 0.01 * 2 * y[1];
16+
return 1;
17+
}
18+
19+
// Euler step (adouble version)
20+
int euler_step_act(size_t n, adouble *y) {
21+
y[0] = y[0] + 0.01 * y[0];
22+
y[1] = y[1] + 0.01 * 2 * y[1];
23+
return 1;
24+
}
25+
26+
BOOST_AUTO_TEST_SUITE(test_checkpoint_example)
27+
BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {
28+
const int16_t tag_full = 1; // Tag for full taping
29+
const int16_t tag_part = 2; // Tag for partial taping with checkpointing
30+
const int16_t tag_check = 3; // Tag for checkpointing
31+
const size_t n = 2; // Number of state variables
32+
const int steps = 100; // Number of time steps
33+
const double t0 = 0.0; // Initial time
34+
const double tf = 1.0; // Final time
35+
36+
// State variables (double and adouble versions)
37+
std::vector<double> y_double(n);
38+
ensureContiguousLocations(2 * n);
39+
std::vector<adouble> y_adouble_1(n);
40+
std::vector<adouble> y_adouble_2(n);
41+
42+
// Control variables (double and adouble versions)
43+
std::vector<double> conp{1.0, 1.0}; // Initial control values
44+
std::vector<adouble> con(n);
45+
46+
// Target value and gradient
47+
std::vector<double> out(2);
48+
std::vector<double> grad_full(n); // Gradient from full taping
49+
std::vector<double> grad_part(n); // Gradient from checkpointing
50+
51+
// Full taping of the time step loop
52+
trace_on(tag_full);
53+
con[0] <<= conp[0];
54+
con[1] <<= conp[1];
55+
y_adouble_1[0] = con[0];
56+
y_adouble_1[1] = con[1];
57+
58+
for (int i = 0; i < steps; i++) {
59+
euler_step_act(n, y_adouble_1.data());
60+
}
61+
y_adouble_1[0] + y_adouble_1[1] >>= out[0];
62+
trace_off();
63+
64+
// Compute gradient using full taping
65+
gradient(tag_full, n, conp.data(), grad_full.data());
66+
67+
// Checkpointing setup
68+
CP_Context cpc(euler_step_act); // Checkpointing context
69+
cpc.setDoubleFct(euler_step); // Double version of the time step function
70+
cpc.setNumberOfSteps(steps); // Number of time steps
71+
cpc.setNumberOfCheckpoints(5); // Number of checkpoints
72+
cpc.setDimensionXY(n); // Dimension of input/output
73+
cpc.setInput(y_adouble_2.data()); // Input vector
74+
cpc.setOutput(y_adouble_2.data()); // Output vector
75+
cpc.setTapeNumber(tag_check); // Tape number for checkpointing
76+
cpc.setAlwaysRetaping(false); // Do not always retape
77+
78+
// Partial taping with checkpointing
79+
trace_on(tag_part);
80+
con[0] <<= conp[0];
81+
con[1] <<= conp[1];
82+
y_adouble_2[0] = con[0];
83+
y_adouble_2[1] = con[1];
84+
85+
cpc.checkpointing(); // Perform checkpointing
86+
87+
y_adouble_2[0] + y_adouble_2[1] >>= out[1];
88+
trace_off();
89+
90+
// test if both taping results are equal
91+
BOOST_TEST(out[0] == out[1], tt::tolerance(tol));
92+
// Compute gradient using checkpointing
93+
gradient(tag_part, n, conp.data(), grad_part.data());
94+
// Compare gradients from full taping and checkpointing
95+
for (size_t i = 0; i < n; i++) {
96+
BOOST_TEST(grad_full[i] == grad_part[i], tt::tolerance(tol));
97+
}
98+
}
99+
BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {
100+
const int16_t tag_full = 1; // Tag for full taping
101+
const int16_t tag_part = 2; // Tag for partial taping with checkpointing
102+
const int16_t tag_check = 3; // Tag for checkpointing
103+
const size_t n = 2; // Number of state variables
104+
const int steps = 100; // Number of time steps
105+
const double t0 = 0.0; // Initial time
106+
const double tf = 1.0; // Final time
107+
108+
// State variables (double and adouble versions)
109+
std::vector<double> y_double(n);
110+
ensureContiguousLocations(2 * n);
111+
std::vector<adouble> y_adouble_1(n);
112+
std::vector<adouble> y_adouble_2(n);
113+
114+
// Control variables (double and adouble versions)
115+
std::vector<double> conp{1.0, 1.0}; // Initial control values
116+
std::vector<adouble> con(n);
117+
118+
// Target value and gradient
119+
std::vector<double> out(2);
120+
std::vector<double> grad_full(n); // Gradient from full taping
121+
std::vector<double> grad_part(n); // Gradient from checkpointing
122+
123+
// Full taping of the time step loop
124+
trace_on(tag_full, 1);
125+
con[0] <<= conp[0];
126+
con[1] <<= conp[1];
127+
y_adouble_1[0] = con[0];
128+
y_adouble_1[1] = con[1];
129+
130+
for (int i = 0; i < steps; i++) {
131+
euler_step_act(n, y_adouble_1.data());
132+
}
133+
y_adouble_1[0] + y_adouble_1[1] >>= out[0];
134+
trace_off();
135+
136+
// weights
137+
double **U = myalloc2(2, 1);
138+
U[0][0] = 1.0;
139+
U[1][0] = -1.0;
140+
141+
// outputs
142+
double **Z_full = myalloc2(2, 2);
143+
double **Z_part = myalloc2(2, 2);
144+
145+
// Compute vector-mode reverse
146+
fov_reverse(tag_full, 1, 2, 2, U, Z_full);
147+
148+
// Checkpointing setup
149+
CP_Context cpc(euler_step_act); // Checkpointing context
150+
cpc.setDoubleFct(euler_step); // Double version of the time step function
151+
cpc.setNumberOfSteps(steps); // Number of time steps
152+
cpc.setNumberOfCheckpoints(5); // Number of checkpoints
153+
cpc.setDimensionXY(n); // Dimension of input/output
154+
cpc.setInput(y_adouble_2.data()); // Input vector
155+
cpc.setOutput(y_adouble_2.data()); // Output vector
156+
cpc.setTapeNumber(tag_check); // Tape number for checkpointing
157+
cpc.setAlwaysRetaping(true); // Do always retape
158+
159+
// Partial taping with checkpointing
160+
trace_on(tag_part, 1);
161+
con[0] <<= conp[0];
162+
con[1] <<= conp[1];
163+
y_adouble_2[0] = con[0];
164+
y_adouble_2[1] = con[1];
165+
166+
cpc.checkpointing(); // Perform checkpointing
167+
168+
y_adouble_2[0] + y_adouble_2[1] >>= out[1];
169+
trace_off();
170+
171+
// test if both taping results are equal
172+
BOOST_TEST(out[0] == out[1], tt::tolerance(tol));
173+
174+
// Compute gradient using checkpointing
175+
fov_reverse(tag_part, 1, 2, 2, U, Z_part);
176+
// Compare gradients from full taping and checkpointing
177+
for (size_t i = 0; i < 2; ++i) {
178+
for (size_t j = 0; j < 2; ++j)
179+
BOOST_TEST(Z_full[i][j] == Z_part[i][j], tt::tolerance(tol));
180+
}
181+
myfree2(U);
182+
myfree2(Z_full);
183+
myfree2(Z_part);
184+
}
185+
BOOST_AUTO_TEST_SUITE_END()
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "../const.h"
2+
#include <adolc/adolc.h>
3+
#include <boost/test/unit_test.hpp>
4+
#include <memory>
5+
#include <vector>
6+
7+
namespace tt = boost::test_tools;
8+
9+
BOOST_AUTO_TEST_SUITE(ExternalFunctionTests)
10+
11+
const double h = 0.01;
12+
const int steps = 100;
13+
14+
const short tag_full = 1;
15+
const short tag_part = 2;
16+
const short tag_ext_fct = 3;
17+
18+
// Control parameters
19+
std::vector<double> conp = {1.0, 1.0};
20+
std::vector<double> grad_full(2);
21+
std::vector<double> grad_ext(2);
22+
23+
ext_diff_fct *edf;
24+
std::vector<double> yp = {0};
25+
std::vector<double> ynewp = {0};
26+
std::vector<double> u = {1.0, 1.0};
27+
std::vector<double> z = {0};
28+
29+
void euler_step_act(size_t n, adouble *yin, size_t m, adouble *yout) {
30+
yout[0] = yin[0] + h * yin[0];
31+
yout[1] = yin[1] + h * 2 * yin[1];
32+
}
33+
34+
int euler_step(size_t n, double *yin, size_t m, double *yout) {
35+
yout[0] = yin[0] + h * yin[0];
36+
yout[1] = yin[1] + h * 2 * yin[1];
37+
return 1;
38+
}
39+
40+
int zos_for_euler_step(size_t n, double *yin, size_t m, double *yout) {
41+
int rc;
42+
set_nested_ctx(tag_ext_fct, true);
43+
rc = zos_forward(tag_ext_fct, 2, 2, 0, yin, yout);
44+
set_nested_ctx(tag_ext_fct, false);
45+
return rc;
46+
}
47+
48+
int fos_rev_euler_step(size_t n, double *u, size_t m, double *z, double *,
49+
double *) {
50+
int rc;
51+
set_nested_ctx(tag_ext_fct, true);
52+
zos_forward(tag_ext_fct, 2, 2, 1, edf->dp_x, edf->dp_y);
53+
rc = fos_reverse(tag_ext_fct, 2, 2, u, z);
54+
set_nested_ctx(tag_ext_fct, false);
55+
return rc;
56+
}
57+
58+
void setup_full_taping(const short tag_full, const std::vector<double> &conp) {
59+
trace_on(tag_full);
60+
std::vector<adouble> y(2), ynew(2);
61+
std::vector<adouble> con(2);
62+
63+
con[0] <<= conp[0];
64+
con[1] <<= conp[1];
65+
y[0] = con[0];
66+
y[1] = con[1];
67+
68+
for (int i = 0; i < steps; i++) {
69+
euler_step_act(2, y.data(), 2, ynew.data());
70+
y[0] = ynew[0];
71+
y[1] = ynew[1];
72+
}
73+
74+
adouble f;
75+
f = y[0] + y[1];
76+
double f_out;
77+
f >>= f_out;
78+
trace_off();
79+
}
80+
81+
void setup_external_function(const short tag_ext_fct,
82+
const std::vector<double> &conp) {
83+
trace_on(tag_ext_fct);
84+
std::vector<adouble> y(2), ynew(2);
85+
y[0] <<= conp[0];
86+
y[1] <<= conp[1];
87+
euler_step_act(2, y.data(), 2, ynew.data());
88+
89+
double f_out;
90+
ynew[0] >>= f_out; // Dummy output
91+
ynew[1] >>= f_out; // Dummy output
92+
trace_off();
93+
94+
edf = reg_ext_fct(euler_step);
95+
edf->zos_forward = zos_for_euler_step;
96+
edf->dp_x = yp.data();
97+
edf->dp_y = ynewp.data();
98+
edf->fos_reverse = fos_rev_euler_step;
99+
edf->dp_U = u.data();
100+
edf->dp_Z = z.data();
101+
}
102+
103+
void setup_external_taping(size_t tag_part, std::vector<double> conp) {
104+
trace_on(tag_part);
105+
ensureContiguousLocations(4);
106+
std::vector<adouble> y(2), ynew(2);
107+
std::vector<adouble> con(2);
108+
109+
con[0] <<= conp[0];
110+
con[1] <<= conp[1];
111+
y[0] = con[0];
112+
y[1] = con[1];
113+
114+
for (int i = 0; i < steps; i++) {
115+
call_ext_fct(edf, 2, y.data(), 2, ynew.data());
116+
y[0] = ynew[0];
117+
y[1] = ynew[1];
118+
}
119+
120+
adouble f;
121+
f = y[0] + y[1];
122+
double f_out;
123+
f >>= f_out;
124+
trace_off();
125+
}
126+
127+
BOOST_AUTO_TEST_CASE(CompareFullAndExternalGradients) {
128+
const double expected0 = exp(h * steps);
129+
const double expected1 = exp(2 * h * steps);
130+
131+
setup_full_taping(tag_full, conp);
132+
gradient(tag_full, 2, conp.data(), grad_full.data());
133+
134+
setup_external_function(tag_ext_fct, conp);
135+
setup_external_taping(tag_part, conp);
136+
137+
gradient(tag_part, 2, conp.data(), grad_ext.data());
138+
139+
// Verify gradients match
140+
for (int i = 0; i < 2; ++i) {
141+
BOOST_TEST(grad_full[i] == grad_ext[i], tt::tolerance(tol));
142+
}
143+
}
144+
145+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)