1 #ifndef CPPAD_CG_MODEL_C_SOURCE_GEN_LOOPS_FOR0_INCLUDED 2 #define CPPAD_CG_MODEL_C_SOURCE_GEN_LOOPS_FOR0_INCLUDED 26 std::vector<CG<Base> > prepareGraphForward0WithLoops(CodeHandler<Base>& handler,
28 const std::vector<CG<Base>>& x,
29 LoopFreeModel<Base>* funNoLoops,
30 const std::set<LoopModel<Base>*>& loopTapes) {
32 using namespace loops;
34 using CGBase = CG<Base>;
36 std::vector<CGBase> y(m);
39 std::vector<CGBase> tmps;
44 if (funNoLoops !=
nullptr) {
45 const std::vector<size_t>& origEq = funNoLoops->getOrigDependentIndexes();
47 std::vector<CGBase> depNL = funNoLoops->getTape().Forward(0, x);
50 for (
size_t e = 0; e < origEq.size(); e++) {
51 y[origEq[e]] = depNL[e];
54 tmps.resize(depNL.size() - origEq.size());
55 for (
size_t i = origEq.size(); i < depNL.size(); i++)
56 tmps[i - origEq.size()] = depNL[i];
62 OperationNode<Base>* iterationIndexDcl = handler.makeIndexDclrNode(LoopModel<Base>::ITERATION_INDEX_NAME);
64 for (LoopModel<Base>* itl : loopTapes) {
65 LoopModel<Base>& lModel = *itl;
66 size_t nIterations = lModel.getIterationCount();
67 const std::vector<std::vector<LoopPosition> >& dependents = lModel.getDependentIndexes();
72 LoopStartOperationNode<Base>* loopStart = handler.makeLoopStartNode(*iterationIndexDcl, nIterations);
74 IndexOperationNode<Base>* iterationIndexOp = handler.makeIndexNode(*loopStart);
75 std::set<IndexOperationNode<Base>*> indexesOps;
76 indexesOps.insert(iterationIndexOp);
78 std::vector<IfElseInfo<Base> > ifElses;
83 std::vector<CGBase> indexedIndeps = createIndexedIndependents(handler, lModel, *iterationIndexOp);
84 std::vector<CGBase> xl = createLoopIndependentVector(handler, lModel, indexedIndeps, x, tmps);
89 std::vector<CGBase> yl = lModel.getTape().Forward(0, xl);
94 size_t assignOrAdd = 0;
96 const std::vector<IndexPattern*>& depPatterns = lModel.getDependentIndexPatterns();
97 std::vector<std::pair<CGBase, IndexPattern*> > indexedLoopResults(yl.size());
98 for (
size_t i = 0; i < yl.size(); i++) {
99 std::map<size_t, size_t> locationsIter2Pos;
101 for (
size_t it = 0; it < nIterations; it++) {
102 if (dependents[i][it].original < m) {
103 locationsIter2Pos[it] = dependents[i][it].original;
107 indexedLoopResults[i] = createLoopResult(handler, locationsIter2Pos, nIterations,
108 yl[i], depPatterns[i], assignOrAdd,
109 *iterationIndexOp, ifElses);
112 LoopEndOperationNode<Base>* loopEnd = createLoopEnd(handler, *loopStart, indexedLoopResults, indexesOps, assignOrAdd);
114 for (
size_t i = 0; i < dependents.size(); i++) {
115 for (
size_t it = 0; it < nIterations; it++) {
117 size_t e = dependents[i][it].original;
119 y[e] = handler.createCG(*handler.makeNode(CGOpCode::DependentRefRhs,{e}, {*loopEnd}));
127 moveNonIndexedOutsideLoop(handler, *loopStart, *loopEnd);
135 const std::vector<CGBase>& x) {
136 return prepareGraphForward0WithLoops(handler, _fun.Range(), x, _funNoLoops, _loopTapes);
virtual std::vector< CGBase > prepareForward0WithLoops(CodeHandler< Base > &handler, const std::vector< CGBase > &x)