giaEmbed.c 61 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [giaEmbed.c]
Alan Mishchenko committed
4 5 6 7 8 9 10 11 12 13 14 15 16

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Logic network derived from AIG.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: giaEmbed.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20 21 22

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

#include <math.h>
#include "gia.h"
23
#include "aig/ioa/ioa.h"
24 25 26

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
27 28 29 30

/* 
    The code is based on the paper by D. Harel and Y. Koren, 
    "Graph drawing by high-dimensional embedding", 
Alan Mishchenko committed
31
    J. Graph Algs & Apps, Vol 8(2), pp. 195-217 (2004).
Alan Mishchenko committed
32 33 34
    http://www.emis.de/journals/JGAA/accepted/2004/HarelKoren2004.8.2.pdf 

    Iterative refinement is described in the paper: F. A. Aloul, I. L. Markov, and K. A. Sakallah.
35
    "FORCE: A Fast and Easy-To-Implement Variable-Ordering Heuristic", Proc. GLSVLSI 03.
Alan Mishchenko committed
36
    http://www.eecs.umich.edu/~imarkov/pubs/conf/glsvlsi03-force.pdf
Alan Mishchenko committed
37 38 39 40 41 42
*/

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

Alan Mishchenko committed
43 44
#define GIA_PLACE_SIZE 0x7fff 
// objects will be placed in box [0, GIA_PLACE_SIZE] x [0, GIA_PLACE_SIZE]
Alan Mishchenko committed
45

Alan Mishchenko committed
46 47
typedef float  Emb_Dat_t;

Alan Mishchenko committed
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
typedef struct Emb_Obj_t_ Emb_Obj_t;
struct Emb_Obj_t_
{
    unsigned       fCi      :  1;    // terminal node CI
    unsigned       fCo      :  1;    // terminal node CO
    unsigned       fMark0   :  1;    // first user-controlled mark
    unsigned       fMark1   :  1;    // second user-controlled mark
    unsigned       nFanins  : 28;    // the number of fanins
    unsigned       nFanouts;         // the number of fanouts
    int            hHandle;          // the handle of the node
    union {
    unsigned       TravId;           // user-specified value
    unsigned       iFanin; 
    };
    union {
    unsigned       Value;            // user-specified value
    unsigned       iFanout; 
    };
    int            Fanios[0];        // the array of fanins/fanouts
};

typedef struct Emb_Man_t_ Emb_Man_t;
struct Emb_Man_t_
{
    Gia_Man_t *    pGia;             // the original AIG manager
    Vec_Int_t *    vCis;             // the vector of CIs (PIs + LOs)
    Vec_Int_t *    vCos;             // the vector of COs (POs + LIs)
    int            nObjs;            // the number of objects
    int            nRegs;            // the number of registers
    int            nTravIds;         // traversal ID of the network
    int *          pObjData;         // the array containing data for objects
    int            nObjData;         // the size of array to store the logic network
Alan Mishchenko committed
80 81
    int            fVerbose;         // verbose output flag
    Emb_Dat_t *    pVecs;            // array of vectors of size nObjs * nDims
Alan Mishchenko committed
82 83 84 85 86
    int            nReached;         // the number of nodes reachable from the pivot
    int            nDistMax;         // the maximum distance from the node
    float **       pMatr;            // covariance matrix nDims * nDims
    float **       pEigen;           // the first several eigen values of the matrix
    float *        pSols;            // solutions to the problem nObjs * nSols;
Alan Mishchenko committed
87
    unsigned short*pPlacement;       // (x,y) coordinates for each cell
Alan Mishchenko committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
};

static inline int         Emb_ManRegNum( Emb_Man_t * p )                              { return p->nRegs;                                    }
static inline int         Emb_ManCiNum( Emb_Man_t * p )                               { return Vec_IntSize(p->vCis);                        }
static inline int         Emb_ManCoNum( Emb_Man_t * p )                               { return Vec_IntSize(p->vCos);                        }
static inline int         Emb_ManPiNum( Emb_Man_t * p )                               { return Vec_IntSize(p->vCis) - p->nRegs;             }
static inline int         Emb_ManPoNum( Emb_Man_t * p )                               { return Vec_IntSize(p->vCos) - p->nRegs;             }
static inline int         Emb_ManObjNum( Emb_Man_t * p )                              { return p->nObjs;                                    } 
static inline int         Emb_ManNodeNum( Emb_Man_t * p )                             { return p->nObjs - Vec_IntSize(p->vCis) - Vec_IntSize(p->vCos); } 

static inline Emb_Obj_t * Emb_ManObj( Emb_Man_t * p, unsigned hHandle )               { return (Emb_Obj_t *)(p->pObjData + hHandle);        } 
static inline Emb_Obj_t * Emb_ManCi( Emb_Man_t * p, int i )                           { return Emb_ManObj( p, Vec_IntEntry(p->vCis,i) );    }
static inline Emb_Obj_t * Emb_ManCo( Emb_Man_t * p, int i )                           { return Emb_ManObj( p, Vec_IntEntry(p->vCos,i) );    }

static inline int         Emb_ObjIsTerm( Emb_Obj_t * pObj )                           { return pObj->fCi || pObj->fCo;                      } 
static inline int         Emb_ObjIsCi( Emb_Obj_t * pObj )                             { return pObj->fCi;                                   } 
static inline int         Emb_ObjIsCo( Emb_Obj_t * pObj )                             { return pObj->fCo;                                   } 
Alan Mishchenko committed
105 106
//static inline int         Emb_ObjIsPi( Emb_Obj_t * pObj )                             { return pObj->fCi && pObj->nFanins == 0;             } 
//static inline int         Emb_ObjIsPo( Emb_Obj_t * pObj )                             { return pObj->fCo && pObj->nFanouts == 0;            } 
Alan Mishchenko committed
107
static inline int         Emb_ObjIsNode( Emb_Obj_t * pObj )                           { return!Emb_ObjIsTerm(pObj) && pObj->nFanins > 0;    } 
Alan Mishchenko committed
108
//static inline int         Emb_ObjIsConst0( Emb_Obj_t * pObj )                         { return!Emb_ObjIsTerm(pObj) && pObj->nFanins == 0;   } 
Alan Mishchenko committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

static inline int         Emb_ObjSize( Emb_Obj_t * pObj )                             { return sizeof(Emb_Obj_t) / 4 + pObj->nFanins + pObj->nFanouts;  } 
static inline int         Emb_ObjFaninNum( Emb_Obj_t * pObj )                         { return pObj->nFanins;                               } 
static inline int         Emb_ObjFanoutNum( Emb_Obj_t * pObj )                        { return pObj->nFanouts;                              } 
static inline Emb_Obj_t * Emb_ObjFanin( Emb_Obj_t * pObj, int i )                     { return (Emb_Obj_t *)(((int *)pObj) - pObj->Fanios[i]);               } 
static inline Emb_Obj_t * Emb_ObjFanout( Emb_Obj_t * pObj, int i )                    { return (Emb_Obj_t *)(((int *)pObj) + pObj->Fanios[pObj->nFanins+i]); } 

static inline void        Emb_ManResetTravId( Emb_Man_t * p )                         { extern void Emb_ManCleanTravId( Emb_Man_t * p ); Emb_ManCleanTravId( p ); p->nTravIds = 1;  }
static inline void        Emb_ManIncrementTravId( Emb_Man_t * p )                     { p->nTravIds++;                                      }
static inline void        Emb_ObjSetTravId( Emb_Obj_t * pObj, int TravId )            { pObj->TravId = TravId;                              }
static inline void        Emb_ObjSetTravIdCurrent( Emb_Man_t * p, Emb_Obj_t * pObj )  { pObj->TravId = p->nTravIds;                         }
static inline void        Emb_ObjSetTravIdPrevious( Emb_Man_t * p, Emb_Obj_t * pObj ) { pObj->TravId = p->nTravIds - 1;                     }
static inline int         Emb_ObjIsTravIdCurrent( Emb_Man_t * p, Emb_Obj_t * pObj )   { return ((int)pObj->TravId == p->nTravIds);          }
static inline int         Emb_ObjIsTravIdPrevious( Emb_Man_t * p, Emb_Obj_t * pObj )  { return ((int)pObj->TravId == p->nTravIds - 1);      }

Alan Mishchenko committed
124 125
static inline Emb_Dat_t * Emb_ManVec( Emb_Man_t * p, int v )                          { return p->pVecs + v * p->nObjs;                     }
static inline float *     Emb_ManSol( Emb_Man_t * p, int v )                          { return p->pSols + v * p->nObjs;                     }
Alan Mishchenko committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

#define Emb_ManForEachObj( p, pObj, i )               \
    for ( i = 0; (i < p->nObjData) && (pObj = Emb_ManObj(p,i)); i += Emb_ObjSize(pObj) )
#define Emb_ManForEachNode( p, pObj, i )              \
    for ( i = 0; (i < p->nObjData) && (pObj = Emb_ManObj(p,i)); i += Emb_ObjSize(pObj) ) if ( Emb_ObjIsTerm(pObj) ) {} else
#define Emb_ManForEachObjVec( vVec, p, pObj, i )                        \
    for ( i = 0; (i < Vec_IntSize(vVec)) && ((pObj) = Emb_ManObj(p, Vec_IntEntry(vVec,i))); i++ )
#define Emb_ObjForEachFanin( pObj, pNext, i )         \
    for ( i = 0; (i < (int)pObj->nFanins) && (pNext = Emb_ObjFanin(pObj,i)); i++ )
#define Emb_ObjForEachFanout( pObj, pNext, i )        \
    for ( i = 0; (i < (int)pObj->nFanouts) && (pNext = Emb_ObjFanout(pObj,i)); i++ )

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

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

