intCore.c 15.9 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
/**CFile****************************************************************

  FileName    [intCore.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Interpolation engine.]

  Synopsis    [Core procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 24, 2008.]

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

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

#include "intInt.h"
22
#include "sat/bmc/bmc.h"
Alan Mishchenko committed
23

24 25 26
ABC_NAMESPACE_IMPL_START


27
////////////////////////////////////////////////////////////////////////`
Alan Mishchenko committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [This procedure sets default values of interpolation parameters.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inter_ManSetDefaultParams( Inter_ManParams_t * p )
Alan Mishchenko committed
47
{ 
Alan Mishchenko committed
48
    memset( p, 0, sizeof(Inter_ManParams_t) );
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    p->nBTLimit      = 0;     // limit on the number of conflicts
    p->nFramesMax    = 0;     // the max number timeframes to unroll
    p->nSecLimit     = 0;     // time limit in seconds
    p->nFramesK      = 1;     // the number of timeframes to use in induction
    p->fRewrite      = 0;     // use additional rewriting to simplify timeframes
    p->fTransLoop    = 0;     // add transition into the init state under new PI var
    p->fUsePudlak    = 0;     // use Pudluk interpolation procedure
    p->fUseOther     = 0;     // use other undisclosed option
    p->fUseMiniSat   = 0;     // use MiniSat-1.14p instead of internal proof engine
    p->fCheckKstep   = 1;     // check using K-step induction
    p->fUseBias      = 0;     // bias decisions to global variables
    p->fUseBackward  = 0;     // perform backward interpolation
    p->fUseSeparate  = 0;     // solve each output separately
    p->fUseTwoFrames = 0;     // create OR of two last timeframes
    p->fDropSatOuts  = 0;     // replace by 1 the solved outputs
    p->fVerbose      = 0;     // print verbose statistics
    p->iFrameMax     =-1;
Alan Mishchenko committed
66 67 68 69 70 71 72 73 74 75 76 77 78
}

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

  Synopsis    [Interplates while the number of conflicts is not exceeded.]

  Description [Returns 1 if proven. 0 if failed. -1 if undecided.]
               
  SideEffects [Does not check the property in 0-th frame.]

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
79
int Inter_ManPerformInterpolation( Aig_Man_t * pAig, Inter_ManParams_t * pPars, int * piFrame )
Alan Mishchenko committed
80 81 82
{
    extern int Inter_ManCheckInductiveContainment( Aig_Man_t * pTrans, Aig_Man_t * pInter, int nSteps, int fBackward );
    Inter_Man_t * p;
83
    Inter_Check_t * pCheck = NULL;
Alan Mishchenko committed
84
    Aig_Man_t * pAigTemp;
85
    int s, i, RetValue, Status;
86 87
    abctime clk, clk2, clkTotal = Abc_Clock(), timeTemp = 0;
    abctime nTimeNewOut = pPars->nSecLimit ? pPars->nSecLimit * CLOCKS_PER_SEC + Abc_Clock() : 0;
88

89 90 91 92
    // enable ORing of the interpolants, if containment check is performed inductively with K > 1
    if ( pPars->nFramesK > 1 )
        pPars->fTransLoop = 1;

Alan Mishchenko committed
93 94 95
    // sanity checks
    assert( Saig_ManRegNum(pAig) > 0 );
    assert( Saig_ManPiNum(pAig) > 0 );
96 97 98
    assert( Saig_ManPoNum(pAig)-Saig_ManConstrNum(pAig) == 1 );
    if ( pPars->fVerbose && Saig_ManConstrNum(pAig) )
        printf( "Performing interpolation with %d constraints...\n", Saig_ManConstrNum(pAig) );
99

Alan Mishchenko committed
100 101
    if ( Inter_ManCheckInitialState(pAig) )
    {
102
        *piFrame = -1;
Alan Mishchenko committed
103 104 105
        printf( "Property trivially fails in the initial state.\n" );
        return 0;
    }
106
/*
Alan Mishchenko committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
    if ( Inter_ManCheckAllStates(pAig) )
    {
        printf( "Property trivially holds in all states.\n" );
        return 1;
    }
*/
    // create interpolation manager
    // can perform SAT sweeping and/or rewriting of this AIG...
    p = Inter_ManCreate( pAig, pPars );
    if ( pPars->fTransLoop )
        p->pAigTrans = Inter_ManStartOneOutput( pAig, 0 );
    else
        p->pAigTrans = Inter_ManStartDuplicated( pAig );
    // derive CNF for the transformed AIG
