tapioca.cpp 34.5 KB
Newer Older
1
#include "tapioca.hpp"
Francois Tessier's avatar
Francois Tessier committed
2

Francois Tessier's avatar
Francois Tessier committed
3
Tapioca::Tapioca ()
Francois Tessier's avatar
Francois Tessier committed
4 5 6 7 8 9
{
  this->SetDefaultValues ();
  this->ParseEnvVariables ();
}


Francois Tessier's avatar
Francois Tessier committed
10
Tapioca::~Tapioca ()
Francois Tessier's avatar
Francois Tessier committed
11 12 13 14
{
}


15
void Tapioca::WriteInitialize (char *filename, int64_t *chunkCount, int *chunkSize, int64_t *chunkOffset,
Francois Tessier's avatar
Francois Tessier committed
16 17 18 19 20 21 22 23
			      int nChunks, int64_t offset, MEMORY_LAYOUT layout, MPI_Comm comm)
{
  int chunk;
#ifdef TIMING
  double startInitTime, endInitTime, startElectTime, endElectTime;
  startInitTime = MPI_Wtime();
#endif
  
24
  this->filename_ = filename;
Francois Tessier's avatar
Francois Tessier committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  this->nChunks_ = nChunks;
  this->chunksIndexMatching.resize (this->nChunks_);
  
  this->chunkCount_  = (int64_t *)malloc (this->nChunks_ * sizeof(int64_t));
  this->chunkSize_   = (int *)malloc (this->nChunks_ * sizeof(int));
  this->chunkOffset_ = (int64_t *)malloc (this->nChunks_ * sizeof(int64_t));
  memcpy (this->chunkCount_, chunkCount, this->nChunks_ * sizeof(int64_t));
  memcpy (this->chunkSize_, chunkSize, this->nChunks_ * sizeof(int));
  memcpy (this->chunkOffset_, chunkOffset, this->nChunks_ * sizeof(int64_t));

  for ( chunk = 0; chunk < this->nChunks_; chunk++ )
    this->rankDataSize_ += this->chunkCount_[chunk] * this->chunkSize_[chunk];
  
  this->offsetInFile_ = offset;
  this->layout_ = layout;
  MPI_Comm_dup (comm, &this->subComm_);
  
  this->SetCommValues ();
  this->SetOffsets ();

  if ( this->writeDevNull_ )
    MPI_File_open(MPI_COMM_SELF, "/dev/null",
		  MPI_MODE_WRONLY | MPI_MODE_CREATE,
		  MPI_INFO_NULL, &this->devNullFileHandle_);

#ifdef DEBUG
  if (this->commRank_ == MASTER) {
    fprintf (stdout, "[DEBUG] #Aggr        = %d   \n", this->nAggr_);
    fprintf (stdout, "[DEBUG] bufferSize   = %lld \n", this->bufferSize_);
    fprintf (stdout, "[DEBUG] commDataSize = %lld \n", this->commDataSize_);
    fprintf (stdout, "[DEBUG] strategy     = %s   \n", this->getStrategyName ());
  }
#endif

  this->SetNodesList ();

  this->IdentifyMyAggregators ();

#ifdef TIMING
  startElectTime = MPI_Wtime();
#endif

  this->ElectAggregators ();
  
#ifdef TIMING
  endElectTime = MPI_Wtime();
#endif
  
  this->InitAggregators ();

#ifdef TIMING
  endInitTime = MPI_Wtime();
  this->PrintTime(startInitTime, endInitTime, "Initialize");
  this->PrintTime(startElectTime, endElectTime, " |-> Elect aggregators");
#endif
}


83
void Tapioca::ReadInitialize (char *filename, int64_t *chunkCount, int *chunkSize, int64_t *chunkOffset,
84
			      int nChunks, int64_t offset, MEMORY_LAYOUT layout, MPI_Comm comm)
Francois Tessier's avatar
Francois Tessier committed
85
{
86 87 88 89 90 91
  int chunk;
#ifdef TIMING
  double startInitTime, endInitTime, startElectTime, endElectTime;
  startInitTime = MPI_Wtime();
#endif
  
92
  this->filename_ = filename;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  this->nChunks_ = nChunks;
  this->chunksIndexMatching.resize (this->nChunks_);
  
  this->chunkCount_  = (int64_t *)malloc (this->nChunks_ * sizeof(int64_t));
  this->chunkSize_   = (int *)malloc (this->nChunks_ * sizeof(int));
  this->chunkOffset_ = (int64_t *)malloc (this->nChunks_ * sizeof(int64_t));
  memcpy (this->chunkCount_, chunkCount, this->nChunks_ * sizeof(int64_t));
  memcpy (this->chunkSize_, chunkSize, this->nChunks_ * sizeof(int));
  memcpy (this->chunkOffset_, chunkOffset, this->nChunks_ * sizeof(int64_t));

  for ( chunk = 0; chunk < this->nChunks_; chunk++ )
    this->rankDataSize_ += this->chunkCount_[chunk] * this->chunkSize_[chunk];
  
  this->offsetInFile_ = offset;
  this->layout_ = layout;
  MPI_Comm_dup (comm, &this->subComm_);
  
  this->SetCommValues ();
  this->SetOffsets ();

#ifdef DEBUG
  if (this->commRank_ == MASTER) {
    fprintf (stdout, "[DEBUG] #Aggr        = %d   \n", this->nAggr_);
    fprintf (stdout, "[DEBUG] bufferSize   = %lld \n", this->bufferSize_);
    fprintf (stdout, "[DEBUG] commDataSize = %lld \n", this->commDataSize_);
    fprintf (stdout, "[DEBUG] strategy     = %s   \n", this->getStrategyName ());
  }
#endif

  this->SetNodesList ();

  //this->IdentifyMyAggregators ();

#ifdef TIMING
  startElectTime = MPI_Wtime();
#endif

  //this->ElectAggregators ();
Francois Tessier's avatar
Francois Tessier committed
131
  
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#ifdef TIMING
  endElectTime = MPI_Wtime();
#endif
  
  this->InitAggregators ();

#ifdef TIMING
  endInitTime = MPI_Wtime();
  this->PrintTime(startInitTime, endInitTime, "Initialize");
  this->PrintTime(startElectTime, endElectTime, " |-> Elect aggregators");
#endif
}


void Tapioca::Finalize ()
{
  this->chunksIndexMatching.clear();
  free (this->chunkCount_);
  free (this->chunkSize_);
  free (this->chunkOffset_);
  this->excludedNode.clear();
Francois Tessier's avatar
Francois Tessier committed
153 154 155 156

  MPI_Win_free (&this->RMAWin1);
  MPI_Win_free (&this->RMAWin2);

157 158
  MPI_Comm_free (&this->subComm_);

Francois Tessier's avatar
Francois Tessier committed
159 160 161 162 163
  free (this->buffer1);
  free (this->buffer2);
}


