summaryrefslogtreecommitdiffstats
path: root/src/aig/dar/darMan.c
blob: 25b15e9c400622d07a7f7e8bf96fccc118223cba (plain)
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
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
208
209
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
/**CFile****************************************************************

  FileName    [darMan.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting.]

  Synopsis    [AIG manager.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: darMan.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

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

#include "dar.h"

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

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

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

  Synopsis    [Starts the AIG manager.]

  Description [The argument of this procedure is a soft limit on the
  the number of nodes, or 0 if the limit is unknown.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Man_t * Dar_ManStart( int nNodesMax )
{
    Dar_Man_t * p;
    int i;
    if ( nNodesMax <= 0 )
        nNodesMax = 10007;
    // start the manager
    p = ALLOC( Dar_Man_t, 1 );
    memset( p, 0, sizeof(Dar_Man_t) );
    // perform initializations
    p->nTravIds = 1;
    p->fCatchExor = 0;
    // allocate arrays for nodes
    p->vPis = Vec_PtrAlloc( 100 );
    p->vPos = Vec_PtrAlloc( 100 );
    p->vObjs = Vec_PtrAlloc( 1000 );
    // prepare the internal memory manager
    p->pMemObjs = Dar_MmFixedStart( sizeof(Dar_Obj_t), nNodesMax );
    p->pMemCuts = Dar_MmFlexStart();
    // prepare storage for cuts
    p->nBaseCuts = DAR_CUT_BASE;
    for ( i = 0; i < p->nBaseCuts; i++ )
        p->pBaseCuts[i] = p->BaseCuts + i;
    // create the constant node
    p->pConst1 = Dar_ManFetchMemory( p );
    p->pConst1->Type = DAR_AIG_CONST1;
    p->pConst1->fPhase = 1;
    p->nObjs[DAR_AIG_CONST1]++;
    // start the table
    p->nTableSize = nNodesMax;
    p->pTable = ALLOC( Dar_Obj_t *, p->nTableSize );
    memset( p->pTable, 0, sizeof(Dar_Obj_t *) * p->nTableSize );
    // other data
    p->vLeavesBest = Vec_PtrAlloc( 4 );
    return p;
}

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

  Synopsis    [Duplicates the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Man_t * Dar_ManStartFrom( Dar_Man_t * p )
{
    Dar_Man_t * pNew;
    Dar_Obj_t * pObj;
    int i;
    // create the new manager
    pNew = Dar_ManStart();
    // create the PIs
    Dar_ManConst1(p)->pData = Dar_ManConst1(pNew);
    Dar_ManForEachPi( p, pObj, i )
        pObj->pData = Dar_ObjCreatePi(pNew);
    return pNew;
}

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

  Synopsis    [Duplicates the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Dar_Man_t * Dar_ManDup( Dar_Man_t * p )
{
    Dar_Man_t * pNew;
    Dar_Obj_t * pObj;
    int i;
    // create the new manager
    pNew = Dar_ManStart();
    // create the PIs
    Dar_ManConst1(p)->pData = Dar_ManConst1(pNew);
    Dar_ManForEachPi( p, pObj, i )
        pObj->pData = Dar_ObjCreatePi(pNew);
    // duplicate internal nodes
    Dar_ManForEachObj( p, pObj, i )
        if ( Dar_ObjIsBuf(pObj) )
            pObj->pData = Dar_ObjChild0Copy(pObj);
        else if ( Dar_ObjIsNode(pObj) )
            pObj->pData = Dar_And( pNew, Dar_ObjChild0Copy(pObj), Dar_ObjChild1Copy(pObj) );
    // add the POs
    Dar_ManForEachPo( p, pObj, i )
        Dar_ObjCreatePo( pNew, Dar_ObjChild0Copy(pObj) );
    // check the resulting network
    if ( !Dar_ManCheck(pNew) )
        printf( "Dar_ManDup(): The check has failed.\n" );
    return pNew;
}

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

  Synopsis    [Stops the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ManStop( Dar_Man_t * p )
{
    Dar_Obj_t * pObj;
    int i;
    // make sure the nodes have clean marks
    Dar_ManForEachObj( p, pObj, i )
        assert( !pObj->fMarkA && !pObj->fMarkB );
    // print time
    if ( p->time1 ) { PRT( "time1", p->time1 ); }
    if ( p->time2 ) { PRT( "time2", p->time2 ); }
//    Dar_TableProfile( p );
    Dar_MmFixedStop( p->pMemObjs, 0 );
    Dar_MmFlexStop( p->pMemCuts, 0 );
    if ( p->vPis )        Vec_PtrFree( p->vPis );
    if ( p->vPos )        Vec_PtrFree( p->vPos );
    if ( p->vObjs )       Vec_PtrFree( p->vObjs );
    if ( p->vRequired )   Vec_IntFree( p->vRequired );
    if ( p->vLeavesBest ) Vec_PtrFree( p->vLeavesBest );
    free( p->pTable );
    free( p );
}

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

  Synopsis    [Returns the number of dangling nodes removed.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dar_ManCleanup( Dar_Man_t * p )
{
    Vec_Ptr_t * vObjs;
    Dar_Obj_t * pNode;
    int i, nNodesOld;
    nNodesOld = Dar_ManNodeNum(p);
    // collect roots of dangling nodes
    vObjs = Vec_PtrAlloc( 100 );
    Dar_ManForEachObj( p, pNode, i )
        if ( Dar_ObjIsNode(pNode) && Dar_ObjRefs(pNode) == 0 )
            Vec_PtrPush( vObjs, pNode );
    // recursively remove dangling nodes
    Vec_PtrForEachEntry( vObjs, pNode, i )
        Dar_ObjDelete_rec( p, pNode, 1 );
    Vec_PtrFree( vObjs );
    return nNodesOld - Dar_ManNodeNum(p);
}

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

  Synopsis    [Stops the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ManPrintStats( Dar_Man_t * p )
{
    printf( "PI/PO/Lat = %5d/%5d/%5d   ", Dar_ManPiNum(p), Dar_ManPoNum(p), Dar_ManLatchNum(p) );
    printf( "A = %6d. ",    Dar_ManAndNum(p) );
    if ( Dar_ManExorNum(p) )
        printf( "X = %5d. ",    Dar_ManExorNum(p) );
    if ( Dar_ManBufNum(p) )
        printf( "B = %3d. ",    Dar_ManBufNum(p) );
    printf( "Cre = %6d. ",  p->nCreated );
    printf( "Del = %6d. ",  p->nDeleted );
//    printf( "Lev = %3d. ",  Dar_ManCountLevels(p) );
    printf( "\n" );
}

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

  Synopsis    [Stops the AIG manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dar_ManPrintRuntime( Dar_Man_t * p )
{
    int i, Gain;
    printf( "Good cuts = %d. Bad cuts = %d.  Cut mem = %d Mb\n", 
        p->nCutsGood, p->nCutsBad, p->nCutMemUsed );
    PRT( "Cuts  ", p->timeCuts );
    PRT( "Eval  ", p->timeEval );
    PRT( "Other ", p->timeOther );
    PRT( "TOTAL ", p->timeTotal );
    Gain = p->nNodesInit - Dar_ManNodeNum(p);
    for ( i = 0; i < 222; i++ )
    {
        if ( p->ClassGains[i] == 0 && p->ClassTimes[i] == 0 )
            continue;
        printf( "%3d : ", i );
        printf( "G = %6d (%5.2f %%)  ", p->ClassGains[i], Gain? 100.0*p->ClassGains[i]/Gain : 0.0 );
        printf( "S = %8d (%5.2f %%)  ", p->ClassSubgs[i], p->nTotalSubgs? 100.0*p->ClassSubgs[i]/p->nTotalSubgs : 0.0 );
        printf( "R = %7d  ", p->ClassGains[i]? p->ClassSubgs[i]/p->ClassGains[i] : 9999999 );
        PRTP( "T", p->ClassTimes[i], p->timeEval );
    }
}



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