tp_network.cpp 7.67 KB
Newer Older
1
#include "tp_network.hpp"
2
#include <math.h>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 83 84 85 86 87 88 89 90 91 92 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

#define MAX_IONODES 1

/**********************/
/*  |-- Nodes         */
/**********************/
int Topology::IONodeId () {
  return MPIX_IO_node_id ();
}


int Topology::BridgeNodeId () {
  return MPIX_IO_link_id ();
}


int Topology::ComputeNodeId () {
  return 0;
}


int Topology::ProcessPerNode () {
  MPIX_Hardware_t hw;
  MPIX_Hardware(&hw);
  
  return hw.ppn;
}


int Topology::IONodesPerFile ( char* filename, int *nodesList ) {
  return 1;
}


/**********************/
/*  |-- Network       */
/**********************/
int Topology::NetworkDimensions () {
  MPIX_Hardware_t hw;
  MPIX_Hardware(&hw);
  
  return hw.torus_dimension;
}


/*  |---- Coordinates */
void Topology::RankToCoordinates ( int rank, int* coord ) {
  MPIX_Rank2torus( rank, coord );
}


void Topology::IONodeCoordinates ( int* coord ) {
  Personality_t personality;
  Kernel_GetPersonality(&personality, sizeof(personality));
  
  coord[0] = personality.Network_Config.cnBridge_A;
  coord[1] = personality.Network_Config.cnBridge_B;
  coord[2] = personality.Network_Config.cnBridge_C;
  coord[3] = personality.Network_Config.cnBridge_D;
  coord[4] = personality.Network_Config.cnBridge_E;
}


/*  |---- Distance    */
int Topology::DistanceToIONode ( int srcRank ) {
  return MPIX_IO_distance ();
}

 
int Topology::DistanceBetweenRanks ( int srcRank, int destRank ) {
  int srcCoords[6], destCoords[6];
  int dim, d, hops, distance;
  MPIX_Hardware_t hw;
  
  RankToCoordinates ( srcRank, srcCoords );
  RankToCoordinates ( destRank, destCoords );
  
  dim = NetworkDimensions ();
  distance = 0;
  MPIX_Hardware( &hw );
  
  for ( d = 0; d < dim; d++ ) {
    hops = abs ( destCoords[d] - srcCoords[d] );
    if ( hw.isTorus[d] == 1 )
      hops = std::min ( hops, (int)hw.Size[d] - hops );
    distance += hops;
  }
  
  return distance;
}


/*  |---- Routes      */
int Topology::RouteToIONode ( int srcRank, int* path ) {
  int srcCoords[6], IOCoords[6];
  
  RankToCoordinates ( srcRank, srcCoords );
  IONodeCoordinates ( IOCoords );
  
  return RouteBetweenCoords ( srcCoords, IOCoords, path );
}


int Topology::RouteBetweenRanks ( int srcRank, int destRank, int* path ) {
  int srcCoords[6], destCoords[6];
  RankToCoordinates (srcRank, srcCoords);
  RankToCoordinates (destRank, destCoords);
  
  return RouteBetweenCoords ( srcCoords, destCoords, path );
}


void Topology::LinksList ( int* list ) {
  return;
}


120
void Topology::PrintNodesAdjencyList ( ) {
121
  int *worldRanks, *worldDistIO;
122
  MPI_Comm nodeComm, masterComm;
123 124 125 126
  int size, rank, nodeRank, nodeSize, masterRank, masterSize, i, color = 0;
  int src, dest, distanceIO, resultlen;
  char name[MPI_MAX_PROCESSOR_NAME];

127 128 129 130

  MPI_Comm_size (MPI_COMM_WORLD, &size);
  MPI_Comm_rank (MPI_COMM_WORLD, &rank);
  
131 132 133
  MPI_Get_processor_name( name, &resultlen );

  color = rank / this->ProcessPerNode ();
134 135 136
  
  MPI_Comm_split ( MPI_COMM_WORLD, color, rank, &nodeComm );
  MPI_Comm_rank  ( nodeComm, &nodeRank );
137 138
  MPI_Comm_size  ( nodeComm, &nodeSize );

139 140 141 142
  MPI_Comm_split ( MPI_COMM_WORLD, nodeRank == 0, rank, &masterComm );
  MPI_Comm_rank  ( masterComm, &masterRank );
  MPI_Comm_size  ( masterComm, &masterSize );

143 144 145
  worldRanks  = (int *) malloc ( masterSize * sizeof ( int ) );
  worldDistIO = (int *) malloc ( masterSize * sizeof ( int ) );
  distanceIO  = this->DistanceToIONode ( 0 );
146

147 148 149 150 151 152 153 154 155 156
  MPI_Gather ( &rank, 1, MPI_INT, worldRanks, 1, MPI_INT, 0, masterComm );
  MPI_Gather ( &distanceIO, 1, MPI_INT, worldDistIO, 1, MPI_INT, 0, masterComm );  
  
  if ( masterRank == 0 && masterSize == ( size / this->ProcessPerNode () ) ) {
    fprintf ( stdout, "  N " );
    for ( src = 0; src < masterSize; src++ )
      fprintf ( stdout, "%3d ", src );
    
    fprintf ( stdout, " IO\n" );
    
157
    for ( src = 0; src < masterSize; src++ ) {
158
      fprintf ( stdout, "%3d ", src );
159
      for ( dest = 0; dest < masterSize; dest++ ) {
160
	fprintf ( stdout, "%3d ", DistanceBetweenRanks ( worldRanks [ src ], worldRanks [ dest ] ) );
161
      }
162 163
      
      fprintf ( stdout, "%3d\n", worldDistIO [ src ] );
164
    }
165 166 167 168 169 170 171

    fprintf ( stdout, " IO " );
    for ( src = 0; src < masterSize; src++ ) {
      fprintf ( stdout, "%3d ", worldDistIO [ src ] );
    }

    fprintf ( stdout, "%3d\n", 0 );
172 173 174 175 176 177
  }
  
  return;
}


