Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 16 additions & 16 deletions ADOL-C/boost-test/ho_rev/hos_ov_reverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace tt = boost::test_tools;
BOOST_AUTO_TEST_SUITE(test_hos_ov_forward)

BOOST_AUTO_TEST_CASE(PlusOperator_HOS_OV_REVERSE) {
const auto tapeId = createNewTape();
setCurrentTape(tapeId);
auto tapePtr = std::make_unique<ValueTape>();
setCurrentTapePtr(tapePtr.get());
const size_t dim_out = 1;
const size_t dim_in = 2;
const size_t degree_hov_forward = 1;
Expand All @@ -22,15 +22,15 @@ BOOST_AUTO_TEST_CASE(PlusOperator_HOS_OV_REVERSE) {
std::vector<adouble> indep(dim_in);
std::vector<double> out(dim_out);

trace_on(tapeId);
trace_on(*tapePtr);
for (size_t i = 0; i < in.size(); ++i)
indep[i] <<= in[i];

// x^2 + y^3
adouble dep = pow(indep[0], 2) + pow(indep[1], 3);

dep >>= out[0];
trace_off();
trace_off(*tapePtr);

double ***X = myalloc3(dim_in, num_dirs, degree_hov_forward);
double ***Y = myalloc3(dim_out, num_dirs, degree_hov_forward);
Expand All @@ -53,10 +53,10 @@ BOOST_AUTO_TEST_CASE(PlusOperator_HOS_OV_REVERSE) {
std::vector<double> test_in{2.0, 3.2};
// x^2 + y^3)
double test_out = std::pow(test_in[0], 2) + std::pow(test_in[1], 3);
hov_wk_forward(tapeId, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
hov_wk_forward(*tapePtr, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
test_in.data(), X, out.data(), Y);

hos_ov_reverse(tapeId, dim_out, dim_in, degree_hos_reverse, num_dirs, U, Z);
hos_ov_reverse(*tapePtr, dim_out, dim_in, degree_hos_reverse, num_dirs, U, Z);

BOOST_TEST(out[0] == test_out, tt::tolerance(tol));
BOOST_TEST(Y[0][0][0] == 2 * test_in[0] * X[0][0][0] +
Expand Down Expand Up @@ -109,8 +109,8 @@ BOOST_AUTO_TEST_CASE(PlusOperator_HOS_OV_REVERSE) {
}

BOOST_AUTO_TEST_CASE(MinOperator_HOS_OV_REVERSE) {
const auto tapeId = createNewTape();
setCurrentTape(tapeId);
auto tapePtr = std::make_unique<ValueTape>();
setCurrentTapePtr(tapePtr.get());
const size_t dim_out = 1;
const size_t dim_in = 2;
const size_t degree_hov_forward = 1;
Expand All @@ -121,15 +121,15 @@ BOOST_AUTO_TEST_CASE(MinOperator_HOS_OV_REVERSE) {
std::vector<adouble> indep(dim_in);
std::vector<double> out(dim_out);

trace_on(tapeId);
trace_on(*tapePtr);
for (size_t i = 0; i < in.size(); ++i)
indep[i] <<= in[i];

// min(x^2, y^3)
adouble dep = min(pow(indep[0], 2), pow(indep[1], 3));

dep >>= out[0];
trace_off();
trace_off(*tapePtr);

double ***X = myalloc3(dim_in, num_dirs, degree_hov_forward);
double ***Y = myalloc3(dim_out, num_dirs, degree_hov_forward);
Expand All @@ -156,10 +156,10 @@ BOOST_AUTO_TEST_CASE(MinOperator_HOS_OV_REVERSE) {
// min(x^2, y^3)
double test_out = std::min(std::pow(test_in[0], 2), std::pow(test_in[1], 3));

hov_wk_forward(tapeId, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
hov_wk_forward(*tapePtr, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
test_in.data(), X, out.data(), Y);

hos_ov_reverse(tapeId, dim_out, dim_in, degree_hos_reverse, num_dirs, U, Z);
hos_ov_reverse(*tapePtr, dim_out, dim_in, degree_hos_reverse, num_dirs, U, Z);

BOOST_TEST(out[0] == test_out, tt::tolerance(tol));
BOOST_TEST(Y[0][0][0] == 2 * test_in[0] * X[0][0][0], tt::tolerance(tol));
Expand Down Expand Up @@ -198,10 +198,10 @@ BOOST_AUTO_TEST_CASE(MinOperator_HOS_OV_REVERSE) {
test_in[1] = 1.0;
// min(x^2, y^3)
test_out = std::min(std::pow(test_in[0], 2), std::pow(test_in[1], 3));
hov_wk_forward(tapeId, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
hov_wk_forward(*tapePtr, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
test_in.data(), X, out.data(), Y);

hos_ov_reverse(tapeId, dim_out, dim_in, degree_hos_reverse, num_dirs, U, Z);
hos_ov_reverse(*tapePtr, dim_out, dim_in, degree_hos_reverse, num_dirs, U, Z);

BOOST_TEST(out[0] == test_out, tt::tolerance(tol));
BOOST_TEST(Y[0][0][0] == 3 * std::pow(test_in[1], 2) * X[1][0][0],
Expand Down Expand Up @@ -246,10 +246,10 @@ BOOST_AUTO_TEST_CASE(MinOperator_HOS_OV_REVERSE) {
// min(x^2, y^3)
test_out = std::min(std::pow(test_in[0], 2), std::pow(test_in[1], 3));
std::cout << "tie point" << std::endl;
hov_wk_forward(tapeId, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
hov_wk_forward(*tapePtr, dim_out, dim_in, degree_hov_forward, keep, num_dirs,
test_in.data(), X, out.data(), Y);

hos_ov_reverse(tapeId, dim_out, dim_in, degree_hos_reverse, num_dirs, U,
hos_ov_reverse(*tapePtr, dim_out, dim_in, degree_hos_reverse, num_dirs, U,
Z);

BOOST_TEST(out[0] == test_out, tt::tolerance(tol));
Expand Down
11 changes: 6 additions & 5 deletions ADOL-C/boost-test/integration_tests/accumulate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../const.h"
#include "adolc/valuetape/valuetape.h"
#include <adolc/adolc.h>
#include <array>
#include <boost/test/unit_test.hpp>
Expand All @@ -13,16 +14,16 @@ template <typename T, size_t N> T your_function(const std::array<T, N> &indep) {
}

BOOST_AUTO_TEST_CASE(AccumulateGradientCorrectness) {
const short tapeId = createNewTape();
setCurrentTape(tapeId);
auto tapePtr = std::make_unique<ValueTape>();
setCurrentTapePtr(tapePtr.get());
constexpr size_t dim = 2;
std::array<double, dim> inputs;
inputs.fill(2.0);
std::array<double, 1> out;

std::array<adouble, dim> indeps;

trace_on(tapeId);
trace_on(*tapePtr);
{
for (size_t i = 0; i < dim; ++i) {
indeps[i] <<= inputs[i];
Expand All @@ -31,10 +32,10 @@ BOOST_AUTO_TEST_CASE(AccumulateGradientCorrectness) {
adouble sum = your_function(indeps);
sum >>= out[0];
}
trace_off();
trace_off(*tapePtr);

std::array<double, dim> grad;
gradient(tapeId, dim, inputs.data(), grad.data());
gradient(*tapePtr, dim, inputs.data(), grad.data());

// Expected gradient of sum(x_i) is all 1s
for (size_t i = 0; i < dim; ++i) {
Expand Down
9 changes: 4 additions & 5 deletions ADOL-C/boost-test/integration_tests/big_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ template <size_t dim> void taping() {

BOOST_AUTO_TEST_CASE(BigExample) {
constexpr size_t dim = 200'000;
const auto tapeId = createNewTape();
trace_on(tapeId, 1);
auto tapePtr = std::make_unique<ValueTape>();
trace_on(*tapePtr, 1);
taping<dim>();
trace_off(1);
trace_off(*tapePtr, 1);
std::vector<double> tangent(dim);
std::vector<double> out(dim);
std::fill(tangent.begin(), tangent.end(), 1.0);
// printTapeStats(tapeId);
gradient(tapeId, dim, tangent.data(), out.data());
gradient(*tapePtr, dim, tangent.data(), out.data());
for (auto i = 0; i < out.size(); i++) {
BOOST_TEST(out[i] == (i + 1), tt::tolerance(tol));
}
Expand Down
55 changes: 27 additions & 28 deletions ADOL-C/boost-test/integration_tests/checkpointing.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "adolc/valuetape/valuetape.h"
#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)
Expand All @@ -25,9 +24,9 @@ int euler_step_act(size_t, adouble *y) {

BOOST_AUTO_TEST_SUITE(test_checkpoint_example)
BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {
const auto tapeIdFull = createNewTape();
const auto tapeIdPart = createNewTape();
const auto tapeIdCheck = createNewTape();
auto tapeFullPtr = std::make_unique<ValueTape>();
auto tapePartPtr = std::make_unique<ValueTape>();
auto tapeCheckPtr = std::make_unique<ValueTape>();

const size_t n = 2; // Number of state variables
const int steps = 100; // Number of time steps
Expand All @@ -44,9 +43,9 @@ BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {
std::vector<double> grad_part(n); // Gradient from checkpointing

// Full taping of the time step loop
trace_on(tapeIdFull);
trace_on(*tapeFullPtr);
{
currentTape().ensureContiguousLocations(n);
currentTapePtr()->ensureContiguousLocations(n);
std::vector<adouble> y_adouble_1(n);
std::vector<adouble> con(n);
con[0] <<= conp[0];
Expand All @@ -59,21 +58,21 @@ BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {
}
y_adouble_1[0] + y_adouble_1[1] >>= out[0];
}
trace_off(); // tapeIdFull
trace_off(*tapeFullPtr); // *tapeFullPtr

// Compute gradient using full taping
gradient(tapeIdFull, n, conp.data(), grad_full.data());
gradient(*tapeFullPtr, n, conp.data(), grad_full.data());
// Do not always retape

// Partial taping with checkpointing
trace_on(tapeIdPart);
trace_on(*tapePartPtr);
{
currentTape().ensureContiguousLocations(n);
currentTapePtr()->ensureContiguousLocations(n);
std::vector<adouble> y_adouble_2(n);
std::vector<adouble> con2(n);

// Checkpointing setup
CP_Context cpc(tapeIdPart, tapeIdCheck,
CP_Context cpc(*tapePartPtr, *tapeCheckPtr,
euler_step_act); // Checkpointing context
cpc.setDoubleFct(euler_step); // Double version of the time step function
cpc.setNumberOfSteps(steps); // Number of time steps
Expand All @@ -87,17 +86,17 @@ BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {
y_adouble_2[0] = con2[0];
y_adouble_2[1] = con2[1];

cpc.checkpointing(tapeIdPart); // Perform checkpointing
cpc.checkpointing(*tapePartPtr); // Perform checkpointing

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

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

// Compute gradient using checkpointing
gradient(tapeIdPart, n, conp.data(), grad_part.data());
gradient(*tapePartPtr, 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));
Expand All @@ -106,9 +105,9 @@ BOOST_AUTO_TEST_CASE(Checkpointing_Gradient_Comparison) {

BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {

const short tapeIdFull = createNewTape();
const short tapeIdPart = createNewTape();
const short tapeIdCheck = createNewTape();
auto tapeFullPtr = std::make_unique<ValueTape>();
auto tapePartPtr = std::make_unique<ValueTape>();
auto tapeCheckPtr = std::make_unique<ValueTape>();

const size_t n = 2; // Number of state variables
const int steps = 100; // Number of time steps
Expand All @@ -124,9 +123,9 @@ BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {
std::vector<double> grad_part(n); // Gradient from checkpointing

// Full taping of the time step loop
trace_on(tapeIdFull, 1);
trace_on(*tapeFullPtr, 1);
{
currentTape().ensureContiguousLocations(n);
currentTapePtr()->ensureContiguousLocations(n);
std::vector<adouble> y_adouble_1(n);
std::vector<adouble> con(n);
con[0] <<= conp[0];
Expand All @@ -139,7 +138,7 @@ BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {
}
y_adouble_1[0] + y_adouble_1[1] >>= out[0];
}
trace_off();
trace_off(*tapeFullPtr);

// weights
double **U = myalloc2(2, 1);
Expand All @@ -151,17 +150,17 @@ BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {
double **Z_part = myalloc2(2, 2);

// Compute vector-mode reverse
fov_reverse(tapeIdFull, 1, 2, 2, U, Z_full);
fov_reverse(*tapeFullPtr, 1, 2, 2, U, Z_full);

// Partial taping with checkpointing
trace_on(tapeIdPart, 1);
trace_on(*tapePartPtr, 1);
{
currentTape().ensureContiguousLocations(n);
currentTapePtr()->ensureContiguousLocations(n);
std::vector<adouble> y_adouble_2(n);
std::vector<adouble> con2(n);

// Checkpointing setup
CP_Context cpc(tapeIdPart, tapeIdCheck,
CP_Context cpc(*tapePartPtr, *tapeCheckPtr,
euler_step_act); // Checkpointing context
cpc.setDoubleFct(euler_step); // Double version of the time step function
cpc.setNumberOfSteps(steps); // Number of time steps
Expand All @@ -175,17 +174,17 @@ BOOST_AUTO_TEST_CASE(Checkpointing_fov_reverse) {
y_adouble_2[0] = con2[0];
y_adouble_2[1] = con2[1];

cpc.checkpointing(tapeIdPart); // Perform checkpointing
cpc.checkpointing(*tapePartPtr); // Perform checkpointing

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

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

// Compute gradient using checkpointing
fov_reverse(tapeIdPart, 1, 2, 2, U, Z_part);
fov_reverse(*tapePartPtr, 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)
Expand Down
12 changes: 7 additions & 5 deletions ADOL-C/boost-test/integration_tests/determinante.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "adolc/valuetape/valuetape.h"
#include <memory>
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

Expand Down Expand Up @@ -49,16 +51,16 @@ adouble det(const T &A, size_t row,

BOOST_AUTO_TEST_SUITE(test_detem_example)
BOOST_AUTO_TEST_CASE(DeterminanteTest) {
const short tapeId = createNewTape();
setCurrentTape(tapeId);
auto tapePtr = std::make_unique<ValueTape>();
setCurrentTapePtr(tapePtr.get());

const int keep = 1;
constexpr size_t n = 7;
int m = 1;

std::array<std::array<adouble, n>, n> A;

trace_on(tapeId, keep); // tapeId=1=keep
trace_on(*tapePtr, keep);
double detout = 0.0;
double diag = 1.0; // here keep the intermediates for
for (size_t i = 0; i < n; i++) // the subsequent call to reverse
Expand All @@ -75,12 +77,12 @@ BOOST_AUTO_TEST_CASE(DeterminanteTest) {

ad >>= detout;
BOOST_TEST(detout == diag, tt ::tolerance(tol));
trace_off();
trace_off(*tapePtr);

std::array<double, 1> u;
u[0] = 1.0;
std::array<double, n * n> B;
reverse(tapeId, 1, n * n, 0, u.data(),
reverse(*tapePtr, 1, n * n, 0, u.data(),
B.data()); // call reverse to calculate the gradient

std::array<double, n> res = {5.4071428571428601, 0, 0, 0, 0, 0, 0};
Expand Down
Loading
Loading