From b8b6915b2504ecf5b2166c36d47cb0069fb33e60 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Thu, 6 Dec 2018 14:40:50 +0100 Subject: [PATCH] Expose AD driver methods via ADHelper bass class. Further features provided by the drivers are exposed through the base helper class. --- .../deal.II/differentiation/ad/ad_helpers.h | 44 +++++++++++++++++++ source/differentiation/ad/ad_helpers.cc | 38 ++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/include/deal.II/differentiation/ad/ad_helpers.h b/include/deal.II/differentiation/ad/ad_helpers.h index 7aad5de7ed..4d8bcc78a3 100644 --- a/include/deal.II/differentiation/ad/ad_helpers.h +++ b/include/deal.II/differentiation/ad/ad_helpers.h @@ -478,6 +478,50 @@ namespace Differentiation activate_recorded_tape( const typename Types::tape_index tape_index); + /** + * Return a flag that, when true, indicates that the + * retaping of the dependent function is necessary for a reliable + * computation to be performed on a tape with the given @p tape_index. + * This may be necessary a sign comparison within branched operations + * yields different results to those computed at the original tape + * evaluation point. + * + * For the output of this function to be meaningful, it must be called + * after the activate_recorded_tape() is called and the new evaluation + * point for the tape (i.e. values of the independent variables) have + * been set and subsequently used (i.e. in the determination of the values + * or derivatives of the dependent variables). + */ + bool + recorded_tape_requires_retaping( + const typename Types::tape_index tape_index) const; + + /** + * Return a flag that, when true, indicates that the + * retaping of the dependent function is necessary for a reliable + * computation to be performed on the avtive tape. This may be necessary a + * sign comparison within branched operations yields different results to + * those computed at the original tape evaluation point. + * + * For the output of this function to be meaningful, it must be called + * after the activate_recorded_tape() is called and the new evaluation + * point for the tape (i.e. values of the independent variables) have + * been set and subsequently used (i.e. in the determination of the values + * or derivatives of the dependent variables). + */ + bool + active_tape_requires_retaping() const; + + /** + * Clears and removes the currently active tape. + * + * This is typically only necessary when branch switching is detected on + * the original tape at evaluation point. This state can be checked using + * the active_tape_requires_retaping() function. + */ + void + clear_active_tape(); + //@} protected: diff --git a/source/differentiation/ad/ad_helpers.cc b/source/differentiation/ad/ad_helpers.cc index c1a29cb1f3..d8a7866ba3 100644 --- a/source/differentiation/ad/ad_helpers.cc +++ b/source/differentiation/ad/ad_helpers.cc @@ -475,6 +475,44 @@ namespace Differentiation + template + bool + ADHelperBase::recorded_tape_requires_retaping( + const typename Types::tape_index tape_index) const + { + if (ADNumberTraits::is_tapeless == true) + return false; + + return taped_driver.requires_retaping(tape_index); + } + + + + template + bool + ADHelperBase::active_tape_requires_retaping() + const + { + if (ADNumberTraits::is_tapeless == true) + return false; + + return taped_driver.last_action_requires_retaping(); + } + + + + template + void + ADHelperBase::clear_active_tape() + { + if (ADNumberTraits::is_tapeless == true) + return; + + taped_driver.remove_tape(taped_driver.active_tape_index()); + } + + + template void ADHelperBase::activate_tape( -- 2.39.5