00001
00007 #ifndef __ARRAYREDISTRIBUTOR_H__
00008 #define __ARRAYREDISTRIBUTOR_H__
00009
00010 #include <vector>
00011 #include <list>
00012 #include <map>
00013 #include <cmath>
00014
00015
00016 #include<pup_stl.h>
00017
00018
00019 #if CMK_WITH_CONTROLPOINT
00020
00021
00028
00029 class redistributor2DMsg : public CMessage_redistributor2DMsg {
00030 public:
00031 int top;
00032 int left;
00033 int height;
00034 int width;
00035 int new_chare_cols;
00036 int new_chare_rows;
00037 int which_array;
00038 double *data;
00039 };
00040
00041
00042
00044 static int maxi(int a, int b){
00045 if(a>b)
00046 return a;
00047 else
00048 return b;
00049 }
00050
00052 static int mini(int a, int b){
00053 if(a<b)
00054 return a;
00055 else
00056 return b;
00057 }
00058
00059
00061 class redistributor2D: public CBase_redistributor2D {
00062 public:
00063
00064 std::map<int,double*> data_arrays;
00065 std::map<int,int> data_arrays_sizes;
00066
00068 CProxyElement_ArrayElement associatedArray;
00069
00070 int incoming_count;
00071 std::map<int,double*> data_arrays_incoming;
00072 std::map<int,int> data_arrays_incoming_sizes;
00073
00075 bool thisElemActive;
00076
00077 bool resizeGranulesHasBeenCalled;
00078
00079 std::vector<redistributor2DMsg *> bufferedMsgs;
00080
00081 private:
00082
00083
00084 void *fakeMemoryUsage;
00085
00086
00087 CkCallback dataRedistributedCallback;
00088
00089 int x_chares;
00090 int y_chares;
00091
00092 int data_width;
00093 int data_height;
00094
00095 int data_x_ghost;
00096 int data_y_ghost;
00097
00098
00099 public:
00100
00101 void pup(PUP::er &p) {
00102 p | data_arrays_sizes;
00103 p | data_arrays_incoming_sizes;
00104 p | incoming_count;
00105 p | associatedArray;
00106 p | thisElemActive;
00107
00108 p | dataRedistributedCallback;
00109
00110 p | resizeGranulesHasBeenCalled;
00111
00112 p | x_chares;
00113 p | y_chares;
00114 p | data_width;
00115 p | data_height;
00116 p | data_x_ghost;
00117 p | data_y_ghost;
00118
00119 if(p.isPacking() && fakeMemoryUsage!=NULL)
00120 free(fakeMemoryUsage);
00121
00122 fakeMemoryUsage = NULL;
00123
00125
00126
00127
00128 {
00129 std::map<int,int>::iterator iter;
00130 for(iter = data_arrays_sizes.begin(); iter != data_arrays_sizes.end(); iter++){
00131 int whichArray = iter->first;
00132 int arraySize = iter->second;
00133
00134
00135 p | whichArray;
00136
00137 if(p.isUnpacking())
00138 data_arrays[whichArray] = new double[arraySize];
00139
00140 PUParray(p,data_arrays[whichArray] ,arraySize);
00141
00142 if(p.isPacking())
00143 delete[] data_arrays[whichArray];
00144
00145 }
00146 }
00147
00148
00150 {
00151 std::map<int,int>::iterator iter;
00152 for(iter = data_arrays_incoming_sizes.begin(); iter != data_arrays_incoming_sizes.end(); iter++){
00153 int whichArray = iter->first;
00154 int arraySize = iter->second;
00155
00156
00157 p | whichArray;
00158
00159 if(p.isUnpacking() && data_arrays_incoming_sizes[whichArray] > 0)
00160 data_arrays_incoming[whichArray] = new double[arraySize];
00161
00162 PUParray(p,data_arrays_incoming[whichArray],arraySize);
00163
00164 if(p.isPacking())
00165 delete[] data_arrays_incoming[whichArray];
00166
00167 }
00168 }
00169
00170
00171 }
00172
00173
00174 void ckJustMigrated(){
00175
00176 }
00177
00178
00179
00180
00181
00182 int top_data_idx();
00183
00184 int bottom_data_idx();
00185
00186 int left_data_idx();
00187
00188 int right_data_idx();
00189
00190 int top_neighbor();
00191
00192 int bottom_neighbor();
00193
00194 int left_neighbor();
00195
00196 int right_neighbor();
00197
00198
00200 int mywidth();
00201
00202
00203
00204 int myheight();
00205
00206
00207
00208
00209
00210 int top_data_idx(int y, int y_total){
00211 return (data_height * y) / y_total;
00212 }
00213
00214 int bottom_data_idx(int y, int y_total){
00215 return ((data_height * (y+1)) / y_total) - 1;
00216 }
00217
00218 int left_data_idx(int x, int x_total){
00219 return (data_width * x) / x_total;
00220 }
00221
00222 int right_data_idx(int x, int x_total){
00223 return ((data_width * (x+1)) / x_total) - 1;
00224 }
00225
00226
00227 int top_data_idx(int y){
00228 return (data_height * y) / y_chares;
00229 }
00230
00231 int bottom_data_idx(int y){
00232 return ((data_height * (y+1)) / y_chares) - 1;
00233 }
00234
00235 int left_data_idx(int x){
00236 return (data_width * x) / x_chares;
00237 }
00238
00239 int right_data_idx(int x){
00240 return ((data_width * (x+1)) / x_chares) - 1;
00241 }
00242
00244 int who_owns_idx_x(int i){
00245 int w=0;
00246 while(1){
00247 if( i >= left_data_idx(w) && i <= right_data_idx(w) ){
00248 return w;
00249 }
00250 w++;
00251 }
00252 }
00253
00255 int who_owns_idx_y(int i){
00256 int w=0;
00257 while(1){
00258 if( i >= top_data_idx(w) && i <= bottom_data_idx(w) ){
00259 return w;
00260 }
00261 w++;
00262 }
00263 }
00264
00265
00266
00267
00268
00269
00270 int local_to_padded(int x, int y){
00271 CkAssert(thisElemActive);
00272 CkAssert(x < (mywidth()+data_x_ghost) && x >= (0-data_x_ghost) && y < (myheight()+data_y_ghost) && y >= (0-data_y_ghost) );
00273 return (mywidth()+2*data_x_ghost)*(y+data_y_ghost)+x+data_x_ghost;
00274 }
00275
00276
00277 double data_local(int which, int x, int y){
00278 CkAssert(local_to_padded(x,y) < data_arrays_sizes[which]);
00279 return data_arrays[which][local_to_padded(x,y)];
00280 }
00281
00282
00283
00284 int local_to_global_x(int x){
00285 return left_data_idx() + x;
00286 }
00287
00288
00289 int local_to_global_y(int y){
00290 return top_data_idx() + y;
00291 }
00292
00293 int global_array_width(){
00294 return data_width;
00295 }
00296
00297 int global_array_height(){
00298 return data_height;
00299 }
00300
00301 int global_array_size(){
00302 return global_array_width() * global_array_height();
00303 }
00304
00305 int my_array_width(){
00306 return mywidth()+2*data_x_ghost;
00307 }
00308
00309 int my_array_height(){
00310 return myheight()+2*data_y_ghost;
00311 }
00312
00313
00314 int my_array_size(){
00315 return my_array_width() * my_array_height();
00316 }
00317
00319 template <typename t> t* createDataArray(int which=0) {
00320 t* data = new t[my_array_size()];
00321 data_arrays[which] = data;
00322 data_arrays_sizes[which] = my_array_size();
00323
00324 if(thisIndex.x==0 && thisIndex.y==0)
00325 CkPrintf("data_arrays_sizes[which] set to %d\n", data_arrays_sizes[which] );
00326
00327
00328 CkAssert(data_arrays[which] != NULL);
00329 #if DEBUG > 2
00330 CkPrintf("Allocated array of size %d at %p\n", my_array_size(), data_arrays[which] );
00331 #endif
00332 return data;
00333 }
00334
00335 template <typename t> t* getDataArray(int which=0) {
00336 return data_arrays[which];
00337 }
00338
00341 void setInitialDimensions(int width, int height, int x_chares_, int y_chares_, int x_ghosts=0, int y_ghosts=0){
00342 data_width = width;
00343 data_height = height;
00344 data_x_ghost = x_ghosts;
00345 data_y_ghost = y_ghosts;
00346
00347 setDimensions(x_chares_, y_chares_);
00348
00349 }
00350
00351
00352 void setDimensions( int x_chares_, int y_chares_){
00353 x_chares = x_chares_;
00354 y_chares = y_chares_;
00355
00356
00357 if( thisIndex.x < x_chares && thisIndex.y < y_chares ){
00358 thisElemActive = true;
00359 } else {
00360 thisElemActive = false;
00361 }
00362
00363 }
00364
00365
00366 redistributor2D(){
00367 incoming_count = 0;
00368 fakeMemoryUsage = NULL;
00369 CkAssert(bufferedMsgs.empty());
00370 }
00371
00372
00373 redistributor2D(CkMigrateMessage*){
00374 CkAssert(bufferedMsgs.empty());
00375 }
00376
00377
00378 void startup(){
00379 #if DEBUG > 3
00380 CkPrintf("redistributor 2D startup %03d,%03d\n", thisIndex.x, thisIndex.y);
00381 #endif
00382
00383 contribute();
00384 }
00385
00386
00387 void printArrays(){
00388 #if DEBUG > 2
00389 CkAssert(data_arrays.size()==2);
00390 for(std::map<int,double*>::iterator diter = data_arrays.begin(); diter != data_arrays.end(); diter++){
00391 int which_array = diter->first;
00392 double *data = diter->second;
00393 CkPrintf("%d,%d data_arrays[%d] = %p\n", thisIndex.x, thisIndex.y, which_array, data);
00394 }
00395 #endif
00396 }
00397
00398
00399
00400 void resizeGranules(int new_active_chare_cols, int new_active_chare_rows){
00401 #if DEBUG>1
00402 CkPrintf("Resize Granules called for elem %d,%d\n", thisIndex.x, thisIndex.y);
00403 #endif
00404
00405 resizeGranulesHasBeenCalled = true;
00406
00407 const bool previouslyActive = thisElemActive;
00408 const int old_top = top_data_idx();
00409 const int old_left = left_data_idx();
00410 const int old_bottom = top_data_idx()+myheight()-1;
00411 const int old_right = left_data_idx()+mywidth()-1;
00412 const int old_myheight = myheight();
00413 const int old_mywidth = mywidth();
00414
00415 setDimensions(new_active_chare_cols, new_active_chare_rows);
00416
00417 const int new_mywidth = mywidth();
00418 const int new_myheight = myheight();
00419
00420
00421
00422
00423 if(previouslyActive){
00424
00425
00426
00427 int newOwnerXmin = who_owns_idx_x(old_left);
00428 int newOwnerXmax = who_owns_idx_x(old_right);
00429 int newOwnerYmin = who_owns_idx_y(old_top);
00430 int newOwnerYmax = who_owns_idx_y(old_bottom);
00431
00432 for(int newx=newOwnerXmin; newx<=newOwnerXmax; newx++){
00433 for(int newy=newOwnerYmin; newy<=newOwnerYmax; newy++){
00434
00435
00436 #if DEBUG > 2
00437 CkPrintf("newy(%d)*new_myheight(%d)=%d, old_top=%d\n",newy,new_myheight,newy*new_myheight,old_top);
00438 #endif
00439
00440 int global_top = maxi(top_data_idx(newy),old_top);
00441 int global_left = maxi(left_data_idx(newx),old_left);
00442 int global_bottom = mini(bottom_data_idx(newy),old_bottom);
00443 int global_right = mini(right_data_idx(newx),old_right);
00444 int w = global_right-global_left+1;
00445 int h = global_bottom-global_top+1;
00446
00447 CkAssert(w*h>0);
00448
00449 int x_offset = global_left - old_left;
00450 int y_offset = global_top - old_top;
00451
00452 #if DEBUG > 2
00453 CkPrintf("w=%d h=%d x_offset=%d y_offset=%d\n", w, h, x_offset, y_offset);
00454 #endif
00455
00456 std::map<int,double*>::iterator diter;
00457 for(diter =data_arrays.begin(); diter != data_arrays.end(); diter++){
00458
00459 redistributor2DMsg* msg = new(w*h) redistributor2DMsg;
00460
00461
00462 int which_array = diter->first;
00463 double *t = diter->second;
00464 int s = data_arrays_sizes[which_array];
00465
00466 for(int j=0; j<h; j++){
00467 for(int i=0; i<w; i++){
00468 CkAssert(j*w+i < w*h);
00469 CkAssert((data_x_ghost*2+old_mywidth)*(j+y_offset+data_y_ghost)+(i+ x_offset+data_x_ghost) < s);
00470 msg->data[j*w+i] = t[(data_x_ghost*2+old_mywidth)*(j+y_offset+data_y_ghost)+(i+ x_offset+data_x_ghost)];
00471 }
00472 }
00473
00474 msg->top = global_top;
00475 msg->left = global_left;
00476 msg->height = h;
00477 msg->width = w;
00478 msg->new_chare_cols = new_active_chare_cols;
00479 msg->new_chare_rows = new_active_chare_rows;
00480 msg->which_array = which_array;
00481
00482
00483 thisProxy(newx, newy).receiveTransposeData(msg);
00484
00485 }
00486
00487 }
00488
00489
00490 }
00491 }
00492
00493 if(!thisElemActive){
00494 #if DEBUG > 2
00495 CkPrintf("Element %d,%d is no longer active\n", thisIndex.x, thisIndex.y);
00496 #endif
00497
00498
00499 for(std::map<int,double*>::iterator diter = data_arrays.begin(); diter != data_arrays.end(); diter++){
00500 int which_array = diter->first;
00501 delete data_arrays[which_array];
00502 data_arrays[which_array] = NULL;
00503 data_arrays_sizes[which_array] = 0;
00504 }
00505 continueToNextStep();
00506
00507 }
00508
00509
00510
00511 int size = bufferedMsgs.size();
00512 for(int i=0;i<size;i++){
00513 redistributor2DMsg *msg = bufferedMsgs[i];
00514
00515 receiveTransposeData(msg);
00516 }
00517 bufferedMsgs.clear();
00518
00519 int newPe = (thisIndex.y * new_active_chare_cols + thisIndex.x) % CkNumPes();
00520 if(newPe == CkMyPe()){
00521
00522 }
00523 else{
00524
00525 migrateMe(newPe);
00526 }
00527
00528 }
00529
00530
00531 void continueToNextStep(){
00532 #if DEBUG > 2
00533 CkPrintf("Elem %d,%d is ready to continue\n", thisIndex.x, thisIndex.y);
00534 #endif
00535
00536 resizeGranulesHasBeenCalled = false;
00537
00538 for(std::map<int,double*>::iterator diter =data_arrays.begin(); diter != data_arrays.end(); diter++){
00539 int which_array = diter->first;
00540 double *data = diter->second;
00541 if( ! ((data==NULL && !thisElemActive) || (data!=NULL && thisElemActive) )){
00542 CkPrintf("[%d] ERROR: ! ((data==NULL && !thisElemActive) || (data!=NULL && thisElemActive) )",CkMyPe());
00543 CkPrintf("[%d] ERROR: data=%p thisElemActive=%d (perhaps continueToNextStep was called too soon)\n",CkMyPe(), data, (int)thisElemActive );
00544
00545 CkAbort("ERROR");
00546 }
00547 }
00548
00549
00550 #if USE_EXTRAMEMORY
00551 #error NO USE_EXTRAMEMORY ALLOWED YET
00552 if(thisElemActive){
00553
00554 long totalArtificialMemory = controlPoint("Artificial Memory Usage", 100, 500);
00555 long artificialMemoryPerChare = totalArtificialMemory *1024*1024 / x_chares / y_chares;
00556
00557 CkPrintf("Allocating fake memory of %d MB (of the total %d MB) (xchares=%d y_chares=%d)\n", artificialMemoryPerChare/1024/1024, totalArtificialMemory, x_chares, y_chares);
00558 free(fakeMemoryUsage);
00559 fakeMemoryUsage = malloc(artificialMemoryPerChare);
00560 CkAssert(fakeMemoryUsage != NULL);
00561 } else {
00562 free(fakeMemoryUsage);
00563 fakeMemoryUsage = NULL;
00564 }
00565 #endif
00566
00567
00568
00569 incoming_count = 0;
00570 contribute();
00571 }
00572
00573
00574
00575
00576
00577
00578
00579 void receiveTransposeData(redistributor2DMsg *msg){
00580
00581
00582 if(!resizeGranulesHasBeenCalled){
00583 bufferedMsgs.push_back(msg);
00584
00585 return;
00586 }
00587
00588 CkAssert(resizeGranulesHasBeenCalled);
00589
00590 int top_new = top_data_idx(thisIndex.y, msg->new_chare_rows);
00591 int bottom_new = bottom_data_idx(thisIndex.y, msg->new_chare_rows);
00592 int left_new = left_data_idx(thisIndex.x, msg->new_chare_cols);
00593 int right_new = right_data_idx(thisIndex.x, msg->new_chare_cols);
00594
00595 int new_height = bottom_new - top_new + 1;
00596 int new_width = right_new - left_new + 1;
00597
00598 if(incoming_count == 0){
00599
00600 std::map<int,double*>::iterator diter;
00601 for(diter =data_arrays.begin(); diter != data_arrays.end(); diter++){
00602 int w = diter->first;
00603 data_arrays_incoming[w] = new double[(new_width+2*data_x_ghost)*(new_height+2*data_y_ghost)];
00604 data_arrays_incoming_sizes[w] = (new_width+2*data_x_ghost)*(new_height+2*data_y_ghost);
00605
00606
00607
00608 }
00609 }
00610
00611
00612
00613
00614
00615
00616 double *localData = data_arrays_incoming[msg->which_array];
00617 int s = data_arrays_incoming_sizes[msg->which_array];
00618
00619
00620
00621 CkAssert(localData != NULL);
00622
00623 for(int j=0; j<msg->height; j++){
00624 for(int i=0; i<msg->width; i++){
00625
00626 if( (msg->top+j >= top_new) && (msg->top+j <= bottom_new) && (msg->left+i >= left_new) && (msg->left+i <= right_new) ) {
00627 CkAssert(j*msg->width+i<msg->height*msg->width);
00628 CkAssert((msg->top+j-top_new)*new_width+(msg->left+i-left_new) < new_width*new_height);
00629 CkAssert((msg->top+j-top_new)*new_width+(msg->left+i-left_new) >= 0);
00630
00631 CkAssert((msg->top+j-top_new+data_y_ghost)*(new_width+2*data_x_ghost)+(msg->left+i-left_new+data_x_ghost) < s);
00632 localData[(msg->top+j-top_new+data_y_ghost)*(new_width+2*data_x_ghost)+(msg->left+i-left_new+data_x_ghost)] = msg->data[j*msg->width+i];
00633 incoming_count++;
00634
00635 }
00636
00637 }
00638 }
00639
00640
00641 delete msg;
00642
00643
00644 if(incoming_count == new_height*new_width*data_arrays.size()){
00645
00646 std::map<int,double*>::iterator diter;
00647 for(diter =data_arrays.begin(); diter != data_arrays.end(); diter++){
00648 int w = diter->first;
00649 delete[] data_arrays[w];
00650 data_arrays[w] = data_arrays_incoming[w];
00651 data_arrays_sizes[w] = data_arrays_incoming_sizes[w];
00652 data_arrays_incoming[w] = NULL;
00653 data_arrays_incoming_sizes[w] = 0;
00654
00655
00656
00657
00658
00659
00660
00661 }
00662
00663 continueToNextStep();
00664 }
00665
00666 }
00667 };
00668
00670 #endif
00671 #endif