seqMaxMeanCycle.c 17.7 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**CFile****************************************************************

  FileName    [seqMaxMeanCycle.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Construction and manipulation of sequential AIGs.]

  Synopsis    [Efficient computation of maximum mean cycle times.]

  Author      [Aaron P. Hurst]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - May 15, 2006.]

  Revision    [$Id: seqMaxMeanCycle.c,v 1.00 2005/05/15 00:00:00 ahurst Exp $]

***********************************************************************/

#include "seqInt.h"
#include "hash.h"

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 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 295 296 297 298 299 300 301 302 303 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 344 345 346 347 348 349 350 351 352 353 354 355
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

struct Abc_ManTime_t_
{
    Abc_Time_t     tArrDef;
    Abc_Time_t     tReqDef;
    Vec_Ptr_t  *   vArrs;
    Vec_Ptr_t  *   vReqs;
};

typedef struct Seq_HowardData_t_
{
  char   visited;
  int    mark;
  int    policy;
  float  cycle;
  float  skew;
  float  delay;
} Seq_HowardData_t;

// accessing the arrival and required times of a node
static inline Abc_Time_t * Abc_NodeArrival( Abc_Obj_t * pNode )  {  return pNode->pNtk->pManTime->vArrs->pArray[pNode->Id];  }
static inline Abc_Time_t * Abc_NodeRequired( Abc_Obj_t * pNode ) {  return pNode->pNtk->pManTime->vReqs->pArray[pNode->Id];  }

Hash_Ptr_t * Seq_NtkPathDelays( Abc_Ntk_t * pNtk, int fVerbose );
void Seq_NtkMergePios( Abc_Ntk_t * pNtk, Hash_Ptr_t * hFwdDelays, int fVerbose );

void Seq_NtkHowardLoop( Abc_Ntk_t * pNtk, Hash_Ptr_t * hFwdDelays,
                        Hash_Ptr_t * hNodeData, int node,
                        int *howardDepth, float *howardDelay, int *howardSink,
                        float *maxMeanCycle);
void Abc_NtkDfsReverse_rec2( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes, Vec_Ptr_t * vEndpoints );

#define Seq_NtkGetPathDelay( hFwdDelays, from, to ) \
  (Hash_PtrExists(hFwdDelays, from)?Hash_FltEntry( ((Hash_Flt_t *)Hash_PtrEntry(hFwdDelays, from, 0)), to, 0):0 )

