00001 #include "statcoll.h"
00002 #include "amr.h"
00003 #define CHK_PT_PUP
00004 #define STATS(x) CkPrintf x
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 AmrCoordinator :: AmrCoordinator(_DMsg* msg)
00017 {
00018 delete msg;
00019 arrayProxy = CProxy_Cell2D::ckNew();
00020
00021
00022 synchInterval = 50;
00023 depth = 2;
00024 dimension = 2;
00025 totalIterations = 500;
00026 myHandle = thishandle;
00027 statCollection = 0;
00028 create_tree();
00029 phase = 0;
00030 phaseStep = 0;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 AmrCoordinator :: AmrCoordinator(StartUpMsg *msg)
00043 {
00044
00045 synchInterval = msg->synchInterval;
00046 depth = msg->depth;
00047 dimension = msg->dimension;
00048 totalIterations = msg->totalIterations;
00049 statCollection = msg->statCollection;
00050 delete msg;
00051 switch (dimension){
00052 case 1:
00053 arrayProxy = CProxy_Cell1D::ckNew();
00054 break;
00055 case 2:
00056 arrayProxy = CProxy_Cell2D::ckNew();
00057 break;
00058 case 3:
00059 arrayProxy = CProxy_Cell3D::ckNew();
00060 break;
00061 }
00062 myHandle = thishandle;
00063
00064 if(statCollection) {
00065 _CreateStatCollMsg *grpMsg = new _CreateStatCollMsg(myHandle);
00066 gid = CProxy_StatCollector::ckNew(grpMsg);
00067 }
00068 phase = 0;
00069 phaseStep = 0;
00070 startTime = CkWallTimer();
00071 create_tree();
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 void AmrCoordinator :: synchronise(_RedMsg *msg)
00089 {
00090 if(msg->type == 0){
00091
00092 delete msg;
00093 if ((phase % 2) != 0) {
00094
00095 ++phase;
00096 resetClock();
00097 STATS(("Refining..\n"));
00098
00099 arrayProxy.refineExec(new _DMsg);
00100 }
00101 else {
00102
00103 ++phase;
00104 resetClock();
00105 STATS(("Load Balancing...\n"));
00106 arrayProxy.goToAtSync(new _DMsg);
00107 }
00108
00109 }
00110 else if (msg->type == 1) {
00111 delete msg;
00112 resetClock();
00113 DEBUGS(("Broadcasting a message to resume\n"));
00114 STATS(("Doing Iterations...\n"));
00115 arrayProxy.resume(new _DMsg);
00116 }
00117 else if (msg->type == 2) {
00118
00119 resetClock();
00120 STATS(("Completed iterations\n"));
00121
00122
00123
00124 delete msg;
00125
00126 if(statCollection) {
00127 CProxy_StatCollector grpProxy(gid);
00128 grpProxy.sendStat(new _DummyMsg);
00129 leaves = 0;
00130 migrations = 0;
00131 refine = 0;
00132 arefine = 0;
00133 statMsgs = 0;
00134 }
00135 else
00136 CkExit();
00137
00138 }
00139 }
00140
00141 void AmrCoordinator :: resetClock()
00142 {
00143 double endTime = CkWallTimer();
00144 STATS(("Phase Step %d Time taken %lf\n",++phaseStep,endTime-startTime));
00145 startTime = CkWallTimer();
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 void AmrCoordinator :: create_tree()
00158 {
00159 BitVec root;
00160 if(depth < 1)
00161 CkError("Initial depth should atleast be 1, Current depth %d\n", depth);
00162 for(int i = 0; i< dimension;i++)
00163 root.vec[i] = 0;
00164 root.numbits = 0;
00165 _ArrInitMsg *msg = new _ArrInitMsg;
00166 msg->type = 'r';
00167 msg->parent = root;
00168 msg->depth = depth;
00169 msg->interval = synchInterval;
00170 msg->totalIterations = totalIterations;
00171 msg->coordHandle = myHandle;
00172 msg->statCollection = statCollection;
00173
00174 msg->gid = gid;
00175
00176 CkArrayIndexBitVec index(root);
00177 switch(dimension) {
00178 case 1: {
00179 CProxy_Cell1D aProxy = *(CProxy_Cell1D *) &arrayProxy;
00180 aProxy[index].insert(msg);
00181 aProxy.doneInserting();
00182 break;
00183 }
00184 case 2: {
00185 CProxy_Cell2D aProxy = *(CProxy_Cell2D *) &arrayProxy;
00186 aProxy[index].insert(msg);
00187 aProxy.doneInserting();
00188 break;
00189 }
00190 case 3: {
00191 CProxy_Cell3D aProxy = *(CProxy_Cell3D *) &arrayProxy;
00192 aProxy[index].insert(msg);
00193 aProxy.doneInserting();
00194 break;
00195 }
00196 }
00197
00198 }
00199
00200 void AmrCoordinator::reportStats(_StatCollMsg* msg)
00201 {
00202 leaves += msg->msgExpected;
00203 refine += msg->refineCount;
00204 arefine += msg->aRefineCount;
00205 migrations += msg->migCount;
00206
00207 CkPrintf("PE %d : refines %d autorefines %d migrations %d leaves %d\n",
00208 msg->pnum,
00209 msg->refineCount,
00210 msg->aRefineCount,
00211 msg->migCount,
00212 msg->msgExpected);
00213 delete msg;
00214 if(++statMsgs == CkNumPes()) {
00215 CkPrintf("Total Leaves %d \n", leaves);
00216 CkPrintf("Total Refines %d \n", refine);
00217 CkPrintf("Total Auto Refines %d \n", arefine);
00218 CkPrintf("Total Migrations %d \n", migrations);
00219 CkExit();
00220 }
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 void* NeighborMsg :: pack(NeighborMsg *msg)
00241 {
00242 int bufSize = msg->dataSize;
00243 bufSize += (4*sizeof(int));
00244 bufSize += sizeof(BitVec);
00245 char *buf = (char *) CkAllocBuffer(msg,bufSize);
00246 memcpy(buf,&(msg->which_neighbor),sizeof(int));
00247 buf += sizeof(int);
00248 memcpy(buf,&(msg->run_until),sizeof(int));
00249 buf += sizeof(int);
00250 memcpy(buf,&(msg->numbits),sizeof(int));
00251 buf += sizeof(int);
00252 memcpy(buf,&(msg->nborIdx),sizeof(BitVec));
00253 buf += sizeof(BitVec);
00254 memcpy(buf,&(msg->dataSize),sizeof(int));
00255 buf += sizeof(int);
00256 memcpy(buf,msg->data,msg->dataSize);
00257 buf -= 4*sizeof(int);
00258 buf -= sizeof(BitVec);
00259 delete msg;
00260 return (void *) buf;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 NeighborMsg* NeighborMsg :: unpack(void* inbuf)
00273 {
00274
00275 char *buf = (char *) inbuf;
00276 NeighborMsg *msg = (NeighborMsg *) CkAllocBuffer(inbuf,sizeof(NeighborMsg));
00277
00278 memcpy(&(msg->which_neighbor),buf,sizeof(int));
00279 buf += sizeof(int);
00280 memcpy(&(msg->run_until),buf,sizeof(int));
00281 buf += sizeof(int);
00282 memcpy(&(msg->numbits),buf,sizeof(int));
00283 buf += sizeof(int);
00284 memcpy(&(msg->nborIdx),buf,sizeof(BitVec));
00285 buf += sizeof(BitVec);
00286 memcpy(&(msg->dataSize),buf,sizeof(int));
00287 buf += sizeof(int);
00288 msg->data = (void *)new char[msg->dataSize];
00289 memcpy(msg->data,buf,msg->dataSize);
00290
00291 CkFreeMsg(inbuf);
00292 return msg;
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 void NeighborMsg :: pup(PUP::er &p)
00306 {
00307 p(which_neighbor);
00308 p(run_until);
00309 p(numbits);
00310 p(dataSize);
00311 nborIdx.pup(p);
00312 if(p.isUnpacking())
00313 data = (void *) new char[dataSize];
00314 p((char *)data,dataSize);
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 void* ChildInitMsg :: pack(ChildInitMsg *msg)
00334 {
00335 int bufSize = msg->dataSize;
00336 bufSize += 4*sizeof(int);
00337 char *buf = (char *) CkAllocBuffer(msg,bufSize);
00338 memcpy(buf,&(msg->num_neighbors),sizeof(int));
00339 buf += sizeof(int);
00340 memcpy(buf,&(msg->run_until),sizeof(int));
00341 buf += sizeof(int);
00342 memcpy(buf,&(msg->synchstep),sizeof(int));
00343 buf += sizeof(int);
00344 memcpy(buf,&(msg->dataSize),sizeof(int));
00345 buf += sizeof(int);
00346 memcpy(buf,msg->data,msg->dataSize);
00347
00348
00349 buf -= 4*sizeof(int);
00350 delete msg;
00351 return (void *) buf;
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 ChildInitMsg* ChildInitMsg :: unpack(void* inbuf)
00365 {
00366
00367
00368 char *buf = (char *) inbuf;
00369 ChildInitMsg *msg = (ChildInitMsg *) CkAllocBuffer(inbuf,sizeof(ChildInitMsg));
00370
00371 memcpy(&(msg->num_neighbors),buf,sizeof(int));
00372 buf += sizeof(int);
00373 memcpy(&(msg->run_until),buf,sizeof(int));
00374 buf += sizeof(int);
00375 memcpy(&(msg->synchstep),buf,sizeof(int));
00376 buf += sizeof(int);
00377 memcpy(&(msg->dataSize),buf,sizeof(int));
00378 buf += sizeof(int);
00379 msg->data = new char[msg->dataSize];
00380 memcpy(msg->data,buf,msg->dataSize);
00381
00382 CkFreeMsg(inbuf);
00383 return msg;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 int Cell :: powerOfTwo(int exp)
00403 {
00404 int result = 1;
00405
00406
00407 result = result << exp;
00408 return result;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 void Cell :: init_cell(_ArrInitMsg *msg)
00423 {
00424 int i;
00425
00426 int temp;
00427
00428 usesAtSync = true;
00429
00430
00431 type = msg->type;
00432 parent = msg->parent;
00433
00434 synchinterval = msg->interval;
00435 synchstep = synchinterval;
00436 synchleavrep = 0;
00437
00438
00439 run_done = msg->totalIterations;
00440
00441
00442 myIndex = thisIndex;
00443 userData = NULL;
00444
00445
00446
00447
00448 num_neighbors = 0;
00449 neighbors_reported = 0;
00450
00451 coordHandle = msg->coordHandle;
00452
00453
00454
00455 nborRecvMsgCount = new int [2*dimension];
00456 for(i=0;i<2*dimension;i++)
00457 nborRecvMsgCount[i] = 0;
00458
00459
00460
00461 temp = powerOfTwo(dimension -1);
00462 nborRecvMsgBuff = new NeighborMsg** [2*dimension];
00463 for(i=0;i<2*dimension;i++) {
00464 nborRecvMsgBuff[i] = new NeighborMsg* [temp];
00465 for(int j=0;j<temp;j++)
00466 nborRecvMsgBuff[i][j] = NULL;
00467 }
00468
00469 run_until = 0;
00470 refined = 0;
00471 autorefine = 0;
00472 justRefined = 0;
00473
00474
00475 msg_queue = FIFO_Create();
00476 start_ptr = NULL;
00477
00478
00479 children = new BitVec* [dimension];
00480 for (i = 0; i < dimension; i++) {
00481 temp = (i==0)? 2: powerOfTwo(i);
00482 children[i] = new BitVec [temp];
00483 }
00484
00485
00486 neighbors = new int [2*dimension];
00487 statCollection = msg->statCollection;
00488 gid = msg->gid;
00489
00490 if(statCollection && type == 'l')
00491 {
00492 CProxy_StatCollector grpProxy(gid);
00493 StatCollector* grp = grpProxy.ckLocalBranch();
00494 grp->registerMe();
00495 }
00496
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 void Cell :: treeSetup(_ArrInitMsg *msg)
00511 {
00512 int size = powerOfTwo(dimension);
00513 if (dimension == 1)
00514 size = 2;
00515
00516 if((type == 'r' || type == 'n') && msg->depth > 0) {
00517
00518
00519
00520
00521 _ArrInitMsg **cmsg;
00522 cmsg = new _ArrInitMsg* [size];
00523 for(int i=0; i<size ;i++) {
00524 cmsg[i] = new _ArrInitMsg;
00525 cmsg[i]->parent = myIndex;
00526 cmsg[i]->type = 'n';
00527 cmsg[i]->interval = synchinterval;
00528 cmsg[i]->depth = (msg->depth) - 1;
00529 cmsg[i]->totalIterations = run_done;
00530 cmsg[i]->coordHandle = coordHandle;
00531 cmsg[i]->statCollection = statCollection;
00532 cmsg[i]->gid = gid;
00533 }
00534
00535 create_children(cmsg);
00536 }
00537 else if(type == 'n' && msg->depth == 0) {
00538
00539
00540
00541
00542
00543
00544 _ArrInitMsg **cmsg;
00545 cmsg = new _ArrInitMsg* [size];
00546 for(int i=0; i<size ;i++) {
00547 cmsg[i] = new _ArrInitMsg;
00548 cmsg[i]->parent = myIndex;
00549 cmsg[i]->type = 'v';
00550 cmsg[i]->interval = synchinterval;
00551 cmsg[i]->depth = (msg->depth) - 1;
00552 cmsg[i]->totalIterations = run_done;
00553 cmsg[i]->coordHandle = coordHandle;
00554 cmsg[i]->statCollection = statCollection;
00555 cmsg[i]->gid = gid;
00556 }
00557
00558 create_children(cmsg);
00559
00560 type = 'l';
00561 }
00562
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580 int Cell :: sendInDimension(int dim,int side, NeighborMsg *msg)
00581 {
00589 BitVec nborvec;
00590 unsigned int mask = 0;
00591 unsigned int mult = 1;
00592 int i,k;
00593 nborvec = myIndex;
00594 nborvec.numbits = myIndex.numbits;
00595 i = myIndex.numbits;
00596 switch(side) {
00597 case 0:{
00598
00599
00600
00601 if(myIndex.vec[dim] == 0) { delete msg; return 0;}
00602 nborvec.vec[dim] = myIndex.vec[dim] -1;
00603 break;
00604 }
00605 case 1: {
00606 i = i/dimension;
00607
00608 for(k=i; k>0; k--) {
00609 mask += mult;
00610 mult *= 2;
00611 }
00612
00613
00614 if((myIndex.vec[dim] & mask) == mask) {
00615 delete msg;
00616 return 0;
00617 }
00618 else
00619 nborvec.vec[dim] = myIndex.vec[dim] +1;
00620 break;
00621 }
00622 }
00623 DEBUGN(("Nbor:sending message to x %dy %d z %d numbits %d \n",
00624 nborvec.vec[0],
00625 nborvec.vec[1],nborvec.vec[2],
00626 nborvec.numbits));
00627
00628 if (!msg)
00629 CkPrintf("sendInDimension:Neighbor message Pointer NULL\n");
00630
00631 CkArrayIndexBitVec index(nborvec);
00632 arrayProxy[index].neighbor_data(msg);
00633 return 1;
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646 void Cell::check_queue()
00647 {
00648
00649 if(!FIFO_Empty(msg_queue)) {
00650 temp_queue= FIFO_Create();
00651 while(!FIFO_Empty(msg_queue)) {
00652 NeighborMsg *temp_msg;
00653 DEBUGN(("Check Queue %d\n",run_until));
00654 FIFO_DeQueue(msg_queue,(void **)&temp_msg);
00655
00656 DEBUGN(("Check Queue x%d y%d bit%d\n",myIndex.vec[0],myIndex.vec[1],myIndex.numbits));
00657 if(temp_msg->run_until > run_until) {
00658 FIFO_EnQueue(temp_queue, temp_msg);
00659 }
00660 else if(temp_msg->run_until == run_until){
00661 CkArrayIndexBitVec index(myIndex);
00662 arrayProxy[index].neighbor_data(temp_msg);
00663 }
00664 else
00665 CkError("Old Message in the queue\n");
00666 }
00667 FIFO_Destroy(msg_queue);
00668 msg_queue = temp_queue;
00669 }
00670 }
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689 void Cell :: neighbor_data(NeighborMsg *msg)
00690 {
00691
00692 int neighbor_side = msg->which_neighbor;
00693
00694 if(type == 'l') {
00695
00696 if(msg->run_until == run_until) {
00697 DEBUGN(("Recv Msg from x %d y %d z %d bits %d side %d--myIndex x %d y %d z %d bits %d- rununtil %d nborsrep %d nbors %d\n",
00698 msg->nborIdx.vec[0],msg->nborIdx.vec[1],msg->nborIdx.vec[2],
00699 msg->nborIdx.numbits,msg->which_neighbor,myIndex.vec[0],
00700 myIndex.vec[1],myIndex.vec[2],myIndex.numbits,run_until,neighbors_reported,num_neighbors));
00701 reg_nbor_msg(neighbor_side, msg);
00702 }
00703
00704 else if(msg->run_until > run_until) {
00705 FIFO_EnQueue(msg_queue,(void *)msg);
00706 DEBUGN(("Enqueueing ------------------>\n"));
00707 return;
00708 }
00709 else {
00710
00711
00712 CkError("Message out of order %d %d x %d y %d z %d bits %d which %d from x %d y %d z %d bits %d: neighbor data\n",
00713 msg->run_until,run_until,
00714 myIndex.vec[0],myIndex.vec[1],
00715 myIndex.vec[2],
00716 myIndex.numbits,
00717 msg->which_neighbor,msg->nborIdx.vec[0],
00718 msg->nborIdx.vec[1],msg->nborIdx.vec[2],
00719 msg->nborIdx.numbits);
00720 return;
00721 }
00722
00723 if (neighbors_reported == num_neighbors && num_neighbors != 0) {
00724
00725 userData->doComputation();
00726
00727 neighbors_reported = 0;
00728 for(int i=0;i<2*dimension;i++)
00729 nborRecvMsgCount[i] = 0;
00730
00731
00732 if(run_until == synchstep) {
00733 CkArrayIndexBitVec index(parent);
00734 arrayProxy[index].synchronise(new _RedMsg(0));
00735 }
00736 else {
00737 DEBUGA(("Calling doIteration from neighborData\n"));
00738 doIterations();
00739 }
00740 }
00741 }
00742 else if (type == 'v') {
00743
00744 if (dimension == 1) {
00745 msg->numbits -= dimension;
00746 }
00747 CkArrayIndexBitVec index(parent);
00748 arrayProxy[index].neighbor_data(msg);
00749 }
00750 else {
00751
00752
00753 msg->numbits += dimension;
00754 forwardSplitMsg(msg ,neighbor_side);
00755 }
00756 }
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768 void Cell :: doIterations() {
00769
00770
00771
00772
00773 int i;
00774 num_neighbors = 0;
00775 justRefined = 0;
00776
00777
00778 for(i=0;i<2*dimension;i++)
00779 neighbors[i] = 0;
00780 int *size ;
00781 size = new int [2*dimension];
00782 for(int i=0;i<2*dimension;i++)
00783 size[i] = 0;
00784
00785 if (++run_until > run_done) {
00786 DEBUGR(("Finished %d my x %d y %d z %d bits %d\n", run_until,
00787 myIndex.vec[0],
00788 myIndex.vec[1],
00789 myIndex.vec[2],
00790 myIndex.numbits));
00791
00792 CkArrayIndexBitVec index(parent);
00793 arrayProxy[index].synchronise(new _RedMsg(2));
00794 delete []size;
00795 return;
00796 }
00797
00798 int nborDim = 0;
00799
00800 void **nborMsgDataArray = userData->getNborMsgArray(size);
00801
00802 for(i = 0; i < 2*dimension; i++) {
00803 int nborDir = (i+1)% 2;
00804 NeighborMsg *nborMsg = new NeighborMsg;
00805 nborMsg->which_neighbor = i;
00806 nborMsg->numbits = myIndex.numbits;
00807 nborMsg->run_until = run_until;
00808 nborMsg->dataSize = size[i];
00809 nborMsg->nborIdx = myIndex;
00810 nborMsg->data = nborMsgDataArray[i];
00811 neighbors[nborDim*2 + nborDir] = sendInDimension(nborDim,nborDir,nborMsg);
00812 num_neighbors += neighbors[nborDim*2 + nborDir];
00813 if(nborDir == 0)
00814 nborDim++;
00815 }
00816 delete []nborMsgDataArray;
00817 delete []size;
00818 DEBUGN(("x = %d, y = %d z = %d numbits %d...synchstep %d ..rununtil %d neighbors %d\n",
00819 myIndex.vec[0],
00820 myIndex.vec[1],
00821 myIndex.vec[2],
00822 myIndex.numbits,
00823 synchstep,
00824 run_until,
00825 num_neighbors));
00826
00827 if(num_neighbors == 0) {
00828 CkPrintf("Finished\n");
00829 CkArrayIndexBitVec index(parent);
00830 arrayProxy[index].synchronise(new _RedMsg(0));
00831 return;
00832 }
00833 check_queue();
00834 }
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851 void Cell :: resume(_DMsg *msg)
00852 {
00853 delete msg;
00854 if(type == 'l') {
00855 DEBUGA(("Calling doIterations from resume\n"));
00856 doIterations();
00857 }
00858 }
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872 void Cell :: synchronise(_RedMsg *msg)
00873 {
00874
00875 if(++synchleavrep == powerOfTwo(dimension)) {
00876 DEBUGS(("Cell sychronise --leaves reported %d dimension %d--my x %d my y %d my z %d bits %d \n",
00877 synchleavrep,dimension,
00878 myIndex.vec[0],myIndex.vec[1],myIndex.vec[2],myIndex.numbits));
00879 synchleavrep = 0;
00880 if(type == 'n') {
00881 CkArrayIndexBitVec index(parent);
00882 arrayProxy[index].synchronise(msg);
00883 }
00884 else if(type == 'r') {
00885 DEBUGS(("Reporting synchronisation message to coordinator x %d y %d z %d bits %d reduction msg type %d\n"
00886 ,myIndex.vec[0],myIndex.vec[1],
00887 myIndex.vec[2],myIndex.numbits,msg->type));
00888 CProxy_AmrCoordinator coordProxy(coordHandle);
00889 coordProxy.synchronise(msg);
00890 }
00891 else
00892 CkError("Error in sychronisation step\n");
00893 }
00894 else
00895 delete msg;
00896 }
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907 void Cell :: refineExec(_DMsg *msg)
00908 {
00909 delete msg;
00910
00911 if(type == 'l' && justRefined == 0) {
00912
00913 if(userData->refineCriterion()) {
00914
00915 DEBUGRC(("Refining node x %d y %d z %d bits %d\n",
00916 myIndex.vec[0],myIndex.vec[1],myIndex.vec[2],
00917 myIndex.numbits));
00918 synchstep += synchinterval;
00919 refine(new _RefineMsg(0));
00920 }
00921 else {
00922
00923
00924 synchstep += synchinterval;
00925 DEBUGRC(("refineExec: x= %d y = %d z %d numbits %d synchstep %d\n",
00926 myIndex.vec[0],
00927 myIndex.vec[1],
00928 myIndex.vec[2],
00929 myIndex.numbits,
00930 synchstep));
00931 CkArrayIndexBitVec index(parent);
00932 arrayProxy[index].synchronise(new _RedMsg(1));
00933 }
00934 }
00935 else if(type =='n' || type == 'r')
00936 synchstep += synchinterval;
00937 }
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951 void Cell :: refine(_RefineMsg *msg)
00952 {
00953
00954
00955 int i, j,flag;
00956
00957 if(autorefine == 0) {
00958 autorefine = msg->autorefine;
00959 flag = 0;
00960
00961 if( msg->autorefine == 1)
00962 retidx = msg->index;
00963 }
00964 else
00965 flag = 1;
00966
00967
00968 if( type == 'l') {
00969 if (neighbors_reported == 0) {
00970 DEBUGRC(("Refine leaf: x %d y %d z %d numbits %d synchstep %d..autorefine %d\n",
00971 myIndex.vec[0],myIndex.vec[1],
00972 myIndex.vec[2],myIndex.numbits,
00973 synchstep,autorefine));
00974 int size=0;
00975
00976 void **cmsgData = userData->fragmentForRefine(&size);
00977 ChildInitMsg ***cmsg = new ChildInitMsg** [dimension];
00978 int temp=2;
00979 for(int index = 0; index < dimension;index++){
00980 temp = ((index==0)? 2:powerOfTwo(index));
00981 cmsg[index] = new ChildInitMsg* [temp];
00982 }
00983 for (int u =0; u<dimension;u++) {
00984 temp = ((u ==0)? 2:powerOfTwo(u));
00985 for(int v=0; v<temp ; v++) {
00986 cmsg[u][v] = new ChildInitMsg;
00987
00988
00989 if(cmsgData[u*2+v] == NULL)
00990 CkError("Error: FragmentForRefine() didnot give a message to be sent to %d child\n"
00991 ,u*2 + v);
00992 else {
00993
00994 cmsg[u][v]->run_until = run_until;
00995 cmsg[u][v]->num_neighbors = 0;
00996
00997 if(synchstep == run_until)
00998 cmsg[u][v]->synchstep = synchstep+synchinterval;
00999 else
01000 cmsg[u][v]->synchstep = synchstep;
01001
01002 cmsg[u][v]->dataSize = size;
01003 cmsg[u][v]->data = cmsgData[u*2+v];
01004
01005
01006
01007 CkArrayIndexBitVec aindex(children[u][v]);
01008 arrayProxy[aindex].change_to_leaf(cmsg[u][v]);
01009 }
01010 }
01011 }
01012
01013
01014
01015
01016
01017
01018
01019 delete []cmsgData;
01020
01021
01022 num_neighbors = 0;
01023 for(i=0; i <dimension; i++)
01024 for(j=0; j<2; j++)
01025 num_neighbors += sendInDimension(i,j);
01026
01027
01028 type = 'n';
01029 if(statCollection) {
01030 CProxy_StatCollector grpProxy(gid);
01031 StatCollector* grp = grpProxy.ckLocalBranch();
01032 if(autorefine == 0)
01033 grp->incrementRefine();
01034 else
01035 grp->incrementAutorefine();
01036 }
01037 }
01038 else {
01039 CkError("Neighbors reported non zero--even after synchronisation in refine x %d y %d z %d bits %d run_until %d parent x %d y %d z %d bits %d\n",
01040 myIndex.vec[0],myIndex.vec[1],myIndex.vec[2],myIndex.numbits,run_until,
01041 parent.vec[0],parent.vec[1],parent.vec[2],parent.numbits);
01042 }
01043 }
01044 else if (autorefine == 1) {
01045
01046 DEBUGRC(("Refine non leaf but autorefine 1 \n"));
01047 DEBUGRC(("Refine Ready....autorefine 1\n"));
01048 if (flag == 0)
01049 autorefine = 0;
01050 refineReady(msg->index,1);
01051 if(statCollection) {
01052 CProxy_StatCollector grpProxy(gid);
01053 StatCollector* grp = grpProxy.ckLocalBranch();
01054 grp->incrementAutorefine();
01055 }
01056 }
01057 else {
01058
01059 CkPrintf("Error:Autorefine %d",autorefine);
01060 CkError(" Refine Msg received by a non leaf--type %c\n",type);
01061 }
01062 }
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074 void Cell :: change_to_leaf(ChildInitMsg *msg)
01075 {
01076 int i;
01077
01078
01079 if(type == 'v') {
01080
01081
01082 justRefined = 1;
01083 run_until = msg->run_until;
01084 num_neighbors = msg->num_neighbors;
01085
01086 synchstep = msg->synchstep;
01087 userData = AmrUserData::createDataWrapper(myIndex,dimension,msg->data,msg->dataSize);
01088 type = 'l';
01089 delete msg;
01090 int temp = powerOfTwo(dimension);
01091 _ArrInitMsg **cmsg = new _ArrInitMsg* [temp];
01092
01093 for(i=0; i<powerOfTwo(dimension) ;i++) {
01094 cmsg[i] = new _ArrInitMsg;
01095 cmsg[i]->parent = myIndex;
01096 cmsg[i]->type = 'v';
01097 cmsg[i]->interval = synchinterval;
01098 cmsg[i]->totalIterations = run_done;
01099 cmsg[i]->coordHandle = coordHandle;
01100 cmsg[i]->depth = -1;
01101 cmsg[i]->statCollection = statCollection;
01102 cmsg[i]->gid = gid;
01103 }
01104 create_children(cmsg);
01105 if(statCollection){
01106 CProxy_StatCollector grpProxy(gid);
01107 StatCollector *grp = grpProxy.ckLocalBranch();
01108 grp->registerMe();
01109 }
01110 CkArrayIndexBitVec index(parent);
01111 arrayProxy[index].refine_confirmed(new _DMsg(myIndex, 0));
01112 }
01113 else
01114 CkError("change to leaf called on a node or leaf\n");
01115 }
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130 void Cell :: refine_confirmed(_DMsg *msg)
01131 {
01132
01133 DEBUGRC(("Refine confirmed for x %d y %d z %d bits %d-- sender x %d y %d z %d numbits %d from pos %d--- refined reports %d, total rep %d\n",myIndex.vec[0],
01134 myIndex.vec[1],myIndex.vec[2],myIndex.numbits,msg->sender.vec[0],
01135 msg->sender.vec[1],msg->sender.vec[2],msg->sender.numbits,msg->from,
01136 refined,(2*dimension+num_neighbors)));
01137 delete msg;
01138 int temp = powerOfTwo(dimension);
01139 if(++refined == (temp+num_neighbors)) {
01140
01141
01142 if(autorefine == 1) {
01143 DEBUGRC(("getting refine confirms bcoz of autorefine\n"));
01144 DEBUGRC(("Refine Ready....autorefine 2\n"));
01145 refineReady(retidx,2);
01146 autorefine = 0;
01147 num_neighbors =0;
01148 neighbors_reported = 0;
01149 }
01150 else {
01151
01152 CkArrayIndexBitVec index(parent);
01153 arrayProxy[index].synchronise(new _RedMsg(1));
01154 num_neighbors = 0;
01155 neighbors_reported = 0;
01156 }
01157
01158 refined = 0;
01159 }
01160 }
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176 int Cell :: sendInDimension(int dim,int side)
01177 {
01185 BitVec nborvec;
01186 unsigned int mask = 0;
01187 unsigned int mult = 1;
01188 int i,k;
01189 nborvec = myIndex;
01190 i = nborvec.numbits = myIndex.numbits;
01191 switch(side) {
01192 case 0:
01193
01194
01195
01196
01197
01198 if(myIndex.vec[dim] == 0) return 0;
01199
01200 nborvec.vec[dim] = myIndex.vec[dim] - 1;
01201 break;
01202 case 1:
01203 i = i/dimension;
01204
01205 for(k=i; k>0; k--) {
01206 mask += mult;
01207 mult *= 2;
01208 }
01209
01210
01211 if((myIndex.vec[dim] & mask)==mask) return 0;
01212 nborvec.vec[dim] = myIndex.vec[dim] + 1;
01213 break;
01214 }
01215
01216 DEBUGN(("sending message to x %dy %d z %d numbits %d \n",nborvec.vec[0],
01217 nborvec.vec[1],nborvec.vec[2],nborvec.numbits));
01218 CkArrayIndexBitVec index(nborvec);
01219 arrayProxy[index].checkRefine(new _RefineChkMsg(myIndex,run_until));
01220 return 1;
01221 }
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237 void Cell :: checkRefine(_RefineChkMsg* msg)
01238 {
01239 if(type == 'v') {
01240
01241 CkArrayIndexBitVec index(parent);
01242 arrayProxy[index].refine(new _RefineMsg(1,msg->index));
01243 delete msg;
01244 }
01245 else {
01246
01247
01248 DEBUGRC(("Refine Ready....autorefine 3\n"));
01249 refineReady(msg->index,3);
01250 delete msg;
01251 }
01252 }
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264 void Cell :: refineReady(BitVec retid,int pos)
01265 {
01266
01267 CkArrayIndexBitVec index(retid);
01268 arrayProxy[index].refine_confirmed(new _DMsg(myIndex, pos));
01269 }
01270
01271 void Cell :: pup(PUP::er &p)
01272 {
01273 DEBUGN(("Puping the array\n"));
01274 ArrayElementT<BitVec>::pup(p);
01275 p(dimension);
01276 p(type);
01277
01278 p|userData;
01279
01280 parent.pup(p);
01281
01282 if (p.isUnpacking()) {
01283 children = new BitVec*[dimension];
01284 for(int i=0; i< dimension; i++) {
01285 int size = (i==0)? 2: powerOfTwo(i);
01286 children[i] = new BitVec[size];
01287 }
01288 }
01289
01290 for(int i=0; i < dimension;i++) {
01291 int size = ((i==0)? 2:powerOfTwo(i));
01292 p((char*)children[i], size*sizeof(BitVec));
01293 }
01294
01295 myIndex.pup(p);
01296 p(num_neighbors);
01297 p(neighbors_reported);
01298 p(run_until);
01299 p(run_done);
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317 p(justRefined);
01318
01319 if (p.isUnpacking()) {
01320 neighbors = new int[2*dimension];
01321 nborRecvMsgCount = new int[2*dimension];
01322 for(int i=0; i< 2*dimension; i++)
01323 nborRecvMsgCount[i] = 0;
01324 }
01325 p(neighbors,2*dimension);
01326 #ifdef CHK_PT_PUP
01327 p(nborRecvMsgCount,2*dimension);
01328 #endif
01329
01330 int count = 0;
01331 int temp = powerOfTwo(dimension -1);
01332
01333 if(p.isUnpacking()) {
01334 nborRecvMsgBuff = new NeighborMsg** [2*dimension];
01335 for(int i=0;i<2*dimension;i++) {
01336 nborRecvMsgBuff[i] = new NeighborMsg* [temp];
01337 for(int j=0; j<temp;j++)
01338 nborRecvMsgBuff[i][j] = NULL;
01339 }
01340 }
01341 else{
01342 for(int i=0;i<2*dimension;i++){
01343 for(int j=0;j<temp;j++){
01344 if(nborRecvMsgBuff && nborRecvMsgBuff[i] && nborRecvMsgBuff[i][j]){
01345 count++;
01346 #ifndef CHK_PT_PUP
01347 NeighborMsg* tempMsg = new NeighborMsg;
01348 cpyNborMsg(tempMsg,nborRecvMsgBuff[i][j]);
01349 delete nborRecvMsgBuff[i][j];
01350 nborRecvMsgBuff[i][j] = NULL;
01351 CkPrintf("Resending message from the partial messages %d \n",count);
01352 arrayProxy[myIndex].neighbor_data(tempMsg);
01353 #endif
01354 }
01355 }
01356 }
01357 }
01358 #ifdef CHK_PT_PUP
01359 p(count);
01360 if(count >0){
01361 for(int i=0;i<2*dimension;i++){
01362 for(int j=0;j<temp;j++){
01363 if(nborRecvMsgBuff && nborRecvMsgBuff[i] && nborRecvMsgBuff[i][j])
01364 nborRecvMsgBuff[i][j]->pup(p);
01365 }
01366 }
01367 }
01368 #endif
01369
01370
01371 #ifdef CHK_PT_PUP
01372 if(!p.isUnpacking()){
01373 msg_count = FIFO_Fill(msg_queue);
01374 p(msg_count);
01375 if(msg_count >0) {
01376 for(int i=0; i<msg_count; i++) {
01377 NeighborMsg *temp_msg;
01378 FIFO_DeQueue(msg_queue, (void **) &temp_msg);
01379 temp_msg->pup(p);
01380 FIFO_EnQueue(msg_queue,(void*)temp_msg);
01381 }
01382 }
01383 }
01384 else{
01385 msg_queue = FIFO_Create();
01386 p(msg_count);
01387 if(msg_count > 0) {
01388 while(msg_count >0) {
01389 NeighborMsg *temp_msg = new NeighborMsg;
01390 temp_msg->pup(p);
01391 FIFO_EnQueue(msg_queue, temp_msg);
01392 msg_count--;
01393 }
01394 }
01395 }
01396 #endif
01397
01398 #ifndef CHK_PT_PUP
01399 if(!p.isUnpacking()){
01400 while(!FIFO_Empty(msg_queue)) {
01401 NeighborMsg* temp_msg;
01402 FIFO_DeQueue(msg_queue,(void**) &temp_msg);
01403 arrayProxy[myIndex].neighbor_data(temp_msg);
01404 }
01405 }
01406 else
01407 msg_queue = FIFO_Create();
01408 #endif
01409
01410 p(refined);
01411 p(autorefine);
01412 retidx.pup(p);
01413 p(synchleavrep);
01414 p(synchinterval);
01415 p(synchstep);
01416
01417
01418 p|coordHandle;
01419 p|statCollection;
01420 p|gid;
01421 if(p.isPacking() && statCollection && type == 'l')
01422 {
01423 CProxy_StatCollector grpProxy(gid);
01424 StatCollector* grp = grpProxy.ckLocalBranch();
01425 grp->migrating();
01426 }
01427 else if(p.isUnpacking() && statCollection && type == 'l')
01428 {
01429 CProxy_StatCollector grpProxy(gid);
01430 StatCollector* grp = grpProxy.ckLocalBranch();
01431 grp->registerMe();
01432 }
01433
01434 }
01435
01436
01437 void Cell :: cpyNborMsg(NeighborMsg* dest,NeighborMsg* src)
01438 {
01439
01440 dest->which_neighbor = src->which_neighbor;
01441 dest->run_until = src->run_until;
01442 dest->numbits = src->numbits;
01443 dest->nborIdx = src->nborIdx;
01444 dest->dataSize = src->dataSize;
01445 dest->data = (void *) new char[src->dataSize];
01446 memcpy(dest->data,src->data,src->dataSize);
01447 }
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466 Cell2D :: Cell2D(_ArrInitMsg *msg )
01467 {
01468 int i, j, k;
01469
01470
01471 dimension = 2;
01472 CProxy_Cell2D aProxy(thisArrayID);
01473 arrayProxy = *(CProxy_Cell*) &aProxy;
01474
01475 init_cell(msg);
01476
01477
01478
01479 treeSetup(msg);
01480
01481
01482 delete msg;
01483
01484
01485 if(type == 'l') {
01486 userData = AmrUserData::createDataWrapper(myIndex,dimension);
01487 doIterations();
01488 }
01489 else
01490 userData = NULL;
01491 }
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504 void Cell2D :: create_children(_ArrInitMsg** cmsg)
01505 {
01506 int i,j,k;
01507 CProxy_Cell2D aProxy = *(CProxy_Cell2D *) &arrayProxy;
01508
01509
01510 for(i =0; i<dimension;i++) {
01511 for(j=0; j<2;j++) {
01512 for(k =0; k<dimension ; k++)
01513 children[i][j].vec[k] = 2*myIndex.vec[k];
01514 children[i][j].numbits = myIndex.numbits + dimension;
01515 }
01516 }
01517 children[0][1].vec[0] += 1;
01518 children[1][0].vec[1] += 1;
01519 children[1][1].vec[0] += 1;
01520 children[1][1].vec[1] += 1;
01521
01522
01523
01524 j = 0;
01525 for(i= 0; i<dimension ; i++)
01526 for(k=0; k<2; k++) {
01527
01528 CkArrayIndexBitVec index(children[i][k]);
01529 aProxy[index].insert(cmsg[j++]);
01530
01531 }
01532 aProxy.doneInserting();
01533
01534 delete [] cmsg;
01535 }
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547 void Cell2D :: reg_nbor_msg(int neighbor_side, NeighborMsg *msg)
01548 {
01549 int i;
01550
01551 if(msg->numbits > myIndex.numbits) {
01552
01553
01554 if(nborRecvMsgCount && nborRecvMsgCount[neighbor_side] == 0){
01555 nborRecvMsgCount[neighbor_side]++;
01556 nborRecvMsgBuff[neighbor_side][0] = msg;
01557 }
01558 else if(nborRecvMsgCount && nborRecvMsgCount[neighbor_side] == 1){
01559 nborRecvMsgCount[neighbor_side]++;
01560 nborRecvMsgBuff[neighbor_side][1] = msg;
01561 userData->combineAndStore(nborRecvMsgBuff[neighbor_side][0],nborRecvMsgBuff[neighbor_side][1]);
01562 delete nborRecvMsgBuff[neighbor_side][0];
01563 delete nborRecvMsgBuff[neighbor_side][1];
01564 neighbors_reported++;
01565 }
01566 else {
01567 CkError("Error in the message received from %d neighbor :my_x %d my_y %d myZ %d\n",
01568 neighbor_side, myIndex.vec[0], myIndex.vec[1],myIndex.vec[2]);
01569 }
01570 }
01571 else if (msg->numbits == myIndex.numbits) {
01572 if(nborRecvMsgCount && nborRecvMsgCount[neighbor_side] == 0){
01573 nborRecvMsgCount[neighbor_side] +=2;
01574 neighbors_reported++;
01575 DEBUG(("Calling Store: x %d y %d z %dnobits %d nborside %d\n",
01576 myIndex.vec[0], myIndex.vec[1],myIndex.vec[2],myIndex.numbits,
01577 neighbor_side));
01578 userData->store(msg);
01579 delete msg;
01580 }
01581 else
01582 CkError("Wrong size message received from %d neighbor :my_x %d my_y %d my_z %d mybits %d msgbits %d\n"
01583 , neighbor_side, myIndex.vec[0],myIndex.vec[1],myIndex.vec[2],
01584 myIndex.numbits,msg->numbits);
01585 }
01586 else
01587 CkError("Bigger Message received then my grainsize from %d neighbor :my_x %d my_y %d my_z %d numbits %d msg numbits %d\n",
01588 neighbor_side,
01589 myIndex.vec[0],
01590 myIndex.vec[1],
01591 myIndex.vec[2],
01592 myIndex.numbits,
01593 msg->numbits);
01594 }
01595
01596
01597
01598 void Cell2D :: forwardSplitMsg(NeighborMsg *msg ,int neighbor_side)
01599 {
01600
01601 switch(neighbor_side) {
01602 case NEG_Y:
01603
01604
01605 frag_msg(msg, 0, 0, 0,1);
01606 break;
01607 case POS_Y:
01608
01609
01610 frag_msg(msg, 1,0,1,1);
01611 break;
01612 case NEG_X:
01613
01614
01615 frag_msg(msg,0,0,1,0);
01616 break;
01617 case POS_X:
01618
01619
01620 frag_msg(msg,0,1,1,1);
01621 break;
01622
01623 }
01624 }
01625
01626 void Cell2D :: frag_msg(NeighborMsg *msg, int child1_x, int child1_y,
01627 int child2_x, int child2_y)
01628 {
01629
01630 int i;
01631 NeighborMsg ** splitMsgArray = userData->fragment(msg,2);
01632
01633 for(i=0;i<2;i++){
01634 if(splitMsgArray[i]){
01635 splitMsgArray[i]->which_neighbor = msg->which_neighbor;
01636 splitMsgArray[i]->run_until = msg->run_until;
01637 splitMsgArray[i]->numbits = msg->numbits;
01638 splitMsgArray[i]->nborIdx = msg->nborIdx;
01639 }
01640 else
01641 CkError("Error: 2 messages were not recieved by frag_msg from fragment\n");
01642 }
01643
01644
01645 CkArrayIndexBitVec index1(children[child1_x][child1_y]);
01646 arrayProxy[index1].neighbor_data(splitMsgArray[0]);
01647 CkArrayIndexBitVec index2(children[child2_x][child2_y]);
01648 arrayProxy[index2].neighbor_data(splitMsgArray[1]);
01649 delete msg;
01650 }
01651
01652
01653
01654
01655
01656
01657
01658 Cell3D :: Cell3D(_ArrInitMsg *msg )
01659 {
01660 int i, j, k;
01661
01662 DEBUGT(("Cell 3D constructor \n"));
01663 dimension = 3;
01664 CProxy_Cell3D aProxy(thisArrayID);
01665 arrayProxy = *(CProxy_Cell*) &aProxy;
01666
01667 init_cell(msg);
01668 DEBUGT(("Init Msg done \n"));
01669
01670
01671 treeSetup(msg);
01672 DEBUGT(("Tree SetUp done\n"));
01673
01674 delete msg;
01675
01676 DEBUGT(("Deleted the message\n"));
01677
01678 if(type == 'l') {
01679 userData = AmrUserData::createDataWrapper(myIndex,dimension);
01680 doIterations();
01681 }
01682 else
01683 userData = NULL;
01684 }
01685
01686 void Cell3D :: create_children(_ArrInitMsg** cmsg)
01687 {
01688 int i,j,k;
01689 CProxy_Cell3D aProxy = *(CProxy_Cell3D *) &arrayProxy;
01690
01691 for(i =0; i<dimension;i++) {
01692 int temp = powerOfTwo(i);
01693 if (i==0)
01694 temp = 2;
01695 for(j=0; j<temp;j++) {
01696 for(k =0; k<dimension ; k++)
01697 children[i][j].vec[k] = 2*myIndex.vec[k];
01698 children[i][j].numbits = myIndex.numbits + dimension;
01699 }
01700 }
01701 children[0][1].vec[0] += 0x0001;
01702 children[1][0].vec[1] += 0x0001;
01703 children[1][1].vec[0] += 0x0001;
01704 children[1][1].vec[1] += 0x0001;
01705
01706 children[2][0].vec[2] += 0x0001;
01707
01708 children[2][1].vec[2] += 0x0001;
01709 children[2][1].vec[0] += 0x0001;
01710
01711 children[2][2].vec[2] += 0x0001;
01712 children[2][2].vec[1] += 0x0001;
01713
01714 children[2][3].vec[2] += 0x0001;
01715 children[2][3].vec[0] += 0x0001;
01716 children[2][3].vec[1] += 0x0001;
01717
01718
01719
01720 j = 0;
01721 for(i= 0; i<dimension ; i++) {
01722 int size = powerOfTwo(i);
01723 if (i==0)
01724 size = 2;
01725 for(k=0; k<size; k++) {
01726
01727 CkArrayIndexBitVec index(children[i][k]);
01728 if(cmsg[j])
01729 DEBUGT(("Child msg is good \n"));
01730 else
01731 DEBUGT(("There is a problem dude\n"));
01732 aProxy[index].insert(cmsg[j++]);
01733
01734 }
01735 }
01736 aProxy.doneInserting();
01737
01738 delete [] cmsg;
01739 }
01740
01741
01742 void Cell3D :: forwardSplitMsg(NeighborMsg *msg ,int neighbor_side)
01743 {
01744
01745 switch(neighbor_side) {
01746 case NEG_Y:
01747
01748
01749
01750
01751
01752 frag_msg(msg, 0, 0, 0,1,2,0,2,1);
01753 break;
01754 case POS_Y:
01755
01756
01757
01758
01759
01760 frag_msg(msg, 1,0,1,1,2,2,2,3);
01761 break;
01762 case NEG_X:
01763
01764
01765
01766
01767 frag_msg(msg,0,0,1,0,2,0,2,2);
01768 break;
01769 case POS_X:
01770
01771
01772
01773
01774 frag_msg(msg,0,1,1,1,2,1,2,3);
01775 break;
01776 case NEG_Z:
01777
01778
01779
01780
01781 frag_msg(msg,0,0,0,1,1,0,1,1);
01782 break;
01783 case POS_Z:
01784
01785
01786
01787
01788 frag_msg(msg,2,0,2,1,2,2,2,3);
01789 break;
01790 }
01791 }
01792
01793 void Cell3D :: frag_msg(NeighborMsg *msg, int child1_x, int child1_y,
01794 int child2_x, int child2_y, int child3_x,
01795 int child3_y, int child4_x, int child4_y)
01796 {
01797 int i;
01798 NeighborMsg ** splitMsgArray = userData->fragment(msg,4);
01799
01800 for(i=0;i<4;i++){
01801 if(splitMsgArray[i]){
01802 splitMsgArray[i]->which_neighbor = msg->which_neighbor;
01803 splitMsgArray[i]->run_until = msg->run_until;
01804 splitMsgArray[i]->numbits = msg->numbits;
01805 splitMsgArray[i]->nborIdx = msg->nborIdx;
01806 }
01807 else
01808 CkError("Error: 4 messages were not recieved by frag_msg from fragment\n");
01809 }
01810
01811 CkArrayIndexBitVec index1(children[child1_x][child1_y]);
01812 arrayProxy[index1].neighbor_data(splitMsgArray[0]);
01813 CkArrayIndexBitVec index2(children[child2_x][child2_y]);
01814 arrayProxy[index2].neighbor_data(splitMsgArray[1]);
01815 CkArrayIndexBitVec index3(children[child3_x][child3_y]);
01816 arrayProxy[index3].neighbor_data(splitMsgArray[2]);
01817 CkArrayIndexBitVec index4(children[child4_x][child4_y]);
01818 arrayProxy[index4].neighbor_data(splitMsgArray[3]);
01819 delete msg;
01820 }
01821
01822 void Cell3D :: reg_nbor_msg(int neighbor_side, NeighborMsg *msg)
01823 {
01824 int i;
01825
01826 if(msg->numbits > myIndex.numbits) {
01827
01828
01829 if(nborRecvMsgCount && nborRecvMsgCount[neighbor_side] < 3){
01830 nborRecvMsgBuff[neighbor_side][nborRecvMsgCount[neighbor_side]] = msg;
01831 nborRecvMsgCount[neighbor_side]++;
01832 }
01833 else if(nborRecvMsgCount && nborRecvMsgCount[neighbor_side] == 3){
01834 nborRecvMsgCount[neighbor_side]++;
01835 nborRecvMsgBuff[neighbor_side][3] = msg;
01836 userData->combineAndStore(nborRecvMsgBuff[neighbor_side][0],nborRecvMsgBuff[neighbor_side][1],nborRecvMsgBuff[neighbor_side][2],nborRecvMsgBuff[neighbor_side][3]);
01837 delete nborRecvMsgBuff[neighbor_side][0];
01838 delete nborRecvMsgBuff[neighbor_side][1];
01839 delete nborRecvMsgBuff[neighbor_side][2];
01840 delete nborRecvMsgBuff[neighbor_side][3];
01841 for(int j =0; j<4;j++)
01842 nborRecvMsgBuff[neighbor_side][j] = NULL;
01843 neighbors_reported++;
01844 }
01845 else {
01846 CkError("Error in the message received from %d neighbor :my_x %d my_y %d my_z %d\n",
01847 neighbor_side, myIndex.vec[0], myIndex.vec[1],myIndex.vec[2]);
01848 }
01849 }
01850 else if (msg->numbits == myIndex.numbits) {
01851 if(nborRecvMsgCount){
01852 if(nborRecvMsgCount[neighbor_side] == 0){
01853 nborRecvMsgCount[neighbor_side] +=4;
01854 neighbors_reported++;
01855 DEBUGJ(("Calling Store: x %d y %d z %d nobits %d nborside %d\n",
01856 myIndex.vec[0], myIndex.vec[1],myIndex.vec[2],
01857 myIndex.numbits, neighbor_side));
01858 userData->store(msg);
01859 delete msg;
01860 }
01861 else
01862 CkError("Receiving the message from the same neighbor twice x %d y %d \
01863 z %d bits %d nborSide %d:nbor x %d y %d z %d bits %d count %d\n",
01864 myIndex.vec[0],myIndex.vec[1],myIndex.vec[2],myIndex.numbits,
01865 neighbor_side,msg->nborIdx.vec[0],msg->nborIdx.vec[1],
01866 msg->nborIdx.vec[2],msg->nborIdx.numbits,nborRecvMsgCount[neighbor_side]);
01867 }
01868 else
01869 CkError("NborRecvPoint Null Pointer for nborSide %d :my_x %d my_y %d my_z %d mybits %d msgbits %d count %d\n"
01870 ,neighbor_side, myIndex.vec[0],myIndex.vec[1],
01871 myIndex.vec[2],myIndex.numbits,msg->numbits,
01872 nborRecvMsgCount[neighbor_side]);
01873 }
01874 else
01875 CkError("Bigger Message received then my grainsize from %d neighbor :my_x %d my_y %d my_z %d numbits %d msg numbits %d\n",
01876 neighbor_side,
01877 myIndex.vec[0],
01878 myIndex.vec[1],
01879 myIndex.vec[2],
01880 myIndex.numbits,
01881 msg->numbits);
01882 }
01883
01884
01885
01886
01887
01888
01889
01890 Cell1D :: Cell1D(_ArrInitMsg *msg )
01891 {
01892 int i, j, k;
01893
01894 DEBUGT(("Cell 1D constructor \n"));
01895 dimension = 1;
01896 CProxy_Cell1D aProxy(thisArrayID);
01897 arrayProxy = *(CProxy_Cell*) &aProxy;
01898
01899 init_cell(msg);
01900 DEBUGT(("Init Msg done \n"));
01901
01902
01903 treeSetup(msg);
01904 DEBUGT(("Tree SetUp done\n"));
01905
01906 delete msg;
01907
01908 DEBUGT(("Deleted the message\n"));
01909
01910 if(type == 'l') {
01911
01912
01913 userData = AmrUserData::createDataWrapper(myIndex,dimension);
01914 DEBUGA(("Calling doIteration from the constructor\n"));
01915 doIterations();
01916
01917 }
01918 else
01919 userData = NULL;
01920 }
01921
01922 void Cell1D :: create_children(_ArrInitMsg** cmsg)
01923 {
01924 int i,j,k;
01925 CProxy_Cell1D aProxy = *(CProxy_Cell1D *) &arrayProxy;
01926
01927 for(i =0; i<dimension;i++) {
01928 for(j=0; j<2;j++) {
01929 for(k =0; k<dimension ; k++)
01930 children[i][j].vec[k] = 2*myIndex.vec[k];
01931 children[i][j].numbits = myIndex.numbits + dimension;
01932 }
01933 }
01934 children[0][1].vec[0] += 0x0001;
01935
01936
01937 j = 0;
01938 for(i= 0; i<dimension ; i++)
01939 for(k=0; k<2; k++) {
01940
01941 CkArrayIndexBitVec index(children[i][k]);
01942 if(cmsg[j])
01943 DEBUG(("Child msg is good \n"));
01944 else
01945 CkError(("There is a problem dude\n"));
01946 aProxy[index].insert(cmsg[j++]);
01947
01948 }
01949 aProxy.doneInserting();
01950
01951 delete [] cmsg;
01952 }
01953
01954
01955 void Cell1D :: reg_nbor_msg(int neighbor_side, NeighborMsg *msg)
01956 {
01957 int i;
01958
01959 if(msg->numbits > myIndex.numbits) {
01960
01961
01962 CkError("Error: reg_nbor_msg-- should not have differernt numbits:msg bits %d , mybits %d\n",msg->numbits,myIndex.numbits);
01963 }
01964 else if (msg->numbits == myIndex.numbits) {
01965 if(nborRecvMsgCount && nborRecvMsgCount[neighbor_side] == 0){
01966 nborRecvMsgCount[neighbor_side] +=2;
01967 neighbors_reported++;
01968 DEBUG(("Calling Store: x %d y %d z %dnobits %d nborside %d\n",
01969 myIndex.vec[0], myIndex.vec[1],myIndex.vec[2],myIndex.numbits,
01970 neighbor_side));
01971 userData->store(msg);
01972 delete msg;
01973 }
01974 else
01975 CkError("Wrong size message received from %d neighbor :my_x %d my_y %d my_z %d mybits %d msgbits %d\n"
01976 , neighbor_side, myIndex.vec[0],myIndex.vec[1],myIndex.vec[2],
01977 myIndex.numbits,msg->numbits);
01978 }
01979 else
01980 CkError("Bigger Message received then my grainsize from %d neighbor :my_x %d my_y %d my_z %d numbits %d msg numbits %d\n",
01981 neighbor_side,
01982 myIndex.vec[0],
01983 myIndex.vec[1],
01984 myIndex.vec[2],
01985 myIndex.numbits,
01986 msg->numbits);
01987 }
01988
01989 void Cell1D :: forwardSplitMsg(NeighborMsg *msg ,int neighbor_side)
01990 {
01991
01992
01993
01994 switch (neighbor_side) {
01995 case NEG_X: {
01996
01997
01998 CkArrayIndexBitVec index1(children[0][0]);
01999 arrayProxy[index1].neighbor_data(msg);
02000 break;
02001 }
02002 case POS_X: {
02003
02004
02005 CkArrayIndexBitVec index2(children[0][1]);
02006 arrayProxy[index2].neighbor_data(msg);
02007 break;
02008 }
02009 }
02010
02011 }
02012
02013
02014
02015
02016
02017
02018 NeighborMsg ** AmrUserData :: fragment(NeighborMsg *msg,int nMsg)
02019 {
02020 NeighborMsg **msgArray = new NeighborMsg* [nMsg];
02021 int size = msg->dataSize;
02022 void **msgArrayData = fragmentNborData(msg->data, &size);
02023
02024 for(int i=0;i<nMsg;i++) {
02025 msgArray[i] = new NeighborMsg;
02026 msgArray[i]->dataSize = size;
02027 msgArray[i]->data = msgArrayData[i];
02028 }
02029 return msgArray;
02030 }
02031
02032 void AmrUserData :: combineAndStore(NeighborMsg* msg1, NeighborMsg *msg2)
02033 {
02034
02035 void *data[2];
02036 data[0] = msg1->data;
02037 data[1] = msg2->data;
02038 if(msg1->dataSize == msg2->dataSize)
02039 combineAndStore(data, msg1->dataSize,msg1->which_neighbor);
02040 else
02041 CkError("Error: AmrUserData::combineAndStore messages are of differentSizes\n");
02042
02043 }
02044
02045 void AmrUserData :: combineAndStore(NeighborMsg* msg1, NeighborMsg *msg2,NeighborMsg *msg3, NeighborMsg *msg4)
02046 {
02047
02048 void* data[4];
02049 data[0] = msg1->data;
02050 data[1] = msg2->data;
02051 data[2] = msg3->data;
02052 data[3] = msg4->data;
02053 combineAndStore(data, msg1->dataSize,msg1->which_neighbor);
02054
02055
02056
02057 }
02058
02059 void AmrUserData :: store(NeighborMsg *msg)
02060 {
02061 store(msg->data, msg->dataSize, msg->which_neighbor);
02062 }
02063
02064
02065
02066 #include "amr.def.h"