souffle  2.0.2-371-g6315b36
Engine.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, The Souffle Developers. All rights reserved.
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file Engine.h
12  *
13  * Declares the Interpreter Engine class. The engine takes in an Node
14  * representation and execute them.
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "Global.h"
20 #include "interpreter/Context.h"
21 #include "interpreter/Generator.h"
22 #include "interpreter/Index.h"
23 #include "interpreter/Node.h"
24 #include "interpreter/Relation.h"
25 #include "ram/TranslationUnit.h"
26 #include "ram/analysis/Index.h"
27 #include "souffle/RamTypes.h"
28 #include "souffle/RecordTable.h"
29 #include "souffle/SymbolTable.h"
31 #include <atomic>
32 #include <cstddef>
33 #include <deque>
34 #include <map>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 #ifdef _OPENMP
39 #include <omp.h>
40 #endif
41 
42 namespace souffle::interpreter {
43 
44 class ProgInterface;
45 
46 /**
47  * @class Engine
48  * @brief This class translate the RAM Program into executable format and interpreter it.
49  */
50 class Engine {
51  using RelationHandle = Own<RelationWrapper>;
52  friend ProgInterface;
53  friend NodeGenerator;
54 
55 public:
57 
58  /** @brief Execute the main program */
59  void executeMain();
60  /** @brief Execute the subroutine program */
61  void executeSubroutine(
62  const std::string& name, const std::vector<RamDomain>& args, std::vector<RamDomain>& ret);
63 
64 private:
65  /** @brief Generate intermediate representation from RAM */
66  void generateIR();
67  /** @brief Remove a relation from the environment */
68  void dropRelation(const size_t relId);
69  /** @brief Swap the content of two relations */
70  void swapRelation(const size_t ramRel1, const size_t ramRel2);
71  /** @brief Return a reference to the relation on the given index */
72  RelationHandle& getRelationHandle(const size_t idx);
73  /** @brief Return the string symbol table */
75  /** @brief Return the record table */
77  /** @brief Return the ram::TranslationUnit */
79  /** @brief Execute the program */
80  RamDomain execute(const Node*, Context&);
81  /** @brief Return method handler */
82  void* getMethodHandle(const std::string& method);
83  /** @brief Load DLL */
84  const std::vector<void*>& loadDLL();
85  /** @brief Return current iteration number for loop operation */
86  size_t getIterationNumber() const;
87  /** @brief Increase iteration number by one */
88  void incIterationNumber();
89  /** @brief Reset iteration number */
90  void resetIterationNumber();
91  /** @brief Increment the counter */
92  int incCounter();
93  /** @brief Return the relation map. */
95  /** @brief Create and add relation into the runtime environment. */
96  void createRelation(const ram::Relation& id, const size_t idx);
97 
98  // -- Defines template for specialized interpreter operation -- */
99  template <typename Rel>
100  RamDomain evalExistenceCheck(const ExistenceCheck& shadow, Context& ctxt);
101 
102  template <typename Rel>
104 
105  template <typename Rel>
106  RamDomain evalScan(const Rel& rel, const ram::Scan& cur, const Scan& shadow, Context& ctxt);
107 
108  template <typename Rel>
110  const Rel& rel, const ram::ParallelScan& cur, const ParallelScan& shadow, Context& ctxt);
111 
112  template <typename Rel>
113  RamDomain evalIndexScan(const ram::IndexScan& cur, const IndexScan& shadow, Context& ctxt);
114 
115  template <typename Rel>
117  const ParallelIndexScan& shadow, Context& ctxt);
118 
119  template <typename Rel>
120  RamDomain evalChoice(const Rel& rel, const ram::Choice& cur, const Choice& shadow, Context& ctxt);
121 
122  template <typename Rel>
124  const Rel& rel, const ram::ParallelChoice& cur, const ParallelChoice& shadow, Context& ctxt);
125 
126  template <typename Rel>
127  RamDomain evalIndexChoice(const ram::IndexChoice& cur, const IndexChoice& shadow, Context& ctxt);
128 
129  template <typename Rel>
131  const ParallelIndexChoice& shadow, Context& ctxt);
132 
133  template <typename Aggregate, typename Iter>
134  RamDomain evalAggregate(const Aggregate& aggregate, const Node& filter, const Node* expression,
135  const Node& nestedOperation, const Iter& ranges, Context& ctxt);
136 
137  template <typename Rel>
139  const ParallelAggregate& shadow, Context& ctxt);
140 
141  template <typename Rel>
143  const ram::ParallelIndexAggregate& cur, const ParallelIndexAggregate& shadow, Context& ctxt);
144 
145  template <typename Rel>
146  RamDomain evalIndexAggregate(const ram::IndexAggregate& cur, const IndexAggregate& shadow, Context& ctxt);
147 
148  template <typename Rel>
149  RamDomain evalProject(Rel& rel, const Project& shadow, Context& ctxt);
150 
151  /** If profile is enable in this program */
152  const bool profileEnabled;
153  /** If running a provenance program */
154  const bool isProvenance;
155  /** subroutines */
157  /** main program */
159  /** Number of threads enabled for this program */
160  size_t numOfThreads;
161  /** Profile counter */
162  std::atomic<RamDomain> counter{0};
163  /** Loop iteration counter */
164  size_t iteration = 0;
165  /** Profile for rule frequencies */
166  std::map<std::string, std::deque<std::atomic<size_t>>> frequencies;
167  /** Profile for relation reads */
168  std::map<std::string, std::atomic<size_t>> reads;
169  /** DLL */
170  std::vector<void*> dll;
171  /** Program */
173  /** IndexAnalysis */
175  /** Record Table*/
177  /** Symbol table for relations */
179 };
180 
181 } // namespace souffle::interpreter
souffle::interpreter::Engine::dll
std::vector< void * > dll
DLL.
Definition: Engine.h:176
souffle::interpreter::Engine::numOfThreads
size_t numOfThreads
Number of threads enabled for this program.
Definition: Engine.h:166
souffle::interpreter::Scan
Definition: Node.h:583
souffle::interpreter::Engine::executeSubroutine
void executeSubroutine(const std::string &name, const std::vector< RamDomain > &args, std::vector< RamDomain > &ret)
Execute the subroutine program.
Definition: Engine.cpp:347
Node.h
souffle::interpreter::Engine::dropRelation
void dropRelation(const size_t relId)
Remove a relation from the environment.
souffle::interpreter::Engine::relations
VecOwn< RelationHandle > relations
Symbol table for relations.
Definition: Engine.h:184
souffle::interpreter::IndexChoice
Definition: Node.h:636
souffle::interpreter::Engine::incCounter
int incCounter()
Increment the counter.
Definition: Engine.cpp:167
souffle::ram::ParallelIndexScan
Search for tuples of a relation matching a criteria.
Definition: ParallelIndexScan.h:58
souffle::interpreter::Engine::createRelation
void createRelation(const ram::Relation &id, const size_t idx)
Create and add relation into the runtime environment.
Definition: Engine.cpp:198
souffle::interpreter::Engine::counter
std::atomic< RamDomain > counter
Profile counter.
Definition: Engine.h:168
Index.h
souffle::interpreter::IndexScan
Definition: Node.h:599
souffle::interpreter::Engine::evalParallelScan
RamDomain evalParallelScan(const Rel &rel, const ram::ParallelScan &cur, const ParallelScan &shadow, Context &ctxt)
Definition: Engine.cpp:1338
souffle::interpreter::Engine::getRecordTable
RecordTable & getRecordTable()
Return the record table.
Definition: Engine.cpp:175
souffle::interpreter::Engine::executeMain
void executeMain()
Execute the main program.
Definition: Engine.cpp:272
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::interpreter::IndexAggregate
Definition: Node.h:695
Relation.h
souffle::interpreter::Engine::evalParallelIndexChoice
RamDomain evalParallelIndexChoice(const Rel &rel, const ram::ParallelIndexChoice &cur, const ParallelIndexChoice &shadow, Context &ctxt)
Definition: Engine.cpp:1475
souffle::interpreter::Node
This is a shadow node for a ram::Node that is enriched for with local information so that the interpr...
Definition: Node.h:178
souffle::interpreter::Engine::evalExistenceCheck
RamDomain evalExistenceCheck(const ExistenceCheck &shadow, Context &ctxt)
Definition: Engine.cpp:1237
souffle::interpreter::ParallelIndexScan
Definition: Node.h:610
SymbolTable.h
souffle::interpreter::Engine::evalAggregate
RamDomain evalAggregate(const Aggregate &aggregate, const Node &filter, const Node *expression, const Node &nestedOperation, const Iter &ranges, Context &ctxt)
Definition: Engine.cpp:1511
souffle::interpreter::Engine::isProvenance
const bool isProvenance
If running a provenance program.
Definition: Engine.h:160
souffle::RecordTable
Definition: RecordTable.h:114
souffle::ram::ParallelIndexChoice
Use an index to find a tuple in a relation such that a given condition holds in parallel.
Definition: ParallelIndexChoice.h:59
souffle::interpreter::ParallelScan
Definition: Node.h:592
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::interpreter::Choice
Definition: Node.h:618
souffle::interpreter::Engine::Engine
Engine(ram::TranslationUnit &tUnit)
Definition: Engine.cpp:146
souffle::interpreter::Engine::isa
ram::analysis::IndexAnalysis * isa
IndexAnalysis.
Definition: Engine.h:180
souffle::interpreter::Engine::profileEnabled
const bool profileEnabled
If profile is enable in this program.
Definition: Engine.h:158
Index.h
souffle::interpreter::Engine::iteration
size_t iteration
Loop iteration counter.
Definition: Engine.h:170
souffle::interpreter::Engine::getMethodHandle
void * getMethodHandle(const std::string &method)
Return method handler.
Definition: Engine.cpp:183
souffle::interpreter::Engine::swapRelation
void swapRelation(const size_t ramRel1, const size_t ramRel2)
Swap the content of two relations.
Definition: Engine.cpp:161
souffle::interpreter::Engine::tUnit
ram::TranslationUnit & tUnit
Program.
Definition: Engine.h:178
souffle::interpreter::Engine::getRelationHandle
RelationHandle & getRelationHandle(const size_t idx)
Return a reference to the relation on the given index.
Definition: Engine.cpp:157
Global.h
souffle::interpreter::Engine::evalParallelChoice
RamDomain evalParallelChoice(const Rel &rel, const ram::ParallelChoice &cur, const ParallelChoice &shadow, Context &ctxt)
Definition: Engine.cpp:1429
souffle::ram::TranslationUnit
Translating a RAM program.
Definition: TranslationUnit.h:55
souffle::interpreter
Definition: BrieIndex.cpp:22
souffle::interpreter::ParallelIndexChoice
Definition: Node.h:647
souffle::interpreter::ParallelIndexAggregate
Definition: Node.h:706
souffle::interpreter::Engine::recordTable
RecordTable recordTable
Record Table.
Definition: Engine.h:182
souffle::interpreter::Engine::NodeGenerator
friend NodeGenerator
Definition: Engine.h:59
souffle::interpreter::Engine::evalScan
RamDomain evalScan(const Rel &rel, const ram::Scan &cur, const Scan &shadow, Context &ctxt)
Definition: Engine.cpp:1327
souffle::filter
std::vector< A > filter(std::vector< A > xs, F &&f)
Filter a vector to include certain elements.
Definition: FunctionalUtil.h:155
ContainerUtil.h
souffle::ram::analysis::IndexAnalysis
Definition: Index.h:413
souffle::ram::ParallelIndexAggregate
Aggregate over values of a relation using an index in parallel.
Definition: ParallelIndexAggregate.h:52
souffle::ram::Relation
An abstract class for performing indexed operations.
Definition: Relation.h:40
souffle::interpreter::ProvenanceExistenceCheck
Definition: Node.h:558
souffle::interpreter::Engine::evalProvenanceExistenceCheck
RamDomain evalProvenanceExistenceCheck(const ProvenanceExistenceCheck &shadow, Context &ctxt)
Definition: Engine.cpp:1282
souffle::ram::ParallelAggregate
Parallel Aggregation function applied on some relation.
Definition: ParallelAggregate.h:53
souffle::interpreter::Engine::resetIterationNumber
void resetIterationNumber()
Reset iteration number.
Definition: Engine.cpp:268
souffle::interpreter::Engine::getRelationMap
VecOwn< RelationHandle > & getRelationMap()
Return the relation map.
Definition: Engine.cpp:194
souffle::SymbolTable
Definition: SymbolTable.h:48
souffle::ram::IndexChoice
Use an index to find a tuple in a relation such that a given condition holds.
Definition: IndexChoice.h:58
souffle::ram::IndexAggregate
Indexed aggregation on a relation. The index allows us to iterate over a restricted range.
Definition: IndexAggregate.h:51
souffle::interpreter::Project
Definition: Node.h:733
souffle::ram::ParallelScan
Iterate all tuples of a relation in parallel.
Definition: ParallelScan.h:52
souffle::interpreter::Engine::evalParallelIndexScan
RamDomain evalParallelIndexScan(const Rel &rel, const ram::ParallelIndexScan &cur, const ParallelIndexScan &shadow, Context &ctxt)
Definition: Engine.cpp:1384
souffle::interpreter::Engine::execute
RamDomain execute(const Node *, Context &)
Execute the program.
Definition: Engine.cpp:359
RecordTable.h
souffle::interpreter::Engine::ProgInterface
friend ProgInterface
Definition: Engine.h:58
souffle::interpreter::Engine::evalProject
RamDomain evalProject(Rel &rel, const Project &shadow, Context &ctxt)
Definition: Engine.cpp:1681
TranslationUnit.h
souffle::interpreter::Engine::subroutine
VecOwn< Node > subroutine
subroutines
Definition: Engine.h:162
souffle::interpreter::Engine::getIterationNumber
size_t getIterationNumber() const
Return current iteration number for loop operation.
Definition: Engine.cpp:262
souffle::interpreter::Engine::evalIndexScan
RamDomain evalIndexScan(const ram::IndexScan &cur, const IndexScan &shadow, Context &ctxt)
Definition: Engine.cpp:1363
souffle::ram::Choice
Find a tuple in a relation such that a given condition holds.
Definition: Choice.h:59
souffle::ram::Scan
Iterate all tuples of a relation.
Definition: Scan.h:47
souffle::ram::IndexScan
Search for tuples of a relation matching a criteria.
Definition: IndexScan.h:52
souffle::interpreter::Engine::getTranslationUnit
ram::TranslationUnit & getTranslationUnit()
Return the ram::TranslationUnit.
Definition: Engine.cpp:179
souffle::interpreter::Engine::RelationHandle
Own< RelationWrapper > RelationHandle
Definition: Engine.h:57
Context.h
souffle::interpreter::Engine::evalIndexChoice
RamDomain evalIndexChoice(const ram::IndexChoice &cur, const IndexChoice &shadow, Context &ctxt)
Definition: Engine.cpp:1454
souffle::interpreter::Engine::evalParallelIndexAggregate
RamDomain evalParallelIndexAggregate(const ram::ParallelIndexAggregate &cur, const ParallelIndexAggregate &shadow, Context &ctxt)
Definition: Engine.cpp:1638
souffle::interpreter::ParallelAggregate
Definition: Node.h:688
souffle::interpreter::Aggregate
Definition: Node.h:670
RamTypes.h
souffle::interpreter::Engine::main
Own< Node > main
main program
Definition: Engine.h:164
souffle::interpreter::Engine::reads
std::map< std::string, std::atomic< size_t > > reads
Profile for relation reads.
Definition: Engine.h:174
souffle::interpreter::ExistenceCheck
Definition: Node.h:530
souffle::interpreter::ParallelChoice
Definition: Node.h:629
souffle::interpreter::Engine::evalParallelAggregate
RamDomain evalParallelAggregate(const Rel &rel, const ram::ParallelAggregate &cur, const ParallelAggregate &shadow, Context &ctxt)
Definition: Engine.cpp:1623
souffle::interpreter::Engine::evalIndexAggregate
RamDomain evalIndexAggregate(const ram::IndexAggregate &cur, const IndexAggregate &shadow, Context &ctxt)
Definition: Engine.cpp:1664
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::interpreter::Engine::incIterationNumber
void incIterationNumber()
Increase iteration number by one.
Definition: Engine.cpp:265
souffle::interpreter::Engine::generateIR
void generateIR()
Generate intermediate representation from RAM.
Definition: Engine.cpp:334
Generator.h
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::interpreter::Engine::evalChoice
RamDomain evalChoice(const Rel &rel, const ram::Choice &cur, const Choice &shadow, Context &ctxt)
Definition: Engine.cpp:1416
souffle::interpreter::Engine::loadDLL
const std::vector< void * > & loadDLL()
Load DLL.
Definition: Engine.cpp:217
souffle::interpreter::Context
Evaluation context for Interpreter operations.
Definition: Context.h:39
souffle::interpreter::Engine::frequencies
std::map< std::string, std::deque< std::atomic< size_t > > > frequencies
Profile for rule frequencies.
Definition: Engine.h:172
souffle::ram::ParallelChoice
Find a tuple in a relation such that a given condition holds in parallel.
Definition: ParallelChoice.h:51
souffle::interpreter::Engine::getSymbolTable
SymbolTable & getSymbolTable()
Return the string symbol table.
Definition: Engine.cpp:171