Documentation of CSL
librarygenerator.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef CSL_LIBRARY_GENERATOR
24 #define CSL_LIBRARY_GENERATOR
25 
26 #include "librarydependency.h"
27 #include "libraryfunction.h"
28 #include "librarygroup.h"
29 #include "abstract.h"
30 #include "literal.h"
31 #include <fstream>
32 #include <limits>
33 
34 namespace csl {
35 
36  // inline std::fstream lib_log("lib_debug_log.txt", std::ios::out);
37 
39 
40  public:
41 
42  struct file: public std::ofstream {
43  file(std::string const& name): std::ofstream(name) {}
44  ~file() { close(); }
45  };
46 
47  enum Compiler {
48  gcc,
49  clang,
50  };
51 
52  inline static int nDigits = std::numeric_limits<double>
53  ::max_digits10 - 2;
54  inline static std::string realType = "double";
55  inline static std::string complexType = "std::complex<double>";
56  inline static std::string realUsing = "real_t";
57  inline static std::string complexUsing = "complex_t";
58  inline static std::string tensorUsing = "csl::LibraryTensor";
59  inline static std::string indexType = "int";
60  inline static short nSpaceIndent = 4;
61  inline static const Expr imaginary = csl::constant_s("_i_");
62  // inline static std::string boostPath = "../../Boost";
63 
64  inline static std::string incDir = "include";
65  inline static std::string srcDir = "src";
66  inline static std::string objDir = "obj";
67  inline static std::string binDir = "bin";
68  inline static std::string libDir = "lib";
69  inline static std::string scriptDir = "script";
70  inline static std::string scriptObjDir = "script/obj";
71 
72  static inline std::string toUpper(std::string const& name) {
73  std::string copy(name);
74  for (char &c : copy)
75  c = toupper(c);
76  return copy;
77  }
78 
79  static std::string regularName(std::string_view name);
80 
81  static std::string indent(short nIndent = 1);
82 
83  static bool isQuadruplePrecision() { return quadruple; }
84  static void setQuadruplePrecision(bool quadruple);
85 
87  std::vector<std::string> mixings;
88  std::vector<std::string> masses;
89  std::vector<std::string> expressions;
90  std::vector<std::string> dependencies;
91  bool squaredMass;
92  };
93 
94  private:
95 
96  static inline bool quadruple = false;
97 
98  public:
99 
101  std::string const& t_name,
102  std::string const& t_path = "."
103  );
104 
105  ~LibraryGenerator() {}
106 
107  std::string getName() const;
108  std::string getPath() const;
109  std::set<std::string> const& getIPath() const;
110  std::set<std::string>& getIPath();
111  std::set<std::string> const& getLPath() const;
112  std::set<std::string>& getLPath();
113  LibDependency getDependencies() const;
114  std::shared_ptr<LibraryGroup> getGroup(std::string_view name) const;
115  bool hasGlobalFile() const {
116  return !diagData.empty() || !massExpressions.empty();
117  }
118  bool hasUniqueParamStruct() const {
119  return uniqueParamStruct;
120  }
121 
122  void cleanExistingSources() const;
123  void setClangCompiler() {
124  compiler = clang;
125  }
126  void setGccCompiler() {
127  compiler = gcc;
128  }
129  void setName(std::string const& t_name);
130  void setUniqueParamStruct(bool t_par) {
131  uniqueParamStruct = t_par;
132  }
133  void setPath(std::string const& t_path);
134  void setIPath(std::vector<std::string> const& t_path);
135  void setLPath(std::vector<std::string> const& t_path);
136  void addIPath(std::string const& t_path);
137  void addLPath(std::string const& t_path);
138  void addDependency(LibDependency const& dep);
139  void addInclude(std::string const& include,
140  bool global = false);
141  void importHeader(std::string const &filePath,
142  std::string const &fileName);
143  void importSource(std::string const &filePath,
144  std::string const &fileName);
145  void addLibrary(std::string const& library);
146 
147  void addTensor(Expr const& tensor) const;
148 
149  void addGroup(
150  std::string const &groupName,
151  bool complexReturn = true
152  );
153  LibFunction &addFunction(
154  std::string const &nameFunction,
155  Expr expression,
156  std::string const &nameGroup = "G"
157  );
158  void addMassExpression(std::string const &mass);
159  void addDiagonalization(
160  std::vector<std::string> const &mixing,
161  std::vector<std::string> const &masses,
162  std::vector<std::string> const &expressions,
163  std::vector<std::string> const &dependencies,
164  bool squaredMass
165  );
166 
167  void print() const;
168  void setupDirectory() const;
169  void printHeader() const;
170  void printGlobal() const;
171  void printSource() const;
172  void printCallable() const;
173  void printTest() const;
174 
175  void printDiagonalizationFacility(
176  std::ostream &header,
177  std::ostream &source
178  ) const;
179 
180  void printMassExpressionsFacility(
181  std::ostream &header,
182  std::ostream &source
183  ) const;
184 
185  void printMakefile() const;
186  void printPythonDir() const;
187  // void printCMakeLists() const;
188  // void printGenerator() const;
189  // void printPythonTest() const;
190 
191  void build(unsigned int nJobs = 1) const;
192 
193  void buildCppLib(unsigned int nJobs) const;
194  // void buildPythonLib() const;
195 
196  private:
197 
198  void updateDiagonalization() const;
199 
200  bool needLibraryTensor() const;
201 
202  void printFunction(LibFunction const& f) const;
203  void printGroup(LibraryGroup const& g) const;
204 
205  void printTensors(std::ostream& out) const;
206 
207  std::vector<csl::Tensor> gatherTensors() const;
208 
209  std::vector<std::string> getDiagonalizationMasses() const;
210  std::vector<std::string> getDiagonalizationMixings() const;
211  std::vector<std::string> getDiagonalizationDependencies() const;
212 
213  std::string regLibName() const;
214  std::string getFunctionFileName(LibFunction const &f) const;
215  std::string getGroupFileName(LibraryGroup const &g) const;
216  // std::string getHeaders() const;
217  // std::string getSources() const;
218 
219  protected:
220 
221  std::string name;
222 
223  Compiler compiler;
224 
225  std::string path;
226 
227  std::set<std::string> IPath;
228 
229  std::set<std::string> LPath;
230 
231  LibDependency dependencies;
232 
233  mutable
234  std::vector<csl::Tensor> tensors;
235 
236  mutable
237  std::vector<std::shared_ptr<LibraryGroup>> groups;
238 
239  mutable
240  std::vector<DiagonalizationData> diagData;
241 
242  mutable
243  std::vector<std::string> massExpressions;
244 
245  bool uniqueParamStruct;
246  };
247 
248  std::vector<LibParameter> inputParams(
249  std::vector<LibraryGenerator::DiagonalizationData> const &diagData
250  );
251 
252  std::vector<LibParameter> outputParams(
253  std::vector<LibraryGenerator::DiagonalizationData> const &diagData,
254  std::vector<std::string> const &massExpressions
255  );
256 
257 } // End of namespace csl
258 
259 #endif
Definition: librarygenerator.h:86
Namespace for csl library.
Definition: abreviation.h:34
Definition: librarygenerator.h:38
Definition: librarygenerator.h:42
Definition: libraryfunction.h:45
Definition: diagonalization.h:34
Definition: librarydependency.h:33
Definition: librarygroup.h:23
Base classes for all exprs in the program.
Expression type/.
Definition: abstract.h:1573