mfsGia.c 9.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [mfsGia.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [The good old minimization with complete don't-cares.]

  Synopsis    [Experimental code based on the new AIG package.]

  Author      [Alan Mishchenko]

  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - July 29, 2009.]

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

***********************************************************************/
 
#include "mfsInt.h"
22
#include "aig/gia/giaAig.h"
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

ABC_NAMESPACE_IMPL_START


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

static inline int Gia_ObjChild0Copy( Aig_Obj_t * pObj )  { return Gia_LitNotCond( Aig_ObjFanin0(pObj)->iData, Aig_ObjFaninC0(pObj) ); }
static inline int Gia_ObjChild1Copy( Aig_Obj_t * pObj )  { return Gia_LitNotCond( Aig_ObjFanin1(pObj)->iData, Aig_ObjFaninC1(pObj) ); }

// r i10_if6.blif;  ps; mfs -v
// r pj1_if6.blif;  ps; mfs -v
// r x/01_if6.blif; ps; mfs -v

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

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

  Synopsis    [Derives the resubstitution miter as an GIA.]

  Description []
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
Gia_Man_t * Gia_ManCreateResubMiter( Aig_Man_t * p )
{
    Gia_Man_t * pNew;//, * pTemp;
    Aig_Obj_t * pObj;
    int i, * pOuts0, * pOuts1;
    Aig_ManSetPioNumbers( p );
    // create the new manager
    pNew = Gia_ManStart( Aig_ManObjNum(p) );
    pNew->pName = Gia_UtilStrsav( p->pName );
    Gia_ManHashAlloc( pNew );
    // create the objects
    pOuts0 = ABC_ALLOC( int, Aig_ManPoNum(p) );
    Aig_ManForEachObj( p, pObj, i )
    {
        if ( Aig_ObjIsAnd(pObj) )
            pObj->iData = Gia_ManHashAnd( pNew, Gia_ObjChild0Copy(pObj), Gia_ObjChild1Copy(pObj) );
        else if ( Aig_ObjIsPi(pObj) )
            pObj->iData = Gia_ManAppendCi( pNew );
        else if ( Aig_ObjIsPo(pObj) )
            pOuts0[ Aig_ObjPioNum(pObj) ] = Gia_ObjChild0Copy(pObj);
        else if ( Aig_ObjIsConst1(pObj) )
            pObj->iData = 1;
        else
            assert( 0 );
    }
    // create the objects
    pOuts1 = ABC_ALLOC( int, Aig_ManPoNum(p) );
    Aig_ManForEachObj( p, pObj, i )
    {
        if ( Aig_ObjIsAnd(pObj) )
            pObj->iData = Gia_ManHashAnd( pNew, Gia_ObjChild0Copy(pObj), Gia_ObjChild1Copy(pObj) );
        else if ( Aig_ObjIsPi(pObj) )
            pObj->iData = Gia_ManAppendCi( pNew );
        else if ( Aig_ObjIsPo(pObj) )
            pOuts1[ Aig_ObjPioNum(pObj) ] = Gia_ObjChild0Copy(pObj);
        else if ( Aig_ObjIsConst1(pObj) )
            pObj->iData = 1;
        else
            assert( 0 );
    }
    // add the outputs
    Gia_ManAppendCo( pNew, pOuts0[0] );
    Gia_ManAppendCo( pNew, pOuts1[0] );
    Gia_ManAppendCo( pNew, pOuts0[1] );
    Gia_ManAppendCo( pNew, Gia_LitNot(pOuts1[1]) );
    for ( i = 2; i < Aig_ManPoNum(p); i++ )
        Gia_ManAppendCo( pNew, Gia_LitNot( Gia_ManHashXor(pNew, pOuts0[i], pOuts1[i]) ) );
    Gia_ManHashStop( pNew );
    ABC_FREE( pOuts0 );
    ABC_FREE( pOuts1 );
//    pNew = Gia_ManCleanup( pTemp = pNew );
//    Gia_ManStop( pTemp );
    return pNew;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkMfsConstructGia( Mfs_Man_t * p )
{
    int nBTLimit = 500;
    // prepare AIG
    assert( p->pGia == NULL );
    p->pGia = Gia_ManCreateResubMiter( p->pAigWin );
    // prepare AIG
    Gia_ManCreateRefs( p->pGia );
    Gia_ManCleanMark0( p->pGia );
    Gia_ManCleanMark1( p->pGia );
    Gia_ManFillValue ( p->pGia ); // maps nodes into trail ids
    Gia_ManCleanPhase( p->pGia ); 
    // prepare solver
    p->pTas = Tas_ManAlloc( p->pGia, nBTLimit );
    p->vCex = Tas_ReadModel( p->pTas );
    p->vGiaLits = Vec_PtrAlloc( 100 );
}


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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkMfsDeconstructGia( Mfs_Man_t * p )
{
    assert( p->pGia != NULL );
    Gia_ManStop( p->pGia ); p->pGia = NULL;
    Tas_ManStop( p->pTas ); p->pTas = NULL;
    Vec_PtrFree( p->vGiaLits );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkMfsResimulate( Gia_Man_t * p, Vec_Int_t * vCex )
{
    Gia_Obj_t * pObj;
    int i, Entry;
//    Gia_ManCleanMark1( p );
    Gia_ManConst0(p)->fMark1 = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->fMark1 = 0;
//        pObj->fMark1 = Gia_ManRandom(0);
    Vec_IntForEachEntry( vCex, Entry, i )
    {
        pObj = Gia_ManCi( p, Gia_Lit2Var(Entry) );
        pObj->fMark1 = !Gia_LitIsCompl(Entry);
    }
    Gia_ManForEachAnd( p, pObj, i )
        pObj->fMark1 = (Gia_ObjFanin0(pObj)->fMark1 ^ Gia_ObjFaninC0(pObj)) & 
                       (Gia_ObjFanin1(pObj)->fMark1 ^ Gia_ObjFaninC1(pObj));
    Gia_ManForEachCo( p, pObj, i )
        pObj->fMark1 =  Gia_ObjFanin0(pObj)->fMark1 ^ Gia_ObjFaninC0(pObj);
}




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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkMfsTryResubOnceGia( Mfs_Man_t * p, int * pCands, int nCands )
{
    int fVeryVerbose = 0;
    int fUseGia = 1;
    unsigned * pData;
208 209
    int i, iVar, status, iOut;
    clock_t clk = clock();
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
    p->nSatCalls++;
//    return -1;
    assert( p->pGia != NULL );
    assert( p->pTas != NULL );
    // convert to literals
    Vec_PtrClear( p->vGiaLits );
    // create the first four literals
    Vec_PtrPush( p->vGiaLits, Gia_ObjChild0(Gia_ManPo(p->pGia, 0)) );
    Vec_PtrPush( p->vGiaLits, Gia_ObjChild0(Gia_ManPo(p->pGia, 1)) );
    Vec_PtrPush( p->vGiaLits, Gia_ObjChild0(Gia_ManPo(p->pGia, 2)) );
    Vec_PtrPush( p->vGiaLits, Gia_ObjChild0(Gia_ManPo(p->pGia, 3)) );
    for ( i = 0; i < nCands; i++ )
    {
        // get the output number
        iOut = Gia_Lit2Var(pCands[i]) - 2 * p->pCnf->nVars;
        // write the literal
        Vec_PtrPush( p->vGiaLits, Gia_ObjChild0(Gia_ManPo(p->pGia, 4 + iOut)) );
    }
    // perform SAT solving
    status = Tas_ManSolveArray( p->pTas, p->vGiaLits );
    if ( status == -1 )
    {
        p->nTimeOuts++;
        if ( fVeryVerbose )
        printf( "t" );
//        p->nSatUndec++;
//        p->nConfUndec += p->Pars.nBTThis;
//        Cec_ManSatAddToStore( vCexStore, NULL, i ); // timeout
//        p->timeSatUndec += clock() - clk;
    }
    else if ( status == 1 )
    {
        if ( fVeryVerbose )
        printf( "u" );
//        p->nSatUnsat++;
//        p->nConfUnsat += p->Pars.nBTThis;
//        p->timeSatUnsat += clock() - clk;
    }
    else
    {
        p->nSatCexes++;
        if ( fVeryVerbose )
        printf( "s" );
//        p->nSatSat++;
//        p->nConfSat += p->Pars.nBTThis;
//        Gia_SatVerifyPattern( pAig, pRoot, vCex, vVisit );
//        Cec_ManSatAddToStore( vCexStore, vCex, i );
//        p->timeSatSat += clock() - clk;

        // resimulate the counter-example
        Abc_NtkMfsResimulate( p->pGia, Tas_ReadModel(p->pTas) );
   
        if ( fUseGia )
        {
/*
            int Val0 = Gia_ManPo(p->pGia, 0)->fMark1;
            int Val1 = Gia_ManPo(p->pGia, 1)->fMark1;
            int Val2 = Gia_ManPo(p->pGia, 2)->fMark1;
            int Val3 = Gia_ManPo(p->pGia, 3)->fMark1;
            assert( Val0 == 1 );
            assert( Val1 == 1 );
            assert( Val2 == 1 );
            assert( Val3 == 1 );
*/
            // store the counter-example
275
            Vec_IntForEachEntry( p->vProjVarsSat, iVar, i )
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
            {
                pData = (unsigned *)Vec_PtrEntry( p->vDivCexes, i );
                iOut = iVar - 2 * p->pCnf->nVars;
//                if ( !Gia_ManPo( p->pGia, 4 + iOut )->fMark1 ) // remove 0s!!!
                if ( Gia_ManPo( p->pGia, 4 + iOut )->fMark1 ) // remove 0s!!!  - rememeber complemented attribute
                {
                    assert( Aig_InfoHasBit(pData, p->nCexes) );
                    Aig_InfoXorBit( pData, p->nCexes );
                }
            }
            p->nCexes++;
        }

    }
    return status;
}


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


ABC_NAMESPACE_IMPL_END