#define HOWARD_EPSILON 1e-3
#define ZERO_SLOP      1e-5
#define REMOVE_ZERO_SLOP( x ) \
  (x = (x > -ZERO_SLOP && x < ZERO_SLOP)?0:x)

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Computes maximum mean cycle time.]

  Description [Uses Howard's algorithm.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Seq_NtkHoward( Abc_Ntk_t * pNtk, int fVerbose ) {

  Abc_Obj_t *  pObj;
  Hash_Ptr_t * hFwdDelays;
  Hash_Flt_t * hOutgoing;
  Hash_Ptr_Entry_t * pSourceEntry, * pNodeEntry;
  Hash_Flt_Entry_t * pSinkEntry;
  int          i, j, iteration = 0;
  int          source, sink;
  int          fChanged;
  int          howardDepth, howardSink = 0;
  float        delay, howardDelay, t;
  float        maxMeanCycle = -ABC_INFINITY;
  Hash_Ptr_t *       hNodeData;
  Seq_HowardData_t * pNodeData, * pSourceData, * pSinkData;

  // gather timing constraints
  hFwdDelays = Seq_NtkPathDelays( pNtk, fVerbose );
  Seq_NtkMergePios( pNtk, hFwdDelays, fVerbose );

  // initialize data, create initial policy
  hNodeData = Hash_PtrAlloc( hFwdDelays->nSize );
  Hash_PtrForEachEntry( hFwdDelays, pSourceEntry, i ) {
    Hash_PtrWriteEntry( hNodeData, pSourceEntry->key, 
                        (pNodeData = ALLOC(Seq_HowardData_t, 1)) );
    pNodeData->skew = 0.0;
    pNodeData->policy = 0;
    hOutgoing = (Hash_Flt_t *)(pSourceEntry->data);
    assert(hOutgoing);

    Hash_FltForEachEntry( hOutgoing, pSinkEntry, j ) {
      sink = pSinkEntry->key;
      delay = pSinkEntry->data;
       if (delay > pNodeData->skew) {
        pNodeData->policy = sink;
        pNodeData->skew = delay;
      }
    }
  }

  // iteratively refine policy
  do {
    iteration++;
    fChanged = 0;
    howardDelay = 0.0;
    howardDepth = 0;

    // reset data
    Hash_PtrForEachEntry( hNodeData, pNodeEntry, i ) {
      pNodeData = (Seq_HowardData_t *)pNodeEntry->data;
      pNodeData->skew = -ABC_INFINITY;
      pNodeData->cycle = -ABC_INFINITY;
      pNodeData->mark = 0;
      pNodeData->visited = 0;
    }

    // find loops in policy graph
    Hash_PtrForEachEntry( hNodeData, pNodeEntry, i ) {
      pNodeData = (Seq_HowardData_t *)(pNodeEntry->data);
      assert(pNodeData);
      if (!pNodeData->visited)
        Seq_NtkHowardLoop( pNtk, hFwdDelays, 
                           hNodeData, pNodeEntry->key,
                           &howardDepth, &howardDelay, &howardSink, &maxMeanCycle);
    }

    if (!howardSink) {
      return -1;
    }

    // improve policy by tightening loops
    Hash_PtrForEachEntry( hFwdDelays, pSourceEntry, i ) {
      source = pSourceEntry->key;
      pSourceData = (Seq_HowardData_t *)Hash_PtrEntry( hNodeData, source, 0 );
      assert(pSourceData);
      hOutgoing = (Hash_Flt_t *)(pSourceEntry->data);
      assert(hOutgoing);
      Hash_FltForEachEntry( hOutgoing, pSinkEntry, j ) {
        sink = pSinkEntry->key;
        pSinkData = (Seq_HowardData_t *)Hash_PtrEntry( hNodeData, sink, 0 );
        assert(pSinkData);
        delay = pSinkEntry->data;
        
        if (pSinkData->cycle > pSourceData->cycle + HOWARD_EPSILON) {
          fChanged = 1;
          pSourceData->cycle = pSinkData->cycle;
          pSourceData->policy = sink;
        }
      }
    }

    // improve policy by correcting skews
    if (!fChanged) {
      Hash_PtrForEachEntry( hFwdDelays, pSourceEntry, i ) {
        source = pSourceEntry->key;
        pSourceData = (Seq_HowardData_t *)Hash_PtrEntry( hNodeData, source, 0 );
        assert(pSourceData);
        hOutgoing = (Hash_Flt_t *)(pSourceEntry->data);
        assert(hOutgoing);
        Hash_FltForEachEntry( hOutgoing, pSinkEntry, j ) {
          sink = pSinkEntry->key;
          pSinkData = (Seq_HowardData_t *)Hash_PtrEntry( hNodeData, sink, 0 );
          assert(pSinkData);
          delay = pSinkEntry->data;

          if (pSinkData->cycle < 0.0 || pSinkData->cycle < pSourceData->cycle)
            continue;

          t = delay - pSinkData->cycle + pSinkData->skew;
          if (t > pSourceData->skew + HOWARD_EPSILON) {
            fChanged = 1;
            pSourceData->skew = t;
            pSourceData->policy = sink;
          }
        }
      }
    }

    if (fVerbose) printf("Iteration %d \t Period = %.2f\n", iteration, maxMeanCycle);
  } while (fChanged);

  // set global skew, mmct
  pNodeData = Hash_PtrEntry( hNodeData, -1, 0 );
  pNtk->globalSkew = -pNodeData->skew;
  pNtk->maxMeanCycle = maxMeanCycle;

  // set endpoint skews
  Vec_FltGrow( pNtk->vSkews, Abc_NtkLatchNum( pNtk ) );
  pNtk->vSkews->nSize =  Abc_NtkLatchNum( pNtk );
  Abc_NtkForEachLatch( pNtk, pObj, i ) {
    pNodeData = Hash_PtrEntry( hNodeData, pObj->Id, 0 );
    // skews are set based on latch # NOT id #
    Abc_NtkSetLatSkew( pNtk, i, pNodeData->skew );
  }

  // free node data
  Hash_PtrForEachEntry( hNodeData, pNodeEntry, i ) {
    pNodeData = (Seq_HowardData_t *)(pNodeEntry->data);
    FREE( pNodeData );
  }
  Hash_PtrFree(hNodeData);

  // free delay data
  Hash_PtrForEachEntry( hFwdDelays, pSourceEntry, i ) {
    Hash_FltFree( (Hash_Flt_t *)(pSourceEntry->data) );
  }
  Hash_PtrFree(hFwdDelays);

  return maxMeanCycle;
}

