sisi4s
Loading...
Searching...
No Matches
Algorithm.hpp
Go to the documentation of this file.
1#ifndef ALGORITHM_DEFINED
2#define ALGORITHM_DEFINED
3
4#include <Data.hpp>
5#include <string>
6#include <vector>
7#include <sstream>
8#include <util/Tensor.hpp>
9
10namespace sisi4s {
11class Argument {
12public:
13 Argument(std::string const &name_)
14 : name(name_)
15 , data(name_) {}
16 Argument(std::string const &name_, std::string const &data_)
17 : name(name_)
18 , data(data_) {}
19 std::string const &getName() const { return name; }
20 std::string const &getData() const { return data; }
21
22protected:
23 std::string name, data;
24};
25
26class Algorithm {
27public:
28 Algorithm(std::vector<Argument> const &argumentList);
29 virtual ~Algorithm();
30 virtual std::string getName() = 0;
31 virtual void run() = 0;
32 virtual void dryRun();
33
34 std::string note;
35 bool fallible = false;
36
37 bool isArgumentGiven(std::string const &argumentName);
38 // retrieving input arguments
39 std::string getTextArgument(std::string const &argumentName);
40 std::string getTextArgument(std::string const &argumentName,
41 std::string const &defaultValue);
42 bool getBooleanArgument(std::string const &name);
43 bool getBooleanArgument(std::string const &name, bool const &defaultValue);
44 int64_t getIntegerArgument(std::string const &argumentName);
45 int64_t getIntegerArgument(std::string const &argumentName,
46 int64_t const defaultValue);
47 real getRealArgument(std::string const &argumentName);
48 real getRealArgument(std::string const &argumentName,
49 real const defaultValue);
50 template <typename F = real, typename T = Tensor<F>>
51 T *getTensorArgument(std::string const &argumentName);
52 template <typename F = real, typename C = std::vector<F>>
53 C *getContainerArgument(std::string const &argumentName);
54 template <typename F = real, typename C = std::vector<F>>
55 void allocateContainerArgument(std::string const &argumentName, C *container);
56
57 // Get all arguments given by the user in the input file
58 std::vector<std::string> getGivenArgumentNames() const {
59 std::vector<std::string> names;
60 for (const auto &p : arguments) { names.push_back(p.first); }
61 return names;
62 }
63
64 // Check that all the arguments given by the user
65 // conform to the args that you're expecting in the algorithm
66 void checkArgumentsOrDie(const std::vector<std::string> args) const {
67 const std::vector<std::string> actualArguments(getGivenArgumentNames());
68 std::stringstream s;
69 for (const auto &p : actualArguments) {
70 if (std::count(args.begin(), args.end(), p) != 1) {
71 s << "Error: parameter (" << p << ") unknown";
72 throw new EXCEPTION(s.str());
73 }
74 }
75 }
76
77 // typing, allocating and setting output arguments
89 template <typename F = real, typename T = Tensor<F>>
90 void allocatedTensorArgument(std::string const &argumentName, T *tensor);
91 void setRealArgument(std::string const &argumentName, real const value);
92 void setIntegerArgument(std::string const &argumentName, int const value);
93
94 // type promotions:
97 template <typename F = real, typename T = Tensor<F>>
99
100 Data *getArgumentData(std::string const &argumentName);
101 std::map<std::string, std::string> arguments;
102};
103
105public:
107 std::map<std::string,
108 std::function<Algorithm *(std::vector<Argument> const &)>>;
109
117 static Algorithm *create(std::string const &name,
118 std::vector<Argument> const &arguments) {
119 auto iterator(getAlgorithmMap()->find(name));
120 return iterator != getAlgorithmMap()->end() ? iterator->second(arguments)
121 : nullptr;
122 }
123
124 static std::vector<std::string> getAlgorithmNames() {
125 std::vector<std::string> result;
126 for (auto const &kv : *algorithmMap) result.push_back(kv.first);
127 return result;
128 }
129
130protected:
133 }
135};
136
140template <typename AlgorithmType>
141Algorithm *createAlgorithm(std::vector<Argument> const &arguments) {
142 return new AlgorithmType(arguments);
143}
144
150template <typename AlgorithmType>
152public:
158 AlgorithmRegistrar(std::string const &name) {
159 (*getAlgorithmMap())[name] = &createAlgorithm<AlgorithmType>;
160 }
161};
162
169#define ALGORITHM_REGISTRAR_DECLARATION(NAME) \
170 virtual std::string getName() { return #NAME; } \
171 static sisi4s::AlgorithmRegistrar<NAME> registrar_
178#define ALGORITHM_REGISTRAR_DEFINITION(NAME) \
179 sisi4s::AlgorithmRegistrar<NAME> NAME::registrar_(#NAME)
180
181#define IMPLEMENT_ALGORITHM(NAME) \
182 ALGORITHM_REGISTRAR_DEFINITION(NAME); \
183 void NAME::run()
184
185#define DEFINE_ALGORITHM_HEADER(NAME, ...) \
186 class NAME : public Algorithm { \
187 public: \
188 ALGORITHM_REGISTRAR_DECLARATION(NAME); \
189 NAME(std::vector<Argument> const &argumentList) \
190 : Algorithm(argumentList) {} \
191 ~NAME() {} \
192 virtual void run(); \
193 __VA_ARGS__ \
194 }
195
196} // namespace sisi4s
197
198#endif
#define EXCEPTION(message)
Definition Exception.hpp:8
Definition Algorithm.hpp:104
static Algorithm * create(std::string const &name, std::vector< Argument > const &arguments)
Creates an algorithm object of the algorithm type specified by the given name. The given arguments ar...
Definition Algorithm.hpp:117
static AlgorithmMap * algorithmMap
Definition Algorithm.hpp:134
static std::vector< std::string > getAlgorithmNames()
Definition Algorithm.hpp:124
static AlgorithmMap * getAlgorithmMap()
Definition Algorithm.hpp:131
std::map< std::string, std::function< Algorithm *(std::vector< Argument > const &)> > AlgorithmMap
Definition Algorithm.hpp:108
Class to be statically instantiated by an algorithm to register it in the AlgorithmFactory....
Definition Algorithm.hpp:151
AlgorithmRegistrar(std::string const &name)
Constructs the registrating instance. The algorithm type must be given as template argument,...
Definition Algorithm.hpp:158
Definition Algorithm.hpp:26
virtual void dryRun()
The dryRun estimates resource consumption, especially memory and processor time.
Definition Algorithm.cxx:29
real getRealArgumentFromInteger(IntegerData *data)
Definition Algorithm.cxx:138
Data * getArgumentData(std::string const &argumentName)
Definition Algorithm.cxx:37
bool fallible
Definition Algorithm.hpp:35
T * getTensorArgument(std::string const &argumentName)
Definition Algorithm.cxx:190
real getRealArgument(std::string const &argumentName)
Definition Algorithm.cxx:121
void allocatedTensorArgument(std::string const &argumentName, T *tensor)
Specifies the location of an output tensor data.
Definition Algorithm.cxx:289
C * getContainerArgument(std::string const &argumentName)
Definition Algorithm.cxx:157
int64_t getIntegerArgument(std::string const &argumentName)
Definition Algorithm.cxx:100
std::string getTextArgument(std::string const &argumentName)
Definition Algorithm.cxx:57
bool getBooleanArgument(std::string const &name)
Definition Algorithm.cxx:77
real getRealArgumentFromTensor(TensorData< real > *data)
Definition Algorithm.cxx:147
void checkArgumentsOrDie(const std::vector< std::string > args) const
Definition Algorithm.hpp:66
T * getTensorArgumentFromReal(RealData *realData)
Converts the given real data into a scalar tensor.
Definition Algorithm.cxx:271
virtual void run()=0
std::string note
Definition Algorithm.hpp:34
std::vector< std::string > getGivenArgumentNames() const
Definition Algorithm.hpp:58
virtual std::string getName()=0
virtual ~Algorithm()
Definition Algorithm.cxx:23
void setIntegerArgument(std::string const &argumentName, int const value)
Definition Algorithm.cxx:369
bool isArgumentGiven(std::string const &argumentName)
Definition Algorithm.cxx:33
void allocateContainerArgument(std::string const &argumentName, C *container)
Definition Algorithm.cxx:170
void setRealArgument(std::string const &argumentName, real const value)
Definition Algorithm.cxx:364
std::map< std::string, std::string > arguments
Definition Algorithm.hpp:101
Definition Algorithm.hpp:11
std::string name
Definition Algorithm.hpp:23
Argument(std::string const &name_)
Definition Algorithm.hpp:13
std::string const & getData() const
Definition Algorithm.hpp:20
Argument(std::string const &name_, std::string const &data_)
Definition Algorithm.hpp:16
std::string data
Definition Algorithm.hpp:23
std::string const & getName() const
Definition Algorithm.hpp:19
Definition Data.hpp:34
Definition Data.hpp:150
Definition Data.hpp:139
Definition Data.hpp:176
Definition Algorithm.hpp:10
Algorithm * createAlgorithm(std::vector< Argument > const &arguments)
template function creating an instance of the given class.
Definition Algorithm.hpp:141