saigSimFast.c 15.6 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    [saigSimFast.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Sequential AIG package.]

  Synopsis    [Fast sequential AIG simulator.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: saigSimFast.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 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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

// the AIG manager
typedef struct Faig_Man_t_ Faig_Man_t;
struct Faig_Man_t_
{
    // parameters
    int            nPis;
    int            nPos;
    int            nCis;
    int            nCos;
    int            nFfs;
    int            nNos;
    // offsets
    int            nPis1;
    int            nCis1;
    int            nCisNos1;
    int            nCisNosPos1;
    int            nObjs;
    // allocated data
    int            nWords;
    int            pObjs[0];
};

static inline int  Faig_CheckIdPi( Faig_Man_t * p, int i )    { return i >= 1 && i < p->nPis1;                     }
static inline int  Faig_CheckIdLo( Faig_Man_t * p, int i )    { return i >= p->nPis1 && i < p->nCis1;              }
static inline int  Faig_CheckIdNo( Faig_Man_t * p, int i )    { return i >= p->nCis1 && i < p->nCisNos1;           }
static inline int  Faig_CheckIdPo( Faig_Man_t * p, int i )    { return i >= p->nCisNos1 && i < p->nCisNosPos1;     }
static inline int  Faig_CheckIdLi( Faig_Man_t * p, int i )    { return i >= p->nCisNosPos1 && i < p->nObjs;        }
static inline int  Faig_CheckIdCo( Faig_Man_t * p, int i )    { return i >= p->nCisNos1 && i < p->nObjs;           }
static inline int  Faig_CheckIdObj( Faig_Man_t * p, int i )   { return i >= 0 && i < p->nObjs;                     }

static inline int  Faig_ObjIdToNumPi( Faig_Man_t * p, int i ) { assert( Faig_CheckIdPi(p,i) ); return i - 1;              }
static inline int  Faig_ObjIdToNumLo( Faig_Man_t * p, int i ) { assert( Faig_CheckIdLo(p,i) ); return i - p->nPis1;       }
static inline int  Faig_ObjIdToNumNo( Faig_Man_t * p, int i ) { assert( Faig_CheckIdNo(p,i) ); return i - p->nCis1;       }
static inline int  Faig_ObjIdToNumPo( Faig_Man_t * p, int i ) { assert( Faig_CheckIdPo(p,i) ); return i - p->nCisNos1;    }
static inline int  Faig_ObjIdToNumLi( Faig_Man_t * p, int i ) { assert( Faig_CheckIdLi(p,i) ); return i - p->nCisNosPos1; }
static inline int  Faig_ObjIdToNumCo( Faig_Man_t * p, int i ) { assert( Faig_CheckIdCo(p,i) ); return i - p->nCisNos1;    }

static inline int  Faig_ObjLoToLi( Faig_Man_t * p, int i )    { assert( Faig_CheckIdLo(p,i) ); return p->nObjs - (p->nCis1 - i); }
static inline int  Faig_ObjLiToLo( Faig_Man_t * p, int i )    { assert( Faig_CheckIdLi(p,i) ); return p->nCis1 - (p->nObjs - i); }

static inline int  Faig_NodeChild0( Faig_Man_t * p, int n )   { return p->pObjs[n<<1];             }
static inline int  Faig_NodeChild1( Faig_Man_t * p, int n )   { return p->pObjs[(n<<1)+1];         }
static inline int  Faig_CoChild0( Faig_Man_t * p, int n )     { return p->pObjs[(p->nNos<<1)+n];   }
static inline int  Faig_ObjFaninC( int iFan )                 { return iFan & 1;                   }
static inline int  Faig_ObjFanin( int iFan )                  { return iFan >> 1;                  }

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

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

  Synopsis    [Checks if the manager is correct.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Faig_ManIsCorrect( Aig_Man_t * pAig )
{
    return Aig_ManObjNumMax(pAig) == 
96
        1 + Aig_ManCiNum(pAig) + Aig_ManNodeNum(pAig) + Aig_ManCoNum(pAig);
Alan Mishchenko committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

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

  Synopsis    [Creates fast simulation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Faig_Man_t * Faig_ManAlloc( Aig_Man_t * pAig )
{
    Faig_Man_t * p;
    int nWords;
//    assert( Faig_ManIsCorrect(pAig) );
115
    nWords = 2 * Aig_ManNodeNum(pAig) + Aig_ManCoNum(pAig);
Alan Mishchenko committed
116
    p = (Faig_Man_t *)ABC_ALLOC( char, sizeof(Faig_Man_t) + sizeof(int) * nWords );
117
//printf( "Allocating %7.2f MB.\n", 1.0 * (sizeof(Faig_Man_t) + sizeof(int) * nWords)/(1<<20) );
Alan Mishchenko committed
118
    memset( p, 0, sizeof(Faig_Man_t) );
119 120 121 122
    p->nPis   = Aig_ManCiNum(pAig) - Aig_ManRegNum(pAig);
    p->nPos   = Aig_ManCoNum(pAig) - Aig_ManRegNum(pAig);
    p->nCis   = Aig_ManCiNum(pAig);
    p->nCos   = Aig_ManCoNum(pAig);
Alan Mishchenko committed
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
    p->nFfs   = Aig_ManRegNum(pAig);
    p->nNos   = Aig_ManNodeNum(pAig);
    // offsets
    p->nPis1  = p->nPis + 1;
    p->nCis1  = p->nCis + 1;
    p->nCisNos1 = p->nCis + p->nNos + 1;
    p->nCisNosPos1 = p->nCis + p->nNos + p->nPos + 1;
    p->nObjs  = p->nCis + p->nNos + p->nCos + 1;
    p->nWords = nWords;
    return p;
}

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

  Synopsis    [Creates fast simulation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Faig_Man_t * Faig_ManCreate( Aig_Man_t * pAig )
{
    Faig_Man_t * p;
    Aig_Obj_t * pObj;
    int i, iWord = 0;
    p = Faig_ManAlloc( pAig );
    Aig_ManForEachNode( pAig, pObj, i )
    {
        p->pObjs[iWord++] = (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj);
        p->pObjs[iWord++] = (Aig_ObjFaninId1(pObj) << 1) | Aig_ObjFaninC1(pObj);
    }
157
    Aig_ManForEachCo( pAig, pObj, i )
Alan Mishchenko committed
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
        p->pObjs[iWord++] = (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj);
    assert( iWord == p->nWords );
    return p;
}

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

  Synopsis    [Simulates one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Faig_SimulateNode( Faig_Man_t * p, int Id, unsigned * pSimInfo )
{
    int n = Faig_ObjIdToNumNo( p, Id );
    int iFan0 = Faig_NodeChild0( p, n );
    int iFan1 = Faig_NodeChild1( p, n );
    if ( Faig_ObjFaninC(iFan0) && Faig_ObjFaninC(iFan1) )
        return ~(pSimInfo[Faig_ObjFanin(iFan0)] | pSimInfo[Faig_ObjFanin(iFan1)]);
    if ( Faig_ObjFaninC(iFan0) && !Faig_ObjFaninC(iFan1) )
        return (~pSimInfo[Faig_ObjFanin(iFan0)] & pSimInfo[Faig_ObjFanin(iFan1)]);
    if ( !Faig_ObjFaninC(iFan0) && Faig_ObjFaninC(iFan1) )
        return (pSimInfo[Faig_ObjFanin(iFan0)] & ~pSimInfo[Faig_ObjFanin(iFan1)]);
    // if ( !Faig_ObjFaninC(iFan0) && !Faig_ObjFaninC(iFan1) )
    return (pSimInfo[Faig_ObjFanin(iFan0)] & pSimInfo[Faig_ObjFanin(iFan1)]);
} 

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

  Synopsis    [Simulates one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Faig_SimulateCo( Faig_Man_t * p, int Id, unsigned * pSimInfo )
{
    int n = Faig_ObjIdToNumCo( p, Id );
    int iFan0 = Faig_CoChild0( p, n );
    if ( Faig_ObjFaninC(iFan0) )
        return ~pSimInfo[Faig_ObjFanin(iFan0)];
    // if ( !Faig_ObjFaninC(iFan0) )
    return pSimInfo[Faig_ObjFanin(iFan0)];
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Faig_SimulateRandomShift( unsigned uOld )
{
    return (uOld << 16) | ((uOld ^ Aig_ManRandom(0)) & 0xffff);
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline unsigned Faig_SimulateTransferShift( unsigned uOld, unsigned uNew )
{
    return (uOld << 16) | (uNew & 0xffff);
}

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

  Synopsis    [Simulates the timeframes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Faig_ManSimulateFrames( Faig_Man_t * p, int nFrames, int nPref, int fTrans )
{
255
    int * pNumOnes = ABC_CALLOC( int, p->nObjs );
Alan Mishchenko committed
256
    unsigned * pSimInfo = ABC_ALLOC( unsigned, p->nObjs );
Alan Mishchenko committed
257
    int f, i;
258 259
//printf( "Allocating %7.2f MB.\n", 1.0 * 4 * p->nObjs/(1<<20) );
//printf( "Allocating %7.2f MB.\n", 1.0 * 4 * p->nObjs/(1<<20) );
Alan Mishchenko committed
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
    // set constant 1
    pSimInfo[0] = ~0;
    for ( f = 0; f < nFrames; f++ )
    {
        if ( fTrans )
        {
            for ( i = 1; i < p->nPis1; i++ )
                pSimInfo[i] = f? Faig_SimulateRandomShift( pSimInfo[i] ) : Aig_ManRandom( 0 );
            for (      ; i < p->nCis1; i++ )
                pSimInfo[i] = f? Faig_SimulateTransferShift( pSimInfo[i], pSimInfo[Faig_ObjLoToLi(p,i)] ) : 0;
        }
        else
        {
            for ( i = 1; i < p->nPis1; i++ )
                pSimInfo[i] = Aig_ManRandom( 0 );
            for (      ; i < p->nCis1; i++ )
                pSimInfo[i] = f? pSimInfo[Faig_ObjLoToLi(p,i)] : 0;
        }
        for (      ; i < p->nCisNos1; i++ )
            pSimInfo[i] = Faig_SimulateNode( p, i, pSimInfo );
        for (      ; i < p->nObjs; i++ )
            pSimInfo[i] = Faig_SimulateCo( p, i, pSimInfo );
        if ( f < nPref )
            continue;
        if ( fTrans )
        {
            for ( i = 0; i < p->nObjs; i++ )
                pNumOnes[i] += Aig_WordCountOnes( (pSimInfo[i] ^ (pSimInfo[i] >> 16)) & 0xffff );
        }
        else
        {
            for ( i = 0; i < p->nObjs; i++ )
                pNumOnes[i] += Aig_WordCountOnes( pSimInfo[i] );
        }
    }
Alan Mishchenko committed
295
    ABC_FREE( pSimInfo );
Alan Mishchenko committed
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
    return pNumOnes;
}

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

  Synopsis    [Computes switching activity of one node.]

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

  SeeAlso     []

***********************************************************************/
float Faig_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 Faig_ManComputeProbOne( int nOnes, int nSimWords )
{
    int nTotal = 32 * nSimWords;
    return (float)nOnes / nTotal;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
344
Vec_Int_t * Faig_ManComputeSwitchProbs4( Aig_Man_t * p, int nFrames, int nPref, int fProbOne )
Alan Mishchenko committed
345 346 347 348 349 350
{
    int fTrans = 1;
    Faig_Man_t * pAig;
    Vec_Int_t * vSwitching;
    int * pProbs;
    float * pSwitching;
351
    int nFramesReal;
352
    abctime clk;//, clkTotal = Abc_Clock();
Alan Mishchenko committed
353 354 355 356
    if ( fProbOne )
        fTrans = 0;
    vSwitching = Vec_IntStart( Aig_ManObjNumMax(p) );
    pSwitching = (float *)vSwitching->pArray;
357
clk = Abc_Clock();
Alan Mishchenko committed
358
    pAig = Faig_ManCreate( p );
359
//ABC_PRT( "\nCreation  ", Abc_Clock() - clk );
Alan Mishchenko committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373
    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 );
374
clk = Abc_Clock();
Alan Mishchenko committed
375
    pProbs = Faig_ManSimulateFrames( pAig, nFramesReal, nPref, fTrans );
376 377
//ABC_PRT( "Simulation", Abc_Clock() - clk );
clk = Abc_Clock();
Alan Mishchenko committed
378 379 380 381 382 383
    if ( fTrans )
    {
        Aig_Obj_t * pObj;
        int i, Counter = 0;
        pObj = Aig_ManConst1(p);
        pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], (nFramesReal - nPref)/2 );
384
        Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
385 386 387
            pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], (nFramesReal - nPref)/2 );
        Aig_ManForEachNode( p, pObj, i )
            pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], (nFramesReal - nPref)/2 );
388
        Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
389 390 391 392 393 394 395 396 397
            pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], (nFramesReal - nPref)/2 );
        assert( Counter == pAig->nObjs );
    }
    else if ( fProbOne )
    {
        Aig_Obj_t * pObj;
        int i, Counter = 0;
        pObj = Aig_ManConst1(p);
        pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], nFramesReal - nPref );
398
        Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
399 400 401
            pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], nFramesReal - nPref );
        Aig_ManForEachNode( p, pObj, i )
            pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], nFramesReal - nPref );
