CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
dynamic_library_processor.hpp
1 #ifndef CPPAD_CG_DYNAMIC_LIBRARY_PROCESSOR_INCLUDED
2 #define CPPAD_CG_DYNAMIC_LIBRARY_PROCESSOR_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2013 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 
28 template<class Base>
30 protected:
34  std::string _libraryName;
38  std::unique_ptr<const std::string> _customLibExtension;
42  std::map<std::string, std::string> _options;
43 public:
44 
54  std::string libraryName = "cppad_cg_model") :
55  ModelLibraryProcessor<Base>(modelLibGen),
56  _libraryName(std::move(libraryName)) {
57  }
58 
59  virtual ~DynamicModelLibraryProcessor() = default;
60 
64  inline const std::string& getLibraryName() const {
65  return _libraryName;
66  }
67 
72  inline void setLibraryName(const std::string& libraryName) {
73  CPPADCG_ASSERT_KNOWN(!libraryName.empty(), "Library name cannot be empty")
74 
75  _libraryName = libraryName;
76  }
77 
83  inline const std::string* getCustomLibraryExtension() const {
84  return _customLibExtension.get();
85  }
86 
92  inline void setCustomLibraryExtension(const std::string& libraryExtension) {
93  _customLibExtension.reset(new std::string(libraryExtension));
94  }
95 
100  _customLibExtension.reset();
101  }
102 
106  inline std::map<std::string, std::string>& getOptions() {
107  return _options;
108  }
109 
113  inline const std::map<std::string, std::string>& getOptions() const {
114  return _options;
115  }
116 
125  std::unique_ptr<DynamicLib<Base>> createDynamicLibrary(CCompiler<Base>& compiler,
126  bool loadLib = true) {
127  // backup output format so that it can be restored
128  OStreamConfigRestore coutb(std::cout);
129 
130  this->modelLibraryHelper_->startingJob("", JobTimer::DYNAMIC_MODEL_LIBRARY);
131 
132  const std::map<std::string, ModelCSourceGen < Base>*>&models = this->modelLibraryHelper_->getModels();
133  try {
134  for (const auto& p : models) {
135  const std::map<std::string, std::string>& modelSources = this->getSources(*p.second);
136 
137  this->modelLibraryHelper_->startingJob("", JobTimer::COMPILING_FOR_MODEL);
138  compiler.compileSources(modelSources, true, this->modelLibraryHelper_);
139  this->modelLibraryHelper_->finishedJob();
140  }
141 
142  const std::map<std::string, std::string>& sources = this->getLibrarySources();
143  compiler.compileSources(sources, true, this->modelLibraryHelper_);
144 
145  const std::map<std::string, std::string>& customSource = this->modelLibraryHelper_->getCustomSources();
146  compiler.compileSources(customSource, true, this->modelLibraryHelper_);
147 
148  std::string libname = _libraryName;
149  if (_customLibExtension != nullptr)
150  libname += *_customLibExtension;
151  else
153 
154  compiler.buildDynamic(libname, this->modelLibraryHelper_);
155 
156  } catch (...) {
157  compiler.cleanup();
158  throw;
159  }
160  compiler.cleanup();
161 
162  this->modelLibraryHelper_->finishedJob();
163 
164  if (loadLib)
165  return loadDynamicLibrary();
166  else
167  return std::unique_ptr<DynamicLib<Base>> (nullptr);
168  }
169 
182  Archiver& ar,
183  bool posIndepCode) {
184  // backup output format so that it can be restored
185  OStreamConfigRestore coutb(std::cout);
186 
187  this->modelLibraryHelper_->startingJob("", JobTimer::STATIC_MODEL_LIBRARY);
188 
189  const std::map<std::string, ModelCSourceGen<Base>*>& models = this->modelLibraryHelper_->getModels();
190  try {
191  for (const auto& p : models) {
192  const std::map<std::string, std::string>& modelSources = this->getSources(*p.second);
193 
194  this->modelLibraryHelper_->startingJob("", JobTimer::COMPILING_FOR_MODEL);
195  compiler.compileSources(modelSources, posIndepCode, this->modelLibraryHelper_);
196  this->modelLibraryHelper_->finishedJob();
197  }
198 
199  const std::map<std::string, std::string>& sources = this->getLibrarySources();
200  compiler.compileSources(sources, posIndepCode, this->modelLibraryHelper_);
201 
202  const std::map<std::string, std::string>& customSource = this->modelLibraryHelper_->getCustomSources();
203  compiler.compileSources(customSource, posIndepCode, this->modelLibraryHelper_);
204 
205  std::string libname = _libraryName;
206  if (_customLibExtension != nullptr)
207  libname += *_customLibExtension;
208  else
210 
211  ar.create(libname, compiler.getObjectFiles(), this->modelLibraryHelper_);
212  } catch (...) {
213  compiler.cleanup();
214  throw;
215  }
216  compiler.cleanup();
217 
218  this->modelLibraryHelper_->finishedJob();
219  }
220 
221 
222 protected:
223 
224  virtual std::unique_ptr<DynamicLib<Base>> loadDynamicLibrary();
225 
226 };
227 
228 } // END cg namespace
229 } // END CppAD namespace
230 
231 #endif
DynamicModelLibraryProcessor(ModelLibraryCSourceGen< Base > &modelLibGen, std::string libraryName="cppad_cg_model")
virtual void compileSources(const std::map< std::string, std::string > &sources, bool posIndepCode, JobTimer *timer=nullptr)=0
STL namespace.
std::map< std::string, std::string > & getOptions()
virtual void buildDynamic(const std::string &library, JobTimer *timer=nullptr)=0
std::unique_ptr< const std::string > _customLibExtension
void setCustomLibraryExtension(const std::string &libraryExtension)
std::map< std::string, std::string > _options
void setLibraryName(const std::string &libraryName)
std::unique_ptr< DynamicLib< Base > > createDynamicLibrary(CCompiler< Base > &compiler, bool loadLib=true)
const std::map< std::string, std::string > & getOptions() const
virtual void cleanup()=0
void createStaticLibrary(CCompiler< Base > &compiler, Archiver &ar, bool posIndepCode)