/**Function*************************************************************

  Synopsis    [Computes the mean cycle times of current policy graph.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Seq_NtkHowardLoop( Abc_Ntk_t * pNtk, Hash_Ptr_t * hFwdDelays,
                        Hash_Ptr_t * hNodeData, int node,
                        int *howardDepth, float *howardDelay, int *howardSink,
                        float *maxMeanCycle) {
  
  Seq_HowardData_t * pNodeData, *pToData;
  float delay, t;

  pNodeData = (Seq_HowardData_t *)Hash_PtrEntry( hNodeData, node, 0 );
  assert(pNodeData);
  pNodeData->visited = 1;
  pNodeData->mark = ++(*howardDepth);
  pNodeData->delay = (*howardDelay);
  if (pNodeData->policy) {
    pToData = (Seq_HowardData_t *)Hash_PtrEntry( hNodeData, pNodeData->policy, 0 );
    assert(pToData);
    delay = Seq_NtkGetPathDelay( hFwdDelays, node, pNodeData->policy );
    assert(delay > 0.0);
    (*howardDelay) += delay;
    if (pToData->mark) {
      t = (*howardDelay - pToData->delay) / (*howardDepth - pToData->mark + 1);
      pNodeData->cycle = t;
      pNodeData->skew = 0.0;
      if (*maxMeanCycle < t) {
        *maxMeanCycle = t;
        *howardSink = pNodeData->policy;
      }
    } else {
      if(!pToData->visited) {
        Seq_NtkHowardLoop(pNtk, hFwdDelays, hNodeData, pNodeData->policy,
                          howardDepth, howardDelay, howardSink, maxMeanCycle);
      }
      if(pToData->cycle > 0) {
        t = delay - pToData->cycle + pToData->skew;
    pNodeData->skew = t;
    pNodeData->cycle = pToData->cycle;
      }
    }
  }
  *howardDelay = pNodeData->delay;
  pNodeData->mark = 0;
  --(*howardDepth);
}

/**Function*************************************************************

  Synopsis    [Computes the register-to-register delays.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hash_Ptr_t * Seq_NtkPathDelays( Abc_Ntk_t * pNtk, int fVerbose ) {

  Abc_Time_t * pTime, ** ppTimes;
  Abc_Obj_t * pObj, * pDriver, * pStart, * pFanout;
  Vec_Ptr_t * vNodes, * vEndpoints;
  int i, j, nPaths = 0;
  Hash_Flt_t *  hOutgoing;
  Hash_Ptr_t *  hFwdDelays;
  float     nMaxPath = 0, nSumPath = 0;

  extern void Abc_NtkTimePrepare( Abc_Ntk_t * pNtk );
  extern void Abc_NodeDelayTraceArrival( Abc_Obj_t * pNode );

  if (fVerbose) printf("Gathering path delays...\n");

  hFwdDelays = Hash_PtrAlloc( Abc_NtkCiNum( pNtk ) );

  assert( Abc_NtkIsMappedLogic(pNtk) );

  Abc_NtkTimePrepare( pNtk );
  ppTimes = (Abc_Time_t **)pNtk->pManTime->vArrs->pArray;
  vNodes = Vec_PtrAlloc( 100 );
  vEndpoints = Vec_PtrAlloc( 100 );

  // set the initial times (i.e. ignore all inputs)
  Abc_NtkForEachObj( pNtk, pObj, i) {
    pTime = ppTimes[pObj->Id];
    pTime->Fall = pTime->Rise = pTime->Worst = -ABC_INFINITY;    
  }

  // starting at each Ci, compute timing forward
  Abc_NtkForEachCi( pNtk, pStart, j ) {
    
    hOutgoing = Hash_FltAlloc( 10 );
    Hash_PtrWriteEntry( hFwdDelays, pStart->Id, (void *)(hOutgoing) );

    // seed the starting point of interest
    pTime = ppTimes[pStart->Id];
    pTime->Fall = pTime->Rise = pTime->Worst = 0.0;

    // find a DFS ordering from the start
    Abc_NtkIncrementTravId( pNtk );
    Abc_NodeSetTravIdCurrent( pStart );
    pObj = Abc_ObjFanout0Ntk(pStart);
    Abc_ObjForEachFanout( pObj, pFanout, i )
      Abc_NtkDfsReverse_rec2( pFanout, vNodes, vEndpoints );
    if ( Abc_ObjIsCo( pStart ) )
      Vec_PtrPush( vEndpoints, pStart );

    // do timing analysis
    for ( i = vNodes->nSize-1; i >= 0; --i )
      Abc_NodeDelayTraceArrival( vNodes->pArray[i] );

    // there is a path to each set of Co endpoints
356
    Vec_PtrForEachEntry( Abc_Obj_t *, vEndpoints, pObj, i )
Alan Mishchenko committed
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 390 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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 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 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    {
      assert(pObj);
      assert( Abc_ObjIsCo( pObj ) );
      pDriver = Abc_ObjFanin0(pObj);
      pTime   = Abc_NodeArrival(pDriver);
      if ( pTime->Worst > 0 ) {
        Hash_FltWriteEntry( hOutgoing, pObj->Id, pTime->Worst );
        nPaths++;
        // if (fVerbose) printf("\tpath %d,%d delay = %f\n", pStart->Id, pObj->Id, pTime->Worst);
        nSumPath += pTime->Worst;
        if (pTime->Worst > nMaxPath)
          nMaxPath = pTime->Worst;
      }
    }

    // clear the times that were altered
    for ( i = 0; i < vNodes->nSize; i++ ) {
      pObj = (Abc_Obj_t *)(vNodes->pArray[i]);
      pTime = ppTimes[pObj->Id];
      pTime->Fall = pTime->Rise = pTime->Worst = -ABC_INFINITY;
    }
    pTime = ppTimes[pStart->Id];
    pTime->Fall = pTime->Rise = pTime->Worst = -ABC_INFINITY;
    
    Vec_PtrClear( vNodes );
    Vec_PtrClear( vEndpoints );
  }

  Vec_PtrFree( vNodes );

  // rezero Cis (note: these should be restored to values if they were nonzero)
  Abc_NtkForEachCi( pNtk, pObj, i) {
    pTime = ppTimes[pObj->Id];
    pTime->Fall = pTime->Rise = pTime->Worst = 0.0;    
  }

  if (fVerbose) printf("Num. paths = %d\tMax. Path Delay = %.2f\tAvg. Path Delay = %.2f\n", nPaths, nMaxPath, nSumPath / nPaths);
  return hFwdDelays;
}


/**Function*************************************************************

  Synopsis    [Merges all the Pios together into one ID = -1.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Seq_NtkMergePios( Abc_Ntk_t * pNtk, Hash_Ptr_t * hFwdDelays, 
                       int fVerbose ) {

  Abc_Obj_t *        pObj;
  Hash_Flt_Entry_t * pSinkEntry;
  Hash_Ptr_Entry_t * pSourceEntry;
  Hash_Flt_t *       hOutgoing, * hPioSource;
  int                i, j;
  int                source, sink, nMerges = 0;
  float              delay = 0, max_delay = 0;
  Vec_Int_t *        vFreeList;

  vFreeList = Vec_IntAlloc( 10 );

  // create a new "-1" source entry for the Pios
  hPioSource = Hash_FltAlloc( 100 );
  Hash_PtrWriteEntry( hFwdDelays, -1, (void *)(hPioSource) );

  // merge all edges with a Pio as a source
  Abc_NtkForEachPi( pNtk, pObj, i ) {
    source = pObj->Id;
    hOutgoing = (Hash_Flt_t *)Hash_PtrEntry( hFwdDelays, source, 0 );
    if (!hOutgoing) continue;

    Hash_PtrForEachEntry( hOutgoing, pSinkEntry, j ) {
      nMerges++;
      sink = pSinkEntry->key;
      delay = pSinkEntry->data;
      if (Hash_FltEntry( hPioSource, sink, 1 ) < delay) {
        Hash_FltWriteEntry( hPioSource, sink, delay );
      }
    }

    Hash_FltFree( hOutgoing );
    Hash_PtrRemove( hFwdDelays, source );
  }

  // merge all edges with a Pio as a sink
  Hash_PtrForEachEntry( hFwdDelays, pSourceEntry, i ) {
    hOutgoing = (Hash_Flt_t *)(pSourceEntry->data);
    Hash_FltForEachEntry( hOutgoing, pSinkEntry, j ) {
      sink = pSinkEntry->key;
      delay = pSinkEntry->data;

      max_delay = -ABC_INFINITY;
      if (Abc_ObjIsPo( Abc_NtkObj( pNtk, sink ) )) {
        nMerges++;
        if (delay > max_delay)
          max_delay = delay;
        Vec_IntPush( vFreeList, sink );
      }
    }
    if (max_delay != -ABC_INFINITY)
      Hash_FltWriteEntry( hOutgoing, -1, delay );
    // do freeing
    while( vFreeList->nSize > 0 ) {
      Hash_FltRemove( hOutgoing, Vec_IntPop( vFreeList ) );
    }
  }

  if (fVerbose) printf("Merged %d paths into one Pio node\n", nMerges);

}

/**Function*************************************************************

  Synopsis    [This is a modification of routine from abcDfs.c]

  Description [Recursive DFS from a starting point.  Keeps the endpoints.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkDfsReverse_rec2( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes, Vec_Ptr_t * vEndpoints )
{
    Abc_Obj_t * pFanout;
    int i;
    assert( !Abc_ObjIsNet(pNode) );
    // if this node is already visited, skip
    if ( Abc_NodeIsTravIdCurrent( pNode ) )
        return;
    // mark the node as visited
    Abc_NodeSetTravIdCurrent( pNode );
    // terminate at the Co
    if ( Abc_ObjIsCo(pNode) ) {
      Vec_PtrPush( vEndpoints, pNode );
      return;
    }
    assert( Abc_ObjIsNode( pNode ) );
    // visit the transitive fanin of the node
    pNode = Abc_ObjFanout0Ntk(pNode);
    Abc_ObjForEachFanout( pNode, pFanout, i )
      Abc_NtkDfsReverse_rec2( pFanout, vNodes, vEndpoints );
    // add the node after the fanins have been added
    Vec_PtrPush( vNodes, pNode );
}

/**Function*************************************************************

  Synopsis    [Converts all skews into forward skews 0<skew<T.]

  Description [Can also minimize total skew by changing global skew.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Seq_NtkSkewForward( Abc_Ntk_t * pNtk, float period, int fMinimize ) {
  
  Abc_Obj_t * pObj;
  int         i;
  float       skew;
  float       currentSum = 0, bestSum = ABC_INFINITY;
  float       currentOffset = 0, nextStep, bestOffset = 0;
  
  assert( pNtk->vSkews->nSize >= Abc_NtkLatchNum( pNtk )-1 );

  if (fMinimize) {
    // search all offsets for the one that minimizes sum of skews
    while(currentOffset < period) {
      currentSum = 0;
      nextStep = period;
      Abc_NtkForEachLatch( pNtk, pObj, i ) {
        skew = Abc_NtkGetLatSkew( pNtk, i ) + currentOffset;
        skew = (float)(skew - period*floor(skew/period));
        currentSum += skew;
        if (skew > ZERO_SLOP && skew < nextStep) {
          nextStep = skew;
        }
      }

      if (currentSum < bestSum) {
        bestSum = currentSum;
        bestOffset = currentOffset;
      }
      currentOffset += nextStep;
    }
    printf("Offseting all skews by %.2f\n", bestOffset);
  }

  // convert global skew into forward skew
  pNtk->globalSkew = pNtk->globalSkew - bestOffset;
  pNtk->globalSkew = (float)(pNtk->globalSkew - period*floor(pNtk->globalSkew/period));
  assert(pNtk->globalSkew>= 0 && pNtk->globalSkew < period);
    
  // convert endpoint skews into forward skews
  Abc_NtkForEachLatch( pNtk, pObj, i ) {
    skew = Abc_NtkGetLatSkew( pNtk, i ) + bestOffset;
    skew = (float)(skew - period*floor(skew/period));
    REMOVE_ZERO_SLOP( skew );
    assert(skew >=0  && skew < period);

    Abc_NtkSetLatSkew( pNtk, i, skew );
  }
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////
571 572
ABC_NAMESPACE_IMPL_END