summaryrefslogtreecommitdiffstats
path: root/src/base/seq/seqRetIter.c
blob: 5c65e72ebb77a8da3dcd92fda82b3819ac162e7f (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
/**CFile****************************************************************

  FileName    [seqRetIter.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Construction and manipulation of sequential AIGs.]

  Synopsis    [Iterative delay computation in FPGA mapping/retiming package.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "seqInt.h"
#include "main.h"
#include "fpga.h"

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

static float Seq_NtkMappingSearch_rec( Abc_Ntk_t * pNtk, float FiMin, float FiMax, float Delta, int fVerbose );
static int   Seq_NtkMappingForPeriod( Abc_Ntk_t * pNtk, float Fi, int fVerbose );
static int   Seq_NtkNodeUpdateLValue( Abc_Obj_t * pObj, float Fi, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vDelays );
static void  Seq_NodeRetimeSetLag_rec( Abc_Obj_t * pNode, char Lag );

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

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

  Synopsis    [Computes the retiming lags for arbitrary network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Seq_NtkRetimeDelayLags( Abc_Ntk_t * pNtkOld, Abc_Ntk_t * pNtk, int fVerbose )
{
    Abc_Seq_t * p = pNtk->pManFunc;
    Abc_Obj_t * pNode;
    float FiMax, FiBest, Delta;
    int i, RetValue;
    char NodeLag;

    assert( Abc_NtkIsSeq( pNtk ) );

    // the root AND gates and node delay should be assigned
    assert( p->vMapAnds );
    assert( p->vMapCuts );
    assert( p->vMapDelays );

    // guess the upper bound on the clock period
    if ( Abc_NtkHasMapping(pNtkOld) )
    {
        // assign the accuracy for min-period computation
        Delta = Mio_LibraryReadDelayNand2Max(Abc_FrameReadLibGen());
        if ( Delta == 0.0 )
        {
            Delta = Mio_LibraryReadDelayAnd2Max(Abc_FrameReadLibGen());
            if ( Delta == 0.0 )
            {
                printf( "Cannot retime/map if the library does not have NAND2 or AND2.\n" );
                return;
            }
        }
        // get the upper bound on the clock period
        FiMax = Delta * (2 + Seq_NtkLevelMax(pNtk));
        Delta /= 2;
    }
    else
    {
        FiMax = (float)2.0 + Abc_NtkGetLevelNum(pNtkOld);
        Delta = 1;
    }

    // make sure this clock period is feasible
    assert( Seq_NtkMappingForPeriod( pNtk, FiMax, fVerbose ) );

    // search for the optimal clock period between 0 and nLevelMax
    FiBest = Seq_NtkMappingSearch_rec( pNtk, 0.0, FiMax, Delta, fVerbose );

    // recompute the best l-values
    RetValue = Seq_NtkMappingForPeriod( pNtk, FiBest, fVerbose );
    assert( RetValue );

    // write the retiming lags for both phases of each node
    Vec_StrFill( p->vLags,  p->nSize, 0 );
    Vec_PtrForEachEntry( p->vMapAnds, pNode, i )
    {
        NodeLag = Seq_NodeComputeLagFloat( Seq_NodeGetLValueP(pNode), FiBest );
//        Seq_NodeSetLag( pNode, NodeLag );
        Seq_NodeRetimeSetLag_rec( pNode, NodeLag );
    }

    // print the result
    if ( fVerbose )
        printf( "The best clock period is %6.2f.\n", FiBest );
}

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

  Synopsis    [Performs binary search for the optimal clock period.]

  Description [Assumes that FiMin is infeasible while FiMax is feasible.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Seq_NtkMappingSearch_rec( Abc_Ntk_t * pNtk, float FiMin, float FiMax, float Delta, int fVerbose )
{
    float Median;
    assert( FiMin < FiMax );
    if ( FiMin + Delta >= FiMax )
        return FiMax;
    Median = FiMin + (FiMax - FiMin)/2;
    if ( Seq_NtkMappingForPeriod( pNtk, Median, fVerbose ) )
        return Seq_NtkMappingSearch_rec( pNtk, FiMin, Median, Delta, fVerbose ); // Median is feasible
    else 
        return Seq_NtkMappingSearch_rec( pNtk, Median, FiMax, Delta, fVerbose ); // Median is infeasible
}

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

  Synopsis    [Returns 1 if retiming with this clock period is feasible.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Seq_NtkMappingForPeriod( Abc_Ntk_t * pNtk, float Fi, int fVerbose )
{
    Abc_Seq_t * p = pNtk->pManFunc;
    Vec_Ptr_t * vLeaves, * vDelays;
    Abc_Obj_t * pObj;
    int i, c, RetValue, fChange, Counter;
    char * pReason = "";

    // set l-values of all nodes to be minus infinity
    Vec_IntFill( p->vLValues,  p->nSize, -ABC_INFINITY );

    // set l-values of constants and PIs
    pObj = Abc_NtkObj( pNtk, 0 );
    Seq_NodeSetLValueP( pObj, 0.0 );
    Abc_NtkForEachPi( pNtk, pObj, i )
        Seq_NodeSetLValueP( pObj, 0.0 );

    // update all values iteratively
    Counter = 0;
    for ( c = 0; c < p->nMaxIters; c++ )
    {
        fChange = 0;
        Vec_PtrForEachEntry( p->vMapAnds, pObj, i )
        {
            Counter++;
            vLeaves = Vec_VecEntry( p->vMapCuts, i );
            vDelays = Vec_VecEntry( p->vMapDelays, i );
            RetValue = Seq_NtkNodeUpdateLValue( pObj, Fi, vLeaves, vDelays );
            if ( RetValue == SEQ_UPDATE_YES ) 
                fChange = 1;
        }
        Abc_NtkForEachPo( pNtk, pObj, i )
        {
            RetValue = Seq_NtkNodeUpdateLValue( pObj, Fi, NULL, NULL );
            if ( RetValue == SEQ_UPDATE_FAIL )
                break;
        }
        if ( RetValue == SEQ_UPDATE_FAIL )
            break;
        if ( fChange == 0 )
            break;
    }
    if ( c == p->nMaxIters )
    {
        RetValue = SEQ_UPDATE_FAIL;
        pReason = "(timeout)";
    }
    else
        c++;

    // report the results
    if ( fVerbose )
    {
        if ( RetValue == SEQ_UPDATE_FAIL )
            printf( "Period = %6.2f.  Iterations = %3d.  Updates = %10d.    Infeasible %s\n", Fi, c, Counter, pReason );
        else
            printf( "Period = %6.2f.  Iterations = %3d.  Updates = %10d.      Feasible\n",    Fi, c, Counter );
    }
    return RetValue != SEQ_UPDATE_FAIL;
}

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

  Synopsis    [Computes the l-value of the node.]

  Description [The node can be internal or a PO.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Seq_NtkNodeUpdateLValue( Abc_Obj_t * pObj, float Fi, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vDelays )
{
    Abc_Seq_t * p = pObj->pNtk->pManFunc;
    float lValueOld, lValueNew, lValueCur, lValuePin;
    unsigned SeqEdge;
    Abc_Obj_t * pLeaf;
    int i;

    assert( !Abc_ObjIsPi(pObj) );
    assert( Abc_ObjFaninNum(pObj) > 0 );
    // consider the case of the PO
    if ( Abc_ObjIsPo(pObj) )
    {
        lValueCur = Seq_NodeGetLValueP(Abc_ObjFanin0(pObj)) - Fi * Seq_ObjFaninL0(pObj);
        return (lValueCur > Fi + p->fEpsilon)? SEQ_UPDATE_FAIL : SEQ_UPDATE_NO;
    }
    // get the new arrival time of the cut output
    lValueNew = -ABC_INFINITY;
    Vec_PtrForEachEntry( vLeaves, pLeaf, i )
    {
        SeqEdge   = (unsigned)pLeaf;
        pLeaf     = Abc_NtkObj( pObj->pNtk, SeqEdge >> 8 );
        lValueCur = Seq_NodeGetLValueP(pLeaf) - Fi * (SeqEdge & 255);
        lValuePin = Abc_Int2Float( (int)Vec_PtrEntry(vDelays, i) );
        if ( lValueNew < lValuePin + lValueCur )
            lValueNew = lValuePin + lValueCur;
    }
    // compare 
    lValueOld = Seq_NodeGetLValueP( pObj );
    if ( lValueNew <= lValueOld + p->fEpsilon )
        return SEQ_UPDATE_NO;
    // update the values
    if ( lValueNew > lValueOld + p->fEpsilon )
        Seq_NodeSetLValueP( pObj, lValueNew );
    return SEQ_UPDATE_YES;
}



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

  Synopsis    [Add sequential edges.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Seq_NodeRetimeSetLag_rec( Abc_Obj_t * pNode, char Lag )
{
    if ( pNode->pCopy )
        return;
    Seq_NodeRetimeSetLag_rec( Abc_ObjFanin0(pNode), Lag );
    Seq_NodeRetimeSetLag_rec( Abc_ObjFanin1(pNode), Lag );
    Seq_NodeSetLag( pNode, Lag );
}


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