nwkMan.c 8.92 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [nwkMan.c]
Alan Mishchenko committed
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7 8
  PackageName [Logic network representation.]
 
Alan Mishchenko committed
9 10 11 12 13 14 15 16
  Synopsis    [Network manager.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: nwkMan.c,v 1.1 2008/10/10 14:09:30 mjarvin Exp $]
Alan Mishchenko committed
18 19 20

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

Alan Mishchenko committed
21
#include "nwk.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

Alan Mishchenko committed
36
  Synopsis    [Allocates the manager.]
Alan Mishchenko committed
37 38 39 40 41 42 43 44

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
Nwk_Man_t * Nwk_ManAlloc()
Alan Mishchenko committed
46
{
Alan Mishchenko committed
47
    Nwk_Man_t * p;
Alan Mishchenko committed
48
    p = ABC_ALLOC( Nwk_Man_t, 1 );
Alan Mishchenko committed
49
    memset( p, 0, sizeof(Nwk_Man_t) );
Alan Mishchenko committed
50 51 52 53
    p->vCis = Vec_PtrAlloc( 1000 );
    p->vCos = Vec_PtrAlloc( 1000 );
    p->vObjs = Vec_PtrAlloc( 1000 );
    p->vTemp = Vec_PtrAlloc( 1000 );
Alan Mishchenko committed
54
    p->nFanioPlus = 2;
Alan Mishchenko committed
55
    p->pMemObjs = Aig_MmFlexStart();
Alan Mishchenko committed
56
    p->pManHop = Hop_ManStart();
Alan Mishchenko committed
57 58 59 60 61
    return p;
}

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

Alan Mishchenko committed
62
  Synopsis    [Deallocates the manager.]
Alan Mishchenko committed
63 64 65 66 67 68 69 70

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
71
void Nwk_ManFree( Nwk_Man_t * p )
Alan Mishchenko committed
72
{
Alan Mishchenko committed
73
//    printf( "The number of realloced nodes = %d.\n", p->nRealloced );
Alan Mishchenko committed
74 75
    if ( p->pName )    ABC_FREE( p->pName );
    if ( p->pSpec )    ABC_FREE( p->pSpec );
Alan Mishchenko committed
76 77 78 79 80 81
    if ( p->vCis )     Vec_PtrFree( p->vCis );
    if ( p->vCos )     Vec_PtrFree( p->vCos );
    if ( p->vObjs )    Vec_PtrFree( p->vObjs );
    if ( p->vTemp )    Vec_PtrFree( p->vTemp );
    if ( p->pManTime ) Tim_ManStop( p->pManTime );
    if ( p->pMemObjs ) Aig_MmFlexStop( p->pMemObjs, 0 );
Alan Mishchenko committed
82
    if ( p->pManHop )  Hop_ManStop( p->pManHop );
Alan Mishchenko committed
83
    ABC_FREE( p );
Alan Mishchenko committed
84 85 86 87
}

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

Alan Mishchenko committed
88
  Synopsis    [Prints stats of the manager.]
Alan Mishchenko committed
89 90 91 92 93 94 95 96

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
97
void Nwk_ManPrintLutSizes( Nwk_Man_t * p, If_LibLut_t * pLutLib )
Alan Mishchenko committed
98 99 100 101 102 103 104 105 106 107 108 109
{
    Nwk_Obj_t * pObj;
    int i, Counters[256] = {0};
    Nwk_ManForEachNode( p, pObj, i )
        Counters[Nwk_ObjFaninNum(pObj)]++;
    printf( "LUTs by size: " );
    for ( i = 0; i <= pLutLib->LutMax; i++ )
        printf( "%d:%d ", i, Counters[i] );
}

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

