CppADCodeGen  2.3.0
A C++ Algorithmic Differentiation Package with Source Code Generation
model_library_c_source_gen.hpp
1 #ifndef CPPAD_CG_MODEL_LIBRARY_C_SOURCE_GEN_INCLUDED
2 #define CPPAD_CG_MODEL_LIBRARY_C_SOURCE_GEN_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2012 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 
26 template<class Base>
27 class ModelLibraryCSourceGen : public JobTimer {
28 public:
29  static const std::string FUNCTION_VERSION;
30  static const std::string FUNCTION_MODELS;
31  static const std::string FUNCTION_ONCLOSE;
32  static const std::string FUNCTION_SETTHREADPOOLDISABLED;
33  static const std::string FUNCTION_ISTHREADPOOLDISABLED;
34  static const std::string FUNCTION_SETTHREADS;
35  static const std::string FUNCTION_GETTHREADS;
36  static const std::string FUNCTION_SETTHREADSCHEDULERSTRAT;
37  static const std::string FUNCTION_GETTHREADSCHEDULERSTRAT;
38  static const std::string FUNCTION_SETTHREADPOOLVERBOSE;
39  static const std::string FUNCTION_ISTHREADPOOLVERBOSE;
40  static const std::string FUNCTION_SETTHREADPOOLGUIDEDMAXGROUPWORK;
41  static const std::string FUNCTION_GETTHREADPOOLGUIDEDMAXGROUPWORK;
42  static const std::string FUNCTION_SETTHREADPOOLNUMBEROFTIMEMEAS;
43  static const std::string FUNCTION_GETTHREADPOOLNUMBEROFTIMEMEAS;
44  static const unsigned long API_VERSION;
45 protected:
46  static const std::string CONST;
47 protected:
51  std::map<std::string, ModelCSourceGen<Base>*> _models;
55  std::map<std::string, std::string> _customSource;
59  std::map<std::string, std::string> _libSources;
65  MultiThreadingType _multiThreading;
69  std::ostringstream _cache;
70 public:
71 
80  _multiThreading(MultiThreadingType::NONE) {
81  CPPADCG_ASSERT_KNOWN(_models.find(model.getName()) == _models.end(),
82  "Another model with the same name was already registered");
83 
84  _models[model.getName()] = &model; // must not use initializer_list constructor of map!
85  }
86 
87  template<class... Ms>
88  inline ModelLibraryCSourceGen(ModelCSourceGen<Base>& headModel, Ms&... rest) :
89  ModelLibraryCSourceGen(rest...) {
90  CPPADCG_ASSERT_KNOWN(_models.find(headModel.getName()) == _models.end(),
91  "Another model with the same name was already registered");
92 
93  _models[headModel.getName()] = &headModel;
94  }
95 
96  ModelLibraryCSourceGen(const ModelLibraryCSourceGen&) = delete;
97  ModelLibraryCSourceGen& operator=(const ModelLibraryCSourceGen&) = delete;
98 
99  virtual ~ModelLibraryCSourceGen() {
100  }
101 
108  inline void addModel(ModelCSourceGen<Base>& model) {
109  CPPADCG_ASSERT_KNOWN(_models.find(model.getName()) == _models.end(),
110  "Another model with the same name was already registered");
111 
112  _models[model.getName()] = &model;
113 
114  _libSources.clear(); // must regenerate library sources again
115  }
116 
117  inline const std::map<std::string, ModelCSourceGen<Base>*>& getModels() const {
118  return _models;
119  }
120 
121  void addCustomFunctionSource(const std::string& filename, const std::string& source) {
122  CPPADCG_ASSERT_KNOWN(!filename.empty(), "The filename name cannot be empty");
123 
124  _customSource[filename] = source;
125  }
126 
133  inline const std::map<std::string, std::string>& getCustomSources() const {
134  return _customSource;
135  }
136 
144  inline MultiThreadingType getMultiThreading() const {
145  return _multiThreading;
146  }
147 
157  inline void setMultiThreading(MultiThreadingType multiThreading) {
158  _multiThreading = multiThreading;
159  }
160 
168  void saveSources(const std::string& sourcesFolder);
169 
178  virtual const std::map<std::string, std::string>& getLibrarySources();
179 protected:
180 
181  virtual void generateVersionSource(std::map<std::string, std::string>& sources);
182 
183  virtual void generateModelsSource(std::map<std::string, std::string>& sources);
184 
185  virtual void generateOnCloseSource(std::map<std::string, std::string>& sources);
186 
187  virtual void generateThreadPoolSources(std::map<std::string, std::string>& sources);
188 
189  static void saveSources(const std::string& sourcesFolder,
190  const std::map<std::string, std::string>& sources);
191 
192  friend class ModelLibraryProcessor<Base>;
193 };
194 
195 } // END cg namespace
196 } // END CppAD namespace
197 
198 #endif
virtual const std::map< std::string, std::string > & getLibrarySources()
void saveSources(const std::string &sourcesFolder)
std::map< std::string, ModelCSourceGen< Base > * > _models
ModelLibraryCSourceGen(ModelCSourceGen< Base > &model)
void setMultiThreading(MultiThreadingType multiThreading)
std::map< std::string, std::string > _libSources
const std::string & getName() const
const std::map< std::string, std::string > & getCustomSources() const
void addModel(ModelCSourceGen< Base > &model)
std::map< std::string, std::string > _customSource