diff --git a/ADOL-C/boost-test/CMakeLists.txt b/ADOL-C/boost-test/CMakeLists.txt index 6daa6f40..137d40de 100644 --- a/ADOL-C/boost-test/CMakeLists.txt +++ b/ADOL-C/boost-test/CMakeLists.txt @@ -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}") diff --git a/ADOL-C/boost-test/uni5_for.cpp b/ADOL-C/boost-test/uni5_for.cpp new file mode 100644 index 00000000..593ec756 --- /dev/null +++ b/ADOL-C/boost-test/uni5_for.cpp @@ -0,0 +1,85 @@ + +/* +File for explicit testing functions from uni5_for.c file. +*/ + +#define BOOST_TEST_DYN_LINK +#include + +namespace tt = boost::test_tools; + +#include + +#include "const.h" + +#include +#include +#include + +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 in = {-2.0, 0.0, 1.5}; + std::array 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 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 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() \ No newline at end of file diff --git a/ADOL-C/src/taping.c b/ADOL-C/src/taping.c index 7972dce2..6451952e 100644 --- a/ADOL-C/src/taping.c +++ b/ADOL-C/src/taping.c @@ -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; + } } /****************************************************************************/ diff --git a/ADOL-C/src/uni5_for.c b/ADOL-C/src/uni5_for.c index 2368a031..10c73200 100644 --- a/ADOL-C/src/uni5_for.c +++ b/ADOL-C/src/uni5_for.c @@ -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_) @@ -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; @@ -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