Alan Mishchenko committed
110 111 112 113 114 115 116 117 118 119 120 121 122
  Synopsis    [If the network is best, saves it in "best.blif" and returns 1.]

  Description [If the networks are incomparable, saves the new network, 
  returns its parameters in the internal parameter structure, and returns 1.
  If the new network is not a logic network, quits without saving and returns 0.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Nwk_ManCompareAndSaveBest( Nwk_Man_t * pNtk, void * pNtl )
{
123
//    extern void Ntl_WriteBlifLogic( Nwk_Man_t * pNtk, void * pNtl, char * pFileName );
Alan Mishchenko committed
124
    extern void Nwk_ManDumpBlif( Nwk_Man_t * pNtk, char * pFileName, Vec_Ptr_t * vPiNames, Vec_Ptr_t * vPoNames );
Alan Mishchenko committed
125 126 127 128 129 130 131 132
    static struct ParStruct {
        char * pName;  // name of the best saved network
        int    Depth;  // depth of the best saved network
        int    Flops;  // flops in the best saved network 
        int    Nodes;  // nodes in the best saved network
        int    nPis;   // the number of primary inputs
        int    nPos;   // the number of primary outputs
    } ParsNew, ParsBest = { 0 };
Alan Mishchenko committed
133
    // free storage for the name
Alan Mishchenko committed
134 135
    if ( pNtk == NULL )
    {
Alan Mishchenko committed
136
        ABC_FREE( ParsBest.pName );
Alan Mishchenko committed
137 138 139 140 141 142 143 144 145
        return 0;
    }
    // get the parameters
    ParsNew.Depth = Nwk_ManLevel( pNtk );
    ParsNew.Flops = Nwk_ManLatchNum( pNtk );
    ParsNew.Nodes = Nwk_ManNodeNum( pNtk );
    ParsNew.nPis  = Nwk_ManPiNum( pNtk );
    ParsNew.nPos  = Nwk_ManPoNum( pNtk );
    // reset the parameters if the network has the same name
Alan Mishchenko committed
146 147 148 149 150
    if (  ParsBest.pName == NULL ||
          strcmp(ParsBest.pName, pNtk->pName) ||
          ParsBest.Depth >  ParsNew.Depth ||
         (ParsBest.Depth == ParsNew.Depth && ParsBest.Flops >  ParsNew.Flops) ||
         (ParsBest.Depth == ParsNew.Depth && ParsBest.Flops == ParsNew.Flops && ParsBest.Nodes >  ParsNew.Nodes) )
Alan Mishchenko committed
151
    {
Alan Mishchenko committed
152
        ABC_FREE( ParsBest.pName );
153
        ParsBest.pName = Abc_UtilStrsav( pNtk->pName );
Alan Mishchenko committed
154 155 156 157 158
        ParsBest.Depth = ParsNew.Depth; 
        ParsBest.Flops = ParsNew.Flops; 
        ParsBest.Nodes = ParsNew.Nodes; 
        ParsBest.nPis  = ParsNew.nPis; 
        ParsBest.nPos  = ParsNew.nPos;
Alan Mishchenko committed
159
        // write the network
160
//        Ntl_WriteBlifLogic( pNtk, pNtl, "best.blif" );
Alan Mishchenko committed
161
//        Nwk_ManDumpBlif( pNtk, "best_map.blif", NULL, NULL );
Alan Mishchenko committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        return 1;
    }
    return 0;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Nwk_FileNameGeneric( char * FileName )
{
Alan Mishchenko committed
180
    char * pDot, * pRes;
181
    pRes = Abc_UtilStrsav( FileName );
Alan Mishchenko committed
182 183
    if ( (pDot = strrchr( pRes, '.' )) )
        *pDot = 0;
Alan Mishchenko committed
184 185 186 187 188
    return pRes;
}

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

Alan Mishchenko committed
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
  Synopsis    [Marks nodes for power-optimization.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Nwl_ManComputeTotalSwitching( Nwk_Man_t * pNtk )
{
    extern Vec_Int_t * Saig_ManComputeSwitchProbs( Aig_Man_t * p, int nFrames, int nPref, int fProbOne );
    Vec_Int_t * vSwitching;
    float * pSwitching;
    Aig_Man_t * pAig;
    Aig_Obj_t * pObjAig;
    Nwk_Obj_t * pObjAbc;
    float Result = (float)0;
    int i;
    // strash the network
    // map network into an AIG
    pAig = Nwk_ManStrash( pNtk );
    vSwitching = Saig_ManComputeSwitchProbs( pAig, 48, 16, 0 );
    pSwitching = (float *)vSwitching->pArray;
    Nwk_ManForEachObj( pNtk, pObjAbc, i )
    {
215
        if ( (pObjAig = Aig_Regular((Aig_Obj_t *)pObjAbc->pCopy)) )
Alan Mishchenko committed
216 217 218 219 220 221
            Result += Nwk_ObjFanoutNum(pObjAbc) * pSwitching[pObjAig->Id];
    }
    Vec_IntFree( vSwitching );
    Aig_ManStop( pAig );
    return Result;
}
222
 
Alan Mishchenko committed
223 224
/**Function*************************************************************

Alan Mishchenko committed
225 226 227 228 229 230 231 232 233
  Synopsis    [Prints stats of the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
234
void Nwk_ManPrintStats( Nwk_Man_t * pNtk, If_LibLut_t * pLutLib, int fSaveBest, int fDumpResult, int fPower, Ntl_Man_t * pNtl )
Alan Mishchenko committed
235
{
236 237
//    extern int Ntl_ManLatchNum( Ntl_Man_t * p );
//    extern void Ntl_ManWriteBlifLogic( Nwk_Man_t * pNtk, void * pNtl, char * pFileName );
Alan Mishchenko committed
238 239 240 241 242
    if ( fSaveBest )
        Nwk_ManCompareAndSaveBest( pNtk, pNtl );
    if ( fDumpResult )
    {
        char Buffer[1000] = {0};
243
        const char * pNameGen = pNtk->pSpec? Nwk_FileNameGeneric( pNtk->pSpec ) : "nameless_";
Alan Mishchenko committed
244
        sprintf( Buffer, "%s_dump.blif", pNameGen );
245
//        Ntl_ManWriteBlifLogic( pNtk, pNtl, Buffer );
Alan Mishchenko committed
246 247
//        sprintf( Buffer, "%s_dump_map.blif", pNameGen );
//        Nwk_ManDumpBlif( pNtk, Buffer, NULL, NULL );
Alan Mishchenko committed
248
        if ( pNtk->pSpec ) ABC_FREE( pNameGen );
Alan Mishchenko committed
249 250 251 252 253 254 255 256
    }

    pNtk->pLutLib = pLutLib;
    printf( "%-15s : ",      pNtk->pName );
    printf( "pi = %5d  ",    Nwk_ManPiNum(pNtk) );
    printf( "po = %5d  ",    Nwk_ManPoNum(pNtk) );
    printf( "ci = %5d  ",    Nwk_ManCiNum(pNtk) );
    printf( "co = %5d  ",    Nwk_ManCoNum(pNtk) );
257
//    printf( "lat = %5d  ",   Ntl_ManLatchNum(pNtl) );
Alan Mishchenko committed
258 259 260 261 262
    printf( "node = %5d  ",  Nwk_ManNodeNum(pNtk) );
    printf( "edge = %5d  ",  Nwk_ManGetTotalFanins(pNtk) );
    printf( "aig = %6d  ",   Nwk_ManGetAigNodeNum(pNtk) );
    printf( "lev = %3d  ",   Nwk_ManLevel(pNtk) );
//    printf( "lev2 = %3d  ",  Nwk_ManLevelBackup(pNtk) );
Alan Mishchenko committed
263 264 265
    printf( "delay = %5.2f  ", Nwk_ManDelayTraceLut(pNtk) );
    if ( fPower )
        printf( "power = %7.2f   ", Nwl_ManComputeTotalSwitching(pNtk) );
Alan Mishchenko committed
266
    Nwk_ManPrintLutSizes( pNtk, pLutLib );
Alan Mishchenko committed
267
    printf( "\n" );
Alan Mishchenko committed
268
//    Nwk_ManDelayTracePrint( pNtk, pLutLib );
Alan Mishchenko committed
269
    fflush( stdout );
Alan Mishchenko committed
270 271 272 273 274 275 276
}

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


277 278
ABC_NAMESPACE_IMPL_END