00001
00005
00006 #ifndef LBDATABASE_H
00007 #define LBDATABASE_H
00008
00009 #include "lbdb.h"
00010 #include "LBDBManager.h"
00011 #include "lbdb++.h"
00012
00013 #define LB_FORMAT_VERSION 3
00014
00015 class MetaBalancer;
00016 extern int _lb_version;
00017
00018
00019 class CkLBArgs
00020 {
00021 private:
00022 double _autoLbPeriod;
00023 double _lb_alpha;
00024 double _lb_beta;
00025 int _lb_debug;
00026 int _lb_printsumamry;
00027 int _lb_loop;
00028 int _lb_ignoreBgLoad;
00029 int _lb_migObjOnly;
00030 int _lb_syncResume;
00031 int _lb_samePeSpeed;
00032 int _lb_testPeSpeed;
00033 int _lb_useCpuTime;
00034 int _lb_statson;
00035 int _lb_traceComm;
00036 int _lb_central_pe;
00037 int _lb_percentMovesAllowed;
00038 int _lb_teamSize;
00039 int _lb_maxDistPhases;
00040 double _lb_targetRatio;
00041 int _lb_metaLbOn;
00042 char* _lb_metaLbModelDir;
00043
00044 public:
00045 CkLBArgs() {
00046 #if CMK_BIGSIM_CHARM
00047 _autoLbPeriod = 0.02;
00048 #else
00049 _autoLbPeriod = 0.5;
00050 #endif
00051 _lb_debug = _lb_ignoreBgLoad = _lb_syncResume = _lb_useCpuTime = 0;
00052 _lb_printsumamry = _lb_migObjOnly = 0;
00053 _lb_statson = _lb_traceComm = 1;
00054 _lb_percentMovesAllowed=100;
00055 _lb_loop = 0;
00056 _lb_central_pe = 0;
00057 _lb_teamSize = 1;
00058 _lb_maxDistPhases = 10;
00059 _lb_targetRatio = 1.05;
00060 _lb_metaLbOn = 0;
00061 _lb_metaLbModelDir = nullptr;
00062 }
00063 inline double & lbperiod() { return _autoLbPeriod; }
00064 inline int & debug() { return _lb_debug; }
00065 inline int & teamSize() {return _lb_teamSize; }
00066 inline int & printSummary() { return _lb_printsumamry; }
00067 inline int & lbversion() { return _lb_version; }
00068 inline int & loop() { return _lb_loop; }
00069 inline int & ignoreBgLoad() { return _lb_ignoreBgLoad; }
00070 inline int & migObjOnly() { return _lb_migObjOnly; }
00071 inline int & syncResume() { return _lb_syncResume; }
00072 inline int & samePeSpeed() { return _lb_samePeSpeed; }
00073 inline int & testPeSpeed() { return _lb_testPeSpeed; }
00074 inline int & useCpuTime() { return _lb_useCpuTime; }
00075 inline int & statsOn() { return _lb_statson; }
00076 inline int & traceComm() { return _lb_traceComm; }
00077 inline int & central_pe() { return _lb_central_pe; }
00078 inline double & alpha() { return _lb_alpha; }
00079 inline double & beta() { return _lb_beta; }
00080 inline int & percentMovesAllowed() { return _lb_percentMovesAllowed;}
00081 inline int & maxDistPhases() { return _lb_maxDistPhases; }
00082 inline double & targetRatio() { return _lb_targetRatio; }
00083 inline int & metaLbOn() {return _lb_metaLbOn;}
00084 inline char*& metaLbModelDir() { return _lb_metaLbModelDir; }
00085 };
00086
00087 extern CkLBArgs _lb_args;
00088
00089 extern int _lb_predict;
00090 extern int _lb_predict_delay;
00091 extern int _lb_predict_window;
00092 extern bool _lb_psizer_on;
00093 #ifndef PREDICT_DEBUG
00094 #define PREDICT_DEBUG 0 // 0 = No debug, 1 = Debug info on
00095 #endif
00096 #define PredictorPrintf if (PREDICT_DEBUG) CmiPrintf
00097
00098
00099 class CkLBOptions
00100 {
00101 private:
00102 int seqno;
00103 public:
00104 CkLBOptions(): seqno(-1) {}
00105 CkLBOptions(int s): seqno(s) {}
00106 int getSeqNo() const { return seqno; }
00107 };
00108 PUPbytes(CkLBOptions)
00109
00110 #include "LBDatabase.decl.h"
00111
00112 extern CkGroupID _lbdb;
00113
00114 class LBDB;
00115
00116 CkpvExtern(int, numLoadBalancers);
00117 CkpvExtern(bool, hasNullLB);
00118 CkpvExtern(bool, lbdatabaseInited);
00119
00120
00121 extern char * _lbtopo;
00122
00123 typedef void (*LBCreateFn)();
00124 typedef BaseLB * (*LBAllocFn)();
00125 void LBDefaultCreate(LBCreateFn f);
00126
00127 void LBRegisterBalancer(const char *, LBCreateFn, LBAllocFn, const char *, int shown=1);
00128
00129 void _LBDBInit();
00130
00131
00132 class LBDBInit : public Chare {
00133 public:
00134 LBDBInit(CkArgMsg*);
00135 LBDBInit(CkMigrateMessage *m):Chare(m) {}
00136 };
00137
00138
00139 class LBPredictorFunction {
00140 public:
00141 virtual ~LBPredictorFunction() {}
00142 int num_params;
00143
00144 virtual void initialize_params(double *x) {double normall=1.0/pow((double)2,(double)31); for (int i=0; i<num_params; ++i) x[i]=rand()*normall;}
00145
00146 virtual double predict(double x, double *params) =0;
00147 virtual void print(double *params) {PredictorPrintf("LB: unknown model\n");};
00148 virtual void function(double x, double *param, double &y, double *dyda) =0;
00149 };
00150
00151
00152 class DefaultFunction : public LBPredictorFunction {
00153 public:
00154
00155 DefaultFunction() {num_params=6;};
00156
00157
00158 double predict(double x, double *param) {return (param[0] + param[1]*x + param[2]*x*x + param[3]*sin(param[4]*(x+param[5])));}
00159
00160 void print(double *param) {PredictorPrintf("LB: %f + %fx + %fx^2 + %fsin%f(x+%f)\n",param[0],param[1],param[2],param[3],param[4],param[5]);}
00161
00162
00163 void function(double x, double *param, double &y, double *dyda) {
00164 double tmp;
00165
00166 y = predict(x, param);
00167
00168 dyda[0] = 1;
00169 dyda[1] = x;
00170 dyda[2] = x*x;
00171 tmp = param[4] * (x+param[5]);
00172 dyda[3] = sin(tmp);
00173 dyda[4] = param[3] * (x+param[5]) * cos(tmp);
00174 dyda[5] = param[3] * param[4] *cos(tmp);
00175 }
00176 };
00177
00178
00179 class LBDatabase : public IrrGroup {
00180 public:
00181 LBDatabase(void) { init(); }
00182 LBDatabase(CkMigrateMessage *m) { (void)m; init(); }
00183 ~LBDatabase() { if (avail_vector) delete [] avail_vector; }
00184
00185 private:
00186 void init();
00187 public:
00188 inline static LBDatabase * Object() { return CkpvAccess(lbdatabaseInited)?(LBDatabase *)CkLocalBranch(_lbdb):NULL; }
00189 #if CMK_LBDB_ON
00190 inline LBDB *getLBDB() {return (LBDB*)(myLDHandle.handle);}
00191 #endif
00192
00193 static void initnodeFn(void);
00194
00195 void pup(PUP::er& p);
00196
00197
00198
00199
00200 inline LDOMHandle RegisterOM(LDOMid userID, void *userptr, LDCallbacks cb) {
00201 return LDRegisterOM(myLDHandle,userID, userptr, cb);
00202 };
00203
00204 inline void UnregisterOM(LDOMHandle omHandle) {
00205 return LDUnregisterOM(myLDHandle, omHandle);
00206 };
00207
00208 inline void RegisteringObjects(LDOMHandle _om) {
00209 LDRegisteringObjects(_om);
00210 };
00211
00212 inline void DoneRegisteringObjects(LDOMHandle _om) {
00213 LDDoneRegisteringObjects(_om);
00214 };
00215
00216 void ResetAdaptive();
00217
00218 inline LDObjHandle RegisterObj(LDOMHandle h, CmiUInt8 id,
00219 void *userptr,int migratable) {
00220 return LDRegisterObj(h,id,userptr,migratable);
00221 };
00222
00223 inline void UnregisterObj(LDObjHandle h) { LDUnregisterObj(h); };
00224
00225 inline void ObjTime(LDObjHandle h, double walltime, double cputime) {
00226 LDObjTime(h,walltime,cputime);
00227 };
00228
00229 inline void GetObjLoad(LDObjHandle &h, LBRealType &walltime, LBRealType &cputime) {
00230 LDGetObjLoad(h,&walltime,&cputime);
00231 };
00232
00233 #if CMK_LB_USER_DATA
00234 inline void *GetDBObjUserData(LDObjHandle &h, int idx)
00235 {
00236 return LDDBObjUserData(h, idx);
00237 }
00238 #endif
00239
00240 inline void QueryKnownObjLoad(LDObjHandle &h, LBRealType &walltime, LBRealType &cputime) {
00241 LDQueryKnownObjLoad(h,&walltime,&cputime);
00242 };
00243
00244 inline int RunningObject(LDObjHandle* _o) const {
00245 #if CMK_LBDB_ON
00246 LBDB *const db = (LBDB*)(myLDHandle.handle);
00247 if (db->ObjIsRunning()) {
00248 *_o = db->RunningObj();
00249 return 1;
00250 }
00251 #endif
00252 return 0;
00253
00254 };
00255 inline const LDObjHandle *RunningObject() const {
00256 #if CMK_LBDB_ON
00257 LBDB *const db = (LBDB*)(myLDHandle.handle);
00258 if (db->ObjIsRunning()) {
00259 return &db->RunningObj();
00260 }
00261 #endif
00262 return NULL;
00263 };
00264 inline const LDObjHandle &GetObjHandle(int idx) { return LDGetObjHandle(myLDHandle, idx);}
00265 inline void ObjectStart(const LDObjHandle &_h) { LDObjectStart(_h); };
00266 inline void ObjectStop(const LDObjHandle &_h) { LDObjectStop(_h); };
00267 inline void Send(const LDOMHandle &_om, const CmiUInt8 _id, unsigned int _b, int _p, int force = 0) {
00268 LDSend(_om, _id, _b, _p, force);
00269 };
00270 inline void MulticastSend(const LDOMHandle &_om, CmiUInt8 *_ids, int _n, unsigned int _b, int _nMsgs=1) {
00271 LDMulticastSend(_om, _ids, _n, _b, _nMsgs);
00272 };
00273
00274 void EstObjLoad(const LDObjHandle &h, double cpuload);
00275 inline void NonMigratable(LDObjHandle h) { LDNonMigratable(h); };
00276 inline void Migratable(LDObjHandle h) { LDMigratable(h); };
00277 inline void setPupSize(LDObjHandle h, size_t pup_size) {LDSetPupSize(h, pup_size);};
00278 inline void UseAsyncMigrate(LDObjHandle h, bool flag) { LDAsyncMigrate(h, flag); };
00279 inline void DumpDatabase(void) { LDDumpDatabase(myLDHandle); };
00280
00281
00282
00283
00284 inline void NotifyMigrated(LDMigratedFn fn, void *data)
00285 {
00286 LDNotifyMigrated(myLDHandle,fn,data);
00287 };
00288
00289 inline void AddStartLBFn(LDStartLBFn fn, void *data)
00290 {
00291 LDAddStartLBFn(myLDHandle,fn,data);
00292 };
00293
00294 inline void RemoveStartLBFn(LDStartLBFn fn)
00295 {
00296 LDRemoveStartLBFn(myLDHandle,fn);
00297 };
00298
00299 inline void StartLB() { LDStartLB(myLDHandle); }
00300
00301 inline void AddMigrationDoneFn(LDMigrationDoneFn fn, void *data)
00302 {
00303 LDAddMigrationDoneFn(myLDHandle,fn,data);
00304 };
00305
00306 inline void RemoveMigrationDoneFn(LDMigrationDoneFn fn)
00307 {
00308 LDRemoveMigrationDoneFn(myLDHandle,fn);
00309 };
00310
00311 inline void MigrationDone() { LDMigrationDone(myLDHandle); }
00312
00313 public:
00314 inline void TurnManualLBOn() { LDTurnManualLBOn(myLDHandle); }
00315 inline void TurnManualLBOff() { LDTurnManualLBOff(myLDHandle); }
00316
00317 inline void PredictorOn(LBPredictorFunction *model) { LDTurnPredictorOn(myLDHandle,model); }
00318 inline void PredictorOn(LBPredictorFunction *model,int wind) { LDTurnPredictorOnWin(myLDHandle,model,wind); }
00319 inline void PredictorOff() { LDTurnPredictorOff(myLDHandle); }
00320 inline void ChangePredictor(LBPredictorFunction *model) { LDTurnPredictorOn(myLDHandle,model); }
00321
00322 inline void CollectStatsOn(void) { LDCollectStatsOn(myLDHandle); };
00323 inline void CollectStatsOff(void) { LDCollectStatsOff(myLDHandle); };
00324 inline int CollectingStats(void) { return LDCollectingStats(myLDHandle); };
00325 inline int CollectingCommStats(void) { return LDCollectingStats(myLDHandle) && _lb_args.traceComm(); };
00326 inline void QueryEstLoad(void) { LDQueryEstLoad(myLDHandle); };
00327
00328 inline int GetObjDataSz(void) { return LDGetObjDataSz(myLDHandle); };
00329 inline void GetObjData(LDObjData *data) { LDGetObjData(myLDHandle,data); };
00330 inline int GetCommDataSz(void) { return LDGetCommDataSz(myLDHandle); };
00331 inline void GetCommData(LDCommData *data) { LDGetCommData(myLDHandle,data); };
00332
00333 inline void GetCommInfo(int& bytes, int& msgs, int& withinbytes, int& outsidebytes, int& num_ngh, int& hops, int& hopbytes) {
00334 return LDGetCommInfo(myLDHandle, bytes, msgs, withinbytes, outsidebytes, num_ngh, hops, hopbytes);
00335 };
00336
00337 inline void BackgroundLoad(LBRealType *walltime, LBRealType *cputime) {
00338 LDBackgroundLoad(myLDHandle,walltime,cputime);
00339 }
00340
00341 inline void IdleTime(LBRealType *walltime) {
00342 LDIdleTime(myLDHandle,walltime);
00343 };
00344
00345 inline void TotalTime(LBRealType *walltime, LBRealType *cputime) {
00346 LDTotalTime(myLDHandle,walltime,cputime);
00347 }
00348
00349 inline void GetTime(LBRealType *total_walltime,LBRealType *total_cputime,
00350 LBRealType *idletime, LBRealType *bg_walltime, LBRealType *bg_cputime) {
00351 LDGetTime(myLDHandle, total_walltime, total_cputime, idletime, bg_walltime, bg_cputime);
00352 }
00353
00354 inline void ClearLoads(void) { LDClearLoads(myLDHandle); };
00355 inline int Migrate(LDObjHandle h, int dest) { return LDMigrate(h,dest); };
00356
00357 inline void Migrated(LDObjHandle h, int waitBarrier=1) {
00358 LDMigrated(h, waitBarrier);
00359 };
00360
00361 inline LDBarrierClient AddLocalBarrierClient(LDResumeFn fn, void* data) {
00362 return LDAddLocalBarrierClient(myLDHandle,fn,data);
00363 };
00364
00365 inline void RemoveLocalBarrierClient(LDBarrierClient h) {
00366 LDRemoveLocalBarrierClient(myLDHandle, h);
00367 };
00368
00369 inline LDBarrierReceiver AddLocalBarrierReceiver(LDBarrierFn fn, void *data) {
00370 return LDAddLocalBarrierReceiver(myLDHandle,fn,data);
00371 };
00372
00373 inline void RemoveLocalBarrierReceiver(LDBarrierReceiver h) {
00374 LDRemoveLocalBarrierReceiver(myLDHandle,h);
00375 };
00376
00377 inline void AtLocalBarrier(LDBarrierClient h) {
00378 LDAtLocalBarrier(myLDHandle,h);
00379 }
00380 inline void DecreaseLocalBarrier(LDBarrierClient h, int c) {
00381 LDDecreaseLocalBarrier(myLDHandle,h,c);
00382 }
00383 inline void LocalBarrierOn(void) { LDLocalBarrierOn(myLDHandle); };
00384 inline void LocalBarrierOff(void) { LDLocalBarrierOn(myLDHandle); };
00385 void ResumeClients();
00386 inline int ProcessorSpeed() { return LDProcessorSpeed(); };
00387 inline void SetLBPeriod(double s) { LDSetLBPeriod(myLDHandle, s);}
00388 inline double GetLBPeriod() { return LDGetLBPeriod(myLDHandle);}
00389
00390 inline void MetaLBResumeWaitingChares(int lb_period) {
00391 #if CMK_LBDB_ON
00392 LDOMMetaLBResumeWaitingChares(myLDHandle, lb_period);
00393 #endif
00394 }
00395
00396 inline void MetaLBCallLBOnChares() {
00397 #if CMK_LBDB_ON
00398 LDOMMetaLBCallLBOnChares(myLDHandle);
00399 #endif
00400 }
00401
00402 void SetMigrationCost(double cost);
00403 void SetStrategyCost(double cost);
00404 void UpdateDataAfterLB(double mLoad, double mCpuLoad, double avgLoad);
00405
00406 private:
00407 int mystep;
00408 LDHandle myLDHandle;
00409 static char *avail_vector;
00410 static bool avail_vector_set;
00411 int new_ld_balancer;
00412 CkVec<BaseLB *> loadbalancers;
00413 int nloadbalancers;
00414 MetaBalancer* metabalancer;
00415
00416 public:
00417 BaseLB** getLoadBalancers() {return loadbalancers.getVec();}
00418 int getNLoadBalancers() {return nloadbalancers;}
00419
00420 public:
00421 static bool manualOn;
00422
00423 public:
00424 char *availVector() { return avail_vector; }
00425 void get_avail_vector(char * bitmap);
00426 void set_avail_vector(char * bitmap, int new_ld=-1);
00427 int & new_lbbalancer() { return new_ld_balancer; }
00428
00429 struct LastLBInfo {
00430 LBRealType *expectedLoad;
00431 LastLBInfo();
00432 };
00433 LastLBInfo lastLBInfo;
00434 inline LBRealType myExpectedLoad() { return lastLBInfo.expectedLoad[CkMyPe()]; }
00435 inline LBRealType* expectedLoad() { return lastLBInfo.expectedLoad; }
00436 inline int useMem() { return LDMemusage(myLDHandle); }
00437
00438 int getLoadbalancerTicket();
00439 void addLoadbalancer(BaseLB *lb, int seq);
00440 void nextLoadbalancer(int seq);
00441 void switchLoadbalancer(int switchFrom, int switchTo);
00442 const char *loadbalancer(int seq);
00443
00444 inline int step() { return mystep; }
00445 inline void incStep() { mystep++; }
00446 };
00447
00448 void TurnManualLBOn();
00449 void TurnManualLBOff();
00450
00451 void LBTurnPredictorOn(LBPredictorFunction *model);
00452 void LBTurnPredictorOn(LBPredictorFunction *model, int wind);
00453 void LBTurnPredictorOff();
00454 void LBChangePredictor(LBPredictorFunction *model);
00455
00456 void LBSetPeriod(double second);
00457
00458 #if CMK_LB_USER_DATA
00459 int LBRegisterObjUserData(int size);
00460 #endif
00461
00462 extern "C" void LBTurnInstrumentOn();
00463 extern "C" void LBTurnInstrumentOff();
00464 extern "C" void LBTurnCommOn();
00465 extern "C" void LBTurnCommOff();
00466 void LBClearLoads();
00467
00468 inline LBDatabase* LBDatabaseObj() { return LBDatabase::Object(); }
00469
00470 inline void CkStartLB() { LBDatabase::Object()->StartLB(); }
00471
00472 inline void get_avail_vector(char * bitmap) {
00473 LBDatabaseObj()->get_avail_vector(bitmap);
00474 }
00475
00476 inline void set_avail_vector(char * bitmap) {
00477 LBDatabaseObj()->set_avail_vector(bitmap);
00478 }
00479
00480
00481
00482
00483 class SystemLoad
00484 {
00485 const LDObjHandle *objHandle;
00486 LBDatabase *lbdb;
00487 public:
00488 SystemLoad() {
00489 lbdb = LBDatabaseObj();
00490 objHandle = lbdb->RunningObject();
00491 if (objHandle != NULL) {
00492 lbdb->ObjectStop(*objHandle);
00493 }
00494 }
00495 ~SystemLoad() {
00496 if (objHandle) lbdb->ObjectStart(*objHandle);
00497 }
00498 };
00499
00500 #define CK_RUNTIME_API SystemLoad load_entry;
00501
00502 #endif
00503