Goal: Use the generialized interface for the adjoint vector access.
Prerequisite: Example 2 - Custom adjoint vector evaluation
Function: Simple vector valued function
template<typename Real>
void func(const Real* x, size_t l, Real* y) {
y[0] = 0.0;
y[1] = 1.0;
for(size_t i = 0; i < l; ++i) {
y[0] += x[i];
y[1] *= x[i];
}
}
Full code:
#include <codi.hpp>
#include <iostream>
template<typename Real>
void func(const Real* x, size_t l, Real* y) {
y[0] = 0.0;
y[1] = 1.0;
for(size_t i = 0; i < l; ++i) {
y[0] += x[i];
y[1] *= x[i];
}
}
int main(int nargs, char** args) {
Real x[5];
Real y[2];
x[0] = 1.0;
x[1] = 2.0;
x[2] = 3.0;
x[3] = 4.0;
x[4] = 5.0;
tape.setActive();
for(size_t i = 0; i < 5; ++i) {
tape.registerInput(x[i]);
}
func(x, 5, y);
tape.registerOutput(y[0]);
tape.registerOutput(y[1]);
tape.setPassive();
for(
size_t dim = 0;
dim < ai->getVectorSize(); ++
dim) {
}
for(size_t i = 0; i < 5; ++i) {
for(
size_t dim = 0;
dim < ai->getVectorSize(); ++
dim) {
}
}
std::cout << "Custom adjoint vector interface:" << std::endl;
std::cout << "f(1 .. 5) = (" << y[0] << ", " << y[1] << ")" << std::endl;
std::cout << "df/dx (1 .. 5) = \n" << jacobian << std::endl;
tape.reset();
delete vh;
return 0;
}
Since the (CustomAdjointVectorHelper) is hardcoded to the adjoint vector type, there is also a codi::CustomAdjointVectorInterface definition. This interface allows to program for an arbitrary vector dimension. In order to access the vector with this interface the codi::VectorAccessInterface needs to be used. It can be obtained via the method getVectorInterface.