1 /**CFile****************************************************************
2 
3   FileName    [nwkMan.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Logic network representation.]
8 
9   Synopsis    [Network manager.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: nwkMan.c,v 1.1 2008/10/10 14:09:30 mjarvin Exp $]
18 
19 ***********************************************************************/
20 
21 #include "nwk.h"
22 
23 ABC_NAMESPACE_IMPL_START
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 ///                        DECLARATIONS                              ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 ///                     FUNCTION DEFINITIONS                         ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36   Synopsis    [Allocates the manager.]
37 
38   Description []
39 
40   SideEffects []
41 
42   SeeAlso     []
43 
44 ***********************************************************************/
Nwk_ManAlloc()45 Nwk_Man_t * Nwk_ManAlloc()
46 {
47     Nwk_Man_t * p;
48     p = ABC_ALLOC( Nwk_Man_t, 1 );
49     memset( p, 0, sizeof(Nwk_Man_t) );
50     p->vCis = Vec_PtrAlloc( 1000 );
51     p->vCos = Vec_PtrAlloc( 1000 );
52     p->vObjs = Vec_PtrAlloc( 1000 );
53     p->vTemp = Vec_PtrAlloc( 1000 );
54     p->nFanioPlus = 2;
55     p->pMemObjs = Aig_MmFlexStart();
56     p->pManHop = Hop_ManStart();
57     return p;
58 }
59 
60 /**Function*************************************************************
61 
62   Synopsis    [Deallocates the manager.]
63 
64   Description []
65 
66   SideEffects []
67 
68   SeeAlso     []
69 
70 ***********************************************************************/
Nwk_ManFree(Nwk_Man_t * p)71 void Nwk_ManFree( Nwk_Man_t * p )
72 {
73 //    printf( "The number of realloced nodes = %d.\n", p->nRealloced );
74     if ( p->pName )    ABC_FREE( p->pName );
75     if ( p->pSpec )    ABC_FREE( p->pSpec );
76     if ( p->vCis )     Vec_PtrFree( p->vCis );
77     if ( p->vCos )     Vec_PtrFree( p->vCos );
78     if ( p->vObjs )    Vec_PtrFree( p->vObjs );
79     if ( p->vTemp )    Vec_PtrFree( p->vTemp );
80     if ( p->pManTime ) Tim_ManStop( p->pManTime );
81     if ( p->pMemObjs ) Aig_MmFlexStop( p->pMemObjs, 0 );
82     if ( p->pManHop )  Hop_ManStop( p->pManHop );
83     ABC_FREE( p );
84 }
85 
86 /**Function*************************************************************
87 
88   Synopsis    [Prints stats of the manager.]
89 
90   Description []
91 
92   SideEffects []
93 
94   SeeAlso     []
95 
96 ***********************************************************************/
Nwk_ManPrintLutSizes(Nwk_Man_t * p,If_LibLut_t * pLutLib)97 void Nwk_ManPrintLutSizes( Nwk_Man_t * p, If_LibLut_t * pLutLib )
98 {
99     Nwk_Obj_t * pObj;
100     int i, Counters[256] = {0};
101     Nwk_ManForEachNode( p, pObj, i )
102         Counters[Nwk_ObjFaninNum(pObj)]++;
103     printf( "LUTs by size: " );
104     for ( i = 0; i <= pLutLib->LutMax; i++ )
105         printf( "%d:%d ", i, Counters[i] );
106 }
107 
108 /**Function*************************************************************
109 
110   Synopsis    [If the network is best, saves it in "best.blif" and returns 1.]
111 
112   Description [If the networks are incomparable, saves the new network,
113   returns its parameters in the internal parameter structure, and returns 1.
114   If the new network is not a logic network, quits without saving and returns 0.]
115 
116   SideEffects []
117 
118   SeeAlso     []
119 
120 ***********************************************************************/
Nwk_ManCompareAndSaveBest(Nwk_Man_t * pNtk,void * pNtl)121 int Nwk_ManCompareAndSaveBest( Nwk_Man_t * pNtk, void * pNtl )
122 {
123 //    extern void Ntl_WriteBlifLogic( Nwk_Man_t * pNtk, void * pNtl, char * pFileName );
124     extern void Nwk_ManDumpBlif( Nwk_Man_t * pNtk, char * pFileName, Vec_Ptr_t * vPiNames, Vec_Ptr_t * vPoNames );
125     static struct ParStruct {
126         char * pName;  // name of the best saved network
127         int    Depth;  // depth of the best saved network
128         int    Flops;  // flops in the best saved network
129         int    Nodes;  // nodes in the best saved network
130         int    nPis;   // the number of primary inputs
131         int    nPos;   // the number of primary outputs
132     } ParsNew, ParsBest = { 0 };
133     // free storage for the name
134     if ( pNtk == NULL )
135     {
136         ABC_FREE( ParsBest.pName );
137         return 0;
138     }
139     // get the parameters
140     ParsNew.Depth = Nwk_ManLevel( pNtk );
141     ParsNew.Flops = Nwk_ManLatchNum( pNtk );
142     ParsNew.Nodes = Nwk_ManNodeNum( pNtk );
143     ParsNew.nPis  = Nwk_ManPiNum( pNtk );
144     ParsNew.nPos  = Nwk_ManPoNum( pNtk );
145     // reset the parameters if the network has the same name
146     if (  ParsBest.pName == NULL ||
147           strcmp(ParsBest.pName, pNtk->pName) ||
148           ParsBest.Depth >  ParsNew.Depth ||
149          (ParsBest.Depth == ParsNew.Depth && ParsBest.Flops >  ParsNew.Flops) ||
150          (ParsBest.Depth == ParsNew.Depth && ParsBest.Flops == ParsNew.Flops && ParsBest.Nodes >  ParsNew.Nodes) )
151     {
152         ABC_FREE( ParsBest.pName );
153         ParsBest.pName = Abc_UtilStrsav( pNtk->pName );
154         ParsBest.Depth = ParsNew.Depth;
155         ParsBest.Flops = ParsNew.Flops;
156         ParsBest.Nodes = ParsNew.Nodes;
157         ParsBest.nPis  = ParsNew.nPis;
158         ParsBest.nPos  = ParsNew.nPos;
159         // write the network
160 //        Ntl_WriteBlifLogic( pNtk, pNtl, "best.blif" );
161 //        Nwk_ManDumpBlif( pNtk, "best_map.blif", NULL, NULL );
162         return 1;
163     }
164     return 0;
165 }
166 
167 /**Function*************************************************************
168 
169   Synopsis    []
170 
171   Description []
172 
173   SideEffects []
174 
175   SeeAlso     []
176 
177 ***********************************************************************/
Nwk_FileNameGeneric(char * FileName)178 char * Nwk_FileNameGeneric( char * FileName )
179 {
180     char * pDot, * pRes;
181     pRes = Abc_UtilStrsav( FileName );
182     if ( (pDot = strrchr( pRes, '.' )) )
183         *pDot = 0;
184     return pRes;
185 }
186 
187 /**Function*************************************************************
188 
189   Synopsis    [Marks nodes for power-optimization.]
190 
191   Description []
192 
193   SideEffects []
194 
195   SeeAlso     []
196 
197 ***********************************************************************/
Nwl_ManComputeTotalSwitching(Nwk_Man_t * pNtk)198 float Nwl_ManComputeTotalSwitching( Nwk_Man_t * pNtk )
199 {
200     extern Vec_Int_t * Saig_ManComputeSwitchProbs( Aig_Man_t * p, int nFrames, int nPref, int fProbOne );
201     Vec_Int_t * vSwitching;
202     float * pSwitching;
203     Aig_Man_t * pAig;
204     Aig_Obj_t * pObjAig;
205     Nwk_Obj_t * pObjAbc;
206     float Result = (float)0;
207     int i;
208     // strash the network
209     // map network into an AIG
210     pAig = Nwk_ManStrash( pNtk );
211     vSwitching = Saig_ManComputeSwitchProbs( pAig, 48, 16, 0 );
212     pSwitching = (float *)vSwitching->pArray;
213     Nwk_ManForEachObj( pNtk, pObjAbc, i )
214     {
215         if ( (pObjAig = Aig_Regular((Aig_Obj_t *)pObjAbc->pCopy)) )
216             Result += Nwk_ObjFanoutNum(pObjAbc) * pSwitching[pObjAig->Id];
217     }
218     Vec_IntFree( vSwitching );
219     Aig_ManStop( pAig );
220     return Result;
221 }
222 
223 /**Function*************************************************************
224 
225   Synopsis    [Prints stats of the manager.]
226 
227   Description []
228 
229   SideEffects []
230 
231   SeeAlso     []
232 
233 ***********************************************************************/
Nwk_ManPrintStats(Nwk_Man_t * pNtk,If_LibLut_t * pLutLib,int fSaveBest,int fDumpResult,int fPower,Ntl_Man_t * pNtl)234 void Nwk_ManPrintStats( Nwk_Man_t * pNtk, If_LibLut_t * pLutLib, int fSaveBest, int fDumpResult, int fPower, Ntl_Man_t * pNtl )
235 {
236 //    extern int Ntl_ManLatchNum( Ntl_Man_t * p );
237 //    extern void Ntl_ManWriteBlifLogic( Nwk_Man_t * pNtk, void * pNtl, char * pFileName );
238     if ( fSaveBest )
239         Nwk_ManCompareAndSaveBest( pNtk, pNtl );
240     if ( fDumpResult )
241     {
242         char Buffer[1000] = {0};
243         const char * pNameGen = pNtk->pSpec? Nwk_FileNameGeneric( pNtk->pSpec ) : "nameless_";
244         sprintf( Buffer, "%s_dump.blif", pNameGen );
245 //        Ntl_ManWriteBlifLogic( pNtk, pNtl, Buffer );
246 //        sprintf( Buffer, "%s_dump_map.blif", pNameGen );
247 //        Nwk_ManDumpBlif( pNtk, Buffer, NULL, NULL );
248         if ( pNtk->pSpec ) ABC_FREE( pNameGen );
249     }
250 
251     pNtk->pLutLib = pLutLib;
252     printf( "%-15s : ",      pNtk->pName );
253     printf( "pi = %5d  ",    Nwk_ManPiNum(pNtk) );
254     printf( "po = %5d  ",    Nwk_ManPoNum(pNtk) );
255     printf( "ci = %5d  ",    Nwk_ManCiNum(pNtk) );
256     printf( "co = %5d  ",    Nwk_ManCoNum(pNtk) );
257 //    printf( "lat = %5d  ",   Ntl_ManLatchNum(pNtl) );
258     printf( "node = %5d  ",  Nwk_ManNodeNum(pNtk) );
259     printf( "edge = %5d  ",  Nwk_ManGetTotalFanins(pNtk) );
260     printf( "aig = %6d  ",   Nwk_ManGetAigNodeNum(pNtk) );
261     printf( "lev = %3d  ",   Nwk_ManLevel(pNtk) );
262 //    printf( "lev2 = %3d  ",  Nwk_ManLevelBackup(pNtk) );
263     printf( "delay = %5.2f  ", Nwk_ManDelayTraceLut(pNtk) );
264     if ( fPower )
265         printf( "power = %7.2f   ", Nwl_ManComputeTotalSwitching(pNtk) );
266     Nwk_ManPrintLutSizes( pNtk, pLutLib );
267     printf( "\n" );
268 //    Nwk_ManDelayTracePrint( pNtk, pLutLib );
269     fflush( stdout );
270 }
271 
272 ////////////////////////////////////////////////////////////////////////
273 ///                       END OF FILE                                ///
274 ////////////////////////////////////////////////////////////////////////
275 
276 
277 ABC_NAMESPACE_IMPL_END
278 
279