Goal: Learn how to use the external function user data structure.
Prerequisite: Tutorial 2 - Reverse mode AD
Function: Simple real valued function
Real func(const Real& x) {
return x * x * x;
}
Full code:
#include <codi.hpp>
#include <iostream>
Real func(const Real& x) {
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) {
Real x = 4.0;
tape.setActive();
tape.registerInput(x);
Real t = func(x);
Real y = func(t);
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;
}
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.