saigSwitch.c 16.4 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
/**CFile****************************************************************

  FileName    [saigSwitch.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Sequential AIG package.]

  Synopsis    [Returns switching propabilities.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: saigSwitch.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "saig.h"

23
#include "base/main/main.h"
24 25 26 27

ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Saig_SimObj_t_ Saig_SimObj_t;
struct Saig_SimObj_t_
{
    int      iFan0;
    int      iFan1;
    unsigned Type   :  8;
    unsigned Number : 24;
    unsigned pData[1];
};

static inline int Saig_SimObjFaninC0( Saig_SimObj_t * pObj )  { return pObj->iFan0 & 1;  }
static inline int Saig_SimObjFaninC1( Saig_SimObj_t * pObj )  { return pObj->iFan1 & 1;  }
static inline int Saig_SimObjFanin0( Saig_SimObj_t * pObj )   { return pObj->iFan0 >> 1; }
static inline int Saig_SimObjFanin1( Saig_SimObj_t * pObj )   { return pObj->iFan1 >> 1; }

//typedef struct Aig_CMan_t_ Aig_CMan_t;

//static Aig_CMan_t * Aig_CManCreate( Aig_Man_t * p );

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

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

  Synopsis    [Creates fast simulation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Saig_SimObj_t * Saig_ManCreateMan( Aig_Man_t * p )
{
    Saig_SimObj_t * pAig, * pEntry;
    Aig_Obj_t * pObj;
    int i;
Alan Mishchenko committed
71
    pAig = ABC_CALLOC( Saig_SimObj_t, Aig_ManObjNumMax(p)+1 );
72
//    printf( "Allocating %7.2f MB.\n", 1.0 * sizeof(Saig_SimObj_t) * (Aig_ManObjNumMax(p)+1)/(1<<20) );
Alan Mishchenko committed
73 74 75 76
    Aig_ManForEachObj( p, pObj, i )
    {
        pEntry = pAig + i;
        pEntry->Type = pObj->Type;
77
        if ( Aig_ObjIsCi(pObj) || i == 0 )
Alan Mishchenko committed
78 79 80 81 82 83 84 85 86
        {
            if ( Saig_ObjIsLo(p, pObj) )
            {
                pEntry->iFan0 = (Saig_ObjLoToLi(p, pObj)->Id << 1);
                pEntry->iFan1 = -1;
            }
            continue;
        }
        pEntry->iFan0 = (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj);
87
        if ( Aig_ObjIsCo(pObj) )
Alan Mishchenko committed
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
            continue;
        assert( Aig_ObjIsNode(pObj) );
        pEntry->iFan1 = (Aig_ObjFaninId1(pObj) << 1) | Aig_ObjFaninC1(pObj);
    }
    pEntry = pAig + Aig_ManObjNumMax(p);
    pEntry->Type = AIG_OBJ_VOID;
    return pAig;
}

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

  Synopsis    [Simulated one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Saig_ManSimulateNode2( Saig_SimObj_t * pAig, Saig_SimObj_t * pObj )
{
    Saig_SimObj_t * pObj0 = pAig + Saig_SimObjFanin0( pObj );
    Saig_SimObj_t * pObj1 = pAig + Saig_SimObjFanin1( pObj );
    if ( Saig_SimObjFaninC0(pObj) && Saig_SimObjFaninC1(pObj) )
        pObj->pData[0] = ~(pObj0->pData[0] | pObj1->pData[0]);
    else if ( Saig_SimObjFaninC0(pObj) && !Saig_SimObjFaninC1(pObj) )
        pObj->pData[0] = (~pObj0->pData[0] & pObj1->pData[0]);
    else if ( !Saig_SimObjFaninC0(pObj) && Saig_SimObjFaninC1(pObj) )
        pObj->pData[0] = (pObj0->pData[0] & ~pObj1->pData[0]);
    else // if ( !Saig_SimObjFaninC0(pObj) && !Saig_SimObjFaninC1(pObj) )
        pObj->pData[0] = (pObj0->pData[0] & pObj1->pData[0]);
}

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

  Synopsis    [Simulated one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Saig_ManSimulateNode( Saig_SimObj_t * pAig, Saig_SimObj_t * pObj )
{
    Saig_SimObj_t * pObj0 = pAig + Saig_SimObjFanin0( pObj );
    Saig_SimObj_t * pObj1 = pAig + Saig_SimObjFanin1( pObj );
    pObj->pData[0] = (Saig_SimObjFaninC0(pObj)? ~pObj0->pData[0] : pObj0->pData[0])
        & (Saig_SimObjFaninC1(pObj)? ~pObj1->pData[0] : pObj1->pData[0]);
}

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

  Synopsis    [Simulated buffer/inverter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Saig_ManSimulateOneInput( Saig_SimObj_t * pAig, Saig_SimObj_t * pObj )
{
    Saig_SimObj_t * pObj0 = pAig + Saig_SimObjFanin0( pObj );
    if ( Saig_SimObjFaninC0(pObj) )
        pObj->pData[0] = ~pObj0->pData[0];
    else // if ( !Saig_SimObjFaninC0(pObj) )
        pObj->pData[0] = pObj0->pData[0];
}

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

  Synopsis    [Simulates the timeframes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Saig_ManSimulateFrames( Saig_SimObj_t * pAig, int nFrames, int nPref )
{
    Saig_SimObj_t * pEntry;
    int f;
    for ( f = 0; f < nFrames; f++ )
    {
        for ( pEntry = pAig; pEntry->Type != AIG_OBJ_VOID; pEntry++ )
        {
            if ( pEntry->Type == AIG_OBJ_AND )
                Saig_ManSimulateNode( pAig, pEntry );
182
            else if ( pEntry->Type == AIG_OBJ_CO )
Alan Mishchenko committed
183
                Saig_ManSimulateOneInput( pAig, pEntry );
184
            else if ( pEntry->Type == AIG_OBJ_CI )
Alan Mishchenko committed
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
            {
                if ( pEntry->iFan0 == 0 ) // true PI
                    pEntry->pData[0] = Aig_ManRandom( 0 );
                else if ( f > 0 ) // register output
                    Saig_ManSimulateOneInput( pAig, pEntry );
            }
            else if ( pEntry->Type == AIG_OBJ_CONST1 )
                pEntry->pData[0] = ~0;
            else if ( pEntry->Type != AIG_OBJ_NONE )
                assert( 0 );
            if ( f >= nPref )
                pEntry->Number += Aig_WordCountOnes( pEntry->pData[0] );
        }
    }
}

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

  Synopsis    [Computes switching activity of one node.]

  Description [Uses the formula: Switching = 2 * nOnes * nZeros / (nTotal ^ 2) ]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Saig_ManComputeSwitching( int nOnes, int nSimWords )
{
    int nTotal = 32 * nSimWords;
    return (float)2.0 * nOnes / nTotal * (nTotal - nOnes) / nTotal;
}

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

  Synopsis    [Computes switching activity of one node.]

  Description [Uses the formula: Switching = 2 * nOnes * nZeros / (nTotal ^ 2) ]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Saig_ManComputeProbOne( int nOnes, int nSimWords )
{
    int nTotal = 32 * nSimWords;
    return (float)nOnes / nTotal;
}

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

  Synopsis    [Computes switching activity of one node.]

  Description [Uses the formula: Switching = 2 * nOnes * nZeros / (nTotal ^ 2) ]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Saig_ManComputeProbOnePlus( int nOnes, int nSimWords, int fCompl )
{
    int nTotal = 32 * nSimWords;
    if ( fCompl )
        return (float)(nTotal-nOnes) / nTotal;
    else
        return (float)nOnes / nTotal;
}

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

  Synopsis    [Compute switching probabilities of all nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
266
Vec_Int_t * Saig_ManComputeSwitchProb4s( Aig_Man_t * p, int nFrames, int nPref, int fProbOne )
Alan Mishchenko committed
267 268 269 270
{
    Saig_SimObj_t * pAig, * pEntry;
    Vec_Int_t * vSwitching;
    float * pSwitching;
271
    int nFramesReal;
272
    abctime clk;//, clkTotal = Abc_Clock();
Alan Mishchenko committed
273 274
    vSwitching = Vec_IntStart( Aig_ManObjNumMax(p) );
    pSwitching = (float *)vSwitching->pArray;
275
clk = Abc_Clock();
Alan Mishchenko committed
276
    pAig = Saig_ManCreateMan( p );
277
//ABC_PRT( "\nCreation  ", Abc_Clock() - clk );
Alan Mishchenko committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

    Aig_ManRandom( 1 );
    // get the number of  frames to simulate
    // if the parameter "seqsimframes" is defined, use it
    // otherwise, use the given number of frames "nFrames"
    nFramesReal = nFrames;
    if ( Abc_FrameReadFlag("seqsimframes") )
        nFramesReal = atoi( Abc_FrameReadFlag("seqsimframes") );
    if ( nFramesReal <= nPref )
    {
        printf( "The total number of frames (%d) should exceed prefix (%d).\n", nFramesReal, nPref );\
        printf( "Setting the total number of frames to be %d.\n", nFrames );
        nFramesReal = nFrames;
    }
//printf( "Simulating %d frames.\n", nFramesReal );
293
clk = Abc_Clock();
Alan Mishchenko committed
294
    Saig_ManSimulateFrames( pAig, nFramesReal, nPref );
295 296
//ABC_PRT( "Simulation", Abc_Clock() - clk );
clk = Abc_Clock();
Alan Mishchenko committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    for ( pEntry = pAig; pEntry->Type != AIG_OBJ_VOID; pEntry++ )
    {
/*
        if ( pEntry->Type == AIG_OBJ_AND )
        {
        Saig_SimObj_t * pObj0 = pAig + Saig_SimObjFanin0( pEntry );
        Saig_SimObj_t * pObj1 = pAig + Saig_SimObjFanin1( pEntry );
        printf( "%5.2f = %5.2f * %5.2f  (%7.4f)\n", 
            Saig_ManComputeProbOnePlus( pEntry->Number, nFrames - nPref, 0 ),
            Saig_ManComputeProbOnePlus( pObj0->Number, nFrames - nPref, Saig_SimObjFaninC0(pEntry) ),
            Saig_ManComputeProbOnePlus( pObj1->Number, nFrames - nPref, Saig_SimObjFaninC1(pEntry) ),
            Saig_ManComputeProbOnePlus( pEntry->Number, nFrames - nPref, 0 ) -
            Saig_ManComputeProbOnePlus( pObj0->Number, nFrames - nPref, Saig_SimObjFaninC0(pEntry) ) *
            Saig_ManComputeProbOnePlus( pObj1->Number, nFrames - nPref, Saig_SimObjFaninC1(pEntry) )
            );
        }
*/
        if ( fProbOne )
            pSwitching[pEntry-pAig] = Saig_ManComputeProbOne( pEntry->Number, nFramesReal - nPref );
        else
            pSwitching[pEntry-pAig] = Saig_ManComputeSwitching( pEntry->Number, nFramesReal - nPref );
