123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #include "sys.hpp" // for libcwd
- #include "debug.hpp" // for libcwd
- #include "simloop.hpp"
- #include "simelement.hpp"
- #include "layer.hpp"
- #include "connection.hpp"
- #include "normalize.hpp"
- #include "filesystem.hpp"
- #include <signal.h>
- /** Signal-Handler
- */
- void ProcessSignal(int SigNum)
- {
- switch (SigNum) {
- case SIGUSR1:
- Dout(dc::simloop, "received custom signal SIGUSR1 ");
- GetGlobalSimLoop()->ProcessSignalUsr1();
- break;
- case SIGUSR2:
- Dout(dc::simloop, "received custom signal SIGUSR2 ");
- break;
- }
-
- }
- SimLoop* GlobalMainSimLoop=0;
- SimLoop* InitLibCSim(int _MacroTimeStep, float _DeltaT)
- {
- GlobalMainSimLoop = new SimLoop(_MacroTimeStep, _DeltaT);
- return GlobalMainSimLoop;
- }
- void deleteGlobalSimLoop()
- {
- delete GlobalMainSimLoop;
- GlobalMainSimLoop=0;
- }
- SimLoop* GetGlobalSimLoop()
- {
- if (GlobalMainSimLoop) {
- return GlobalMainSimLoop;
- } else {
- return InitLibCSim();
- }
- }
- ///////////////////////////////
- SimLoop::SimLoop(int _MacroTimeStep, float _DeltaT)
- :MacroTimeStep(_MacroTimeStep),
- DeltaT(_DeltaT),
- ElementCounter(0),
- MaximumDelay(0)
- {
- Dout(dc::simloop, "SimLoop Default Constructor");
- SimElementList = new vector<SimElement*>;
- ConnectionList = new TConnectionList;
- NormList = new TNormList;
-
- DataDirectory="";
- // DeltaT=0.25;
- Dout(dc::simloop, " One time step is DeltaT = " << DeltaT << " ms");
- if (signal(SIGUSR1, ProcessSignal) == SIG_ERR) {
- cerr << "ERROR connecting signal to handler\n";
- } else {
- Dout(dc::simloop, "connected signal to handler");
- };
-
- }
- /**
- * @brief element is added to SimLoop, and SimLoop takes ownership of the SimElement.
- * SimLoop is therefore responsible for deleting the SimElements
- *
- * @param element
- * @return
- */
- int SimLoop::AddSimElement(SimElement* element)
- {
- (*SimElementList).push_back(element);
- element->IdNumber = ++ElementCounter;
- Dout(dc::simloop, "add SimElement");
- element->Hallo();
- }
- int SimLoop::AddSimElement(layer* element)
- {
- SimElement* NewElement = static_cast<SimElement*>(element);
- AddSimElement(NewElement);
- Dout(dc::simloop, " new SimElement is a Layer ");
- }
- int SimLoop::AddSimElement(Connection* element)
- {
- (*ConnectionList).push_back(element);
- SimElement* NewElement = static_cast<SimElement*>(element);
- AddSimElement(NewElement);
- Dout(dc::simloop, " new SimElement is a Connection ");
- }
- int SimLoop::AddSimElement(AbstractNormalize* element)
- {
- (*NormList).push_back(element);
- SimElement* NewElement = static_cast<SimElement*>(element);
- AddSimElement(NewElement);
- Dout(dc::simloop, " new SimElement is a Normalization Object");
- }
- void SimLoop::Hallo()
- {
- Dout(dc::simloop, "This is SimLoop::Hallo()");
- Debug( libcw_do.inc_indent(2) );
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- (*it)->Hallo();
- }
- Debug( libcw_do.dec_indent(2) );
- Dout(dc::simloop, "DataDirectory=" << DataDirectory << " MacroTimeStep=" << MacroTimeStep);
- }
- void SimLoop::SetSimTag(const char* _tag)
- {
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- (*it)->SetSimTag(_tag);
- }
- }
- int SimLoop::proceede(int TotalTime)
- {
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- if ((*it)->On()) (*it)->proceede(TotalTime);
- }
- }
- int SimLoop::reset(int t)
- {
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- if ((*it)->On() && (*it)->Resetable()) (*it)->reset(t);
- }
- }
- void SimLoop::SetParameter(ParaType p, double value)
- {
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- (*it)->SetParameter(p, value);
- }
- }
- void SimLoop::showMemoryConsumption()
- {
- cout << "MemoryConsumption:\n";
- long TotalMemorySum=0;
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- long CurElementMemConsumption = (*it)->calcMemoryConsumption();
- TotalMemorySum +=CurElementMemConsumption;
- cout << (*it)->Name << ": " << CurElementMemConsumption/1000000. << " MB \n ";
- }
- cout << "Total Memory Sum = " << TotalMemorySum/1000000. << " MB\n";
- }
- int SimLoop::prepare(int mst)
- {
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- if ((*it)->On()) (*it)->prepare(mst); // fm14.8.08: if-Abfrage eingefuegt, noch nicht auf side effects geprueft
- }
- }
- void SimLoop::SetDataDirectory(const char* _dirname)
- {
- SetDataDirectory_(_dirname);
- }
- void SimLoop::SetDataDirectory_(const char* _dirname)
- {
- // expand $HOME
- std::string HomeDir(getenv("HOME"));
- std::string TmpDataDir = _dirname;
- string::size_type HomeStrPos = TmpDataDir.find("$HOME");
- if (HomeStrPos != string::npos) {
- TmpDataDir.replace(HomeStrPos, HomeStrPos+5, HomeDir);
- Dout(dc::simloop, "Expand $HOME to " << TmpDataDir);
- }
-
- if (fexist(TmpDataDir.c_str()))
- {
- Dout(dc::simloop, "DataDirectory = " << TmpDataDir);
- DataDirectory = TmpDataDir;
- } else {
- Dout(dc::simloop, "Directory " << TmpDataDir << " doesn't exist.");
- Dout(dc::simloop, "Try creating "<< TmpDataDir);
- if (MakeDirRecursively(TmpDataDir.c_str())) {
- Dout(dc::simloop, "New Directory "<<TmpDataDir <<" was created.");
- Dout(dc::simloop, "DataDirectory = " << TmpDataDir << "");
- DataDirectory = TmpDataDir;
- } else {
- TmpDataDir = "tmpsimdata";
- if (MakeDirRecursively(TmpDataDir.c_str())) {
- Dout(dc::simloop, "New Directory "<<TmpDataDir <<" was created.");
- Dout(dc::simloop, "DataDirectory = " << TmpDataDir << "");
- DataDirectory = TmpDataDir;
- } else {
- // throw exception here or quit simulation
- }
- }
- }
-
- // add trailing slash if necessary
- if (DataDirectory[DataDirectory.length()-1] != char('/')) DataDirectory += "/";
- }
- string SimLoop::GetDataDirectory()
- {
- return DataDirectory;
- }
- SimLoop::~SimLoop()
- {
- Dout(dc::simloop, "SimLoop-Destructor");
- SaveSimInfo();
- deleteAllSimElements();
- }
- void SimLoop::SaveSimInfo()
- {
-
- string siFileName = "Sim.SimInfo";
- // fw = fopen((DataDirectory+siFileName).c_str(),"w");
- fstream file_op((DataDirectory+siFileName).c_str(),ios::out);
- file_op << "<CsimData>\n";
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- (*it)->SaveSimInfo();
- (*it)->WriteSimInfo(file_op);
- }
- file_op << "</CsimData>\n";
- file_op.close();
- // fclose(fw);
- }
- void SimLoop::deleteAllSimElements()
- {
- Dout(dc::simloop, "SimLoop deletes all registered SimElements");
- for (vector<SimElement*>::iterator it=(*SimElementList).begin(); it!=(*SimElementList).end(); ++it)
- {
- delete (*it);
- }
- }
- void SimLoop::SetMaximumDelay(int newmax)
- {
- if (newmax>MaximumDelay) {
- MaximumDelay=newmax;
- }
- }
- int SimLoop::GetMaximumDelay()
- {
- return MaximumDelay;
- }
- int SimLoop::GetMacroTimeStep()
- {
- return MacroTimeStep;
- }
- int SimLoop::SetDeltaT(float _dt)
- {
- DeltaT = _dt;
- }
- float SimLoop::GetDeltaT()
- {
- return DeltaT;
- }
- void SimLoop::TurnOffLearning()
- {
- Dout(dc::simloop, "TurnOffLearning");fflush(stdout);
- for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
- {
- (*it)->SetLearn(false);
- }
- for (NormIter it=(*NormList).begin(); it!=(*NormList).end(); ++it)
- {
- (*it)->TurnOff();
- }
- }
- void SimLoop::TurnOnLearning()
- {
- Dout(dc::simloop, "TurnOnLearning");fflush(stdout);
- for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
- {
- (*it)->SetLearn(true);
- }
-
- for (NormIter it=(*NormList).begin(); it!=(*NormList).end(); ++it)
- {
- (*it)->TurnOn();
- }
- }
- void SimLoop::SaveLearningWeights(int TrialNr)
- {
- for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
- {
- if ((*it)->Learning()) (*it)->Save(TrialNr);
- }
- }
- void SimLoop::CropLearningWeights(float threshold)
- {
- for (ConIter it=(*ConnectionList).begin(); it!=(*ConnectionList).end(); ++it)
- {
- if ((*it)->Learning()) (*it)->DeleteLowWeights(threshold);
- }
- }
- void SimLoop::ProcessSignalUsr1()
- {
- Dout(dc::simloop, "SimLoop::ProcessSignalUsr1()");
- Hallo();
- }
|