summaryrefslogtreecommitdiffstats
path: root/src/map/pga/pgaMatch.c
blob: f9f6b5c413294f9fde56aa06f77ea3bfbcdf38ee (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
371
372
373
374
375
376
377
378
/**CFile****************************************************************

  FileName    [pgaMatch.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapper.]

  Synopsis    [Mapping procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "pgaInt.h"

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

static char * s_Modes[4] = { "Delay", "Flow", "Area", "Switch" };

static int Pga_MappingMatchNode( Pga_Man_t * p, int NodeId, Cut_Cut_t * pList, int Mode );

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

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

  Synopsis    [Performs mapping for delay, area-flow, area, switching.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pga_MappingMatches( Pga_Man_t * p, int Mode )
{
    ProgressBar * pProgress;
    Abc_Ntk_t * pNtk;
    Abc_Obj_t * pObj;
    Cut_Cut_t * pList;
    int i, clk;

    assert( Mode >= 0 && Mode <= 2 );

    // match LUTs with nodes in the topological order
    pNtk = p->pParams->pNtk;
    pProgress = Extra_ProgressBarStart( stdout, Abc_NtkObjNumMax(pNtk) );
    Abc_NtkForEachObj( pNtk, pObj, i )
    {
        // skip the CIs
        if ( Abc_ObjIsCi(pObj) )
            continue;
        // when we reached a CO, it is time to deallocate the cuts
        if ( Abc_ObjIsCo(pObj) )
        {
            if ( p->pParams->fDropCuts )
                Cut_NodeTryDroppingCuts( p->pManCut, Abc_ObjFaninId0(pObj) );
            continue;
        }
        // skip constant node, it has no cuts
        if ( Abc_NodeIsConst(pObj) )
            continue;
        // get the cuts
clk = clock();
        pList = Abc_NodeGetCutsRecursive( p->pManCut, pObj );
p->timeCuts += clock() - clk;
        // match the node
        Pga_MappingMatchNode( p, pObj->Id, pList, Mode );
        Extra_ProgressBarUpdate( pProgress, i, s_Modes[Mode] );
    }
    Extra_ProgressBarStop( pProgress );
}




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

  Synopsis    [Computes the match of the cut.]

  Description [Returns 1 if feasible.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline float Pga_CutGetArrival( Pga_Man_t * p, Cut_Cut_t * pCut )
{
    float DelayCur, DelayWorst;
    unsigned i;
    assert( pCut->nLeaves > 1 );
    DelayWorst = -ABC_INFINITY;
    for ( i = 0; i < pCut->nLeaves; i++ )
    {
        DelayCur = Pga_Node(p, pCut->pLeaves[i])->Match.Delay;
        if ( DelayWorst < DelayCur )
             DelayWorst = DelayCur;
    }
    DelayWorst += p->pLutDelays[pCut->nLeaves];
    return DelayWorst;
}

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

  Synopsis    [Computes the match of the cut.]

  Description [Returns 1 if feasible.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline float Pga_CutGetAreaFlow( Pga_Man_t * p, Cut_Cut_t * pCut )
{
    float Flow;
    Pga_Node_t * pNode;
    unsigned i;
    assert( pCut->nLeaves > 1 );
    Flow = p->pLutAreas[pCut->nLeaves];
    for ( i = 0; i < pCut->nLeaves; i++ )
    {
        pNode = Pga_Node(p, pCut->pLeaves[i]);
        assert( pNode->EstRefs > 0 );
        Flow += pNode->Match.Area / pNode->EstRefs;
    }
    return Flow;
}

/**function*************************************************************

  synopsis    [References the cut.]

  description [This procedure is similar to the procedure NodeReclaim.]
               
  sideeffects []

  seealso     []

***********************************************************************/
float Pga_CutRef( Pga_Man_t * p, Pga_Node_t * pNode, Cut_Cut_t * pCut )
{
    Pga_Node_t * pFanin;
    float aArea;
    unsigned i;
    // start the area of this cut
    aArea = p->pLutAreas[pCut->nLeaves];
    // go through the children
    for ( i = 0; i < pCut->nLeaves; i++ )
    {
        pFanin = Pga_Node(p, pCut->pLeaves[i]);
        assert( pFanin->nRefs >= 0 );
        if ( pFanin->nRefs++ > 0 )  
            continue;
        if ( pFanin->Match.pCut == NULL ) 
            continue;
        aArea += Pga_CutRef( p, pFanin, pFanin->Match.pCut );
    }
    return aArea;
}

/**function*************************************************************

  synopsis    [Dereferences the cut.]

  description [This procedure is similar to the procedure NodeRecusiveDeref.]
               
  sideeffects []

  seealso     []

***********************************************************************/
float Pga_CutDeref( Pga_Man_t * p, Pga_Node_t * pNode, Cut_Cut_t * pCut )
{
    Pga_Node_t * pFanin;
    float aArea;
    unsigned i;
    // start the area of this cut
    aArea = p->pLutAreas[pCut->nLeaves];
    // go through the children
    for ( i = 0; i < pCut->nLeaves; i++ )
    {
        pFanin = Pga_Node(p, pCut->pLeaves[i]);
        assert( pFanin->nRefs > 0 );
        if ( --pFanin->nRefs > 0 )  
            continue;
        if ( pFanin->Match.pCut == NULL ) 
            continue;
        aArea += Pga_CutDeref( p, pFanin, pFanin->Match.pCut );
    }
    return aArea;
}

/**function*************************************************************

  synopsis    [Computes the exact area associated with the cut.]

  description [Assumes that the cut is deferenced.]
               
  sideeffects []

  seealso     []

***********************************************************************/
static inline float Pga_CutGetAreaDerefed( Pga_Man_t * p, Pga_Node_t * pNode, Cut_Cut_t * pCut )
{
    float aResult, aResult2;
    assert( pCut->nLeaves > 1 );
    aResult2 = Pga_CutRef( p, pNode, pCut );
    aResult  = Pga_CutDeref( p, pNode, pCut );
    assert( aResult == aResult2 );
    return aResult;
}



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

  Synopsis    [Computes the match of the cut.]

  Description [Returns 1 if feasible.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Pga_MappingMatchCut( Pga_Man_t * p, Pga_Node_t * pNode, Cut_Cut_t * pCut, int Mode, Pga_Match_t * pMatch )
{
    // compute the arrival time of the cut and its area flow
    pMatch->Delay = Pga_CutGetArrival( p, pCut );
    // drop the cut if it does not meet the required times
    if ( pMatch->Delay > pNode->Required + p->Epsilon )
        return 0;
    // get the second parameter
    if ( Mode == 0 || Mode == 1 )
        pMatch->Area = Pga_CutGetAreaFlow( p, pCut );
    else if ( Mode == 2 )
        pMatch->Area = Pga_CutGetAreaDerefed( p, pNode, pCut );
//    else if ( Mode == 3 )
//        pMatch->Area = Pga_CutGetSwitching( p, pNode, pCut );
    // if no cut is assigned, use the current one
    pMatch->pCut = pCut;
    return 1;
}

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

  Synopsis    [Compares two matches.]

  Description [Returns 1 if the second match is better.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Pga_MappingCompareMatches( Pga_Man_t * p, Pga_Match_t * pMatchBest, Pga_Match_t * pMatchCur, int Mode )
{
    if ( pMatchBest->pCut == NULL )
        return 1;
    if ( Mode == 0 )
    {
        // compare delays
        if ( pMatchBest->Delay < pMatchCur->Delay - p->Epsilon )
            return 0;
        if ( pMatchBest->Delay > pMatchCur->Delay + p->Epsilon )
            return 1;
        // compare areas
        if ( pMatchBest->Area < pMatchCur->Area - p->Epsilon )
            return 0;
        if ( pMatchBest->Area > pMatchCur->Area + p->Epsilon )
            return 1;
        // if equal, do not update
        return 0;
    }
    else
    {
        // compare areas
        if ( pMatchBest->Area < pMatchCur->Area - p->Epsilon )
            return 0;
        if ( pMatchBest->Area > pMatchCur->Area + p->Epsilon )
            return 1;
        // compare delays
        if ( pMatchBest->Delay < pMatchCur->Delay - p->Epsilon )
            return 0;
        if ( pMatchBest->Delay > pMatchCur->Delay + p->Epsilon )
            return 1;
        // if equal, do not update
        return 0;
    }
}


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

  Synopsis    [Computes the best matching for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pga_MappingMatchNode( Pga_Man_t * p, int NodeId, Cut_Cut_t * pList, int Mode )
{
    Pga_Match_t MatchCur,  * pMatchCur = &MatchCur;
    Pga_Match_t MatchBest, * pMatchBest = &MatchBest;
    Pga_Node_t * pNode;
    Cut_Cut_t * pCut;

    // get the mapping node
    pNode = Pga_Node( p, NodeId );

    // prepare for mapping
    if ( Mode == 0 )
        pNode->EstRefs = (float)pNode->nRefs;
    else if ( Mode == 1 )
        pNode->EstRefs = (float)((2.0 * pNode->EstRefs + pNode->nRefs) / 3.0);
    else if ( Mode == 2 && pNode->nRefs > 0 )
        Pga_CutDeref( p, pNode, pNode->Match.pCut );
//    else if ( Mode == 3 && pNode->nRefs > 0 )
//        Pga_CutDerefSwitch( p, pNode, pNode->Match.pCut );

    // start the best match
    pMatchBest->pCut = NULL;

    // go through the other cuts
    assert( pList->pNext );
    for ( pCut = pList->pNext; pCut; pCut = pCut->pNext )
    {
        // compute match for this cut
        if ( !Pga_MappingMatchCut( p, pNode, pCut, Mode, pMatchCur ) )
            continue;
        // compare matches
        if ( !Pga_MappingCompareMatches( p, pMatchBest, pMatchCur, Mode ) )
            continue;
        // the best match should be updated
        *pMatchBest = *pMatchCur;
    }

    // make sure the match is found
    if ( pMatchBest->pCut != NULL )
        pNode->Match = *pMatchBest;
    else
    {
        assert( 0 );
//        Pga_MappingMatchCut( p, pNode, pCut, Mode, pNode->Match );
    }

    // reference the best cut
    if ( Mode == 2 && pNode->nRefs > 0 )
        Pga_CutRef( p, pNode, pNode->Match.pCut );
//    else if ( Mode == 3 && pNode->nRefs > 0 )
//        Pga_CutRefSwitch( p, pNode, pNode->Match.pCut );
    return 1;
}



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