CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
code_handler_loops.hpp
1 #ifndef CPPAD_CG_CODE_HANDLER_LOOPS_INCLUDED
2 #define CPPAD_CG_CODE_HANDLER_LOOPS_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2013 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 namespace cg {
20 
21 template<class Base>
22 const std::map<size_t, LoopModel<Base>*>& CodeHandler<Base>::getLoops() const {
23  return _loops.loopModels;
24 }
25 
26 template<class Base>
27 inline LoopModel<Base>* CodeHandler<Base>::getLoop(size_t loopId) const {
28  return _loops.getLoop(loopId);
29 }
30 
31 template<class Base>
32 inline size_t CodeHandler<Base>::addLoopDependentIndexPattern(IndexPattern& jacPattern) {
33  return _loops.addDependentIndexPattern(jacPattern);
34 }
35 
36 template<class Base>
37 inline void CodeHandler<Base>::manageLoopDependentIndexPattern(const IndexPattern* pattern) {
38  _loops.manageDependentIndexPattern(pattern);
39 }
40 
41 template<class Base>
42 inline size_t CodeHandler<Base>::addLoopIndependentIndexPattern(IndexPattern& pattern,
43  size_t hint) {
44  return _loops.addIndependentIndexPattern(pattern, hint);
45 }
46 
47 template<class Base>
48 inline void CodeHandler<Base>::LoopData::prepare4NewSourceGen() {
49  indexes.clear();
50  indexRandomPatterns.clear();
51  outerVars.clear();
52  depth = -1;
53  startEvalOrder.clear();
54  endNodes.clear();
55 
56  endNodes.reserve(loopModels.size());
57 }
58 
59 template<class Base>
60 inline void CodeHandler<Base>::LoopData::reset() {
61  loopModels.clear();
62  indexes.clear();
63  indexRandomPatterns.clear();
64  dependentIndexPatterns.clear();
65  independentIndexPatterns.clear();
66  endNodes.clear();
67 
68  for (const IndexPattern* itip : dependentIndexPatternManaged) {
69  delete itip;
70  }
71  dependentIndexPatternManaged.clear();
72 }
73 
74 template<class Base>
75 inline const std::string* CodeHandler<Base>::LoopData::getLoopName(size_t id) const {
76  typename std::map<size_t, LoopModel<Base>*>::const_iterator it;
77  it = loopModels.find(id);
78  if (it != loopModels.end())
79  return &(it->second->atomic_name());
80  else
81  return nullptr;
82 }
83 
84 template<class Base>
86  loopModels[loop.getLoopId()] = &loop;
87 }
88 
89 template<class Base>
91  typename std::map<size_t, LoopModel<Base>*>::const_iterator it = loopModels.find(loopId);
92  if (it != loopModels.end()) {
93  return it->second;
94  }
95 
96  return nullptr;
97 }
98 
99 template<class Base>
101  size_t size = dependentIndexPatterns.size();
102  if (dependentIndexPatterns.capacity() == size) {
103  dependentIndexPatterns.reserve((size * 3) / 2 + 1);
104  }
105  dependentIndexPatterns.push_back(&pattern);
106 
107  return size;
108 }
109 
110 template<class Base>
112  size_t sizeM = dependentIndexPatternManaged.size();
113  if (dependentIndexPatternManaged.capacity() == sizeM) {
114  dependentIndexPatternManaged.reserve((sizeM * 3) / 2 + 1);
115  }
116  dependentIndexPatternManaged.push_back(pattern);
117 }
118 
119 template<class Base>
121  size_t size = independentIndexPatterns.size();
122  if (hint < size && independentIndexPatterns[hint] == &pattern) {
123  return hint;
124  }
125  if (independentIndexPatterns.capacity() == size) {
126  independentIndexPatterns.reserve((size * 3) / 2 + 1);
127  }
128  independentIndexPatterns.push_back(&pattern);
129 
130  return size;
131 }
132 
133 template<class Base>
135  CPPADCG_ASSERT_UNKNOWN(node.getOperationType() == CGOpCode::LoopEnd);
136  LoopEndOperationNode<Base>& loopEnd = static_cast<LoopEndOperationNode<Base>&> (node);
137  endNodes.push_back(&loopEnd);
138 }
139 
140 } // END cg namespace
141 } // END CppAD namespace
142 
143 #endif
const std::string * getLoopName(size_t id) const
size_t getLoopId() const
Definition: loop_model.hpp:236
CGOpCode getOperationType() const