402
        Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
403 404 405 406 407 408 409 410 411
            pSwitching[pObj->Id] = Faig_ManComputeProbOne( pProbs[Counter++], nFramesReal - nPref );
        assert( Counter == pAig->nObjs );
    }
    else 
    {
        Aig_Obj_t * pObj;
        int i, Counter = 0;
        pObj = Aig_ManConst1(p);
        pSwitching[pObj->Id] = Faig_ManComputeSwitching( pProbs[Counter++], nFramesReal - nPref );
412
        Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
413 414 415
            pSwitching[pObj->Id] = Faig_ManComputeSwitching( pProbs[Counter++], nFramesReal - nPref );
        Aig_ManForEachNode( p, pObj, i )
            pSwitching[pObj->Id] = Faig_ManComputeSwitching( pProbs[Counter++], nFramesReal - nPref );
416
        Aig_ManForEachCo( p, pObj, i )
Alan Mishchenko committed
417 418 419
            pSwitching[pObj->Id] = Faig_ManComputeSwitching( pProbs[Counter++], nFramesReal - nPref );
        assert( Counter == pAig->nObjs );
    }
Alan Mishchenko committed
420 421
    ABC_FREE( pProbs );
    ABC_FREE( pAig );
422 423
//ABC_PRT( "Switch    ", Abc_Clock() - clk );
//ABC_PRT( "TOTAL     ", Abc_Clock() - clkTotal );
Alan Mishchenko committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437
    return vSwitching;
}

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

  Synopsis    [Computes probability of switching (or of being 1).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
438
Vec_Int_t * Saig_ManComputeSwitchProb3s( Aig_Man_t * p, int nFrames, int nPref, int fProbOne )
Alan Mishchenko committed
439
{
Alan Mishchenko committed
440 441
//    return Faig_ManComputeSwitchProbs( p, nFrames, nPref, fProbOne );
    return NULL;
Alan Mishchenko committed
442 443 444 445 446 447 448 449
}


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


450 451
ABC_NAMESPACE_IMPL_END