164
int Tapioca::Write (MPI_File fileHandle, MPI_Offset offset, void *buf, 
165
		    int count, MPI_Datatype datatype, MPI_Status *status, char* var, int64_t bufOffset)
Francois Tessier's avatar
Francois Tessier committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
  int retval, i, c, targetRoundIdx, targetAggrIdx, targetGlobAggr;
  int typeSize, targetAggr, win, buffer;
  bool multipleRounds = false;
  int64_t chunkDataSize, subChunkDataSize, cumulDataSize = 0, cumulDataSizeInRound;
  int64_t winOffset = 0, rankDataOffset, offsetInAggrData;
  MPI_Status iStatus;
  MPI_Request iRequest = NULL;
  

  MPI_Type_size (datatype, &typeSize);
  c                = this->nCommit_;
  chunkDataSize    = count * typeSize;
  subChunkDataSize = chunkDataSize;
  offsetInAggrData = offset - this->offsetInFile_;
  winOffset        = offsetInAggrData % this->bufferSize_;

  targetRoundIdx   = (*this->chunksIndexMatching[c].begin());
  targetAggrIdx    = (*this->chunksIndexMatching[c].begin());
  if ( this->chunksIndexMatching[c].size() > 1 ) {
    multipleRounds   = true;
    subChunkDataSize = this->dataSize[targetRoundIdx];
    this->chunksIndexMatching[c].erase ( this->chunksIndexMatching[c].begin() );
  }
  
  /*
   *  Wait if it's not the appropriate round
   */
  while ( this->roundsIds[targetRoundIdx] > this->currentRound_ ) {
195
    /* TODO: Need to add a condition. What if only one aggr is necessary during the last round ?*/
Francois Tessier's avatar
Francois Tessier committed
196
    for ( i = 0; i < this->nAggr_ ; i++ ) {
197 198 199 200 201 202 203 204 205 206 207
#ifdef XC40
      if ( this->totalNeededBuffers_ != this->countNeededBuffers_ ) {
#endif
	fprintf (stdout, "[INFO] %d calls 1st GlobalFence, buff %d, dat %s (r: %d, f: %d)\n", this->commRank_, (this->currentRound_ % NBUFFERS), var, this->currentRound_, this->currentFence_);
	this->GlobalFence (var);
	fprintf (stdout, "[INFO] %d passes 1st GlobalFence, buff %d, dat %s (r: %d, f: %d)\n", this->commRank_, (this->currentRound_ % NBUFFERS), var, this->currentRound_, this->currentFence_);
	this->currentFence_++;
	this->countNeededBuffers_++;
#ifdef XC40
      }
#endif
Francois Tessier's avatar
Francois Tessier committed
208 209 210
    }
    
    if ( this->amAnAggr_ ) {
211 212
      // if (iRequest != NULL)
      //  	MPI_Wait ( &iRequest, &iStatus );
Francois Tessier's avatar
Francois Tessier committed
213
      fprintf (stdout, "[INFO] %d calls 1st Push (r: %d)\n", this->commRank_, this->currentRound_);
214 215
      this->Push (fileHandle, status);
      //this->iPush (fileHandle, &iRequest);
Francois Tessier's avatar
Francois Tessier committed
216 217
    }
    
Francois Tessier's avatar
Francois Tessier committed
218
    fprintf (stdout, "[INFO] %d increment 1st currentRound (r: %d)\n", this->commRank_, this->currentRound_);
Francois Tessier's avatar
Francois Tessier committed
219 220 221 222 223 224 225 226 227 228 229 230
    this->currentRound_++;
  }

#ifdef TIMING
    this->startAggrTime = MPI_Wtime();
#endif
    

  buffer         = this->currentRound_ % NBUFFERS;
  targetGlobAggr = this->globalAggregatorsRanks[targetAggrIdx];
  targetAggr     = this->aggregatorsRanks[targetAggrIdx];
  
Francois Tessier's avatar
Francois Tessier committed
231
  fprintf (stdout, "[INFO] %d Put %s data in buffer %d, agg %d (r: %d)\n", this->commRank_, var, buffer, targetGlobAggr, this->currentRound_);
Francois Tessier's avatar
Francois Tessier committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  switch (buffer)
    {
    case 0:
      retval = MPI_Put (static_cast<char*>(buf) + bufOffset, subChunkDataSize, MPI_BYTE,
			 targetAggr, winOffset, subChunkDataSize, MPI_BYTE, this->RMAWin1);
      this->HandleMPIError (retval);
      break;
    case 1:
      retval = MPI_Put (static_cast<char*>(buf) + bufOffset, subChunkDataSize, MPI_BYTE,
			 targetAggr, winOffset, subChunkDataSize, MPI_BYTE, this->RMAWin2);
      this->HandleMPIError (retval);
      break;
    }
 
  this->currentDataSize_ += subChunkDataSize;

  /*
   *  If all the data have been written, wait
   */
  if ( this->currentDataSize_ == this->rankDataSize_) {
252 253 254 255
    while ( this->countNeededBuffers_ < this->totalNeededBuffers_) {
      fprintf (stdout, "[INFO] %d calls 2nd GlobalFence, buff %d, dat %s (r: %d, f: %d)\n",
	       this->commRank_, (this->currentRound_ % NBUFFERS), var, this->currentRound_, this->currentFence_);

256
      this->GlobalFence (var);
Francois Tessier's avatar
Francois Tessier committed
257

258 259 260 261 262 263 264 265
      fprintf (stdout, "[INFO] %d passes 2nd GlobalFence, buff %d, dat %s (r: %d, f: %d)\n",
	       this->commRank_, (this->currentRound_ % NBUFFERS), var, this->currentRound_, this->currentFence_);
      this->currentFence_++;
      
#ifdef XC40
      this->countNeededBuffers_++;
#endif
      if ( (this->countNeededBuffers_ % this->nAggr_) == 0 ) {
Francois Tessier's avatar
Francois Tessier committed
266
	if ( this->amAnAggr_ ) {
267 268
	  // if (iRequest != NULL)
	  //   MPI_Wait ( &iRequest, &iStatus );
Francois Tessier's avatar
Francois Tessier committed
269
	  fprintf (stdout, "[INFO] %d calls 2nd Push (r: %d)\n", this->commRank_, this->currentRound_);
270 271
	  this->Push (fileHandle, status);
	  //this->iPush (fileHandle, &iRequest);
272
	}	
273
	fprintf (stdout, "[INFO] %d increment 2nd currentRound (r: %d, rc: %d, nag: %d)\n", this->commRank_, this->currentRound_, this->countNeededBuffers_, this->nAggr_);
274
	this->currentRound_++;	
Francois Tessier's avatar
Francois Tessier committed
275
      }
276 277 278
#ifdef BGQ
      this->countNeededBuffers_++;
#endif
Francois Tessier's avatar
Francois Tessier committed
279 280 281 282
    }
  }
  
  if ( multipleRounds ) {
Francois Tessier's avatar
Francois Tessier committed
283
    fprintf (stdout, "[INFO] %d has a second round (r: %d)\n", this->commRank_, this->currentRound_);
284
    retval = this->Write (fileHandle, offset + subChunkDataSize, buf, 
285
			  chunkDataSize - subChunkDataSize, MPI_BYTE, status, var, subChunkDataSize);
Francois Tessier's avatar
Francois Tessier committed
286 287 288 289 290
  }
  else {
    this->nCommit_ ++;
  }

