Goal: Add external functions to the tape via Enzyme.
Prerequisite: Example 10 - External function helper
Function:
template<typename Type>
Type func(Type const& v) {
return 42.0 * v * v;
}
y[0] = 42.0 * x[0] * x[0];
}
Full code:
#include <iostream>
#include <codi.hpp>
template<typename Type>
Type func(Type const& v) {
return 42.0 * v * v;
}
y[0] = 42.0 * x[0] * x[0];
}
int main(int nargs, char** args) {
tape.setActive();
tape.registerInput(x);
y[0] = func(x);
#if CODI_EnableEnzyme
eh.template callAndAddToTape<func_prim>();
eh.template callAndAddToTape<func_prim>(&x, 1, &y[2], 1);
#else
std::cerr << "Enzyme is not enabled for CoDiPack. Enable it with: -DCODI_EnableEnzyme=1." << std::endl;
#endif
for(int i = 0; i < 3; i += 1) {
tape.registerOutput(y[i]);
}
tape.setPassive();
for(int i = 0; i < 3; i += 1) {
tape.clearAdjoints();
tape.evaluate();
std::cout <<
"Gradient of dy[" << i <<
"]/dx: " << x.
getGradient() << std::endl;
}
}
Helper class for the implementation of an external function with Enzyme in CoDiPack.
Definition enzymeExternalFunctionHelper.hpp:84