giaSatoko.c 6.03 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
/**CFile****************************************************************

  FileName    [giaSatoko.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Interface to Satoko solver.]

  Author      [Alan Mishchenko, Bruno Schmitt]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
#include "sat/cnf/cnf.h"
#include "sat/satoko/satoko.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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
void Gia_ManCollectVars_rec( int Var, Vec_Int_t * vMapping, Vec_Int_t * vRes, Vec_Bit_t * vVisit )
{
    int * pCut, i;
    if ( Vec_BitEntry(vVisit, Var) )
        return;
    Vec_BitWriteEntry(vVisit, Var, 1);
    if ( Vec_IntEntry(vMapping, Var) ) // primary input or constant 0
    {
        pCut = Vec_IntEntryP( vMapping, Vec_IntEntry(vMapping, Var) );
        for ( i = 1; i <= pCut[0]; i++ )
            Gia_ManCollectVars_rec( pCut[i], vMapping, vRes, vVisit );
    }
    Vec_IntPush( vRes, Var );        
}
Vec_Int_t * Gia_ManCollectVars( int Root, Vec_Int_t * vMapping, int nVars )
{
    Vec_Int_t * vRes = Vec_IntAlloc( 100 );
    Vec_Bit_t * vVisit = Vec_BitStart( nVars );
    assert( Vec_IntEntry(vMapping, Root) );
    Gia_ManCollectVars_rec( Root, vMapping, vRes, vVisit );
    Vec_BitFree( vVisit );
    return vRes;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
satoko_t * Gia_ManSatokoInit( Cnf_Dat_t * pCnf, satoko_opts_t * opts )
Alan Mishchenko committed
83 84
{
    satoko_t * pSat = satoko_create();
85
    int i;
Alan Mishchenko committed
86 87 88
    //sat_solver_setnvars( pSat, p->nVars );
    for ( i = 0; i < pCnf->nClauses; i++ )
    {
Alan Mishchenko committed
89
        if ( !satoko_add_clause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1]-pCnf->pClauses[i] ) )
Alan Mishchenko committed
90 91 92 93 94
        {
            satoko_destroy( pSat );
            return NULL;
        }
    }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    satoko_configure(pSat, opts);
    return pSat;
}
void Gia_ManSatokoReport( int iOutput, int status, abctime clk )
{
    if ( iOutput >= 0 )
        Abc_Print( 1, "Output %6d : ", iOutput );
    else
        Abc_Print( 1, "Total: " );

    if ( status == SATOKO_UNDEC )
        Abc_Print( 1, "UNDECIDED      " );
    else if ( status == SATOKO_SAT )
        Abc_Print( 1, "SATISFIABLE    " );
    else
        Abc_Print( 1, "UNSATISFIABLE  " );

    Abc_PrintTime( 1, "Time", clk );
}
satoko_t * Gia_ManSatokoCreate( Gia_Man_t * p, satoko_opts_t * opts )
{
    Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 1, 0, 0 );
    satoko_t * pSat = Gia_ManSatokoInit( pCnf, opts );
    int status = pSat ? satoko_simplify(pSat) : SATOKO_OK;
Alan Mishchenko committed
119 120 121 122 123 124
    Cnf_DataFree( pCnf );
    if ( status == SATOKO_OK )
        return pSat;
    satoko_destroy( pSat );
    return NULL;
}
125
int Gia_ManSatokoCallOne( Gia_Man_t * p, satoko_opts_t * opts, int iOutput )
Alan Mishchenko committed
126 127 128
{
    abctime clk = Abc_Clock();
    satoko_t * pSat;
129 130
    int status, Cost = 0;

131
    pSat = Gia_ManSatokoCreate( p, opts );
Alan Mishchenko committed
132 133 134
    if ( pSat )
    {
        status = satoko_solve( pSat );
135
        Cost = satoko_stats(pSat)->n_conflicts;
Alan Mishchenko committed
136 137 138 139 140
        satoko_destroy( pSat );
    }
    else 
        status = SATOKO_UNSAT;

141
    Gia_ManSatokoReport( iOutput, status, Abc_Clock() - clk );
142
    return Cost;
Alan Mishchenko committed
143
}
144
void Gia_ManSatokoCall( Gia_Man_t * p, satoko_opts_t * opts, int fSplit, int fIncrem )
Alan Mishchenko committed
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
    int fUseCone = 1;
    Gia_Man_t * pOne;
    Gia_Obj_t * pRoot;
    Vec_Int_t * vCone;
    int i, iLit, status;
    if ( fIncrem )
    {
        abctime clk = Abc_Clock();
        Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, fUseCone, 0 );
        satoko_t * pSat = Gia_ManSatokoInit( pCnf, opts );
        Gia_ManForEachCo( p, pRoot, i )
        {
            abctime clk = Abc_Clock();
            iLit = Abc_Var2Lit( i+1, 0 );
            satoko_assump_push( pSat, iLit );
            if ( fUseCone )
            {
                vCone = Gia_ManCollectVars( i+1, pCnf->vMapping, pCnf->nVars );
                satoko_mark_cone( pSat, Vec_IntArray(vCone), Vec_IntSize(vCone) );
                printf( "Cone has %6d vars (out of %6d).  ", Vec_IntSize(vCone), pCnf->nVars );
                status = satoko_solve( pSat );
                satoko_unmark_cone( pSat, Vec_IntArray(vCone), Vec_IntSize(vCone) );
                Vec_IntFree( vCone );
            }
            else
            {
                status = satoko_solve( pSat );
            }
            satoko_assump_pop( pSat );
            Gia_ManSatokoReport( i, status, Abc_Clock() - clk );
        }
        Cnf_DataFree( pCnf );
        satoko_destroy( pSat );
        Abc_PrintTime( 1, "Total time", Abc_Clock() - clk );
        return;
    }
Alan Mishchenko committed
182 183
    if ( fSplit )
    {
184
        abctime clk = Abc_Clock();
Alan Mishchenko committed
185 186 187
        Gia_ManForEachCo( p, pRoot, i )
        {
            pOne = Gia_ManDupDfsCone( p, pRoot );
188
            Gia_ManSatokoCallOne( pOne, opts, i );
Alan Mishchenko committed
189 190
            Gia_ManStop( pOne );
        }
191
        Abc_PrintTime( 1, "Total time", Abc_Clock() - clk );
Alan Mishchenko committed
192 193
        return;
    }
194
    Gia_ManSatokoCallOne( p, opts, -1 );
Alan Mishchenko committed
195 196 197 198 199 200 201 202 203 204
}    


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


ABC_NAMESPACE_IMPL_END