291 292
  // if (iRequest != NULL)
  //   MPI_Wait ( &iRequest, &iStatus );
Francois Tessier's avatar
Francois Tessier committed
293 294 295 296 297

  return retval;
}


Francois Tessier's avatar
Francois Tessier committed
298
void Tapioca::Push (MPI_File fileHandle, MPI_Status *status)
Francois Tessier's avatar
Francois Tessier committed
299 300 301 302
{
  int64_t offset, dataSize;
  int win, buffer;

303
  fprintf (stdout, "[DEBUG] Enter Push\n");
Francois Tessier's avatar
Francois Tessier committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  buffer = this->currentRound_ % NBUFFERS;

  if ( this->amAnAggr_ ) {
#ifdef TIMING
    this->startIOTime = MPI_Wtime();
#endif
    offset   = (this->nAggr_ * this->currentRound_ + this->globalAggrRank_) * this->bufferSize_;
    offset   += this->offsetInFile_;
    
    dataSize = this->bufferSize_;
    if ( this->aggrDataSize_ < this->bufferSize_ )
      dataSize = this->aggrDataSize_;
    
    switch (buffer)
      {
      case 0: 
	if ( this->writeDevNull_ )
	  MPI_File_write_at (this->devNullFileHandle_, 0, buffer1, dataSize, MPI_BYTE, status);
	else
	  MPI_File_write_at (fileHandle, offset, buffer1, dataSize, MPI_BYTE, status);
	break;
      case 1:
	if ( this->writeDevNull_ )
	  MPI_File_write_at (this->devNullFileHandle_, 0, buffer2, dataSize, MPI_BYTE, status);
	else
	  MPI_File_write_at (fileHandle, offset, buffer2, dataSize, MPI_BYTE, status);
	break;
      }

    this->aggrDataSize_ -= dataSize;
#ifdef TIMING
    this->endIOTime = MPI_Wtime();
    this->totIOTime = this->endIOTime - this->startIOTime;
    if ( dataSize > 0 )
      fprintf (stdout, "[TIMING][AGG][IO] Agg. %d, Rnd %d - %.2f ms\n",
	       this->commRank_, this->currentRound_, this->totIOTime * 1000);
#endif
  }    
}

Francois Tessier's avatar
Francois Tessier committed
344
void Tapioca::iPush (MPI_File fileHandle, MPI_Request *request)
Francois Tessier's avatar
Francois Tessier committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
  int64_t offset, dataSize;
  int win, buffer;

  buffer = this->currentRound_ % NBUFFERS;

  if ( this->amAnAggr_ ) {
#ifdef TIMING
    this->startIOTime = MPI_Wtime();
#endif
    offset   = (this->nAggr_ * this->currentRound_ + this->globalAggrRank_) * this->bufferSize_;
    offset   += this->offsetInFile_;
    
    dataSize = this->bufferSize_;
    if ( this->aggrDataSize_ < this->bufferSize_ )
      dataSize = this->aggrDataSize_;
    
    switch (buffer)
      {
      case 0: 
	if ( this->writeDevNull_ )
	  MPI_File_iwrite_at (this->devNullFileHandle_, 0, buffer1, dataSize, MPI_BYTE, request);
	else
	  MPI_File_iwrite_at (fileHandle, offset, buffer1, dataSize, MPI_BYTE, request);
	break;
      case 1:
	if ( this->writeDevNull_ )
	  MPI_File_iwrite_at (this->devNullFileHandle_, 0, buffer2, dataSize, MPI_BYTE, request);
	else
	  MPI_File_iwrite_at (fileHandle, offset, buffer2, dataSize, MPI_BYTE, request);
	break;
      }

    this->aggrDataSize_ -= dataSize;
#ifdef TIMING
    this->endIOTime = MPI_Wtime();
    this->totIOTime = this->endIOTime - this->startIOTime;
    if ( dataSize > 0 )
      fprintf (stdout, "[TIMING][AGG][IO] Agg. %d, Rnd %d - %.2f ms\n",
	       this->commRank_, this->currentRound_, this->totIOTime * 1000);
#endif
  }    
}


390
void Tapioca::GlobalFence (char* var) 
Francois Tessier's avatar
Francois Tessier committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
{
  int buffer;

  buffer = this->currentRound_ % NBUFFERS;

  switch (buffer)
    {
    case 0:
      MPI_Win_fence (0, this->RMAWin1);
      break;
    case 1:
      MPI_Win_fence (0, this->RMAWin2);
      break;
    }
  
#ifdef TIMING
  this->endAggrTime = MPI_Wtime();
  
  if ( this->startAggrTime != 0 ) {
    this->totAggrTime = this->endAggrTime - this->startAggrTime;
    // fprintf (stdout, "[TIMING][AGG][AGGR] Rank %d, Rnd %d - %.2f ms\n",
    // 	     this->commRank_, this->currentRound_, this->totAggrTime * 1000);
  }
    
  this->startAggrTime = 0;
#endif

}


/***********************/
/*  INITIALIZATION     */
/***********************/
Francois Tessier's avatar
Francois Tessier committed
424
void Tapioca::SetDefaultValues ()
Francois Tessier's avatar
Francois Tessier committed
425 426 427 428
{
  this->rankDataSize_ = 0;
  this->strategy_ = SHORTEST_PATH;
  this->nAggr_ = 8;
429
  this->bufferSize_ = 16777216;
Francois Tessier's avatar
Francois Tessier committed
430
  this->amAnAggr_ = false;
431
  this->commSplit_ = true;
Francois Tessier's avatar
Francois Tessier committed
432
  this->currentRound_ = 0;
433
  this->countNeededBuffers_ = 0;
Francois Tessier's avatar
Francois Tessier committed
434 435 436
  this->currentDataSize_ = 0;
  this->nCommit_ = 0;
  this->writeDevNull_ = false;
Francois Tessier's avatar
Francois Tessier committed
437 438
  /* DEBUG */
  this->currentFence_ = 1;
Francois Tessier's avatar
Francois Tessier committed
439 440 441
}


