rwrMan.c 8.59 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8
/**CFile****************************************************************

  FileName    [rwrMan.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting package.]

Alan Mishchenko committed
9
  Synopsis    [Rewriting manager.]
Alan Mishchenko committed
10 11 12 13 14 15 16 17 18 19 20 21

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "rwr.h"
22 23
#include "base/main/main.h"
#include "bool/dec/dec.h"
Alan Mishchenko committed
24

25 26 27
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
28 29 30 31 32
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
33
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
34 35 36 37
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
38
  Synopsis    [Starts rewriting manager.]
Alan Mishchenko committed
39 40 41 42 43 44 45 46

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
47
Rwr_Man_t * Rwr_ManStart( int  fPrecompute )
Alan Mishchenko committed
48
{
Alan Mishchenko committed
49
    Dec_Man_t * pManDec;
Alan Mishchenko committed
50
    Rwr_Man_t * p;
51 52
    abctime clk = Abc_Clock();
clk = Abc_Clock();
Alan Mishchenko committed
53
    p = ABC_ALLOC( Rwr_Man_t, 1 );
Alan Mishchenko committed
54
    memset( p, 0, sizeof(Rwr_Man_t) );
Alan Mishchenko committed
55
    p->nFuncs = (1<<16);
56
    pManDec   = (Dec_Man_t *)Abc_FrameReadManDec();
Alan Mishchenko committed
57 58 59 60
    p->puCanons = pManDec->puCanons; 
    p->pPhases  = pManDec->pPhases; 
    p->pPerms   = pManDec->pPerms; 
    p->pMap     = pManDec->pMap; 
Alan Mishchenko committed
61 62 63
    // initialize practical NPN classes
    p->pPractical  = Rwr_ManGetPractical( p );
    // create the table
Alan Mishchenko committed
64
    p->pTable = ABC_ALLOC( Rwr_Node_t *, p->nFuncs );
Alan Mishchenko committed
65 66 67 68 69 70 71 72 73
    memset( p->pTable, 0, sizeof(Rwr_Node_t *) * p->nFuncs );
    // create the elementary nodes
    p->pMmNode  = Extra_MmFixedStart( sizeof(Rwr_Node_t) );
    p->vForest  = Vec_PtrAlloc( 100 );
    Rwr_ManAddVar( p, 0x0000, fPrecompute ); // constant 0
    Rwr_ManAddVar( p, 0xAAAA, fPrecompute ); // var A
    Rwr_ManAddVar( p, 0xCCCC, fPrecompute ); // var B
    Rwr_ManAddVar( p, 0xF0F0, fPrecompute ); // var C
    Rwr_ManAddVar( p, 0xFF00, fPrecompute ); // var D
Alan Mishchenko committed
74
    p->nClasses = 5;
Alan Mishchenko committed
75
    // other stuff
Alan Mishchenko committed
76 77 78 79 80
    p->nTravIds   = 1;
    p->pPerms4    = Extra_Permutations( 4 );
    p->vLevNums   = Vec_IntAlloc( 50 );
    p->vFanins    = Vec_PtrAlloc( 50 );
    p->vFaninsCur = Vec_PtrAlloc( 50 );
Alan Mishchenko committed
81
    p->vNodesTemp = Vec_PtrAlloc( 50 );
Alan Mishchenko committed
82 83
    if ( fPrecompute )
    {   // precompute subgraphs
Alan Mishchenko committed
84
        Rwr_ManPrecompute( p );
Alan Mishchenko committed
85
//        Rwr_ManPrint( p );
Alan Mishchenko committed
86
        Rwr_ManWriteToArray( p );
Alan Mishchenko committed
87 88
    }
    else
Alan Mishchenko committed
89
    {   // load saved subgraphs
Alan Mishchenko committed
90
        Rwr_ManLoadFromArray( p, 0 );
Alan Mishchenko committed
91
//        Rwr_ManPrint( p );
Alan Mishchenko committed
92
        Rwr_ManPreprocess( p );
Alan Mishchenko committed
93
    }
94
p->timeStart = Abc_Clock() - clk;
Alan Mishchenko committed
95 96 97 98 99
    return p;
}

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

Alan Mishchenko committed
100
  Synopsis    [Stops rewriting manager.]
Alan Mishchenko committed
101 102 103 104 105 106 107 108

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
109
void Rwr_ManStop( Rwr_Man_t * p )
Alan Mishchenko committed
110
{
Alan Mishchenko committed
111 112 113 114
    if ( p->vClasses )
    {
        Rwr_Node_t * pNode;
        int i, k;
115
        Vec_VecForEachEntry( Rwr_Node_t *, p->vClasses, pNode, i, k )
Alan Mishchenko committed
116
            Dec_GraphFree( (Dec_Graph_t *)pNode->pNext );
Alan Mishchenko committed
117 118
    }
    if ( p->vClasses )  Vec_VecFree( p->vClasses );
Alan Mishchenko committed
119
    Vec_PtrFree( p->vNodesTemp );
Alan Mishchenko committed
120
    Vec_PtrFree( p->vForest );
Alan Mishchenko committed
121 122
    Vec_IntFree( p->vLevNums );
    Vec_PtrFree( p->vFanins );
Alan Mishchenko committed
123
    Vec_PtrFree( p->vFaninsCur );
Alan Mishchenko committed
124
    Extra_MmFixedStop( p->pMmNode );
Alan Mishchenko committed
125 126 127 128 129
    ABC_FREE( p->pMapInv );
    ABC_FREE( p->pTable );
    ABC_FREE( p->pPractical );
    ABC_FREE( p->pPerms4 );
    ABC_FREE( p );
Alan Mishchenko committed
130 131 132 133
}

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

Alan Mishchenko committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwr_ManPrintStats( Rwr_Man_t * p )
{
    int i, Counter = 0;
    for ( i = 0; i < 222; i++ )
        Counter += (p->nScores[i] > 0);

    printf( "Rewriting statistics:\n" );
    printf( "Total cuts tries  = %8d.\n", p->nCutsGood );
    printf( "Bad cuts found    = %8d.\n", p->nCutsBad );
    printf( "Total subgraphs   = %8d.\n", p->nSubgraphs );
    printf( "Used NPN classes  = %8d.\n", Counter );
    printf( "Nodes considered  = %8d.\n", p->nNodesConsidered );
    printf( "Nodes rewritten   = %8d.\n", p->nNodesRewritten );
Alan Mishchenko committed
156
    printf( "Gain              = %8d. (%6.2f %%).\n", p->nNodesBeg-p->nNodesEnd, 100.0*(p->nNodesBeg-p->nNodesEnd)/p->nNodesBeg );
Alan Mishchenko committed
157 158 159 160 161 162 163
    ABC_PRT( "Start       ", p->timeStart );
    ABC_PRT( "Cuts        ", p->timeCut );
    ABC_PRT( "Resynthesis ", p->timeRes );
    ABC_PRT( "    Mffc    ", p->timeMffc );
    ABC_PRT( "    Eval    ", p->timeEval );
    ABC_PRT( "Update      ", p->timeUpdate );
    ABC_PRT( "TOTAL       ", p->timeTotal );
Alan Mishchenko committed
164

Alan Mishchenko committed
165
/*
Alan Mishchenko committed
166
    printf( "The scores are:\n" );
Alan Mishchenko committed
167 168
    for ( i = 0; i < 222; i++ )
        if ( p->nScores[i] > 0 )
Alan Mishchenko committed
169 170 171 172 173
        {
            extern void Ivy_TruthDsdComputePrint( unsigned uTruth );
            printf( "%3d = %8d  canon = %5d  ", i, p->nScores[i], p->pMapInv[i] );
            Ivy_TruthDsdComputePrint( (unsigned)p->pMapInv[i] | ((unsigned)p->pMapInv[i] << 16) );
        }
Alan Mishchenko committed
174
*/
Alan Mishchenko committed
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
    printf( "\n" );

}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwr_ManPrintStatsFile( Rwr_Man_t * p )
{
    FILE * pTable;
    pTable = fopen( "stats.txt", "a+" );
    fprintf( pTable, "%d ", p->nCutsGood );
    fprintf( pTable, "%d ", p->nSubgraphs );
    fprintf( pTable, "%d ", p->nNodesRewritten );
    fprintf( pTable, "%d", p->nNodesGained );
    fprintf( pTable, "\n" );
    fclose( pTable );
Alan Mishchenko committed
200 201 202 203 204 205 206 207 208 209 210 211 212
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
213
void * Rwr_ManReadDecs( Rwr_Man_t * p )
Alan Mishchenko committed
214
{
Alan Mishchenko committed
215
    return p->pGraph;
Alan Mishchenko committed
216 217
}

Alan Mishchenko committed
218 219 220 221 222 223 224 225 226 227 228
/**Function*************************************************************

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
Vec_Ptr_t * Rwr_ManReadLeaves( Rwr_Man_t * p )
{
    return p->vFanins;
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
int Rwr_ManReadCompl( Rwr_Man_t * p )
{
    return p->fCompl;
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
261
void Rwr_ManAddTimeCuts( Rwr_Man_t * p, abctime Time )
Alan Mishchenko committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
{
    p->timeCut += Time;
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
277
void Rwr_ManAddTimeUpdate( Rwr_Man_t * p, abctime Time )
Alan Mishchenko committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
{
    p->timeUpdate += Time;
}

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

  Synopsis    [Stops the resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
293
void Rwr_ManAddTimeTotal( Rwr_Man_t * p, abctime Time )
Alan Mishchenko committed
294 295 296 297
{
    p->timeTotal += Time;
}

Alan Mishchenko committed
298

Alan Mishchenko committed
299 300
/**Function*************************************************************

Alan Mishchenko committed
301
  Synopsis    [Precomputes AIG subgraphs.]
Alan Mishchenko committed
302 303 304 305 306 307 308 309

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
310
void Rwr_Precompute()
Alan Mishchenko committed
311
{
Alan Mishchenko committed
312 313 314
    Rwr_Man_t * p;
    p = Rwr_ManStart( 1 );
    Rwr_ManStop( p );
Alan Mishchenko committed
315 316 317 318 319 320 321
}

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


322 323
ABC_NAMESPACE_IMPL_END