giaCone.c 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [giaCone.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
22
#include "misc/extra/extra.h"
23 24
#include "misc/vec/vecHsh.h"
#include "misc/vec/vecWec.h"
25 26 27 28 29 30 31 32

ABC_NAMESPACE_IMPL_START


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

33 34 35 36 37 38 39 40 41 42
typedef struct Opa_Man_t_ Opa_Man_t;
struct Opa_Man_t_
{
    Gia_Man_t *    pGia;
    Vec_Int_t *    vFront;
    Vec_Int_t *    pvParts;
    int *          pId2Part; 
    int            nParts;
};

43 44 45
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Opa_Man_t * Opa_ManStart( Gia_Man_t * pGia)
{
    Opa_Man_t * p;
    Gia_Obj_t * pObj;
    int i;
    p = ABC_CALLOC( Opa_Man_t, 1 );
    p->pGia = pGia;
    p->pvParts  = ABC_CALLOC( Vec_Int_t, Gia_ManPoNum(pGia) );
    p->pId2Part = ABC_FALLOC( int, Gia_ManObjNum(pGia) );
    p->vFront   = Vec_IntAlloc( 100 );
    Gia_ManForEachPo( pGia, pObj, i )
    {
        Vec_IntPush( p->pvParts + i, Gia_ObjId(pGia, pObj) );
        p->pId2Part[Gia_ObjId(pGia, pObj)] = i;
        Vec_IntPush( p->vFront, Gia_ObjId(pGia, pObj) );
    }
    p->nParts = Gia_ManPoNum(pGia);
    return p;
}
static inline void Opa_ManStop( Opa_Man_t * p )
{
    int i;
    Vec_IntFree( p->vFront );
    for ( i = 0; i < Gia_ManPoNum(p->pGia); i++ )
        ABC_FREE( p->pvParts[i].pArray );
    ABC_FREE( p->pvParts );
    ABC_FREE( p->pId2Part );
    ABC_FREE( p );
}
static inline void Opa_ManPrint( Opa_Man_t * p )
{
    int i, k;
    printf( "Groups:\n" );
    for ( i = 0; i < Gia_ManPoNum(p->pGia); i++ )
    {
        if ( p->pvParts[i].nSize == 0 )
            continue;
        printf( "%3d : ", i );
        for ( k = 0; k < p->pvParts[i].nSize; k++ )
            printf( "%d ", p->pvParts[i].pArray[k] );
        printf( "\n" );
    }
}
static inline void Opa_ManPrint2( Opa_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i, k, Count;
    printf( "Groups %d: ", p->nParts );
    for ( i = 0; i < Gia_ManPoNum(p->pGia); i++ )
    {
        if ( p->pvParts[i].nSize == 0 )
            continue;
        // count POs in this group
        Count = 0;
        Gia_ManForEachObjVec( p->pvParts + i, p->pGia, pObj, k )
            Count += Gia_ObjIsPo(p->pGia, pObj);
        printf( "%d ", Count );
    }
    printf( "\n" );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Opa_ManMoveOne( Opa_Man_t * p, Gia_Obj_t * pObj, Gia_Obj_t * pFanin )
{
    int iObj   = Gia_ObjId(p->pGia, pObj);
    int iFanin = Gia_ObjId(p->pGia, pFanin);
    if ( iFanin == 0 )
        return;
    assert( p->pId2Part[ iObj ] >= 0 );
    if ( p->pId2Part[ iFanin ] == -1 )
    {
        p->pId2Part[ iFanin ] = p->pId2Part[ iObj ];
        Vec_IntPush( p->pvParts + p->pId2Part[ iObj ], iFanin );
        assert( Gia_ObjIsCi(pFanin) || Gia_ObjIsAnd(pFanin) );
        if ( Gia_ObjIsAnd(pFanin) )
            Vec_IntPush( p->vFront, iFanin );
        else if ( Gia_ObjIsRo(p->pGia, pFanin) )
        {
            pFanin = Gia_ObjRoToRi(p->pGia, pFanin);
            iFanin = Gia_ObjId(p->pGia, pFanin);
            assert( p->pId2Part[ iFanin ] == -1 );
            p->pId2Part[ iFanin ] = p->pId2Part[ iObj ];
            Vec_IntPush( p->pvParts + p->pId2Part[ iObj ], iFanin );
            Vec_IntPush( p->vFront, iFanin );
        }
    }
    else if ( p->pId2Part[ iObj ] != p->pId2Part[ iFanin ] )
    {
        Vec_Int_t * vPartObj = p->pvParts + p->pId2Part[ iObj ];
        Vec_Int_t * vPartFan = p->pvParts + p->pId2Part[ iFanin ];
        int iTemp, i;
//        printf( "Moving %d to %d (%d -> %d)\n", iObj, iFanin, Vec_IntSize(vPartObj), Vec_IntSize(vPartFan) );
        // add group of iObj to group of iFanin
        assert( Vec_IntSize(vPartObj) > 0 );
        Vec_IntForEachEntry( vPartObj, iTemp, i )
        {
            Vec_IntPush( vPartFan, iTemp );
            p->pId2Part[ iTemp ] = p->pId2Part[ iFanin ];
        }
        Vec_IntShrink( vPartObj, 0 );
        p->nParts--;
    }
}
void Opa_ManPerform( Gia_Man_t * pGia )
{
    Opa_Man_t * p;
    Gia_Obj_t * pObj;
Alan Mishchenko committed
175 176
    int i, Limit, Count = 0;
 
177
    p = Opa_ManStart( pGia );
Alan Mishchenko committed
178
    Limit = Vec_IntSize(p->vFront);
179 180 181
//Opa_ManPrint2( p );
    Gia_ManForEachObjVec( p->vFront, pGia, pObj, i )
    {
Alan Mishchenko committed
182 183 184 185 186 187 188
        if ( i == Limit )
        {
            printf( "%6d : %6d -> %6d\n", ++Count, i, p->nParts );
            Limit = Vec_IntSize(p->vFront);
            if ( Count > 1 )
                Opa_ManPrint2( p );
        }
189 190 191 192 193 194 195 196 197
//        printf( "*** Object %d  ", Gia_ObjId(pGia, pObj) );
        if ( Gia_ObjIsAnd(pObj) )
        {
            Opa_ManMoveOne( p, pObj, Gia_ObjFanin0(pObj) );
            Opa_ManMoveOne( p, pObj, Gia_ObjFanin1(pObj) );
        }
        else if ( Gia_ObjIsCo(pObj) )
            Opa_ManMoveOne( p, pObj, Gia_ObjFanin0(pObj) );
        else assert( 0 );
Alan Mishchenko committed
198 199
//        if ( i % 10 == 0 )
//            printf( "%d   ", p->nParts );
200 201
        if ( p->nParts == 1 )
            break;
Alan Mishchenko committed
202 203
        if ( Count == 5 )
            break;
204 205 206 207 208 209
    }
    printf( "\n" );
    Opa_ManStop( p );
}


210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManConeMark_rec( Gia_Man_t * p, Gia_Obj_t * pObj, Vec_Int_t * vRoots, int nLimit )
{
    if ( Gia_ObjIsTravIdCurrent(p, pObj) )
        return 0;
    Gia_ObjSetTravIdCurrent(p, pObj);
    if ( Gia_ObjIsAnd(pObj) )
    {
        if ( Gia_ManConeMark_rec( p, Gia_ObjFanin0(pObj), vRoots, nLimit ) )
            return 1;
        if ( Gia_ManConeMark_rec( p, Gia_ObjFanin1(pObj), vRoots, nLimit ) )
            return 1;
    }
    else if ( Gia_ObjIsCo(pObj) )
    {
        if ( Gia_ManConeMark_rec( p, Gia_ObjFanin0(pObj), vRoots, nLimit ) )
            return 1;
    }
    else if ( Gia_ObjIsRo(p, pObj) )
        Vec_IntPush( vRoots, Gia_ObjId(p, Gia_ObjRoToRi(p, pObj)) );
    else if ( Gia_ObjIsPi(p, pObj) )
    {}
    else assert( 0 );
    return (int)(Vec_IntSize(vRoots) > nLimit);
}
int Gia_ManConeMark( Gia_Man_t * p, int iOut, int Limit )
{
    Vec_Int_t * vRoots;
    Gia_Obj_t * pObj;
    int i, RetValue;
    // start the outputs
    pObj = Gia_ManPo( p, iOut );
    vRoots = Vec_IntAlloc( 100 );
    Vec_IntPush( vRoots, Gia_ObjId(p, pObj) );
    // mark internal nodes
    Gia_ManIncrementTravId( p );
    Gia_ObjSetTravIdCurrent( p, Gia_ManConst0(p) );
    Gia_ManForEachObjVec( vRoots, p, pObj, i )
        if ( Gia_ManConeMark_rec( p, pObj, vRoots, Limit ) )
            break;
    RetValue = Vec_IntSize( vRoots ) - 1;
    Vec_IntFree( vRoots );
    return RetValue;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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
int Gia_ManCountFlops( Gia_Man_t * p, Vec_Int_t * vOuts )
{
    int Limit = ABC_INFINITY;
    Vec_Int_t * vRoots;
    Gia_Obj_t * pObj;
    int i, RetValue, iOut;
    // start the outputs
    vRoots = Vec_IntAlloc( 100 );
    Vec_IntForEachEntry( vOuts, iOut, i )
    {
        pObj = Gia_ManPo( p, iOut );
        Vec_IntPush( vRoots, Gia_ObjId(p, pObj) );
    }
    // mark internal nodes
    Gia_ManIncrementTravId( p );
    Gia_ObjSetTravIdCurrent( p, Gia_ManConst0(p) );
    Gia_ManForEachObjVec( vRoots, p, pObj, i )
        if ( Gia_ManConeMark_rec( p, pObj, vRoots, Limit ) )
            break;
    RetValue = Vec_IntSize( vRoots ) - Vec_IntSize(vOuts);
    Vec_IntFree( vRoots );
    return RetValue;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFindPoPartition3( Gia_Man_t * p, int iOut, int nDelta, int nOutsMin, int nOutsMax, int fVerbose, Vec_Ptr_t ** pvPosEquivs )
313
{
314
/*
315 316 317 318 319 320 321 322
    int i, Count = 0;
    // mark nodes belonging to output 'iOut'
    for ( i = 0; i < Gia_ManPoNum(p); i++ )
        Count += (Gia_ManConeMark(p, i, 10000) < 10000);
     //   printf( "%d ", Gia_ManConeMark(p, i, 1000) );
    printf( "%d out of %d\n", Count, Gia_ManPoNum(p) );

    // add other outputs as long as they are nDelta away
323
*/
324
//    Opa_ManPerform( p );
325 326 327 328

    return NULL;
}

329 330 331 332 333 334 335 336 337 338 339 340

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
341
Vec_Int_t * Gia_ManFindPivots( Gia_Man_t * p, int SelectShift, int fOnlyCis, int fVerbose )
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
{
    Vec_Int_t * vPivots, * vWeights;
    Vec_Int_t * vCount, * vResult;
    int i, j, Count, * pPerm, Limit;
/*
    Gia_Obj_t * pObj;
    // count MUX controls
    vCount = Vec_IntStart( Gia_ManObjNum(p) );
    Gia_ManForEachAnd( p, pObj, i )
    {
        Gia_Obj_t * pNodeC, * pNodeT, * pNodeE;
        if ( !Gia_ObjIsMuxType(pObj) ) 
            continue;
        pNodeC = Gia_ObjRecognizeMux( pObj, &pNodeT, &pNodeE );
        Vec_IntAddToEntry( vCount, Gia_ObjId(p, Gia_Regular(pNodeC)), 1 );
    }
*/
    // count references
    Gia_ManCreateRefs( p );
    vCount = Vec_IntAllocArray( p->pRefs, Gia_ManObjNum(p) ); p->pRefs = NULL;

    // collect nodes 
    vPivots  = Vec_IntAlloc( 100 );
    vWeights = Vec_IntAlloc( 100 );
    Vec_IntForEachEntry( vCount, Count, i )
    {
        if ( Count < 2 ) continue;
369 370
        if ( fOnlyCis && !Gia_ObjIsCi(Gia_ManObj(p, i)) )
            continue;
371 372 373 374 375 376
        Vec_IntPush( vPivots, i );
        Vec_IntPush( vWeights, Count );
    }
    Vec_IntFree( vCount );

    if ( fVerbose )
377
        printf( "Selected %d pivots with more than one fanout (out of %d CIs and ANDs).\n", Vec_IntSize(vWeights), Gia_ManCiNum(p) + Gia_ManAndNum(p) );
378 379 380 381 382 383 384 385 386 387 388

    // permute
    Gia_ManRandom(1);
    Gia_ManRandom(0);
    for ( i = 0; i < Vec_IntSize(vWeights); i++ )
    {
        j = (Gia_ManRandom(0) >> 1) % Vec_IntSize(vWeights);
        ABC_SWAP( int, vPivots->pArray[i], vPivots->pArray[j] );
        ABC_SWAP( int, vWeights->pArray[i], vWeights->pArray[j] );
    }
    // sort
389 390 391 392 393 394 395 396 397
    if ( SelectShift == 0 )
        pPerm = Abc_QuickSortCost( Vec_IntArray(vWeights), Vec_IntSize(vWeights), 1 );
    else
    {
        Vec_Int_t * vTemp = Vec_IntStartNatural( Vec_IntSize(vWeights) );
        pPerm = Vec_IntReleaseArray( vTemp );
        Vec_IntFree( vTemp );
    }

398 399 400 401 402 403 404
    // select    
    Limit = Abc_MinInt( 64, Vec_IntSize(vWeights) );
    vResult = Vec_IntAlloc( Limit );
    for ( i = 0; i < Limit; i++ )
    {
        j = (i + SelectShift) % Vec_IntSize(vWeights);
        if ( fVerbose )
405
            printf( "%2d : Pivot =%7d  Fanout =%7d\n", j, Vec_IntEntry(vPivots, pPerm[j]), Vec_IntEntry(vWeights, pPerm[j]) );
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
        Vec_IntPush( vResult, Vec_IntEntry(vPivots, pPerm[j]) );
    }

    Vec_IntFree( vPivots );
    Vec_IntFree( vWeights );
    ABC_FREE( pPerm );

    return vResult;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wrd_t * Gia_ManDeriveSigns( Gia_Man_t * p, Vec_Int_t * vPivots, int fVerbose )
{
    Vec_Wrd_t * vSigns;
    Gia_Obj_t * pObj, * pObjRi;
    int i, fChange = 1, Counter;

    Gia_ManFillValue( p );
    Gia_ManForEachObjVec( vPivots, p, pObj, i )
        pObj->Value = i;

    if ( fVerbose )
        printf( "Signature propagation: " );
    vSigns = Vec_WrdStart( Gia_ManObjNum(p) );
    while ( fChange )
    {
        fChange = 0;
        Gia_ManForEachObj( p, pObj, i )
        {
            if ( ~pObj->Value )
            {
                assert( pObj->Value >= 0 && pObj->Value < 64 );
448
                *Vec_WrdEntryP( vSigns, i ) |= ( (word)1 << pObj->Value );
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
            }
            if ( Gia_ObjIsAnd(pObj) )
                *Vec_WrdEntryP( vSigns, i ) |= Vec_WrdEntry(vSigns, Gia_ObjFaninId0(pObj, i)) | Vec_WrdEntry(vSigns, Gia_ObjFaninId1(pObj, i));
            else if ( Gia_ObjIsCo(pObj) )
                *Vec_WrdEntryP( vSigns, i ) |= Vec_WrdEntry(vSigns, Gia_ObjFaninId0(pObj, i));
        }
        Counter = 0;
        Gia_ManForEachRiRo( p, pObjRi, pObj, i )
        {
            word Value = Vec_WrdEntry(vSigns, Gia_ObjId(p, pObj));
            *Vec_WrdEntryP( vSigns, Gia_ObjId(p, pObj) ) |= Vec_WrdEntry(vSigns, Gia_ObjId(p, pObjRi));
            if ( Value != Vec_WrdEntry(vSigns, Gia_ObjId(p, pObj)) )
                fChange = 1, Counter++;
        }
        if ( fVerbose )
            printf( "%d ", Counter );
    }
    if ( fVerbose )
       printf( "\n" );
    return vSigns;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Gia_ManHashOutputs( Gia_Man_t * p, Vec_Wrd_t * vSigns, int fVerbose )
{
    Vec_Ptr_t * vBins;
485 486 487 488 489 490 491
    Vec_Wec_t * vClasses;
    Vec_Wrd_t * vSignsPo;
    Vec_Int_t * vPriority, * vBin;
    Gia_Obj_t * pObj;
    int i;
    // collect PO signatures
    vSignsPo = Vec_WrdAlloc( Gia_ManPoNum(p) );
492
    Gia_ManForEachPo( p, pObj, i )
493 494 495 496 497 498 499 500
        Vec_WrdPush( vSignsPo, Vec_WrdEntry(vSigns, Gia_ObjId(p, pObj)) );
    // find equivalence classes
    vPriority = Hsh_WrdManHashArray( vSignsPo, 1 );
    Vec_WrdFree( vSignsPo );
    vClasses = Vec_WecCreateClasses( vPriority );
    Vec_IntFree( vPriority );
    vBins = (Vec_Ptr_t *)Vec_WecConvertToVecPtr( vClasses );
    Vec_WecFree( vClasses );
501 502 503 504
    Vec_VecSort( (Vec_Vec_t *)vBins, 1 );

    if ( fVerbose )
        printf( "Computed %d partitions:\n", Vec_PtrSize(vBins) );
505 506
    if ( !fVerbose )
        printf( "Listing partitions with more than 100 outputs:\n" );
507 508
    Vec_PtrForEachEntry( Vec_Int_t *, vBins, vBin, i )
    {
509 510
        assert( Vec_IntSize(vBin) > 0 );
        if ( fVerbose || Vec_IntSize(vBin) > 100 )
511
        {
512 513 514 515 516 517 518 519 520
            int PoNum = Vec_IntEntry( vBin, 0 );
            Gia_Obj_t * pObj = Gia_ManPo( p, PoNum );
            word Sign = Vec_WrdEntry( vSigns, Gia_ObjId(p, pObj) );
            // print
            printf( "%3d ", i );
            Extra_PrintBinary( stdout, (unsigned *)&Sign, 64 );
            printf( "  " );
            printf( "PO =%7d  ", Vec_IntSize(vBin) );
            printf( "FF =%7d", Gia_ManCountFlops(p, vBin) );
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
            printf( "\n" );
        }
    }
    return vBins;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManFindPoPartition2( Gia_Man_t * p, int iStartNum, int nDelta, int nOutsMin, int nOutsMax, int fSetLargest, int fVerbose, Vec_Ptr_t ** pvPosEquivs )
{
    return NULL;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
555
Gia_Man_t * Gia_ManFindPoPartition( Gia_Man_t * p, int SelectShift, int fOnlyCis, int fSetLargest, int fVerbose, Vec_Ptr_t ** pvPosEquivs )
556 557 558 559 560 561
{
    Gia_Man_t * pGia = NULL;
    Vec_Int_t * vPivots;
    Vec_Wrd_t * vSigns;
    Vec_Ptr_t * vParts;
    Vec_Int_t * vPart;
562
    abctime clk = Abc_Clock();
563
    vPivots = Gia_ManFindPivots( p, SelectShift, fOnlyCis, fVerbose );
564 565 566 567 568 569 570 571 572 573 574 575
    vSigns = Gia_ManDeriveSigns( p, vPivots, fVerbose );
    Vec_IntFree( vPivots );
    vParts = Gia_ManHashOutputs( p, vSigns, fVerbose );
    Vec_WrdFree( vSigns );
    if ( fSetLargest )
    {
        vPart = Vec_VecEntryInt( (Vec_Vec_t *)vParts, 0 );
        pGia = Gia_ManDupCones( p, Vec_IntArray(vPart), Vec_IntSize(vPart), 1 );
    }
    if ( pvPosEquivs )
    {
        *pvPosEquivs = vParts;
576
        printf( "The algorithm divided %d POs into %d partitions.   ", Gia_ManPoNum(p), Vec_PtrSize(vParts) );
577
        Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
578 579 580 581 582 583
    }
    else
        Vec_VecFree( (Vec_Vec_t *)vParts );
    return pGia;
}

584 585 586 587 588 589 590
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END