Francois Tessier's avatar
Francois Tessier committed
442
void Tapioca::ParseEnvVariables ()
Francois Tessier's avatar
Francois Tessier committed
443
{
444 445 446 447 448
  char *envStrategy   = getenv("TAPIOCA_STRATEGY");
  char *envNAggr      = getenv("TAPIOCA_NBAGGR");
  char *envBufferSize = getenv("TAPIOCA_BUFFERSIZE");
  char *envSplit      = getenv("TAPIOCA_COMMSPLIT");
  char *envDevNull    = getenv("TAPIOCA_DEVNULL");
Francois Tessier's avatar
Francois Tessier committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
  
  if (envStrategy != NULL) {
    strcmp(envStrategy, "SHORTEST_PATH")    ? 0 : this->strategy_ = SHORTEST_PATH;
    strcmp(envStrategy, "LONGEST_PATH")     ? 0 : this->strategy_ = LONGEST_PATH;
    strcmp(envStrategy, "TOPOLOGY_AWARE")   ? 0 : this->strategy_ = TOPOLOGY_AWARE;
    strcmp(envStrategy, "CONTENTION_AWARE") ? 0 : this->strategy_ = CONTENTION_AWARE;
    strcmp(envStrategy, "UNIFORM")          ? 0 : this->strategy_ = UNIFORM;
  }

  if (envNAggr != NULL) {
    this->nAggr_ = atoi(envNAggr);
  }

  if (envBufferSize != NULL) {
    this->bufferSize_ = atoi(envBufferSize);
  }

  if (envSplit != NULL) {
    strcmp(envSplit, "true")  ? 0 : this->commSplit_ = true;
    strcmp(envSplit, "false") ? 0 : this->commSplit_ = false;
  }

  if (envDevNull != NULL) {
    strcmp(envDevNull, "true")  ? 0 : this->writeDevNull_ = true;
    strcmp(envDevNull, "false") ? 0 : this->writeDevNull_ = false;
  }
}


Francois Tessier's avatar
Francois Tessier committed
478
void Tapioca::SetCommValues ()
Francois Tessier's avatar
Francois Tessier committed
479 480 481 482 483 484 485 486 487
{
  MPI_Comm_rank (this->subComm_, &this->commRank_);
  MPI_Comm_size (this->subComm_, &this->commSize_);
  MPI_Comm_rank (MPI_COMM_WORLD, &this->worldRank_);

  MPI_Allreduce (&this->rankDataSize_, &this->commDataSize_, 1, MPI_LONG_LONG, MPI_SUM, this->subComm_);
}


Francois Tessier's avatar
Francois Tessier committed
488
void Tapioca::SetOffsets ()
Francois Tessier's avatar
Francois Tessier committed
489 490 491 492 493 494 495 496
{
  MPI_Exscan (&this->rankDataSize_, &this->offsetInAggrData_, 1, 
	      MPI_LONG_LONG, MPI_SUM, this->subComm_);
  if (this->commRank_ == 0)
    this->offsetInAggrData_ = 0;
}


Francois Tessier's avatar
Francois Tessier committed
497
void Tapioca::SetNodesList ()
Francois Tessier's avatar
Francois Tessier committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
{
  int *coords, *myCoords, i, worldSize, dimensions;
  
  MPI_Comm_size ( MPI_COMM_WORLD, &worldSize );
  coords = (int *)malloc (worldSize * sizeof (int));

  dimensions = topology.NetworkDimensions () + 1;
  myCoords = (int *)malloc (dimensions * sizeof (int));

  topology.RankToCoordinates (this->worldRank_, myCoords);
  this->intCoords_ = this->CoordsToInt (myCoords, dimensions - 1);
  
  MPI_Allgather(&this->intCoords_, 1, MPI_INT, coords, 1, MPI_INT, MPI_COMM_WORLD);

  for ( i = 0; i < worldSize; i++ )
    this->excludedNode[coords[i]] = false;
514 515 516

  free (coords);
  free (myCoords);
Francois Tessier's avatar
Francois Tessier committed
517 518 519 520 521 522
}


/***********************/
/*    AGGREGATION      */
/***********************/
Francois Tessier's avatar
Francois Tessier committed
523
int Tapioca::NumberOfAggregators ()
Francois Tessier's avatar
Francois Tessier committed
524 525 526 527 528
{
  return 0;
}


