|
| 1 | +//! [Example 29 - Tape cache optimization] |
| 2 | +#include <codi.hpp> |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +//! [Function] |
| 6 | +template<typename Real> |
| 7 | +void func(const Real* x, size_t l, Real* y) { |
| 8 | + y[0] = 0.0; |
| 9 | + y[1] = 1.0; |
| 10 | + for(size_t i = 0; i < l; ++i) { |
| 11 | + y[0] += x[i]; |
| 12 | + y[1] *= x[i]; |
| 13 | + } |
| 14 | +} |
| 15 | +//! [Function] |
| 16 | + |
| 17 | +int main(int nargs, char** args) { |
| 18 | + |
| 19 | + using Real = codi::RealReverseIndex; |
| 20 | + using Identifier = typename Real::Identifier; |
| 21 | + using Tape = typename Real::Tape; |
| 22 | + |
| 23 | + Real x[5]; |
| 24 | + Real y[2]; |
| 25 | + x[0] = 1.0; |
| 26 | + x[1] = 2.0; |
| 27 | + x[2] = 3.0; |
| 28 | + x[3] = 4.0; |
| 29 | + x[4] = 5.0; |
| 30 | + |
| 31 | + // Step 1: Record the tape. |
| 32 | + Tape& tape = Real::getTape(); |
| 33 | + tape.setActive(); |
| 34 | + |
| 35 | + for(size_t i = 0; i < 5; ++i) { |
| 36 | + tape.registerInput(x[i]); |
| 37 | + } |
| 38 | + |
| 39 | + func(x, 5, y); |
| 40 | + |
| 41 | + tape.registerOutput(y[0]); |
| 42 | + tape.registerOutput(y[1]); |
| 43 | + |
| 44 | + tape.setPassive(); |
| 45 | + |
| 46 | + // Step 2: Gather the input and output identifiers. |
| 47 | + Identifier xIds[5]; |
| 48 | + Identifier yIds[2]; |
| 49 | + for(int i = 0; i < 5; i += 1) { |
| 50 | + xIds[i] = x[i].getIdentifier(); |
| 51 | + } |
| 52 | + for(int i = 0; i < 2; i += 1) { |
| 53 | + yIds[i] = y[i].getIdentifier(); |
| 54 | + } |
| 55 | + |
| 56 | + // Step 3: Define the input and output iterators. |
| 57 | + auto iterX = [&xIds](auto&& func) { |
| 58 | + for(size_t i = 0; i < 5; ++i) { |
| 59 | + func(xIds[i]); |
| 60 | + } |
| 61 | + }; |
| 62 | + auto iterY = [&yIds](auto&& func) { |
| 63 | + for(size_t i = 0; i < 2; ++i) { |
| 64 | + func(yIds[i]); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + // Step 4: Apply the optimization. |
| 69 | + codi::IdentifierCacheOptimizerHotCold<Tape> co{tape}; |
| 70 | + co.eval(iterX, iterY); |
| 71 | + |
| 72 | + // Step 5: Do a tape evaluation with the translated ids. |
| 73 | + codi::Jacobian<double> jacobian(2,5); |
| 74 | + for(size_t curY = 0; curY < 2; curY += 1) { |
| 75 | + tape.gradient(yIds[curY]) = 1.0; |
| 76 | + tape.evaluate(); |
| 77 | + |
| 78 | + for(size_t curX = 0; curX < 5; curX += 1) { |
| 79 | + jacobian(curY,curX) = tape.gradient(xIds[curX]); |
| 80 | + tape.gradient(xIds[curX]) = 0.0; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + std::cout << "Reverse Jacobian:" << std::endl; |
| 85 | + std::cout << "f(1 .. 5) = (" << y[0] << ", " << y[1] << ")" << std::endl; |
| 86 | + std::cout << "df/dx (1 .. 5) = \n" << jacobian << std::endl; |
| 87 | + |
| 88 | + tape.reset(); |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | +//! [Example 29 - Tape cache optimization] |
0 commit comments