fsimSim.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 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 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 356 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
/**CFile****************************************************************

  FileName    [fsimSim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Fast sequential AIG simulator.]

  Synopsis    [Simulation procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "fsimInt.h"
#include "ssw.h"

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimInfoRandom( Fsim_Man_t * p, unsigned * pInfo )
{
    int w;
    for ( w = p->nWords-1; w >= 0; w-- )
        pInfo[w] = Aig_ManRandom( 0 );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimInfoZero( Fsim_Man_t * p, unsigned * pInfo )
{
    int w;
    for ( w = p->nWords-1; w >= 0; w-- )
        pInfo[w] = 0;
}

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

  Synopsis    [Returns index of the first pattern that failed.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Fsim_ManSimInfoIsZero( Fsim_Man_t * p, unsigned * pInfo )
{
    int w;
    for ( w = p->nWords-1; w >= 0; w-- )
        if ( pInfo[w] )
            return 32*(w-1) + Aig_WordFindFirstBit( pInfo[w] );
    return -1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimInfoOne( Fsim_Man_t * p, unsigned * pInfo )
{
    int w;
    for ( w = p->nWords-1; w >= 0; w-- )
        pInfo[w] = ~0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimInfoCopy( Fsim_Man_t * p, unsigned * pInfo, unsigned * pInfo0 )
{
    int w;
    for ( w = p->nWords-1; w >= 0; w-- )
        pInfo[w] = pInfo0[w];
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimulateCi( Fsim_Man_t * p, int iNode, int iCi )
{
    unsigned * pInfo  = Fsim_SimData( p, iNode % p->nFront );
    unsigned * pInfo0 = Fsim_SimDataCi( p, iCi );
    int w;
    for ( w = p->nWords-1; w >= 0; w-- )
        pInfo[w] = pInfo0[w];
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimulateCo( Fsim_Man_t * p, int iCo, int iFan0 )
{
    unsigned * pInfo  = Fsim_SimDataCo( p, iCo );
    unsigned * pInfo0 = Fsim_SimData( p, Fsim_Lit2Var(iFan0) % p->nFront );
    int w;
    if ( Fsim_LitIsCompl(iFan0) )
        for ( w = p->nWords-1; w >= 0; w-- )
            pInfo[w] = ~pInfo0[w];
    else //if ( !Fsim_LitIsCompl(iFan0) )
        for ( w = p->nWords-1; w >= 0; w-- )
            pInfo[w] = pInfo0[w];
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimulateNode( Fsim_Man_t * p, int iNode, int iFan0, int iFan1 )
{
    unsigned * pInfo  = Fsim_SimData( p, iNode % p->nFront );
    unsigned * pInfo0 = Fsim_SimData( p, Fsim_Lit2Var(iFan0) % p->nFront );
    unsigned * pInfo1 = Fsim_SimData( p, Fsim_Lit2Var(iFan1) % p->nFront );
    int w;
    if ( Fsim_LitIsCompl(iFan0) && Fsim_LitIsCompl(iFan1) )
        for ( w = p->nWords-1; w >= 0; w-- )
            pInfo[w] = ~(pInfo0[w] | pInfo1[w]);
    else if ( Fsim_LitIsCompl(iFan0) && !Fsim_LitIsCompl(iFan1) )
        for ( w = p->nWords-1; w >= 0; w-- )
            pInfo[w] = ~pInfo0[w] & pInfo1[w];
    else if ( !Fsim_LitIsCompl(iFan0) && Fsim_LitIsCompl(iFan1) )
        for ( w = p->nWords-1; w >= 0; w-- )
            pInfo[w] = pInfo0[w] & ~pInfo1[w];
    else //if ( !Fsim_LitIsCompl(iFan0) && !Fsim_LitIsCompl(iFan1) )
        for ( w = p->nWords-1; w >= 0; w-- )
            pInfo[w] = pInfo0[w] & pInfo1[w];
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimInfoInit( Fsim_Man_t * p )
{
    int iPioNum, i;
    Vec_IntForEachEntry( p->vCis2Ids, iPioNum, i )
    {
        if ( iPioNum < p->nPis )
            Fsim_ManSimInfoRandom( p, Fsim_SimDataCi(p, i) );
        else
            Fsim_ManSimInfoZero( p, Fsim_SimDataCi(p, i) );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimInfoTransfer( Fsim_Man_t * p )
{
    int iPioNum, i;
    Vec_IntForEachEntry( p->vCis2Ids, iPioNum, i )
    {
        if ( iPioNum < p->nPis )
            Fsim_ManSimInfoRandom( p, Fsim_SimDataCi(p, i) );
        else
            Fsim_ManSimInfoCopy( p, Fsim_SimDataCi(p, i), Fsim_SimDataCo(p, p->nPos+iPioNum-p->nPis) );
    }
}

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

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Fsim_ManRestoreNum( Fsim_Man_t * p )
{
    int ch, i, x = 0;
    for ( i = 0; (ch = *p->pDataCur++) & 0x80; i++ )
        x |= (ch & 0x7f) << (7 * i);
    assert( p->pDataCur - p->pDataAig < p->nDataAig );
    return x | (ch << (7 * i));
}

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

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Fsim_ManRestoreObj( Fsim_Man_t * p, Fsim_Obj_t * pObj )
{
    int iValue = Fsim_ManRestoreNum( p );
    if ( (iValue & 3) == 3 ) // and
    {
        pObj->iNode = (iValue >> 2) + p->iNodePrev;
        pObj->iFan0 = (pObj->iNode << 1) - Fsim_ManRestoreNum( p );
        pObj->iFan1 = pObj->iFan0 - Fsim_ManRestoreNum( p );
        p->iNodePrev = pObj->iNode;
    }
    else if ( (iValue & 3) == 1 ) // ci
    {
        pObj->iNode = (iValue >> 2) + p->iNodePrev;
        pObj->iFan0 = 0;
        pObj->iFan1 = 0;
        p->iNodePrev = pObj->iNode;
    }
    else // if ( (iValue & 1) == 0 ) // co
    {
        pObj->iNode = 0;
        pObj->iFan0 = ((p->iNodePrev << 1) | 1) - (iValue >> 1);
        pObj->iFan1 = 0;
    }
    return 1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimulateRound2( Fsim_Man_t * p )
{
    Fsim_Obj_t * pObj;
    int i, iCis = 0, iCos = 0;
    if ( Aig_ObjRefs(Aig_ManConst1(p->pAig)) )
        Fsim_ManSimInfoOne( p, Fsim_SimData(p, 1) );
    Fsim_ManForEachObj( p, pObj, i )
    {
        if ( pObj->iFan0 == 0 )
            Fsim_ManSimulateCi( p, pObj->iNode, iCis++ );
        else if ( pObj->iFan1 == 0 )
            Fsim_ManSimulateCo( p, iCos++, pObj->iFan0 );
        else
            Fsim_ManSimulateNode( p, pObj->iNode, pObj->iFan0, pObj->iFan1 );
    }
    assert( iCis == p->nCis );
    assert( iCos == p->nCos );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Fsim_ManSimulateRound( Fsim_Man_t * p )
{
    int * pCur, * pEnd;
    int iCis = 0, iCos = 0;
    if ( p->pDataAig2 == NULL )
    {
        Fsim_ManSimulateRound2( p );
        return;
    }
    if ( Aig_ObjRefs(Aig_ManConst1(p->pAig)) )
        Fsim_ManSimInfoOne( p, Fsim_SimData(p, 1) );
    pCur = p->pDataAig2 + 6;
    pEnd = p->pDataAig2 + 3 * p->nObjs;
    while ( pCur < pEnd )
    {
        if ( pCur[1] == 0 )
            Fsim_ManSimulateCi( p, pCur[0], iCis++ );
        else if ( pCur[2] == 0 )
            Fsim_ManSimulateCo( p, iCos++, pCur[1] );
        else
            Fsim_ManSimulateNode( p, pCur[0], pCur[1], pCur[2] );
        pCur += 3;
    }
    assert( iCis == p->nCis );
    assert( iCos == p->nCos );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Fsim_ManSimulateRoundTest( Fsim_Man_t * p )
{
    Fsim_Obj_t * pObj;
    int i, clk = clock();
    Fsim_ManForEachObj( p, pObj, i )
    {
    }
Alan Mishchenko committed
388
//    ABC_PRT( "Unpacking time", p->pPars->nIters * (clock() - clk) );
Alan Mishchenko committed
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
}

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

  Synopsis    [Returns index of the PO and pattern that failed it.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Fsim_ManCheckPos( Fsim_Man_t * p, int * piPo, int * piPat )
{
    int i, iPat;
    for ( i = 0; i < p->nPos; i++ )
    {
        iPat = Fsim_ManSimInfoIsZero( p, Fsim_SimDataCo(p, i) );
        if ( iPat >= 0 )
        {
            *piPo = i;
            *piPat = iPat;
            return 1;
        }
    }
    return 0;
}

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

  Synopsis    [Returns the counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ssw_Cex_t * Fsim_ManGenerateCounter( Aig_Man_t * pAig, int iFrame, int iOut, int nWords, int iPat, Vec_Int_t * vCis2Ids )
{
    Ssw_Cex_t * p;
    unsigned * pData;
    int f, i, w, iPioId, Counter;
    p = Ssw_SmlAllocCounterExample( Aig_ManRegNum(pAig), Saig_ManPiNum(pAig), iFrame+1 );
    p->iFrame = iFrame;
    p->iPo = iOut;
    // fill in the binary data
    Aig_ManRandom( 1 );
    Counter = p->nRegs;
Alan Mishchenko committed
440
    pData = ABC_ALLOC( unsigned, nWords );
Alan Mishchenko committed
441 442 443 444 445 446 447 448 449 450 451
    for ( f = 0; f <= iFrame; f++, Counter += p->nPis )
    for ( i = 0; i < Aig_ManPiNum(pAig); i++ )
    {
        iPioId = Vec_IntEntry( vCis2Ids, i );
        if ( iPioId >= p->nPis )
            continue;
        for ( w = nWords-1; w >= 0; w-- )
            pData[w] = Aig_ManRandom( 0 );
        if ( Aig_InfoHasBit( pData, iPat ) )
            Aig_InfoSetBit( p->pData, Counter + iPioId );
    }
Alan Mishchenko committed
452
    ABC_FREE( pData );
Alan Mishchenko committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Fsim_ManSimulate( Aig_Man_t * pAig, Fsim_ParSim_t * pPars )
{
    Fsim_Man_t * p;
    Sec_MtrStatus_t Status;
    int i, iOut, iPat, clk, clkTotal = clock(), clk2, clk2Total = 0;
    assert( Aig_ManRegNum(pAig) > 0 );
Alan Mishchenko committed
473
    if ( pPars->fCheckMiter )
Alan Mishchenko committed
474
    {
Alan Mishchenko committed
475 476 477 478 479 480 481 482 483 484 485
        Status = Sec_MiterStatus( pAig );
        if ( Status.nSat > 0 )
        {
            printf( "Miter is trivially satisfiable (output %d).\n", Status.iOut );
            return 1;
        }
        if ( Status.nUndec == 0 )
        {
            printf( "Miter is trivially unsatisfiable.\n" );
            return 0;
        }
Alan Mishchenko committed
486 487 488 489 490 491 492 493 494 495
    }
    // create manager
    clk = clock();
    p = Fsim_ManCreate( pAig );
    p->nWords = pPars->nWords;
    if ( pPars->fVerbose )
    {
        printf( "Obj = %8d (%8d). Cut = %6d. Front = %6d.  FrtMem = %7.2f Mb. ", 
            p->nObjs, p->nCis + p->nNodes, p->nCrossCutMax, p->nFront, 
            4.0*p->nWords*(p->nFront)/(1<<20) );
Alan Mishchenko committed
496
        ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
497 498 499 500 501 502 503 504 505 506
    }
    // create simulation frontier
    clk = clock();
    Fsim_ManFront( p, pPars->fCompressAig );
    if ( pPars->fVerbose )
    {
        printf( "Max ID = %8d. Log max ID = %2d.  AigMem = %7.2f Mb (%5.2f byte/obj).  ", 
            p->iNumber, Aig_Base2Log(p->iNumber), 
            1.0*(p->pDataCur-p->pDataAig)/(1<<20), 
            1.0*(p->pDataCur-p->pDataAig)/p->nObjs ); 
Alan Mishchenko committed
507
        ABC_PRT( "Time", clock() - clk );
Alan Mishchenko committed
508 509 510 511
    }
    // perform simulation
    Aig_ManRandom( 1 );
    assert( p->pDataSim == NULL );
Alan Mishchenko committed
512 513 514
    p->pDataSim = ABC_ALLOC( unsigned, p->nWords * p->nFront );
    p->pDataSimCis = ABC_ALLOC( unsigned, p->nWords * p->nCis );
    p->pDataSimCos = ABC_ALLOC( unsigned, p->nWords * p->nCos );
Alan Mishchenko committed
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
    Fsim_ManSimInfoInit( p );
    for ( i = 0; i < pPars->nIters; i++ )
    {
        Fsim_ManSimulateRound( p );
        if ( pPars->fVerbose )
        {
            printf( "Frame %4d out of %4d and timeout %3d sec. ", i+1, pPars->nIters, pPars->TimeLimit );
            printf( "Time = %7.2f sec\r", (1.0*clock()-clkTotal)/CLOCKS_PER_SEC );
        }
        if ( pPars->fCheckMiter && Fsim_ManCheckPos( p, &iOut, &iPat ) )
        {
            assert( pAig->pSeqModel == NULL );
            pAig->pSeqModel = Fsim_ManGenerateCounter( pAig, i, iOut, p->nWords, iPat, p->vCis2Ids );
            if ( pPars->fVerbose )
            printf( "Miter is satisfiable after simulation (output %d).\n", iOut );
            break;
        }
        if ( (clock() - clkTotal)/CLOCKS_PER_SEC >= pPars->TimeLimit )
        {
            printf( "No bug detected after %d frames with time limit %d seconds.\n", i+1, pPars->TimeLimit );
            break;
        }
        clk2 = clock();
        if ( i < pPars->nIters - 1 )
            Fsim_ManSimInfoTransfer( p );
        clk2Total += clock() - clk2;
    }
    if ( pPars->fVerbose )
    {
        printf( "Maxcut = %8d.  AigMem = %7.2f Mb.  SimMem = %7.2f Mb.  ", 
            p->nCrossCutMax, 
            p->pDataAig2? 12.0*p->nObjs/(1<<20) : 1.0*(p->pDataCur-p->pDataAig)/(1<<20), 
            4.0*p->nWords*(p->nFront+p->nCis+p->nCos)/(1<<20) );
Alan Mishchenko committed
548
        ABC_PRT( "Sim time", clock() - clkTotal );
Alan Mishchenko committed
549

Alan Mishchenko committed
550
//        ABC_PRT( "Additional time", clk2Total );
Alan Mishchenko committed
551 552 553 554 555 556 557 558 559 560 561 562 563
//        Fsim_ManSimulateRoundTest( p );
//        Fsim_ManSimulateRoundTest2( p );
    }
    Fsim_ManDelete( p );
    return pAig->pSeqModel != NULL;

}

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