Francois Tessier's avatar
Francois Tessier committed
529
void Tapioca::IdentifyMyAggregators ()
Francois Tessier's avatar
Francois Tessier committed
530 531 532 533 534
{
  int i, j, c, globalRoundId, upperBound, index = 0;
  int64_t remainingData, offsetInAggrData;
  std::vector<Round_t> rounds;
  
535
  this->totalNeededBuffers_  = ceil ( (double)this->commDataSize_ / (double)this->bufferSize_ );
Francois Tessier's avatar
Francois Tessier committed
536

537
  for ( i = 0; i < this->totalNeededBuffers_; i++ ) {
Francois Tessier's avatar
Francois Tessier committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    Round_t r;
    r.aggr = i % this->nAggr_;
    r.round = i / this->nAggr_;
    rounds.push_back(r);
  }

  for ( c = 0; c < this->nChunks_; c++ ) {
    remainingData    = this->chunkCount_[c] * this->chunkSize_[c];
    offsetInAggrData = this->chunkOffset_[c] - this->offsetInFile_;
    globalRoundId = floor ( offsetInAggrData / this->bufferSize_ );
    upperBound = ( offsetInAggrData % this->bufferSize_ ) + remainingData;
    
    while ( remainingData > 0 ) {
      this->globalAggregatorsRanks.push_back ( rounds[globalRoundId].aggr );
      this->roundsIds.push_back ( rounds[globalRoundId].round );
      this->chunksIndexMatching[c].push_back ( index );
      
      if ( upperBound > this->bufferSize_) {
	remainingData = (upperBound - this->bufferSize_);
	this->dataSize.push_back ( this->chunkCount_[c] * this->chunkSize_[c] - remainingData );
      }
      else {
	this->dataSize.push_back ( remainingData );
	remainingData = 0;
      }
      
      upperBound -= this->bufferSize_;
      globalRoundId++;
      index++;
    }
  }
  
#ifdef DEBUG
Francois Tessier's avatar
Francois Tessier committed
571
  if (this->commRank_ == 4) {
Francois Tessier's avatar
Francois Tessier committed
572
    fprintf (stdout, "[DEBUG] Rounds distrib. on %d aggregators: AGG ", this->nAggr_);
573
    for ( i = 0; i < this->totalNeededBuffers_; i++ )
Francois Tessier's avatar
Francois Tessier committed
574 575
      fprintf (stdout, "%d ", rounds[i].aggr);
    fprintf (stdout, "\n                                          RND ");
576
    for ( i = 0; i < this->totalNeededBuffers_; i++ )
Francois Tessier's avatar
Francois Tessier committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
      fprintf (stdout, "%d ", rounds[i].round);
    fprintf (stdout, "\n                                          AGG ");
    for ( i = 0; i < this->globalAggregatorsRanks.size(); i++ )
      fprintf (stdout, "%d ", this->globalAggregatorsRanks[i]);
    fprintf (stdout, "\n                                          RID ");
    for ( i = 0; i < this->roundsIds.size(); i++ )
      fprintf (stdout, "%d ", this->roundsIds[i]);
    fprintf (stdout, "\n                                          DAS ");
    for ( i = 0; i < this->dataSize.size(); i++ )
      fprintf (stdout, "%d ", this->dataSize[i]);
    fprintf (stdout, "\n                                          CIM ");
    for ( i = 0; i < this->chunksIndexMatching.size(); i++ ) {
      fprintf (stdout, "{ ");
      for ( j = 0; j < this->chunksIndexMatching[i].size(); j++ )
	fprintf (stdout, "%d ", this->chunksIndexMatching[i][j]);
      fprintf (stdout, "}");
    }
    fprintf (stdout, "\n");
  }
#endif
}


Francois Tessier's avatar
Francois Tessier committed
600
void Tapioca::ElectAggregators ()
Francois Tessier's avatar
Francois Tessier committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
  int aggr, aggrRank, rankAggrComm, sizeAggrComm, aggrRankAggrComm, i, j, aggrCoords, worldSize;
  int64_t color;
  double startTime, endTime, totTime = 0.0;
  MPI_Comm aggrComm;
  MPI_Request request;
  MPI_Status status;
  /* Groups */
  MPI_Group commGroup, aggrGroup;
  int *ranks, *join, *groupRanks, groupSize, groupRank, joinGroup;

  MPI_Comm_size (MPI_COMM_WORLD, &worldSize);

  this->aggregatorsRanks.resize ( this->globalAggregatorsRanks.size() );
  
  for ( aggr = 0; aggr < this->nAggr_; aggr++ ) {
    color = this->DataSizeSentToAggr (aggr);
    
    startTime = MPI_Wtime();

    if ( this->commSplit_ )
      MPI_Comm_split (this->subComm_, color > 0, this->commRank_, &aggrComm);
    else {
      
      MPI_Comm_group (this->subComm_, &commGroup);

      if ( color > 0 ) {
	joinGroup = 1;
	MPI_Allreduce (&joinGroup, &groupSize, 1, MPI_INT, MPI_SUM, this->subComm_ );
      }
      else {
	joinGroup = 0;
	MPI_Allreduce (&joinGroup, &groupSize, 1, MPI_INT, MPI_SUM, this->subComm_ );
	groupSize = this->commSize_ - groupSize;
      }

      join = (int *)malloc (this->commSize_ * sizeof (int));
      MPI_Allgather(&joinGroup, 1, MPI_INT, join, 1, MPI_INT, this->subComm_);
      groupRanks = (int *)malloc (groupSize * sizeof (int));
      
      j = 0;
      for ( i = 0; i < this->commSize_; i++ ) {
	if (join[i] == joinGroup) {
	  groupRanks[j] = i;
	  j++;
	}
      }

      MPI_Group_incl (commGroup, groupSize, groupRanks, &aggrGroup);
      MPI_Comm_create (this->subComm_, aggrGroup, &aggrComm);
      
      free (join);
      free (groupRanks);
    }
    
    MPI_Comm_rank (aggrComm, &rankAggrComm);
    MPI_Comm_size (aggrComm, &sizeAggrComm);

    endTime = MPI_Wtime();
    totTime += ( endTime - startTime );
    
    if ( color > 0 ) {
      switch ( this->strategy_ )
	{
	case SHORTEST_PATH:
	  aggrRank = this->RankShortestPath (aggrComm, color);
	  break;
	case LONGEST_PATH:
	  aggrRank = this->RankLongestPath (aggrComm, color);
	  break;
	case TOPOLOGY_AWARE:
	  aggrRank = this->RankTopologyAware (aggrComm, color);
	  break;
	case CONTENTION_AWARE:
	  aggrRank = this->RankContentionAware (aggrComm, color);
	  break;
	case UNIFORM:
	  aggrRank = this->RankUniformDistribution (aggrComm, color);
	  break;
	}

      if ( this->commRank_ == aggrRank ) {
	this->totalWrites_     = ceil ( (double)this->aggrDataSize_ / (double)this->bufferSize_);
	this->globalAggrRank_  = aggr;
	aggrCoords             = this->intCoords_;
	MPI_Isend ( &aggrCoords, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &request );
      }

      for ( i = 0; i < this->aggregatorsRanks.size(); i++ )
	if ( this->globalAggregatorsRanks[i] == aggr )
	  this->aggregatorsRanks[i] = aggrRank;

#ifdef DEBUG
      int coords[topology.NetworkDimensions() + 1];
      if ( this->commRank_ == aggrRank ) {
	topology.RankToCoordinates (this->worldRank_, coords);
	fprintf (stdout, "[DEBUG] AggRank %d, (", this->worldRank_);
	for ( i = 0; i < topology.NetworkDimensions() + 1; i++ )
	  fprintf (stdout, "%u ", coords[i]);
	fprintf (stdout, ") -> %d, part %d, %lld B from %d ranks, %d rounds\n",
		 this->intCoords_, topology.BridgeNodeId(), this->aggrDataSize_, sizeAggrComm, this->totalWrites_);
      }
#endif
    }

    /*
     *  TODO: Compute this... 
     *              - Vesta: 16 nodes/bridge node
     *              - Mira : 64 nodes/bridge node
     */
    for ( i = 0; i < ( (worldSize / topology.ProcessPerNode ()) / (this->commSize_ / topology.ProcessPerNode ()) ); i++ ) {
      if ( this->worldRank_ == 0 )
	MPI_Recv ( &aggrCoords, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status );
      
      MPI_Bcast ( &aggrCoords, 1, MPI_INT, 0, MPI_COMM_WORLD);
      this->excludedNode[aggrCoords] = true;
    }
  }

