From a2679e3bc2f407fae6b97908aeebb215c6678ebc Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 17 Jul 2012 09:11:40 -0600 Subject: It all works multi-threaded. It restarts the threads every generation. Maybe not the worst thing every, but it seems like it would be better to stay in the thread and notify the main thread that they're done with that iteration. --- src/explicitsimulation.h | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src/explicitsimulation.h') diff --git a/src/explicitsimulation.h b/src/explicitsimulation.h index ebddd62..c2aeed9 100644 --- a/src/explicitsimulation.h +++ b/src/explicitsimulation.h @@ -4,6 +4,10 @@ #include "genetic/population.h" #include "genetic/config.h" +#include +#include +#include + namespace Genetic { class Operator; @@ -13,7 +17,8 @@ namespace Genetic { public: ExplicitSimulation( Operator *pOper, FitnessFunction *pFunc, - int iPopSize, float fKeep, float fRandom, bool bKeepBest=true ); + int iThreads, int iPopSize, float fKeep, float fRandom, + bool bKeepBest=true ); virtual ~ExplicitSimulation(); void timestep(); @@ -25,11 +30,11 @@ namespace Genetic protected: void updateFitness(); + void setFitness( Genetic::PhenotypeId, double dFitness ); protected: Population xPop; Operator *pOper; - FitnessFunction *pFunc; private: int iPopSize; @@ -42,6 +47,32 @@ namespace Genetic double dMaxFitness; double dTotalFitness; PhenotypeId uMaxFitness; + + Bu::Mutex mFitness; + typedef Bu::SynchroQueue WorkQueue; + WorkQueue qWork; + + class Processor : public Bu::Thread + { + public: + Processor( ExplicitSimulation &rSim, FitnessFunction *pFunc, + WorkQueue &rqWork, int iId ); + virtual ~Processor(); + + void setRunning( bool b ) { bRunning = b; } + + protected: + virtual void run(); + + ExplicitSimulation &rSim; + FitnessFunction *pFunc; + WorkQueue &rqWork; + int iId; + bool bRunning; + }; + + typedef Bu::List ProcessorList; + ProcessorList lProcessor; }; }; -- cgit v1.2.3