00001
00005
00006 #include <converse.h>
00007
00008 #include <math.h>
00009
00010 #include "lbdb.h"
00011 #include "LBObj.h"
00012 #include "LBOM.h"
00013 #include "LBDatabase.h"
00014 #include "LBDBManager.h"
00015
00016 #if CMK_LBDB_ON
00017
00018 extern "C" LDHandle LDCreate(void)
00019 {
00020 LDHandle h;
00021 h.handle = (void*)(new LBDB);
00022 return h;
00023 }
00024
00025 extern "C" LDOMHandle LDRegisterOM(LDHandle _db, LDOMid _userID,
00026 void *_userptr, LDCallbacks _callbacks)
00027 {
00028 LBDB *const db = (LBDB*)(_db.handle);
00029 return db->AddOM(_userID, _userptr, _callbacks);
00030 }
00031
00032 extern "C" void LDUnregisterOM(LDHandle _db, LDOMHandle om)
00033 {
00034 LBDB *const db = (LBDB*)(_db.handle);
00035 db->RemoveOM(om);
00036 }
00037
00038 extern "C" void LDOMMetaLBResumeWaitingChares(LDHandle _db, int lb_ideal_period) {
00039 LBDB *const db = (LBDB*)(_db.handle);
00040 db->MetaLBResumeWaitingChares(lb_ideal_period);
00041 }
00042
00043 extern "C" void LDOMMetaLBCallLBOnChares(LDHandle _db) {
00044 LBDB *const db = (LBDB*)(_db.handle);
00045 db->MetaLBCallLBOnChares();
00046 }
00047
00048 extern "C" void * LDOMUserData(LDOMHandle &_h)
00049 {
00050 LBDB *const db = (LBDB*)(_h.ldb.handle);
00051 return db->LbOM(_h)->getUserData();
00052 }
00053
00054 extern "C" void LDRegisteringObjects(LDOMHandle _h)
00055 {
00056 LBDB *const db = (LBDB*)(_h.ldb.handle);
00057 db->RegisteringObjects(_h);
00058 }
00059
00060 extern "C" void LDDoneRegisteringObjects(LDOMHandle _h)
00061 {
00062 LBDB *const db = (LBDB*)(_h.ldb.handle);
00063 db->DoneRegisteringObjects(_h);
00064 }
00065
00066 extern "C" LDObjHandle LDRegisterObj(LDOMHandle _h, CmiUInt8 _id,
00067 void *_userData, bool _migratable)
00068 {
00069 LBDB *const db = (LBDB*)(_h.ldb.handle);
00070 return db->AddObj(_h, _id, _userData, (bool)(_migratable));
00071 }
00072
00073 extern "C" void LDUnregisterObj(LDObjHandle _h)
00074 {
00075 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00076 db->UnregisterObj(_h);
00077 return;
00078 }
00079
00080 const LDObjHandle &LDGetObjHandle(LDHandle h, int oh)
00081 {
00082 LBDB *const db = (LBDB*)(h.handle);
00083 LBObj *const obj = db->LbObjIdx(oh);
00084 return obj->GetLDObjHandle();
00085 }
00086
00087 extern "C" void LDObjTime(LDObjHandle &_h,
00088 LBRealType walltime, LBRealType cputime)
00089 {
00090 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00091 LBObj *const obj = db->LbObj(_h);
00092 obj->IncrementTime(walltime,cputime);
00093 db->MeasuredObjTime(walltime,cputime);
00094 }
00095
00096 extern "C" void LDGetObjLoad(LDObjHandle &_h, LBRealType *wallT, LBRealType *cpuT)
00097 {
00098 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00099 LBObj *const obj = db->LbObj(_h);
00100 obj->getTime(wallT, cpuT);
00101 }
00102
00103 extern "C" void LDQueryKnownObjLoad(LDObjHandle &_h, LBRealType *wallT, LBRealType *cpuT)
00104 {
00105 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00106 LBObj *const obj = db->LbObj(_h);
00107 obj->lastKnownLoad(wallT, cpuT);
00108 }
00109
00110 extern "C" void * LDObjUserData(LDObjHandle &_h)
00111 {
00112 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00113 LBObj *const obj = db->LbObj(_h);
00114 return obj->getLocalUserData();
00115 }
00116
00117 #if CMK_LB_USER_DATA
00118 extern "C" void * LDDBObjUserData(LDObjHandle &_h, int idx)
00119 {
00120 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00121 LBObj *const obj = db->LbObj(_h);
00122 return obj->getDBUserData(idx);
00123 }
00124 #endif
00125
00126 extern "C" void LDDumpDatabase(LDHandle _db)
00127 {
00128 LBDB *const db = (LBDB*)(_db.handle);
00129 db->DumpDatabase();
00130 }
00131
00132 extern "C" void LDNotifyMigrated(LDHandle _db, LDMigratedFn fn, void* data)
00133 {
00134 LBDB *const db = (LBDB*)(_db.handle);
00135 db->NotifyMigrated(fn,data);
00136 }
00137
00138 extern "C" void LDAddStartLBFn(LDHandle _db, LDStartLBFn fn, void* data)
00139 {
00140 LBDB *const db = (LBDB*)(_db.handle);
00141 db->AddStartLBFn(fn,data);
00142 }
00143
00144 extern "C" void LDRemoveStartLBFn(LDHandle _db, LDStartLBFn fn)
00145 {
00146 LBDB *const db = (LBDB*)(_db.handle);
00147 db->RemoveStartLBFn(fn);
00148 }
00149
00150 extern "C" void LDStartLB(LDHandle _db)
00151 {
00152 LBDB *const db = (LBDB*)(_db.handle);
00153 db->StartLB();
00154 }
00155
00156 extern "C" void LDTurnManualLBOn(LDHandle _db)
00157 {
00158 LBDB *const db = (LBDB*)(_db.handle);
00159 db->TurnManualLBOn();
00160 }
00161
00162 extern "C" void LDTurnManualLBOff(LDHandle _db)
00163 {
00164 LBDB *const db = (LBDB*)(_db.handle);
00165 db->TurnManualLBOff();
00166 }
00167
00168 extern "C" int LDAddMigrationDoneFn(LDHandle _db, LDMigrationDoneFn fn, void* data)
00169 {
00170 LBDB *const db = (LBDB*)(_db.handle);
00171 return db->AddMigrationDoneFn(fn,data);
00172 }
00173
00174 extern "C" void LDRemoveMigrationDoneFn(LDHandle _db, LDMigrationDoneFn fn)
00175 {
00176 LBDB *const db = (LBDB*)(_db.handle);
00177 db->RemoveMigrationDoneFn(fn);
00178 }
00179
00180 extern "C" void LDMigrationDone(LDHandle _db)
00181 {
00182 LBDB *const db = (LBDB*)(_db.handle);
00183 db->MigrationDone();
00184 }
00185
00186 extern "C" void LDTurnPredictorOn(LDHandle _db, void *model)
00187 {
00188 LBDB *const db = (LBDB*)(_db.handle);
00189 db->TurnPredictorOn(model);
00190 }
00191
00192 extern "C" void LDTurnPredictorOnWin(LDHandle _db, void *model, int wind)
00193 {
00194 LBDB *const db = (LBDB*)(_db.handle);
00195 db->TurnPredictorOn(model, wind);
00196 }
00197
00198 extern "C" void LDTurnPredictorOff(LDHandle _db)
00199 {
00200 LBDB *const db = (LBDB*)(_db.handle);
00201 db->TurnPredictorOff();
00202 }
00203
00204
00205 extern "C" void LDChangePredictor(LDHandle _db, void *model)
00206 {
00207 LBDB *const db = (LBDB*)(_db.handle);
00208 db->ChangePredictor(model);
00209 }
00210
00211 extern "C" void LDCollectStatsOn(LDHandle _db)
00212 {
00213 LBDB *const db = (LBDB*)(_db.handle);
00214
00215 if (!db->StatsOn()) {
00216 if (db->ObjIsRunning()) {
00217
00218 const LDObjHandle &oh = db->RunningObj();
00219 LBObj *obj = db->LbObj(oh);
00220 obj->StartTimer();
00221 }
00222 db->TurnStatsOn();
00223 }
00224 }
00225
00226 extern "C" void LDCollectStatsOff(LDHandle _db)
00227 {
00228 LBDB *const db = (LBDB*)(_db.handle);
00229 db->TurnStatsOff();
00230 }
00231
00232 extern "C" int CLDCollectingStats(LDHandle _db)
00233 {
00234
00235
00236
00237 return LDCollectingStats(_db);
00238 }
00239
00240 extern "C" int CLDRunningObject(LDHandle _h, LDObjHandle* _o)
00241 {
00242 return LDRunningObject(_h, _o);
00243 }
00244
00245 extern "C" void LDObjectStart(const LDObjHandle &_h)
00246 {
00247 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00248
00249 if (db->ObjIsRunning()) LDObjectStop(db->RunningObj());
00250
00251 db->SetRunningObj(_h);
00252
00253 if (db->StatsOn()) {
00254 LBObj *const obj = db->LbObj(_h);
00255 obj->StartTimer();
00256 }
00257 }
00258
00259 extern "C" void LDObjectStop(const LDObjHandle &_h)
00260 {
00261 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00262 LBObj *const obj = db->LbObj(_h);
00263
00264 if (db->StatsOn()) {
00265 LBRealType walltime, cputime;
00266 obj->StopTimer(&walltime,&cputime);
00267 obj->IncrementTime(walltime,cputime);
00268 db->MeasuredObjTime(walltime,cputime);
00269 }
00270 db->NoRunningObj();
00271 }
00272
00273 extern "C" void LDSend(const LDOMHandle &destOM, const CmiUInt8 &destid, unsigned int bytes, int destObjProc, int force)
00274 {
00275 LBDB *const db = (LBDB*)(destOM.ldb.handle);
00276 if (force || (db->StatsOn() && _lb_args.traceComm()))
00277 db->Send(destOM,destid,bytes, destObjProc);
00278 }
00279
00280 extern "C" void LDMulticastSend(const LDOMHandle &destOM, CmiUInt8 *destids, int ndests, unsigned int bytes, int nMsgs)
00281 {
00282 LBDB *const db = (LBDB*)(destOM.ldb.handle);
00283 if (db->StatsOn() && _lb_args.traceComm())
00284 db->MulticastSend(destOM,destids,ndests,bytes,nMsgs);
00285 }
00286
00287 extern "C" void LDBackgroundLoad(LDHandle _db,
00288 LBRealType* walltime, LBRealType* cputime)
00289 {
00290 LBDB *const db = (LBDB*)(_db.handle);
00291 db->BackgroundLoad(walltime,cputime);
00292
00293 return;
00294 }
00295
00296 extern "C" void LDIdleTime(LDHandle _db,LBRealType* walltime)
00297 {
00298 LBDB *const db = (LBDB*)(_db.handle);
00299 db->IdleTime(walltime);
00300
00301 return;
00302 }
00303
00304 extern "C" void LDTotalTime(LDHandle _db,LBRealType* walltime, LBRealType* cputime)
00305 {
00306 LBDB *const db = (LBDB*)(_db.handle);
00307 db->TotalTime(walltime,cputime);
00308
00309 return;
00310 }
00311
00312 extern "C" void LDGetTime(LDHandle _db, LBRealType *total_walltime,
00313 LBRealType *total_cputime,
00314 LBRealType *idletime, LBRealType *bg_walltime, LBRealType *bg_cputime)
00315 {
00316 LBDB *const db = (LBDB*)(_db.handle);
00317 db->GetTime(total_walltime, total_cputime, idletime, bg_walltime, bg_cputime);
00318 }
00319
00320 extern "C" void LDNonMigratable(const LDObjHandle &h)
00321 {
00322 LBDB *const db = (LBDB*)(h.omhandle.ldb.handle);
00323 LBObj *const obj = db->LbObj(h);
00324
00325 obj->SetMigratable(false);
00326 }
00327
00328 extern "C" void LDMigratable(const LDObjHandle &h)
00329 {
00330 LBDB *const db = (LBDB*)(h.omhandle.ldb.handle);
00331 LBObj *const obj = db->LbObj(h);
00332
00333 obj->SetMigratable(true);
00334 }
00335
00336 extern "C" void LDSetPupSize(const LDObjHandle &h, size_t obj_pup_size)
00337 {
00338 LBDB *const db = (LBDB*)(h.omhandle.ldb.handle);
00339 LBObj *const obj = db->LbObj(h);
00340
00341 obj->setPupSize(obj_pup_size);
00342 }
00343
00344 extern "C" void LDAsyncMigrate(const LDObjHandle &h, bool async)
00345 {
00346 LBDB *const db = (LBDB*)(h.omhandle.ldb.handle);
00347 LBObj *const obj = db->LbObj(h);
00348
00349 obj->UseAsyncMigrate(async);
00350 }
00351
00352 extern "C" void LDClearLoads(LDHandle _db)
00353 {
00354 LBDB *const db = (LBDB*)(_db.handle);
00355
00356 db->ClearLoads();
00357 }
00358
00359 extern "C" int LDGetObjDataSz(LDHandle _db)
00360 {
00361 LBDB *const db = (LBDB*)(_db.handle);
00362
00363 return db->ObjDataCount();
00364 }
00365
00366 extern "C" void LDGetObjData(LDHandle _db, LDObjData *data)
00367 {
00368 LBDB *const db = (LBDB*)(_db.handle);
00369
00370 db->GetObjData(data);
00371 }
00372
00373 extern "C" int LDGetCommDataSz(LDHandle _db)
00374 {
00375 LBDB *const db = (LBDB*)(_db.handle);
00376
00377 return db->CommDataCount();
00378 }
00379
00380 extern "C" void LDGetCommData(LDHandle _db, LDCommData *data)
00381 {
00382 LBDB *const db = (LBDB*)(_db.handle);
00383
00384 db->GetCommData(data);
00385 return;
00386 }
00387
00388 extern "C" void LDGetCommInfo(LDHandle _db, int& bytes, int& msgs, int& withinbytes, int& outsidebytes, int& n_nghbors, int& hops, int& hopbytes) {
00389 LBDB *const db = (LBDB*)(_db.handle);
00390
00391 db->GetCommInfo(bytes, msgs, withinbytes, outsidebytes, n_nghbors, hops, hopbytes);
00392 return;
00393 }
00394
00395 extern "C" int LDMigrate(LDObjHandle _h, int dest)
00396 {
00397 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00398
00399 return db->Migrate(_h,dest);
00400 }
00401
00402 extern "C" void LDMigrated(LDObjHandle _h, int waitBarrier)
00403 {
00404 LBDB *const db = (LBDB*)(_h.omhandle.ldb.handle);
00405
00406 db->Migrated(_h, waitBarrier);
00407 }
00408
00409 LDBarrierClient LDAddLocalBarrierClient(LDHandle _db, LDResumeFn fn, void* data)
00410 {
00411 LBDB *const db = (LBDB*)(_db.handle);
00412
00413 return db->AddLocalBarrierClient(fn,data);
00414 }
00415
00416 void LDRemoveLocalBarrierClient(LDHandle _db, LDBarrierClient h)
00417 {
00418 LBDB *const db = (LBDB*)(_db.handle);
00419
00420 db->RemoveLocalBarrierClient(h);
00421 }
00422
00423 LDBarrierReceiver LDAddLocalBarrierReceiver(LDHandle _db,LDBarrierFn fn, void* data)
00424 {
00425 LBDB *const db = (LBDB*)(_db.handle);
00426
00427 return db->AddLocalBarrierReceiver(fn,data);
00428 }
00429
00430 void LDRemoveLocalBarrierReceiver(LDHandle _db,LDBarrierReceiver h)
00431 {
00432 LBDB *const db = (LBDB*)(_db.handle);
00433
00434 db->RemoveLocalBarrierReceiver(h);
00435 }
00436
00437 extern "C" void LDAtLocalBarrier(LDHandle _db, LDBarrierClient h)
00438 {
00439 LBDB *const db = (LBDB*)(_db.handle);
00440
00441 db->AtLocalBarrier(h);
00442 }
00443
00444 extern "C" void LDDecreaseLocalBarrier(LDHandle _db, LDBarrierClient h, int c)
00445 {
00446 LBDB *const db = (LBDB*)(_db.handle);
00447
00448 db->DecreaseLocalBarrier(h, c);
00449 }
00450
00451 extern "C" void LDLocalBarrierOn(LDHandle _db)
00452 {
00453 LBDB *const db = (LBDB*)(_db.handle);
00454
00455 db->LocalBarrierOn();
00456 }
00457
00458 extern "C" void LDLocalBarrierOff(LDHandle _db)
00459 {
00460 LBDB *const db = (LBDB*)(_db.handle);
00461
00462 db->LocalBarrierOff();
00463 }
00464
00465
00466 extern "C" void LDResumeClients(LDHandle _db)
00467 {
00468 LBDB *const db = (LBDB*)(_db.handle);
00469
00470 db->ResumeClients();
00471 }
00472
00473 static void work(int iter_block, int* result) {
00474 int i;
00475 *result = 1;
00476 for(i=0; i < iter_block; i++) {
00477 double b=0.1 + 0.1 * *result;
00478 *result=(int)(sqrt(1+cos(b * 1.57)));
00479 }
00480 }
00481
00482 extern "C" int LDProcessorSpeed()
00483 {
00484
00485
00486 static int thisProcessorSpeed = -1;
00487
00488 if (_lb_args.samePeSpeed() || CkNumPes() == 1)
00489 return 1;
00490
00491 if (thisProcessorSpeed != -1) return thisProcessorSpeed;
00492
00493
00494
00495 static int result=0;
00496
00497 int wps = 0;
00498 const double elapse = 0.4;
00499
00500
00501 const double end_time = CmiCpuTimer()+elapse;
00502 wps = 0;
00503 while(CmiCpuTimer() < end_time) {
00504 work(1000,&result);
00505 wps+=1000;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514 for(int i=0; i < 2; i++) {
00515 const double start_time = CmiCpuTimer();
00516 work(wps,&result);
00517 const double end_time = CmiCpuTimer();
00518 const double correction = elapse / (end_time-start_time);
00519 wps = (int)((double)wps * correction + 0.5);
00520 }
00521
00522
00523
00524
00525
00526
00527
00528 thisProcessorSpeed = wps;
00529
00530
00531
00532 return wps;
00533 }
00534
00535 extern "C" void LDSetLBPeriod(LDHandle _db, double s)
00536 {
00537 LBDB *const db = (LBDB*)(_db.handle);
00538 db->SetPeriod(s);
00539 }
00540
00541 extern "C" double LDGetLBPeriod(LDHandle _db)
00542 {
00543 LBDB *const db = (LBDB*)(_db.handle);
00544 return db->GetPeriod();
00545 }
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555 extern "C" void LDQueryEstLoad(LDHandle bdb)
00556 {
00557 }
00558
00559 extern "C" int LDMemusage(LDHandle _db)
00560 {
00561 LBDB *const db = (LBDB*)(_db.handle);
00562 return db->useMem();
00563 }
00564
00565 #else
00566 extern "C" int LDProcessorSpeed() { return 1; }
00567 #endif // CMK_LBDB_ON
00568
00569 bool LDOMidEqual(const LDOMid &i1, const LDOMid &i2)
00570 {
00571 return i1.id == i2.id?true:false;
00572 }
00573