Francois Tessier's avatar
Francois Tessier committed
720
#ifdef TIMING
Francois Tessier's avatar
Francois Tessier committed
721
  this->PrintTime( 0, totTime, " |-> Create subcommunicator");
Francois Tessier's avatar
Francois Tessier committed
722
#endif
Francois Tessier's avatar
Francois Tessier committed
723 724 725
}


Francois Tessier's avatar
Francois Tessier committed
726
int64_t Tapioca::DataSizeSentToAggr (int aggrId)
Francois Tessier's avatar
Francois Tessier committed
727 728 729 730 731 732 733 734 735 736 737 738
{
  int i;
  int64_t dataSize = 0;

  for ( i = 0; i < this->aggregatorsRanks.size(); i++ )
    if ( this->globalAggregatorsRanks[i] == aggrId )
      dataSize += this->dataSize[i];

  return dataSize;
}


Francois Tessier's avatar
Francois Tessier committed
739
void Tapioca::InitAggregators () 
Francois Tessier's avatar
Francois Tessier committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
{
  int aggr, retval;
  
  if ( this->amAnAggr_ ) {
    this->buffer1 = malloc (this->bufferSize_);
    this->buffer2 = malloc (this->bufferSize_);

    retval = MPI_Win_create (this->buffer1, this->bufferSize_, 1, MPI_INFO_NULL, this->subComm_, &this->RMAWin1);
    this->HandleMPIError (retval);
    MPI_Win_create (this->buffer2, this->bufferSize_, 1, MPI_INFO_NULL, this->subComm_, &this->RMAWin2);
    this->HandleMPIError (retval);
  }
  else {
    retval = MPI_Win_create (NULL, 0, 1, MPI_INFO_NULL, this->subComm_, &this->RMAWin1);
    this->HandleMPIError (retval);
    retval = MPI_Win_create (NULL, 0, 1, MPI_INFO_NULL, this->subComm_, &this->RMAWin2);
    this->HandleMPIError (retval);
  }

  retval = MPI_Win_fence (0, this->RMAWin1);
  this->HandleMPIError (retval);
  retval = MPI_Win_fence (0, this->RMAWin2);
  this->HandleMPIError (retval);

#ifdef DEBUG
  if (this->commRank_ == MASTER) {
    fprintf (stdout, "[DEBUG] %d RMA windows created (%d aggr., %d buffers)\n", 
	     NBUFFERS, this->nAggr_, NBUFFERS);
  }
#endif
}


/***********************/
/*     PLACEMENT       */
/***********************/
Francois Tessier's avatar
Francois Tessier committed
776
int Tapioca::RankShortestPath (MPI_Comm aggrComm, int64_t dataSize)
Francois Tessier's avatar
Francois Tessier committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
{
  int commRank, aggrRank, aggrPrank, ppn, nodeId;
  struct { int hops; int rank; } hopsToIONnode, shortestPath;

  MPI_Comm_rank (aggrComm, &commRank);
  
  hopsToIONnode.hops = topology.DistanceToIONode ( this->worldRank_ );
  hopsToIONnode.rank = commRank;

  if ( this->excludedNode[this->intCoords_] )
    hopsToIONnode.hops = INT_MAX;

  MPI_Allreduce ( &hopsToIONnode, &shortestPath, 1, MPI_2INTEGER, MPI_MINLOC, aggrComm );
  MPI_Reduce ( &dataSize, &this->aggrDataSize_, 1, MPI_LONG_LONG, MPI_SUM, shortestPath.rank, aggrComm );

  if ( shortestPath.rank == commRank ) {
    aggrRank = this->commRank_;
    this->amAnAggr_ = true;
  }

#ifdef DEBUG
  if ( shortestPath.rank == commRank )
    fprintf (stdout, "[DEBUG] Aggr. rank %d in aggrComm, distance to I/O node %d hops\n",
	     shortestPath.rank, shortestPath.hops);
#endif
  
  MPI_Bcast ( &aggrRank, 1, MPI_INT, shortestPath.rank, aggrComm);

  return aggrRank;
}


Francois Tessier's avatar
Francois Tessier committed
809
int Tapioca::RankLongestPath (MPI_Comm aggrComm, int64_t dataSize)
Francois Tessier's avatar
Francois Tessier committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
{
  int commRank, aggrRank;
  struct { int hops; int rank; } hopsToIONnode, longestPath;

  MPI_Comm_rank (aggrComm, &commRank);
  
  hopsToIONnode.hops = topology.DistanceToIONode ( this->worldRank_ );
  hopsToIONnode.rank = commRank;

  if ( this->excludedNode[this->intCoords_] )
    hopsToIONnode.hops = INT_MIN;
  
  MPI_Allreduce ( &hopsToIONnode, &longestPath, 1, MPI_2INTEGER, MPI_MAXLOC, aggrComm );
  MPI_Reduce ( &dataSize, &this->aggrDataSize_, 1, MPI_LONG_LONG, MPI_SUM, longestPath.rank, aggrComm );

  if ( longestPath.rank == commRank ) {
    aggrRank  = this->commRank_;
    this->amAnAggr_ = true;
  }

#ifdef DEBUG
  if ( longestPath.rank == commRank )
    fprintf (stdout, "[DEBUG] Aggr. rank %d in aggrComm, distance to I/O node %d hops\n",
	     longestPath.rank, longestPath.hops);
#endif
  
  MPI_Bcast ( &aggrRank, 1, MPI_INT, longestPath.rank, aggrComm);

  return aggrRank;
}


