pdrCore.c 36.1 KB
Newer Older
1 2 3 4 5 6
/**CFile****************************************************************

  FileName    [pdrCore.c]

  SystemName  [ABC: Logic synthesis and verification system.]

7
  PackageName [Property driven reachability.]
8 9 10 11 12 13 14 15 16 17 18 19 20 21

  Synopsis    [Core procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 20, 2010.]

  Revision    [$Id: pdrCore.c,v 1.00 2010/11/20 00:00:00 alanmi Exp $]

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

#include "pdrInt.h"
Alan Mishchenko committed
22
#include "base/main/main.h"
23 24 25 26 27 28 29 30

ABC_NAMESPACE_IMPL_START


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

31
extern int Gia_ManToBridgeResult( FILE * pFile, int Result, Abc_Cex_t * pCex, int iPoProved );
Alan Mishchenko committed
32
extern int Gia_ManToBridgeAbort( FILE * pFile, int Size, unsigned char * pBuffer );
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Returns 1 if the state could be blocked.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pdr_ManSetDefaultParams( Pdr_Par_t * pPars )
{
    memset( pPars, 0, sizeof(Pdr_Par_t) );
52 53 54 55 56 57
//    pPars->iOutput        =      -1;  // zero-based output number
    pPars->nRecycle       =     300;  // limit on vars for recycling
    pPars->nFrameMax      =   10000;  // limit on number of timeframes
    pPars->nTimeOut       =       0;  // timeout in seconds
    pPars->nTimeOutGap    =       0;  // timeout in seconds since the last solved
    pPars->nConfLimit     =       0;  // limit on SAT solver conflicts
58
    pPars->nConfGenLimit  =       0;  // limit on SAT solver conflicts during generalization
59 60 61 62 63
    pPars->nRestLimit     =       0;  // limit on the number of proof-obligations
    pPars->fTwoRounds     =       0;  // use two rounds for generalization
    pPars->fMonoCnf       =       0;  // monolythic CNF
    pPars->fDumpInv       =       0;  // dump inductive invariant
    pPars->fShortest      =       0;  // forces bug traces to be shortest
64
    pPars->fUsePropOut    =       1;  // use property output
65 66 67 68
    pPars->fVerbose       =       0;  // verbose output
    pPars->fVeryVerbose   =       0;  // very verbose output
    pPars->fNotVerbose    =       0;  // not printing line-by-line progress
    pPars->iFrame         =      -1;  // explored up to this frame
69 70
    pPars->nFailOuts      =       0;  // the number of disproved outputs
    pPars->nDropOuts      =       0;  // the number of timed out outputs
71
    pPars->timeLastSolved =       0;  // last one solved
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
}

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

  Synopsis    [Reduces clause using analyzeFinal.]

  Description [Assumes that the SAT solver just terminated an UNSAT call.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Pdr_Set_t * Pdr_ManReduceClause( Pdr_Man_t * p, int k, Pdr_Set_t * pCube )
{
    Pdr_Set_t * pCubeMin;
    Vec_Int_t * vLits;
    int i, Entry, nCoreLits, * pCoreLits;
    // get relevant SAT literals
    nCoreLits = sat_solver_final(Pdr_ManSolver(p, k), &pCoreLits);
    // translate them into register literals and remove auxiliary
    vLits = Pdr_ManLitsToCube( p, k, pCoreLits, nCoreLits );
    // skip if there is no improvement
    if ( Vec_IntSize(vLits) == pCube->nLits )
        return NULL;
    assert( Vec_IntSize(vLits) < pCube->nLits );
    // if the cube overlaps with init, add any literal
    Vec_IntForEachEntry( vLits, Entry, i )
        if ( lit_sign(Entry) == 0 ) // positive literal
            break;
    if ( i == Vec_IntSize(vLits) ) // only negative literals
    {
        // add the first positive literal
        for ( i = 0; i < pCube->nLits; i++ )
            if ( lit_sign(pCube->Lits[i]) == 0 ) // positive literal
            {
                Vec_IntPush( vLits, pCube->Lits[i] );
                break;
            }
        assert( i < pCube->nLits );
    }
    // generate a starting cube
    pCubeMin  = Pdr_SetCreateSubset( pCube, Vec_IntArray(vLits), Vec_IntSize(vLits) );
    assert( !Pdr_SetIsInit(pCubeMin, -1) );
/*
    // make sure the cube works
    {
    int RetValue;
120
    RetValue = Pdr_ManCheckCube( p, k, pCubeMin, NULL, 0, 0 );
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    assert( RetValue );
    }
*/
    return pCubeMin;
}

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

  Synopsis    [Returns 1 if the state could be blocked.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManPushClauses( Pdr_Man_t * p )
{
    Pdr_Set_t * pTemp, * pCubeK, * pCubeK1;
    Vec_Ptr_t * vArrayK, * vArrayK1;
142
    int i, j, k, m, RetValue = 0, RetValue2, kMax = Vec_PtrSize(p->vSolvers)-1;
143
    int iStartFrame = p->pPars->fShiftStart ? p->iUseFrame : 1;
144
    int Counter = 0;
145
    abctime clk = Abc_Clock();
146
    assert( p->iUseFrame > 0 );
147
    Vec_VecForEachLevelStartStop( p->vClauses, vArrayK, k, iStartFrame, kMax )
148 149
    {
        Vec_PtrSort( vArrayK, (int (*)(void))Pdr_SetCompare );
150
        vArrayK1 = Vec_VecEntry( p->vClauses, k+1 );
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        Vec_PtrForEachEntry( Pdr_Set_t *, vArrayK, pCubeK, j )
        {
            Counter++;

            // remove cubes in the same frame that are contained by pCubeK
            Vec_PtrForEachEntryStart( Pdr_Set_t *, vArrayK, pTemp, m, j+1 )
            {
                if ( !Pdr_SetContains( pTemp, pCubeK ) ) // pCubeK contains pTemp
                    continue;
                Pdr_SetDeref( pTemp );
                Vec_PtrWriteEntry( vArrayK, m, Vec_PtrEntryLast(vArrayK) );
                Vec_PtrPop(vArrayK);
                m--;
            }

            // check if the clause can be moved to the next frame
167
            RetValue2 = Pdr_ManCheckCube( p, k, pCubeK, NULL, 0, 0 );
168 169 170
            if ( RetValue2 == -1 )
                return -1;
            if ( !RetValue2 )
171 172 173 174 175 176 177
                continue;

            {
                Pdr_Set_t * pCubeMin;
                pCubeMin = Pdr_ManReduceClause( p, k, pCubeK );
                if ( pCubeMin != NULL )
                {
178
//                Abc_Print( 1, "%d ", pCubeK->nLits - pCubeMin->nLits );
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
                    Pdr_SetDeref( pCubeK );
                    pCubeK = pCubeMin;
                }
            }

            // if it can be moved, add it to the next frame
            Pdr_ManSolverAddClause( p, k+1, pCubeK );
            // check if the clause subsumes others
            Vec_PtrForEachEntry( Pdr_Set_t *, vArrayK1, pCubeK1, i )
            {
                if ( !Pdr_SetContains( pCubeK1, pCubeK ) ) // pCubeK contains pCubeK1
                    continue;
                Pdr_SetDeref( pCubeK1 );
                Vec_PtrWriteEntry( vArrayK1, i, Vec_PtrEntryLast(vArrayK1) );
                Vec_PtrPop(vArrayK1);
                i--;
            }
            // add the last clause
            Vec_PtrPush( vArrayK1, pCubeK );
            Vec_PtrWriteEntry( vArrayK, j, Vec_PtrEntryLast(vArrayK) );
            Vec_PtrPop(vArrayK);
            j--;
        }
        if ( Vec_PtrSize(vArrayK) == 0 )
            RetValue = 1;
    }

    // clean up the last one
207
    vArrayK = Vec_VecEntry( p->vClauses, kMax );
208 209 210 211 212 213 214 215 216
    Vec_PtrSort( vArrayK, (int (*)(void))Pdr_SetCompare );
    Vec_PtrForEachEntry( Pdr_Set_t *, vArrayK, pCubeK, j )
    {
        // remove cubes in the same frame that are contained by pCubeK
        Vec_PtrForEachEntryStart( Pdr_Set_t *, vArrayK, pTemp, m, j+1 )
        {
            if ( !Pdr_SetContains( pTemp, pCubeK ) ) // pCubeK contains pTemp
                continue;
/*
217
            Abc_Print( 1, "===\n" );
218
            Pdr_SetPrint( stdout, pCubeK, Aig_ManRegNum(p->pAig), NULL );
219
            Abc_Print( 1, "\n" );
220
            Pdr_SetPrint( stdout, pTemp, Aig_ManRegNum(p->pAig), NULL );
221
            Abc_Print( 1, "\n" );
222 223 224 225 226 227 228
*/
            Pdr_SetDeref( pTemp );
            Vec_PtrWriteEntry( vArrayK, m, Vec_PtrEntryLast(vArrayK) );
            Vec_PtrPop(vArrayK);
            m--;
        }
    }
229
    p->tPush += Abc_Clock() - clk;
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    return RetValue;
}

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

  Synopsis    [Returns 1 if the clause is contained in higher clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManCheckContainment( Pdr_Man_t * p, int k, Pdr_Set_t * pSet )
{
    Pdr_Set_t * pThis;
    Vec_Ptr_t * vArrayK;
    int i, j, kMax = Vec_PtrSize(p->vSolvers)-1;
249
    Vec_VecForEachLevelStartStop( p->vClauses, vArrayK, i, k, kMax+1 )
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
        Vec_PtrForEachEntry( Pdr_Set_t *, vArrayK, pThis, j )
            if ( Pdr_SetContains( pSet, pThis ) )
                return 1;
    return 0;
}


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

  Synopsis    [Sorts literals by priority.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Pdr_ManSortByPriority( Pdr_Man_t * p, Pdr_Set_t * pCube )
{
    int * pPrios = Vec_IntArray(p->vPrio);
    int * pArray = p->pOrder;
    int temp, i, j, best_i, nSize = pCube->nLits;
    // initialize variable order
    for ( i = 0; i < nSize; i++ )
        pArray[i] = i;
    for ( i = 0; i < nSize-1; i++ )
    {
        best_i = i;
        for ( j = i+1; j < nSize; j++ )
//            if ( pArray[j] < pArray[best_i] )
            if ( pPrios[pCube->Lits[pArray[j]]>>1] < pPrios[pCube->Lits[pArray[best_i]]>>1] )
                best_i = j;
283 284
        temp = pArray[i];
        pArray[i] = pArray[best_i];
285 286 287 288
        pArray[best_i] = temp;
    }
/*
    for ( i = 0; i < pCube->nLits; i++ )
289 290
        Abc_Print( 1, "%2d : %5d    %5d  %5d\n", i, pArray[i], pCube->Lits[pArray[i]]>>1, pPrios[pCube->Lits[pArray[i]]>>1] );
    Abc_Print( 1, "\n" );
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
*/
    return pArray;
}


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

  Synopsis    [Returns 1 if the state could be blocked.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManGeneralize( Pdr_Man_t * p, int k, Pdr_Set_t * pCube, Pdr_Set_t ** ppPred, Pdr_Set_t ** ppCubeMin )
{
    Pdr_Set_t * pCubeMin, * pCubeTmp = NULL;
310
    int i, j, n, Lit, RetValue;
311
    abctime clk = Abc_Clock();
312 313 314
    int * pOrder;
    // if there is no induction, return
    *ppCubeMin = NULL;
315
    RetValue = Pdr_ManCheckCube( p, k, pCube, ppPred, p->pPars->nConfLimit, 0 );
316 317 318 319
    if ( RetValue == -1 )
        return -1;
    if ( RetValue == 0 )
    {
320
        p->tGeneral += Abc_Clock() - clk;
321 322
        return 0;
    }
323

324 325 326 327 328 329
    // reduce clause using assumptions
//    pCubeMin = Pdr_SetDup( pCube );
    pCubeMin = Pdr_ManReduceClause( p, k, pCube );
    if ( pCubeMin == NULL )
        pCubeMin = Pdr_SetDup( pCube );

330 331
    // perform generalization
    if ( !p->pPars->fSkipGeneral )
332
    {
333 334 335 336
        // sort literals by their occurences
        pOrder = Pdr_ManSortByPriority( p, pCubeMin );
        // try removing literals
        for ( j = 0; j < pCubeMin->nLits; j++ )
337
        {
338 339 340 341 342 343 344 345 346
            // use ordering
    //        i = j;
            i = pOrder[j];

            // check init state
            assert( pCubeMin->Lits[i] != -1 );
            if ( Pdr_SetIsInit(pCubeMin, i) )
                continue;
            // try removing this literal
347
            Lit = pCubeMin->Lits[i]; pCubeMin->Lits[i] = -1;
348
            RetValue = Pdr_ManCheckCube( p, k, pCubeMin, NULL, p->pPars->nConfLimit, 1 );
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
            if ( RetValue == -1 )
            {
                Pdr_SetDeref( pCubeMin );
                return -1;
            }
            pCubeMin->Lits[i] = Lit;
            if ( RetValue == 0 )
                continue;

            // remove j-th entry
            for ( n = j; n < pCubeMin->nLits-1; n++ )
                pOrder[n] = pOrder[n+1];
            j--;

            // success - update the cube
            pCubeMin = Pdr_SetCreateFrom( pCubeTmp = pCubeMin, i );
            Pdr_SetDeref( pCubeTmp );
            assert( pCubeMin->nLits > 0 );
            i--;

            // get the ordering by decreasing priorit
            pOrder = Pdr_ManSortByPriority( p, pCubeMin );
371 372
        }

373 374
        if ( p->pPars->fTwoRounds )
        for ( j = 0; j < pCubeMin->nLits; j++ )
375
        {
376 377 378 379 380 381 382 383 384
            // use ordering
    //        i = j;
            i = pOrder[j];

            // check init state
            assert( pCubeMin->Lits[i] != -1 );
            if ( Pdr_SetIsInit(pCubeMin, i) )
                continue;
            // try removing this literal
385
            Lit = pCubeMin->Lits[i]; pCubeMin->Lits[i] = -1;
386
            RetValue = Pdr_ManCheckCube( p, k, pCubeMin, NULL, p->pPars->nConfLimit, 0 );
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            if ( RetValue == -1 )
            {
                Pdr_SetDeref( pCubeMin );
                return -1;
            }
            pCubeMin->Lits[i] = Lit;
            if ( RetValue == 0 )
                continue;

            // remove j-th entry
            for ( n = j; n < pCubeMin->nLits-1; n++ )
                pOrder[n] = pOrder[n+1];
            j--;

            // success - update the cube
            pCubeMin = Pdr_SetCreateFrom( pCubeTmp = pCubeMin, i );
            Pdr_SetDeref( pCubeTmp );
            assert( pCubeMin->nLits > 0 );
            i--;

            // get the ordering by decreasing priorit
            pOrder = Pdr_ManSortByPriority( p, pCubeMin );
409 410 411 412 413
        }
    }

    assert( ppCubeMin != NULL );
    *ppCubeMin = pCubeMin;
414
    p->tGeneral += Abc_Clock() - clk;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    return 1;
}

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

  Synopsis    [Returns 1 if the state could be blocked.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManBlockCube( Pdr_Man_t * p, Pdr_Set_t * pCube )
{
    Pdr_Obl_t * pThis;
    Pdr_Set_t * pPred, * pCubeMin;
    int i, k, RetValue, Prio = ABC_INFINITY, Counter = 0;
434
    int kMax = Vec_PtrSize(p->vSolvers)-1;
435
    abctime clk;
436 437
    p->nBlocks++;
    // create first proof obligation
438
//    assert( p->pQueue == NULL );
439 440 441 442 443 444 445 446 447
    pThis = Pdr_OblStart( kMax, Prio--, pCube, NULL ); // consume ref
    Pdr_QueuePush( p, pThis );
    // try to solve it recursively
    while ( !Pdr_QueueIsEmpty(p) )
    {
        Counter++;
        pThis = Pdr_QueueHead( p );
        if ( pThis->iFrame == 0 )
            return 0; // SAT
448 449
        if ( pThis->iFrame > kMax ) // finished this level
            return 1;
Alan Mishchenko committed
450 451
        if ( p->nQueLim && p->nQueCur >= p->nQueLim )
        {
Alan Mishchenko committed
452
            p->nQueLim = p->nQueLim * 3 / 2;
Alan Mishchenko committed
453 454 455
            Pdr_QueueStop( p );
            return 1; // restart
        }
456 457 458
        pThis = Pdr_QueuePop( p );
        assert( pThis->iFrame > 0 );
        assert( !Pdr_SetIsInit(pThis->pState, -1) );
459
        p->iUseFrame = Abc_MinInt( p->iUseFrame, pThis->iFrame );
460
        clk = Abc_Clock();
461 462
        if ( Pdr_ManCheckContainment( p, pThis->iFrame, pThis->pState ) )
        {
463
            p->tContain += Abc_Clock() - clk;
464 465 466
            Pdr_OblDeref( pThis );
            continue;
        }
467
        p->tContain += Abc_Clock() - clk;
468 469

        // check if the cube is already contained
470
        RetValue = Pdr_ManCheckCubeCs( p, pThis->iFrame, pThis->pState );
471
        if ( RetValue == -1 ) // resource limit is reached
472 473 474 475 476
        {
            Pdr_OblDeref( pThis );
            return -1;
        }
        if ( RetValue ) // cube is blocked by clauses in this frame
477 478 479 480 481 482 483 484
        {
            Pdr_OblDeref( pThis );
            continue;
        }

        // check if the cube holds with relative induction
        pCubeMin = NULL;
        RetValue = Pdr_ManGeneralize( p, pThis->iFrame-1, pThis->pState, &pPred, &pCubeMin );
485
        if ( RetValue == -1 ) // resource limit is reached
486 487 488 489 490 491 492 493 494 495 496 497
        {
            Pdr_OblDeref( pThis );
            return -1;
        }
        if ( RetValue ) // cube is blocked inductively in this frame
        {
            assert( pCubeMin != NULL );
            // k is the last frame where pCubeMin holds
            k = pThis->iFrame;
            // check other frames
            assert( pPred == NULL );
            for ( k = pThis->iFrame; k < kMax; k++ )
498
            {
499
                RetValue = Pdr_ManCheckCube( p, k, pCubeMin, NULL, 0, 0 );
500 501 502 503 504 505
                if ( RetValue == -1 )
                {
                    Pdr_OblDeref( pThis );
                    return -1;
                }
                if ( !RetValue )
506
                    break;
507
            }
508 509 510
            // add new clause
            if ( p->pPars->fVeryVerbose )
            {
Alan Mishchenko committed
511 512 513
                Abc_Print( 1, "Adding cube " );
                Pdr_SetPrint( stdout, pCubeMin, Aig_ManRegNum(p->pAig), NULL );
                Abc_Print( 1, " to frame %d.\n", k );
514 515 516 517 518 519 520 521 522 523 524 525 526 527
            }
            // set priority flops
            for ( i = 0; i < pCubeMin->nLits; i++ )
            {
                assert( pCubeMin->Lits[i] >= 0 );
                assert( (pCubeMin->Lits[i] / 2) < Aig_ManRegNum(p->pAig) );
                Vec_IntAddToEntry( p->vPrio, pCubeMin->Lits[i] / 2, 1 );
            }
            Vec_VecPush( p->vClauses, k, pCubeMin );   // consume ref
            p->nCubes++;
            // add clause
            for ( i = 1; i <= k; i++ )
                Pdr_ManSolverAddClause( p, i, pCubeMin );
            // schedule proof obligation
528
            if ( (k < kMax || p->pPars->fReuseProofOblig) && !p->pPars->fShortest )
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
            {
                pThis->iFrame = k+1;
                pThis->prio   = Prio--;
                Pdr_QueuePush( p, pThis );
            }
            else
            {
                Pdr_OblDeref( pThis );
            }
        }
        else
        {
            assert( pCubeMin == NULL );
            assert( pPred != NULL );
            pThis->prio = Prio--;
            Pdr_QueuePush( p, pThis );
            pThis = Pdr_OblStart( pThis->iFrame-1, Prio--, pPred, Pdr_OblRef(pThis) );
            Pdr_QueuePush( p, pThis );
        }

549 550 551
        // check termination
        if ( p->pPars->pFuncStop && p->pPars->pFuncStop(p->pPars->RunId) )
            return -1;
552
        if ( p->timeToStop && Abc_Clock() > p->timeToStop )
553
            return -1;
554
        if ( p->timeToStopOne && Abc_Clock() > p->timeToStopOne )
555
            return -1;
556
        if ( p->pPars->nTimeOutGap && p->pPars->timeLastSolved && Abc_Clock() > p->pPars->timeLastSolved + p->pPars->nTimeOutGap * CLOCKS_PER_SEC )
557
            return -1;
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    }
    return 1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pdr_ManSolveInt( Pdr_Man_t * p )
{
    int fPrintClauses = 0;
576
    Pdr_Set_t * pCube = NULL;
577
    Aig_Obj_t * pObj;
578
    Abc_Cex_t * pCexNew;
579
    int k, RetValue = -1;
580
    int nOutDigits = Abc_Base10Log( Saig_ManPoNum(p->pAig) );
581 582
    abctime clkStart = Abc_Clock(), clkOne = 0;
    p->timeToStop = p->pPars->nTimeOut ? p->pPars->nTimeOut * CLOCKS_PER_SEC + Abc_Clock(): 0;
583
    assert( Vec_PtrSize(p->vSolvers) == 0 );
584 585 586 587 588 589 590 591 592 593
    // in the multi-output mode, mark trivial POs (those fed by const0) as solved 
    if ( p->pPars->fSolveAll )
        Saig_ManForEachPo( p->pAig, pObj, k )
            if ( Aig_ObjChild0(pObj) == Aig_ManConst0(p->pAig) )
            {
                Vec_IntWriteEntry( p->pPars->vOutMap, k, 1 ); // unsat
                p->pPars->nProveOuts++;
                if ( p->pPars->fUseBridge )
                    Gia_ManToBridgeResult( stdout, 1, NULL, k );
            }
594
    // create the first timeframe
595
    p->pPars->timeLastSolved = Abc_Clock();
596 597 598 599 600
    Pdr_ManCreateSolver( p, (k = 0) );
    while ( 1 )
    {
        p->nFrames = k;
        assert( k == Vec_PtrSize(p->vSolvers)-1 );
601
        p->iUseFrame = Abc_MaxInt(k, 1);
602
        Saig_ManForEachPo( p->pAig, pObj, p->iOutCur )
603
        {
604
            // skip disproved outputs
605 606
            if ( p->vCexes && Vec_PtrEntry(p->vCexes, p->iOutCur) )
                continue;
607 608 609
            // skip output whose time has run out
            if ( p->pTime4Outs && p->pTime4Outs[p->iOutCur] == 0 )
                continue;
610 611 612 613 614
            // check if the output is trivially solved
            if ( Aig_ObjChild0(pObj) == Aig_ManConst0(p->pAig) )
                continue;
            // check if the output is trivially solved
            if ( Aig_ObjChild0(pObj) == Aig_ManConst1(p->pAig) )
615
            {
616 617
                if ( !p->pPars->fSolveAll )
                {
618 619
                    pCexNew = Abc_CexMakeTriv( Aig_ManRegNum(p->pAig), Saig_ManPiNum(p->pAig), Saig_ManPoNum(p->pAig), k*Saig_ManPoNum(p->pAig)+p->iOutCur );
                    p->pAig->pSeqModel = pCexNew;
620 621
                    return 0; // SAT
                }
622
                pCexNew = (p->pPars->fUseBridge || p->pPars->fStoreCex) ? Abc_CexMakeTriv( Aig_ManRegNum(p->pAig), Saig_ManPiNum(p->pAig), Saig_ManPoNum(p->pAig), k*Saig_ManPoNum(p->pAig)+p->iOutCur ) : (Abc_Cex_t *)(ABC_PTRINT_T)1;
623
                p->pPars->nFailOuts++;
624
                if ( p->pPars->vOutMap ) Vec_IntWriteEntry( p->pPars->vOutMap, p->iOutCur, 0 );
625
                if ( !p->pPars->fNotVerbose )
626
                Abc_Print( 1, "Output %*d was trivially asserted in frame %2d (solved %*d out of %*d outputs).\n",
627 628
                    nOutDigits, p->iOutCur, k, nOutDigits, p->pPars->nFailOuts, nOutDigits, Saig_ManPoNum(p->pAig) );
                assert( Vec_PtrEntry(p->vCexes, p->iOutCur) == NULL );
629 630 631
                if ( p->pPars->fUseBridge )
                    Gia_ManToBridgeResult( stdout, 0, pCexNew, pCexNew->iPo );
                Vec_PtrWriteEntry( p->vCexes, p->iOutCur, pCexNew );
632 633
                if ( p->pPars->pFuncOnFail && p->pPars->pFuncOnFail(p->iOutCur, p->pPars->fStoreCex ? (Abc_Cex_t *)Vec_PtrEntry(p->vCexes, p->iOutCur) : NULL) )
                {
634
                    if ( p->pPars->fVerbose )
635
                        Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
636 637 638 639 640
                    if ( !p->pPars->fSilent )
                        Abc_Print( 1, "Quitting due to callback on fail.\n" );
                    p->pPars->iFrame = k;
                    return -1;
                }
641 642
                if ( p->pPars->nFailOuts + p->pPars->nDropOuts == Saig_ManPoNum(p->pAig) )
                    return p->pPars->nFailOuts ? 0 : -1; // SAT or UNDEC
643
                p->pPars->timeLastSolved = Abc_Clock();
644
                continue;
645
            }
646
            // try to solve this output
647 648 649
            if ( p->pTime4Outs )
            {
                assert( p->pTime4Outs[p->iOutCur] > 0 );
650 651
                clkOne = Abc_Clock();
                p->timeToStopOne = p->pTime4Outs[p->iOutCur] + Abc_Clock();
652
            }
653
            while ( 1 )
654
            {
655
                if ( p->pPars->nTimeOutGap && p->pPars->timeLastSolved && Abc_Clock() > p->pPars->timeLastSolved + p->pPars->nTimeOutGap * CLOCKS_PER_SEC )
656
                {
657
                    if ( p->pPars->fVerbose )
658
                        Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
659 660 661
                    if ( !p->pPars->fSilent )
                        Abc_Print( 1, "Reached gap timeout (%d seconds).\n",  p->pPars->nTimeOutGap );
                    p->pPars->iFrame = k;
662
                    return -1;
663
                }
664
                RetValue = Pdr_ManCheckCube( p, k, NULL, &pCube, p->pPars->nConfLimit, 0 );
665 666 667
                if ( RetValue == 1 )
                    break;
                if ( RetValue == -1 )
668
                {
669
                    if ( p->pPars->fVerbose )
670
                        Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
671 672 673 674 675 676 677 678 679 680 681
                    if ( p->timeToStop && Abc_Clock() > p->timeToStop )
                        Abc_Print( 1, "Reached timeout (%d seconds).\n",  p->pPars->nTimeOut );
                    else if ( p->pPars->nTimeOutGap && p->pPars->timeLastSolved && Abc_Clock() > p->pPars->timeLastSolved + p->pPars->nTimeOutGap * CLOCKS_PER_SEC )
                        Abc_Print( 1, "Reached gap timeout (%d seconds).\n",  p->pPars->nTimeOutGap );
                    else if ( p->timeToStopOne && Abc_Clock() > p->timeToStopOne )
                    {
                        Pdr_QueueClean( p );
                        pCube = NULL;
                        break; // keep solving
                    }
                    else if ( p->pPars->nConfLimit )
682
                        Abc_Print( 1, "Reached conflict limit (%d).\n",  p->pPars->nConfLimit );
683
                    else if ( p->pPars->fVerbose )
684
                        Abc_Print( 1, "Computation cancelled by the callback.\n" );
685
                    p->pPars->iFrame = k;
686
                    return -1;
687
                }
688 689 690 691 692
                if ( RetValue == 0 )
                {
                    RetValue = Pdr_ManBlockCube( p, pCube );
                    if ( RetValue == -1 )
                    {
693
                        if ( p->pPars->fVerbose )
694
                            Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
695
                        if ( p->timeToStop && Abc_Clock() > p->timeToStop )
696
                            Abc_Print( 1, "Reached timeout (%d seconds).\n",  p->pPars->nTimeOut );
697
                        else if ( p->pPars->nTimeOutGap && p->pPars->timeLastSolved && Abc_Clock() > p->pPars->timeLastSolved + p->pPars->nTimeOutGap * CLOCKS_PER_SEC )
698
                            Abc_Print( 1, "Reached gap timeout (%d seconds).\n",  p->pPars->nTimeOutGap );
699
                        else if ( p->timeToStopOne && Abc_Clock() > p->timeToStopOne )
700 701 702 703 704
                        {
                            Pdr_QueueClean( p );
                            pCube = NULL;
                            break; // keep solving
                        }
705 706
                        else if ( p->pPars->nConfLimit )
                            Abc_Print( 1, "Reached conflict limit (%d).\n",  p->pPars->nConfLimit );
707
                        else if ( p->pPars->fVerbose )
708 709
                            Abc_Print( 1, "Computation cancelled by the callback.\n" );
                        p->pPars->iFrame = k;
710
                        return -1;
711 712 713 714 715 716 717 718
                    }
                    if ( RetValue == 0 )
                    {
                        if ( fPrintClauses )
                        {
                            Abc_Print( 1, "*** Clauses after frame %d:\n", k );
                            Pdr_ManPrintClauses( p, 0 );
                        }
719
                        if ( p->pPars->fVerbose )
720
                            Pdr_ManPrintProgress( p, !p->pPars->fSolveAll, Abc_Clock() - clkStart );
721 722 723
                        p->pPars->iFrame = k;
                        if ( !p->pPars->fSolveAll )
                        {
724
                            p->pAig->pSeqModel = Pdr_ManDeriveCex(p);
725 726 727
                            return 0; // SAT
                        }
                        p->pPars->nFailOuts++;
728
                        pCexNew = (p->pPars->fUseBridge || p->pPars->fStoreCex) ? Pdr_ManDeriveCex(p) : (Abc_Cex_t *)(ABC_PTRINT_T)1;
729
                        if ( p->pPars->vOutMap ) Vec_IntWriteEntry( p->pPars->vOutMap, p->iOutCur, 0 );
730
                        assert( Vec_PtrEntry(p->vCexes, p->iOutCur) == NULL );
731 732 733
                        if ( p->pPars->fUseBridge )
                            Gia_ManToBridgeResult( stdout, 0, pCexNew, pCexNew->iPo );
                        Vec_PtrWriteEntry( p->vCexes, p->iOutCur, pCexNew );
734 735
                        if ( p->pPars->pFuncOnFail && p->pPars->pFuncOnFail(p->iOutCur, p->pPars->fStoreCex ? (Abc_Cex_t *)Vec_PtrEntry(p->vCexes, p->iOutCur) : NULL) )
                        {
736
                            if ( p->pPars->fVerbose )
737
                                Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
738 739 740 741 742
                            if ( !p->pPars->fSilent )
                                Abc_Print( 1, "Quitting due to callback on fail.\n" );
                            p->pPars->iFrame = k;
                            return -1;
                        }
743
                        if ( !p->pPars->fNotVerbose )
744
                            Abc_Print( 1, "Output %*d was asserted in frame %2d (%2d) (solved %*d out of %*d outputs).\n",
745
                                nOutDigits, p->iOutCur, k, k, nOutDigits, p->pPars->nFailOuts, nOutDigits, Saig_ManPoNum(p->pAig) );
746 747
                        if ( p->pPars->nFailOuts == Saig_ManPoNum(p->pAig) )
                            return 0; // all SAT
748 749 750
                        Pdr_QueueClean( p );
                        pCube = NULL;
                        break; // keep solving
751
                    }
752
                    if ( p->pPars->fVerbose )
753
                        Pdr_ManPrintProgress( p, 0, Abc_Clock() - clkStart );
754
                }
755
            }
756 757
            if ( p->pTime4Outs )
            {
758
                abctime timeSince = Abc_Clock() - clkOne;
759 760
                assert( p->pTime4Outs[p->iOutCur] > 0 );
                p->pTime4Outs[p->iOutCur] = (p->pTime4Outs[p->iOutCur] > timeSince) ? p->pTime4Outs[p->iOutCur] - timeSince : 0;
761
                if ( p->pTime4Outs[p->iOutCur] == 0 && Vec_PtrEntry(p->vCexes, p->iOutCur) == NULL ) // undecided
762 763
                {
                    p->pPars->nDropOuts++;
764 765
                    if ( p->pPars->vOutMap ) 
                        Vec_IntWriteEntry( p->pPars->vOutMap, p->iOutCur, -1 );
766 767
                    if ( !p->pPars->fNotVerbose ) 
                        Abc_Print( 1, "Timing out on output %*d.\n", nOutDigits, p->iOutCur );
768
                }
769
                p->timeToStopOne = 0;
770
            }
771
        }
772

773
        if ( p->pPars->fVerbose )
774
            Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
775 776 777 778 779 780 781 782 783 784 785 786 787
        // open a new timeframe
        p->nQueLim = p->pPars->nRestLimit;
        assert( pCube == NULL );
        Pdr_ManSetPropertyOutput( p, k );
        Pdr_ManCreateSolver( p, ++k );
        if ( fPrintClauses )
        {
            Abc_Print( 1, "*** Clauses after frame %d:\n", k );
            Pdr_ManPrintClauses( p, 0 );
        }
        // push clauses into this timeframe
        RetValue = Pdr_ManPushClauses( p );
        if ( RetValue == -1 )
788
        {
789
            if ( p->pPars->fVerbose )
790
                Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
791
            if ( !p->pPars->fSilent )
792
            {
793
                if ( p->timeToStop && Abc_Clock() > p->timeToStop )
794 795 796 797
                    Abc_Print( 1, "Reached timeout (%d seconds).\n",  p->pPars->nTimeOut );
                else
                    Abc_Print( 1, "Reached conflict limit (%d).\n",  p->pPars->nConfLimit );
            }
798
            p->pPars->iFrame = k;
799
            return -1;
800 801 802
        }
        if ( RetValue )
        {
803
            if ( p->pPars->fVerbose )
804
                Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
805 806 807 808 809
            if ( !p->pPars->fSilent )
                Pdr_ManReportInvariant( p );
            if ( !p->pPars->fSilent )
                Pdr_ManVerifyInvariant( p );
            p->pPars->iFrame = k;
810 811 812
            // count the number of UNSAT outputs
            p->pPars->nProveOuts = Saig_ManPoNum(p->pAig) - p->pPars->nFailOuts - p->pPars->nDropOuts;
            // convert previously 'unknown' into 'unsat'
813
            if ( p->pPars->vOutMap )
814
                for ( k = 0; k < Saig_ManPoNum(p->pAig); k++ )
815
                    if ( Vec_IntEntry(p->pPars->vOutMap, k) == -2 ) // unknown
816
                    {
817
                        Vec_IntWriteEntry( p->pPars->vOutMap, k, 1 ); // unsat
818 819 820
                        if ( p->pPars->fUseBridge )
                            Gia_ManToBridgeResult( stdout, 1, NULL, k );
                    }
821 822 823 824
            if ( p->pPars->nProveOuts == Saig_ManPoNum(p->pAig) )
                return 1; // UNSAT
            if ( p->pPars->nFailOuts > 0 )
                return 0; // SAT
825
            return -1;
826
        }
827
        if ( p->pPars->fVerbose )
828
            Pdr_ManPrintProgress( p, 0, Abc_Clock() - clkStart );
829

830 831 832 833
        // check termination
        if ( p->pPars->pFuncStop && p->pPars->pFuncStop(p->pPars->RunId) )
        {
            p->pPars->iFrame = k;
834
            return -1;
835
        }
836
        if ( p->timeToStop && Abc_Clock() > p->timeToStop )
837 838 839
        {
            if ( fPrintClauses )
            {
840
                Abc_Print( 1, "*** Clauses after frame %d:\n", k );
841 842
                Pdr_ManPrintClauses( p, 0 );
            }
843
            if ( p->pPars->fVerbose )
844
                Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
845 846
            if ( !p->pPars->fSilent )
                Abc_Print( 1, "Reached timeout (%d seconds).\n",  p->pPars->nTimeOut );
847
            p->pPars->iFrame = k;
848
            return -1;
849
        }
850
        if ( p->pPars->nTimeOutGap && p->pPars->timeLastSolved && Abc_Clock() > p->pPars->timeLastSolved + p->pPars->nTimeOutGap * CLOCKS_PER_SEC )
851 852 853 854 855 856
        {
            if ( fPrintClauses )
            {
                Abc_Print( 1, "*** Clauses after frame %d:\n", k );
                Pdr_ManPrintClauses( p, 0 );
            }
857
            if ( p->pPars->fVerbose )
858
                Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
859
            if ( !p->pPars->fSilent )
860
                Abc_Print( 1, "Reached gap timeout (%d seconds).\n",  p->pPars->nTimeOutGap );
861
            p->pPars->iFrame = k;
862
            return -1;
863
        }
864 865
        if ( p->pPars->nFrameMax && k >= p->pPars->nFrameMax )
        {
866
            if ( p->pPars->fVerbose )
867
                Pdr_ManPrintProgress( p, 1, Abc_Clock() - clkStart );
868 869
            if ( !p->pPars->fSilent )
                Abc_Print( 1, "Reached limit on the number of timeframes (%d).\n", p->pPars->nFrameMax );
870
            p->pPars->iFrame = k;
871
            return -1;
872
        }
873
    }
874 875
    assert( 0 );
    return -1;
876 877 878 879 880 881 882 883 884 885 886 887 888
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
889
int Pdr_ManSolve( Aig_Man_t * pAig, Pdr_Par_t * pPars )
890 891
{
    Pdr_Man_t * p;
892
    int k, RetValue;
893
    abctime clk = Abc_Clock();
894 895
    if ( pPars->nTimeOutOne && !pPars->fSolveAll )
        pPars->nTimeOutOne = 0;
896
    if ( pPars->nTimeOutOne && pPars->nTimeOut == 0 )
897
        pPars->nTimeOut = pPars->nTimeOutOne * Saig_ManPoNum(pAig) / 1000 + (int)((pPars->nTimeOutOne * Saig_ManPoNum(pAig) % 1000) > 0);
898 899 900
    if ( pPars->fVerbose )
    {
//    Abc_Print( 1, "Running PDR by Niklas Een (aka IC3 by Aaron Bradley) with these parameters:\n" );
901 902 903 904
        Abc_Print( 1, "VarMax = %d. FrameMax = %d. QueMax = %d. TimeMax = %d. ",
            pPars->nRecycle,
            pPars->nFrameMax,
            pPars->nRestLimit,
905
            pPars->nTimeOut );
906 907 908
        Abc_Print( 1, "MonoCNF = %s. SkipGen = %s. SolveAll = %s.\n",
            pPars->fMonoCnf ?     "yes" : "no",
            pPars->fSkipGeneral ? "yes" : "no",
909
            pPars->fSolveAll ?    "yes" : "no" );
910 911 912
    }
    ABC_FREE( pAig->pSeqModel );
    p = Pdr_ManStart( pAig, pPars, NULL );
913
    RetValue = Pdr_ManSolveInt( p );
914 915 916 917 918 919 920 921
    if ( RetValue == 0 )
        assert( pAig->pSeqModel != NULL || p->vCexes != NULL );
    if ( p->vCexes )
    {
        assert( p->pAig->vSeqModelVec == NULL );
        p->pAig->vSeqModelVec = p->vCexes;
        p->vCexes = NULL;
    }
922
    if ( p->pPars->fDumpInv )
923 924 925 926
    {
        Abc_FrameSetCnf( Pdr_ManDeriveInfinityClauses( p, RetValue!=1 ) );
        Abc_FrameSetStr( Pdr_ManDumpString(p) );
        Abc_FrameSetInv( Pdr_ManCountFlopsInv(p) );
927
        Pdr_ManDumpClauses( p, (char *)"inv.pla", RetValue==1 );
928
    }
929
    p->tTotal += Abc_Clock() - clk;
930
    Pdr_ManStop( p );
931
    pPars->iFrame--;
932 933 934 935 936
    // convert all -2 (unknown) entries into -1 (undec)
    if ( pPars->vOutMap )
        for ( k = 0; k < Saig_ManPoNum(pAig); k++ )
            if ( Vec_IntEntry(pPars->vOutMap, k) == -2 ) // unknown
                Vec_IntWriteEntry( pPars->vOutMap, k, -1 ); // undec
Alan Mishchenko committed
937 938
    if ( pPars->fUseBridge )
        Gia_ManToBridgeAbort( stdout, 7, (unsigned char *)"timeout" );
939 940 941 942 943 944 945 946 947
    return RetValue;
}

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


ABC_NAMESPACE_IMPL_END