Muliple tapes in CoDiPack #21
MaxSagebaum
started this conversation in
General
Replies: 2 comments
|
Thanks for the example. I will test it as soon as possible. |
0 replies
|
I managed to create a simple example. Do you have any comments or spot any mistakes? using Real = codi::RealReverse;
using Tape = typename Real::Tape;
Tape& tape = Real::getTape();
Real f(Real a, Real b)
{
return (a + b) * a;
}
Real g(Real a, Real b)
{
return a / b;
}
Tape tape1;
Tape tape2;
int main()
{
Real a = 2.0;
int ai1, ai2;
Real b = 3.0;
int bi1, bi2;
Real y1;
Real y2;
tape.setActive();
tape.registerInput(a);
tape.registerInput(b);
y1 = f(a, b);
tape.registerOutput(y1);
y1.setGradient(1.0);
tape.setPassive();
ai1 = a.getIdentifier();
bi1 = b.getIdentifier();
tape1.swap(tape);
//tape.deactivateValue(a);
//tape.deactivateValue(b);
// ----------------------------------
tape.setActive();
tape.registerInput(a);
tape.registerInput(b);
y2 = g(a, b);
tape.registerOutput(y2);
y2.setGradient(1.0);
tape.setPassive();
ai2 = a.getIdentifier();
bi2 = b.getIdentifier();
tape2.swap(tape);
for(int I = 0; I < 2; ++I)
{
cout << "=============================== outer iteration " << I << "\n\n";
cout << "-------------- tape 1\n";
tape1.evaluate();
a.getIdentifier() = ai1;
b.getIdentifier() = bi1;
tape.swap(tape1);
cout << "a.getGradient() = " << a.getGradient() << endl;
cout << "b.getGradient() = " << b.getGradient() << endl;
cout << "\n-------------- tape 2\n";
tape1.swap(tape);
tape2.evaluate();
a.getIdentifier() = ai2;
b.getIdentifier() = bi2;
tape.swap(tape2);
cout << "a.getGradient() = " << a.getGradient() << endl;
cout << "b.getGradient() = " << b.getGradient() << endl;
tape2.swap(tape);
}
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
@benno90
This feature is already there:
All reactions