Francois Tessier's avatar
Francois Tessier committed
842
int Tapioca::RankTopologyAware (MPI_Comm aggrComm, int64_t dataSize)
Francois Tessier's avatar
Francois Tessier committed
843 844
{
  struct { double cost; int rank; } aggrCost, minCost;
845
  int aggrCommRank, aggrCommSize, worldRank, rank, distance, dim, hops, aggrRank, nIOnodes;
Francois Tessier's avatar
Francois Tessier committed
846
  int64_t *dataDistrib, aggregatedData = 0;
847
  int *srcCoords, *destCoords, *globalRanks, *IOnodesList;
Francois Tessier's avatar
Francois Tessier committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  MPI_Comm_rank (aggrComm, &aggrCommRank);
  MPI_Comm_size (aggrComm, &aggrCommSize);
  MPI_Comm_rank (MPI_COMM_WORLD, &worldRank);

  aggrCost.rank = aggrCommRank;
  aggrCost.cost = 0;

  dataDistrib = (int64_t*) malloc (aggrCommSize * sizeof(int64_t));
  globalRanks = (int *) malloc (aggrCommSize * sizeof(int));
  MPI_Allgather(&worldRank, 1, MPI_INT, globalRanks, 1, MPI_INT, aggrComm);
  MPI_Allgather(&dataSize, 1, MPI_LONG_LONG, dataDistrib, 1, MPI_LONG_LONG, aggrComm);

  for ( rank = 0; rank < aggrCommSize; rank++ ) {
    aggregatedData += dataDistrib[rank];
    if ( rank != aggrCommRank ) {
      distance = topology.DistanceBetweenRanks ( globalRanks[rank], worldRank );

865 866 867
      // aggrCost.cost = std::max ( distance * LATENCY + (double)dataDistrib[rank] / BANDWIDTH,
      // 			 aggrCost.cost );
      aggrCost.cost += (distance * LATENCY + (double)dataDistrib[rank] / BANDWIDTH);
Francois Tessier's avatar
Francois Tessier committed
868 869
    }
  }
870 871 872 873 874 875 876 877 878 879 880 881 882

  IOnodesList = (int *) malloc ( MAX_IONODES * sizeof ( int ) );
  nIOnodes = topology.IONodesPerFile (this->filename_, IOnodesList);

  if ( this->commRank_ == 0 ) {
    fprintf (stdout, "[LUSTRE] nLnet = %d\n", nIOnodes);
    fprintf (stdout, "[LUSTRE] list = ");
    for ( int i = 0; i < nIOnodes; i++ )
      fprintf (stdout, "%d ", IOnodesList[i]);
    fprintf (stdout, "\n");
  }
    
  //aggrCost.cost += topology.DistanceToIONode ( worldRank ) * LATENCY + (double)aggregatedData / BANDWIDTH;
Francois Tessier's avatar
Francois Tessier committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

  if ( this->excludedNode[this->intCoords_] )
    aggrCost.cost = DBL_MAX;

  MPI_Allreduce ( &aggrCost, &minCost, 1, MPI_DOUBLE_INT, MPI_MINLOC, aggrComm );

  MPI_Reduce ( &dataSize, &this->aggrDataSize_, 1, MPI_LONG_LONG, MPI_SUM, minCost.rank, aggrComm );

  if ( minCost.rank == aggrCommRank ) {
    aggrRank = this->commRank_;
    this->amAnAggr_ = true;
  }

  MPI_Bcast ( &aggrRank, 1, MPI_INT, minCost.rank, aggrComm );

#ifdef DEBUG
  if ( minCost.rank == aggrCommRank )
    fprintf (stdout, "[DEBUG] Aggr. rank %d in aggrComm, distance to I/O node %d hops, cost: %.4f\n",
	     minCost.rank, topology.DistanceToIONode ( worldRank ), minCost.cost );
#endif

  return aggrRank;
}


Francois Tessier's avatar
Francois Tessier committed
908
int Tapioca::RankUniformDistribution (MPI_Comm aggrComm, int64_t dataSize)
Francois Tessier's avatar
Francois Tessier committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
{
  int aggrRank, aggrCommRank, rootRank = 0, rootCoords;
  
  MPI_Comm_rank (aggrComm, &aggrCommRank);

  if ( aggrCommRank == rootRank )
    rootCoords = this->intCoords_;
  
  MPI_Bcast ( &rootCoords, 1, MPI_INT, rootRank, aggrComm);
  
  while ( this->excludedNode[rootCoords] ) {
    rootRank += topology.ProcessPerNode ();
    if ( aggrCommRank == rootRank )
      rootCoords = this->intCoords_;

    MPI_Bcast ( &rootCoords, 1, MPI_INT, rootRank, aggrComm);
  }

  MPI_Reduce ( &dataSize, &this->aggrDataSize_, 1, MPI_LONG_LONG, MPI_SUM, rootRank, aggrComm );

  if ( aggrCommRank == rootRank ) {
    aggrRank = this->commRank_;
    this->amAnAggr_ = true;
  }    
  
  MPI_Bcast ( &aggrRank, 1, MPI_INT, rootRank, aggrComm);
  
  return aggrRank;
}