121
clk = Abc_Clock();
Alan Mishchenko committed
122
    p->pCnfAig = Cnf_Derive( p->pAigTrans, Aig_ManRegNum(p->pAigTrans) ); 
123
p->timeCnf += Abc_Clock() - clk;    
Alan Mishchenko committed
124 125 126 127 128 129 130
    if ( pPars->fVerbose )
    { 
        printf( "AIG: PI/PO/Reg = %d/%d/%d. And = %d. Lev = %d.  CNF: Var/Cla = %d/%d.\n",
            Saig_ManPiNum(pAig), Saig_ManPoNum(pAig), Saig_ManRegNum(pAig), 
            Aig_ManAndNum(pAig), Aig_ManLevelNum(pAig),
            p->pCnfAig->nVars, p->pCnfAig->nClauses );
    }
131
 
Alan Mishchenko committed
132
    // derive interpolant
Alan Mishchenko committed
133
    *piFrame = -1;
Alan Mishchenko committed
134 135 136
    p->nFrames = 1;
    for ( s = 0; ; s++ )
    {
137 138
        Cnf_Dat_t * pCnfInter2;

139
clk2 = Abc_Clock();
Alan Mishchenko committed
140 141 142 143 144
        // initial state
        if ( pPars->fUseBackward )
            p->pInter = Inter_ManStartOneOutput( pAig, 1 );
        else
            p->pInter = Inter_ManStartInitState( Aig_ManRegNum(pAig) );
145
        assert( Aig_ManCoNum(p->pInter) == 1 );
146
clk = Abc_Clock();
Alan Mishchenko committed
147
        p->pCnfInter = Cnf_Derive( p->pInter, 0 );  
148
p->timeCnf += Abc_Clock() - clk;    
Alan Mishchenko committed
149
        // timeframes
150
        p->pFrames = Inter_ManFramesInter( pAig, p->nFrames, pPars->fUseBackward, pPars->fUseTwoFrames );
151
clk = Abc_Clock();
Alan Mishchenko committed
152 153 154 155 156 157 158
        if ( pPars->fRewrite )
        {
            p->pFrames = Dar_ManRwsat( pAigTemp = p->pFrames, 1, 0 );
            Aig_ManStop( pAigTemp );
//        p->pFrames = Fra_FraigEquivence( pAigTemp = p->pFrames, 100, 0 );
//        Aig_ManStop( pAigTemp );
        }
159
p->timeRwr += Abc_Clock() - clk;
Alan Mishchenko committed
160
        // can also do SAT sweeping on the timeframes...
161
clk = Abc_Clock();
Alan Mishchenko committed
162
        if ( pPars->fUseBackward )
163
            p->pCnfFrames = Cnf_Derive( p->pFrames, Aig_ManCoNum(p->pFrames) );  
Alan Mishchenko committed
164
        else
165 166
//            p->pCnfFrames = Cnf_Derive( p->pFrames, 0 );  
            p->pCnfFrames = Cnf_DeriveSimple( p->pFrames, 0 );  
167
p->timeCnf += Abc_Clock() - clk;    
Alan Mishchenko committed
168 169 170 171 172
        // report statistics
        if ( pPars->fVerbose )
        {
            printf( "Step = %2d. Frames = 1 + %d. And = %5d. Lev = %5d.  ", 
                s+1, p->nFrames, Aig_ManNodeNum(p->pFrames), Aig_ManLevelNum(p->pFrames) );
173
            ABC_PRT( "Time", Abc_Clock() - clk2 );
Alan Mishchenko committed
174
        }
175 176 177 178


        //////////////////////////////////////////
        // start containment checking
179
        if ( !(pPars->fTransLoop || pPars->fUseBackward || pPars->nFramesK > 1) )
180 181 182
        {
            pCheck = Inter_CheckStart( p->pAigTrans, pPars->nFramesK );
            // try new containment check for the initial state
183
clk = Abc_Clock();
184
            pCnfInter2 = Cnf_Derive( p->pInter, 1 );  
185 186
p->timeCnf += Abc_Clock() - clk;    
clk = Abc_Clock();
187
            RetValue = Inter_CheckPerform( pCheck, pCnfInter2, nTimeNewOut );
188
p->timeEqu += Abc_Clock() - clk;
189 190
//            assert( RetValue == 0 );
            Cnf_DataFree( pCnfInter2 );
191 192
            if ( p->vInters )
                Vec_PtrPush( p->vInters, Aig_ManDupSimple(p->pInter) );
193 194 195
        }
        //////////////////////////////////////////

Alan Mishchenko committed
196 197 198
        // iterate the interpolation procedure
        for ( i = 0; ; i++ )
        {
199
            if ( pPars->nFramesMax && p->nFrames + i >= pPars->nFramesMax )
Alan Mishchenko committed
200 201 202
            { 
                if ( pPars->fVerbose )
                    printf( "Reached limit (%d) on the number of timeframes.\n", pPars->nFramesMax );
203
                p->timeTotal = Abc_Clock() - clkTotal;
204
                Inter_ManStop( p, 0 );
205
                Inter_CheckStop( pCheck );
Alan Mishchenko committed
206 207 208 209
                return -1;
            }

            // perform interpolation
210
            clk = Abc_Clock();
Alan Mishchenko committed
211 212 213 214 215 216 217 218
#ifdef ABC_USE_LIBRARIES
            if ( pPars->fUseMiniSat )
            {
                assert( !pPars->fUseBackward );
                RetValue = Inter_ManPerformOneStepM114p( p, pPars->fUsePudlak, pPars->fUseOther );
            }
            else 
#endif
219
                RetValue = Inter_ManPerformOneStep( p, pPars->fUseBias, pPars->fUseBackward, nTimeNewOut );
Alan Mishchenko committed
220 221 222 223 224

            if ( pPars->fVerbose )
            {
                printf( "   I = %2d. Bmc =%3d. IntAnd =%6d. IntLev =%5d. Conf =%6d.  ", 
                    i+1, i + 1 + p->nFrames, Aig_ManNodeNum(p->pInter), Aig_ManLevelNum(p->pInter), p->nConfCur );
225
                ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
226
            }
227
            // remember the number of timeframes completed
228
            pPars->iFrameMax = i - 1 + p->nFrames;
Alan Mishchenko committed
229 230 231 232 233
            if ( RetValue == 0 ) // found a (spurious?) counter-example
            {
                if ( i == 0 ) // real counterexample
                {
                    if ( pPars->fVerbose )
Alan Mishchenko committed
234
                        printf( "Found a real counterexample in frame %d.\n", p->nFrames );
235
                    p->timeTotal = Abc_Clock() - clkTotal;
Alan Mishchenko committed
236
                    *piFrame = p->nFrames;
237 238 239 240 241 242 243 244 245 246 247 248 249 250
//                    pAig->pSeqModel = (Abc_Cex_t *)Inter_ManGetCounterExample( pAig, p->nFrames+1, pPars->fVerbose );
                    {
                        int RetValue;
                        Saig_ParBmc_t ParsBmc, * pParsBmc = &ParsBmc;
                        Saig_ParBmcSetDefaultParams( pParsBmc );
                        pParsBmc->nConfLimit = 100000000;
                        pParsBmc->nStart     = p->nFrames;
                        pParsBmc->fVerbose   = pPars->fVerbose;
                        RetValue = Saig_ManBmcScalable( pAig, pParsBmc );
                        if ( RetValue == 1 )
                            printf( "Error: The problem should be SAT but it is UNSAT.\n" );
                        else if ( RetValue == -1 )
                            printf( "Error: The problem timed out.\n" );
                    }
251
                    Inter_ManStop( p, 0 );
252
                    Inter_CheckStop( pCheck );
Alan Mishchenko committed
253 254 255 256
                    return 0;
                }
                // likely spurious counter-example
                p->nFrames += i;
Alan Mishchenko committed
257
                Inter_ManClean( p ); 
Alan Mishchenko committed
258 259
                break;
            }
260
            else if ( RetValue == -1 ) 
Alan Mishchenko committed
261
            {
262
                if ( pPars->nSecLimit && Abc_Clock() > nTimeNewOut ) // timed out
263 264 265 266 267 268 269 270 271 272
                {
                    if ( pPars->fVerbose )
                        printf( "Reached timeout (%d seconds).\n",  pPars->nSecLimit );
                }
                else
                {
                    assert( p->nConfCur >= p->nConfLimit );
                    if ( pPars->fVerbose )
                        printf( "Reached limit (%d) on the number of conflicts.\n", p->nConfLimit );
                }
273
                p->timeTotal = Abc_Clock() - clkTotal;
274
                Inter_ManStop( p, 0 );
275
                Inter_CheckStop( pCheck );
Alan Mishchenko committed
276 277 278 279
                return -1;
            }
            assert( RetValue == 1 ); // found new interpolant
            // compress the interpolant
280
clk = Abc_Clock();
Alan Mishchenko committed
281 282
            if ( p->pInterNew )
            {
283 284
                // save the timeout value
                p->pInterNew->Time2Quit = nTimeNewOut;
Alan Mishchenko committed
285
//                Ioa_WriteAiger( p->pInterNew, "interpol.aig", 0, 0 );
Alan Mishchenko committed
286
                p->pInterNew = Dar_ManRwsat( pAigTemp = p->pInterNew, 1, 0 );
287
//                p->pInterNew = Dar_ManRwsat( pAigTemp = p->pInterNew, 0, 0 );
Alan Mishchenko committed
288
                Aig_ManStop( pAigTemp );
289 290 291 292 293 294 295 296
                if ( p->pInterNew == NULL )
                {
                    printf( "Reached timeout (%d seconds) during rewriting.\n",  pPars->nSecLimit );
                    p->timeTotal = Abc_Clock() - clkTotal;
                    Inter_ManStop( p, 1 );
                    Inter_CheckStop( pCheck );
                    return -1;
                }
Alan Mishchenko committed
297
            }
298
p->timeRwr += Abc_Clock() - clk;
Alan Mishchenko committed
299 300

            // check if interpolant is trivial
301
            if ( p->pInterNew == NULL || Aig_ObjChild0(Aig_ManCo(p->pInterNew,0)) == Aig_ManConst0(p->pInterNew) )
Alan Mishchenko committed
302 303 304 305
            { 
//                printf( "interpolant is constant 0\n" );
                if ( pPars->fVerbose )
                    printf( "The problem is trivially true for all states.\n" );
306
                p->timeTotal = Abc_Clock() - clkTotal;
307
                Inter_ManStop( p, 1 );
308
                Inter_CheckStop( pCheck );
Alan Mishchenko committed
309 310 311 312
                return 1;
            }

            // check containment of interpolants
313
clk = Abc_Clock();
Alan Mishchenko committed
314 315
            if ( pPars->fCheckKstep ) // k-step unique-state induction
            {
316
                if ( Aig_ManCiNum(p->pInterNew) == Aig_ManCiNum(p->pInter) )
317
                {
318 319
                    if ( pPars->fTransLoop || pPars->fUseBackward || pPars->nFramesK > 1 )
                    {
320
clk2 = Abc_Clock();
321
                        Status = Inter_ManCheckInductiveContainment( p->pAigTrans, p->pInterNew, Abc_MinInt(i + 1, pPars->nFramesK), pPars->fUseBackward );
322
timeTemp = Abc_Clock() - clk2;
323
                    }
324 325
                    else
                    {   // new containment check
326
clk2 = Abc_Clock();
327
                        pCnfInter2 = Cnf_Derive( p->pInterNew, 1 );  
328 329
p->timeCnf += Abc_Clock() - clk2;
timeTemp = Abc_Clock() - clk2;
330 331
            
                        Status = Inter_CheckPerform( pCheck, pCnfInter2, nTimeNewOut );
332
                        Cnf_DataFree( pCnfInter2 );
333 334
                        if ( p->vInters )
                            Vec_PtrPush( p->vInters, Aig_ManDupSimple(p->pInterNew) );
335 336
                    }
                }
Alan Mishchenko committed
337 338 339 340 341
                else
                    Status = 0;
            }
            else // combinational containment
            {
342
                if ( Aig_ManCiNum(p->pInterNew) == Aig_ManCiNum(p->pInter) )
Alan Mishchenko committed
343 344 345 346
                    Status = Inter_ManCheckContainment( p->pInterNew, p->pInter );
                else
                    Status = 0;
            }
347
p->timeEqu += Abc_Clock() - clk - timeTemp;
Alan Mishchenko committed
348 349 350 351
            if ( Status ) // contained
            {
                if ( pPars->fVerbose )
                    printf( "Proved containment of interpolants.\n" );
352
                p->timeTotal = Abc_Clock() - clkTotal;
353
                Inter_ManStop( p, 1 );
354
                Inter_CheckStop( pCheck );
Alan Mishchenko committed
355 356
                return 1;
            }
357
            if ( pPars->nSecLimit && Abc_Clock() > nTimeNewOut )
358 359
            {
                printf( "Reached timeout (%d seconds).\n",  pPars->nSecLimit );
360
                p->timeTotal = Abc_Clock() - clkTotal;
361
                Inter_ManStop( p, 1 );
362
                Inter_CheckStop( pCheck );
363 364
                return -1;
            }
Alan Mishchenko committed
365 366 367 368 369 370 371 372
            // save interpolant and convert it into CNF
            if ( pPars->fTransLoop )
            {
                Aig_ManStop( p->pInter );
                p->pInter = p->pInterNew; 
            }
            else
            {
373 374 375 376 377 378
                if ( pPars->fUseBackward )
                {
                    p->pInter = Aig_ManCreateMiter( pAigTemp = p->pInter, p->pInterNew, 2 );
                    Aig_ManStop( pAigTemp );
                    Aig_ManStop( p->pInterNew );
                    // compress the interpolant
379
clk = Abc_Clock();
380 381
                    p->pInter = Dar_ManRwsat( pAigTemp = p->pInter, 1, 0 );
                    Aig_ManStop( pAigTemp );
382
p->timeRwr += Abc_Clock() - clk;
383 384 385 386 387 388
                }
                else // forward with the new containment checking (using only the frontier)
                {
                    Aig_ManStop( p->pInter );
                    p->pInter = p->pInterNew; 
                }
Alan Mishchenko committed
389 390 391
            }
            p->pInterNew = NULL;
            Cnf_DataFree( p->pCnfInter );
392
clk = Abc_Clock();
Alan Mishchenko committed
393
            p->pCnfInter = Cnf_Derive( p->pInter, 0 );  
394
p->timeCnf += Abc_Clock() - clk;
Alan Mishchenko committed
395
        }
396 397 398

        // start containment checking
        Inter_CheckStop( pCheck );
Alan Mishchenko committed
399 400 401 402 403 404 405 406 407 408 409 410
    }
    assert( 0 );
    return RetValue;
}



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


411 412
ABC_NAMESPACE_IMPL_END