Alan Mishchenko committed
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
  Synopsis    [Creates fanin/fanout pair.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ObjAddFanin( Emb_Obj_t * pObj, Emb_Obj_t * pFanin )
{ 
    assert( pObj->iFanin < pObj->nFanins );
    assert( pFanin->iFanout < pFanin->nFanouts );
    pFanin->Fanios[pFanin->nFanins + pFanin->iFanout++] = 
    pObj->Fanios[pObj->iFanin++] = pObj->hHandle - pFanin->hHandle;
}

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

  Synopsis    [Creates logic network isomorphic to the given AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Emb_Man_t * Emb_ManStartSimple( Gia_Man_t * pGia )
{
    Emb_Man_t * p;
    Emb_Obj_t * pObjLog, * pFanLog;
    Gia_Obj_t * pObj, * pObjRi, * pObjRo;
    int i, nNodes, hHandle = 0;
    // prepare the AIG
    Gia_ManCreateRefs( pGia );
    // create logic network
    p = ABC_CALLOC( Emb_Man_t, 1 );
    p->pGia  = pGia;
    p->nRegs = Gia_ManRegNum(pGia);
    p->vCis  = Vec_IntAlloc( Gia_ManCiNum(pGia) );
    p->vCos  = Vec_IntAlloc( Gia_ManCoNum(pGia) );
Alan Mishchenko committed
186
    p->nObjData = (sizeof(Emb_Obj_t) / 4) * Gia_ManObjNum(pGia) + 2 * (2 * Gia_ManAndNum(pGia) + Gia_ManCoNum(pGia) + Gia_ManRegNum(pGia) + Gia_ManCoNum(pGia));
Alan Mishchenko committed
187 188 189 190 191
    p->pObjData = ABC_CALLOC( int, p->nObjData );
    // create constant node
    Gia_ManConst0(pGia)->Value = hHandle;
    pObjLog = Emb_ManObj( p, hHandle );
    pObjLog->hHandle  = hHandle;
Alan Mishchenko committed
192
    pObjLog->nFanins  = Gia_ManCoNum(pGia);  //0;
193
    pObjLog->nFanouts = Gia_ObjRefNum( pGia, Gia_ManConst0(pGia) );
Alan Mishchenko committed
194 195 196 197 198 199 200 201 202 203 204 205 206
    // count objects
    hHandle += Emb_ObjSize( pObjLog );
    nNodes = 1;
    p->nObjs++;
    // create the PIs
    Gia_ManForEachCi( pGia, pObj, i )
    {
        // create PI object
        pObj->Value = hHandle;
        Vec_IntPush( p->vCis, hHandle );
        pObjLog = Emb_ManObj( p, hHandle );
        pObjLog->hHandle  = hHandle;
        pObjLog->nFanins  = Gia_ObjIsRo( pGia, pObj );
207
        pObjLog->nFanouts = Gia_ObjRefNum( pGia, pObj );
Alan Mishchenko committed
208 209 210 211 212 213 214 215
        pObjLog->fCi = 1;
        // count objects
        hHandle += Emb_ObjSize( pObjLog );
        p->nObjs++;
    }
    // create internal nodes
    Gia_ManForEachAnd( pGia, pObj, i )
    {
216
        assert( Gia_ObjRefNum( pGia, pObj ) > 0 );
Alan Mishchenko committed
217 218 219 220 221
        // create node object
        pObj->Value = hHandle;
        pObjLog = Emb_ManObj( p, hHandle );
        pObjLog->hHandle  = hHandle;
        pObjLog->nFanins  = 2;
222
        pObjLog->nFanouts = Gia_ObjRefNum( pGia, pObj );
Alan Mishchenko committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        // add fanins
        pFanLog = Emb_ManObj( p, Gia_ObjValue(Gia_ObjFanin0(pObj)) ); 
        Emb_ObjAddFanin( pObjLog, pFanLog );
        pFanLog = Emb_ManObj( p, Gia_ObjValue(Gia_ObjFanin1(pObj)) ); 
        Emb_ObjAddFanin( pObjLog, pFanLog );
        // count objects
        hHandle += Emb_ObjSize( pObjLog );
        nNodes++;
        p->nObjs++;
    }
    // create the POs
    Gia_ManForEachCo( pGia, pObj, i )
    {
        // create PO object
        pObj->Value = hHandle;
        Vec_IntPush( p->vCos, hHandle );
        pObjLog = Emb_ManObj( p, hHandle );
        pObjLog->hHandle  = hHandle;
        pObjLog->nFanins  = 1;
Alan Mishchenko committed
242
        pObjLog->nFanouts = 1 + Gia_ObjIsRi( pGia, pObj );
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252 253 254 255
        pObjLog->fCo = 1;
        // add fanins
        pFanLog = Emb_ManObj( p, Gia_ObjValue(Gia_ObjFanin0(pObj)) );
        Emb_ObjAddFanin( pObjLog, pFanLog );
        // count objects
        hHandle += Emb_ObjSize( pObjLog );
        p->nObjs++;
    }
    // connect registers
    Gia_ManForEachRiRo( pGia, pObjRi, pObjRo, i )
        Emb_ObjAddFanin( Emb_ManObj(p,Gia_ObjValue(pObjRo)), Emb_ManObj(p,Gia_ObjValue(pObjRi)) );
    assert( nNodes  == Emb_ManNodeNum(p) );
    assert( hHandle == p->nObjData );
Alan Mishchenko committed
256
    assert( p->nObjs == Gia_ManObjNum(pGia) );
Alan Mishchenko committed
257 258 259 260 261 262 263 264
    if ( hHandle != p->nObjData )
        printf( "Emb_ManStartSimple(): Fatal error in internal representation.\n" );
    // make sure the fanin/fanout counters are correct
    Gia_ManForEachObj( pGia, pObj, i )
    {
        if ( !~Gia_ObjValue(pObj) )
            continue;
        pObjLog = Emb_ManObj( p, Gia_ObjValue(pObj) );
Alan Mishchenko committed
265 266
        assert( pObjLog->nFanins  == pObjLog->iFanin || Gia_ObjIsConst0(pObj) );
        assert( pObjLog->nFanouts == pObjLog->iFanout || Gia_ObjIsCo(pObj) );
Alan Mishchenko committed
267 268 269 270 271 272 273 274
        pObjLog->iFanin = pObjLog->iFanout = 0;
    }
    ABC_FREE( pGia->pRefs );
    return p;
}

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

Alan Mishchenko committed
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
  Synopsis    [Collect the fanin IDs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManCollectSuper_rec( Gia_Man_t * p, Gia_Obj_t * pObj, Vec_Int_t * vSuper, Vec_Int_t * vVisit )  
{
    if ( pObj->fMark1 )
        return;
    pObj->fMark1 = 1;
    Vec_IntPush( vVisit, Gia_ObjId(p, pObj) );
    if ( pObj->fMark0 )
    {
        Vec_IntPush( vSuper, Gia_ObjId(p, pObj) );
        return;
    }
    assert( Gia_ObjIsAnd(pObj) );
    Emb_ManCollectSuper_rec( p, Gia_ObjFanin0(pObj), vSuper, vVisit );
    Emb_ManCollectSuper_rec( p, Gia_ObjFanin1(pObj), vSuper, vVisit );
    
}

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

  Synopsis    [Collect the fanin IDs.]

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
void Emb_ManCollectSuper( Gia_Man_t * p, Gia_Obj_t * pObj, Vec_Int_t * vSuper, Vec_Int_t * vVisit )  
{
    int Entry, i;
    Vec_IntClear( vSuper );
    Vec_IntClear( vVisit );
    assert( pObj->fMark0 == 1 );
    pObj->fMark0 = 0;
    Emb_ManCollectSuper_rec( p, pObj, vSuper, vVisit );
    pObj->fMark0 = 1;
    Vec_IntForEachEntry( vVisit, Entry, i )
        Gia_ManObj(p, Entry)->fMark1 = 0;
}

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

  Synopsis    [Assigns references while removing the MUX/XOR ones.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManCreateRefsSpecial( Gia_Man_t * p )  
{
    Gia_Obj_t * pObj, * pFan0, * pFan1;
    Gia_Obj_t * pObjC, * pObjD0, * pObjD1;
    int i;
    assert( p->pRefs == NULL );
    Gia_ManCleanMark0( p );
    Gia_ManCreateRefs( p );
    Gia_ManForEachAnd( p, pObj, i )
    {
        assert( pObj->fMark0 == 0 );
        pFan0 = Gia_ObjFanin0(pObj);
        pFan1 = Gia_ObjFanin1(pObj);
        // skip nodes whose fanins are PIs or are already marked
        if ( Gia_ObjIsCi(pFan0) || pFan0->fMark0 || 
             Gia_ObjIsCi(pFan1) || pFan1->fMark0 )
             continue;
        // skip nodes that are not MUX type
        if ( !Gia_ObjIsMuxType(pObj) )
            continue;
        // the node is MUX type, mark it and its fanins
        pObj->fMark0  = 1;
        pFan0->fMark0 = 1;
        pFan1->fMark0 = 1;
        // deref the control 
        pObjC = Gia_ObjRecognizeMux( pObj, &pObjD1, &pObjD0 );
        Gia_ObjRefDec( p, Gia_Regular(pObjC) );
        if ( Gia_Regular(pObjD0) == Gia_Regular(pObjD1) )
            Gia_ObjRefDec( p, Gia_Regular(pObjD0) );
    }
    Gia_ManForEachAnd( p, pObj, i )
367
        assert( Gia_ObjRefNum(p, pObj) > 0 );
Alan Mishchenko committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    Gia_ManCleanMark0( p );
}

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

  Synopsis    [Assigns references while removing the MUX/XOR ones.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManTransformRefs( Gia_Man_t * p, int * pnObjs, int * pnFanios )  
{
    Vec_Int_t * vSuper, * vVisit;
    Gia_Obj_t * pObj, * pFanin;
    int i, k, Counter;
    assert( p->pRefs != NULL );

    // mark nodes to be used in the logic network
    Gia_ManCleanMark0( p );
    Gia_ManConst0(p)->fMark0 = 1;
    // mark the inputs
    Gia_ManForEachCi( p, pObj, i )
        pObj->fMark0 = 1;
    // mark those nodes that have ref count more than 1
    Gia_ManForEachAnd( p, pObj, i )
397
        pObj->fMark0 = (Gia_ObjRefNum(p, pObj) > 1);
Alan Mishchenko committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    // mark the output drivers
    Gia_ManForEachCoDriver( p, pObj, i )
        pObj->fMark0 = 1;

    // count the number of nodes
    Counter = 0;
    Gia_ManForEachObj( p, pObj, i )
        Counter += pObj->fMark0;
    *pnObjs = Counter + Gia_ManCoNum(p);

    // reset the references
    ABC_FREE( p->pRefs );
    p->pRefs = ABC_CALLOC( int, Gia_ManObjNum(p) );
    // reference from internal nodes
    Counter = 0;
    vSuper = Vec_IntAlloc( 100 );
    vVisit = Vec_IntAlloc( 100 );
    Gia_ManCleanMark1( p );
    Gia_ManForEachAnd( p, pObj, i )
    {
        if ( pObj->fMark0 == 0 )
            continue;
        Emb_ManCollectSuper( p, pObj, vSuper, vVisit );
        Gia_ManForEachObjVec( vSuper, p, pFanin, k )
        {
            assert( pFanin->fMark0 );
            Gia_ObjRefInc( p, pFanin );
        }
        Counter += Vec_IntSize( vSuper );
    }
    Gia_ManCheckMark1( p );
    Vec_IntFree( vSuper );
    Vec_IntFree( vVisit );
    // reference from outputs
    Gia_ManForEachCoDriver( p, pObj, i )
    {
        assert( pObj->fMark0 );
        Gia_ObjRefInc( p, pObj );
    }
    *pnFanios = Counter + Gia_ManCoNum(p);
}

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

  Synopsis    [Cleans the value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManCleanTravId( Emb_Man_t * p )  
{
    Emb_Obj_t * pObj;
    int i;
    Emb_ManForEachObj( p, pObj, i )
        pObj->TravId = 0;
}

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

  Synopsis    [Cleans the value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManSetValue( Emb_Man_t * p )  
{
    Emb_Obj_t * pObj;
    int i, Counter = 0;
    Emb_ManForEachObj( p, pObj, i )
    {
        pObj->Value = Counter++;
//        if ( pObj->fCi && pObj->nFanins == 0 ) 
//            printf( "CI:  Handle = %8d.  Value = %6d. Fanins = %d.\n", pObj->hHandle, pObj->Value, pObj->nFanins );
    }
}

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

  Synopsis    [Creates logic network isomorphic to the given AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Emb_Man_t * Emb_ManStart( Gia_Man_t * pGia )
{
    Emb_Man_t * p;
    Emb_Obj_t * pObjLog, * pFanLog;
    Gia_Obj_t * pObj, * pObjRi, * pObjRo, * pFanin;
    Vec_Int_t * vSuper, * vVisit;
    int nObjs, nFanios, nNodes = 0;
    int i, k, hHandle = 0;
    // prepare the AIG
//    Gia_ManCreateRefs( pGia );
    Emb_ManCreateRefsSpecial( pGia );
    Emb_ManTransformRefs( pGia, &nObjs, &nFanios );
    Gia_ManFillValue( pGia );
    // create logic network
    p = ABC_CALLOC( Emb_Man_t, 1 );
    p->pGia  = pGia;
    p->nRegs = Gia_ManRegNum(pGia);
    p->vCis  = Vec_IntAlloc( Gia_ManCiNum(pGia) );
    p->vCos  = Vec_IntAlloc( Gia_ManCoNum(pGia) );
Alan Mishchenko committed
512
    p->nObjData = (sizeof(Emb_Obj_t) / 4) * nObjs + 2 * (nFanios + Gia_ManRegNum(pGia) + Gia_ManCoNum(pGia));
Alan Mishchenko committed
513 514 515 516 517
    p->pObjData = ABC_CALLOC( int, p->nObjData );
    // create constant node
    Gia_ManConst0(pGia)->Value = hHandle;
    pObjLog = Emb_ManObj( p, hHandle );
    pObjLog->hHandle  = hHandle;
Alan Mishchenko committed
518
    pObjLog->nFanins  = Gia_ManCoNum(pGia);  //0;
519
    pObjLog->nFanouts = Gia_ObjRefNum( pGia, Gia_ManConst0(pGia) );
Alan Mishchenko committed
520 521 522 523 524 525 526 527 528 529 530 531 532
    // count objects
    hHandle += Emb_ObjSize( pObjLog );
    nNodes++;
    p->nObjs++;
    // create the PIs
    Gia_ManForEachCi( pGia, pObj, i )
    {
        // create PI object
        pObj->Value = hHandle;
        Vec_IntPush( p->vCis, hHandle );
        pObjLog = Emb_ManObj( p, hHandle );
        pObjLog->hHandle  = hHandle;
        pObjLog->nFanins  = Gia_ObjIsRo( pGia, pObj );
533
        pObjLog->nFanouts = Gia_ObjRefNum( pGia, pObj );
Alan Mishchenko committed
534 535 536 537 538 539 540 541 542 543 544 545
        pObjLog->fCi = 1;
        // count objects
        hHandle += Emb_ObjSize( pObjLog );
        p->nObjs++;
    }
    // create internal nodes
    vSuper = Vec_IntAlloc( 100 );
    vVisit = Vec_IntAlloc( 100 );
    Gia_ManForEachAnd( pGia, pObj, i )
    {
        if ( pObj->fMark0 == 0 )
        {
546
            assert( Gia_ObjRefNum( pGia, pObj ) == 0 );
Alan Mishchenko committed
547 548
            continue;
        }
549
        assert( Gia_ObjRefNum( pGia, pObj ) > 0 );
Alan Mishchenko committed
550 551 552 553 554 555
        Emb_ManCollectSuper( pGia, pObj, vSuper, vVisit );
        // create node object
        pObj->Value = hHandle;
        pObjLog = Emb_ManObj( p, hHandle );
        pObjLog->hHandle  = hHandle;
        pObjLog->nFanins  = Vec_IntSize( vSuper );
556
        pObjLog->nFanouts = Gia_ObjRefNum( pGia, pObj );
Alan Mishchenko committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
        // add fanins
        Gia_ManForEachObjVec( vSuper, pGia, pFanin, k )
        {
            pFanLog = Emb_ManObj( p, Gia_ObjValue(pFanin) ); 
            Emb_ObjAddFanin( pObjLog, pFanLog );
        }
        // count objects
        hHandle += Emb_ObjSize( pObjLog );
        nNodes++;
        p->nObjs++;
    }
    Vec_IntFree( vSuper );
    Vec_IntFree( vVisit );
    // create the POs
    Gia_ManForEachCo( pGia, pObj, i )
    {
        // create PO object
        pObj->Value = hHandle;
        Vec_IntPush( p->vCos, hHandle );
        pObjLog = Emb_ManObj( p, hHandle );
        pObjLog->hHandle  = hHandle;
        pObjLog->nFanins  = 1;
Alan Mishchenko committed
579
        pObjLog->nFanouts = 1 + Gia_ObjIsRi( pGia, pObj );
Alan Mishchenko committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
        pObjLog->fCo = 1;
        // add fanins
        pFanLog = Emb_ManObj( p, Gia_ObjValue(Gia_ObjFanin0(pObj)) );
        Emb_ObjAddFanin( pObjLog, pFanLog );
        // count objects
        hHandle += Emb_ObjSize( pObjLog );
        p->nObjs++;
    }
    // connect registers
    Gia_ManForEachRiRo( pGia, pObjRi, pObjRo, i )
        Emb_ObjAddFanin( Emb_ManObj(p,Gia_ObjValue(pObjRo)), Emb_ManObj(p,Gia_ObjValue(pObjRi)) );
    Gia_ManCleanMark0( pGia );
    assert( nNodes  == Emb_ManNodeNum(p) );
    assert( nObjs   == p->nObjs );
    assert( hHandle == p->nObjData );
    if ( hHandle != p->nObjData )
        printf( "Emb_ManStart(): Fatal error in internal representation.\n" );
    // make sure the fanin/fanout counters are correct
    Gia_ManForEachObj( pGia, pObj, i )
    {
        if ( !~Gia_ObjValue(pObj) )
            continue;
        pObjLog = Emb_ManObj( p, Gia_ObjValue(pObj) );
Alan Mishchenko committed
603 604
        assert( pObjLog->nFanins  == pObjLog->iFanin || Gia_ObjIsConst0(pObj) );
        assert( pObjLog->nFanouts == pObjLog->iFanout || Gia_ObjIsCo(pObj) );
Alan Mishchenko committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
        pObjLog->iFanin = pObjLog->iFanout = 0;
    }
    ABC_FREE( pGia->pRefs );
    return p;
}

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

  Synopsis    [Creates logic network isomorphic to the given AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManPrintStats( Emb_Man_t * p )
{
//    if ( p->pName )
//        printf( "%8s : ", p->pName );
    printf( "i/o =%7d/%7d  ", Emb_ManPiNum(p), Emb_ManPoNum(p) );
    if ( Emb_ManRegNum(p) )
        printf( "ff =%7d  ", Emb_ManRegNum(p) );
    printf( "node =%8d  ", Emb_ManNodeNum(p) );
    printf( "obj =%8d  ", Emb_ManObjNum(p) );
//    printf( "lev =%5d  ", Emb_ManLevelNum(p) );
//    printf( "cut =%5d  ", Emb_ManCrossCut(p) );
633
    printf( "mem =%5.2f MB", 4.0*p->nObjData/(1<<20) );
Alan Mishchenko committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
//    printf( "obj =%5d  ", Emb_ManObjNum(p) );
    printf( "\n" );

//    Emb_ManSatExperiment( p );
}

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

  Synopsis    [Creates logic network isomorphic to the given AIG.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManStop( Emb_Man_t * p )
{
    Vec_IntFree( p->vCis );
    Vec_IntFree( p->vCos );
Alan Mishchenko committed
655
    ABC_FREE( p->pPlacement );
Alan Mishchenko committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    ABC_FREE( p->pVecs );
    ABC_FREE( p->pSols );
    ABC_FREE( p->pMatr );
    ABC_FREE( p->pEigen );
    ABC_FREE( p->pObjData );
    ABC_FREE( p );
}

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

  Synopsis    [Prints the distribution of fanins/fanouts in the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManPrintFanio( Emb_Man_t * p )
{
    char Buffer[100];
    Emb_Obj_t * pNode;
    Vec_Int_t * vFanins, * vFanouts;
    int nFanins, nFanouts, nFaninsMax, nFanoutsMax, nFaninsAll, nFanoutsAll;
    int i, k, nSizeMax;

    // determine the largest fanin and fanout
    nFaninsMax = nFanoutsMax = 0;
    nFaninsAll = nFanoutsAll = 0;
    Emb_ManForEachNode( p, pNode, i )
    {
        if ( i == 0 ) continue; // skip const 0 obj
        nFanins  = Emb_ObjFaninNum(pNode);
        nFanouts = Emb_ObjFanoutNum(pNode);
        nFaninsAll  += nFanins;
        nFanoutsAll += nFanouts;
693 694
        nFaninsMax   = Abc_MaxInt( nFaninsMax, nFanins );
        nFanoutsMax  = Abc_MaxInt( nFanoutsMax, nFanouts );
Alan Mishchenko committed
695 696 697
    }

    // allocate storage for fanin/fanout numbers
698
    nSizeMax = Abc_MaxInt( 10 * (Abc_Base10Log(nFaninsMax) + 1), 10 * (Abc_Base10Log(nFanoutsMax) + 1) );
Alan Mishchenko committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    vFanins  = Vec_IntStart( nSizeMax );
    vFanouts = Vec_IntStart( nSizeMax );

    // count the number of fanins and fanouts
    Emb_ManForEachNode( p, pNode, i )
    {
        if ( i == 0 ) continue; // skip const 0 obj
        nFanins  = Emb_ObjFaninNum(pNode);
        nFanouts = Emb_ObjFanoutNum(pNode);

        if ( nFanins < 10 )
            Vec_IntAddToEntry( vFanins, nFanins, 1 );
        else if ( nFanins < 100 )
            Vec_IntAddToEntry( vFanins, 10 + nFanins/10, 1 );
        else if ( nFanins < 1000 )
            Vec_IntAddToEntry( vFanins, 20 + nFanins/100, 1 );
        else if ( nFanins < 10000 )
            Vec_IntAddToEntry( vFanins, 30 + nFanins/1000, 1 );
        else if ( nFanins < 100000 )
            Vec_IntAddToEntry( vFanins, 40 + nFanins/10000, 1 );
        else if ( nFanins < 1000000 )
            Vec_IntAddToEntry( vFanins, 50 + nFanins/100000, 1 );
        else if ( nFanins < 10000000 )
            Vec_IntAddToEntry( vFanins, 60 + nFanins/1000000, 1 );

        if ( nFanouts < 10 )
            Vec_IntAddToEntry( vFanouts, nFanouts, 1 );
        else if ( nFanouts < 100 )
            Vec_IntAddToEntry( vFanouts, 10 + nFanouts/10, 1 );
        else if ( nFanouts < 1000 )
            Vec_IntAddToEntry( vFanouts, 20 + nFanouts/100, 1 );
        else if ( nFanouts < 10000 )
            Vec_IntAddToEntry( vFanouts, 30 + nFanouts/1000, 1 );
        else if ( nFanouts < 100000 )
            Vec_IntAddToEntry( vFanouts, 40 + nFanouts/10000, 1 );
        else if ( nFanouts < 1000000 )
            Vec_IntAddToEntry( vFanouts, 50 + nFanouts/100000, 1 );
        else if ( nFanouts < 10000000 )
            Vec_IntAddToEntry( vFanouts, 60 + nFanouts/1000000, 1 );
    }

    printf( "The distribution of fanins and fanouts in the network:\n" );
    printf( "         Number   Nodes with fanin  Nodes with fanout\n" );
    for ( k = 0; k < nSizeMax; k++ )
    {
        if ( vFanins->pArray[k] == 0 && vFanouts->pArray[k] == 0 )
            continue;
        if ( k < 10 )
            printf( "%15d : ", k );
        else
        {
750
            sprintf( Buffer, "%d - %d", (int)pow((double)10, k/10) * (k%10), (int)pow((double)10, k/10) * (k%10+1) - 1 ); 
Alan Mishchenko committed
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
            printf( "%15s : ", Buffer );
        }
        if ( vFanins->pArray[k] == 0 )
            printf( "              " );
        else
            printf( "%12d  ", vFanins->pArray[k] );
        printf( "    " );
        if ( vFanouts->pArray[k] == 0 )
            printf( "              " );
        else
            printf( "%12d  ", vFanouts->pArray[k] );
        printf( "\n" );
    }
    Vec_IntFree( vFanins );
    Vec_IntFree( vFanouts );

    printf( "Fanins: Max = %d. Ave = %.2f.  Fanouts: Max = %d. Ave =  %.2f.\n", 
        nFaninsMax,  1.0*nFaninsAll/Emb_ManNodeNum(p), 
        nFanoutsMax, 1.0*nFanoutsAll/Emb_ManNodeNum(p)  );
}

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

  Synopsis    [Computes the distance from the given object]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Emb_ManComputeDistance_old( Emb_Man_t * p, Emb_Obj_t * pPivot )
{
    Vec_Int_t * vThis, * vNext, * vTemp;
    Emb_Obj_t * pThis, * pNext;
    int i, k, d, nVisited = 0;
//    assert( Emb_ObjIsTerm(pPivot) );
    vThis = Vec_IntAlloc( 1000 );
    vNext = Vec_IntAlloc( 1000 );
    Emb_ManIncrementTravId( p );
    Emb_ObjSetTravIdCurrent( p, pPivot );
    Vec_IntPush( vThis, pPivot->hHandle );
    for ( d = 0; Vec_IntSize(vThis) > 0; d++ )
    {
        nVisited += Vec_IntSize(vThis);
        Vec_IntClear( vNext );
        Emb_ManForEachObjVec( vThis, p, pThis, i )
        {
            Emb_ObjForEachFanin( pThis, pNext, k )
            {
                if ( Emb_ObjIsTravIdCurrent(p, pNext) )
                    continue;
                Emb_ObjSetTravIdCurrent(p, pNext);
                Vec_IntPush( vNext, pNext->hHandle );
                nVisited += !Emb_ObjIsTerm(pNext);
            }
            Emb_ObjForEachFanout( pThis, pNext, k )
            {
                if ( Emb_ObjIsTravIdCurrent(p, pNext) )
                    continue;
                Emb_ObjSetTravIdCurrent(p, pNext);
                Vec_IntPush( vNext, pNext->hHandle );
                nVisited += !Emb_ObjIsTerm(pNext);
            }
        }
        vTemp = vThis; vThis = vNext; vNext = vTemp;
    }
    Vec_IntFree( vThis );
    Vec_IntFree( vNext );
    // check if there are several strongly connected components
//    if ( nVisited < Emb_ManNodeNum(p) )
//        printf( "Visited less nodes (%d) than present (%d).\n", nVisited, Emb_ManNodeNum(p) );
    return d;
}

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

  Synopsis    [Traverses from the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
void Gia_ManTestDistanceInternal( Emb_Man_t * p )
{
    int nAttempts = 20;
841
    int i, iNode, Dist;
842
    abctime clk;
Alan Mishchenko committed
843
    Emb_Obj_t * pPivot, * pNext;
Alan Mishchenko committed
844
    Gia_ManRandom( 1 );
Alan Mishchenko committed
845 846
    Emb_ManResetTravId( p );
    // compute distances from several randomly selected PIs
847
    clk = Abc_Clock();
Alan Mishchenko committed
848 849 850
    printf( "From inputs: " );
    for ( i = 0; i < nAttempts; i++ )
    {
Alan Mishchenko committed
851
        iNode = Gia_ManRandom( 0 ) % Emb_ManCiNum(p);
Alan Mishchenko committed
852 853 854 855 856 857 858 859 860
        pPivot = Emb_ManCi( p, iNode );
        if ( Emb_ObjFanoutNum(pPivot) == 0 )
            { i--; continue; }
        pNext = Emb_ObjFanout( pPivot, 0 );
        if ( !Emb_ObjIsNode(pNext) )
            { i--; continue; }
        Dist = Emb_ManComputeDistance_old( p, pPivot );
        printf( "%d ", Dist );
    }
861
    ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
862
    // compute distances from several randomly selected POs
863
    clk = Abc_Clock();
Alan Mishchenko committed
864 865 866
    printf( "From outputs: " );
    for ( i = 0; i < nAttempts; i++ )
    {
Alan Mishchenko committed
867
        iNode = Gia_ManRandom( 0 ) % Emb_ManCoNum(p);
Alan Mishchenko committed
868 869 870 871 872 873 874
        pPivot = Emb_ManCo( p, iNode );
        pNext = Emb_ObjFanin( pPivot, 0 );
        if ( !Emb_ObjIsNode(pNext) )
            { i--; continue; }
        Dist = Emb_ManComputeDistance_old( p, pPivot );
        printf( "%d ", Dist );
    }
875
    ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
876
    // compute distances from several randomly selected nodes
877
    clk = Abc_Clock();
Alan Mishchenko committed
878 879 880
    printf( "From nodes: " );
    for ( i = 0; i < nAttempts; i++ )
    {
Alan Mishchenko committed
881
        iNode = Gia_ManRandom( 0 ) % Gia_ManObjNum(p->pGia);
Alan Mishchenko committed
882 883 884 885 886 887 888 889
        if ( !~Gia_ManObj(p->pGia, iNode)->Value )
            { i--; continue; }
        pPivot = Emb_ManObj( p, Gia_ManObj(p->pGia, iNode)->Value );
        if ( !Emb_ObjIsNode(pPivot) )
            { i--; continue; }
        Dist = Emb_ManComputeDistance_old( p, pPivot );
        printf( "%d ", Dist );
    }
890
    ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
}

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

  Synopsis    [Returns sorted array of node handles with largest fanout.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManTestDistance( Gia_Man_t * pGia )
{
    Emb_Man_t * p;
907
    abctime clk = Abc_Clock();
Alan Mishchenko committed
908 909 910
    p = Emb_ManStart( pGia );
//    Emb_ManPrintFanio( p );
    Emb_ManPrintStats( p );
911
ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
912 913 914 915 916 917 918 919 920
    Gia_ManTestDistanceInternal( p );
    Emb_ManStop( p );
}




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

Alan Mishchenko committed
921
  Synopsis    [Perform BFS from the set of nodes.]
Alan Mishchenko committed
922 923 924 925 926 927 928 929

  Description [Returns one of the most distant objects.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
930
Emb_Obj_t * Emb_ManPerformBfs( Emb_Man_t * p, Vec_Int_t * vThis, Vec_Int_t * vNext, Emb_Dat_t * pDist )
Alan Mishchenko committed
931
{
Alan Mishchenko committed
932
    Vec_Int_t * vTemp;
Alan Mishchenko committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    Emb_Obj_t * pThis, * pNext, * pResult;
    int i, k;
    assert( Vec_IntSize(vThis) > 0 );
    for ( p->nDistMax = 0; Vec_IntSize(vThis) > 0; p->nDistMax++ )
    {
        p->nReached += Vec_IntSize(vThis);
        Vec_IntClear( vNext );
        Emb_ManForEachObjVec( vThis, p, pThis, i )
        {
            if ( pDist ) pDist[pThis->Value] = p->nDistMax;
            Emb_ObjForEachFanin( pThis, pNext, k )
            {
                if ( Emb_ObjIsTravIdCurrent(p, pNext) )
                    continue;
                Emb_ObjSetTravIdCurrent(p, pNext);
                Vec_IntPush( vNext, pNext->hHandle );
            }
            Emb_ObjForEachFanout( pThis, pNext, k )
            {
                if ( Emb_ObjIsTravIdCurrent(p, pNext) )
                    continue;
                Emb_ObjSetTravIdCurrent(p, pNext);
                Vec_IntPush( vNext, pNext->hHandle );
            }
        }
        vTemp = vThis; vThis = vNext; vNext = vTemp;
    }
    assert( Vec_IntSize(vNext) > 0 );
    pResult = Emb_ManObj( p, Vec_IntEntry(vNext, 0) );
    assert( pDist == NULL || pDist[pResult->Value] == p->nDistMax - 1 );
Alan Mishchenko committed
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    return pResult;
}

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

  Synopsis    [Computes the distances from the given set of objects.]

  Description [Returns one of the most distant objects.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Emb_ManConnectedComponents( Emb_Man_t * p )
{
    Gia_Obj_t * pObj;
    Vec_Int_t * vThis, * vNext, * vResult;
    Emb_Obj_t * pThis;
    int i;
    vResult = Vec_IntAlloc( 1000 );
    vThis   = Vec_IntAlloc( 1000 );
    vNext   = Vec_IntAlloc( 1000 );
    p->nReached = 0;
    Emb_ManIncrementTravId( p );
    Gia_ManForEachCo( p->pGia, pObj, i )
    {
        pThis = Emb_ManObj( p, Gia_ObjValue(pObj) );
        if ( Emb_ObjIsTravIdCurrent(p, pThis) )
            continue;
        Emb_ObjSetTravIdCurrent( p, pThis );
        Vec_IntPush( vResult, pThis->hHandle );
        // perform BFS from this node
        Vec_IntClear( vThis );
        Vec_IntPush( vThis, pThis->hHandle );
        Emb_ManPerformBfs( p, vThis, vNext, NULL ); 
    }
    Vec_IntFree( vThis );
    Vec_IntFree( vNext );
    return vResult;
}

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

  Synopsis    [Computes the distances from the given set of objects.]

  Description [Returns one of the most distant objects.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Emb_Obj_t * Emb_ManFindDistances( Emb_Man_t * p, Vec_Int_t * vStart, Emb_Dat_t * pDist )
{
    Vec_Int_t * vThis, * vNext;
    Emb_Obj_t * pThis, * pResult;
    int i;
    p->nReached = p->nDistMax = 0;
    vThis = Vec_IntAlloc( 1000 );
    vNext = Vec_IntAlloc( 1000 );
    Emb_ManIncrementTravId( p );
    Emb_ManForEachObjVec( vStart, p, pThis, i )
    {
        Emb_ObjSetTravIdCurrent( p, pThis );
        Vec_IntPush( vThis, pThis->hHandle );
    }
    pResult = Emb_ManPerformBfs( p, vThis, vNext, pDist );
Alan Mishchenko committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    Vec_IntFree( vThis );
    Vec_IntFree( vNext );
    return pResult;
}

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

  Synopsis    [Traverses from the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []
 
***********************************************************************/
Emb_Obj_t * Emb_ManRandomVertex( Emb_Man_t * p )
{
    Emb_Obj_t * pPivot;
    do {
Alan Mishchenko committed
1051
        int iNode = (911 * Gia_ManRandom(0)) % Gia_ManObjNum(p->pGia);
Alan Mishchenko committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
        if ( ~Gia_ManObj(p->pGia, iNode)->Value )
            pPivot = Emb_ManObj( p, Gia_ManObj(p->pGia, iNode)->Value );
        else
            pPivot = NULL;
    }
    while ( pPivot == NULL || !Emb_ObjIsNode(pPivot) );
    return pPivot;
}

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

Alan Mishchenko committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  Synopsis    [Computes the distances from the given set of objects.]

  Description [Returns one of the most distant objects.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_DumpGraphIntoFile( Emb_Man_t * p )
{
    FILE * pFile;
    Emb_Obj_t * pThis, * pNext;
    int i, k;
    pFile = fopen( "1.g", "w" );
    Emb_ManForEachObj( p, pThis, i )
    {
        if ( !Emb_ObjIsTravIdCurrent(p, pThis) )
            continue;
        Emb_ObjForEachFanout( pThis, pNext, k )
        {
            assert( Emb_ObjIsTravIdCurrent(p, pNext) );
            fprintf( pFile, "%d %d\n", pThis->Value, pNext->Value );
        }
    }
    fclose( pFile );
}

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

Alan Mishchenko committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
  Synopsis    [Computes dimentions of the graph.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManComputeDimensions( Emb_Man_t * p, int nDims )
{
    Emb_Obj_t * pRandom, * pPivot;
Alan Mishchenko committed
1105
    Vec_Int_t * vStart, * vComps;
Alan Mishchenko committed
1106
    int d, nReached;
Alan Mishchenko committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    int i;//, Counter;
    // connect unconnected components
    vComps = Emb_ManConnectedComponents( p );
//    printf( "Components = %d. Considered %d objects (out of %d).\n", Vec_IntSize(vComps), p->nReached, Emb_ManObjNum(p) );
    if ( Vec_IntSize(vComps) > 1 )
    {
        Emb_Obj_t * pFanin, * pObj = Emb_ManObj( p, 0 );
        Emb_ManForEachObjVec( vComps, p, pFanin, i )
        {
            assert( Emb_ObjIsCo(pFanin) );
            pFanin->Fanios[pFanin->nFanins + pFanin->nFanouts-1] = 
                pObj->Fanios[i] = pObj->hHandle - pFanin->hHandle;
        }
    }
    Vec_IntFree( vComps );
    // allocate memory for vectors
Alan Mishchenko committed
1123
    assert( p->pVecs == NULL );
Alan Mishchenko committed
1124 1125 1126
    p->pVecs = ABC_CALLOC( Emb_Dat_t, p->nObjs * nDims );
//    for ( i = 0; i < p->nObjs * nDims; i++ )
//        p->pVecs[i] = ABC_INFINITY;
Alan Mishchenko committed
1127 1128 1129 1130 1131 1132
    vStart = Vec_IntAlloc( nDims );
    // get the pivot vertex
    pRandom = Emb_ManRandomVertex( p );
    Vec_IntPush( vStart, pRandom->hHandle );
    // get the most distant vertex from the pivot
    pPivot = Emb_ManFindDistances( p, vStart, NULL );
Alan Mishchenko committed
1133
//    Emb_DumpGraphIntoFile( p );
Alan Mishchenko committed
1134 1135
    nReached = p->nReached;
    if ( nReached < Emb_ManObjNum(p) )
Alan Mishchenko committed
1136
    {
Alan Mishchenko committed
1137
//        printf( "Considering a connected component with %d objects (out of %d).\n", p->nReached, Emb_ManObjNum(p) );
Alan Mishchenko committed
1138
    }
Alan Mishchenko committed
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    // start dimensions with this vertex
    Vec_IntClear( vStart );
    for ( d = 0; d < nDims; d++ )
    {
//        printf( "%3d : Adding vertex %7d with distance %3d.\n", d+1, pPivot->Value, p->nDistMax ); 
        Vec_IntPush( vStart, pPivot->hHandle );
        if ( d+1 == nReached )
            break;
        pPivot = Emb_ManFindDistances( p, vStart, Emb_ManVec(p, d) );
        assert( nReached == p->nReached );
    }
    Vec_IntFree( vStart );
    // make sure the number of reached objects is correct
Alan Mishchenko committed
1152 1153 1154 1155 1156
//    Counter = 0;
//    for ( i = 0; i < p->nObjs; i++ )
//        if ( p->pVecs[i] < ABC_INFINITY )
//            Counter++;
//    assert( Counter == nReached );
Alan Mishchenko committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
}

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

  Synopsis    [Allocated square matrix of floats.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float ** Emb_ManMatrAlloc( int nDims )
{
    int i;
    float ** pMatr = (float **)ABC_ALLOC( char, sizeof(float *) * nDims + sizeof(float) * nDims * nDims );
    pMatr[0] = (float *)(pMatr + nDims);
    for ( i = 1; i < nDims; i++ )
        pMatr[i] = pMatr[i-1] + nDims;
    return pMatr;
}

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

  Synopsis    [Computes covariance matrix.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManComputeCovariance( Emb_Man_t * p, int nDims )
{
Alan Mishchenko committed
1193 1194 1195
    Emb_Dat_t * pOne, * pTwo;
    double Ave;
    float * pRow;
Alan Mishchenko committed
1196
    int d, i, k, v;
Alan Mishchenko committed
1197
    // average vectors
Alan Mishchenko committed
1198 1199
    for ( d = 0; d < nDims; d++ )
    {
Alan Mishchenko committed
1200 1201
        // compute average
        Ave = 0.0;
Alan Mishchenko committed
1202 1203
        pOne = Emb_ManVec( p, d );
        for ( v = 0; v < p->nObjs; v++ )
Alan Mishchenko committed
1204 1205 1206 1207 1208 1209 1210 1211 1212
            if ( pOne[v] < ABC_INFINITY )
                Ave += pOne[v];
        Ave /= p->nReached;
        // update the vector
        for ( v = 0; v < p->nObjs; v++ )
            if ( pOne[v] < ABC_INFINITY )
                pOne[v] -= Ave;
            else
                pOne[v] = 0.0;        
Alan Mishchenko committed
1213 1214 1215 1216 1217 1218 1219 1220 1221
    }
    // compute the matrix
    assert( p->pMatr == NULL );
    assert( p->pEigen == NULL );
    p->pMatr = Emb_ManMatrAlloc( nDims );
    p->pEigen = Emb_ManMatrAlloc( nDims );
    for ( i = 0; i < nDims; i++ )
    {
        pOne = Emb_ManVec( p, i );
Alan Mishchenko committed
1222
        pRow = p->pMatr[i];
Alan Mishchenko committed
1223 1224 1225
        for ( k = 0; k < nDims; k++ )
        {
            pTwo = Emb_ManVec( p, k );
Alan Mishchenko committed
1226
            pRow[k] = 0.0;
Alan Mishchenko committed
1227
            for ( v = 0; v < p->nObjs; v++ )
Alan Mishchenko committed
1228
                pRow[k] += pOne[v]*pTwo[v];
Alan Mishchenko committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
        }
    }
}

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

  Synopsis    [Returns random vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManVecRandom( float * pVec, int nDims )
{
    int i;
    for ( i = 0; i < nDims; i++ )
Alan Mishchenko committed
1248
        pVec[i] = Gia_ManRandom( 0 );
Alan Mishchenko committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
}

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

  Synopsis    [Returns normalized vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManVecNormal( float * pVec, int nDims )
{
    int i;
    double Norm = 0.0;
    for ( i = 0; i < nDims; i++ )
        Norm += pVec[i] * pVec[i];
    Norm = pow( Norm, 0.5 );
    for ( i = 0; i < nDims; i++ )
        pVec[i] /= Norm;
}

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

  Synopsis    [Multiplies vector by vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Emb_ManVecMultiplyOne( float * pVec0, float * pVec1, int nDims )
{
    float Res = 0.0;
    int i;
    for ( i = 0; i < nDims; i++ )
        Res += pVec0[i] * pVec1[i];
    return Res;
}

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

  Synopsis    [Copies the vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManVecCopyOne( float * pVecDest, float * pVecSour, int nDims )
{
    int i;
    for ( i = 0; i < nDims; i++ )
        pVecDest[i] = pVecSour[i];
}

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

  Synopsis    [Multiplies matrix by vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManVecMultiply( float ** pMatr, float * pVec, int nDims, float * pRes )
{
Alan Mishchenko committed
1324 1325 1326
    int k;
    for ( k = 0; k < nDims; k++ )
        pRes[k] = Emb_ManVecMultiplyOne( pMatr[k], pVec, nDims );
Alan Mishchenko committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
}

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

  Synopsis    [Multiplies vector by matrix.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1340
void Emb_ManVecOrthogonolizeOne( float * pEigen, float * pVecI, int nDims, float * pVecRes )
Alan Mishchenko committed
1341
{
Alan Mishchenko committed
1342 1343 1344
    int k;
    for ( k = 0; k < nDims; k++ )
        pVecRes[k] = pVecI[k] - pEigen[k] * Emb_ManVecMultiplyOne( pVecI, pEigen, nDims );
Alan Mishchenko committed
1345 1346 1347 1348
}

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

Alan Mishchenko committed
1349
  Synopsis    [Computes the first nSols eigen-vectors.]
Alan Mishchenko committed
1350 1351 1352 1353 1354 1355 1356 1357

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1358
void Emb_ManComputeEigenvectors( Emb_Man_t * p, int nDims, int nSols )
Alan Mishchenko committed
1359
{
Alan Mishchenko committed
1360 1361
    float * pVecUiHat, * pVecUi;
    int i, j, k;
Alan Mishchenko committed
1362
    assert( nSols < nDims );
Alan Mishchenko committed
1363
    pVecUiHat = p->pEigen[nSols];
Alan Mishchenko committed
1364 1365
    for ( i = 0; i < nSols; i++ )
    {
Alan Mishchenko committed
1366 1367 1368 1369
        pVecUi = p->pEigen[i];
        Emb_ManVecRandom( pVecUiHat, nDims );
        Emb_ManVecNormal( pVecUiHat, nDims );
        k = 0;
Alan Mishchenko committed
1370
        do {
Alan Mishchenko committed
1371 1372
            k++;
            Emb_ManVecCopyOne( pVecUi, pVecUiHat, nDims );
Alan Mishchenko committed
1373
            for ( j = 0; j < i; j++ )
Alan Mishchenko committed
1374 1375 1376 1377 1378 1379 1380 1381 1382
            {
                Emb_ManVecOrthogonolizeOne( p->pEigen[j], pVecUi, nDims, pVecUiHat );
                Emb_ManVecCopyOne( pVecUi, pVecUiHat, nDims );
            }
            Emb_ManVecMultiply( p->pMatr, pVecUi, nDims, pVecUiHat );
            Emb_ManVecNormal( pVecUiHat, nDims );
        } while ( Emb_ManVecMultiplyOne( pVecUiHat, pVecUi, nDims ) < 0.999 && k < 100 );
        Emb_ManVecCopyOne( pVecUi, pVecUiHat, nDims );
//        printf( "Converged after %d iterations.\n", k );
Alan Mishchenko committed
1383
    }
Alan Mishchenko committed
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
}

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

  Synopsis    [Derives solutions from original vectors and eigenvectors.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManComputeSolutions( Emb_Man_t * p, int nDims, int nSols )
{
    Emb_Dat_t * pX;
    float * pY;
    int i, j, k;
Alan Mishchenko committed
1402 1403
    assert( p->pSols == NULL );
    p->pSols = ABC_CALLOC( float, p->nObjs * nSols );
Alan Mishchenko committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    for ( i = 0; i < nDims; i++ )
    {
        pX = Emb_ManVec( p, i );
        for ( j = 0; j < nSols; j++ )
        {
            pY = Emb_ManSol( p, j );
            for ( k = 0; k < p->nObjs; k++ )
                pY[k] += pX[k] * p->pEigen[j][i];
        }
    }
}

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

Alan Mishchenko committed
1418
  Synopsis    [Projects into square of size [0;GIA_PLACE_SIZE] x [0;GIA_PLACE_SIZE].]
Alan Mishchenko committed
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManDerivePlacement( Emb_Man_t * p, int nSols )
{
    float * pY0, * pY1, Max0, Max1, Min0, Min1, Str0, Str1;
    int * pPerm0, * pPerm1;
    int k;
    if ( nSols != 2 )
        return;
    // compute intervals
    Min0 =  ABC_INFINITY;
    Max0 = -ABC_INFINITY;
    pY0 = Emb_ManSol( p, 0 );
    for ( k = 0; k < p->nObjs; k++ )
    {
1440 1441
        Min0 = Abc_MinInt( Min0, pY0[k] );
        Max0 = Abc_MaxInt( Max0, pY0[k] );
Alan Mishchenko committed
1442
    }
Alan Mishchenko committed
1443
    Str0 = 1.0*GIA_PLACE_SIZE/(Max0 - Min0);
Alan Mishchenko committed
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
    // update the coordinates
    for ( k = 0; k < p->nObjs; k++ )
        pY0[k] = (pY0[k] != 0.0) ? ((pY0[k] - Min0) * Str0) : 0.0;

    // compute intervals
    Min1 =  ABC_INFINITY;
    Max1 = -ABC_INFINITY;
    pY1 = Emb_ManSol( p, 1 );
    for ( k = 0; k < p->nObjs; k++ )
    {
1454 1455
        Min1 = Abc_MinInt( Min1, pY1[k] );
        Max1 = Abc_MaxInt( Max1, pY1[k] );
Alan Mishchenko committed
1456
    }
Alan Mishchenko committed
1457
    Str1 = 1.0*GIA_PLACE_SIZE/(Max1 - Min1);
Alan Mishchenko committed
1458 1459 1460 1461 1462
    // update the coordinates
    for ( k = 0; k < p->nObjs; k++ )
        pY1[k] = (pY1[k] != 0.0) ? ((pY1[k] - Min1) * Str1) : 0.0;

    // derive the order of these numbers
Alan Mishchenko committed
1463 1464
    pPerm0 = Gia_SortFloats( pY0, NULL, p->nObjs );
    pPerm1 = Gia_SortFloats( pY1, NULL, p->nObjs );
Alan Mishchenko committed
1465

Alan Mishchenko committed
1466
    // average solutions and project them into square [0;GIA_PLACE_SIZE] x [0;GIA_PLACE_SIZE]
Alan Mishchenko committed
1467 1468 1469
    p->pPlacement = ABC_ALLOC( unsigned short, 2 * p->nObjs );
    for ( k = 0; k < p->nObjs; k++ )
    {
Alan Mishchenko committed
1470 1471
        p->pPlacement[2*pPerm0[k]+0] = (unsigned short)(int)(1.0 * k * GIA_PLACE_SIZE / p->nObjs);
        p->pPlacement[2*pPerm1[k]+1] = (unsigned short)(int)(1.0 * k * GIA_PLACE_SIZE / p->nObjs);
Alan Mishchenko committed
1472 1473 1474 1475 1476
    }
    ABC_FREE( pPerm0 );
    ABC_FREE( pPerm1 );
}

Alan Mishchenko committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

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

  Synopsis    [Computes wire-length.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
double Emb_ManComputeHPWL( Emb_Man_t * p )
{
    double Result = 0.0;
    Emb_Obj_t * pThis, * pNext;
    int i, k, iMinX, iMaxX, iMinY, iMaxY;
    if ( p->pPlacement == NULL )
        return 0.0;
    Emb_ManForEachObj( p, pThis, i )
    {
        iMinX = iMaxX = p->pPlacement[2*pThis->Value+0];
        iMinY = iMaxY = p->pPlacement[2*pThis->Value+1];
        Emb_ObjForEachFanout( pThis, pNext, k )
        {
1502 1503 1504 1505
            iMinX = Abc_MinInt( iMinX, p->pPlacement[2*pNext->Value+0] );
            iMaxX = Abc_MaxInt( iMaxX, p->pPlacement[2*pNext->Value+0] );
            iMinY = Abc_MinInt( iMinY, p->pPlacement[2*pNext->Value+1] );
            iMaxY = Abc_MaxInt( iMaxY, p->pPlacement[2*pNext->Value+1] );
Alan Mishchenko committed
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
        }
        Result += (iMaxX - iMinX) + (iMaxY - iMinY);
    }
    return Result;
}


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

  Synopsis    [Performs iterative refinement of the given placement.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManPlacementRefine( Emb_Man_t * p, int nIters, int fVerbose )
{
    Emb_Obj_t * pThis, * pNext;
    double CostThis, CostPrev;
    float * pEdgeX, * pEdgeY;
    float * pVertX, * pVertY;
    float VertX, VertY;
    int * pPermX, * pPermY;
    int i, k, Iter, iMinX, iMaxX, iMinY, iMaxY;
1533
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
    if ( p->pPlacement == NULL )
        return;
    pEdgeX = ABC_ALLOC( float, p->nObjs );
    pEdgeY = ABC_ALLOC( float, p->nObjs );
    pVertX = ABC_ALLOC( float, p->nObjs );
    pVertY = ABC_ALLOC( float, p->nObjs );
    // refine placement
    CostPrev = 0.0;
    for ( Iter = 0; Iter < nIters; Iter++ )
    {
        // compute centers of hyperedges
        CostThis = 0.0;
        Emb_ManForEachObj( p, pThis, i )
        {
            iMinX = iMaxX = p->pPlacement[2*pThis->Value+0];
            iMinY = iMaxY = p->pPlacement[2*pThis->Value+1];
            Emb_ObjForEachFanout( pThis, pNext, k )
            {
1552 1553 1554 1555
                iMinX = Abc_MinInt( iMinX, p->pPlacement[2*pNext->Value+0] );
                iMaxX = Abc_MaxInt( iMaxX, p->pPlacement[2*pNext->Value+0] );
                iMinY = Abc_MinInt( iMinY, p->pPlacement[2*pNext->Value+1] );
                iMaxY = Abc_MaxInt( iMaxY, p->pPlacement[2*pNext->Value+1] );
Alan Mishchenko committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
            }
            pEdgeX[pThis->Value] = 0.5 * (iMaxX + iMinX);
            pEdgeY[pThis->Value] = 0.5 * (iMaxY + iMinY);
            CostThis += (iMaxX - iMinX) + (iMaxY - iMinY);
        }
        // compute new centers of objects
        Emb_ManForEachObj( p, pThis, i )
        {
            VertX = pEdgeX[pThis->Value];
            VertY = pEdgeY[pThis->Value];
            Emb_ObjForEachFanin( pThis, pNext, k )
            {
                VertX += pEdgeX[pNext->Value];
                VertY += pEdgeY[pNext->Value];
            }
            pVertX[pThis->Value] = VertX / (Emb_ObjFaninNum(pThis) + 1);
            pVertY[pThis->Value] = VertY / (Emb_ObjFaninNum(pThis) + 1);
        }
        // sort these numbers
        pPermX = Gia_SortFloats( pVertX, NULL, p->nObjs );
        pPermY = Gia_SortFloats( pVertY, NULL, p->nObjs );
        for ( k = 0; k < p->nObjs; k++ )
        {
Alan Mishchenko committed
1579 1580
            p->pPlacement[2*pPermX[k]+0] = (unsigned short)(int)(1.0 * k * GIA_PLACE_SIZE / p->nObjs);
            p->pPlacement[2*pPermY[k]+1] = (unsigned short)(int)(1.0 * k * GIA_PLACE_SIZE / p->nObjs);
Alan Mishchenko committed
1581 1582 1583 1584 1585 1586 1587
        }
        ABC_FREE( pPermX );
        ABC_FREE( pPermY );
        // evaluate cost
        if ( fVerbose )
        {
        printf( "%2d : HPWL = %e  ", Iter+1, CostThis );
1588
        ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
1589 1590 1591 1592 1593 1594 1595 1596 1597
        }
    }
    ABC_FREE( pEdgeX );
    ABC_FREE( pEdgeY );
    ABC_FREE( pVertX );
    ABC_FREE( pVertY );
}


Alan Mishchenko committed
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
/**Function*************************************************************

  Synopsis    [Derives solutions from original vectors and eigenvectors.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Emb_ManPrintSolutions( Emb_Man_t * p, int nSols )
{
    float * pSol;
    int i, k;
Alan Mishchenko committed
1613
    for ( i = 0; i < nSols; i++ )
Alan Mishchenko committed
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
    {
        pSol = Emb_ManSol( p, i );
        for ( k = 0; k < p->nObjs; k++ )
            printf( "%4d ", (int)(100 * pSol[k]) );
        printf( "\n" );
    }
}

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

Alan Mishchenko committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
  Synopsis    [Prepares image for dumping.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Emb_ManDumpGnuplotPrepare( Emb_Man_t * p )
{
//    int nRows = 496;
//    int nCols = 710;
    int nRows = 500;
    int nCols = 700;
    Vec_Int_t * vLines;
    Emb_Obj_t * pThis;
    char * pBuffer, ** ppRows;
    int i, k, placeX, placeY;
    int fStart;
    // alloc memory
    pBuffer = ABC_CALLOC( char, nRows * (nCols+1) );
    ppRows  = ABC_ALLOC( char *, nRows );
    for ( i = 0; i < nRows; i++ )
        ppRows[i] = pBuffer + i*(nCols+1);
    // put data into them
    Emb_ManForEachObj( p, pThis, i )
    {
        placeX = p->pPlacement[2*pThis->Value+0] * nCols / (1<<16);
        placeY = p->pPlacement[2*pThis->Value+1] * nRows / (1<<16);
        assert( placeX < nCols && placeY < nRows );
        ppRows[placeY][placeX] = 1;
    }
    // select lines
    vLines = Vec_IntAlloc( 1000 );
    for ( i = 0; i < nRows; i++ )
    {
        fStart = 0;
        for ( k = 0; k <= nCols; k++ )
        {
            if ( ppRows[i][k] && !fStart )
            {
                Vec_IntPush( vLines, k );
                Vec_IntPush( vLines, i );
                fStart = 1;
            }
            if ( !ppRows[i][k] && fStart )
            {
                Vec_IntPush( vLines, k-1 );
                Vec_IntPush( vLines, i );
                fStart = 0;
            }
        }
        assert( fStart == 0 );
    }
    ABC_FREE( pBuffer );
    ABC_FREE( ppRows );
    return vLines;
}

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

Alan Mishchenko committed
1686 1687 1688 1689 1690 1691 1692 1693 1694
  Synopsis    [Derives solutions from original vectors and eigenvectors.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1695
void Emb_ManDumpGnuplot( Emb_Man_t * p, char * pName, int fDumpLarge, int fShowImage )
Alan Mishchenko committed
1696
{
Alan Mishchenko committed
1697
    extern void Gia_ManGnuplotShow( char * pPlotFileName );
Alan Mishchenko committed
1698 1699
//    char * pDirectory = "place\\";
    char * pDirectory = "";
1700
//    extern char * Ioa_TimeStamp();
Alan Mishchenko committed
1701 1702 1703 1704 1705 1706 1707 1708 1709
    FILE * pFile;
    char Buffer[1000];
    Emb_Obj_t * pThis, * pNext;
    int i, k;
    if ( p->pPlacement == NULL )
    {
        printf( "Emb_ManDumpGnuplot(): Placement is not available.\n" );
        return;
    }
Alan Mishchenko committed
1710
    sprintf( Buffer, "%s%s", pDirectory, Gia_FileNameGenericAppend(pName, ".plt") ); 
Alan Mishchenko committed
1711 1712 1713 1714
    pFile = fopen( Buffer, "w" );
    fprintf( pFile, "# This Gnuplot file was produced by ABC on %s\n", Ioa_TimeStamp() );
    fprintf( pFile, "\n" );
    fprintf( pFile, "set nokey\n" );
Alan Mishchenko committed
1715 1716 1717
    fprintf( pFile, "\n" );
    if ( !fShowImage )
    {
Alan Mishchenko committed
1718 1719
//    fprintf( pFile, "set terminal postscript\n" );
    fprintf( pFile, "set terminal gif font \'arial\' 10 size 800,600 xffffff x000000 x000000 x000000\n" );
Alan Mishchenko committed
1720
    fprintf( pFile, "set output \'%s\'\n", Gia_FileNameGenericAppend(pName, ".gif") );
Alan Mishchenko committed
1721 1722
    fprintf( pFile, "\n" );
    }
Alan Mishchenko committed
1723 1724
    fprintf( pFile, "set title \"%s :  PI = %d   PO = %d   FF = %d   Node = %d   Obj = %d  HPWL = %.2e\\n", 
        pName, Emb_ManPiNum(p), Emb_ManPoNum(p), Emb_ManRegNum(p), Emb_ManNodeNum(p), Emb_ManObjNum(p), Emb_ManComputeHPWL(p) );
Alan Mishchenko committed
1725 1726 1727 1728 1729
    fprintf( pFile, "(image generated by ABC and Gnuplot on %s)\"", Ioa_TimeStamp() );
    fprintf( pFile, "font \"Times, 12\"\n" );
    fprintf( pFile, "\n" );
    fprintf( pFile, "plot [:] '-' w l\n" );
    fprintf( pFile, "\n" );
Alan Mishchenko committed
1730
    if ( fDumpLarge )
Alan Mishchenko committed
1731
    {
Alan Mishchenko committed
1732 1733 1734
        int begX, begY, endX, endY;
        Vec_Int_t * vLines = Emb_ManDumpGnuplotPrepare( p );
        Vec_IntForEachEntry( vLines, begX, i )
Alan Mishchenko committed
1735
        {
Alan Mishchenko committed
1736 1737 1738 1739 1740 1741
            begY = Vec_IntEntry( vLines, i+1 );
            endX = Vec_IntEntry( vLines, i+2 );
            endY = Vec_IntEntry( vLines, i+3 );
            i += 3;
            fprintf( pFile, "%5d %5d\n", begX, begY );
            fprintf( pFile, "%5d %5d\n", endX, endY );
Alan Mishchenko committed
1742 1743
            fprintf( pFile, "\n" );
        }
Alan Mishchenko committed
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
        Vec_IntFree( vLines );
    }
    else
    {
        Emb_ManForEachObj( p, pThis, i )
        {
            if ( !Emb_ObjIsTravIdCurrent(p, pThis) )
                continue;
            Emb_ObjForEachFanout( pThis, pNext, k )
            {
                assert( Emb_ObjIsTravIdCurrent(p, pNext) );
                fprintf( pFile, "%5d %5d\n", p->pPlacement[2*pThis->Value+0], p->pPlacement[2*pThis->Value+1] );
                fprintf( pFile, "%5d %5d\n", p->pPlacement[2*pNext->Value+0], p->pPlacement[2*pNext->Value+1] );
                fprintf( pFile, "\n" );
            }
        }
Alan Mishchenko committed
1760 1761 1762
    }
    fprintf( pFile, "EOF\n" );
    fprintf( pFile, "\n" );
Alan Mishchenko committed
1763
    if ( fShowImage )
Alan Mishchenko committed
1764
    {
Alan Mishchenko committed
1765
    fprintf( pFile, "pause -1 \"Close window\"\n" );  // Hit return to continue
Alan Mishchenko committed
1766 1767 1768
    fprintf( pFile, "reset\n" );
    fprintf( pFile, "\n" );
    }
Alan Mishchenko committed
1769 1770 1771 1772 1773 1774
    else
    {
    fprintf( pFile, "# pause -1 \"Close window\"\n" );  // Hit return to continue
    fprintf( pFile, "# reset\n" );
    fprintf( pFile, "\n" );
    }
Alan Mishchenko committed
1775
    fclose( pFile );
Alan Mishchenko committed
1776 1777
    if ( fShowImage )
        Gia_ManGnuplotShow( Buffer );
Alan Mishchenko committed
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
}

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

  Synopsis    [Computes dimentions of the graph.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1791
void Gia_ManSolveProblem( Gia_Man_t * pGia, Emb_Par_t * pPars )
Alan Mishchenko committed
1792 1793
{
    Emb_Man_t * p;
1794
    int i;
1795 1796
    abctime clkSetup;
    abctime clk;
Alan Mishchenko committed
1797 1798
//   Gia_ManTestDistance( pGia );

Alan Mishchenko committed
1799
    // transform AIG into internal data-structure
1800
clk = Abc_Clock();
Alan Mishchenko committed
1801
    if ( pPars->fCluster )
Alan Mishchenko committed
1802 1803
    {
        p = Emb_ManStart( pGia );
Alan Mishchenko committed
1804
        if ( pPars->fVerbose )
Alan Mishchenko committed
1805
        {
Alan Mishchenko committed
1806
            printf( "Clustered: " );
Alan Mishchenko committed
1807 1808 1809 1810 1811
            Emb_ManPrintStats( p );
        }
    }
    else
        p = Emb_ManStartSimple( pGia );
Alan Mishchenko committed
1812
    p->fVerbose = pPars->fVerbose;
Alan Mishchenko committed
1813
//    Emb_ManPrintFanio( p );
Alan Mishchenko committed
1814 1815

    // prepare data-structure
Alan Mishchenko committed
1816
    Gia_ManRandom( 1 );  // reset random numbers for deterministic behavior
Alan Mishchenko committed
1817 1818
    Emb_ManResetTravId( p );
    Emb_ManSetValue( p );
1819
clkSetup = Abc_Clock() - clk;
Alan Mishchenko committed
1820

1821
clk = Abc_Clock();
Alan Mishchenko committed
1822 1823
    Emb_ManComputeDimensions( p, pPars->nDims );
if ( pPars->fVerbose )
Alan Mishchenko committed
1824
ABC_PRT( "Setup     ", clkSetup );
Alan Mishchenko committed
1825
if ( pPars->fVerbose )
1826
ABC_PRT( "Dimensions", Abc_Clock() - clk );
Alan Mishchenko committed
1827

1828
clk = Abc_Clock();
Alan Mishchenko committed
1829 1830
    Emb_ManComputeCovariance( p, pPars->nDims );
if ( pPars->fVerbose )
1831
ABC_PRT( "Matrix    ", Abc_Clock() - clk );
Alan Mishchenko committed
1832

1833
clk = Abc_Clock();
Alan Mishchenko committed
1834 1835 1836 1837
    Emb_ManComputeEigenvectors( p, pPars->nDims, pPars->nSols );
    Emb_ManComputeSolutions( p, pPars->nDims, pPars->nSols );
    Emb_ManDerivePlacement( p, pPars->nSols );
if ( pPars->fVerbose )
1838
ABC_PRT( "Eigenvecs ", Abc_Clock() - clk );
Alan Mishchenko committed
1839

Alan Mishchenko committed
1840 1841
    if ( pPars->fRefine )
    {
1842
clk = Abc_Clock();
Alan Mishchenko committed
1843 1844
    Emb_ManPlacementRefine( p, pPars->nIters, pPars->fVerbose );
if ( pPars->fVerbose )
1845
ABC_PRT( "Refinement", Abc_Clock() - clk );
Alan Mishchenko committed
1846 1847 1848 1849
    }

    if ( (pPars->fDump || pPars->fDumpLarge) && pPars->nSols == 2 )
    {
1850
clk = Abc_Clock();
Alan Mishchenko committed
1851 1852
        Emb_ManDumpGnuplot( p, pGia->pName, pPars->fDumpLarge, pPars->fShowImage );
if ( pPars->fVerbose )
1853
ABC_PRT( "Image dump", Abc_Clock() - clk );
Alan Mishchenko committed
1854 1855
    }

Alan Mishchenko committed
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
    // transfer placement
    if ( Gia_ManObjNum(pGia) == p->nObjs )
    {
        // assuming normalized ordering of the AIG
        pGia->pPlacement = ABC_CALLOC( Gia_Plc_t, p->nObjs );
        for ( i = 0; i < p->nObjs; i++ ) 
        {
            pGia->pPlacement[i].xCoord = p->pPlacement[2*i+0];
            pGia->pPlacement[i].yCoord = p->pPlacement[2*i+1];
        }
    }
Alan Mishchenko committed
1867 1868 1869 1870 1871 1872 1873 1874
    Emb_ManStop( p );
}

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


1875 1876
ABC_NAMESPACE_IMPL_END