1 #ifndef EMP_EVENT_DRIVEN_GP_H 2 #define EMP_EVENT_DRIVEN_GP_H 6 #include <unordered_map> 11 #include "../tools/BitSet.h" 12 #include "../tools/BitVector.h" 13 #include "../tools/map_utils.h" 14 #include "../tools/string_utils.h" 15 #include "../tools/Random.h" 16 #include "../base/vector.h" 17 #include "../base/Ptr.h" 18 #include "../base/array.h" 19 #include "../control/SignalControl.h" 20 #include "../control/Signal.h" 97 template<
size_t AFFINITY_WIDTH>
106 using memory_t = std::unordered_map<mem_key_t, mem_val_t>;
128 : id(_id), affinity(aff), msg(_msg), properties(_properties) { ; }
136 bool HasProperty(std::string property)
const {
return properties.count(property); }
153 : begin(_begin), end(_end), type(_type) { ; }
171 : local_mem(), input_mem(), output_mem(), default_mem_val(_default_mem_val),
172 func_ptr(0), inst_ptr(0), block_stack(), is_main(_is_main) { ; }
181 func_ptr = 0; inst_ptr = 0;
186 size_t GetFP()
const {
return func_ptr; }
189 size_t GetIP()
const {
return inst_ptr; }
195 void SetIP(
size_t ip) { inst_ptr = ip; }
198 void SetFP(
size_t fp) { func_ptr = fp; }
242 if (!
Has(local_mem, key)) local_mem[key] = default_mem_val;
243 return local_mem[key];
249 if (!
Has(input_mem, key)) input_mem[key] = default_mem_val;
250 return input_mem[key];
256 if (!
Has(output_mem, key)) output_mem[key] = default_mem_val;
257 return output_mem[key];
272 : id(_id), args(), affinity(_aff) { args[0] = a0; args[1] = a1; args[2] = a2; }
280 {
id = _id; args[0] = _a0; args[1] = _a1; args[2] = _a2; affinity = _aff; }
284 args[0] = other.
args[0]; args[1] = other.
args[1]; args[2] = other.
args[2];
294 return std::tie(
id, args, affinity) < std::tie(other.
id, other.
args, other.
affinity);
314 : affinity(_aff), inst_seq(_seq) { ; }
325 return std::tie(inst_seq, affinity) < std::tie(other.
inst_seq, other.
affinity);
341 inst_seq[pos].Set(
id, a0, a1, a2);
345 inst_seq[pos].Set(inst);
362 : inst_lib(_ilib), program(_prgm) { ; }
374 return program < other.
program;
383 for (
size_t i = 0; i <
GetSize(); ++i) cnt += program[i].
GetSize();
390 return fID < program.
size() && pos < program[fID].GetSize();
408 if (program.empty()) { program.
emplace_back(); fp = 0; }
409 else if (fID < 0 || fID >= (
int)program.
size()) { fp = program.
size() - 1; }
410 else fp = (size_t)fID;
411 program[fp].PushInst(
id, a0, a1, a2, aff);
422 size_t id = inst_lib->GetID(name);
423 if (program.empty()) { program.
emplace_back(); fp = 0; }
424 else if (fID < 0 || fID >= (
int)program.
size()) { fp = program.
size() - 1; }
425 else fp = (size_t)fID;
426 program[fp].PushInst(
id, a0, a1, a2, aff);
435 if (program.empty()) { program.
emplace_back(); fp = 0; }
436 else if (fID < 0 || fID >= (
int)program.
size()) { fp = program.
size() - 1; }
437 else fp = (size_t)fID;
438 program[fp].PushInst(inst);
444 program[fID].SetInst(pos,
id, a0, a1, a2, aff);
449 program[fID].SetInst(pos, inst);
461 void Load(std::istream & input) {
464 std::string cur_line;
466 while (!input.eof()) {
467 std::getline(input, cur_line);
471 slice(cur_line, line_components,
'-');
472 if (
to_lower(line_components[0]) ==
"fn" && line_components.
size() > 1) {
474 std::string & aff_str = line_components[1];
476 for (
size_t i = 0; i < aff_str.size(); ++i) {
477 if (i >= fun_aff.
GetSize())
break;
478 if (aff_str[i] ==
'1') fun_aff.
Set(fun_aff.
GetSize() - i - 1,
true);
484 int a0 = 0;
int a1 = 0;
int a2 = 0;
486 size_t aff_begin = cur_line.find_first_of(
'[');
487 size_t aff_end = cur_line.find_first_of(
']');
488 if ((aff_begin != std::string::npos) && (aff_end != std::string::npos) && (aff_begin < aff_end)) {
490 std::string aff_str =
string_get_range(cur_line, aff_begin+1, aff_end-(aff_begin+1));
491 for (
size_t i = 0; i < aff_str.size(); ++i) {
492 if (i >= inst_aff.
GetSize())
break;
493 if (aff_str[i] ==
'1') inst_aff.
Set(inst_aff.
GetSize() - i - 1,
true);
499 size_t args_begin = cur_line.find_first_of(
'(');
500 size_t args_end = cur_line.find_first_of(
')');
502 if ((args_begin != std::string::npos) && (args_end != std::string::npos) && (args_begin < args_end)) {
504 std::string args_str =
string_get_range(cur_line, args_begin+1, args_end-(args_begin+1));
505 line_components.clear();
507 slice(args_str, line_components,
',');
508 if (args_cnt < line_components.
size()) {
510 a0 = std::stoi(line_components[args_cnt]); ++args_cnt;
512 if (args_cnt < line_components.
size()) {
514 a1 = std::stoi(line_components[args_cnt]); ++args_cnt;
516 if (args_cnt < line_components.
size()) {
518 a2 = std::stoi(line_components[args_cnt]); ++args_cnt;
524 emp_assert(inst_lib->GetID(cur_line) != (size_t)-1);
526 PushInst(cur_line, a0, a1, a2, inst_aff);
533 os << inst_lib->GetName(inst.
id);
534 if (inst_lib->HasProperty(inst.
id,
"affinity")) {
537 const size_t num_args = inst_lib->GetNumArgs(inst.
id);
538 for (
size_t i = 0; i < num_args; i++) {
539 os <<
' ' << inst.
args[i];
545 os << inst_lib->GetName(inst.
id);
548 for (
size_t i = 0; i < MAX_INST_ARGS - 1; i++) {
549 os << inst.
args[i] <<
',';
550 }
if (MAX_INST_ARGS > 0) { os << inst.
args[MAX_INST_ARGS-1]; }
556 for (
size_t fID = 0; fID <
GetSize(); fID++) {
558 os <<
"Fn-" << fID <<
" ";
559 program[fID].affinity.Print(os);
562 for (
size_t i = 0; i < program[fID].GetSize(); i++) {
563 const inst_t & inst = program[fID].inst_seq[i];
564 int num_spaces = 2 + (2 * depth);
565 for (
int s = 0; s < num_spaces; s++) os <<
' ';
568 if (inst_lib->HasProperty(inst.
id,
"block_def")) {
571 }
else if (inst_lib->HasProperty(inst.
id,
"block_close") && depth > 0) {
582 for (
size_t fID = 0; fID <
GetSize(); fID++) {
585 program[fID].affinity.Print(os);
588 for (
size_t i = 0; i < program[fID].GetSize(); i++) {
589 const inst_t & inst = program[fID][i];
590 int num_spaces = 2 + (2 * depth);
591 for (
int s = 0; s < num_spaces; s++) os <<
' ';
592 PrintInstFull(inst, os);
594 if (inst_lib->HasProperty(inst.
id,
"block_def")) {
597 }
else if (inst_lib->HasProperty(inst.
id,
"block_close") && depth > 0) {
641 random_ptr(rnd), random_owner(false),
646 max_cores(DEFAULT_MAX_CORES), max_call_depth(DEFAULT_MAX_CALL_DEPTH),
647 default_mem_value(DEFAULT_MEM_VALUE), min_bind_thresh(DEFAULT_MIN_BIND_THRESH),
648 stochastic_fun_call(true),
649 cores(max_cores), active_cores(), inactive_cores(max_cores), pending_cores(),
650 exec_core_id(0), is_executing(false)
655 for (
size_t i = 0; i < inactive_cores.
size(); ++i)
656 inactive_cores[i] = (inactive_cores.
size() - 1) - i;
671 : event_lib(in.event_lib),
672 random_ptr(in.random_ptr), random_owner(in.random_owner),
674 shared_mem(in.shared_mem),
675 event_queue(in.event_queue),
676 traits(in.traits), errors(in.errors),
677 max_cores(in.max_cores), max_call_depth(in.max_call_depth),
678 default_mem_value(in.default_mem_value), min_bind_thresh(in.min_bind_thresh),
679 stochastic_fun_call(in.stochastic_fun_call),
681 active_cores(in.active_cores), inactive_cores(in.inactive_cores),
682 pending_cores(in.pending_cores),
683 exec_core_id(in.exec_core_id), is_executing(in.is_executing)
685 in.random_ptr =
nullptr;
686 in.random_owner =
false;
687 in.event_lib =
nullptr;
688 in.program.inst_lib =
nullptr;
692 : event_lib(in.event_lib),
693 random_ptr(nullptr), random_owner(false),
695 shared_mem(in.shared_mem),
696 event_queue(in.event_queue),
697 traits(in.traits), errors(in.errors),
698 max_cores(in.max_cores), max_call_depth(in.max_call_depth),
699 default_mem_value(in.default_mem_value), min_bind_thresh(in.min_bind_thresh),
700 stochastic_fun_call(in.stochastic_fun_call),
702 active_cores(in.active_cores), inactive_cores(in.inactive_cores),
703 pending_cores(in.pending_cores),
704 exec_core_id(in.exec_core_id), is_executing(in.is_executing)
711 if (random_owner) random_ptr.
Delete();
730 for (
size_t i = 0; i < cores.
size(); ++i) cores[i].clear();
731 active_cores.clear();
732 pending_cores.clear();
733 inactive_cores.
resize(max_cores);
735 for (
size_t i = 0; i < inactive_cores.
size(); ++i)
736 inactive_cores[i] = (inactive_cores.
size() - 1) - i;
737 exec_core_id = (size_t)-1;
739 is_executing =
false;
747 if (!inactive_cores.
size())
return;
750 if (best_matches.empty())
return;
751 if (best_matches.
size() == 1.0) fID = best_matches[0];
752 else if (stochastic_fun_call) fID = best_matches[(size_t)random_ptr->GetUInt(0, best_matches.
size())];
753 else fID = best_matches[0];
761 if (!inactive_cores.
size())
return;
763 size_t core_id = inactive_cores.
back();
774 if (is_executing) pending_cores.push_back(core_id);
805 return program[fID].inst_seq[pos];
860 return (shared_mem)[key];
868 min_bind_thresh =
emp::Max(val, 0.0);
881 if (val > max_cores) {
885 for (
size_t i = max_cores; i < val; i++)
887 }
else if (val < max_cores) {
892 size_t ac_cnt = active_cores.
size();
893 size_t ac_adjust = 0;
894 while (ac_idx < ac_cnt) {
895 size_t core_id = active_cores[ac_idx];
896 if (core_id >= val) {
898 active_cores[ac_idx - ac_adjust] = (size_t)-1;
900 }
else if (ac_adjust) {
901 active_cores[ac_idx] = (size_t)-1;
902 active_cores[ac_idx - ac_adjust] = core_id;
906 active_cores.
resize(ac_cnt - ac_adjust);
909 size_t ic_cnt = inactive_cores.
size();
910 size_t ic_adjust = 0;
911 while (ic_idx < ic_cnt) {
912 size_t core_id = inactive_cores[ic_idx];
913 if (core_id >= val) {
915 inactive_cores[ic_idx - ic_adjust] = (size_t)-1;
917 }
else if (ic_adjust) {
918 inactive_cores[ic_idx] = (size_t)-1;
919 inactive_cores[ic_idx - ic_adjust] = core_id;
923 inactive_cores.
resize(ic_cnt - ic_adjust);
925 if (active_cores.
size()) exec_core_id = active_cores[0];
938 default_mem_value = val;
940 for (
size_t i = 0; i < cores.
size(); ++i)
941 for (
size_t k = 0; k < cores[i].
size(); ++k)
952 if (
id >= traits.
size()) traits.
resize(
id+1, 0.0);
959 if (
id >= traits.
size()) traits.
resize(
id+1, 0.0);
966 if (
id >= traits.
size()) traits.
resize(
id+1, 0.0);
976 program.
SetInst(fID, pos, inst);
983 program.
SetInst(fID, pos,
id, a0, a1, a2, aff);
1003 { program.
PushInst(
id, a0, a1, a2, aff, fID); }
1011 { program.
PushInst(name, a0, a1, a2, aff, fID); }
1028 void Load(std::istream & input) { program.
Load(input); }
1033 if (random_owner) random_ptr.
Delete();
1034 else random_ptr =
nullptr;
1035 random_ptr.
New(seed);
1036 random_owner =
true;
1054 int depth_counter = 1;
1058 if (inst_lib->HasProperty(inst.
id,
"block_def")) {
1060 }
else if (inst_lib->HasProperty(inst.
id,
"block_close")) {
1063 if (depth_counter == 0)
break;
1080 switch (block.
type) {
1112 for (
size_t i=0; i < program.
GetSize(); ++i) {
1114 if (bind == threshold) best_matches.
push_back(i);
1115 else if (bind > threshold) {
1117 best_matches[0] = i;
1121 return best_matches;
1128 if (
GetCurCore().size() >= max_call_depth)
return;
1131 if (best_matches.empty())
return;
1132 if (best_matches.
size() == 1.0) fID = best_matches[0];
1133 else if (stochastic_fun_call) fID = best_matches[(size_t)random_ptr->GetUInt(0, best_matches.
size())];
1134 else fID = best_matches[0];
1148 State & caller_state = core[core.
size() - 2];
1150 new_state.
SetFP(fID);
1152 for (
auto mem : caller_state.
local_mem) {
1153 new_state.
SetInput(mem.first, mem.second);
1163 if (returning_state.
IsMain())
return;
1166 if (core.
size() > 1) {
1168 State & caller_state = core[core.
size() - 2];
1169 for (
auto mem : returning_state.
output_mem) caller_state.
SetLocal(mem.first, mem.second);
1188 const size_t id = event_lib->GetID(name);
1189 event_t event(
id, affinity, msg, properties);
1190 event_lib->TriggerEvent(*
this, event);
1196 event_t event(
id, affinity, msg, properties);
1197 event_lib->TriggerEvent(*
this, event);
1206 const size_t id = event_lib->GetID(name);
1207 event_queue.emplace_back(
id, affinity, msg, properties);
1213 event_queue.emplace_back(
id, affinity, msg, properties);
1220 while (!event_queue.empty()) {
1222 event_queue.pop_front();
1225 size_t active_core_idx = 0;
1226 size_t core_cnt = active_cores.
size();
1228 is_executing =
true;
1229 while (active_core_idx < core_cnt) {
1231 exec_core_id = active_cores[active_core_idx];
1234 active_cores[active_core_idx] = (size_t)-1;
1240 const size_t ip = cur_state.
inst_ptr;
1241 const size_t fp = cur_state.
func_ptr;
1245 if (ip >= program[fp].
GetSize()) {
1260 program.
inst_lib->ProcessInst(*
this, program[fp].inst_seq[ip]);
1265 active_cores[active_core_idx - adjust] = (size_t)-1;
1271 is_executing =
false;
1273 active_cores.
resize(core_cnt - adjust);
1275 while (pending_cores.size()) {
1277 pending_cores.pop_front();
1280 if (active_cores.
size()) exec_core_id = active_cores[0];
1291 os <<
"[" << event_lib->GetName(event.
id) <<
",";
event.affinity.Print(os); os <<
",(";
1292 for (
const auto & mem : event.
msg) std::cout <<
"{" << mem.first <<
":" << mem.second <<
"}";
1293 os <<
"),(Properties:";
1294 for (
const auto & property : event.
properties) std::cout <<
" " << property;
1305 if (traits.
size() == 0) { os <<
"[]";
return; }
1307 for (
size_t i = 0; i < traits.
size() - 1; ++i) {
1308 os << traits[i] <<
", ";
1309 } os << traits[traits.
size() - 1] <<
"]";
1334 os <<
"Shared memory: ";
1335 for (
auto mem : shared_mem) os <<
'{' << mem.first <<
':' << mem.second <<
'}'; os <<
'\n';
1337 os <<
"Errors: " << errors <<
"\n";
1339 os <<
"Event queue: ";
1340 for (
auto event : event_queue) {
PrintEvent(event, os); os <<
" "; }
1343 for (
size_t i = 0; i < active_cores.
size(); ++i) {
1344 size_t core_id = active_cores[i];
1346 os <<
"Core " << i <<
"(CID=" << core_id <<
"):\n" <<
" Call stack (" << core.
size() <<
"):\n --TOP--\n";
1347 for (
size_t k = core.
size() - 1; k < core.
size(); --k) {
1350 const State & state = core[k];
1351 os <<
" Inst ptr: " << state.
inst_ptr <<
" (";
1357 os <<
" Func ptr: " << state.
func_ptr <<
"\n";
1358 os <<
" Input memory: ";
1359 for (
auto mem : state.
input_mem) os <<
"{" << mem.first <<
":" << mem.second <<
"}"; os <<
"\n";
1360 os <<
" Local memory: ";
1361 for (
auto mem : state.
local_mem) os <<
"{" << mem.first <<
":" << mem.second <<
"}"; os <<
"\n";
1362 os <<
" Output memory: ";
1363 for (
auto mem : state.
output_mem) os <<
"{" << mem.first <<
":" << mem.second <<
"}"; os <<
"\n";
1425 if (denom == 0.0) ++hw.
errors;
1437 if (base == 0) ++hw.
errors;
1438 else state.
SetLocal(inst.
args[2], static_cast<int64_t>(num) % static_cast<int64_t>(base));
1633 if (inst_lib.
GetSize() == 0) {
1634 inst_lib.
AddInst(
"Inc",
Inst_Inc, 1,
"Increment value in local memory Arg1");
1635 inst_lib.
AddInst(
"Dec",
Inst_Dec, 1,
"Decrement value in local memory Arg1");
1636 inst_lib.
AddInst(
"Not",
Inst_Not, 1,
"Logically toggle value in local memory Arg1");
1637 inst_lib.
AddInst(
"Add",
Inst_Add, 3,
"Local memory: Arg3 = Arg1 + Arg2");
1638 inst_lib.
AddInst(
"Sub",
Inst_Sub, 3,
"Local memory: Arg3 = Arg1 - Arg2");
1639 inst_lib.
AddInst(
"Mult",
Inst_Mult, 3,
"Local memory: Arg3 = Arg1 * Arg2");
1640 inst_lib.
AddInst(
"Div",
Inst_Div, 3,
"Local memory: Arg3 = Arg1 / Arg2");
1641 inst_lib.
AddInst(
"Mod",
Inst_Mod, 3,
"Local memory: Arg3 = Arg1 % Arg2");
1651 inst_lib.
AddInst(
"Return",
Inst_Return, 0,
"Return from current function if possible.");
1652 inst_lib.
AddInst(
"SetMem",
Inst_SetMem, 2,
"Local memory: Arg1 = numerical value of Arg2");
1654 inst_lib.
AddInst(
"SwapMem",
Inst_SwapMem, 2,
"Local memory: Swap values of Arg1 and Arg2.");
1655 inst_lib.
AddInst(
"Input",
Inst_Input, 2,
"Input memory Arg1 => Local memory Arg2.");
1656 inst_lib.
AddInst(
"Output",
Inst_Output, 2,
"Local memory Arg1 => Output memory Arg2.");
1657 inst_lib.
AddInst(
"Commit",
Inst_Commit, 2,
"Local memory Arg1 => Shared memory Arg2.");
1658 inst_lib.
AddInst(
"Pull",
Inst_Pull, 2,
"Shared memory Arg1 => Shared memory Arg2.");
1680 if (event_lib.
GetSize() == 0) {
Instruction(size_t _id=0, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &_aff=affinity_t())
Definition: EventDrivenGP.h:271
static std::string string_get_range(const std::string &in_string, std::size_t start_pos, std::size_t end_pos)
Get a segment from the beginning of a string as another string, leaving original untouched.
Definition: string_utils.h:295
Program(Ptr< const inst_lib_t > _ilib, const program_t &_prgm=program_t())
Definition: EventDrivenGP.h:361
void SingleProcess()
Advance hardware by single instruction.
Definition: EventDrivenGP.h:1217
void SetInst(size_t fID, size_t pos, size_t id, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &aff=affinity_t())
Definition: EventDrivenGP.h:441
static const std::string & empty_string()
Definition: string_utils.h:29
BlockType
Definition: EventDrivenGP.h:144
bool ValidFunction(size_t fID) const
Definition: EventDrivenGP.h:392
iterator insert(ARGS &&...args)
Definition: vector.h:201
static void Inst_TestLess(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1460
void QueueEvent(const std::string &name, const affinity_t &affinity=affinity_t(), const memory_t &msg=memory_t(), const properties_t &properties=properties_t())
Queue event by name.
Definition: EventDrivenGP.h:1204
Random & GetRandom()
Get reference to random number generator used by this hardware.
Definition: EventDrivenGP.h:786
size_t GetInstCnt() const
Get the total number of instructions across all functions that make up this program.
Definition: EventDrivenGP.h:381
const Function & operator[](size_t id) const
Definition: EventDrivenGP.h:368
size_t GetNumErrors() const
Get current number of errors committed by this hardware.
Definition: EventDrivenGP.h:812
std::function< void(EventDrivenGP_t &, const event_t &)> fun_event_handler_t
Event handler function type alias.
Definition: EventDrivenGP.h:611
emp::vector< exec_stk_t > & GetCores()
Definition: EventDrivenGP.h:833
static void Inst_Mod(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1433
Ptr< const inst_lib_t > GetInstLib() const
Definition: EventDrivenGP.h:387
static void Inst_Break(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1522
size_t id
Definition: EventDrivenGP.h:267
bool is_main
Definition: EventDrivenGP.h:168
void PrintInst(const inst_t &inst, std::ostream &os=std::cout)
Print out a single instruction with its arguments.
Definition: EventDrivenGP.h:532
static void Inst_While(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1486
void Set(size_t index, bool value)
Set the bit as a specified index.
Definition: BitSet.h:231
const program_t & GetConstProgram() const
Get program loaded on this hardware.
Definition: EventDrivenGP.h:792
size_t begin
Definition: EventDrivenGP.h:148
inst_seq_t inst_seq
Definition: EventDrivenGP.h:311
This file maintains information about instructions availabel in virtual hardware. ...
static void Inst_SetMem(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1550
bool IsMain() const
Is this a main state?
Definition: EventDrivenGP.h:207
bool operator<(const Program &other) const
Definition: EventDrivenGP.h:373
void SetMaxCores(size_t val)
Definition: EventDrivenGP.h:877
static constexpr size_t DEFAULT_MAX_CALL_DEPTH
Definition: EventDrivenGP.h:114
void PrintState(std::ostream &os=std::cout)
Print out current state (full) of virtual hardware using given output stream (default = std::cout)...
Definition: EventDrivenGP.h:1323
void CallFunction(size_t fID)
Definition: EventDrivenGP.h:1140
void CallFunction(const affinity_t &affinity, double threshold)
Definition: EventDrivenGP.h:1126
void SetMinBindThresh(double val)
Definition: EventDrivenGP.h:866
void NewRandom(int seed=-1)
Generate new random number generator for this hardware object with the given seed value...
Definition: EventDrivenGP.h:1032
void SetTrait(size_t id, double val)
Definition: EventDrivenGP.h:951
static void Inst_Nop(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1608
void QueueEvent(size_t id, const affinity_t &affinity=affinity_t(), const memory_t &msg=memory_t(), const properties_t &properties=properties_t())
Queue event by id.
Definition: EventDrivenGP.h:1211
void AddEvent(const std::string &name, const fun_t &handler_fun, const std::string &desc="", const properties_t &event_properties=properties_t())
Add a new event to the event library.
Definition: EventLib.h:86
void Delete()
Definition: Ptr.h:737
program_t program
Definition: EventDrivenGP.h:617
bool ValidPosition(size_t fID, size_t pos) const
Definition: EventDrivenGP.h:389
static void Inst_Inc(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1373
size_t GetCurCoreID()
Definition: EventDrivenGP.h:838
size_t GetIP() const
Get instruction pointer.
Definition: EventDrivenGP.h:189
emp::vector< size_t > FindBestFuncMatch(const affinity_t &affinity, double threshold)
Find best matching functions (by ID) given affinity.
Definition: EventDrivenGP.h:1110
void SetProgram(const program_t &_program)
Definition: EventDrivenGP.h:394
mem_val_t & AccessLocal(mem_key_t key)
Definition: EventDrivenGP.h:241
affinity_t affinity
Definition: EventDrivenGP.h:269
void SpawnCore(const affinity_t &affinity, double threshold, const memory_t &input_mem=memory_t(), bool is_main=false)
Definition: EventDrivenGP.h:746
void SetInput(mem_key_t key, mem_val_t value)
Set input memory specified by key to value.
Definition: EventDrivenGP.h:234
static void Inst_Close(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1529
static void remove_whitespace(std::string &in_string)
Remove all whitespace from anywhere within a string.
Definition: string_utils.h:392
void SetOutput(mem_key_t key, mem_val_t value)
Set output memory specified by key to value.
Definition: EventDrivenGP.h:237
affinity_t & GetAffinity()
Definition: EventDrivenGP.h:330
void Set(size_t _id, arg_t _a0=0, arg_t _a1=0, arg_t _a2=0, const affinity_t &_aff=affinity_t())
Definition: EventDrivenGP.h:279
double min_bind_thresh
Definition: EventDrivenGP.h:625
mem_val_t & AccessShared(mem_key_t key)
Definition: EventDrivenGP.h:858
size_t GetSize() const
Get the number of instructions in this set.
Definition: InstLib.h:99
static void Inst_Input(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1576
void PrintTraits(std::ostream &os=std::cout)
Print hardware traits using given output stream (default = std::cout).
Definition: EventDrivenGP.h:1304
static void Inst_Add(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1397
const inst_t & GetInst(size_t fID, size_t pos) const
Get reference to particular instruction in hardware's program given function ID and instruction posit...
Definition: EventDrivenGP.h:803
void TriggerEvent(size_t id, const affinity_t &affinity=affinity_t(), const memory_t &msg=memory_t(), const properties_t &properties=properties_t())
Trigger an event (from this hardware).
Definition: EventDrivenGP.h:1194
void PrintProgram(std::ostream &os=std::cout)
Print out entire program.
Definition: EventDrivenGP.h:555
void SetMaxCallDepth(size_t val)
Definition: EventDrivenGP.h:934
static constexpr size_t DEFAULT_MAX_CORES
Definition: EventDrivenGP.h:113
mem_val_t GetShared(mem_key_t key) const
Definition: EventDrivenGP.h:854
double mem_val_t
Definition: EventDrivenGP.h:105
~EventDrivenGP_AW()
Definition: EventDrivenGP.h:710
std::deque< event_t > event_queue
Definition: EventDrivenGP.h:619
emp::vector< Block > block_stack
Definition: EventDrivenGP.h:167
A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).
Definition: ce_random.h:52
EventDrivenGP_AW(EventDrivenGP_t &&in)
Definition: EventDrivenGP.h:670
static void Inst_Div(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1422
std::string string_get_word(const std::string &in_string, size_t start_pos=0)
Return a prefix of a string, up to the first whitespace (do not modify the original string) ...
Definition: string_utils.h:332
affinity_t affinity
Definition: EventDrivenGP.h:310
double default_mem_value
Definition: EventDrivenGP.h:624
memory_t & GetInputMemory()
Get a reference to the input memory map for this state.
Definition: EventDrivenGP.h:213
void Reset()
Definition: EventDrivenGP.h:717
void OpenBlock(size_t begin, size_t end, BlockType type)
Open a block in the current local program state as specified by begin, end, and type.
Definition: EventDrivenGP.h:1093
static void slice(const std::string &in_string, emp::vector< std::string > &out_set, char delim='\n')
Cut up a string based on the provided delimitor; fill them in to the provided vector.
Definition: string_utils.h:421
static void Inst_Countdown(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1503
void SetInst(size_t fID, size_t pos, size_t id, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &aff=affinity_t())
Shortcut to this hardware object's program's SetInst function of the same signature.
Definition: EventDrivenGP.h:980
static void Inst_Pull(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1600
static constexpr size_t GetSize()
How many bits are in this BitSet?
Definition: BitSet.h:220
static Ptr< const EventLib< EventDrivenGP_t > > DefaultEventLib()
Definition: EventDrivenGP.h:1678
mem_val_t GetLocal(mem_key_t key) const
Definition: EventDrivenGP.h:220
size_t id
Definition: EventDrivenGP.h:121
void push_back(PB_Ts &&...args)
Definition: vector.h:189
std::unordered_map< mem_key_t, mem_val_t > memory_t
Definition: EventDrivenGP.h:106
void PushTrait(double val)
Push a trait onto end of traits vector.
Definition: EventDrivenGP.h:971
memory_t input_mem
Definition: EventDrivenGP.h:161
mem_val_t GetOutput(mem_key_t key) const
Definition: EventDrivenGP.h:228
void ReturnFunction()
Definition: EventDrivenGP.h:1159
size_t errors
Definition: EventDrivenGP.h:621
EventDrivenGP_AW(inst_lib_t &_ilib, event_lib_t &_elib, Ptr< Random > rnd=nullptr)
Definition: EventDrivenGP.h:661
arg_set_t args
Definition: EventDrivenGP.h:268
bool IsStochasticFunCall() const
Definition: EventDrivenGP.h:829
EventDrivenGP_AW(Ptr< const event_lib_t > _elib, Ptr< Random > rnd=nullptr)
Definition: EventDrivenGP.h:664
memory_t local_mem
Definition: EventDrivenGP.h:160
size_t GetFP() const
Get function pointer.
Definition: EventDrivenGP.h:186
A linear GP (inspired by AvidaGP) virtual hardware CPU that supports an event-driven programming para...
Definition: EventDrivenGP.h:98
void SetInst(size_t pos, size_t id, arg_t a0, arg_t a1, arg_t a2, const affinity_t &aff)
Definition: EventDrivenGP.h:340
EventDrivenGP_AW(Ptr< Random > rnd=nullptr)
Definition: EventDrivenGP.h:667
size_t GetSize() const
Get the number of events registered to this event library.
Definition: EventLib.h:77
size_t size() const
Definition: vector.h:151
Definition: EventLib.h:21
static void Inst_Return(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1543
static void Inst_Sub(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1405
mem_val_t GetDefaultMemValue() const
Get the default memory value for local/shared/input/output memory maps.
Definition: EventDrivenGP.h:825
void QueueEvent(const event_t &event)
Queue an event (to be handled by this hardware).
Definition: EventDrivenGP.h:1201
void emplace_back(ARGS &&...args)
Definition: vector.h:219
static void Inst_TestEqu(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1444
mem_val_t & AccessOutput(mem_key_t key)
Definition: EventDrivenGP.h:255
bool is_executing
Definition: EventDrivenGP.h:632
void ProcessInst(const inst_t &inst)
Process a single instruction, provided by the caller.
Definition: EventDrivenGP.h:1177
void SetInst(size_t fID, size_t pos, const inst_t &inst)
Shortcut to this hardware object's program's SetInst function of the same signature.
Definition: EventDrivenGP.h:974
memory_t & GetOutputMemory()
Get a reference to the output memory map for this state.
Definition: EventDrivenGP.h:216
static Ptr< const InstLib< EventDrivenGP_t > > DefaultInstLib()
Get a pointer to const default instruction library. Will only construct the default instruction libra...
Definition: EventDrivenGP.h:1631
void SetIP(size_t ip)
Set instruction pointer to given value, ip.
Definition: EventDrivenGP.h:195
std::unordered_set< std::string > properties_t
Definition: EventDrivenGP.h:110
Program program_t
Definition: EventDrivenGP.h:608
static void Inst_Dec(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1381
bool operator!=(const Function &in) const
Definition: EventDrivenGP.h:322
bool operator!=(const Instruction &in) const
Definition: EventDrivenGP.h:291
static void Inst_Call(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1536
void PushFunction(const affinity_t &_aff=affinity_t(), const inst_seq_t &_seq=inst_seq_t())
Shortcut to this hardware object's program's PushFunction operation of the same signature.
Definition: EventDrivenGP.h:993
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)
Definition: map_utils.h:29
static void HandleEvent_Message(EventDrivenGP_t &hw, const event_t &event)
Definition: EventDrivenGP.h:1670
void Process(size_t num_inst)
Advance hardware by some arbitrary number instructions.
Definition: EventDrivenGP.h:1284
void PrintInst(const inst_t &inst, std::ostream &os=std::cout)
Print given instruction using given output stream (default = std::cout).
Definition: EventDrivenGP.h:1299
static void Inst_Commit(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1592
static constexpr size_t MAX_INST_ARGS
Maximum number of instruction arguments. Currently hardcoded. At some point, will make flexible...
Definition: EventDrivenGP.h:101
Function & operator[](size_t id)
Definition: EventDrivenGP.h:367
static void Inst_If(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1468
void PrintProgram(std::ostream &os=std::cout)
Print out entire program using given output stream (default = std::cout).
Definition: EventDrivenGP.h:1313
EventDrivenGP_AW(Ptr< const inst_lib_t > _ilib, Ptr< const event_lib_t > _elib, Ptr< Random > rnd=nullptr)
Definition: EventDrivenGP.h:639
size_t func_ptr
Definition: EventDrivenGP.h:165
void pop_back()
Definition: vector.h:194
int arg_t
Definition: EventDrivenGP.h:107
void PushInst(const inst_t &inst, int fID=-1)
Definition: EventDrivenGP.h:1017
size_t GetMaxCallDepth() const
Get the maximum call depth allowed on this hardware object (max call states allowed on a single core'...
Definition: EventDrivenGP.h:822
void BreakBlock()
Definition: EventDrivenGP.h:1100
bool ValidFunction(size_t fID) const
Is the function given by function ID (fID) a valid function in this hardware object's program...
Definition: EventDrivenGP.h:1044
Ptr< const event_lib_t > event_lib
Definition: EventDrivenGP.h:614
properties_t properties
Definition: EventDrivenGP.h:124
bool operator!=(const Program &in) const
Definition: EventDrivenGP.h:371
program_t program
Definition: EventDrivenGP.h:359
bool HasProperty(std::string property) const
Does event object have given property?
Definition: EventDrivenGP.h:136
static constexpr double DEFAULT_MIN_BIND_THRESH
Definition: EventDrivenGP.h:116
void PushInst(size_t id, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &aff=affinity_t(), int fID=-1)
Definition: EventDrivenGP.h:404
void Clear()
Definition: EventDrivenGP.h:365
void SetStochasticFunCall(bool val)
Definition: EventDrivenGP.h:947
static std::string to_lower(std::string value)
Convert a string to all lowercase.
Definition: string_utils.h:132
int mem_key_t
Definition: EventDrivenGP.h:104
static void Inst_BroadcastMsg(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1615
bool operator<(const Function &other) const
Definition: EventDrivenGP.h:324
void Print(std::ostream &out=std::cout) const
Print all bits to the provided output stream.
Definition: BitSet.h:333
void SetShared(mem_key_t key, mem_val_t value)
Set given shared memory map location (key) to given value.
Definition: EventDrivenGP.h:1047
void PushInst(const inst_t &inst, int fID=-1)
Definition: EventDrivenGP.h:433
void PushFunction(const Function &_function)
Shortcut to this hardware object's program's PushFunction operation of the same signature.
Definition: EventDrivenGP.h:990
void Reset()
Reset state object.
Definition: EventDrivenGP.h:177
memory_t output_mem
Definition: EventDrivenGP.h:162
constexpr T Max(T in1)
Max of only one element is that element itself!
Definition: math.h:61
size_t end
Definition: EventDrivenGP.h:149
size_t max_call_depth
Definition: EventDrivenGP.h:623
void SpawnCore(size_t fID, const memory_t &input_mem=memory_t(), bool is_main=false)
Definition: EventDrivenGP.h:760
size_t GetSize() const
Definition: EventDrivenGP.h:328
const Function & GetFunction(size_t fID) const
Get reference to a particular function in hardware's program.
Definition: EventDrivenGP.h:797
void Load(std::istream &input)
Definition: EventDrivenGP.h:461
Ptr< Random > GetRandomPtr()
Get pointer to random number generator used by this hardware.
Definition: EventDrivenGP.h:789
void Load(std::istream &input)
Definition: EventDrivenGP.h:1028
bool Has(const MAP_T &in_map, const KEY_T &key)
Take any map type, and run find to determine if a key is present.
Definition: map_utils.h:21
bool is_digit(char test_char)
Determine if a character is a digit.
Definition: string_utils.h:185
double GetTrait(size_t id) const
Get a particular trait given its ID.
Definition: EventDrivenGP.h:809
State & GetCurState()
Get a reference to the current local call state.
Definition: EventDrivenGP.h:844
void PrintInstFull(const inst_t &inst, std::ostream &os=std::cout)
Fully print out a single instruction with its arguments/affinity.
Definition: EventDrivenGP.h:544
size_t inst_ptr
Definition: EventDrivenGP.h:166
void PushInst(size_t id, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &aff=affinity_t(), int fID=-1)
Definition: EventDrivenGP.h:1001
emp::vector< size_t > inactive_cores
Definition: EventDrivenGP.h:629
void resize(size_t new_size)
Definition: vector.h:161
BitSet< AFFINITY_WIDTH > affinity_t
Definition: EventDrivenGP.h:109
Block(size_t _begin=0, size_t _end=0, BlockType _type=BlockType::BASIC)
Definition: EventDrivenGP.h:152
memory_t & GetLocalMemory()
Get a reference to the local memory map for this state.
Definition: EventDrivenGP.h:210
emp::vector< size_t > active_cores
Definition: EventDrivenGP.h:628
void TriggerEvent(const std::string &name, const affinity_t &affinity=affinity_t(), const memory_t &msg=memory_t(), const properties_t &properties=properties_t())
Trigger an event (from this hardware).
Definition: EventDrivenGP.h:1186
Definition: EventDrivenGP.h:309
bool operator==(const Function &in) const
Definition: EventDrivenGP.h:319
size_t exec_core_id
Definition: EventDrivenGP.h:631
double default_mem_val
Definition: EventDrivenGP.h:163
static constexpr mem_val_t DEFAULT_MEM_VALUE
Definition: EventDrivenGP.h:115
void SetDefaultMemValue(mem_val_t val)
Set default memory value to given value, val.
Definition: EventDrivenGP.h:201
void IncTrait(size_t id, double inc=1.0)
Definition: EventDrivenGP.h:958
void PushFunction(const Function &_function)
Definition: EventDrivenGP.h:396
iterator begin() noexcept
Definition: vector.h:153
size_t FindEndOfBlock(size_t fp, size_t ip)
Definition: EventDrivenGP.h:1051
program_t & GetProgram()
Definition: EventDrivenGP.h:793
void DecTrait(size_t id, double dec=1.0)
Definition: EventDrivenGP.h:965
exec_stk_t & GetCurCore()
Get a reference to the current core/execution stack.
Definition: EventDrivenGP.h:841
Definition: EventDrivenGP.h:120
double SimpleMatchCoeff(const BitSet< NUM_BITS > &in1, const BitSet< NUM_BITS > &in2)
Computes simple matching coefficient (https://en.wikipedia.org/wiki/Simple_matching_coefficient).
Definition: BitSet.h:595
If we are in emscripten, make sure to include the header.
Definition: array.h:37
static void Inst_SwapMem(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1566
void PushInst(const std::string &name, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &aff=affinity_t(), int fID=-1)
Definition: EventDrivenGP.h:1009
void ResetHardware()
Definition: EventDrivenGP.h:726
Definition: EventDrivenGP.h:266
static void Inst_Mult(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1413
static void Inst_TestNEqu(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1452
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
Ptr< Random > random_ptr
Definition: EventDrivenGP.h:615
void AdvanceIP(size_t inc=1)
Advance instruction pointer by amount given by inc.
Definition: EventDrivenGP.h:204
void AddInst(const std::string &name, const fun_t &fun_call, size_t num_args=0, const std::string &desc="", ScopeType scope_type=ScopeType::NONE, size_t scope_arg=(size_t)-1, const inst_properties_t &inst_properties=inst_properties_t())
Add a new instruction to the set.
Definition: InstLib.h:140
memory_t & GetSharedMem()
Get a reference to hardware's shared memory map.
Definition: EventDrivenGP.h:850
size_t GetMaxCores() const
Get the maximum number of cores allowed to run simultaneously on this hardware object.
Definition: EventDrivenGP.h:819
static void Inst_CopyMem(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1558
memory_t shared_mem
Definition: EventDrivenGP.h:618
#define emp_assert(...)
Definition: assert.h:199
InstLib maintains a set of instructions for use in virtual hardware.
Definition: InstLib.h:34
T & back()
Definition: vector.h:183
mem_val_t GetDefaultMemValue() const
Get default memory value.
Definition: EventDrivenGP.h:192
affinity_t affinity
Definition: EventDrivenGP.h:122
Definition: EventDrivenGP.h:355
memory_t msg
Definition: EventDrivenGP.h:123
void SetLocal(mem_key_t key, mem_val_t value)
Set local memory specified by key to value.
Definition: EventDrivenGP.h:231
Event(size_t _id=0, const affinity_t &aff=affinity_t(), const memory_t &_msg=memory_t(), const properties_t &_properties=properties_t())
Definition: EventDrivenGP.h:126
emp::vector< exec_stk_t > cores
Definition: EventDrivenGP.h:627
Ptr< const inst_lib_t > inst_lib
Definition: EventDrivenGP.h:358
emp::vector< inst_t > inst_seq_t
Definition: EventDrivenGP.h:300
Event & operator=(const Event &)=default
EventDrivenGP_AW(const EventDrivenGP_t &in)
Definition: EventDrivenGP.h:691
void CloseBlock()
Definition: EventDrivenGP.h:1074
void New(T &&...args)
Definition: Ptr.h:735
void TriggerEvent(const event_t &event)
Trigger an event (from this hardware).
Definition: EventDrivenGP.h:1183
inst_t & operator[](size_t id)
Definition: EventDrivenGP.h:316
Ptr< const event_lib_t > GetEventLib() const
Get event library associated with hardware.
Definition: EventDrivenGP.h:783
void PrintEvent(const event_t &event, std::ostream &os=std::cout)
Print given event using given output stream (default = std::cout).
Definition: EventDrivenGP.h:1290
Ptr< const inst_lib_t > GetInstLib() const
Get instruction library associated with hardware's program.
Definition: EventDrivenGP.h:780
static void Inst_Output(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1584
bool random_owner
Definition: EventDrivenGP.h:616
void PrintProgramFull(std::ostream &os=std::cout)
Print out entire program.
Definition: EventDrivenGP.h:581
bool operator==(const Instruction &in) const
Definition: EventDrivenGP.h:288
void SetFP(size_t fp)
Set function pointer to given value, fp.
Definition: EventDrivenGP.h:198
void HandleEvent(const event_t &event)
Handle an event (on this hardware).
Definition: EventDrivenGP.h:1180
void PushInst(size_t id, arg_t a0, arg_t a1, arg_t a2, const affinity_t &aff)
Definition: EventDrivenGP.h:332
void PushInst(const std::string &name, arg_t a0=0, arg_t a1=0, arg_t a2=0, const affinity_t &aff=affinity_t(), int fID=-1)
Definition: EventDrivenGP.h:418
mem_val_t & AccessInput(mem_key_t key)
Definition: EventDrivenGP.h:248
emp::vector< double > traits
Definition: EventDrivenGP.h:620
double GetMinBindThresh() const
Definition: EventDrivenGP.h:816
size_t max_cores
Definition: EventDrivenGP.h:622
void SetInst(size_t fID, size_t pos, const inst_t &inst)
Definition: EventDrivenGP.h:447
std::deque< size_t > pending_cores
Definition: EventDrivenGP.h:630
void PrintProgramFull(std::ostream &os=std::cout)
Print out entire program using given output stream (default = std::cout).
Definition: EventDrivenGP.h:1318
void Set(const Instruction &other)
Definition: EventDrivenGP.h:282
mem_val_t GetInput(mem_key_t key) const
Definition: EventDrivenGP.h:224
static void Inst_SendMsg(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1625
bool stochastic_fun_call
Definition: EventDrivenGP.h:626
State(mem_val_t _default_mem_val=0.0, bool _is_main=false)
Definition: EventDrivenGP.h:170
size_t GetSize() const
Get number of functions that make up this program.
Definition: EventDrivenGP.h:378
void SetInst(size_t pos, const inst_t &inst)
Definition: EventDrivenGP.h:344
bool ValidPosition(size_t fID, size_t pos) const
Definition: EventDrivenGP.h:1041
constexpr size_t GetSize(T(&)[N])
Determine the size of a built-in array.
Definition: functions.h:81
void SetDefaultMemValue(mem_val_t val)
Configure the default memory value.
Definition: EventDrivenGP.h:937
void SetProgram(const program_t &_program)
Set program for this hardware object.
Definition: EventDrivenGP.h:987
static void Inst_Not(EventDrivenGP_t &hw, const inst_t &inst)
Definition: EventDrivenGP.h:1389
Function(const affinity_t &_aff=affinity_t(), const inst_seq_t &_seq=inst_seq_t())
Definition: EventDrivenGP.h:313
bool operator<(const Instruction &other) const
Definition: EventDrivenGP.h:293
void PushFunction(const affinity_t &_aff=affinity_t(), const inst_seq_t &_seq=inst_seq_t())
Definition: EventDrivenGP.h:397
bool is_valid(char test_char)
If no functions are provided to is_valid(), always return false as base case.
Definition: string_utils.h:261
void PushInst(const inst_t &inst)
Definition: EventDrivenGP.h:336
bool operator==(const Program &in) const
Definition: EventDrivenGP.h:370
BlockType type
Definition: EventDrivenGP.h:150
Definition: EventDrivenGP.h:147
Definition: EventDrivenGP.h:159
const inst_t & operator[](size_t id) const
Definition: EventDrivenGP.h:317