00001 #ifndef Communicator_h
00002 #define Communicator_h
00003
00004 class Individual;
00005 typedef boost::shared_ptr<Individual> IndividualP;
00006 class State;
00007 typedef boost::shared_ptr<State> StateP;
00008
00009 typedef unsigned int uint;
00010
00011
00012 namespace Comm
00013 {
00014
00015
00016 const int T_DEFAULT = 0;
00017 const int T_CONTROL = 99;
00018 const int T_CONTINUE = 1;
00019 const int T_TERMINATE = 2;
00020 const int T_VALUES = 3;
00021 const int T_LOGS = 4;
00022 const int T_FINAL = 5;
00023 const int T_DATA = 6;
00024
00025
00026 const int CONTINUE = 1;
00027 const int TERMINATE = 0;
00028
00029
00030 enum timing
00031 { COMP, IDLE, SEND, RECV, PACK, UNPACK };
00032
00033 #ifdef _MPI
00034
00035
00036 const int ANY_PROCESS = MPI::ANY_SOURCE;
00037
00044 class Communicator
00045 {
00046 protected:
00047
00048 uint mpiGlobalSize_, mpiGlobalRank_;
00049 uint mpiSize_, mpiRank_;
00050 std::string processorName_;
00051 StateP state_;
00052 MPI::Status status_;
00053 MPI::Status controlStatus_;
00054 int logLevel_;
00055 std::vector<uint> demeMasters;
00056 bool bInitialized_;
00057
00058
00071 MPI::Intercomm frameworkComm_;
00072
00078 MPI::Intercomm demeComm_;
00079
00080
00081 double currentTime_, lastTime_;
00082 double beginTime_, endTime_;
00083 double idleTime_;
00084 double sendTime_;
00085 double recvTime_;
00086 double compTime_;
00087 double packTime_, unpackTime_;
00088 uint sendCnt_;
00089 uint recvCnt_;
00090 double time(enum timing T);
00091 public:
00092 Communicator();
00093 bool initialize(StateP, int, char**);
00094 bool finalize();
00095 bool isInitialized()
00096 { return bInitialized_; }
00097
00098
00099 bool sendIndividuals(std::vector<IndividualP>, uint, uint nIndividuals = 0);
00100 bool sendIndividualsGlobal(std::vector<IndividualP>, uint, uint nIndividuals = 0);
00101 bool sendFitness(std::vector<IndividualP>, uint, uint nIndividuals = 0);
00102 bool sendValuesGlobal(std::vector<double>, uint);
00103 bool sendLogsGlobal(std::string, uint iProcess = 0, bool blocking = false);
00104 bool sendDataGlobal(voidP, uint, uint);
00105
00106
00107 uint recvDemeIndividuals(std::vector<IndividualP>&, uint);
00108 std::vector<IndividualP> recvIndividuals(uint iProcess = MPI::ANY_SOURCE);
00109 std::vector<IndividualP> recvIndividualsGlobal(uint iProcess = MPI::ANY_SOURCE);
00110 uint recvReplaceIndividuals(std::vector<IndividualP>&, uint);
00111 uint recvDemeFitness(std::vector<IndividualP>&, uint);
00112 std::vector<uint> recvFitnessVector(std::vector<IndividualP>&, uint);
00113 std::vector<double> recvValuesGlobal(uint iProcess = MPI::ANY_SOURCE);
00114 std::string recvLogsGlobal();
00115 voidP recvDataGlobal(uint iProcess = MPI::ANY_SOURCE);
00116
00117
00118 bool messageWaiting(uint iProcess = MPI::ANY_SOURCE, uint tag = MPI::ANY_TAG);
00119 void synchronize();
00120 bool sendControlMessage(uint, int);
00121 int recvControlMessage(uint);
00122 bool sendTerminateMessage(uint, bool);
00123 bool recvTerminateMessage(uint);
00124 bool checkTerminationMessage(uint master = 0);
00125 uint createDemeCommunicator(uint nDemes);
00126 uint getDemeMaster(uint iDeme);
00127
00128
00129 uint getLastSource();
00130 uint getCommRank()
00131 { return mpiRank_; }
00132 uint getCommSize()
00133 { return mpiSize_; }
00134 uint getCommGlobalRank()
00135 { return mpiGlobalRank_; }
00136 uint getCommGlobalSize()
00137 { return mpiGlobalSize_; }
00138
00139 };
00140 typedef boost::shared_ptr<Communicator> CommunicatorP;
00141
00142 #else // non _MPI
00143
00144 class Communicator
00145 {
00146 public:
00147 bool initialize(StateP state, int argc, char** argv)
00148 { return true; }
00149 bool finalize()
00150 { return true; }
00151 uint getCommRank()
00152 { return 0; }
00153 uint getCommGlobalRank()
00154 { return 0; }
00155 uint getCommSize()
00156 { return 0; }
00157
00158 };
00159 typedef boost::shared_ptr<Communicator> CommunicatorP;
00160 #endif // _MPI
00161
00162 }
00163
00164 typedef boost::shared_ptr<Comm::Communicator> CommunicatorP;
00165
00166
00167 #endif // Communicator_h