Francois Tessier's avatar
Francois Tessier committed
940
int Tapioca::RankContentionAware (MPI_Comm aggrComm, int64_t dataSize)
Francois Tessier's avatar
Francois Tessier committed
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
{
  struct { double cost; int rank; } aggrCost, minCost;
  int aggrCommRank, aggrCommSize, worldRank, rank, distance, interRanks;
  int aggrRank, dim, hops, ppn, intaggrRank, node, i;
  int64_t *dataDistrib, aggregatedData = 0;
  int *srcCoords, *destCoords, *globalRanks;
  
  char *split;
  int srcNode, destNode, interNode, srcLink, destLink;
  std::map< int, std::map<int, int> > links;
  std::map< int, std::vector<int> > routes;
  std::map< int, int > routeCost;
  int *path;

  MPI_Comm_rank (aggrComm, &aggrCommRank);
  MPI_Comm_size (aggrComm, &aggrCommSize);
  MPI_Comm_rank (MPI_COMM_WORLD, &worldRank);

  aggrCost.rank = aggrCommRank;
  aggrCost.cost = 0;

  ppn = topology.ProcessPerNode ();

  dataDistrib = (int64_t*) malloc (aggrCommSize * sizeof(int64_t));
  globalRanks = (int *) malloc (aggrCommSize * sizeof(int));
  MPI_Allgather(&worldRank, 1, MPI_INT, globalRanks, 1, MPI_INT, aggrComm);
  MPI_Allgather(&dataSize, 1, MPI_LONG_LONG, dataDistrib, 1, MPI_LONG_LONG, aggrComm);

  destNode = globalRanks[aggrCommRank]/ppn;
  
  /*
   *  Contention:
   *     - routes: map < srcNode, {<intermediate nodes>}
   *     - links: map < node, < node, weight > >
   */
  for ( rank = 0; rank < aggrCommSize; rank++ ) {
    srcNode = globalRanks[rank]/ppn;
    srcLink = srcNode;
    path = (int *)calloc (50, sizeof(int));
    
    if ( srcNode != destNode && routes.find(srcNode) == routes.end() ) {
      interRanks = topology.RouteBetweenRanks (globalRanks[rank], globalRanks[aggrCommRank], path);
      routeCost[srcNode] = 0;
      
      for ( i = 1; i < interRanks; i++ ) {
	interNode = path[i] / ppn;
	routes[srcNode].push_back (interNode);
	links[srcLink][interNode] = 0;
	srcLink = interNode;
      }
    }
    free (path);
  }

  /* I/O Node */
  path = (int *)calloc (50, sizeof(int));
  interRanks = topology.RouteToIONode (this->worldRank_, path );
  srcNode = this->worldRank_ / ppn;
  srcLink = srcNode;

  for ( i = 1; i < interRanks; i++ ) {
    interNode = path[i] / ppn;
    routes[srcNode].push_back (interNode);
    links[srcLink][interNode] = 0;
    srcLink = interNode;
  }
  free (path);

 
  for ( rank = 0; rank < aggrCommSize; rank++ ) {
    srcNode = globalRanks[rank]/ppn;
    srcLink = srcNode;
    
    if ( srcNode != destNode ) {
      for ( node = 0; node < routes[srcNode].size (); node++ ) {
	links[srcLink][routes[srcNode][node]]++;
	routeCost[srcNode] = std::max(routeCost[srcNode], links[srcLink][routes[srcNode][node]]);
	srcLink = routes[srcNode][node];
      }
    }
    else
      routeCost[srcNode] = 1;
  }

  
  for ( rank = 0; rank < aggrCommSize; rank++ ) {
    aggregatedData += dataDistrib[rank];
    srcNode = globalRanks[rank]/ppn;
    
    if ( rank != aggrCommRank ) {
      
      aggrCost.cost = std::max ( (double)dataDistrib[rank] / ( BANDWIDTH / routeCost[srcNode] ),
				 aggrCost.cost );
    }
  }

  /* I/O Node */
  srcNode = this->worldRank_ / ppn;
  aggrCost.cost += aggregatedData / ( BANDWIDTH / routeCost[srcNode] );
 
  
  if ( this->excludedNode[this->intCoords_] )
    aggrCost.cost = DBL_MAX;
  
  MPI_Allreduce ( &aggrCost, &minCost, 1, MPI_DOUBLE_INT, MPI_MINLOC, aggrComm );

  MPI_Reduce ( &dataSize, &this->aggrDataSize_, 1, MPI_LONG_LONG, MPI_SUM, minCost.rank, aggrComm );

  if ( minCost.rank == aggrCommRank ) {
    aggrRank = this->commRank_;
    this->amAnAggr_ = true;
  }

  MPI_Bcast ( &aggrRank, 1, MPI_INT, minCost.rank, aggrComm );

#ifdef DEBUG
  if ( minCost.rank == aggrCommRank )
    fprintf (stdout, "[DEBUG] Aggr. rank %d in aggrComm, distance to I/O node %d hops, cost: %.4f\n",
	     minCost.rank, topology.DistanceToIONode ( this->worldRank_ ), minCost.cost );
#endif

  return aggrRank;
}


Francois Tessier's avatar
Francois Tessier committed
1066
int Tapioca::CoordsToInt (int *coords, int dim)
Francois Tessier's avatar
Francois Tessier committed
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
{
  int i, res = 0;

  for ( i = 0; i < dim; i++ )
    res += coords[i] * pow (10.0, (double)i);

  return res;
}

/***********************/
/*        MISC.        */
/***********************/
Francois Tessier's avatar
Francois Tessier committed
1079
const char* Tapioca::getStrategyName ()
Francois Tessier's avatar
Francois Tessier committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
{
  switch (this->strategy_)
    {
    case SHORTEST_PATH:    return "Shortest path";
    case LONGEST_PATH:     return "Longest path";
    case TOPOLOGY_AWARE:   return "Topology-aware placement";
    case CONTENTION_AWARE: return "Contention-aware placement";
    case UNIFORM:          return "Uniform placement";
    default:               return "No placement strategy defined!";
    }
}


Francois Tessier's avatar
Francois Tessier committed
1093
void Tapioca::HandleMPIError (int retval) 
Francois Tessier's avatar
Francois Tessier committed
1094 1095 1096 1097 1098 1099
{
#ifdef DEBUG
  char msg[MPI_MAX_ERROR_STRING];
  int resultlen;
  if (retval != MPI_SUCCESS) {
    MPI_Error_string(retval, msg, &resultlen);
1100
    fprintf(stderr, "[ERR] %s\n", msg);
Francois Tessier's avatar
Francois Tessier committed
1101 1102 1103 1104 1105 1106 1107
  }
#endif

  return;
}


Francois Tessier's avatar
Francois Tessier committed
1108
void Tapioca::PrintTime ( double startTime, double endTime, char* func )
Francois Tessier's avatar
Francois Tessier committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
{
  double totTime, avgTime, minTime, maxTime;
  int commSize, commRank;
  
  MPI_Comm_rank(MPI_COMM_WORLD, &commRank);
  MPI_Comm_size(MPI_COMM_WORLD, &commSize);
  
  totTime = endTime - startTime;
  totTime = totTime * 1000;
  MPI_Reduce(&totTime, &avgTime, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  MPI_Reduce(&totTime, &maxTime, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  MPI_Reduce(&totTime, &minTime, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
  if (commRank == 0) {
    avgTime = avgTime / commSize;
    fprintf (stdout, "[TIMING][AGG] %s: %.2f ms [%.2f ; %.2f]\n",
             func, avgTime, minTime, maxTime);
  }
}


Francois Tessier's avatar
Francois Tessier committed
1129
void Tapioca::MPIIOInfo ( MPI_File fileHandle )
Francois Tessier's avatar
Francois Tessier committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
{
  MPI_Info info;
  int flag;
  char value[1024];

  if ( this->worldRank_ == 0 ) {
    MPI_File_get_info ( fileHandle, &info );
    
    fprintf ( stdout, "[INFO] MPI Two-phases I/O\n");
    
    MPI_Info_get ( info, "cb_buffer_size", 1024, value, &flag );
    fprintf ( stdout, "[INFO] cb_buffer_size = %s\n", value );
    
    MPI_Info_get ( info, "cb_nodes", 1024, value, &flag );
    fprintf ( stdout, "[INFO] cb_nodes = %s\n", value );

    MPI_Info_get ( info, "romio_cb_read", 1024, value, &flag );
    fprintf ( stdout, "[INFO] romio_cb_read = %s\n", value );
    
    MPI_Info_get ( info, "romio_cb_write", 1024, value, &flag );
    fprintf ( stdout, "[INFO] romio_cb_write = %s\n", value );
    
    MPI_Info_get ( info, "romio_no_indep_rw", 1024, value, &flag );
    fprintf ( stdout, "[INFO] romio_no_indep_rw = %s\n", value );
  }
}