CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::EnzymeExternalFunctionHelper< T_Type > Struct Template Reference

Helper class for the implementation of an external function with Enzyme in CoDiPack. More...

#include <enzymeExternalFunctionHelper.hpp>

Inheritance diagram for codi::EnzymeExternalFunctionHelper< T_Type >:

Public Types

using Base = ExternalFunctionHelper< Type >
 Base class abbreviation.
 
using Identifier = typename Type::Identifier
 See LhsExpressionInterface.
 
using PrimalFunc = typename Base::PrimalFunc
 See ExternalFunctionHelper.
 
using Real = typename Type::Real
 See LhsExpressionInterface.
 
using Tape = typename Type::Tape
 See LhsExpressionInterface.
 
using Type = T_Type
 See ExternalFunctionHelper.
 
- Public Types inherited from codi::ExternalFunctionHelper< T_Type, T_Synchronization, T_ThreadInformation >
using ForwardFunc = void(*)(Real const *x, Real const *x_d, size_t m, Real *y, Real *y_d, size_t n, ExternalFunctionUserData *d)
 Function interface for the forward AD call of an external function.
 
using Identifier = typename Type::Identifier
 See LhsExpressionInterface.
 
using PrimalFunc = void(*)(Real const *x, size_t m, Real *y, size_t n, ExternalFunctionUserData *d)
 Function interface for the primal call of an external function.
 
using Real = typename Type::Real
 See LhsExpressionInterface.
 
using ReverseFunc = void(*)(Real const *x, Real *x_b, size_t m, Real const *y, Real const *y_b, size_t n, ExternalFunctionUserData *d)
 Function interface for the reverse AD call of an external function.
 
using Synchronization = T_Synchronization
 See ExternalFunctionHelper.
 
using Tape = typename Type::Tape
 See LhsExpressionInterface.
 
using ThreadInformation = T_ThreadInformation
 See ExternalFunctionHelper.
 
using Type = T_Type
 See ExternalFunctionHelper.
 

Public Member Functions

template<PrimalFunc func>
void callAndAddToTape ()
 
template<PrimalFunc func>
void callAndAddToTape (Type const *x, size_t m, Type *y, size_t n)
 Adds all inputs in x and outputs in y to the external function and then calls callAndAddToTape().
 
 EnzymeExternalFunctionHelper ()
 Constructor.
 
- Public Member Functions inherited from codi::ExternalFunctionHelper< T_Type, T_Synchronization, T_ThreadInformation >
void addInput (Type const &input)
 Add an input value.
 
void addOutput (Type &output)
 Add an output value.
 
void addToTape (ReverseFunc reverseFunc, ForwardFunc forwardFunc=nullptr, PrimalFunc primalFunc=nullptr)
 Add the external function to the tape.
 
template<typename Data >
void addUserData (Data const &data)
 Add user data. See ExternalFunctionUserData for details.
 
void callPrimalFunc (PrimalFunc func)
 
template<typename FuncObj , typename... Args>
void callPrimalFuncWithADType (FuncObj &func, Args &&... args)
 
void disableInputPrimalStore ()
 Do not store primal input values. In function calls, pointers to primal inputs will be null.
 
void disableOutputPrimalStore ()
 Do not store primal output values. In function calls, pointers to primal outputs will be null.
 
void disableRenewOfPrimalValues ()
 Do not update the inputs and outputs from the primal values of the tape. Has no effect on Jacobian tapes.
 
void enableReallocationOfPrimalValueVectors ()
 
 ExternalFunctionHelper (bool primalFuncUsesADType=false)
 Constructor.
 
ExternalFunctionUserDatagetExternalFunctionUserData ()
 
 ~ExternalFunctionHelper ()
 Destructor.
 

Additional Inherited Members

- Protected Attributes inherited from codi::ExternalFunctionHelper< T_Type, T_Synchronization, T_ThreadInformation >
EvalData * data
 External function data.
 
bool getPrimalValuesFromPrimalValueVector
 
std::vector< Type * > outputValues
 References to output values.
 
bool reallocatePrimalVectors
 
bool storeInputOutputForPrimalEval
 If a primal call with a self-implemented function will be done.
 
bool storeInputPrimals
 If input primals are stored. Can be disabled by the user.
 
bool storeOutputPrimals
 If output primals are stored. Can be disabled by the user.
 
std::vector< Realy
 Shared vector of output variables.
 

Detailed Description

template<typename T_Type>
struct codi::EnzymeExternalFunctionHelper< T_Type >

Helper class for the implementation of an external function with Enzyme in CoDiPack.

The class helps the user to create derivative functions with Enzyme and add them to the tape. See ExternalFunctionHelper for the general configuration options and procedures. This class only supports the operation mode 1 "Implemented primal function". In this implementation, this mode requires just one call to callAndAddToTape() which calls the primal function and adds it to the tape.

The procedure of pushing a differentiated function with Enzyme is as follows.

  1. All function inputs and outputs are specified.
  2. Call callAndAddToTape method with the primal function.

An example is:

eh.addInput(x);
eh.addOutput(y[1]);
eh.template callAndAddToTape<func_prim>();
Helper class for the implementation of an external function with Enzyme in CoDiPack.
Definition: enzymeExternalFunctionHelper.hpp:84
void addInput(Type const &input)
Add an input value.
Definition: externalFunctionHelper.hpp:451
void addOutput(Type &output)
Add an output value.
Definition: externalFunctionHelper.hpp:492
std::vector< Real > y
Shared vector of output variables.
Definition: externalFunctionHelper.hpp:397

This helper provides an additional overload where the inputs and outputs can be directly added:

eh.template callAndAddToTape<func_prim>(&x, 1, &y[2], 1);
Helper class for the implementation of an external function in CoDiPack.
Definition: externalFunctionHelper.hpp:102

The full example can be found at Example 24 - Enzyme external function helper.

Template Parameters
T_TypeThe CoDiPack type that is used outside of the external function.

Member Function Documentation

◆ callAndAddToTape()

template<typename T_Type >
template<PrimalFunc func>
void codi::EnzymeExternalFunctionHelper< T_Type >::callAndAddToTape ( )
inline

Calls the primal function and adds the Enzyme generated functions to the tape.

Is a combination of ExternalFunctionHelper::callPrimalFunc and ExternalFunctionHelper::addToTape.


The documentation for this struct was generated from the following file: