tapioca.cpp 18.8 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
  this->reElectAggr_ = true;
  this->electedAggr_ = false;
7
  this->amAnAggr_    = false;
Francois Tessier's avatar
Francois Tessier committed
8 9 10
}


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

15 16 17 18 19 20 21 22 23
void Tapioca::Init (int64_t *chunkCount, int *chunkSize, int64_t *chunkOffset,
		    int nChunks, int64_t header, MPI_Comm comm)
{
  int chunk;
#ifdef TIMING
  double startInitTime, endInitTime, startElectTime, endElectTime;
  startInitTime = MPI_Wtime();
#endif
  
24 25 26
  this->SetDefaultValues ();
  this->ParseEnvVariables ();

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
  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_ = header;
  MPI_Comm_dup (comm, &this->subComm_);
  
  this->SetCommValues ();
  this->SetOffsets ();

#ifdef DBG
  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

63
  if ( this->reElectAggr_ ) {
64
    this->ElectAggregators ();
65
  }
66 67 68 69 70 71 72 73
  else {
    if ( !this->electedAggr_ ) {
      this->ElectAggregators ();
      this->electedAggr_ =  true;
    }
    else
      this->KeepAggregators ();
  }
74 75 76 77 78 79 80 81 82 83 84 85
  
#ifdef TIMING
  endElectTime = MPI_Wtime();
#endif
  
#ifdef TIMING
  endInitTime = MPI_Wtime();
  this->PrintTime(startInitTime, endInitTime, "Initialize");
  this->PrintTime(startElectTime, endElectTime, " |-> Elect aggregators");
#endif
}

86
void Tapioca::setAggregationTier ( int nBuffers, mem_t mem, char* fileName )
87
{
88
  int i;
89 90 91 92 93

  // if ( mem == NVR ) {
  //   this->nBuffers
  // }
  // else
94
  this->nBuffers_ = nBuffers;
95
  
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  for ( i = 0; i < this->nBuffers_; i++ ) {
    Memory memBuffer;
    
    if ( mem == NVR ) {
      char file[100], extension[100];
      strcpy ( file, fileName );
      snprintf ( extension, 100, "%d.agg", i );
      strcat ( file, extension );
      
      memBuffer.memAlloc ( this->bufferSize_, mem, this->amAnAggr_, file, this->subComm_ );
      this->memBuffers.push_back ( memBuffer );
    }
    else {
      memBuffer.memAlloc ( this->bufferSize_, mem, this->amAnAggr_, fileName, this->subComm_ );
      this->memBuffers.push_back ( memBuffer );
    }
112
  }
113 114 115 116 117 118 119 120
}


void Tapioca::setTargetTier ( mem_t mem, int64_t buffSize, char* fileName )
{
  this->memTarget.memAlloc ( buffSize, mem, this->amAnAggr_, fileName, this->subComm_ );
}

Francois Tessier's avatar
Francois Tessier committed
121

122 123
void Tapioca::Finalize ()
{
124 125
  int i;

126 127 128 129 130
  this->chunksIndexMatching.clear();
  free (this->chunkCount_);
  free (this->chunkSize_);
  free (this->chunkOffset_);
  this->excludedNode.clear();
Francois Tessier's avatar
Francois Tessier committed
131

132 133
  this->commDataSize_ = 0;

134 135 136 137
  for ( i = 0; i < this->nBuffers_; i++ ) {
    this->memBuffers[i].memFree ();
  }
  this->memBuffers.clear ();
138

139
  this->memTarget.memFlush ();
140
  this->memTarget.memFree ();
141

142
  MPI_Comm_free (&this->subComm_);
Francois Tessier's avatar
Francois Tessier committed
143 144 145
}


146
void Tapioca::GlobalFence () 
Francois Tessier's avatar
Francois Tessier committed
147 148 149
{
  int buffer;

150
  buffer = this->currentRound_ % this->nBuffers_;
Francois Tessier's avatar
Francois Tessier committed
151

152
  this->memBuffers[buffer].memFlush ();
Francois Tessier's avatar
Francois Tessier committed
153 154 155 156 157 158
  
#ifdef TIMING
  this->endAggrTime = MPI_Wtime();
  
  if ( this->startAggrTime != 0 ) {
    this->totAggrTime = this->endAggrTime - this->startAggrTime;
159 160
    fprintf (stdout, "[TIMING][AGG][AGGR] Rank %d, Rnd %d - %.2f ms\n",
     	     this->commRank_, this->currentRound_, this->totAggrTime * 1000);
Francois Tessier's avatar
Francois Tessier committed
161
  }
162

Francois Tessier's avatar
Francois Tessier committed
163 164 165 166 167 168 169 170 171
  this->startAggrTime = 0;
#endif

}


/***********************/
/*  INITIALIZATION     */
/***********************/
Francois Tessier's avatar
Francois Tessier committed
172
void Tapioca::SetDefaultValues ()
Francois Tessier's avatar
Francois Tessier committed
173 174 175 176
{
  this->rankDataSize_ = 0;
  this->strategy_ = SHORTEST_PATH;
  this->nAggr_ = 8;
177
  this->bufferSize_ = 16777216;
178
  this->nBuffers_ = 2;
179
  this->commSplit_ = true;
Francois Tessier's avatar
Francois Tessier committed
180
  this->currentRound_ = 0;
181
  this->totalRounds_ = 0;
182 183
  this->readRound_ = 0;
  this->firstRead_ = false;
Francois Tessier's avatar
Francois Tessier committed
184 185 186
  this->currentDataSize_ = 0;
  this->nCommit_ = 0;
  this->writeDevNull_ = false;
187
  this->pipelinedBuffers_ = true;
Francois Tessier's avatar
Francois Tessier committed
188 189 190
}


Francois Tessier's avatar
Francois Tessier committed
191
void Tapioca::ParseEnvVariables ()
Francois Tessier's avatar
Francois Tessier committed
192
{
193 194 195 196 197 198 199
  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");
  char *envPipelining  = getenv("TAPIOCA_PIPELINING");
  char *envReElectAggr = getenv("TAPIOCA_REELECTAGGR");
Francois Tessier's avatar
Francois Tessier committed
200 201 202 203 204 205 206
  
  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;
207
    strcmp(envStrategy, "RANDOM")           ? 0 : this->strategy_ = RANDOM;
Francois Tessier's avatar
Francois Tessier committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }

  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;
  }
227 228 229 230 231

  if (envPipelining != NULL) {
    strcmp(envPipelining, "true")  ? 0 : this->pipelinedBuffers_ = true;
    strcmp(envPipelining, "false") ? 0 : this->pipelinedBuffers_ = false;
  }
232 233 234 235 236

  if (envReElectAggr != NULL) {
    strcmp(envReElectAggr, "true")  ? 0 : this->reElectAggr_ = true;
    strcmp(envReElectAggr, "false") ? 0 : this->reElectAggr_ = false;
  }
Francois Tessier's avatar
Francois Tessier committed
237 238 239
}


Francois Tessier's avatar
Francois Tessier committed
240
void Tapioca::SetCommValues ()
Francois Tessier's avatar
Francois Tessier committed
241 242 243 244 245 246 247 248 249
{
  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
250
void Tapioca::SetOffsets ()
Francois Tessier's avatar
Francois Tessier committed
251 252 253 254 255 256 257 258
{
  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
259
void Tapioca::SetNodesList ()
Francois Tessier's avatar
Francois Tessier committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
  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;
276

277 278
  this->nNodes_ = this->excludedNode.size();

279 280
  free (coords);
  free (myCoords);
Francois Tessier's avatar
Francois Tessier committed
281 282 283 284 285 286
}


/***********************/
/*    AGGREGATION      */
/***********************/
Francois Tessier's avatar
Francois Tessier committed
287
int Tapioca::NumberOfAggregators ()
Francois Tessier's avatar
Francois Tessier committed
288 289 290 291 292
{
  return 0;
}


Francois Tessier's avatar
Francois Tessier committed
293
void Tapioca::IdentifyMyAggregators ()
Francois Tessier's avatar
Francois Tessier committed
294
{
295
  int i, j, c, globalRoundId, upperBound, index = 0, nAggr;
Francois Tessier's avatar
Francois Tessier committed
296 297 298
  int64_t remainingData, offsetInAggrData;
  std::vector<Round_t> rounds;
  
299
  this->totalNeededBuffers_  = ceil ( (double)this->commDataSize_ / (double)this->bufferSize_ );
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

  /*
   * If we have more aggregators (and buffers) than needed, decrease the number of aggregators
   */
  nAggr = this->nAggr_;
  
  if ( nAggr > this->nNodes_ )
    nAggr = this->nNodes_;
  
  if ( nAggr > this->totalNeededBuffers_ )
    nAggr = this->totalNeededBuffers_;
  
  if ( nAggr != this->nAggr_ ) {
    if ( this->commRank_ == 0 )
      fprintf ( stdout, "[INFO] Number of aggregators decreased from %d to %d\n",
		this->nAggr_, nAggr);
    
    this->nAggr_ = nAggr;      
  }

  this->totalRounds_ = ceil ( (double)this->totalNeededBuffers_ / (double)this->nAggr_ );
Francois Tessier's avatar
Francois Tessier committed
321

322
  for ( i = 0; i < this->totalNeededBuffers_; i++ ) {
Francois Tessier's avatar
Francois Tessier committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    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++;
    }
  }
  
355
#ifdef DBG
Francois Tessier's avatar
Francois Tessier committed
356
  if (this->commRank_ == 4) {
357
    printMsg ( DEBUG, "Rounds distrib. on %d aggregators: AGG ", this->nAggr_);
358
    for ( i = 0; i < this->totalNeededBuffers_; i++ )
Francois Tessier's avatar
Francois Tessier committed
359 360
      fprintf (stdout, "%d ", rounds[i].aggr);
    fprintf (stdout, "\n                                          RND ");
361
    for ( i = 0; i < this->totalNeededBuffers_; i++ )
Francois Tessier's avatar
Francois Tessier committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
      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
}


385 386
void Tapioca::KeepAggregators ()
{
387
  int aggr, aggrRank, aggrSize;
388
  int64_t dataSize;
389
  int *aggrSubCommRanks;
390
  MPI_Comm aggrComm;
391
  MPI_Status status;
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  aggrSubCommRanks = ( int * ) malloc ( this->nAggr_ * sizeof ( int ) );
  
  MPI_Comm_split (this->subComm_, this->amAnAggr_, this->commRank_, &aggrComm);
  MPI_Comm_rank ( aggrComm, &aggrRank );
  MPI_Comm_size ( aggrComm, &aggrSize );

  if ( this->amAnAggr_ ) {
    MPI_Gather ( &this->commRank_, 1, MPI_INT, aggrSubCommRanks, 1, MPI_INT, 0, aggrComm );
    if ( aggrRank == 0 ) {
      fprintf ( stdout, "[DEBUG] Keep the previously selected aggregators : " );
      for ( aggr = 0; aggr < this->nAggr_; aggr++ )
	fprintf ( stdout, "%d ", aggrSubCommRanks[aggr] );
      fprintf ( stdout, "\n" );
    }
  }
408

409 410 411 412 413 414 415 416 417 418
  MPI_Barrier ( this->subComm_ );

  if ( this->amAnAggr_ && aggrRank == 0 && this->commRank_ != 0 )
    MPI_Send ( aggrSubCommRanks, this->nAggr_, MPI_INT, 0, 0, this->subComm_ );
  
  if ( this->commRank_ == 0 )
    MPI_Recv ( aggrSubCommRanks, this->nAggr_, MPI_INT, MPI_ANY_SOURCE, 0, this->subComm_, &status);
  
  MPI_Bcast ( aggrSubCommRanks, this->nAggr_, MPI_INT, 0, this->subComm_ );
  
419 420 421
  for ( aggr = 0; aggr < this->nAggr_; aggr++ ) {
    dataSize = this->DataSizeSentToAggr (aggr);
    
422 423 424
    MPI_Reduce ( &dataSize, &this->aggrDataSize_, 1, MPI_LONG_LONG, MPI_SUM, aggrSubCommRanks[aggr], this->subComm_ );
    
    if ( this->commRank_ == aggrSubCommRanks[aggr] )
425 426 427 428
      this->totalWrites_ = ceil ( (double)this->aggrDataSize_ / (double)this->bufferSize_);
  }
}

Francois Tessier's avatar
Francois Tessier committed
429
void Tapioca::ElectAggregators ()
Francois Tessier's avatar
Francois Tessier committed
430 431 432 433 434 435 436 437 438 439 440
{
  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;

441
  this->amAnAggr_ = false;
Francois Tessier's avatar
Francois Tessier committed
442 443 444 445 446 447 448 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
  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;
510 511 512
	case RANDOM:
	  aggrRank = this->RankRandom (aggrComm, color);
	  break;
Francois Tessier's avatar
Francois Tessier committed
513 514 515 516 517 518 519 520 521 522 523 524 525
	}

      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;

526
#ifdef DBG
Francois Tessier's avatar
Francois Tessier committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
      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
553
#ifdef TIMING
Francois Tessier's avatar
Francois Tessier committed
554
  this->PrintTime( 0, totTime, " |-> Create subcommunicator");
Francois Tessier's avatar
Francois Tessier committed
555
#endif
Francois Tessier's avatar
Francois Tessier committed
556 557 558
}


Francois Tessier's avatar
Francois Tessier committed
559
int64_t Tapioca::DataSizeSentToAggr (int aggrId)
Francois Tessier's avatar
Francois Tessier committed
560 561 562 563 564 565 566 567 568 569 570 571
{
  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
572
int Tapioca::CoordsToInt (int *coords, int dim)
Francois Tessier's avatar
Francois Tessier committed
573 574 575 576 577 578 579 580 581 582 583 584
{
  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
585
const char* Tapioca::getStrategyName ()
Francois Tessier's avatar
Francois Tessier committed
586 587 588 589 590 591 592 593
{
  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";
594
    case RANDOM :          return "Random placement";
Francois Tessier's avatar
Francois Tessier committed
595 596 597 598 599
    default:               return "No placement strategy defined!";
    }
}


Francois Tessier's avatar
Francois Tessier committed
600
void Tapioca::HandleMPIError (int retval) 
Francois Tessier's avatar
Francois Tessier committed
601
{
602
#ifdef DBG
Francois Tessier's avatar
Francois Tessier committed
603 604 605 606
  char msg[MPI_MAX_ERROR_STRING];
  int resultlen;
  if (retval != MPI_SUCCESS) {
    MPI_Error_string(retval, msg, &resultlen);
607
    fprintf(stderr, "[ERR] %s\n", msg);
Francois Tessier's avatar
Francois Tessier committed
608 609 610 611 612 613 614
  }
#endif

  return;
}


Francois Tessier's avatar
Francois Tessier committed
615
void Tapioca::PrintTime ( double startTime, double endTime, char* func )
Francois Tessier's avatar
Francois Tessier committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
  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
636
void Tapioca::MPIIOInfo ( MPI_File fileHandle )
Francois Tessier's avatar
Francois Tessier committed
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
{
  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 );
  }
}