Goal: Learn how to use the external function user data structure.
Prerequisite: Tutorial 2 - Reverse mode AD
Function: Simple real valued function
Full code:
#include <codi.hpp>
#include <iostream>
return x * x * x;
}
Identifier t_i = data->
getData<
int>();
double scale = data->
getData<
double>();
std::cout << " Reverse: t_b = " << t_b << ", scale = " << scale << std::endl;
}
void extFunc_del(Tape* t, void* d) {
delete data;
std::cout << " Reset: data is deleted." << std::endl;
}
int main(int nargs, char** args) {
tape.setActive();
tape.registerInput(x);
tape.registerOutput(y);
tape.setPassive();
tape.evaluate();
std::cout << "f(f(4.0)) = " << y << std::endl;
std::cout <<
"d(f ○ f)/dx(4.0) = " << x.
getGradient() << std::endl;
tape.reset();
return 0;
}
typename Tape::Identifier Identifier
See LhsExpressionInterface.
Definition activeTypeBase.hpp:78
void getData(Type &value)
Get a copy of the next data item.
Definition externalFunctionUserData.hpp:171
size_t addData(Type const &value)
Definition externalFunctionUserData.hpp:151
User-defined evaluation functions for the taping process.
Definition externalFunction.hpp:102
virtual Real getAdjoint(Identifier const &index, size_t dim)=0
Get the adjoint component.
The data in codi::ExternalFunctionUserData is accessed in a toroidal pattern. After all added data items have been accessed the next call will start again at the first data item. This allows for multiple reverse interpretations.