summaryrefslogtreecommitdiffstats
path: root/src/map/pga/pgaUtil.c
blob: 73f3d381f9d626d0aa195f59260a4c472975989a (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
/**CFile****************************************************************

  FileName    [pgaUtil.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapper.]

  Synopsis    [Verious utilities.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "pgaInt.h"

#define PGA_CO_LIST_SIZE  5

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

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Returns the results of mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Pga_MappingResults( Pga_Man_t * p )
{
    Vec_Ptr_t * vResult;
    Pga_Node_t * pNode;
    int i;
    vResult = Vec_PtrAlloc( 1000 );
    Pga_ManForEachObjDirect( p, pNode, i )
    {
        // skip the CIs and nodes not used in the mapping
        if ( !pNode->Match.pCut || !pNode->nRefs )
            continue;
        pNode->Match.pCut->uSign = pNode->Id;
        Vec_PtrPush( vResult, pNode->Match.pCut );
    }
    return vResult;
}

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

  Synopsis    [Computes the maximum arrival times.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Pga_TimeComputeArrivalMax( Pga_Man_t * p )
{
    Pga_Node_t * pNode;
    float ArrivalMax;
    int i;
    ArrivalMax = -ABC_INFINITY;
    Pga_ManForEachCoDriver( p, pNode, i )
        ArrivalMax = ABC_MAX( ArrivalMax, pNode->Match.Delay );
    return ArrivalMax;
}


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

  Synopsis    [Computes required times of all nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pga_MappingComputeRequired( Pga_Man_t * p )
{
    Pga_Node_t * pNode, * pFanin;
    Cut_Cut_t * pCutBest;
    float RequiredNew;
    int i, k;
    // clean the required times of all nodes
    Pga_ManForEachObjDirect( p, pNode, i )
        pNode->Required = ABC_INFINITY;
    // get the global required times
    p->AreaGlobal     = Pga_TimeComputeArrivalMax( p );
    p->RequiredGlobal = ABC_MAX( p->AreaGlobal, p->RequiredUser );
    // set the global required times of the CO drivers
    Pga_ManForEachCoDriver( p, pNode, i )
        pNode->Required = p->RequiredGlobal;
    // propagate the required times in the reverse topological order
    Pga_ManForEachObjReverse( p, pNode, i )
    {
        // skip the CIs and nodes not used in the mapping
        if ( !pNode->Match.pCut || !pNode->nRefs )
            continue;
        // get the required time for children
        pCutBest    = pNode->Match.pCut;
        RequiredNew = pNode->Required - p->pLutDelays[pCutBest->nLeaves];
        // update the required time of the children
        for ( k = 0; k < (int)pCutBest->nLeaves; k++ )
        {
            pFanin = Pga_Node( p, pCutBest->pLeaves[k] );
            pFanin->Required = ABC_MIN( pFanin->Required, RequiredNew );
        }
    }
    // check that the required times does not contradict the arrival times
    Pga_ManForEachObjDirect( p, pNode, i )
        assert( !pNode->Match.pCut || pNode->Match.Delay < pNode->Required + p->Epsilon );

}

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

  Synopsis    [Sets references and computes area for the current mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Pga_MappingSetRefsAndArea( Pga_Man_t * p )
{
    Pga_Node_t * pNode, * pFanin;
    Cut_Cut_t * pCutBest;
    float AreaTotal;
    int i, k;
    // clean all the references
    Pga_ManForEachObjDirect( p, pNode, i )
        pNode->nRefs = 0;
    // set the references of the CO drivers
    Pga_ManForEachCoDriver( p, pNode, i )
        pNode->nRefs++;
    // go through the nodes in the reverse order
    AreaTotal = 0.0;
    Pga_ManForEachObjReverse( p, pNode, i )
    {
        // skip the CIs and nodes not used in the mapping
        if ( !pNode->Match.pCut || !pNode->nRefs )
            continue;
        // increate the reference count of the children
        pCutBest   = pNode->Match.pCut;
        AreaTotal += p->pLutAreas[pCutBest->nLeaves];
        // update the required time of the children
        for ( k = 0; k < (int)pCutBest->nLeaves; k++ )
        {
            pFanin = Pga_Node( p, pCutBest->pLeaves[k] );
            pFanin->nRefs++;
        }
    }
    return AreaTotal;
}

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

  Synopsis    [Computes switching activity of the mapping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Pga_MappingGetSwitching( Pga_Man_t * p )
{
    float Switching;
    Pga_Node_t * pNode;
    int i;
    Switching = 0;
    Pga_ManForEachObjDirect( p, pNode, i )
    {
        // skip the CIs and nodes not used in the mapping
        if ( !pNode->Match.pCut || !pNode->nRefs )
            continue;
        Switching += pNode->Switching;
    }
    return Switching;
}

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

  Synopsis    [Compares the outputs by their arrival times.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pga_MappingCompareOutputDelay( Pga_Node_t ** ppNode1, Pga_Node_t ** ppNode2 )
{
    Pga_Node_t * pNode1 = *ppNode1;
    Pga_Node_t * pNode2 = *ppNode2;
    float Arrival1 = pNode1->Match.Delay;
    float Arrival2 = pNode2->Match.Delay;
    if ( Arrival1 < Arrival2 )
        return -1;
    if ( Arrival1 > Arrival2 )
        return 1;
    return 0;
}

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

  Synopsis    [Finds given number of latest arriving COs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pga_MappingFindLatest( Pga_Man_t * p, int * pNodes, int nNodesMax )
{
    Pga_Node_t * pNodeI, * pNodeK;
    Abc_Obj_t * pObjCo;
    int nNodes, i, k, v;
    assert( Abc_NtkCoNum(p->pParams->pNtk) >= nNodesMax );
    pNodes[0] = 0;
    nNodes = 1;
//    for ( i = 1; i < p->nOutputs; i++ )
    Pga_ManForEachCoDriver( p, pNodeI, i )
    {
        for ( k = nNodes - 1; k >= 0; k-- )
        {
            pObjCo = Abc_NtkCo( p->pParams->pNtk, pNodes[k] );
            pNodeK = Pga_Node( p, Abc_ObjFaninId0(pObjCo) );
            if ( Pga_MappingCompareOutputDelay( &pNodeK, &pNodeI ) >= 0 )
                break;
        }
        if ( k == nNodesMax - 1 )
            continue;
        if ( nNodes < nNodesMax )
            nNodes++;
        for ( v = nNodes - 1; v > k+1; v-- )
            pNodes[v] = pNodes[v-1];
        pNodes[k+1] = i;
    }
}

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

  Synopsis    [Prints a bunch of latest arriving outputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pga_MappingPrintOutputArrivals( Pga_Man_t * p )
{
    int pSorted[PGA_CO_LIST_SIZE];
    Abc_Ntk_t * pNtk = p->pParams->pNtk;
    Abc_Obj_t * pObjCo;
    Pga_Node_t * pNode;
    int Limit, MaxNameSize, i;

    // determine the number of nodes to print
    Limit = (Abc_NtkCoNum(pNtk) < PGA_CO_LIST_SIZE)? Abc_NtkCoNum(pNtk) : PGA_CO_LIST_SIZE;

    // determine the order
    Pga_MappingFindLatest( p, pSorted, Limit );

    // determine max size of the node's name
    MaxNameSize = 0;
    for ( i = 0; i < Limit; i++ )
    {
        pObjCo = Abc_NtkCo( pNtk, pSorted[i] );
        if ( MaxNameSize < (int)strlen( Abc_ObjName(pObjCo) ) )
            MaxNameSize = strlen( Abc_ObjName(pObjCo) );
    }

    // print the latest outputs
    for ( i = 0; i < Limit; i++ )
    {
        // get the i-th latest output
        pObjCo = Abc_NtkCo( pNtk, pSorted[i] );
        pNode  = Pga_Node( p, pObjCo->Id );
        // print out the best arrival time
        printf( "Output  %-*s : ", MaxNameSize + 3, Abc_ObjName(pObjCo) );
        printf( "Delay = %8.2f  ", (double)pNode->Match.Delay );
        if ( Abc_ObjFaninC0(pObjCo) )
            printf( "NEG" );
        else
            printf( "POS" );
        printf( "\n" );
    }
}

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