178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
int Topology::getRoutingOrder ( int *ro ) {
  uint64_t dcr_det_order = DCRReadUser(ND_500_DCR(CTRL_DET_ORDER));
  
  int A = ND_500_DCR__CTRL_DET_ORDER__MASK0_get(dcr_det_order);
  int B = ND_500_DCR__CTRL_DET_ORDER__MASK1_get(dcr_det_order);
  int C = ND_500_DCR__CTRL_DET_ORDER__MASK2_get(dcr_det_order);
  int D = ND_500_DCR__CTRL_DET_ORDER__MASK3_get(dcr_det_order);
  int E = ND_500_DCR__CTRL_DET_ORDER__MASK4_get(dcr_det_order);
  
  int torus[5] = {A, B, C, D, E};
  
  int index = 0;
  for (int i=0; i<5 ; i++) {
    if (torus[i] == 1)            index = 4;
    else if (torus[i] == 2)       index = 3;
    else if (torus[i] == 4)       index = 2;
    else if (torus[i] == 8)       index = 1;
    else if (torus[i] == 16)      index = 0;
    ro[i] = index;
  }
  
  return 0;
}


int Topology::RouteBetweenCoords ( int *srcCoords, int *destCoords, int *path ) {
  int i, interNode = 0;
  int unitHop = 1;
  int childCoords[6], intmdtCoords[6];
  char buf[64];
  MPIX_Hardware_t hw;
  int dimSize[MPIX_TORUS_MAX_DIMS];
  int isTorus[MPIX_TORUS_MAX_DIMS];
  int *routingOrder;
  int srcRank, destRank;
  
  MPIX_Torus2rank ( srcCoords, &srcRank );
  MPIX_Torus2rank ( destCoords, &destRank );
  
  routingOrder = new int[MPIX_TORUS_MAX_DIMS];
  getRoutingOrder(routingOrder);
  
  MPIX_Hardware(&hw);
  
  for (i=0; i<MPIX_TORUS_MAX_DIMS ; i++) {
    isTorus[i] = hw.isTorus[i];
    dimSize[i] = hw.Size[i];
  }
  
  //Initialize intermediate nodes in original path to the destination node
  for (int dim=0; dim < MPIX_TORUS_MAX_DIMS; dim++)
    intmdtCoords[dim] = srcCoords[dim];
  intmdtCoords[MPIX_TORUS_MAX_DIMS] = destCoords[MPIX_TORUS_MAX_DIMS];  //T
  
  int hopnum = 0;
  int hopDiff, intmdt_rank, child, parent;
  
  child = srcRank;
  for (int dim=0; dim<MPIX_TORUS_MAX_DIMS; dim++) {
    
    int dimID = routingOrder[dim];
    hopDiff = abs(destCoords[dimID] - srcCoords[dimID]);
    
    if (hw.isTorus[dimID] == 1)
      hopDiff = std::min (hopDiff, (int)hw.Size[dimID] - hopDiff) ;
    
    for(int diff=0; diff<hopDiff ;diff++) {
      if (hw.isTorus[dimID] == 0) {
	if(destCoords[dimID] < srcCoords[dimID])
	  intmdtCoords[dimID] -= unitHop;
	else intmdtCoords[dimID] += unitHop;
      }
      else {            // torus
	if (abs(destCoords[dimID] - srcCoords[dimID])*2 > hw.Size[dimID])
	  {
	    if (destCoords[dimID] > srcCoords[dimID])
	      intmdtCoords[dimID] = ((intmdtCoords[dimID] - unitHop) + hw.Size[dimID]) % hw.Size[dimID];
	    else
	      intmdtCoords[dimID] = (intmdtCoords[dimID] + unitHop) % hw.Size[dimID];
	  }
	else if (abs(destCoords[dimID] - srcCoords[dimID])*2 < hw.Size[dimID])
	  {
	    if (destCoords[dimID] < srcCoords[dimID])
	      intmdtCoords[dimID] = ((intmdtCoords[dimID] - unitHop) + hw.Size[dimID]) % hw.Size[dimID];
	    else
	      intmdtCoords[dimID] = (intmdtCoords[dimID] + unitHop) % hw.Size[dimID];
	  }
	else
	  {
	    //if source coord is even, plus direction
	    if (srcCoords[dimID]%2 == 0)
	      intmdtCoords[dimID] = (intmdtCoords[dimID] + unitHop) % hw.Size[dimID];
	    //even source coord: traverse in plus direction
	    else
	      intmdtCoords[dimID] = ((intmdtCoords[dimID] - unitHop) + hw.Size[dimID]) % hw.Size[dimID];
	  }
      }
      
      ++hopnum;
      
      //get the rank
      MPIX_Torus2rank (intmdtCoords, &intmdt_rank);
      parent = intmdt_rank;
      
      path[interNode] = child;
      interNode++;
      
      MPIX_Rank2torus (child, childCoords);
      child = parent;
    }
  }
  
  path[interNode] = destRank;
  interNode++;
  
  return interNode;
}