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

Fix memory leak in zos_pl_forward and add deallocation of tape signature to freeTapeResources #84

Merged
merged 5 commits into from
Nov 22, 2024
Merged
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
1 change: 1 addition & 0 deletions ADOL-C/boost-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(SOURCE_FILES
traceSecOrderScalar.cpp
traceSecOrderVector.cpp
traceFixedPointScalarTests.cpp
uni5_for.cpp
)
add_executable(boost-test-adolc ${SOURCE_FILES})
target_include_directories(boost-test-adolc PRIVATE "${ADOLC_INCLUDE_DIR}")
Expand Down
85 changes: 85 additions & 0 deletions ADOL-C/boost-test/uni5_for.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

/*
File for explicit testing functions from uni5_for.c file.
*/

#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

namespace tt = boost::test_tools;

#include <adolc/adolc.h>

#include "const.h"

#include <array>
#include <numeric>
#include <vector>

BOOST_AUTO_TEST_SUITE(uni5_for)

BOOST_AUTO_TEST_CASE(ZOS_PL_FORWARD) {

enableMinMaxUsingAbs();

const int16_t tag = 1;

const int dim_out = 1;
const int dim_in = 3;

std::array<double, dim_in> in = {-2.0, 0.0, 1.5};
std::array<adouble, dim_in> indep;
double out[] = {0.0};

// ---------------------- trace on ---------------------
// function is given by fabs(in_2 + fabs(in_1 + fabs(in_0)))
trace_on(tag);

// init independents
std::for_each(in.begin(), in.end(), [&, i = 0](int value) mutable {
indep[i] <<= in[i];
++i;
});

// fabs(in_2 + fabs(in_1 + fabs(in_0)))
adouble dep =
std::accumulate(indep.begin(), indep.end(), adouble(0.0),
[](adouble sum, adouble val) { return fabs(sum + val); });

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

// ---------------------- trace off ---------------------

// test outout
double test_out =
std::accumulate(in.begin(), in.end(), 0.0, [](double sum, double val) {
return std::fabs(sum + val);
});
BOOST_TEST(out[0] == test_out, tt::tolerance(tol));

// test num switches
int num_switches = get_num_switches(tag);
BOOST_TEST(num_switches == dim_in, tt::tolerance(tol));

const int keep = 0;
std::vector<double> switching_vec(num_switches);
zos_pl_forward(tag, dim_out, dim_in, keep, in.data(), out,
switching_vec.data());

// test outout of zos_pl_forward
BOOST_TEST(out[0] == test_out, tt::tolerance(tol));

// test switching vector
std::vector<double> test_switching_vec(num_switches);
test_switching_vec[0] = in[0];
for (std::size_t i = 1; i < num_switches; ++i) {
test_switching_vec[i] = std::fabs(test_switching_vec[i - 1]) + in[i];
}
BOOST_CHECK_EQUAL_COLLECTIONS(switching_vec.begin(), switching_vec.end(),
test_switching_vec.begin(),
test_switching_vec.end());

removeTape(tag, ADOLC_REMOVE_COMPLETELY);
}
BOOST_AUTO_TEST_SUITE_END()
4 changes: 4 additions & 0 deletions ADOL-C/src/taping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,10 @@ void freeTapeResources(TapeInfos *tapeInfos) {
fclose(tapeInfos->tay_file);
tapeInfos->tay_file = NULL;
}
if (tapeInfos->signature != NULL) {
free(tapeInfos->signature);
tapeInfos->signature = NULL;
}
}

/****************************************************************************/
Expand Down
16 changes: 5 additions & 11 deletions ADOL-C/src/uni5_for.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,6 @@ int hov_forward(
#endif

locint switchnum = 0;
#if !defined(_NTIGHT_)
double *signature = NULL;
#endif

/* extern diff. function variables */
#if defined(_EXTERN_)
Expand Down Expand Up @@ -1097,13 +1094,10 @@ int hov_forward(
dp_T0 = myalloc1(ADOLC_CURRENT_TAPE_INFOS.stats[NUM_MAX_LIVES]);
ADOLC_CURRENT_TAPE_INFOS.dp_T0 = dp_T0;

if (ADOLC_CURRENT_TAPE_INFOS.stats[NO_MIN_MAX]) {
if (ADOLC_CURRENT_TAPE_INFOS.signature == NULL) {
signature = myalloc1(ADOLC_CURRENT_TAPE_INFOS.stats[NUM_SWITCHES]);
ADOLC_CURRENT_TAPE_INFOS.signature = signature;
} else
signature = ADOLC_CURRENT_TAPE_INFOS.signature;
}
if ((ADOLC_CURRENT_TAPE_INFOS.stats[NO_MIN_MAX]) &&
(ADOLC_CURRENT_TAPE_INFOS.signature == NULL))
ADOLC_CURRENT_TAPE_INFOS.signature =
myalloc1(ADOLC_CURRENT_TAPE_INFOS.stats[NUM_SWITCHES]);

ADOLC_CURRENT_TAPE_INFOS.dpp_T = &dp_T0;
ADOLC_CURRENT_TAPE_INFOS.numTay = 0;
Expand Down Expand Up @@ -4283,7 +4277,7 @@ int hov_forward(
MINDEC(ret_c, 2);
}
if (ADOLC_CURRENT_TAPE_INFOS.stats[NO_MIN_MAX]) {
signature[switchnum] = dp_T0[arg];
ADOLC_CURRENT_TAPE_INFOS.signature[switchnum] = dp_T0[arg];
#if defined(_ABS_NORM_) || defined(_ABS_NORM_SIG_)
swargs[switchnum] = dp_T0[arg];
#endif
Expand Down
Loading