CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
Example 14 - RefrenceActiveType

Goal: Optimize statements where the same argument occurs multiple times.

Prerequisite: Tutorial 2 - Reverse mode AD

Function: 1D polynomial

template<typename Real>
void func(Real const& x, Real& y) {
y = 3.0*x*x*x*x + 5.0*x*x*x - 3.0*x*x + 2.0*x -4.0;
}

Full code:

#include <codi.hpp>
#include <iostream>
#include <sstream>
template<typename Real>
void func(Real const& x, Real& y) {
y = 3.0*x*x*x*x + 5.0*x*x*x - 3.0*x*x + 2.0*x -4.0;
}
template<typename Real>
void funcRef(Real& x, Real& y) {
y = 3.0*xRef*xRef*xRef*xRef + 5.0*xRef*xRef*xRef - 3.0*xRef*xRef + 2.0*xRef -4.0;
}
int main(int nargs, char** args) {
using Tape = typename Real::Tape;
Real x = 3.14;
Real y;
Tape& tape = Real::getTape();
std::cout << "Func with standard codi type." << std::endl;
tape.setActive();
tape.registerInput(x);
func(x, y);
tape.registerOutput(y);
tape.setPassive();
std::cout << "f(3.14) = (" << y << ")" << std::endl;
y.setGradient(1.0);
tape.evaluate();
std::cout << "df/dx = (" << x.getGradient() << ")" << std::endl;
std::ostringstream run1;
tape.printStatistics(run1);
tape.reset();
std::cout << "Func with reference codi type." << std::endl;
tape.setActive();
tape.registerInput(x);
funcRef(x, y);
tape.registerOutput(y);
tape.setPassive();
std::cout << "f(3.14) = (" << y << ")" << std::endl;
y.setGradient(1.0);
tape.evaluate();
std::cout << "df/dx = (" << x.getGradient() << ")" << std::endl;
std::ostringstream run2;
tape.printStatistics(run2);
tape.reset();
std::cout << std::endl;
std::cout << "Statistics for the standard codi type:" << std::endl;
std::cout << run1.str() << std::endl << std::endl;
std::cout << "Statistics for the reference codi type:" << std::endl;
std::cout << run2.str() << std::endl << std::endl;
return 0;
}
Holds a reference to an ActiveType for manual optimization of common arguments.
Definition referenceActiveType.hpp:59

The codi:ReferenceActiveType can be used to optimize statements where the same argument is used multiple times. This works only with a Jacobian taping approach and 12 bytes are saved per extra occurrence of the variable.

An alternative is to use the flag -DCODI_RemoveDuplicateJacobianArguments=1. This will enable an extra pass in Jacobian tapes that removes such duplicated arguments but introduces a slight performance reduction.