GEGELATI

Loading...
Searching...
No Matches
programEngine.h
1
39#ifndef PROGRAMENGINE_H
40#define PROGRAMENGINE_H
41
42#include "data/primitiveTypeArray.h"
43#include "data/untypedSharedPtr.h"
44#include "program/program.h"
45
46 namespace Program {
54 class ProgramEngine
55 {
56 protected:
59 const Program* program;
60
62 ProgramEngine() = delete;
63
65 Data::PrimitiveTypeArray<double>
66 registers; // If the type of registers attribute is
67 // changed one day
68 // make sure to update the Program::identifyIntrons()
69 // method as it create its own
70 // Data::PrimitiveTypeArray<double> to keep track of
71 // accessed addresses.
72
74 std::vector<std::reference_wrapper<const Data::DataHandler>>
75 dataSources;
76
78 std::vector<std::reference_wrapper<const Data::DataHandler>>
79 dataScsConstsAndRegs;
80
82 uint64_t programCounter;
83
84 protected:
93 ProgramEngine(const Environment& env)
94 : programCounter{0}, registers{env.getParams().nbRegisters},
95 program{NULL}, dataSources{env.getDataSources()}
96 {
97 // Setup the data sources
98 dataScsConstsAndRegs.push_back(this->registers);
99
100 if (env.getParams().nbProgramConstant > 0) {
101 dataScsConstsAndRegs.push_back(env.getFakeDataSources().at(1));
102 }
103
104 // Cannot use insert here because it dataSourcesAndRegisters
105 // requires constnessand dataSrc data are not const...
106 for (auto data : env.getDataSources()) {
107 dataScsConstsAndRegs.push_back(data.get());
108 }
109 }
110
126 template <class T>
127 ProgramEngine(const Program& prog,
128 const std::vector<std::reference_wrapper<T>>& dataSrc)
129 : programCounter{0},
130 registers{prog.getEnvironment().getParams().nbRegisters},
131 program{NULL}
132 {
133 // Check that T is either convertible to a const DataHandler
134 static_assert(
135 std::is_convertible<T&, const Data::DataHandler&>::value);
136 // Setup the data sources
137 this->dataScsConstsAndRegs.push_back(this->registers);
138
139 if (prog.getEnvironment().getParams().nbProgramConstant > 0) {
140 this->dataScsConstsAndRegs.push_back(
141 prog.cGetConstantHandler());
142 }
143
144 // Cannot use insert here because it dataSourcesAndRegisters
145 // requires constnessand dataSrc data are not const...
146 for (std::reference_wrapper<T> data : dataSrc) {
147 this->dataScsConstsAndRegs.push_back(data.get());
148 this->dataSources.push_back(data.get());
149 }
150
151 // Set the Program
152 this->setProgram(prog);
153 };
154
164 ProgramEngine(const Program& prog)
165 : ProgramEngine(prog, prog.getEnvironment().getDataSources()){};
166
171 virtual void processLine() = 0;
172
173 public:
183 void setProgram(const Program& prog);
184
194 template <class T>
195 void setDataSources(
196 const std::vector<std::reference_wrapper<T>>& dataSrc);
197
204 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
205 getDataSources() const;
206
217 const bool next();
218
228 const Line& getCurrentLine() const;
229
241 const Instructions::Instruction& getCurrentInstruction() const;
242
258 const void fetchCurrentOperands(
259 std::vector<Data::UntypedSharedPtr>& operands) const;
260
276 uint64_t getOperandLocation(uint64_t idxOp) const;
277
286 virtual void iterateThroughtProgram(const bool ignoreException);
287
296 virtual std::vector<double> getRegisterValues(uint64_t nbRegisters);
297 };
298
299 template <class T>
300 inline void ProgramEngine::setDataSources(
301 const std::vector<std::reference_wrapper<T>>& dataSrc)
302 {
303 // Check that T is either convertible to a const DataHandler
304 static_assert(std::is_convertible<T&, const Data::DataHandler&>::value);
305
306 // Replace the references in attributes
307 this->dataSources = dataSrc;
308 // we need this offset to push the constant at the first
309 size_t offset =
310 this->program->getEnvironment().getParams().nbProgramConstant > 0
311 ? 2
312 : 1;
313 if (offset == 2) {
314 this->dataScsConstsAndRegs.at(1) =
315 this->program->cGetConstantHandler();
316 }
317 for (size_t idx = 0; idx < this->dataSources.size(); idx++) {
318 this->dataScsConstsAndRegs.at(idx + offset) = dataSrc.at(idx);
319 }
320
321 // Set program to check compatibility with new data source
322 this->setProgram(*this->program);
323 }
324} // namespace Program
325
326#endif // PROGRAMENGINE_H
Data::PrimitiveTypeArray2D
DataHandler for 2D arrays of primitive types.
Definition primitiveTypeArray2D.h:69
Environment
The Environment class contains all information needed to execute a Program.
Definition environment.h:86
Environment::getDataSources
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the Environment.
Definition environment.cpp:204
Environment::getParams
const Learn::LearningParameters & getParams() const
Get the instance of parameters used.
Definition environment.cpp:168
Environment::getFakeDataSources
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getFakeDataSources() const
Definition environment.cpp:210
Instructions::Instruction
This abstract class is the base class for any instruction to be used in a Program.
Definition instruction.h:61
Program::Line
Definition line.h:49
Program::ProgramEngine
This abstract class is the base class for any program engine (generation and execution)
Definition programEngine.h:55
Program::ProgramEngine::program
const Program * program
Definition programEngine.h:59
Program::ProgramEngine::processLine
virtual void processLine()=0
operator parenthesis used when iterating through the program with the function iterationThroughtProgr...
Program::ProgramEngine::ProgramEngine
ProgramEngine(const Environment &env)
Constructor of the class.
Definition programEngine.h:93
Program::ProgramEngine::programCounter
uint64_t programCounter
Program counter of the execution engine.
Definition programEngine.h:82
Program::ProgramEngine::fetchCurrentOperands
const void fetchCurrentOperands(std::vector< Data::UntypedSharedPtr > &operands) const
Get the operands for the current Instruction.
Definition programEngine.cpp:126
Program::ProgramEngine::setDataSources
void setDataSources(const std::vector< std::reference_wrapper< T > > &dataSrc)
Method for changing the dataSources on which the Program will be executed.
Definition programEngine.h:300
Program::ProgramEngine::next
const bool next()
Increments the programCounter and checks for the end of the Program.
Definition programEngine.cpp:98
Program::ProgramEngine::getDataSources
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the ProgramExecutionEngine.
Definition programEngine.cpp:93
Program::ProgramEngine::iterateThroughtProgram
virtual void iterateThroughtProgram(const bool ignoreException)
Function that iterates through the lines of the program and execute the function processLine().
Definition programEngine.cpp:167
Program::ProgramEngine::getRegisterValues
virtual std::vector< double > getRegisterValues(uint64_t nbRegisters)
Return the current registers value of the program indicated.
Definition programEngine.cpp:194
Program::ProgramEngine::dataScsConstsAndRegs
std::vector< std::reference_wrapper< const Data::DataHandler > > dataScsConstsAndRegs
Data sources (including registers) used in the Program.
Definition programEngine.h:79
Program::ProgramEngine::getOperandLocation
uint64_t getOperandLocation(uint64_t idxOp) const
Get the location for the current Instruction.
Definition programEngine.cpp:150
Program::ProgramEngine::setProgram
void setProgram(const Program &prog)
Method for changing the Program executed by a ProgramExecutionEngin.
Definition programEngine.cpp:43
Program::ProgramEngine::dataSources
std::vector< std::reference_wrapper< const Data::DataHandler > > dataSources
Data sources from the environment used for archiving a program.
Definition programEngine.h:75
Program::ProgramEngine::ProgramEngine
ProgramEngine()=delete
Default constructor is deleted.
Program::ProgramEngine::getCurrentLine
const Line & getCurrentLine() const
Get the Program Line corresponding to the current programCounter.
Definition programEngine.cpp:109
Program::ProgramEngine::registers
Data::PrimitiveTypeArray< double > registers
Registers used for the Program execution.
Definition programEngine.h:66
Program::ProgramEngine::getCurrentInstruction
const Instructions::Instruction & getCurrentInstruction() const
Get the Instruction corresponding to the current programCounter.
Definition programEngine.cpp:114
Program::ProgramEngine::ProgramEngine
ProgramEngine(const Program &prog, const std::vector< std::reference_wrapper< T > > &dataSrc)
Constructor of the class.
Definition programEngine.h:127
Program::ProgramEngine::ProgramEngine
ProgramEngine(const Program &prog)
Constructor of the class.
Definition programEngine.h:164
Program::Program::cGetConstantHandler
const Data::ConstantHandler & cGetConstantHandler() const
get a const reference to the constantHandler object of the Program
Definition program.cpp:236
Program::Program::getEnvironment
const Environment & getEnvironment() const
Get the environment associated to the Program at construction.
Definition program.cpp:126
Program
Definition line.h:44
Learn::LearningParameters::nbProgramConstant
size_t nbProgramConstant
Number of Constants available in a program.
Definition learningParameters.h:143