CppADCodeGen  2.3.0
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  *
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 
27 template<class Base>
29 protected:
33  std::string _libraryName;
37  const std::string* _customLibExtension;
41  std::map<std::string, std::string> _options;
42 public:
43 
53  const std::string& libraryName = "cppad_cg_model") :
54  ModelLibraryProcessor<Base>(modelLibGen),
55  _libraryName(libraryName),
56  _customLibExtension(nullptr) {
57  }
58 
59  inline const std::string& getLibraryName() const {
60  return _libraryName;
61  }
62 
63  inline void setLibraryName(const std::string& libraryName) {
64  CPPADCG_ASSERT_KNOWN(!libraryName.empty(), "Library name cannot be empty");
65 
66  _libraryName = libraryName;
67  }
68 
74  inline const std::string* getCustomLibraryExtension() const {
75  return _customLibExtension;
76  }
77 
83  inline void setCustomLibraryExtension(const std::string& libraryExtension) {
84  delete _customLibExtension;
85  _customLibExtension = new std::string(libraryExtension);
86  }
87 
92  delete _customLibExtension;
93  _customLibExtension = nullptr;
94  }
95 
99  inline std::map<std::string, std::string>& getOptions() {
100  return _options;
101  }
102 
106  inline const std::map<std::string, std::string>& getOptions() const {
107  return _options;
108  }
109 
118  std::unique_ptr<DynamicLib<Base>> createDynamicLibrary(CCompiler<Base>& compiler,
119  bool loadLib = true) {
120  // backup output format so that it can be restored
121  OStreamConfigRestore coutb(std::cout);
122 
123  this->modelLibraryHelper_->startingJob("", JobTimer::DYNAMIC_MODEL_LIBRARY);
124 
125  const std::map<std::string, ModelCSourceGen<Base>*>& models = this->modelLibraryHelper_->getModels();
126  try {
127  for (const auto& p : models) {
128  const std::map<std::string, std::string>& modelSources = this->getSources(*p.second);
129 
130  this->modelLibraryHelper_->startingJob("", JobTimer::COMPILING_FOR_MODEL);
131  compiler.compileSources(modelSources, true, this->modelLibraryHelper_);
132  this->modelLibraryHelper_->finishedJob();
133  }
134 
135  const std::map<std::string, std::string>& sources = this->getLibrarySources();
136  compiler.compileSources(sources, true, this->modelLibraryHelper_);
137 
138  const std::map<std::string, std::string>& customSource = this->modelLibraryHelper_->getCustomSources();
139  compiler.compileSources(customSource, true, this->modelLibraryHelper_);
140 
141  std::string libname = _libraryName;
142  if (_customLibExtension != nullptr)
143  libname += *_customLibExtension;
144  else
146 
147  compiler.buildDynamic(libname, this->modelLibraryHelper_);
148 
149  } catch (...) {
150  compiler.cleanup();
151  throw;
152  }
153  compiler.cleanup();
154 
155  this->modelLibraryHelper_->finishedJob();
156 
157  if (loadLib)
158  return loadDynamicLibrary();
159  else
160  return std::unique_ptr<DynamicLib<Base>>(nullptr);
161  }
162 
175  Archiver& ar,
176  bool posIndepCode) {
177  // backup output format so that it can be restored
178  OStreamConfigRestore coutb(std::cout);
179 
180  this->modelLibraryHelper_->startingJob("", JobTimer::STATIC_MODEL_LIBRARY);
181 
182  const std::map<std::string, ModelCSourceGen<Base>*>& models = this->modelLibraryHelper_->getModels();
183  try {
184  for (const auto& p : models) {
185  const std::map<std::string, std::string>& modelSources = this->getSources(*p.second);
186 
187  this->modelLibraryHelper_->startingJob("", JobTimer::COMPILING_FOR_MODEL);
188  compiler.compileSources(modelSources, posIndepCode, this->modelLibraryHelper_);
189  this->modelLibraryHelper_->finishedJob();
190  }
191 
192  const std::map<std::string, std::string>& sources = this->getLibrarySources();
193  compiler.compileSources(sources, posIndepCode, this->modelLibraryHelper_);
194 
195  const std::map<std::string, std::string>& customSource = this->modelLibraryHelper_->getCustomSources();
196  compiler.compileSources(customSource, posIndepCode, this->modelLibraryHelper_);
197 
198  std::string libname = _libraryName;
199  if (_customLibExtension != nullptr)
200  libname += *_customLibExtension;
201  else
203 
204  ar.create(libname, compiler.getObjectFiles(), this->modelLibraryHelper_);
205  } catch (...) {
206  compiler.cleanup();
207  throw;
208  }
209  compiler.cleanup();
210 
211  this->modelLibraryHelper_->finishedJob();
212  }
213 
214  virtual ~DynamicModelLibraryProcessor() {
215  delete _customLibExtension;
216  }
217 
218 protected:
219 
220  virtual std::unique_ptr<DynamicLib<Base>> loadDynamicLibrary();
221 
222 };
223 
224 } // END cg namespace
225 } // END CppAD namespace
226 
227 #endif
virtual void compileSources(const std::map< std::string, std::string > &sources, bool posIndepCode, JobTimer *timer=nullptr)=0
std::map< std::string, std::string > & getOptions()
DynamicModelLibraryProcessor(ModelLibraryCSourceGen< Base > &modelLibGen, const std::string &libraryName="cppad_cg_model")
virtual void buildDynamic(const std::string &library, JobTimer *timer=nullptr)=0
void setCustomLibraryExtension(const std::string &libraryExtension)
std::map< std::string, std::string > _options
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)