simloop.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "sys.hpp" // for libcwd
  2. #include "debug.hpp" // for libcwd
  3. #include "simloop.hpp"
  4. #include "simelement.hpp"
  5. #include "layer.hpp"
  6. #include "connection.hpp"
  7. #include "normalize.hpp"
  8. #include "filesystem.hpp"
  9. #include <signal.h>
  10. /** Signal-Handler
  11. */
  12. void ProcessSignal(int SigNum)
  13. {
  14. switch (SigNum) {
  15. case SIGUSR1:
  16. Dout(dc::simloop, "received custom signal SIGUSR1 ");
  17. GetGlobalSimLoop()->ProcessSignalUsr1();
  18. break;
  19. case SIGUSR2:
  20. Dout(dc::simloop, "received custom signal SIGUSR2 ");
  21. break;
  22. }
  23. }
  24. SimLoop* GlobalMainSimLoop=0;
  25. SimLoop* InitLibCSim(int _MacroTimeStep, float _DeltaT)
  26. {
  27. GlobalMainSimLoop = new SimLoop(_MacroTimeStep, _DeltaT);
  28. return GlobalMainSimLoop;
  29. }
  30. void deleteGlobalSimLoop()
  31. {
  32. delete GlobalMainSimLoop;
  33. GlobalMainSimLoop=0;
  34. }
  35. SimLoop* GetGlobalSimLoop()
  36. {
  37. if (GlobalMainSimLoop) {
  38. return GlobalMainSimLoop;
  39. } else {
  40. return InitLibCSim();
  41. }
  42. }
  43. ///////////////////////////////
  44. SimLoop::SimLoop(int _MacroTimeStep, float _DeltaT)
  45. :MacroTimeStep(_MacroTimeStep),
  46. DeltaT(_DeltaT),
  47. ElementCounter(0),
  48. MaximumDelay(0)
  49. {
  50. Dout(dc::simloop, "SimLoop Default Constructor");
  51. SimElementList = new vector<SimElement*>;
  52. ConnectionList = new TConnectionList;
  53. NormList = new TNormList;
  54. DataDirectory="";
  55. // DeltaT=0.25;
  56. Dout(dc::simloop, " One time step is DeltaT = " << DeltaT << " ms");
  57. if (signal(SIGUSR1, ProcessSignal) == SIG_ERR) {
  58. cerr << "ERROR connecting signal to handler\n";
  59. } else {
  60. Dout(dc::simloop, "connected signal to handler");
  61. };
  62. }
  63. /**
  64. * @brief element is added to SimLoop, and SimLoop takes ownership of the SimElement.
  65. * SimLoop is therefore responsible for deleting the SimElements
  66. *
  67. * @param element
  68. * @return
  69. */
  70. int SimLoop::AddSimElement(SimElement* element)
  71. {
  72. (*SimElementList).push_back(element);
  73. element->IdNumber = ++ElementCounter;
  74. Dout(dc::simloop, "add SimElement");
  75. element->Hallo();
  76. }
  77. int SimLoop::AddSimElement(layer* element)
  78. {
  79. SimElement* NewElement = static_cast<SimElement*>(element);
  80. AddSimElement(NewElement);
  81. Dout(dc::simloop, " new SimElement is a Layer ");
  82. }
  83. int SimLoop::AddSimElement(Connection* element)
  84. {
  85. (*ConnectionList).push_back(element);
  86. SimElement* NewElement = static_cast<SimElement*>(element);
  87. AddSimElement(NewElement);
  88. Dout(dc::simloop, " new SimElement is a Connection ");
  89. }
  90. int SimLoop::AddSimElement(AbstractNormalize* element)
  91. {
  92. (*NormList).push_back(element);
  93. SimElement* NewElement = static_cast<SimElement*>(element);
  94. AddSimElement(NewElement);
  95. Dout(dc::simloop, " new SimElement is a Normalization Object");
  96. }
  97. void SimLoop::Hallo()
  98. {
  99. Dout(dc::simloop, "This is SimLoop::Hallo()");
  100. Debug( libcw_do.inc_indent(2) );
  101. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  102. {
  103. (*it)->Hallo();
  104. }
  105. Debug( libcw_do.dec_indent(2) );
  106. Dout(dc::simloop, "DataDirectory=" << DataDirectory << " MacroTimeStep=" << MacroTimeStep);
  107. }
  108. void SimLoop::SetSimTag(const char* _tag)
  109. {
  110. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  111. {
  112. (*it)->SetSimTag(_tag);
  113. }
  114. }
  115. int SimLoop::proceede(int TotalTime)
  116. {
  117. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  118. {
  119. if ((*it)->On()) (*it)->proceede(TotalTime);
  120. }
  121. }
  122. int SimLoop::reset(int t)
  123. {
  124. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  125. {
  126. if ((*it)->On() && (*it)->Resetable()) (*it)->reset(t);
  127. }
  128. }
  129. void SimLoop::SetParameter(ParaType p, double value)
  130. {
  131. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  132. {
  133. (*it)->SetParameter(p, value);
  134. }
  135. }
  136. void SimLoop::showMemoryConsumption()
  137. {
  138. cout << "MemoryConsumption:\n";
  139. long TotalMemorySum=0;
  140. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  141. {
  142. long CurElementMemConsumption = (*it)->calcMemoryConsumption();
  143. TotalMemorySum +=CurElementMemConsumption;
  144. cout << (*it)->Name << ": " << CurElementMemConsumption/1000000. << " MB \n ";
  145. }
  146. cout << "Total Memory Sum = " << TotalMemorySum/1000000. << " MB\n";
  147. }
  148. int SimLoop::prepare(int mst)
  149. {
  150. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  151. {
  152. if ((*it)->On()) (*it)->prepare(mst); // fm14.8.08: if-Abfrage eingefuegt, noch nicht auf side effects geprueft
  153. }
  154. }
  155. void SimLoop::SetDataDirectory(const char* _dirname)
  156. {
  157. SetDataDirectory_(_dirname);
  158. }
  159. void SimLoop::SetDataDirectory_(const char* _dirname)
  160. {
  161. // expand $HOME
  162. std::string HomeDir(getenv("HOME"));
  163. std::string TmpDataDir = _dirname;
  164. string::size_type HomeStrPos = TmpDataDir.find("$HOME");
  165. if (HomeStrPos != string::npos) {
  166. TmpDataDir.replace(HomeStrPos, HomeStrPos+5, HomeDir);
  167. Dout(dc::simloop, "Expand $HOME to " << TmpDataDir);
  168. }
  169. if (fexist(TmpDataDir.c_str()))
  170. {
  171. Dout(dc::simloop, "DataDirectory = " << TmpDataDir);
  172. DataDirectory = TmpDataDir;
  173. } else {
  174. Dout(dc::simloop, "Directory " << TmpDataDir << " doesn't exist.");
  175. Dout(dc::simloop, "Try creating "<< TmpDataDir);
  176. if (MakeDirRecursively(TmpDataDir.c_str())) {
  177. Dout(dc::simloop, "New Directory "<<TmpDataDir <<" was created.");
  178. Dout(dc::simloop, "DataDirectory = " << TmpDataDir << "");
  179. DataDirectory = TmpDataDir;
  180. } else {
  181. TmpDataDir = "tmpsimdata";
  182. if (MakeDirRecursively(TmpDataDir.c_str())) {
  183. Dout(dc::simloop, "New Directory "<<TmpDataDir <<" was created.");
  184. Dout(dc::simloop, "DataDirectory = " << TmpDataDir << "");
  185. DataDirectory = TmpDataDir;
  186. } else {
  187. // throw exception here or quit simulation
  188. }
  189. }
  190. }
  191. // add trailing slash if necessary
  192. if (DataDirectory[DataDirectory.length()-1] != char('/')) DataDirectory += "/";
  193. }
  194. string SimLoop::GetDataDirectory()
  195. {
  196. return DataDirectory;
  197. }
  198. SimLoop::~SimLoop()
  199. {
  200. Dout(dc::simloop, "SimLoop-Destructor");
  201. SaveSimInfo();
  202. deleteAllSimElements();
  203. }
  204. void SimLoop::SaveSimInfo()
  205. {
  206. string siFileName = "Sim.SimInfo";
  207. // fw = fopen((DataDirectory+siFileName).c_str(),"w");
  208. fstream file_op((DataDirectory+siFileName).c_str(),ios::out);
  209. file_op << "<CsimData>\n";
  210. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  211. {
  212. (*it)->SaveSimInfo();
  213. (*it)->WriteSimInfo(file_op);
  214. }
  215. file_op << "</CsimData>\n";
  216. file_op.close();
  217. // fclose(fw);
  218. }
  219. void SimLoop::deleteAllSimElements()
  220. {
  221. Dout(dc::simloop, "SimLoop deletes all registered SimElements");
  222. for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
  223. {
  224. delete (*it);
  225. }
  226. }
  227. void SimLoop::SetMaximumDelay(int newmax)
  228. {
  229. if (newmax>MaximumDelay) {
  230. MaximumDelay=newmax;
  231. }
  232. }
  233. int SimLoop::GetMaximumDelay()
  234. {
  235. return MaximumDelay;
  236. }
  237. int SimLoop::GetMacroTimeStep()
  238. {
  239. return MacroTimeStep;
  240. }
  241. int SimLoop::SetDeltaT(float _dt)
  242. {
  243. DeltaT = _dt;
  244. }
  245. float SimLoop::GetDeltaT()
  246. {
  247. return DeltaT;
  248. }
  249. void SimLoop::TurnOffLearning()
  250. {
  251. Dout(dc::simloop, "TurnOffLearning");fflush(stdout);
  252. for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
  253. {
  254. (*it)->SetLearn(false);
  255. }
  256. for (NormIter it=(*NormList).begin(); it!=(*NormList).end(); ++it)
  257. {
  258. (*it)->TurnOff();
  259. }
  260. }
  261. void SimLoop::TurnOnLearning()
  262. {
  263. Dout(dc::simloop, "TurnOnLearning");fflush(stdout);
  264. for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
  265. {
  266. (*it)->SetLearn(true);
  267. }
  268. for (NormIter it=(*NormList).begin(); it!=(*NormList).end(); ++it)
  269. {
  270. (*it)->TurnOn();
  271. }
  272. }
  273. void SimLoop::SaveLearningWeights(int TrialNr)
  274. {
  275. for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
  276. {
  277. if ((*it)->Learning()) (*it)->Save(TrialNr);
  278. }
  279. }
  280. void SimLoop::CropLearningWeights(float threshold)
  281. {
  282. for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
  283. {
  284. if ((*it)->Learning()) (*it)->DeleteLowWeights(threshold);
  285. }
  286. }
  287. void SimLoop::ProcessSignalUsr1()
  288. {
  289. Dout(dc::simloop, "SimLoop::ProcessSignalUsr1()");
  290. Hallo();
  291. }