• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/Communicator.h

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 // message tags:
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 // controls:
00026 const int CONTINUE = 1;
00027 const int TERMINATE = 0;
00028 
00029 // timing categories
00030 enum timing
00031 {   COMP, IDLE, SEND, RECV, PACK, UNPACK    };
00032 
00033 #ifdef _MPI
00034 
00035 // MPI defines
00036 const int ANY_PROCESS = MPI::ANY_SOURCE;
00037 
00044 class Communicator
00045 {
00046 protected:
00047     // MPI parameters
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     // communicators
00059     MPI::Intercomm frameworkComm_;
00060     MPI::Intercomm demeComm_;
00061 
00062     // timing
00063     double currentTime_, lastTime_;
00064     double beginTime_, endTime_;
00065     double idleTime_;
00066     double sendTime_;
00067     double recvTime_;
00068     double compTime_;
00069     double packTime_, unpackTime_;
00070     uint sendCnt_;
00071     uint recvCnt_;
00072     double time(enum timing T);
00073 public:
00074     Communicator();
00075     bool initialize(StateP, int, char**);
00076     bool finalize();
00077     bool isInitialized()
00078     {   return bInitialized_;   }
00079 
00080     // send methods:
00081     bool sendIndividuals(std::vector<IndividualP>, uint, uint nIndividuals = 0);
00082     bool sendIndividualsGlobal(std::vector<IndividualP>, uint, uint nIndividuals = 0);
00083     bool sendFitness(std::vector<IndividualP>, uint, uint nIndividuals = 0);
00084     bool sendValuesGlobal(std::vector<double>, uint);
00085     bool sendLogsGlobal(std::string, uint iProcess = 0, bool blocking = false);
00086     bool sendDataGlobal(voidP, uint, uint);
00087 
00088     // receive methods:
00089     uint recvDemeIndividuals(std::vector<IndividualP>&, uint);
00090     std::vector<IndividualP> recvIndividuals(uint iProcess = MPI::ANY_SOURCE);
00091     std::vector<IndividualP> recvIndividualsGlobal(uint iProcess = MPI::ANY_SOURCE);
00092     uint recvReplaceIndividuals(std::vector<IndividualP>&, uint);
00093     uint recvDemeFitness(std::vector<IndividualP>&, uint);
00094     std::vector<uint> recvFitnessVector(std::vector<IndividualP>&, uint);
00095     std::vector<double> recvValuesGlobal(uint iProcess = MPI::ANY_SOURCE);
00096     std::string recvLogsGlobal();
00097     voidP recvDataGlobal(uint iProcess = MPI::ANY_SOURCE);
00098 
00099     // other:
00100     bool messageWaiting(uint iProcess = MPI::ANY_SOURCE, uint tag = MPI::ANY_TAG);
00101     void synchronize();
00102     bool sendControlMessage(uint, int);
00103     int recvControlMessage(uint);
00104     bool sendTerminateMessage(uint, bool);
00105     bool recvTerminateMessage(uint);
00106     bool checkTerminationMessage(uint master = 0);
00107     uint createDemeCommunicator(uint nDemes);
00108     uint getDemeMaster(uint iDeme);
00109 
00110     // info:
00111     uint getLastSource();
00112     uint getCommRank()
00113     {   return mpiRank_;    }
00114     uint getCommSize()
00115     {   return mpiSize_;    }
00116     uint getCommGlobalRank()
00117     {   return mpiGlobalRank_;  }
00118     uint getCommGlobalSize()
00119     {   return mpiGlobalSize_;  }
00120 
00121 };
00122 typedef boost::shared_ptr<Communicator> CommunicatorP;
00123 
00124 #else // non _MPI
00125 
00126 class Communicator
00127 {
00128 public:
00129     bool initialize(StateP state, int argc, char** argv)
00130     {   return true;    }
00131     bool finalize()
00132     {   return true;    }
00133     uint getCommRank()
00134     {   return 0;   }
00135     uint getCommGlobalRank()
00136     {   return 0;   }
00137     uint getCommSize()
00138     {   return 0;   }
00139 
00140 };
00141 typedef boost::shared_ptr<Communicator> CommunicatorP;
00142 #endif // _MPI
00143 
00144 }   // namespace
00145 
00146 typedef boost::shared_ptr<Comm::Communicator> CommunicatorP;
00147 
00148 
00149 #endif // Communicator_h

Generated on Thu Oct 6 2011 13:41:00 for ECF by  doxygen 1.7.1