summaryrefslogtreecommitdiffstats
path: root/src/bdd/reo/reoProfile.c
blob: 037e3202fb2c40dd11e24a0b39f8efad0629a36f (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
269
270
271
272
273
274
275
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**CFile****************************************************************

  FileName    [reoProfile.c]

  PackageName [REO: A specialized DD reordering engine.]

  Synopsis    [Procudures that compute variables profiles (nodes, width, APL).]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - October 15, 2002.]

  Revision    [$Id: reoProfile.c,v 1.0 2002/15/10 03:00:00 alanmi Exp $]

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

#include "reo.h"

ABC_NAMESPACE_IMPL_START


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


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


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

  Synopsis    [Start the profile for the BDD nodes.]

  Description [TopRef is the first level, on this the given node counts towards 
  the width of the BDDs. (In other words, it is the level of the referencing node plus 1.)]

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileNodesStart( reo_man * p )
{
    int Total, i;
    Total = 0;
    for ( i = 0; i <= p->nSupp; i++ )
    {
        p->pPlanes[i].statsCost = p->pPlanes[i].statsNodes;
        Total += p->pPlanes[i].statsNodes;
    }
    assert( Total == p->nNodesCur );
    p->nNodesBeg = p->nNodesCur;
}

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

  Synopsis    [Start the profile for the APL.]

  Description [Computes the total path length. The path length is normalized
  by dividing it by 2^|supp(f)|. To get the "real" APL, multiply by 2^|supp(f)|.
  This procedure assumes that Weight field of all nodes has been set to 0.0 
  before the call, except for the weight of the topmost node, which is set to 1.0 
  (1.0 is the probability of traversing the topmost node). This procedure 
  assigns the edge weights. Because of the equal probability of selecting 0 and 1 
  assignment at a node, the edge weights are the same for the node. 
  Instead of storing them, we store the weight of the node, which is the probability 
  of traversing the node (pUnit->Weight) during the top down evalation of the BDD. ]

  SideEffects []

  SeeAlso     []

