abcMffc.c 40 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
/**CFile****************************************************************

  FileName    [abcMffc.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Computing multi-output maximum fanout-free cones.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "abc.h"

ABC_NAMESPACE_IMPL_START
 
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Dereferences and collects the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_MffcDeref_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    Abc_Obj_t * pFanin;
    int i;
    if ( Abc_ObjIsCi(pNode) )
        return;
    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
        assert( pFanin->vFanouts.nSize > 0 );
        if ( --pFanin->vFanouts.nSize == 0 )
            Abc_MffcDeref_rec( pFanin, vNodes );
    }
    if ( vNodes )
        Vec_PtrPush( vNodes, pNode );
}

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

  Synopsis    [References the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_MffcRef_rec( Abc_Obj_t * pNode )
{
    Abc_Obj_t * pFanin;
    int i;
    if ( Abc_ObjIsCi(pNode) )
        return;
    Abc_ObjForEachFanin( pNode, pFanin, i )
    {
        if ( pFanin->vFanouts.nSize++ == 0 )
            Abc_MffcRef_rec( pFanin );
    }
}

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

  Synopsis    [Collects nodes belonging to the MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_MffcCollectNodes( Abc_Obj_t ** pNodes, int nNodes, Vec_Ptr_t * vNodes )
{
    int i;
    Vec_PtrClear( vNodes );
    for ( i = 0; i < nNodes; i++ )
        Abc_MffcDeref_rec( pNodes[i], vNodes );
    for ( i = 0; i < nNodes; i++ )
        Abc_MffcRef_rec( pNodes[i] );
}

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

  Synopsis    [Collects leaves of the MFFC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_MffcCollectLeaves( Vec_Ptr_t * vNodes, Vec_Ptr_t * vLeaves )
{
    Abc_Obj_t * pNode, * pFanin;
    int i, k;
    assert( Vec_PtrSize(vNodes) > 0 );
    pNode = (Abc_Obj_t *)Vec_PtrEntry( vNodes, 0 );
    // label them
    Abc_NtkIncrementTravId( pNode->pNtk );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
        Abc_NodeSetTravIdCurrent( pNode );
    // collect non-labeled fanins
    Vec_PtrClear( vLeaves );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
    Abc_ObjForEachFanin( pNode, pFanin, k )
    {
        if ( Abc_NodeIsTravIdCurrent(pFanin) )
            continue;
        Abc_NodeSetTravIdCurrent( pFanin );
        Vec_PtrPush( vLeaves, pFanin );
    }
}

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

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Vec_IntPrint( Vec_Int_t * vVec )
{
    int i, Entry;
    Vec_IntForEachEntry( vVec, Entry, i )
        printf( "%d ", Entry );
    printf( "\n" );
}


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

160 161 162 163 164 165 166 167 168
  Synopsis    [Collects internal nodes that are roots of MFFCs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
169
Vec_Ptr_t * Abc_NktMffcMarkRoots( Abc_Ntk_t * pNtk, int fSkipPis )
170 171 172 173 174 175 176 177 178 179
{
    Vec_Ptr_t * vRoots, * vNodes, * vLeaves;
    Abc_Obj_t * pObj, * pLeaf;
    int i, k;
    Abc_NtkCleanMarkA( pNtk );
    // mark the drivers of combinational outputs
    vRoots  = Vec_PtrAlloc( 1000 );
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        pObj = Abc_ObjFanin0( pObj );
180 181
//        if ( Abc_ObjIsCi(pObj) || Abc_ObjFaninNum(pObj) == 0 || pObj->fMarkA )
        if ( Abc_ObjIsCi(pObj) || pObj->fMarkA )
182 183 184 185 186 187 188 189 190
            continue;
        pObj->fMarkA = 1;
        Vec_PtrPush( vRoots, pObj );
    }
    // explore starting from the drivers
    vNodes  = Vec_PtrAlloc( 100 );
    vLeaves = Vec_PtrAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
191 192
        if ( Abc_ObjIsCi(pObj) )
            continue;
193 194 195 196 197 198 199
        // collect internal nodes 
        Abc_MffcCollectNodes( &pObj, 1, vNodes );
        // collect leaves
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        // add non-PI leaves
        Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pLeaf, k )
        {
200
            if ( (fSkipPis && Abc_ObjIsCi(pLeaf)) || pLeaf->fMarkA )
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 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 367 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 397 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
                continue;
            pLeaf->fMarkA = 1;
            Vec_PtrPush( vRoots, pLeaf );
        }
    }
    Vec_PtrFree( vLeaves );
    Vec_PtrFree( vNodes );
    Abc_NtkCleanMarkA( pNtk );
    return vRoots;
}

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

  Synopsis    [Collect fanout reachable root nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcCollectFanout_rec( Abc_Obj_t * pObj, Vec_Ptr_t * vFanouts )
{
    Abc_Obj_t * pFanout;
    int i;
    if ( Abc_ObjIsCo(pObj) )
        return;
    if ( Abc_ObjFanoutNum(pObj) > 64 )
        return;
    if ( Abc_NodeIsTravIdCurrent(pObj) )
        return;
    Abc_NodeSetTravIdCurrent(pObj);
    if ( pObj->fMarkA )
    {
        if ( pObj->vFanouts.nSize > 0 )
            Vec_PtrPush( vFanouts, pObj );
        return;
    }
    Abc_ObjForEachFanout( pObj, pFanout, i )
        Abc_NktMffcCollectFanout_rec( pFanout, vFanouts );
}

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

  Synopsis    [Collect fanout reachable root nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcCollectFanout( Abc_Obj_t ** pNodes, int nNodes, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vFanouts )
{
    Abc_Obj_t * pFanin, * pFanout;
    int i, k;
    // dereference nodes
    for ( i = 0; i < nNodes; i++ )
        Abc_MffcDeref_rec( pNodes[i], NULL );
    // collect fanouts
    Vec_PtrClear( vFanouts );
    pFanin = (Abc_Obj_t *)Vec_PtrEntry( vLeaves, 0 );
    Abc_NtkIncrementTravId( pFanin->pNtk );
    Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pFanin, i )
        Abc_ObjForEachFanout( pFanin, pFanout, k )
            Abc_NktMffcCollectFanout_rec( pFanout, vFanouts );
    // reference nodes
    for ( i = 0; i < nNodes; i++ )
        Abc_MffcRef_rec( pNodes[i] );
}

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

  Synopsis    [Grow one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NktMffcGrowOne( Abc_Ntk_t * pNtk, Abc_Obj_t ** ppObjs, int nObjs, Vec_Ptr_t * vNodes, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vFanouts )
{
    Abc_Obj_t * pFanout, * pFanoutBest = NULL;
    double CostBest = 0.0;
    int i, k;
    Abc_MffcCollectNodes( ppObjs, nObjs, vNodes );
    Abc_MffcCollectLeaves( vNodes, vLeaves );
    // collect fanouts of all fanins
    Abc_NktMffcCollectFanout( ppObjs, nObjs, vLeaves, vFanouts );
    // try different fanouts
    Vec_PtrForEachEntry( Abc_Obj_t *, vFanouts, pFanout, i )
    {
        for ( k = 0; k < nObjs; k++ )
            if ( pFanout == ppObjs[k] )
                break;
        if ( k < nObjs )
            continue;
        ppObjs[nObjs] = pFanout;
        Abc_MffcCollectNodes( ppObjs, nObjs+1, vNodes );
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        if ( pFanoutBest == NULL || CostBest < 1.0 * Vec_PtrSize(vNodes)/Vec_PtrSize(vLeaves) ) 
        {
            CostBest = 1.0 * Vec_PtrSize(vNodes)/Vec_PtrSize(vLeaves);
            pFanoutBest = pFanout;
        }
    }
    return pFanoutBest;
}

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

  Synopsis    [Procedure to increase MFF size by pairing nodes.]

  Description [For each node in the array vRoots, find a matching node, 
  so that the ratio of nodes inside to the leaf nodes is maximized.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NktMffcGrowRoots( Abc_Ntk_t * pNtk, Vec_Ptr_t * vRoots )
{
    Vec_Ptr_t * vRoots1, * vNodes, * vLeaves, * vFanouts;
    Abc_Obj_t * pObj, * pRoot2, * pNodes[2];
    int i;
    Abc_NtkCleanMarkA( pNtk );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
        pObj->fMarkA = 1;
    vRoots1  = Vec_PtrAlloc( 100 );
    vNodes   = Vec_PtrAlloc( 100 );
    vLeaves  = Vec_PtrAlloc( 100 );
    vFanouts = Vec_PtrAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        pNodes[0] = pObj;
        pRoot2 = Abc_NktMffcGrowOne( pNtk, pNodes, 1, vNodes, vLeaves, vFanouts );
        Vec_PtrPush( vRoots1, pRoot2 );
    }
    Vec_PtrFree( vNodes );
    Vec_PtrFree( vLeaves );
    Vec_PtrFree( vFanouts );
    Abc_NtkCleanMarkA( pNtk );
    return vRoots1;
}

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

  Synopsis    [Procedure to increase MFF size by pairing nodes.]

  Description [For each node in the array vRoots, find a matching node, 
  so that the ratio of nodes inside to the leaf nodes is maximized.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NktMffcGrowRootsAgain( Abc_Ntk_t * pNtk, Vec_Ptr_t * vRoots, Vec_Ptr_t * vRoots1 )
{
    Vec_Ptr_t * vRoots2, * vNodes, * vLeaves, * vFanouts;
    Abc_Obj_t * pObj, * pRoot2, * ppObjs[3];
    int i;
    Abc_NtkCleanMarkA( pNtk );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
        pObj->fMarkA = 1;
    vRoots2  = Vec_PtrAlloc( 100 );
    vNodes   = Vec_PtrAlloc( 100 );
    vLeaves  = Vec_PtrAlloc( 100 );
    vFanouts = Vec_PtrAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        ppObjs[0] = pObj;
        ppObjs[1] = (Abc_Obj_t *)Vec_PtrEntry( vRoots1, i );
        if ( ppObjs[1] == NULL )
        {
            Vec_PtrPush( vRoots2, NULL );
            continue;
        }
        pRoot2    = Abc_NktMffcGrowOne( pNtk, ppObjs, 2, vNodes, vLeaves, vFanouts );
        Vec_PtrPush( vRoots2, pRoot2 );
    }
    Vec_PtrFree( vNodes );
    Vec_PtrFree( vLeaves );
    Vec_PtrFree( vFanouts );
    Abc_NtkCleanMarkA( pNtk );
    assert( Vec_PtrSize(vRoots) == Vec_PtrSize(vRoots2) );
    return vRoots2;
}

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

  Synopsis    [Testbench.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcPrint( char * pFileName, Abc_Obj_t ** pNodes, int nNodes, Vec_Ptr_t * vNodes, Vec_Ptr_t * vLeaves )
{
    FILE * pFile;
    Abc_Obj_t * pObj, * pFanin;
    int i, k;
    // convert the network
    Abc_NtkToSop( pNodes[0]->pNtk, 0 );
    // write the file
    pFile = fopen( pFileName, "wb" );
    fprintf( pFile, ".model %s_part\n", pNodes[0]->pNtk->pName );
    fprintf( pFile, ".inputs" );
    Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pObj, i )
        fprintf( pFile, " %s", Abc_ObjName(pObj) );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".outputs" );
    for ( i = 0; i < nNodes; i++ )
        fprintf( pFile, " %s", Abc_ObjName(pNodes[i]) );
    fprintf( pFile, "\n" );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
        fprintf( pFile, ".names" );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            fprintf( pFile, " %s", Abc_ObjName(pFanin) );
        fprintf( pFile, " %s", Abc_ObjName(pObj) );
        fprintf( pFile, "\n%s", (char *)pObj->pData );
    }
    fprintf( pFile, ".end\n" );
    fclose( pFile );
}

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

  Synopsis    [Testbench.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
447 448 449 450 451 452 453 454 455 456 457
void Abc_NktMffcPrintInt( char * pFileName, Abc_Ntk_t * pNtk, Vec_Int_t * vRoots, Vec_Int_t * vNodes, Vec_Int_t * vLeaves )
{
    FILE * pFile;
    Abc_Obj_t * pObj, * pFanin;
    int i, k;
    // convert the network
    Abc_NtkToSop( pNtk, 0 );
    // write the file
    pFile = fopen( pFileName, "wb" );
    fprintf( pFile, ".model %s_part\n", pNtk->pName );
    fprintf( pFile, ".inputs" );
458
    Abc_NtkForEachObjVec( vLeaves, pNtk, pObj, i )
459 460 461
        fprintf( pFile, " %s", Abc_ObjName(pObj) );
    fprintf( pFile, "\n" );
    fprintf( pFile, ".outputs" );
462
    Abc_NtkForEachObjVec( vRoots, pNtk, pObj, i )
463 464
        fprintf( pFile, " %s", Abc_ObjName(pObj) );
    fprintf( pFile, "\n" );
465
    Abc_NtkForEachObjVec( vNodes, pNtk, pObj, i )
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    {
        fprintf( pFile, ".names" );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            fprintf( pFile, " %s", Abc_ObjName(pFanin) );
        fprintf( pFile, " %s", Abc_ObjName(pObj) );
        fprintf( pFile, "\n%s", (char *)pObj->pData );
    }
    fprintf( pFile, ".end\n" );
    fclose( pFile );
}

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

  Synopsis    [Testbench.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
488 489 490 491 492 493
void Abc_NktMffcTest( Abc_Ntk_t * pNtk )
{
    char pFileName[1000];
    Vec_Ptr_t * vRoots, * vRoots1, * vRoots2, * vNodes, * vLeaves;
    Abc_Obj_t * pNodes[3], * pObj;
    int i, nNodes = 0, nNodes2 = 0;
494
    vRoots  = Abc_NktMffcMarkRoots( pNtk, 1 );
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 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
    vRoots1 = Abc_NktMffcGrowRoots( pNtk, vRoots );
    vRoots2 = Abc_NktMffcGrowRootsAgain( pNtk, vRoots, vRoots1 );
    vNodes  = Vec_PtrAlloc( 100 );
    vLeaves = Vec_PtrAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        printf( "%6d : ",      i );

        Abc_MffcCollectNodes( &pObj, 1, vNodes );
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        nNodes += Vec_PtrSize(vNodes);
        printf( "%6d  ",      Abc_ObjId(pObj) );
        printf( "Vol =%3d  ", Vec_PtrSize(vNodes) );
        printf( "Cut =%3d  ", Vec_PtrSize(vLeaves) );
        if ( Vec_PtrSize(vLeaves) < 2 )
        {
            printf( "\n" );
            continue;
        }

        pNodes[0] = pObj;
        pNodes[1] = (Abc_Obj_t *)Vec_PtrEntry( vRoots1, i );
        pNodes[2] = (Abc_Obj_t *)Vec_PtrEntry( vRoots2, i );
        if ( pNodes[1] == NULL || pNodes[2] == NULL )
        {
            printf( "\n" );
            continue;
        }
        Abc_MffcCollectNodes( pNodes, 3, vNodes );
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        nNodes2 += Vec_PtrSize(vNodes);
        printf( "%6d  ",      Abc_ObjId(pNodes[1]) );
        printf( "%6d  ",      Abc_ObjId(pNodes[2]) );
        printf( "Vol =%3d  ", Vec_PtrSize(vNodes) );
        printf( "Cut =%3d  ", Vec_PtrSize(vLeaves) );
 
        printf( "%4.2f  ",    1.0 * Vec_PtrSize(vNodes)/Vec_PtrSize(vLeaves)  );
        printf( "\n" );

        // generate file
        if ( Vec_PtrSize(vNodes) < 10 )
            continue;
        sprintf( pFileName, "%s_mffc%04d_%02d.blif", Abc_NtkName(pNtk), Abc_ObjId(pObj), Vec_PtrSize(vNodes) );
        Abc_NktMffcPrint( pFileName, pNodes, 3, vNodes, vLeaves );
    }
    printf( "Total nodes = %d.  Root nodes = %d.  Mffc nodes = %d.  Mffc nodes2 = %d.\n", 
        Abc_NtkNodeNum(pNtk), Vec_PtrSize(vRoots), nNodes, nNodes2 );
    Vec_PtrFree( vNodes );
    Vec_PtrFree( vLeaves );
    Vec_PtrFree( vRoots );
    Vec_PtrFree( vRoots1 );
    Vec_PtrFree( vRoots2 );
}


550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 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 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 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 693 694 695 696 697 698 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 750 751 752 753 754 755 756 757 758 759

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

  Synopsis    [Create the network of supernodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcTestSuper( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vRoots, * vFanins, * vFanouts, * vNodes, * vLeaves;
    Abc_Obj_t * pObj, * pFanin; 
    Vec_Int_t * vCounts, * vNumbers, * vSizes, * vMarks;
    Vec_Int_t * vNode1, * vNode2;
    int i, k, Entry, nSizes, clk = clock();
    vRoots   = Abc_NktMffcMarkRoots( pNtk, 1 );
    vFanins  = Vec_PtrStart( Abc_NtkObjNumMax(pNtk) );
    vFanouts = Vec_PtrStart( Abc_NtkObjNumMax(pNtk) );
    vCounts  = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
    vNode1   = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
    vNode2   = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
    vSizes   = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
    vMarks   = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );

    // create fanins/fanouts
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Vec_PtrWriteEntry( vFanins,  Abc_ObjId(pObj), Vec_IntAlloc(8) );
        Vec_PtrWriteEntry( vFanouts, Abc_ObjId(pObj), Vec_IntAlloc(8) );
    }
    // add fanins/fanouts
    vNodes   = Vec_PtrAlloc( 100 );
    vLeaves  = Vec_PtrAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Abc_MffcCollectNodes( &pObj, 1, vNodes );
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pFanin, k )
        {
            if ( !Abc_ObjIsNode(pFanin) )
                continue;
            Vec_IntPush( (Vec_Int_t *)Vec_PtrEntry(vFanins,  Abc_ObjId(pObj)),   Abc_ObjId(pFanin) );
            Vec_IntPush( (Vec_Int_t *)Vec_PtrEntry(vFanouts, Abc_ObjId(pFanin)), Abc_ObjId(pObj) );
            // count how many times each object is a fanin
            Vec_IntAddToEntry( vCounts, Abc_ObjId(pFanin), 1 );
        }
        Vec_IntWriteEntry( vSizes, Abc_ObjId(pObj), Vec_PtrSize(vNodes) );
    }

    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Abc_MffcCollectNodes( &pObj, 1, vNodes );
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pFanin, k )
        {
            if ( !Abc_ObjIsNode(pFanin) )
                continue;
            if ( Vec_IntEntry(vCounts, Abc_ObjId(pFanin)) != 2 )
                continue;
            if ( Vec_IntEntry(vNode1, Abc_ObjId(pFanin)) == 0 )
                Vec_IntWriteEntry( vNode1, Abc_ObjId(pFanin), Abc_ObjId(pObj) );
            else //if ( Vec_IntEntry(vNode2, Abc_ObjId(pFanin)) == 0 )
                Vec_IntWriteEntry( vNode2, Abc_ObjId(pFanin), Abc_ObjId(pObj) );

            Vec_IntWriteEntry( vMarks, Abc_ObjId(pFanin), 1 );
            Vec_IntWriteEntry( vMarks, Abc_ObjId(pObj),   1 );
        }
    }

    // count sizes
    nSizes = 0;
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        if ( Vec_IntEntry( vMarks, Abc_ObjId(pObj) ) )
            nSizes += Vec_IntEntry( vSizes, Abc_ObjId(pObj) );
    }
    printf( "Included = %6d.  Total = %6d. (%6.2f %%)\n", 
        nSizes, Abc_NtkNodeNum(pNtk), 100.0 * nSizes / Abc_NtkNodeNum(pNtk) );


    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        if ( Vec_IntEntry(vCounts, Abc_ObjId(pObj)) != 2 )
            continue;
        printf( "%d ", Vec_IntEntry( vSizes, Abc_ObjId(pObj) ) + 
                       Vec_IntEntry( vSizes, Vec_IntEntry(vNode1, Abc_ObjId(pObj)) ) + 
                       Vec_IntEntry( vSizes, Vec_IntEntry(vNode2, Abc_ObjId(pObj)) ) );
    }
    printf( "\n" );

    // print how many times they appear
    vNumbers = Vec_IntStart( 32 );
    Vec_IntForEachEntry( vCounts, Entry, i )
    {
/*
if ( Entry == 2 )
{
    pObj = Abc_NtkObj( pNtk, i );
    Abc_MffcCollectNodes( &pObj, 1, vNodes );
    Abc_MffcCollectLeaves( vNodes, vLeaves );
    printf( "%d(%d) ", Vec_PtrSize(vNodes), Vec_PtrSize(vLeaves) );
}
*/
        if ( Entry == 0 )
            continue;
        if ( Entry <= 10 )
            Vec_IntAddToEntry( vNumbers, Entry, 1 );
        else if ( Entry <= 100 )
            Vec_IntAddToEntry( vNumbers, 10 + Entry/10, 1 );
        else if ( Entry < 1000 )
            Vec_IntAddToEntry( vNumbers, 20 + Entry/100, 1 );
        else
            Vec_IntAddToEntry( vNumbers, 30, 1 );
    }
    for ( i = 1; i <= 10; i++ )
        if ( Vec_IntEntry(vNumbers,i) )
            printf( "       n =  %4d   %6d\n", i, Vec_IntEntry(vNumbers,i) );
    for ( i = 11; i <= 20; i++ )
        if ( Vec_IntEntry(vNumbers,i) )
            printf( "%4d < n <= %4d   %6d\n", 10*(i-10),  10*(i-9),  Vec_IntEntry(vNumbers,i) );
    for ( i = 21; i < 30; i++ )
        if ( Vec_IntEntry(vNumbers,i) )
            printf( "%4d < n <= %4d   %6d\n", 100*(i-20), 100*(i-19), Vec_IntEntry(vNumbers,i) );
    if ( Vec_IntEntry(vNumbers,31) )
    printf( "       n >  1000   %6d\n", Vec_IntEntry(vNumbers,30) );
    printf( "Total MFFCs = %d. ", Vec_PtrSize(vRoots) );
    Abc_PrintTime( 1, "Time", clock() - clk );
    Vec_IntFree( vNumbers );
    Vec_PtrFree( vNodes );
    Vec_PtrFree( vLeaves );

    // delete fanins/fanouts
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vFanins,  Abc_ObjId(pObj)) );
        Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vFanouts, Abc_ObjId(pObj)) );
    }

    Vec_IntFree( vCounts );
    Vec_PtrFree( vFanouts );
    Vec_PtrFree( vFanins );
    Vec_PtrFree( vRoots );
}


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

  Synopsis    [Collects the leaves and the roots of the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffCollectLeafRoot( Abc_Ntk_t * pNtk, Vec_Ptr_t * vNodes, Vec_Ptr_t * vLeaves, Vec_Ptr_t * vRoots )
{
    Abc_Obj_t * pObj, * pNext;
    int i, k;
    // mark
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
        pObj->fMarkA = 1;
    // collect leaves
    Vec_PtrClear( vLeaves );
    Abc_NtkIncrementTravId( pNtk );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
        Abc_ObjForEachFanin( pObj, pNext, k )
        {
            if ( pNext->fMarkA || Abc_NodeIsTravIdCurrent(pNext) )
                continue;
            Abc_NodeSetTravIdCurrent(pNext);
            Vec_PtrPush( vLeaves, pNext );
        }
    // collect roots
    Vec_PtrClear( vRoots );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
        Abc_ObjForEachFanout( pObj, pNext, k )
            if ( !pNext->fMarkA )
            {
                Vec_PtrPush( vRoots, pObj );
                break;
            }
    }
    // unmark
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
        pObj->fMarkA = 0;
}

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

  Synopsis    [Collects the leaves and the roots of the window.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffCollectLeafRootInt( Abc_Ntk_t * pNtk, Vec_Int_t * vNodes, Vec_Int_t * vLeaves, Vec_Int_t * vRoots )
{
    Abc_Obj_t * pObj, * pNext;
    int i, k;
    // mark
760
    Abc_NtkForEachObjVec( vNodes, pNtk, pObj, i )
761 762 763 764
        pObj->fMarkA = 1;
    // collect leaves
    Vec_IntClear( vLeaves );
    Abc_NtkIncrementTravId( pNtk );
765
    Abc_NtkForEachObjVec( vNodes, pNtk, pObj, i )
766 767 768 769 770 771 772 773 774 775 776
        Abc_ObjForEachFanin( pObj, pNext, k )
        {
            if ( pNext->fMarkA || Abc_NodeIsTravIdCurrent(pNext) )
                continue;
            Abc_NodeSetTravIdCurrent(pNext);
            Vec_IntPush( vLeaves, Abc_ObjId(pNext) );
        }
    // collect roots
    if ( vRoots )
    {
        Vec_IntClear( vRoots );
777
        Abc_NtkForEachObjVec( vNodes, pNtk, pObj, i )
778 779 780 781 782 783 784 785 786 787
        {
            Abc_ObjForEachFanout( pObj, pNext, k )
                if ( !pNext->fMarkA )
                {
                    Vec_IntPush( vRoots, Abc_ObjId(pObj) );
                    break;
                }
        }
    }
    // unmark
788
    Abc_NtkForEachObjVec( vNodes, pNtk, pObj, i )
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 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 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 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
        pObj->fMarkA = 0;
}


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

  Synopsis    [Create the network of supernodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcTestIdeaOne( Abc_Ntk_t * pNtk, Abc_Obj_t * pObj )
{
    Vec_Ptr_t * vNodes, * vLeaves, * vRoots, * vVolume;
    Vec_Ptr_t * vLeaves2, * vRoots2, * vVolume2;
    Abc_Obj_t * pNode, * pNodeBest = pObj;
    double Cost, CostBest = 0.0;
    int i, k;
    vNodes   = Vec_PtrAlloc( 100 );
    vLeaves  = Vec_PtrAlloc( 100 );
    vRoots   = Vec_PtrAlloc( 100 );
    vVolume  = Vec_PtrAlloc( 100 );
    vLeaves2 = Vec_PtrAlloc( 100 );
    vRoots2  = Vec_PtrAlloc( 100 );
    vVolume2 = Vec_PtrAlloc( 100 );
printf( "\n" );
    for ( i = 1; i <= 16; i++ )
    {
        Vec_PtrPush( vNodes, pNodeBest );
        Abc_NktMffCollectLeafRoot( pNtk, vNodes, vLeaves, vRoots );
        Abc_MffcCollectNodes( (Abc_Obj_t **)Vec_PtrArray(vRoots), Vec_PtrSize(vRoots), vVolume );

        printf( "%2d : Node =%6d (%2d%3d)  Cost =%6.2f   ", i, Abc_ObjId(pNodeBest), 
            Abc_ObjFaninNum(pNodeBest), Abc_ObjFanoutNum(pNodeBest), CostBest );
        printf( "Leaf =%2d  Root =%2d  Vol =%2d\n", Vec_PtrSize(vLeaves), Vec_PtrSize(vRoots), Vec_PtrSize(vVolume) );

        // try including different nodes
        pNodeBest = NULL;
        Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pNode, k )
        {
            if ( !Abc_ObjIsNode(pNode) )
                continue;
            Vec_PtrPush( vNodes, pNode );
            Abc_NktMffCollectLeafRoot( pNtk, vNodes, vLeaves2, vRoots2 );
            Abc_MffcCollectNodes( (Abc_Obj_t **)Vec_PtrArray(vRoots2), Vec_PtrSize(vRoots2), vVolume2 );
            Cost = 1.0 * Vec_PtrSize(vVolume2) / (Vec_PtrSize(vLeaves2) + 3 * Vec_PtrSize(vRoots2));
            if ( pNodeBest == NULL || CostBest < Cost )
            {
                pNodeBest = pNode;
                CostBest  = Cost;
            }
            Vec_PtrPop( vNodes );
        }
        Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pNode, k )
        {
            if ( Vec_PtrFind(vNodes, pNode) >= 0 )
                continue;
            if ( !Abc_ObjIsNode(pNode) )
                continue;
            Vec_PtrPush( vNodes, pNode );
            Abc_NktMffCollectLeafRoot( pNtk, vNodes, vLeaves2, vRoots2 );
            Abc_MffcCollectNodes( (Abc_Obj_t **)Vec_PtrArray(vRoots2), Vec_PtrSize(vRoots2), vVolume2 );
            Cost = 1.0 * Vec_PtrSize(vVolume2) / (Vec_PtrSize(vLeaves2) + 3 * Vec_PtrSize(vRoots2));
            if ( pNodeBest == NULL || CostBest < Cost )
            {
                pNodeBest = pNode;
                CostBest  = Cost;
            }
            Vec_PtrPop( vNodes );
        }
        if ( pNodeBest == NULL )
            break;
    }

    Vec_PtrFree( vNodes );
    Vec_PtrFree( vLeaves );
    Vec_PtrFree( vRoots );
    Vec_PtrFree( vVolume );
    Vec_PtrFree( vLeaves2 );
    Vec_PtrFree( vRoots2 );
    Vec_PtrFree( vVolume2 );
}

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

  Synopsis    [Create the network of supernodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcTestIdea( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj; 
    int i;
    Abc_NtkForEachNode( pNtk, pObj, i )
        if ( Abc_ObjIsNode(pObj) && Abc_ObjId(pObj) % 100 == 0 )
            Abc_NktMffcTestIdeaOne( pNtk, pObj );
}





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

  Synopsis    [Creates MFFCs and their fanins/fanouts/volumes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NktMffcDerive( Abc_Ntk_t * pNtk, Vec_Ptr_t ** pvFanins, Vec_Ptr_t ** pvFanouts, Vec_Ptr_t ** pvVolumes )
{
    Vec_Ptr_t * vRoots, * vFanins, * vFanouts, * vVolumes, * vNodes, * vLeaves;
    Abc_Obj_t * pObj, * pFanin; 
    int i, k, clk = clock();
    // create roots
    vRoots   = Abc_NktMffcMarkRoots( pNtk, 0 );
    // create fanins/fanouts/volumes
    vFanins  = Vec_PtrStart( Abc_NtkObjNumMax(pNtk) );
    vFanouts = Vec_PtrStart( Abc_NtkObjNumMax(pNtk) );
    vVolumes = Vec_PtrStart( Abc_NtkObjNumMax(pNtk) );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Vec_PtrWriteEntry( vFanins,  Abc_ObjId(pObj), Vec_IntAlloc(8) );
        Vec_PtrWriteEntry( vFanouts, Abc_ObjId(pObj), Vec_IntAlloc(8) );
        Vec_PtrWriteEntry( vVolumes, Abc_ObjId(pObj), Vec_IntAlloc(8) );
    }
    // add fanins/fanouts
    vNodes   = Vec_PtrAlloc( 100 );
    vLeaves  = Vec_PtrAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        if ( Abc_ObjIsCi(pObj) )
            continue;
        Abc_MffcCollectNodes( &pObj, 1, vNodes );
        Abc_MffcCollectLeaves( vNodes, vLeaves );
        Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pFanin, k )
        {
            Vec_IntPush( (Vec_Int_t *)Vec_PtrEntry(vFanins,  Abc_ObjId(pObj)),   Abc_ObjId(pFanin) );
            Vec_IntPush( (Vec_Int_t *)Vec_PtrEntry(vFanouts, Abc_ObjId(pFanin)), Abc_ObjId(pObj) );
        }
        Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pFanin, k )
            Vec_IntPush( (Vec_Int_t *)Vec_PtrEntry(vVolumes, Abc_ObjId(pObj)),   Abc_ObjId(pFanin) );
    }

    Vec_PtrFree( vNodes );
    Vec_PtrFree( vLeaves );
    // sort
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Vec_IntSort( (Vec_Int_t *)Vec_PtrEntry(vFanins,  Abc_ObjId(pObj)), 0 );
        Vec_IntSort( (Vec_Int_t *)Vec_PtrEntry(vFanouts, Abc_ObjId(pObj)), 0 );
    }
    // return
    *pvFanins  = vFanins;
    *pvFanouts = vFanouts;
    *pvVolumes = vVolumes;
    return vRoots;
}

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

  Synopsis    [Frees MFFCs and their fanins/fanouts.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcFree( Vec_Ptr_t * vRoots, Vec_Ptr_t * vFanins, Vec_Ptr_t * vFanouts, Vec_Ptr_t * vVolumes )
{
    Abc_Obj_t * pObj; 
    int i;
    Vec_PtrForEachEntry( Abc_Obj_t *, vRoots, pObj, i )
    {
        Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vFanins,  Abc_ObjId(pObj)) );
        Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vFanouts, Abc_ObjId(pObj)) );
        Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vVolumes, Abc_ObjId(pObj)) );
    }
    Vec_PtrFree( vVolumes );
    Vec_PtrFree( vFanouts );
    Vec_PtrFree( vFanins );
    Vec_PtrFree( vRoots );
}


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

  Synopsis    [Returns the cost of two supports.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1000
double Abc_NktMffcCostTwo( Vec_Int_t * vSupp1, Vec_Int_t * vSupp2, int Volume, int Limit )
1001 1002
{
    int nCommon = Vec_IntTwoCountCommon( vSupp1, vSupp2 );
1003
//printf( "s1=%2d s2=%2d c=%2d v=%2d   ", Vec_IntSize(vSupp1), Vec_IntSize(vSupp2), nCommon, Volume );
1004
    if ( Vec_IntSize(vSupp1) + Vec_IntSize(vSupp2) - nCommon > Limit )
1005 1006
        return (double)-ABC_INFINITY;
    return 0.6 * nCommon - 1.2 * Vec_IntSize(vSupp2) + 0.8 * Volume;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
}

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

  Synopsis    [Returns support of the group.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Abc_NktMffcSupport( Vec_Ptr_t * vThis, Vec_Ptr_t * vFanins )
{
    Vec_Int_t * vIns, * vIns2, * vTemp;
    Abc_Obj_t * pObj;
    int i;
    vIns = Vec_IntAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vThis, pObj, i )
    {
        vIns2 = (Vec_Int_t *)Vec_PtrEntry( vFanins, Abc_ObjId(pObj) );
        vIns  = Vec_IntTwoMerge( vTemp = vIns, vIns2 );
        Vec_IntFree( vTemp );
    }
    return vIns;
}

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

  Synopsis    [Returns the best merger for the cluster of given node (pPivot).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1046
Abc_Obj_t * Abc_NktMffcFindBest( Abc_Ntk_t * pNtk, Vec_Int_t * vMarks, Vec_Int_t * vIns, Vec_Ptr_t * vFanins, Vec_Ptr_t * vFanouts, Vec_Ptr_t * vVolumes, int Limit )
1047 1048 1049
{
    Vec_Int_t * vIns2, * vOuts, * vOuts2, * vTemp;
    Abc_Obj_t * pPivot2, * pObj, * pObjBest = NULL;
1050 1051
    double Cost, CostBest = (double)-ABC_INFINITY;
    int i, Volume;
1052 1053
    // collect the fanouts of the fanins
    vOuts = Vec_IntAlloc( 100 );
1054
    Abc_NtkForEachObjVec( vIns, pNtk, pObj, i )
1055 1056 1057 1058 1059 1060 1061 1062
    {
        vOuts2 = (Vec_Int_t *)Vec_PtrEntry( vFanouts, Abc_ObjId(pObj) );
        if ( Vec_IntSize(vOuts2) > 16 )
            continue;
        vOuts  = Vec_IntTwoMerge( vTemp = vOuts, vOuts2 );
        Vec_IntFree( vTemp );
    }
    // check the pairs
1063
    Abc_NtkForEachObjVec( vOuts, pNtk, pPivot2, i )
1064 1065 1066
    {       
        if ( Vec_IntEntry(vMarks, Abc_ObjId(pPivot2)) == 0 )
            continue;
1067 1068 1069 1070 1071
        vIns2  = (Vec_Int_t *)Vec_PtrEntry( vFanins, Abc_ObjId(pPivot2) );
        Volume = Vec_IntSize((Vec_Int_t *)Vec_PtrEntry(vVolumes, Abc_ObjId(pPivot2)));
        Cost   = Abc_NktMffcCostTwo( vIns, vIns2, Volume, Limit );
//printf( "%5d  %2d\n", Abc_ObjId(pPivot2), Cost  );
        if ( Cost == (double)-ABC_INFINITY )
1072 1073 1074 1075 1076 1077 1078
            continue;
        if ( pObjBest == NULL || CostBest < Cost )
        {
            pObjBest = pPivot2;
            CostBest = Cost;
        }
    }    
1079
//printf( "Choosing %d\n", pObjBest->Id );
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    Vec_IntFree( vOuts );
    return pObjBest;
}

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

  Synopsis    [Processes one cluster.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Abc_NktMffcSaveOne( Vec_Ptr_t * vThis, Vec_Ptr_t * vVolumes )
{
    Vec_Int_t * vVolume, * vResult;
    Abc_Obj_t * pObj;
    int i, k, Entry;
    vResult = Vec_IntAlloc( 100 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vThis, pObj, i )
    {
        vVolume = (Vec_Int_t *)Vec_PtrEntry( vVolumes, Abc_ObjId(pObj) );
        Vec_IntForEachEntry( vVolume, Entry, k )
            Vec_IntPush( vResult, Entry );
    }
    return vResult;
}

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

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
  Synopsis    [Procedure used for sorting the nodes in decreasing order of levels.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NodeCompareVolumeDecrease( Abc_Obj_t ** pp1, Abc_Obj_t ** pp2 )
{
    int Diff = Abc_ObjRegular(*pp1)->iTemp - Abc_ObjRegular(*pp2)->iTemp;
    if ( Diff > 0 )
        return -1;
    if ( Diff < 0 ) 
        return 1;
    return 0; 
}

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

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  Synopsis    [Create the network of supernodes.]

  Description [Returns array of interger arrays of IDs of nodes
  included in a disjoint structural decomposition of the network.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Ptr_t * Abc_NktMffcServer( Abc_Ntk_t * pNtk, int nInMax, int nOutMax )
{
    Vec_Ptr_t * vResult, * vThis;
    Vec_Ptr_t * vPivots, * vFanins, * vFanouts, * vVolumes;
    Vec_Int_t * vLeaves, * vMarks;
    Abc_Obj_t * pObj, * pObj2;
    int i, k;
    assert( nOutMax >= 1 && nOutMax <= 32 );
    vResult = Vec_PtrAlloc( 100 );
    // create fanins/fanouts
    vPivots = Abc_NktMffcDerive( pNtk, &vFanins, &vFanouts, &vVolumes );
1154 1155 1156 1157
    // sort by their MFFC size
    Vec_PtrForEachEntry( Abc_Obj_t *, vPivots, pObj, i )
        pObj->iTemp = Vec_IntSize((Vec_Int_t *)Vec_PtrEntry(vVolumes, Abc_ObjId(pObj)));
    Vec_PtrSort( vPivots, (int (*)(void))Abc_NodeCompareVolumeDecrease ); 
1158 1159 1160
    // create marks
    vMarks = Vec_IntStart( Abc_NtkObjNumMax(pNtk) );
    Vec_PtrForEachEntry( Abc_Obj_t *, vPivots, pObj, i )
1161
        if ( Abc_ObjIsNode(pObj) && Vec_IntSize((Vec_Int_t *)Vec_PtrEntry(vVolumes, Abc_ObjId(pObj))) > 1 )
1162 1163 1164
            Vec_IntWriteEntry( vMarks, Abc_ObjId(pObj), 1 );
    // consider nodes in the order of the marks
    vThis = Vec_PtrAlloc( 10 );
1165
//    while ( 1 )
1166 1167
    Vec_PtrForEachEntry( Abc_Obj_t *, vPivots, pObj, i )
    {
1168
//        pObj = Abc_NtkObj( pNtk, 589 );
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        if ( Vec_IntEntry(vMarks, Abc_ObjId(pObj)) == 0 )
            continue;
        // start the set
        Vec_PtrClear( vThis );        
        Vec_PtrPush( vThis, pObj );
        Vec_IntWriteEntry( vMarks, Abc_ObjId(pObj), 0 );
        // quit if exceeded the limit
        vLeaves = (Vec_Int_t *)Vec_PtrEntry( vFanins, Abc_ObjId(pObj) );
        if ( Vec_IntSize(vLeaves) > nInMax )
        {
            Vec_PtrPush( vResult, Abc_NktMffcSaveOne(vThis, vVolumes) );
            continue;
        }
        // try adding one node at a time
        for ( k = 1; k < nOutMax; k++ )
        {
            // quit if exceeded the limit
            vLeaves = Abc_NktMffcSupport( vThis, vFanins );
            assert( Vec_IntSize(vLeaves) <= nInMax );
1188
            pObj2 = Abc_NktMffcFindBest( pNtk, vMarks, vLeaves, vFanins, vFanouts, vVolumes, nInMax );
1189 1190 1191 1192 1193 1194 1195 1196
            Vec_IntFree( vLeaves );
            // quit if there is no extension
            if ( pObj2 == NULL )
                break;
            Vec_PtrPush( vThis, pObj2 );
            Vec_IntWriteEntry( vMarks, Abc_ObjId(pObj2), 0 );
        }
        Vec_PtrPush( vResult, Abc_NktMffcSaveOne(vThis, vVolumes) );
1197
//        break;
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    }
    Vec_PtrFree( vThis );
    Vec_IntFree( vMarks );
    // delele fanins/outputs
    Abc_NktMffcFree( vPivots, vFanins, vFanouts, vVolumes );
    return vResult;
}

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

  Synopsis    [Testbench.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NktMffcServerTest( Abc_Ntk_t * pNtk )
{
1219
    char pFileName[1000];
1220 1221
    Vec_Ptr_t * vGlobs;
    Vec_Int_t * vGlob, * vLeaves, * vRoots;
1222 1223
    double Cost, CostAll = 0.0;
    int i, k, Entry, nNodes = 0, clk = clock();
1224 1225 1226 1227 1228 1229 1230
    vGlobs  = Abc_NktMffcServer( pNtk, 18, 3 );
    vLeaves = Vec_IntAlloc( 100 );
    vRoots  = Vec_IntAlloc( 100 );
    Vec_PtrForEachEntry( Vec_Int_t *, vGlobs, vGlob, i )
    {
        nNodes += Vec_IntSize(vGlob);
        Abc_NktMffCollectLeafRootInt( pNtk, vGlob, vLeaves, vRoots );
1231 1232
        if ( Vec_IntSize(vGlob) <= Vec_IntSize(vRoots) )
            continue;
1233
        Cost = 1.0 * Vec_IntSize(vGlob)/(Vec_IntSize(vLeaves) + Vec_IntSize(vRoots));
1234
        CostAll += Cost;
1235
        if ( Cost < 0.5 )
1236
            continue;
1237 1238

        printf( "%6d : Root =%3d. Leaf =%3d. Node =%4d. ", 
1239
            i, Vec_IntSize(vRoots), Vec_IntSize(vLeaves), Vec_IntSize(vGlob) );
1240 1241 1242 1243 1244
        printf( "Cost =%6.2f     ", Cost );
        Vec_IntForEachEntry( vRoots, Entry, k )
            printf( "%d ", Entry );
        printf( "\n" );

1245
        sprintf( pFileName, "%sc%04di%02dn%02d.blif", Abc_NtkName(pNtk), i, Vec_IntSize(vLeaves), Vec_IntSize(vGlob) );
1246
        Abc_NktMffcPrintInt( pFileName, pNtk, vRoots, vGlob, vLeaves );
1247 1248 1249 1250 1251 1252 1253
    }
    Vec_IntFree( vLeaves );
    Vec_IntFree( vRoots );
    Vec_PtrForEachEntry( Vec_Int_t *, vGlobs, vGlob, i )
        Vec_IntFree( vGlob );
    Vec_PtrFree( vGlobs );
    printf( "Total = %6d.  Nodes = %6d.  ", Abc_NtkNodeNum(pNtk), nNodes );
1254
    printf( "Cost = %6.2f  ", CostAll );
1255 1256 1257
    Abc_PrintTime( 1, "Time", clock() - clk );
}

1258 1259 1260 1261 1262 1263 1264
ABC_NAMESPACE_IMPL_END

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