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

Introduce move semantics to adouble, clean-up tape types, clean-up include files #96

Open
wants to merge 87 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
474352b
remove unused argument and add some comments
TimSiebert1 Jan 10, 2025
e71e916
We start removing the type "adub". Its use case was to create a tempo…
TimSiebert1 Jan 10, 2025
5e67fbd
change comparison operators, addition and subtraction to move semanti…
TimSiebert1 Jan 11, 2025
a98dae3
change multiplication, division and exp to new class structure and mo…
TimSiebert1 Jan 11, 2025
effd7c7
change log, sqrt, cbrt, sin and cos to new class structure and move s…
TimSiebert1 Jan 12, 2025
1049ceb
change tan, asin, acos, atan, sinh, cosh, tanh, acosh, asinh, atanh, …
TimSiebert1 Jan 13, 2025
8419f5b
change ceil, floor, fabs, fmin, fmax, ldexp, frexp, atan2, pow and my…
TimSiebert1 Jan 14, 2025
1765f94
rm std::move on return; compiler should decide for rvo
TimSiebert1 Jan 14, 2025
61d28d7
rm arithmetics from friends to reduce coupling
TimSiebert1 Jan 15, 2025
3f3f5e4
rm arithmetics from friends to reduce coupling
TimSiebert1 Jan 15, 2025
67f35c7
rm late init; all adoubles are allocated with tape_location
TimSiebert1 Jan 15, 2025
9fc7547
restructure the file; remove lateInit from header and move ensureCont…
TimSiebert1 Jan 15, 2025
9f0f22a
mv declarations of ensureContiguousLocations
TimSiebert1 Jan 15, 2025
94b8957
start moving pdouble to new class structure, modify and clean up comp…
TimSiebert1 Jan 15, 2025
70e9eb6
mv getter and setter to header and inline them; correct assert condit…
TimSiebert1 Jan 16, 2025
4b038d9
change structure of pdouble class to new type structure; mv arithmeti…
TimSiebert1 Jan 16, 2025
b352741
mv getter and setter; inline them
TimSiebert1 Jan 16, 2025
1f5c33f
remove move functions
TimSiebert1 Jan 16, 2025
6cf8fef
add operations to allow two tmp inputs
TimSiebert1 Jan 16, 2025
b450425
change to new type structrure
TimSiebert1 Jan 16, 2025
039c7df
add copy assignment and arithmetic assignments for pdouble
TimSiebert1 Jan 16, 2025
4320775
add copy assignment and arithmetic assignments for pdouble
TimSiebert1 Jan 16, 2025
926a412
add comments
TimSiebert1 Jan 16, 2025
c64ae4b
mv function decls from pdouble
TimSiebert1 Jan 16, 2025
08c3ae4
mkparam is now static
TimSiebert1 Jan 16, 2025
b8de3fb
mv vector operations to adouble.cpp and adouble.h
TimSiebert1 Jan 16, 2025
66b63ff
change adubref to new type system and clean up the class
TimSiebert1 Jan 16, 2025
4a67bd4
mv and inline comp operators
TimSiebert1 Jan 17, 2025
7665551
clean up and adapt advector class to new type system
TimSiebert1 Jan 17, 2025
4e189e2
add ensureContiguousLocaitons_ to simplify advector constructors
TimSiebert1 Jan 17, 2025
9564c88
fix imports
TimSiebert1 Jan 17, 2025
04a2d70
sumarize pdouble and adouble
TimSiebert1 Jan 17, 2025
b35d39b
change headers for adouble and pdouble
TimSiebert1 Jan 17, 2025
b6d44d2
rm adouble.h and param.h for ad_types.h
TimSiebert1 Jan 17, 2025
cc05af9
rm adubref::
TimSiebert1 Jan 17, 2025
c86ff74
fix various typos and correct compile erros
TimSiebert1 Jan 17, 2025
feea91d
fix includes, move headers to include file
TimSiebert1 Jan 17, 2025
ce0ecef
fix circular dependency in ops
TimSiebert1 Jan 18, 2025
ffe55ab
correct the function
TimSiebert1 Jan 19, 2025
4c7e43a
correct assert condition
TimSiebert1 Jan 19, 2025
ed7b7f1
change order for correct calling sequence
TimSiebert1 Jan 19, 2025
01050a5
clean up the files; use meaningful variables and data types
TimSiebert1 Jan 19, 2025
84790b9
change arg types
TimSiebert1 Jan 19, 2025
f4173f8
fix error
TimSiebert1 Jan 19, 2025
3892c56
clean-up and homogenize arg names
TimSiebert1 Jan 20, 2025
72109f8
adapt header files
TimSiebert1 Jan 20, 2025
a68ff15
add naming for the function typdefs
TimSiebert1 Jan 21, 2025
d9aeb89
add naming for the function typdefs
TimSiebert1 Jan 21, 2025
eca1ca1
use interface of adoubles; remove move assignment
TimSiebert1 Jan 21, 2025
47c3428
the point where we track the location of the output of the external d…
TimSiebert1 Jan 22, 2025
49ab28e
add move assignment and fix double move of same variable in tan operator
TimSiebert1 Jan 23, 2025
2a22a93
add move assignment
TimSiebert1 Jan 23, 2025
abc9c45
the external function must now be registered after the call to the ad…
TimSiebert1 Jan 23, 2025
19bda2a
rename to pdouble
TimSiebert1 Jan 23, 2025
d53ecda
rm unused imports
TimSiebert1 Jan 23, 2025
2e7f85c
rename param
TimSiebert1 Jan 23, 2025
626a8e7
create new location to follow the expectations of a copy constr.
TimSiebert1 Jan 23, 2025
a289f0d
start documenting the types and rename the getter and setter, inline …
TimSiebert1 Jan 23, 2025
f562ea3
clean up interface of adouble and move inlined functions
TimSiebert1 Jan 23, 2025
59e94a2
replace with pdouble interface
TimSiebert1 Jan 23, 2025
e3c464c
homogenize and apply the interface of ad types
TimSiebert1 Jan 23, 2025
b926a70
change file description
TimSiebert1 Jan 23, 2025
f34c2e2
add ad_types.h
TimSiebert1 Jan 23, 2025
8d68c3d
rename to adtb_types.h
TimSiebert1 Jan 23, 2025
ea3a376
add missing includes
TimSiebert1 Jan 23, 2025
30cb187
apperently the use of 'a' and 'move(a)' leads to undefined behavior s…
TimSiebert1 Jan 23, 2025
b7f93a9
update documentation for pdouble
TimSiebert1 Jan 24, 2025
c76b061
add higher order tests for cbrt: thanks to gertrud
TimSiebert1 Jan 28, 2025
988c728
add test for cbrt
TimSiebert1 Jan 28, 2025
ba0c3a9
add test for pdouble exp
TimSiebert1 Jan 28, 2025
2d23c7e
fix bug; missing else
TimSiebert1 Jan 28, 2025
411e88a
add tests for exp fos and zos for pdouble
TimSiebert1 Jan 28, 2025
251ccc9
update documentation
TimSiebert1 Jan 28, 2025
4f4fe82
remove declaration, was a bug
TimSiebert1 Feb 4, 2025
7ca68a7
add integration test for checkpointing
TimSiebert1 Feb 4, 2025
ce83168
update example, we need the locations of the adouble to be consecutiv…
TimSiebert1 Feb 4, 2025
9872984
remove macro. We could include dvlparams and always set it to 1, but …
TimSiebert1 Feb 5, 2025
2350649
we need required to have consecutive locations, since only the first …
TimSiebert1 Feb 5, 2025
ddf4dc4
add tests for pdouble
TimSiebert1 Feb 5, 2025
7142d43
add further tests for pdouble
TimSiebert1 Feb 5, 2025
a32c140
add tests for fov_reverse checkpointing
TimSiebert1 Feb 5, 2025
2025621
allow implicit conversion
TimSiebert1 Feb 5, 2025
3a21b20
change place where the location of the result is taped
TimSiebert1 Feb 5, 2025
b75573f
add another test for external fcts
TimSiebert1 Feb 5, 2025
530e7e8
push test coverage
TimSiebert1 Feb 5, 2025
2e7f30d
This commit simplifies the pdouble internals. Before this commit, man…
TimSiebert1 Feb 6, 2025
1ed9f47
add further tests
TimSiebert1 Feb 6, 2025
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
5 changes: 5 additions & 0 deletions ADOL-C/boost-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ endif()
set(SOURCE_FILES
adouble.cpp
main.cpp
pdouble.cpp
traceCompositeTests.cpp
tracelessCompositeTests.cpp
tracelessOperatorScalar.cpp
Expand All @@ -31,6 +32,10 @@ set(SOURCE_FILES
file(GLOB UNI5_FOR_FILES "uni5_for/*.cpp")
list(APPEND SOURCE_FILES ${UNI5_FOR_FILES})

# Add all source files from integration_tests
file(GLOB INTEGRATION_TESTS_FILES "integration_tests/*.cpp")
list(APPEND SOURCE_FILES ${INTEGRATION_TESTS_FILES})

add_executable(boost-test-adolc ${SOURCE_FILES})
target_include_directories(boost-test-adolc PRIVATE "${ADOLC_INCLUDE_DIR}")
target_link_libraries(boost-test-adolc PRIVATE
Expand Down
185 changes: 185 additions & 0 deletions ADOL-C/boost-test/integration_tests/checkpointing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

namespace tt = boost::test_tools;

#include "../const.h"
#include <adolc/adolc.h>
#include <array>
#include <numeric>
#include <vector>

// Euler step (double version)
int euler_step(size_t n, double *y) {
y[0] = y[0] + 0.01 * y[0];
y[1] = y[1] + 0.01 * 2 * y[1];
return 1;
}

// Euler step (adouble version)
int euler_step_act(size_t n, adouble *y) {
y[0] = y[0] + 0.01 * y[0];
y[1] = y[1] + 0.01 * 2 * y[1];
return 1;
}

BOOST_AUTO_TEST_SUITE(test_checkpoint_example)
BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {
const int16_t tag_full = 1; // Tag for full taping
const int16_t tag_part = 2; // Tag for partial taping with checkpointing
const int16_t tag_check = 3; // Tag for checkpointing
const size_t n = 2; // Number of state variables
const int steps = 100; // Number of time steps
const double t0 = 0.0; // Initial time
const double tf = 1.0; // Final time

// State variables (double and adouble versions)
std::vector<double> y_double(n);
ensureContiguousLocations(2 * n);
std::vector<adouble> y_adouble_1(n);
std::vector<adouble> y_adouble_2(n);

// Control variables (double and adouble versions)
std::vector<double> conp{1.0, 1.0}; // Initial control values
std::vector<adouble> con(n);

// Target value and gradient
std::vector<double> out(2);
std::vector<double> grad_full(n); // Gradient from full taping
std::vector<double> grad_part(n); // Gradient from checkpointing

// Full taping of the time step loop
trace_on(tag_full);
con[0] <<= conp[0];
con[1] <<= conp[1];
y_adouble_1[0] = con[0];
y_adouble_1[1] = con[1];

for (int i = 0; i < steps; i++) {
euler_step_act(n, y_adouble_1.data());
}
y_adouble_1[0] + y_adouble_1[1] >>= out[0];
trace_off();

// Compute gradient using full taping
gradient(tag_full, n, conp.data(), grad_full.data());

// Checkpointing setup
CP_Context cpc(euler_step_act); // Checkpointing context
cpc.setDoubleFct(euler_step); // Double version of the time step function
cpc.setNumberOfSteps(steps); // Number of time steps
cpc.setNumberOfCheckpoints(5); // Number of checkpoints
cpc.setDimensionXY(n); // Dimension of input/output
cpc.setInput(y_adouble_2.data()); // Input vector
cpc.setOutput(y_adouble_2.data()); // Output vector
cpc.setTapeNumber(tag_check); // Tape number for checkpointing
cpc.setAlwaysRetaping(false); // Do not always retape

// Partial taping with checkpointing
trace_on(tag_part);
con[0] <<= conp[0];
con[1] <<= conp[1];
y_adouble_2[0] = con[0];
y_adouble_2[1] = con[1];

cpc.checkpointing(); // Perform checkpointing

y_adouble_2[0] + y_adouble_2[1] >>= out[1];
trace_off();

// test if both taping results are equal
BOOST_TEST(out[0] == out[1], tt::tolerance(tol));
// Compute gradient using checkpointing
gradient(tag_part, n, conp.data(), grad_part.data());
// Compare gradients from full taping and checkpointing
for (size_t i = 0; i < n; i++) {
BOOST_TEST(grad_full[i] == grad_part[i], tt::tolerance(tol));
}
}
BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {
const int16_t tag_full = 1; // Tag for full taping
const int16_t tag_part = 2; // Tag for partial taping with checkpointing
const int16_t tag_check = 3; // Tag for checkpointing
const size_t n = 2; // Number of state variables
const int steps = 100; // Number of time steps
const double t0 = 0.0; // Initial time
const double tf = 1.0; // Final time

// State variables (double and adouble versions)
std::vector<double> y_double(n);
ensureContiguousLocations(2 * n);
std::vector<adouble> y_adouble_1(n);
std::vector<adouble> y_adouble_2(n);

// Control variables (double and adouble versions)
std::vector<double> conp{1.0, 1.0}; // Initial control values
std::vector<adouble> con(n);

// Target value and gradient
std::vector<double> out(2);
std::vector<double> grad_full(n); // Gradient from full taping
std::vector<double> grad_part(n); // Gradient from checkpointing

// Full taping of the time step loop
trace_on(tag_full, 1);
con[0] <<= conp[0];
con[1] <<= conp[1];
y_adouble_1[0] = con[0];
y_adouble_1[1] = con[1];

for (int i = 0; i < steps; i++) {
euler_step_act(n, y_adouble_1.data());
}
y_adouble_1[0] + y_adouble_1[1] >>= out[0];
trace_off();

// weights
double **U = myalloc2(2, 1);
U[0][0] = 1.0;
U[1][0] = -1.0;

// outputs
double **Z_full = myalloc2(2, 2);
double **Z_part = myalloc2(2, 2);

// Compute vector-mode reverse
fov_reverse(tag_full, 1, 2, 2, U, Z_full);

// Checkpointing setup
CP_Context cpc(euler_step_act); // Checkpointing context
cpc.setDoubleFct(euler_step); // Double version of the time step function
cpc.setNumberOfSteps(steps); // Number of time steps
cpc.setNumberOfCheckpoints(5); // Number of checkpoints
cpc.setDimensionXY(n); // Dimension of input/output
cpc.setInput(y_adouble_2.data()); // Input vector
cpc.setOutput(y_adouble_2.data()); // Output vector
cpc.setTapeNumber(tag_check); // Tape number for checkpointing
cpc.setAlwaysRetaping(true); // Do always retape

// Partial taping with checkpointing
trace_on(tag_part, 1);
con[0] <<= conp[0];
con[1] <<= conp[1];
y_adouble_2[0] = con[0];
y_adouble_2[1] = con[1];

cpc.checkpointing(); // Perform checkpointing

y_adouble_2[0] + y_adouble_2[1] >>= out[1];
trace_off();

// test if both taping results are equal
BOOST_TEST(out[0] == out[1], tt::tolerance(tol));

// Compute gradient using checkpointing
fov_reverse(tag_part, 1, 2, 2, U, Z_part);
// Compare gradients from full taping and checkpointing
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j)
BOOST_TEST(Z_full[i][j] == Z_part[i][j], tt::tolerance(tol));
}
myfree2(U);
myfree2(Z_full);
myfree2(Z_part);
}
BOOST_AUTO_TEST_SUITE_END()
145 changes: 145 additions & 0 deletions ADOL-C/boost-test/integration_tests/external_fct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include "../const.h"
#include <adolc/adolc.h>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <vector>

namespace tt = boost::test_tools;

BOOST_AUTO_TEST_SUITE(ExternalFunctionTests)

const double h = 0.01;
const int steps = 100;

const short tag_full = 1;
const short tag_part = 2;
const short tag_ext_fct = 3;

// Control parameters
std::vector<double> conp = {1.0, 1.0};
std::vector<double> grad_full(2);
std::vector<double> grad_ext(2);

ext_diff_fct *edf;
std::vector<double> yp = {0};
std::vector<double> ynewp = {0};
std::vector<double> u = {1.0, 1.0};
std::vector<double> z = {0};

void euler_step_act(size_t n, adouble *yin, size_t m, adouble *yout) {
yout[0] = yin[0] + h * yin[0];
yout[1] = yin[1] + h * 2 * yin[1];
}

int euler_step(size_t n, double *yin, size_t m, double *yout) {
yout[0] = yin[0] + h * yin[0];
yout[1] = yin[1] + h * 2 * yin[1];
return 1;
}

int zos_for_euler_step(size_t n, double *yin, size_t m, double *yout) {
int rc;
set_nested_ctx(tag_ext_fct, true);
rc = zos_forward(tag_ext_fct, 2, 2, 0, yin, yout);
set_nested_ctx(tag_ext_fct, false);
return rc;
}

int fos_rev_euler_step(size_t n, double *u, size_t m, double *z, double *,
double *) {
int rc;
set_nested_ctx(tag_ext_fct, true);
zos_forward(tag_ext_fct, 2, 2, 1, edf->dp_x, edf->dp_y);
rc = fos_reverse(tag_ext_fct, 2, 2, u, z);
set_nested_ctx(tag_ext_fct, false);
return rc;
}

void setup_full_taping(const short tag_full, const std::vector<double> &conp) {
trace_on(tag_full);
std::vector<adouble> y(2), ynew(2);
std::vector<adouble> con(2);

con[0] <<= conp[0];
con[1] <<= conp[1];
y[0] = con[0];
y[1] = con[1];

for (int i = 0; i < steps; i++) {
euler_step_act(2, y.data(), 2, ynew.data());
y[0] = ynew[0];
y[1] = ynew[1];
}

adouble f;
f = y[0] + y[1];
double f_out;
f >>= f_out;
trace_off();
}

void setup_external_function(const short tag_ext_fct,
const std::vector<double> &conp) {
trace_on(tag_ext_fct);
std::vector<adouble> y(2), ynew(2);
y[0] <<= conp[0];
y[1] <<= conp[1];
euler_step_act(2, y.data(), 2, ynew.data());

double f_out;
ynew[0] >>= f_out; // Dummy output
ynew[1] >>= f_out; // Dummy output
trace_off();

edf = reg_ext_fct(euler_step);
edf->zos_forward = zos_for_euler_step;
edf->dp_x = yp.data();
edf->dp_y = ynewp.data();
edf->fos_reverse = fos_rev_euler_step;
edf->dp_U = u.data();
edf->dp_Z = z.data();
}

void setup_external_taping(size_t tag_part, std::vector<double> conp) {
trace_on(tag_part);
ensureContiguousLocations(4);
std::vector<adouble> y(2), ynew(2);
std::vector<adouble> con(2);

con[0] <<= conp[0];
con[1] <<= conp[1];
y[0] = con[0];
y[1] = con[1];

for (int i = 0; i < steps; i++) {
call_ext_fct(edf, 2, y.data(), 2, ynew.data());
y[0] = ynew[0];
y[1] = ynew[1];
}

adouble f;
f = y[0] + y[1];
double f_out;
f >>= f_out;
trace_off();
}

BOOST_AUTO_TEST_CASE(CompareFullAndExternalGradients) {
const double expected0 = exp(h * steps);
const double expected1 = exp(2 * h * steps);

setup_full_taping(tag_full, conp);
gradient(tag_full, 2, conp.data(), grad_full.data());

setup_external_function(tag_ext_fct, conp);
setup_external_taping(tag_part, conp);

gradient(tag_part, 2, conp.data(), grad_ext.data());

// Verify gradients match
for (int i = 0; i < 2; ++i) {
BOOST_TEST(grad_full[i] == grad_ext[i], tt::tolerance(tol));
}
}

BOOST_AUTO_TEST_SUITE_END()
Loading
Loading