ProcessorStandard.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "ProcessorStandard.h"
  2. namespace mdd {
  3. ProcessorStandard::ProcessorStandard()
  4. :ProcessorBase(R"JSON(
  5. [{
  6. "name":"priority",
  7. "value":"manual",
  8. "options": [
  9. "manual",
  10. "static",
  11. "dynamic",
  12. "time"
  13. ]
  14. },{
  15. "name":"iterations",
  16. "value": -1
  17. }
  18. ])JSON")
  19. {
  20. _priorityEvaluation = MANUAL;
  21. _maxIterations = -1;
  22. key = "StandardProcessor";
  23. setName(key);
  24. processor_outputs.push_back(std::make_shared<Output>(this, "Iterator", 0, std::vector<double>{0}));
  25. }
  26. bool ProcessorStandard::configure(const std::string& config) {
  27. json config_parsed = json::parse(config);
  28. bool found = false;
  29. for (size_t i = 0; i < config_parsed.size(); i++)
  30. {
  31. if (config_parsed[i].contains("name"))
  32. {
  33. if (config_parsed[i]["name"].get<std::string>() == "priority")
  34. {
  35. std::string op = config_parsed[i]["value"].get<std::string>();
  36. found = true;
  37. if (op == "manual") {
  38. _priorityEvaluation = MANUAL;
  39. break;
  40. }
  41. else if (op == "static") {
  42. _priorityEvaluation = STATIC;
  43. }
  44. else if (op == "dynamic") {
  45. _priorityEvaluation = DYNAMIC;
  46. }
  47. else if (op == "time") {
  48. _priorityEvaluation = TIME;
  49. }
  50. else {
  51. found = false;
  52. }
  53. }
  54. if (config_parsed[i]["name"].get<std::string>() == "iterations")
  55. {
  56. _maxIterations = config_parsed[i]["value"].get<int>();
  57. found = true;
  58. }
  59. }
  60. }
  61. return found;
  62. }
  63. std::string ProcessorStandard::addModule(std::shared_ptr<IModule> module)
  64. {
  65. std::string id = ProcessorBase::addModule(module);
  66. //_priority_list.emplace_back(module);
  67. return id;
  68. }
  69. void ProcessorStandard::removeModule(std::shared_ptr<IModule> module)
  70. {
  71. ProcessorBase::removeModule(module);
  72. //for (auto it = _priority_list.begin(); it != _priority_list.end(); ++it) {
  73. // if (it->module_ptr == module)
  74. // {
  75. // _priority_list.erase(it);
  76. // return;
  77. // }
  78. //}
  79. }
  80. std::vector<std::shared_ptr<IModule>> ProcessorStandard::getModulePriority()
  81. {
  82. std::vector <std::shared_ptr<IModule>> ret;
  83. for (auto& p : _priority_list) {
  84. ret.push_back(p.module_ptr);
  85. }
  86. return ret;
  87. }
  88. state ProcessorStandard::update() {
  89. _priority_list.clear();
  90. for (size_t i = 0; i < inputs.size(); i++)
  91. {
  92. _priority_list.emplace_back(inputs[i]);
  93. }
  94. for (size_t i = 0; i < modules.size(); i++)
  95. {
  96. _priority_list.emplace_back(modules[i]);
  97. }
  98. for (size_t i = 0; i < outputs.size(); i++)
  99. {
  100. _priority_list.emplace_back(outputs[i]);
  101. }
  102. if (_priorityEvaluation != MANUAL)
  103. {
  104. //update priorities
  105. for (auto it = _priority_list.begin(); it != _priority_list.end(); ++it) {
  106. //collect connected inputs
  107. it->inputCounter = 0;
  108. for (size_t i = 0; i < it->module_ptr->getNumInputs(); ++i) {
  109. if (it->module_ptr->getInput(i)->getConnection() != nullptr)
  110. {
  111. ++(it->inputCounter);
  112. }
  113. }
  114. //collect connected outputs
  115. for (size_t i = 0; i < it->module_ptr->getNumOutputs(); ++i) {
  116. if (!it->module_ptr->getOutput(i)->getConnections().empty())
  117. {
  118. ++(it->outputCounter);
  119. }
  120. }
  121. }
  122. //sort by connections
  123. std::sort(_priority_list.begin(), _priority_list.end(), [](module_priority a, module_priority b) {
  124. if (a.inputCounter == b.inputCounter)
  125. {
  126. return a.outputCounter > b.outputCounter;
  127. }
  128. return a.inputCounter < b.inputCounter;
  129. });
  130. }
  131. typedef std::chrono::high_resolution_clock Time;
  132. //update
  133. state ret = state::UNCHANGED;
  134. state group_state = state::CHANGED;
  135. size_t restart = 0;
  136. while (group_state == state::CHANGED) {
  137. processor_outputs[0]->setValue()[0] += 1;
  138. processor_outputs[0]->resetState();
  139. group_state = state::UNCHANGED;
  140. for (int i = restart; i < _priority_list.size(); ++i) {
  141. auto t_start = Time::now();
  142. state module_state = _priority_list[i].module_ptr->update();
  143. auto t_end = Time::now();
  144. if (_priorityEvaluation == TIME)
  145. {
  146. _priority_list[i].time_priority = std::round(std::log10(std::chrono::duration_cast<std::chrono::milliseconds>(t_end - t_start).count()));
  147. }
  148. if (module_state == state::CHANGED) {
  149. group_state = state::CHANGED;
  150. ret = state::CHANGED;
  151. }
  152. // ignore modules which have to initilize once in the future
  153. if (_priority_list[i].inputCounter == 0 && _priorityEvaluation != MANUAL) {
  154. restart = i;
  155. }
  156. if ((_priorityEvaluation == DYNAMIC || _priorityEvaluation == TIME) && _priority_list.size() < i+1) {
  157. if (_priority_list[i + 1].inputCounter == _priority_list[i].inputCounter && _priority_list[i + 1].inputCounter != 0)
  158. {
  159. //collect changes
  160. for (auto it = _priority_list.begin() + i + 1; it != _priority_list.end(); ++it) {
  161. it->changeCounter = 0;
  162. for (size_t i = 0; i < it->module_ptr->getNumInputs(); ++i) {
  163. if (it->module_ptr->getInput(i)->getState() == state::CHANGED)
  164. {
  165. ++(it->changeCounter);
  166. }
  167. }
  168. }
  169. std::sort(_priority_list.begin() + i + 1, _priority_list.end(), [](module_priority a, module_priority b) {
  170. if (a.inputCounter == b.inputCounter)
  171. {
  172. if (a.time_priority == b.time_priority)
  173. {
  174. if ((a.inputCounter - a.changeCounter) == (b.inputCounter - b.changeCounter)) {
  175. if (a.changeCounter == b.changeCounter) {
  176. return a.outputCounter > b.outputCounter;
  177. }
  178. return a.changeCounter > b.changeCounter;
  179. }
  180. return (a.inputCounter - a.changeCounter) < (b.inputCounter - b.changeCounter);
  181. }
  182. return a.time_priority < b.time_priority;
  183. }
  184. return a.inputCounter < b.inputCounter;
  185. });
  186. }
  187. }
  188. }
  189. if (_maxIterations != -1 && processor_outputs[0]->getValue()[0] >= _maxIterations)
  190. {
  191. return state::STATE_ERROR;
  192. }
  193. }
  194. return ret;
  195. }
  196. std::shared_ptr<IOutput> ProcessorStandard::getIteration(){
  197. return processor_outputs[0];
  198. }
  199. ProcessorStandard::module_priority::module_priority(std::shared_ptr<IModule> module, size_t inputs, size_t outputs, size_t change)
  200. {
  201. module_ptr = module;
  202. inputCounter = inputs;
  203. outputCounter = outputs;
  204. changeCounter = change;
  205. time_priority = 0;
  206. }
  207. }
  208. //*/