CppADCodeGen  HEAD
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  * Copyright (C) 2020 Joao Leal
7  *
8  * CppADCodeGen is distributed under multiple licenses:
9  *
10  * - Eclipse Public License Version 1.0 (EPL1), and
11  * - GNU General Public License Version 3 (GPL3).
12  *
13  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15  * ----------------------------------------------------------------------------
16  * Author: Joao Leal
17  */
18 
19 namespace CppAD {
20 namespace cg {
21 
27 template<class Base>
28 class ModelLibraryCSourceGen : public JobTimer {
29 public:
30  static const std::string FUNCTION_VERSION;
31  static const std::string FUNCTION_MODELS;
32  static const std::string FUNCTION_ONCLOSE;
33  static const std::string FUNCTION_SETTHREADPOOLDISABLED;
34  static const std::string FUNCTION_ISTHREADPOOLDISABLED;
35  static const std::string FUNCTION_SETTHREADS;
36  static const std::string FUNCTION_GETTHREADS;
37  static const std::string FUNCTION_SETTHREADSCHEDULERSTRAT;
38  static const std::string FUNCTION_GETTHREADSCHEDULERSTRAT;
39  static const std::string FUNCTION_SETTHREADPOOLVERBOSE;
40  static const std::string FUNCTION_ISTHREADPOOLVERBOSE;
41  static const std::string FUNCTION_SETTHREADPOOLGUIDEDMAXGROUPWORK;
42  static const std::string FUNCTION_GETTHREADPOOLGUIDEDMAXGROUPWORK;
43  static const std::string FUNCTION_SETTHREADPOOLNUMBEROFTIMEMEAS;
44  static const std::string FUNCTION_GETTHREADPOOLNUMBEROFTIMEMEAS;
45  static const unsigned long API_VERSION;
46 protected:
47  static const std::string CONST;
48 protected:
52  std::map<std::string, ModelCSourceGen<Base>*> _models;
56  std::map<std::string, std::string> _customSource;
60  std::map<std::string, std::string> _libSources;
66  MultiThreadingType _multiThreading;
70  std::ostringstream _cache;
71 public:
72 
81  _multiThreading(MultiThreadingType::NONE) {
82  CPPADCG_ASSERT_KNOWN(_models.find(model.getName()) == _models.end(),
83  "Another model with the same name was already registered")
84 
85  _models[model.getName()] = &model; // must not use initializer_list constructor of map!
86  }
87 
88  template<class... Ms>
89  inline ModelLibraryCSourceGen(ModelCSourceGen<Base>& headModel, Ms&... rest) :
90  ModelLibraryCSourceGen(rest...) {
91  CPPADCG_ASSERT_KNOWN(_models.find(headModel.getName()) == _models.end(),
92  "Another model with the same name was already registered")
93 
94  _models[headModel.getName()] = &headModel;
95  }
96 
97  ModelLibraryCSourceGen(const ModelLibraryCSourceGen&) = delete;
98  ModelLibraryCSourceGen& operator=(const ModelLibraryCSourceGen&) = delete;
99 
100  virtual ~ModelLibraryCSourceGen() = default;
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