AutoPas  3.0.0
Loading...
Searching...
No Matches
RuleBasedProgramTree.h
1#pragma once
2
3#include "RuleVM.h"
4
5namespace autopas {
6
10enum class Type { BOOL, DOUBLE, SIZE_T, CONTAINER, TRAVERSAL, LOAD_ESTIMATOR, DATA_LAYOUT, NEWTON3, CELL_SIZE_FACTOR };
11
21 std::set<ContainerOption> _containers;
25 std::set<TraversalOption> _traversals;
29 std::set<LoadEstimatorOption> _loadEstimators;
33 std::set<DataLayoutOption> _dataLayouts;
37 std::set<Newton3Option> _newton3Options;
41 std::set<double> _cellSizeFactors;
42
47
52 ConfigurationPattern(const ConfigurationPattern &configurationPattern);
53
58
65 void add(const RuleVM::MemoryCell &value) {
66 std::visit(
67 [&](const auto &val) {
69 },
70 value);
71 }
72
77 [[nodiscard]] bool matches(const Configuration &configuration) const {
78 return (_containers.empty() or contains(_containers, configuration.container)) and
79 (_traversals.empty() or contains(_traversals, configuration.traversal)) and
80 (_loadEstimators.empty() or contains(_loadEstimators, configuration.loadEstimator)) and
81 (_dataLayouts.empty() or contains(_dataLayouts, configuration.dataLayout)) and
82 (_newton3Options.empty() or contains(_newton3Options, configuration.newton3)) and
83 (_cellSizeFactors.empty() or contains(_cellSizeFactors, configuration.cellSizeFactor));
84 }
85
89 [[nodiscard]] std::string toString() const {
90 std::string res = optionSetToString(_containers) + "; " + optionSetToString(_traversals) + "; " +
91 optionSetToString(_loadEstimators) + "; " + optionSetToString(_loadEstimators) + "; " +
92 optionSetToString(_dataLayouts) + "; " + optionSetToString(_newton3Options) + "; ";
93 if (not _cellSizeFactors.empty()) {
94 res += std::accumulate(std::next(_cellSizeFactors.begin()), _cellSizeFactors.end(),
95 std::to_string(*_cellSizeFactors.begin()),
96 [](std::string s, double d) { return std::move(s) + ',' + std::to_string(d); });
97 }
98 return res;
99 }
100
101 private:
102 template <class T>
103 [[nodiscard]] static std::string optionSetToString(const std::set<T> &set) {
104 if (not set.empty()) {
105 auto comma_fold = [](std::string a, T b) { return std::move(a) + ',' + b.to_string(); };
106
107 return std::accumulate(std::next(set.begin()), set.end(), set.begin()->to_string(), comma_fold);
108 }
109 return {};
110 }
111
112 template <typename T>
113 [[nodiscard]] static bool contains(const std::set<T> &set, const T &option) {
114 return set.find(option) != set.end();
115 }
116
117 template <class T, class SetT>
118 static void addHelper2(T value, SetT &set) {
119 if constexpr (std::is_same_v<T, typename SetT::value_type>) {
120 set.insert(value);
121 }
122 }
123
124 template <class T, class... SetT>
125 static void addHelper(T value, SetT &...sets) {
126 (addHelper2(value, sets), ...);
127 }
128};
129
133namespace RuleSyntax {
134
135class CodeGenerationContext;
136
137struct Define;
138
142struct Statement {
146 virtual ~Statement() = default;
152 virtual void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const = 0;
153};
154
161inline Type typeOf(const RuleVM::MemoryCell &memoryCell) { return static_cast<Type>(memoryCell.index()); }
162
170 virtual ~Expression() = default;
174 [[nodiscard]] virtual Type getType() const = 0;
181 virtual void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const = 0;
182};
183
184struct DefineList;
185
202
206 enum class SameProperty { container, traversal, dataLayout, newton3, loadEstimator, cellSizeFactor };
207
211 std::vector<SameProperty> sameProperties;
212
218
222 ~ConfigurationOrder() override = default;
229 ConfigurationOrder(ConfigurationPattern greater, ConfigurationPattern smaller, std::vector<SameProperty> same = {});
230
231 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
232
236 [[nodiscard]] std::string toString() const;
237
244 [[nodiscard]] bool haveEqualSameProperties(const Configuration &conf1, const Configuration &conf2) const;
245
246 private:
247 static std::string samePropertyToString(SameProperty same);
248};
249
256 public:
261 explicit CodeGenerationContext(std::map<std::string, std::pair<const Define *, size_t>> initialAddressEnvironment);
262
267
272 void allocateStack(size_t num);
273
278 void freeStack(size_t num);
279
284 void addLocalVariable(const Define &definition);
285
290 void addGlobalVariable(const Define &definition);
291
296 [[nodiscard]] size_t addressOf(const std::string &name) const;
297
302 [[nodiscard]] const Define *definitionOf(const std::string &name) const;
303
307 [[nodiscard]] auto getMaxStackSize() const { return maxNeededStack; }
308
314 void addList(const std::string &name, const DefineList *list);
315
320 const DefineList *getList(const std::string &name);
321
327 size_t addConfigurationOrder(const ConfigurationOrder &configurationOrder);
328
332 [[nodiscard]] const std::vector<ConfigurationOrder> &getConfigurationOrders() const;
333
338 [[nodiscard]] ConfigurationPattern smallerConfigurationPatternByIndex(size_t idx) const;
339
343 [[nodiscard]] size_t getNumLocalVariables() const;
344
345 private:
346 size_t currentNeededStack;
347 size_t maxNeededStack;
348
349 std::map<std::string, std::pair<const Define *, size_t>> addressEnvironment;
350 size_t initialNumVariables;
351 std::map<std::string, const DefineList *> lists;
352 std::vector<ConfigurationOrder> configurationOrders;
353};
354
358struct Literal : public Expression {
367
373
377 [[nodiscard]] Type getType() const override;
378
379 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
380};
381
385struct DefineList : public Statement {
389 std::string listName;
393 std::vector<Literal> values;
394
400 DefineList(std::string listName, std::vector<Literal> values);
401
402 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
403};
404
408struct Variable : public Expression {
413
419
420 [[nodiscard]] Type getType() const override;
421
422 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
423};
424
428struct UnaryOperator : public Expression {
432 enum Operator { NOT };
433
437 std::shared_ptr<Expression> child;
442
448 UnaryOperator(Operator op, std::shared_ptr<Expression> child);
449
450 [[nodiscard]] Type getType() const override;
451
452 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
453};
454
458struct BinaryOperator : public Expression {
462 enum Operator { LESS, GREATER, AND, OR, ADD, SUB, MUL, DIV };
463
467 std::shared_ptr<Expression> left;
475 std::shared_ptr<Expression> right;
476
483 BinaryOperator(Operator op, std::shared_ptr<Expression> left, std::shared_ptr<Expression> right);
484
485 [[nodiscard]] Type getType() const override;
486
487 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
488};
489
493struct Define : public Statement {
497 std::string variable;
501 std::shared_ptr<Expression> value;
502
508 Define(std::string variable, std::shared_ptr<Expression> value);
509
513 ~Define() override;
514
515 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
516};
517
521struct If : public Statement {
525 std::shared_ptr<Expression> condition;
529 std::vector<std::shared_ptr<Statement>> consequences;
530
536 If(std::shared_ptr<Expression> condition, std::vector<std::shared_ptr<Statement>> consequences);
537
538 void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override;
539};
540
548 std::vector<std::shared_ptr<Statement>> statements;
549
555 [[nodiscard]] RuleVM::Program generateCode(CodeGenerationContext &context) const;
556};
557
558} // namespace RuleSyntax
559} // namespace autopas
Class containing multiple options that form an algorithm configuration for the pairwise iteration.
Definition: Configuration.h:24
LoadEstimatorOption loadEstimator
Load Estimator option.
Definition: Configuration.h:126
TraversalOption traversal
Traversal option.
Definition: Configuration.h:122
double cellSizeFactor
CellSizeFactor.
Definition: Configuration.h:138
DataLayoutOption dataLayout
Data Layout option.
Definition: Configuration.h:130
ContainerOption container
Container option.
Definition: Configuration.h:118
Newton3Option newton3
Newton 3 option.
Definition: Configuration.h:134
A CodeGenerationContext keeps track of the needed stack size of the program, the mapping from variabl...
Definition: RuleBasedProgramTree.h:255
void addGlobalVariable(const Define &definition)
Adds a global variable definition that can be used in the program later.
Definition: RuleBasedProgramTree.cpp:126
void allocateStack(size_t num)
Allocates the given number of MemoryCells on the stack.
Definition: RuleBasedProgramTree.cpp:139
void freeStack(size_t num)
Frees the given number of MemoryCells on the stack.
Definition: RuleBasedProgramTree.cpp:144
size_t addConfigurationOrder(const ConfigurationOrder &configurationOrder)
Adds a configuration order and assigns it an index that can be used to refer to it later.
Definition: RuleBasedProgramTree.cpp:150
const std::vector< ConfigurationOrder > & getConfigurationOrders() const
Definition: RuleBasedProgramTree.cpp:155
ConfigurationPattern smallerConfigurationPatternByIndex(size_t idx) const
Definition: RuleBasedProgramTree.cpp:159
const DefineList * getList(const std::string &name)
Definition: RuleBasedProgramTree.cpp:148
const Define * definitionOf(const std::string &name) const
Definition: RuleBasedProgramTree.cpp:135
void addLocalVariable(const Define &definition)
Adds a local variable definition that can be used in the program later.
Definition: RuleBasedProgramTree.cpp:122
void addList(const std::string &name, const DefineList *list)
Adds a list definition that can be used later in the program.
Definition: RuleBasedProgramTree.cpp:146
size_t getNumLocalVariables() const
Definition: RuleBasedProgramTree.cpp:163
auto getMaxStackSize() const
Definition: RuleBasedProgramTree.h:307
size_t addressOf(const std::string &name) const
Definition: RuleBasedProgramTree.cpp:131
A VM that is capable of executing a program with simple instructions on a stack of MemoryCells.
Definition: RuleVM.h:18
std::variant< bool, double, size_t, ContainerOption, TraversalOption, LoadEstimatorOption, DataLayoutOption, Newton3Option > MemoryCell
The type of a memory cell in the stack the VM operates on.
Definition: RuleVM.h:24
Type typeOf(const RuleVM::MemoryCell &memoryCell)
Converts the type of a RuleVM memory cell to the type in the AST.
Definition: RuleBasedProgramTree.h:161
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
Type
Enum describing all types that are allowed in a rule program.
Definition: RuleBasedProgramTree.h:10
A configuration pattern that matches zero or more actual configurations, used in the rule language.
Definition: RuleBasedProgramTree.h:17
ConfigurationPattern()
Constructs a pattern that matches all configurations.
~ConfigurationPattern() noexcept
Destructor.
std::set< LoadEstimatorOption > _loadEstimators
The allowed load estimators.
Definition: RuleBasedProgramTree.h:29
std::set< TraversalOption > _traversals
The allowed traversals.
Definition: RuleBasedProgramTree.h:25
std::set< ContainerOption > _containers
The allowed containers.
Definition: RuleBasedProgramTree.h:21
std::set< double > _cellSizeFactors
The allowed cell size factors.
Definition: RuleBasedProgramTree.h:41
void add(const RuleVM::MemoryCell &value)
Adds a value to the allowed values of this configuration pattern.
Definition: RuleBasedProgramTree.h:65
std::string toString() const
Definition: RuleBasedProgramTree.h:89
std::set< Newton3Option > _newton3Options
The allowed newton3 options.
Definition: RuleBasedProgramTree.h:37
ConfigurationPattern(const ConfigurationPattern &configurationPattern)
Copy Constructor.
std::set< DataLayoutOption > _dataLayouts
The allowed data layouts.
Definition: RuleBasedProgramTree.h:33
bool matches(const Configuration &configuration) const
Definition: RuleBasedProgramTree.h:77
A binary operator in the rule language.
Definition: RuleBasedProgramTree.h:458
Type getType() const override
Definition: RuleBasedProgramTree.cpp:46
Operator
Enum of all binary operators.
Definition: RuleBasedProgramTree.h:462
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for evaluating this expression at runtime.
Definition: RuleBasedProgramTree.cpp:29
std::shared_ptr< Expression > left
The left subexpression of the operator.
Definition: RuleBasedProgramTree.h:467
Operator op
The operator to apply.
Definition: RuleBasedProgramTree.h:471
std::shared_ptr< Expression > right
The right subexpression of the operator.
Definition: RuleBasedProgramTree.h:475
A ConfigurationOrder statement in the rule language e.g.
Definition: RuleBasedProgramTree.h:193
std::vector< SameProperty > sameProperties
Vector containing all set same properties.
Definition: RuleBasedProgramTree.h:211
ConfigurationOrder()=default
Constructs a configuration order that says all configurations are better than all configurations.
ConfigurationPattern greater
The greater/better configuration pattern.
Definition: RuleBasedProgramTree.h:197
SameProperty
Enum containing all possible values of 'with same' options.
Definition: RuleBasedProgramTree.h:206
ConfigurationPattern smaller
The smaller/worse configuration pattern.
Definition: RuleBasedProgramTree.h:201
bool haveEqualSameProperties(const Configuration &conf1, const Configuration &conf2) const
Checks if two configurations have the equal same properties as required by this configuration order.
Definition: RuleBasedProgramTree.cpp:69
~ConfigurationOrder() override=default
Destructor.
std::string toString() const
Definition: RuleBasedProgramTree.cpp:61
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for this statement.
Definition: RuleBasedProgramTree.cpp:56
A list definition in the rule language.
Definition: RuleBasedProgramTree.h:385
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for this statement.
Definition: RuleBasedProgramTree.cpp:177
std::vector< Literal > values
The values in the list.
Definition: RuleBasedProgramTree.h:393
std::string listName
The name of the list.
Definition: RuleBasedProgramTree.h:389
A variable definition in the rule language.
Definition: RuleBasedProgramTree.h:493
std::shared_ptr< Expression > value
The value of the variable.
Definition: RuleBasedProgramTree.h:501
~Define() override
Destructor.
Definition: RuleBasedProgramTree.cpp:184
std::string variable
The name of the variable.
Definition: RuleBasedProgramTree.h:497
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for this statement.
Definition: RuleBasedProgramTree.cpp:186
An expression in the rule language.
Definition: RuleBasedProgramTree.h:166
virtual ~Expression()=default
Virtual default constructor.
virtual void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const =0
Generates code for evaluating this expression at runtime.
virtual Type getType() const =0
An if statement in the rule language.
Definition: RuleBasedProgramTree.h:521
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for this statement.
Definition: RuleBasedProgramTree.cpp:197
std::shared_ptr< Expression > condition
The condition of the if statement.
Definition: RuleBasedProgramTree.h:525
std::vector< std::shared_ptr< Statement > > consequences
The statements contained in the body of the if statement.
Definition: RuleBasedProgramTree.h:529
A literal in the rule language.
Definition: RuleBasedProgramTree.h:358
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for evaluating this expression at runtime.
Definition: RuleBasedProgramTree.cpp:169
RuleVM::MemoryCell value
The value of the literal.
Definition: RuleBasedProgramTree.h:362
Type getType() const override
Definition: RuleBasedProgramTree.cpp:167
Type type
The type of the literal in AST.
Definition: RuleBasedProgramTree.h:366
The AST of a rule program.
Definition: RuleBasedProgramTree.h:544
std::vector< std::shared_ptr< Statement > > statements
All statements in the program.
Definition: RuleBasedProgramTree.h:548
RuleVM::Program generateCode(CodeGenerationContext &context) const
Generates code for the whole program described by this AST.
Definition: RuleBasedProgramTree.cpp:210
A statement in the rule language.
Definition: RuleBasedProgramTree.h:142
virtual ~Statement()=default
Virtual default constructor.
virtual void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const =0
Generates code for this statement.
A unary operator in the rule language.
Definition: RuleBasedProgramTree.h:428
std::shared_ptr< Expression > child
Subexpression of unary operator.
Definition: RuleBasedProgramTree.h:437
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for evaluating this expression at runtime.
Definition: RuleBasedProgramTree.cpp:21
Operator op
Unary operator to apply.
Definition: RuleBasedProgramTree.h:441
Operator
Enum of all unary operators.
Definition: RuleBasedProgramTree.h:432
Type getType() const override
Definition: RuleBasedProgramTree.cpp:27
A variable access in the rule language.
Definition: RuleBasedProgramTree.h:408
Type getType() const override
Definition: RuleBasedProgramTree.cpp:16
const Define * definition
The definition of the variable to access.
Definition: RuleBasedProgramTree.h:412
Variable(const Define *definition)
Constructs a variable access in the rule language.
Definition: RuleBasedProgramTree.h:418
void generateCode(CodeGenerationContext &context, RuleVM::Program &program) const override
Generates code for evaluating this expression at runtime.
Definition: RuleBasedProgramTree.cpp:11
A program that can be executed by this VM.
Definition: RuleVM.h:135