00001 #ifndef Impala_Util_Mpi_JobManager_h
00002 #define Impala_Util_Mpi_JobManager_h
00003
00004 #include <map>
00005 #include "Util/PropertySet.h"
00006
00007
00008 #ifdef GetJob
00009 #undef GetJob
00010 #endif
00011
00012 namespace Impala
00013 {
00014 namespace Util
00015 {
00016 namespace Mpi
00017 {
00018
00019
00020 class JobGroup
00021 {
00022 public:
00023 JobGroup(std::string groupProperties, std::string groupId, int jobCount) :
00024 mGroupProperties(groupProperties)
00025 {
00026 mJobs = jobCount;
00027 mNextJob = 0;
00028 mGroupId = groupId;
00029 mWorkers = 0;
00030 }
00031
00032 void JobDone(PropertySet* job)
00033 {
00034 if(job->GetString("JobManager::group-id") != mGroupId)
00035 return;
00036 --mWorkers;
00037 }
00038
00039 bool GetJob(PropertySet* job)
00040 {
00041 if(mNextJob < mJobs)
00042 {
00043 Append(job, &mGroupProperties);
00044 job->Add("JobManager::group-id", mGroupId);
00045 job->Add("JobManager::job-id", mNextJob);
00046 ++mNextJob;
00047 ++mWorkers;
00048 return true;
00049 }
00050
00051
00052 return false;
00053 }
00054
00055 int OpenJobs()
00056 {
00057 return mJobs - mNextJob;
00058 }
00059
00060 private:
00061 PropertySet mGroupProperties;
00062 int mJobs;
00063 int mNextJob;
00064 std::string mGroupId;
00065 int mWorkers;
00066 };
00067
00068
00069 class JobManager
00070 {
00071 public:
00072 JobManager()
00073 {
00074 }
00075
00076 virtual ~JobManager()
00077 {
00078 std::map<std::string, JobGroup*>::iterator it;
00079 for(it=mGroups.begin() ; it!=mGroups.end() ; ++it)
00080 delete it->second;
00081 }
00082
00083 void CreateGroup(std::string groupProperties, std::string groupId, int jobCount)
00084 {
00085 mGroups[groupId] = new JobGroup(groupProperties, groupId, jobCount);
00086 }
00087
00089 void GetJob(PropertySet* job)
00090 {
00091 std::string lastgroup = job->GetString("JobManager::group-id");
00092 if(lastgroup != "")
00093 {
00094 mGroups[lastgroup]->JobDone(job);
00095 if(mGroups[lastgroup]->GetJob(job))
00096 return;
00097 }
00098
00099 std::map<std::string, JobGroup*>::iterator it = mGroups.begin();
00100 JobGroup* current = it->second;
00101 JobGroup* best = current;
00102 int todo = current->OpenJobs();
00103 while(it!=mGroups.end())
00104 {
00105 current = it->second;
00106 if(current->OpenJobs() > todo)
00107 {
00108 todo = current->OpenJobs();
00109 best = current;
00110 }
00111 ++it;
00112 }
00113 if(best->GetJob(job))
00114 return;
00115
00116
00117 job->Add("JobManager::group-id", "");
00118 job->Add("JobManager::job-id", "-1");
00119 }
00120
00121 private:
00122 std::map<std::string, JobGroup*> mGroups;
00123 };
00124
00125
00126 }
00127 }
00128 }
00129
00130 #endif Impala_Util_Mpi_JobManager_h