***********************************************************************/
void reoProfileAplStart( reo_man * p )
{
    reo_unit * pER, * pTR;
    reo_unit * pUnit;
    double Res, Half;
    int i;

    // clean the weights of all nodes
    for ( i = 0; i < p->nSupp; i++ )
        for ( pUnit = p->pPlanes[i].pHead; pUnit; pUnit = pUnit->Next )
            pUnit->Weight = 0.0;
    // to assign the node weights (the probability of visiting each node)
    // we visit the node after visiting its predecessors

    // set the probability of visits to the top nodes
    for ( i = 0; i < p->nTops; i++ )
        Unit_Regular(p->pTops[i])->Weight += 1.0;

    // to compute the path length (the sum of products of edge weight by edge length)
    // we visit the nodes in any order (the above order will do)
    Res = 0.0;
    for ( i = 0; i < p->nSupp; i++ )
    {
        p->pPlanes[i].statsCost = 0.0;
        for ( pUnit = p->pPlanes[i].pHead; pUnit; pUnit = pUnit->Next )
        {
            pER  = Unit_Regular(pUnit->pE);
            pTR  = Unit_Regular(pUnit->pT);
            Half = 0.5 * pUnit->Weight;
            pER->Weight += Half;
            pTR->Weight += Half;
            // add to the path length
            p->pPlanes[i].statsCost += pUnit->Weight;
        }
        Res += p->pPlanes[i].statsCost;
    }
    p->pPlanes[p->nSupp].statsCost = 0.0;
    p->nAplBeg = p->nAplCur = Res;
}

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

  Synopsis    [Start the profile for the BDD width. Complexity of the algorithm is O(N + n).]

  Description [TopRef is the first level, on which the given node counts towards 
  the width of the BDDs. (In other words, it is the level of the referencing node plus 1.)]

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileWidthStart( reo_man * p )
{
    reo_unit * pUnit;
    int * pWidthStart;
    int * pWidthStop;
    int v;

    // allocate and clean the storage for starting and stopping levels
    pWidthStart = ABC_ALLOC( int, p->nSupp + 1 );
    pWidthStop  = ABC_ALLOC( int, p->nSupp + 1 );
    memset( pWidthStart, 0, sizeof(int) * (p->nSupp + 1) );
    memset( pWidthStop, 0, sizeof(int) * (p->nSupp + 1) );

    // go through the non-constant nodes and set the topmost level of their cofactors
    for ( v = 0; v <= p->nSupp; v++ )
    for ( pUnit = p->pPlanes[v].pHead; pUnit; pUnit = pUnit->Next )
    {
        pUnit->TopRef = REO_TOPREF_UNDEF;
        pUnit->Sign   = 0;
    }

    // add the topmost level of the width profile
    for ( v = 0; v < p->nTops; v++ )
    {
        pUnit = Unit_Regular(p->pTops[v]);
        if ( pUnit->TopRef == REO_TOPREF_UNDEF )
        {
            // set the starting level
            pUnit->TopRef = 0;
            pWidthStart[pUnit->TopRef]++;
            // set the stopping level
            if ( pUnit->lev != REO_CONST_LEVEL )
                pWidthStop[pUnit->lev+1]++;
        }
    }

    for ( v = 0; v < p->nSupp; v++ )
    for ( pUnit = p->pPlanes[v].pHead; pUnit; pUnit = pUnit->Next )
    {
        if ( pUnit->pE->TopRef == REO_TOPREF_UNDEF )
        {
            // set the starting level
            pUnit->pE->TopRef = pUnit->lev + 1;
            pWidthStart[pUnit->pE->TopRef]++;
            // set the stopping level
            if ( pUnit->pE->lev != REO_CONST_LEVEL )
                pWidthStop[pUnit->pE->lev+1]++;
        }
        if ( pUnit->pT->TopRef == REO_TOPREF_UNDEF )
        {
            // set the starting level
            pUnit->pT->TopRef = pUnit->lev + 1;
            pWidthStart[pUnit->pT->TopRef]++;
            // set the stopping level
            if ( pUnit->pT->lev != REO_CONST_LEVEL )
                pWidthStop[pUnit->pT->lev+1]++;
        }
    }

    // verify the top reference
    for ( v = 0; v < p->nSupp; v++ )
        reoProfileWidthVerifyLevel( p->pPlanes + v, v );

    // derive the profile
    p->nWidthCur = 0;
    for ( v = 0; v <= p->nSupp; v++ )
    {
        if ( v == 0 )
            p->pPlanes[v].statsWidth = pWidthStart[v] - pWidthStop[v];
        else
            p->pPlanes[v].statsWidth = p->pPlanes[v-1].statsWidth + pWidthStart[v] - pWidthStop[v];
        p->pPlanes[v].statsCost = p->pPlanes[v].statsWidth;
        p->nWidthCur += p->pPlanes[v].statsWidth;
        printf( "Level %2d: Width = %5d.\n", v, p->pPlanes[v].statsWidth );
    }
    p->nWidthBeg = p->nWidthCur;
    ABC_FREE( pWidthStart );
    ABC_FREE( pWidthStop );
}

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

  Synopsis    [Start the profile for the BDD width. Complexity of the algorithm is O(N * n).]

  Description [TopRef is the first level, on which the given node counts towards 
  the width of the BDDs. (In other words, it is the level of the referencing node plus 1.)]

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileWidthStart2( reo_man * p )
{
    reo_unit * pUnit;
    int i, v;

    // clean the profile
    for ( i = 0; i <= p->nSupp; i++ )
        p->pPlanes[i].statsWidth = 0;
    
    // clean the node structures
    for ( v = 0; v <= p->nSupp; v++ )
    for ( pUnit = p->pPlanes[v].pHead; pUnit; pUnit = pUnit->Next )
    {
        pUnit->TopRef = REO_TOPREF_UNDEF;
        pUnit->Sign   = 0;
    }

    // set the topref to the topmost nodes
    for ( i = 0; i < p->nTops; i++ )
        Unit_Regular(p->pTops[i])->TopRef = 0;

    // go through the non-constant nodes and set the topmost level of their cofactors
    for ( i = 0; i < p->nSupp; i++ )
        for ( pUnit = p->pPlanes[i].pHead; pUnit; pUnit = pUnit->Next )
        {
            if ( pUnit->pE->TopRef > i+1 )
                 pUnit->pE->TopRef = i+1;
            if ( pUnit->pT->TopRef > i+1 )
                 pUnit->pT->TopRef = i+1;
        }

    // verify the top reference
    for ( i = 0; i < p->nSupp; i++ )
        reoProfileWidthVerifyLevel( p->pPlanes + i, i );

    // compute the profile for the internal nodes
    for ( i = 0; i < p->nSupp; i++ )
        for ( pUnit = p->pPlanes[i].pHead; pUnit; pUnit = pUnit->Next )
            for ( v = pUnit->TopRef; v <= pUnit->lev; v++ )
                p->pPlanes[v].statsWidth++;

    // compute the profile for the constant nodes
    for ( pUnit = p->pPlanes[p->nSupp].pHead; pUnit; pUnit = pUnit->Next )
        for ( v = pUnit->TopRef; v <= p->nSupp; v++ )
            p->pPlanes[v].statsWidth++;

    // get the width cost
    p->nWidthCur = 0;
    for ( i = 0; i <= p->nSupp; i++ )
    {
        p->pPlanes[i].statsCost = p->pPlanes[i].statsWidth;
        p->nWidthCur           += p->pPlanes[i].statsWidth;
    }
    p->nWidthBeg = p->nWidthCur;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileNodesPrint( reo_man * p )
{
    printf( "NODES: Total = %6d. Average = %6.2f.\n", p->nNodesCur, p->nNodesCur / (float)p->nSupp );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileAplPrint( reo_man * p )
{
    printf( "APL: Total = %8.2f. Average =%6.2f.\n", p->nAplCur, p->nAplCur / (float)p->nSupp );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileWidthPrint( reo_man * p )
{
    int WidthMax;
    int TotalWidth;
    int i;

    WidthMax   = 0;
    TotalWidth = 0;
    for ( i = 0; i <= p->nSupp; i++ )
    {
        printf( "Level = %2d. Width = %3d.\n", i, p->pPlanes[i].statsWidth );
        if ( WidthMax < p->pPlanes[i].statsWidth )
             WidthMax = p->pPlanes[i].statsWidth;
        TotalWidth += p->pPlanes[i].statsWidth;
    }
    assert( p->nWidthCur == TotalWidth );
    printf( "WIDTH: " );
    printf( "Maximum = %5d.  ", WidthMax );
    printf( "Total = %7d.  ", p->nWidthCur );
    printf( "Average = %6.2f.\n", TotalWidth / (float)p->nSupp );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
void reoProfileWidthVerifyLevel( reo_plane * pPlane, int Level )
{
    reo_unit * pUnit;
    for ( pUnit = pPlane->pHead; pUnit; pUnit = pUnit->Next )
    {
        assert( pUnit->TopRef     <= Level );
        assert( pUnit->pE->TopRef <= Level + 1 );
        assert( pUnit->pT->TopRef <= Level + 1 );
    }
}

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

ABC_NAMESPACE_IMPL_END