//printf( "%3d : %7.2f\n", pEntry-pAig, pSwitching[pEntry-pAig] );
    }
Alan Mishchenko committed
320
    ABC_FREE( pAig );
321 322
//ABC_PRT( "Switch    ", Abc_Clock() - clk );
//ABC_PRT( "TOTAL     ", Abc_Clock() - clkTotal );
Alan Mishchenko 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 355 356 357 358 359 360 361 362 363

//    Aig_CManCreate( p );
    return vSwitching;
}




typedef struct Aig_CMan_t_ Aig_CMan_t;
struct Aig_CMan_t_
{
    // parameters
    int             nIns;
    int             nNodes;
    int             nOuts;
    // current state
    int             iNode;
    int             iDiff0;
    int             iDiff1;
    unsigned char * pCur;
    // stored data
    int             iPrev;
    int             nBytes;
    unsigned char   Data[0];
};


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_CMan_t * Aig_CManStart( int nIns, int nNodes, int nOuts )
{
    Aig_CMan_t * p;
Alan Mishchenko committed
364
    p = (Aig_CMan_t *)ABC_ALLOC( char, sizeof(Aig_CMan_t) + 2*(2*nNodes + nOuts) );
Alan Mishchenko committed
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
    memset( p, 0, sizeof(Aig_CMan_t) );
    // set parameters
    p->nIns = nIns;
    p->nOuts = nOuts;
    p->nNodes = nNodes;
    p->nBytes = 2*(2*nNodes + nOuts);
    // prepare the manager
    p->iNode = 1 + p->nIns;
    p->iPrev = -1;
    p->pCur = p->Data;
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_CManStop( Aig_CMan_t * p )
{
Alan Mishchenko committed
391
    ABC_FREE( p );
Alan Mishchenko committed
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
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_CManRestart( Aig_CMan_t * p )
{
    assert( p->iNode == 1 + p->nIns + p->nNodes + p->nOuts );
    p->iNode = 1 + p->nIns;
    p->iPrev = -1;
    p->pCur = p->Data;
}


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

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_CManStoreNum( Aig_CMan_t * p, unsigned x )
{
    while ( x & ~0x7f )
    {
        *p->pCur++ = (x & 0x7f) | 0x80;
        x >>= 7;
    }
    *p->pCur++ = x;
    assert( p->pCur - p->Data < p->nBytes - 10 );
}

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

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Aig_CManRestoreNum( Aig_CMan_t * p )
{
    int ch, i, x = 0;
    for ( i = 0; (ch = *p->pCur++) & 0x80; i++ )
        x |= (ch & 0x7f) << (7 * i);
    return x | (ch << (7 * i));
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_CManAddNode( Aig_CMan_t * p, int iFan0, int iFan1 )
{
    assert( iFan0 < iFan1 );
    assert( iFan1 < (p->iNode << 1) );
    Aig_CManStoreNum( p, (p->iNode++ << 1) - iFan1 );
    Aig_CManStoreNum( p, iFan1 - iFan0 );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_CManAddPo( Aig_CMan_t * p, int iFan0 )
{
    if ( p->iPrev == -1 )
        Aig_CManStoreNum( p, p->iNode - iFan0 );
    else if ( p->iPrev <= iFan0 )
        Aig_CManStoreNum( p, (iFan0 - p->iPrev) << 1 );
    else 
        Aig_CManStoreNum( p,((p->iPrev - iFan0) << 1) | 1 );
    p->iPrev = iFan0;
    p->iNode++;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_CManGetNode( Aig_CMan_t * p, int * piFan0, int * piFan1 )
{
    *piFan1 = (p->iNode++ << 1) - Aig_CManRestoreNum( p );
    *piFan0 = *piFan1 - Aig_CManRestoreNum( p );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Aig_CManGetPo( Aig_CMan_t * p )
{
    int Num = Aig_CManRestoreNum( p );
    if ( p->iPrev == -1 )
        p->iPrev = p->iNode;
    p->iNode++;
    if ( Num & 1 )
        return p->iPrev = p->iPrev + (Num >> 1);
    return p->iPrev = p->iPrev - (Num >> 1);
}

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

  Synopsis    [Compute switching probabilities of all nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_CMan_t * Aig_CManCreate( Aig_Man_t * p )
{
    Aig_CMan_t * pCMan;
    Aig_Obj_t * pObj;
    int i;
552
    pCMan = Aig_CManStart( Aig_ManCiNum(p), Aig_ManNodeNum(p), Aig_ManCoNum(p) );
Alan Mishchenko committed
553 554 555 556
    Aig_ManForEachNode( p, pObj, i )
        Aig_CManAddNode( pCMan, 
            (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj), 
            (Aig_ObjFaninId1(pObj) << 1) | Aig_ObjFaninC1(pObj) );
557
    Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
558 559 560
        Aig_CManAddPo( pCMan, 
            (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj) ); 
    printf( "\nBytes alloc = %5d.  Bytes used = %7d.  Ave per node = %4.2f. \n", 
561
        pCMan->nBytes, (int)(pCMan->pCur - pCMan->Data), 
Alan Mishchenko committed
562 563 564 565 566 567 568 569 570 571
        1.0 * (pCMan->pCur - pCMan->Data) / (pCMan->nNodes + pCMan->nOuts ) );
//    Aig_CManStop( pCMan );
    return pCMan;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


572 573
ABC_NAMESPACE_IMPL_END