abcPart.c 37.8 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [abcPart.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Output partitioning package.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21 22 23
#include "base/abc/abc.h"
#include "base/main/main.h"
#include "base/cmd/cmd.h"
24 25 26

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
27 28 29 30 31

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

Alan Mishchenko committed
32 33 34
typedef struct Supp_Man_t_     Supp_Man_t;
struct Supp_Man_t_
{
35
    int              nChunkSize;    // the size of one chunk of memory (~1 MB)
Alan Mishchenko committed
36
    int              nStepSize;     // the step size in saving memory (~64 bytes)
37 38
    char *           pFreeBuf;      // the pointer to free memory
    int              nFreeSize;     // the size of remaining free memory
Alan Mishchenko committed
39
    Vec_Ptr_t *      vMemory;       // the memory allocated
40
    Vec_Ptr_t *      vFree;         // the vector of free pieces of memory
Alan Mishchenko committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
};

typedef struct Supp_One_t_     Supp_One_t;
struct Supp_One_t_
{
    int              nRefs;         // the number of references
    int              nOuts;         // the number of outputs
    int              nOutsAlloc;    // the array size
    int              pOuts[0];      // the array of outputs
};

static inline int    Supp_SizeType( int nSize, int nStepSize )     { return nSize / nStepSize + ((nSize % nStepSize) > 0); }
static inline char * Supp_OneNext( char * pPart )                  { return *((char **)pPart);                             }
static inline void   Supp_OneSetNext( char * pPart, char * pNext ) { *((char **)pPart) = pNext;                            }

Alan Mishchenko committed
56 57 58 59 60 61
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
62
  Synopsis    [Start the memory manager.]
Alan Mishchenko committed
63

Alan Mishchenko committed
64
  Description []
Alan Mishchenko committed
65 66 67 68 69 70
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
71
Supp_Man_t * Supp_ManStart( int nChunkSize, int nStepSize )
Alan Mishchenko committed
72
{
Alan Mishchenko committed
73
    Supp_Man_t * p;
Alan Mishchenko committed
74
    p = ABC_ALLOC( Supp_Man_t, 1 );
Alan Mishchenko committed
75 76 77 78 79 80
    memset( p, 0, sizeof(Supp_Man_t) );
    p->nChunkSize = nChunkSize;
    p->nStepSize  = nStepSize;
    p->vMemory    = Vec_PtrAlloc( 1000 );
    p->vFree      = Vec_PtrAlloc( 1000 );
    return p;
Alan Mishchenko committed
81 82 83 84
}

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

Alan Mishchenko committed
85
  Synopsis    [Stops the memory manager.]
Alan Mishchenko committed
86

Alan Mishchenko committed
87
  Description []
Alan Mishchenko committed
88 89 90 91 92 93
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
94
void Supp_ManStop( Supp_Man_t * p )
Alan Mishchenko committed
95
{
Alan Mishchenko committed
96 97
    void * pMemory;
    int i;
98
    Vec_PtrForEachEntry( void *, p->vMemory, pMemory, i )
Alan Mishchenko committed
99
        ABC_FREE( pMemory );
Alan Mishchenko committed
100 101
    Vec_PtrFree( p->vMemory );
    Vec_PtrFree( p->vFree );
Alan Mishchenko committed
102
    ABC_FREE( p );
Alan Mishchenko committed
103 104 105 106
}

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

Alan Mishchenko committed
107
  Synopsis    [Fetches the memory entry of the given size.]
Alan Mishchenko committed
108 109 110 111 112 113 114 115

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
116
char * Supp_ManFetch( Supp_Man_t * p, int nSize )
Alan Mishchenko committed
117
{
Alan Mishchenko committed
118 119 120 121 122
    int Type, nSizeReal;
    char * pMemory;
    assert( nSize > 0 );
    Type = Supp_SizeType( nSize, p->nStepSize );
    Vec_PtrFillExtra( p->vFree, Type + 1, NULL );
123
    if ( (pMemory = (char *)Vec_PtrEntry( p->vFree, Type )) )
Alan Mishchenko committed
124 125 126 127 128 129 130
    {
        Vec_PtrWriteEntry( p->vFree, Type, Supp_OneNext(pMemory) );
        return pMemory;
    }
    nSizeReal = p->nStepSize * Type;
    if ( p->nFreeSize < nSizeReal )
    {
Alan Mishchenko committed
131
        p->pFreeBuf = ABC_ALLOC( char, p->nChunkSize );
Alan Mishchenko committed
132 133 134 135 136 137 138 139
        p->nFreeSize = p->nChunkSize;
        Vec_PtrPush( p->vMemory, p->pFreeBuf );
    }
    assert( p->nFreeSize >= nSizeReal );
    pMemory = p->pFreeBuf;
    p->pFreeBuf  += nSizeReal;
    p->nFreeSize -= nSizeReal;
    return pMemory;
Alan Mishchenko committed
140 141 142 143
}

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

Alan Mishchenko committed
144
  Synopsis    [Recycles the memory entry of the given size.]
Alan Mishchenko committed
145 146 147 148 149 150 151 152

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
153
void Supp_ManRecycle( Supp_Man_t * p, char * pMemory, int nSize )
Alan Mishchenko committed
154
{
Alan Mishchenko committed
155 156 157
    int Type;
    Type = Supp_SizeType( nSize, p->nStepSize );
    Vec_PtrFillExtra( p->vFree, Type + 1, NULL );
158
    Supp_OneSetNext( pMemory, (char *)Vec_PtrEntry(p->vFree, Type) );
Alan Mishchenko committed
159
    Vec_PtrWriteEntry( p->vFree, Type, pMemory );
Alan Mishchenko committed
160 161 162 163
}

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

Alan Mishchenko committed
164
  Synopsis    [Fetches the memory entry of the given size.]
Alan Mishchenko committed
165 166 167 168 169 170 171 172

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
173
static inline Supp_One_t * Supp_ManFetchEntry( Supp_Man_t * p, int nWords, int nRefs )
Alan Mishchenko committed
174
{
Alan Mishchenko committed
175 176 177 178 179 180
    Supp_One_t * pPart;
    pPart = (Supp_One_t *)Supp_ManFetch( p, sizeof(Supp_One_t) + sizeof(int) * nWords );
    pPart->nRefs = nRefs;
    pPart->nOuts = 0;
    pPart->nOutsAlloc = nWords;
    return pPart;
Alan Mishchenko committed
181 182 183 184
}

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

Alan Mishchenko committed
185
  Synopsis    [Recycles the memory entry of the given size.]
Alan Mishchenko committed
186 187 188 189 190 191 192 193

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
194
static inline void Supp_ManRecycleEntry( Supp_Man_t * p, Supp_One_t * pEntry )
Alan Mishchenko committed
195
{
Alan Mishchenko committed
196 197 198
    assert( pEntry->nOuts <= pEntry->nOutsAlloc );
    assert( pEntry->nOuts >= pEntry->nOutsAlloc/2 );
    Supp_ManRecycle( p, (char *)pEntry, sizeof(Supp_One_t) + sizeof(int) * pEntry->nOutsAlloc );
Alan Mishchenko committed
199 200 201 202
}

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

Alan Mishchenko committed
203
  Synopsis    [Merges two entries.]
Alan Mishchenko committed
204 205 206 207 208 209 210 211

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
212
Supp_One_t * Supp_ManMergeEntry( Supp_Man_t * pMan, Supp_One_t * p1, Supp_One_t * p2, int nRefs )
Alan Mishchenko committed
213
{
Alan Mishchenko committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    Supp_One_t * p = Supp_ManFetchEntry( pMan, p1->nOuts + p2->nOuts, nRefs );
    int * pBeg1 = p1->pOuts;
    int * pBeg2 = p2->pOuts;
    int * pBeg  = p->pOuts;
    int * pEnd1 = p1->pOuts + p1->nOuts;
    int * pEnd2 = p2->pOuts + p2->nOuts;
    while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
    {
        if ( *pBeg1 == *pBeg2 )
            *pBeg++ = *pBeg1++, pBeg2++;
        else if ( *pBeg1 < *pBeg2 )
            *pBeg++ = *pBeg1++;
        else 
            *pBeg++ = *pBeg2++;
    }
    while ( pBeg1 < pEnd1 )
        *pBeg++ = *pBeg1++;
    while ( pBeg2 < pEnd2 )
        *pBeg++ = *pBeg2++;
    p->nOuts = pBeg - p->pOuts;
    assert( p->nOuts <= p->nOutsAlloc );
    assert( p->nOuts >= p1->nOuts );
    assert( p->nOuts >= p2->nOuts );
    return p;
Alan Mishchenko committed
238 239 240 241
}

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

Alan Mishchenko committed
242
  Synopsis    [Tranfers the entry.]
Alan Mishchenko committed
243 244 245 246 247 248 249 250

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
251
Vec_Int_t * Supp_ManTransferEntry( Supp_One_t * p )
Alan Mishchenko committed
252
{
Alan Mishchenko committed
253 254 255 256 257 258
    Vec_Int_t * vSupp;
    int i;
    vSupp = Vec_IntAlloc( p->nOuts );
    for ( i = 0; i < p->nOuts; i++ )
        Vec_IntPush( vSupp, p->pOuts[i] );
    return vSupp;
Alan Mishchenko committed
259 260 261 262
}

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

Alan Mishchenko committed
263
  Synopsis    [Computes supports of the POs in the multi-output AIG.]
Alan Mishchenko committed
264 265 266 267 268 269 270 271

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
272
Vec_Ptr_t * Abc_NtkDfsNatural( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
273 274
{
    Vec_Ptr_t * vNodes;
Alan Mishchenko committed
275
    Abc_Obj_t * pObj, * pNext;
Alan Mishchenko committed
276
    int i, k;
Alan Mishchenko committed
277 278 279 280 281 282 283 284 285
    assert( Abc_NtkIsStrash(pNtk) );
    vNodes = Vec_PtrAlloc( Abc_NtkObjNum(pNtk) );
    Abc_NtkIncrementTravId( pNtk );
    // add the constant-1 nodes
    pObj = Abc_AigConst1(pNtk);
    Abc_NodeSetTravIdCurrent( pObj );
    Vec_PtrPush( vNodes, pObj );
    // add the CIs/nodes/COs in the topological order
    Abc_NtkForEachNode( pNtk, pObj, i )
Alan Mishchenko committed
286
    {
Alan Mishchenko committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        // check the fanins and add CIs
        Abc_ObjForEachFanin( pObj, pNext, k )
            if ( Abc_ObjIsCi(pNext) && !Abc_NodeIsTravIdCurrent(pNext) )
            {
                Abc_NodeSetTravIdCurrent( pNext );
                Vec_PtrPush( vNodes, pNext );
            }
        // add the node
        Vec_PtrPush( vNodes, pObj );
        // check the fanouts and add COs
        Abc_ObjForEachFanout( pObj, pNext, k )
            if ( Abc_ObjIsCo(pNext) && !Abc_NodeIsTravIdCurrent(pNext) )
            {
                Abc_NodeSetTravIdCurrent( pNext );
                Vec_PtrPush( vNodes, pNext );
            }
Alan Mishchenko committed
303
    }
Alan Mishchenko committed
304
    return vNodes;
Alan Mishchenko committed
305 306 307 308
}

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

Alan Mishchenko committed
309
  Synopsis    [Computes supports of the POs.]
Alan Mishchenko committed
310

Alan Mishchenko committed
311
  Description [Returns the ptr-vector of int-vectors.]
Alan Mishchenko committed
312 313 314 315 316 317
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
318
Vec_Ptr_t * Abc_NtkComputeSupportsSmart( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
319
{
Alan Mishchenko committed
320 321 322 323 324
    Vec_Ptr_t * vSupports;
    Vec_Ptr_t * vNodes;
    Vec_Int_t * vSupp;
    Supp_Man_t * p;
    Supp_One_t * pPart0, * pPart1;
Alan Mishchenko committed
325
    Abc_Obj_t * pObj;
Alan Mishchenko committed
326 327 328
    int i;
    // set the number of PIs/POs
    Abc_NtkForEachCi( pNtk, pObj, i )
Alan Mishchenko committed
329
        pObj->pNext = (Abc_Obj_t *)(ABC_PTRINT_T)i;
Alan Mishchenko committed
330
    Abc_NtkForEachCo( pNtk, pObj, i )
Alan Mishchenko committed
331
        pObj->pNext = (Abc_Obj_t *)(ABC_PTRINT_T)i;
Alan Mishchenko committed
332 333 334 335 336 337 338
    // start the support computation manager
    p = Supp_ManStart( 1 << 20, 1 << 6 );
    // consider objects in the topological order
    vSupports = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
    Abc_NtkCleanCopy(pNtk);
    // order the nodes so that the PIs and POs follow naturally
    vNodes = Abc_NtkDfsNatural( pNtk );
339
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
340
    {
Alan Mishchenko committed
341
        if ( Abc_ObjIsNode(pObj) )
Alan Mishchenko committed
342
        {
Alan Mishchenko committed
343 344 345 346 347 348 349 350 351 352
            pPart0 = (Supp_One_t *)Abc_ObjFanin0(pObj)->pCopy;
            pPart1 = (Supp_One_t *)Abc_ObjFanin1(pObj)->pCopy;
            pObj->pCopy = (Abc_Obj_t *)Supp_ManMergeEntry( p, pPart0, pPart1, Abc_ObjFanoutNum(pObj) );
            assert( pPart0->nRefs > 0 );
            if ( --pPart0->nRefs == 0 )
                Supp_ManRecycleEntry( p, pPart0 );
            assert( pPart1->nRefs > 0 );
            if ( --pPart1->nRefs == 0 )
                Supp_ManRecycleEntry( p, pPart1 );
            continue;
Alan Mishchenko committed
353
        }
Alan Mishchenko committed
354 355 356 357 358 359 360
        if ( Abc_ObjIsCo(pObj) )
        {
            pPart0 = (Supp_One_t *)Abc_ObjFanin0(pObj)->pCopy;
            // only save the CO if it is non-trivial
            if ( Abc_ObjIsNode(Abc_ObjFanin0(pObj)) )
            {
                vSupp = Supp_ManTransferEntry(pPart0);
Alan Mishchenko committed
361
                Vec_IntPush( vSupp, (int)(ABC_PTRINT_T)pObj->pNext );
Alan Mishchenko committed
362 363 364 365 366
                Vec_PtrPush( vSupports, vSupp );
            }
            assert( pPart0->nRefs > 0 );
            if ( --pPart0->nRefs == 0 )
                Supp_ManRecycleEntry( p, pPart0 );
Alan Mishchenko committed
367
            continue;
Alan Mishchenko committed
368 369
        }
        if ( Abc_ObjIsCi(pObj) )
Alan Mishchenko committed
370
        {
Alan Mishchenko committed
371 372 373
            if ( Abc_ObjFanoutNum(pObj) )
            {
                pPart0 = (Supp_One_t *)Supp_ManFetchEntry( p, 1, Abc_ObjFanoutNum(pObj) );
Alan Mishchenko committed
374
                pPart0->pOuts[ pPart0->nOuts++ ] = (int)(ABC_PTRINT_T)pObj->pNext;
Alan Mishchenko committed
375 376 377
                pObj->pCopy = (Abc_Obj_t *)pPart0;
            }
            continue;
Alan Mishchenko committed
378
        }
Alan Mishchenko committed
379 380 381 382 383 384 385
        if ( pObj == Abc_AigConst1(pNtk) )
        {
            if ( Abc_ObjFanoutNum(pObj) )
                pObj->pCopy = (Abc_Obj_t *)Supp_ManFetchEntry( p, 0, Abc_ObjFanoutNum(pObj) );
            continue;
        }
        assert( 0 );
Alan Mishchenko committed
386
    }
Alan Mishchenko committed
387
    Vec_PtrFree( vNodes );
388
//printf( "Memory usage = %d MB.\n", Vec_PtrSize(p->vMemory) * p->nChunkSize / (1<<20) );
Alan Mishchenko committed
389 390 391 392 393 394 395 396 397
    Supp_ManStop( p );
    // sort supports by size
    Vec_VecSort( (Vec_Vec_t *)vSupports, 1 );
    // clear the number of PIs/POs
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->pNext = NULL;
    Abc_NtkForEachCo( pNtk, pObj, i )
        pObj->pNext = NULL;
/*
398
    Vec_PtrForEachEntry( Vec_Int_t *, vSupports, vSupp, i )
Alan Mishchenko committed
399 400 401 402
        printf( "%d ", Vec_IntSize(vSupp) );
    printf( "\n" );
*/
    return vSupports;
Alan Mishchenko committed
403 404 405 406 407
}


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

Alan Mishchenko committed
408
  Synopsis    [Computes supports of the POs using naive method.]
Alan Mishchenko committed
409

Alan Mishchenko committed
410
  Description [Returns the ptr-vector of int-vectors.]
Alan Mishchenko committed
411 412 413 414 415 416
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
417
Vec_Ptr_t * Abc_NtkComputeSupportsNaive( Abc_Ntk_t * pNtk )
Alan Mishchenko committed
418 419 420 421 422
{
    Vec_Ptr_t * vSupp, * vSupports;
    Vec_Int_t * vSuppI;
    Abc_Obj_t * pObj, * pTemp;
    int i, k;
Alan Mishchenko committed
423 424
    // set the PI numbers
    Abc_NtkForEachCi( pNtk, pObj, i )
425
        pObj->pNext = (Abc_Obj_t *)(ABC_PTRINT_T)i;
Alan Mishchenko committed
426
    // save the CI numbers
Alan Mishchenko committed
427 428 429
    vSupports = Vec_PtrAlloc( Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
Alan Mishchenko committed
430 431
        if ( !Abc_ObjIsNode(Abc_ObjFanin0(pObj)) )
            continue;
Alan Mishchenko committed
432 433
        vSupp = Abc_NtkNodeSupport( pNtk, &pObj, 1 );
        vSuppI = (Vec_Int_t *)vSupp;
434
        Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pTemp, k )
Alan Mishchenko committed
435
            Vec_IntWriteEntry( vSuppI, k, (int)(ABC_PTRINT_T)pTemp->pNext );
Alan Mishchenko committed
436 437 438 439 440 441
        Vec_IntSort( vSuppI, 0 );
        // append the number of this output
        Vec_IntPush( vSuppI, i );
        // save the support in the vector
        Vec_PtrPush( vSupports, vSuppI );
    }
Alan Mishchenko committed
442 443 444
    // clean the CI numbers
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->pNext = NULL;
Alan Mishchenko committed
445 446
    // sort supports by size
    Vec_VecSort( (Vec_Vec_t *)vSupports, 1 );
Alan Mishchenko committed
447
/*
448
    Vec_PtrForEachEntry( Vec_Int_t *, vSupports, vSuppI, i )
Alan Mishchenko committed
449 450 451
        printf( "%d ", Vec_IntSize(vSuppI) );
    printf( "\n" );
*/
Alan Mishchenko committed
452 453 454 455 456
    return vSupports;
}

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

Alan Mishchenko committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470
  Synopsis    [Start bitwise support representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Abc_NtkSuppCharStart( Vec_Int_t * vOne, int nPis )
{
    unsigned * pBuffer;
    int i, Entry;
    int nWords = Abc_BitWordNum(nPis);
Alan Mishchenko committed
471
    pBuffer = ABC_ALLOC( unsigned, nWords );
Alan Mishchenko committed
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 512 513 514 515 516 517 518 519 520 521 522
    memset( pBuffer, 0, sizeof(unsigned) * nWords );
    Vec_IntForEachEntry( vOne, Entry, i )
    {
        assert( Entry < nPis );
        Abc_InfoSetBit( pBuffer, Entry );
    }
    return pBuffer;
}

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

  Synopsis    [Add to bitwise support representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkSuppCharAdd( unsigned * pBuffer, Vec_Int_t * vOne, int nPis )
{
    int i, Entry;
    Vec_IntForEachEntry( vOne, Entry, i )
    {
        assert( Entry < nPis );
        Abc_InfoSetBit( pBuffer, Entry );
    }
}

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

  Synopsis    [Find the common variables using bitwise support representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkSuppCharCommon( unsigned * pBuffer, Vec_Int_t * vOne )
{
    int i, Entry, nCommon = 0;
    Vec_IntForEachEntry( vOne, Entry, i )
        nCommon += Abc_InfoHasBit(pBuffer, Entry);
    return nCommon;
}

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

Alan Mishchenko committed
523 524 525 526 527 528 529 530 531
  Synopsis    [Find the best partition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
532
int Abc_NtkPartitionSmartFindPart( Vec_Ptr_t * vPartSuppsAll, Vec_Ptr_t * vPartsAll, Vec_Ptr_t * vPartSuppsChar, int nSuppSizeLimit, Vec_Int_t * vOne )
Alan Mishchenko committed
533
{
Alan Mishchenko committed
534
/*
Alan Mishchenko committed
535
    Vec_Int_t * vPartSupp, * vPart;
Alan Mishchenko committed
536 537 538 539
    double Attract, Repulse, Cost, CostBest;
    int i, nCommon, iBest;
    iBest = -1;
    CostBest = 0.0;
540
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsAll, vPartSupp, i )
Alan Mishchenko committed
541
    {
Alan Mishchenko committed
542 543 544
        vPart = Vec_PtrEntry( vPartsAll, i );
        if ( nPartSizeLimit > 0 && Vec_IntSize(vPart) >= nPartSizeLimit )
            continue;
Alan Mishchenko committed
545
        nCommon = Vec_IntTwoCountCommon( vPartSupp, vOne );
Alan Mishchenko committed
546 547
        if ( nCommon == 0 )
            continue;
Alan Mishchenko committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
        if ( nCommon == Vec_IntSize(vOne) )
            return i;
        Attract = 1.0 * nCommon / Vec_IntSize(vOne);
        if ( Vec_IntSize(vPartSupp) < 100 )
            Repulse = 1.0;
        else
            Repulse = log10( Vec_IntSize(vPartSupp) / 10.0 );
        Cost = pow( Attract, pow(Repulse, 5.0) );
        if ( CostBest < Cost )
        {
            CostBest = Cost;
            iBest = i;
        }
    }
    if ( CostBest < 0.6 )
        return -1;
    return iBest;
Alan Mishchenko committed
565 566 567 568 569 570 571 572
*/

    Vec_Int_t * vPartSupp;//, * vPart;
    int Attract, Repulse, Value, ValueBest;
    int i, nCommon, iBest;
//    int nCommon2;
    iBest = -1;
    ValueBest = 0;
573
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsAll, vPartSupp, i )
Alan Mishchenko committed
574 575 576 577 578 579 580
    {
        // skip partitions with too many outputs
//        vPart = Vec_PtrEntry( vPartsAll, i );
//        if ( nSuppSizeLimit > 0 && Vec_IntSize(vPart) >= nSuppSizeLimit )
//            continue;
        // find the number of common variables between this output and the partitions
//        nCommon2 = Vec_IntTwoCountCommon( vPartSupp, vOne );
581
        nCommon = Abc_NtkSuppCharCommon( (unsigned *)Vec_PtrEntry(vPartSuppsChar, i), vOne );
Alan Mishchenko committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
//        assert( nCommon2 == nCommon );
        // if no common variables, continue searching
        if ( nCommon == 0 )
            continue;
        // if all variables are common, the best partition if found
        if ( nCommon == Vec_IntSize(vOne) )
            return i;
        // skip partitions whose size exceeds the limit
        if ( nSuppSizeLimit > 0 && Vec_IntSize(vPartSupp) >= 2 * nSuppSizeLimit )
            continue;
        // figure out might be the good partition for this one
        Attract = 1000 * nCommon / Vec_IntSize(vOne);
        if ( Vec_IntSize(vPartSupp) < 100 )
            Repulse = 1;
        else
597
            Repulse = 1+Abc_Base2Log(Vec_IntSize(vPartSupp)-100);
Alan Mishchenko committed
598 599 600 601 602 603 604 605 606 607
        Value = Attract/Repulse;
        if ( ValueBest < Value )
        {
            ValueBest = Value;
            iBest = i;
        }
    }
    if ( ValueBest < 75 )
        return -1;
    return iBest;
Alan Mishchenko committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
}

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

  Synopsis    [Perform the smart partitioning.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPartitionPrint( Abc_Ntk_t * pNtk, Vec_Ptr_t * vPartsAll, Vec_Ptr_t * vPartSuppsAll )
{
    Vec_Int_t * vOne;
    int i, nOutputs, Counter;

    Counter = 0;
627
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsAll, vOne, i )
Alan Mishchenko committed
628
    {
629
        nOutputs = Vec_IntSize((Vec_Int_t *)Vec_PtrEntry(vPartsAll, i));
Alan Mishchenko committed
630 631 632 633 634
        printf( "%d=(%d,%d) ", i, Vec_IntSize(vOne), nOutputs );
        Counter += nOutputs;
        if ( i == Vec_PtrSize(vPartsAll) - 1 )
            break;
    }
Alan Mishchenko committed
635
//    assert( Counter == Abc_NtkCoNum(pNtk) );
Alan Mishchenko committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649
    printf( "\nTotal = %d. Outputs = %d.\n", Counter, Abc_NtkCoNum(pNtk) );
}

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

  Synopsis    [Perform the smart partitioning.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
650
void Abc_NtkPartitionCompact( Vec_Ptr_t * vPartsAll, Vec_Ptr_t * vPartSuppsAll, int nSuppSizeLimit )
Alan Mishchenko committed
651 652 653 654
{
    Vec_Int_t * vOne, * vPart, * vPartSupp, * vTemp;
    int i, iPart;

Alan Mishchenko committed
655 656
    if ( nSuppSizeLimit == 0 )
        nSuppSizeLimit = 200;
Alan Mishchenko committed
657

Alan Mishchenko committed
658 659 660
    // pack smaller partitions into larger blocks
    iPart = 0;
    vPart = vPartSupp = NULL;
661
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsAll, vOne, i )
Alan Mishchenko committed
662
    {
Alan Mishchenko committed
663
        if ( Vec_IntSize(vOne) < nSuppSizeLimit )
Alan Mishchenko committed
664 665 666 667 668
        {
            if ( vPartSupp == NULL )
            {
                assert( vPart == NULL );
                vPartSupp = Vec_IntDup(vOne);
669
                vPart = (Vec_Int_t *)Vec_PtrEntry(vPartsAll, i);
Alan Mishchenko committed
670 671 672 673 674
            }
            else
            {
                vPartSupp = Vec_IntTwoMerge( vTemp = vPartSupp, vOne );
                Vec_IntFree( vTemp );
675
                vPart = Vec_IntTwoMerge( vTemp = vPart, (Vec_Int_t *)Vec_PtrEntry(vPartsAll, i) );
Alan Mishchenko committed
676
                Vec_IntFree( vTemp );
677
                Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vPartsAll, i) );
Alan Mishchenko committed
678
            }
Alan Mishchenko committed
679
            if ( Vec_IntSize(vPartSupp) < nSuppSizeLimit )
Alan Mishchenko committed
680 681 682
                continue;
        }
        else
683
            vPart = (Vec_Int_t *)Vec_PtrEntry(vPartsAll, i);
Alan Mishchenko committed
684 685 686 687 688
        // add the partition 
        Vec_PtrWriteEntry( vPartsAll, iPart, vPart );  
        vPart = NULL;
        if ( vPartSupp ) 
        {
689
            Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vPartSuppsAll, iPart) );
Alan Mishchenko committed
690 691 692 693 694 695 696 697 698 699 700 701
            Vec_PtrWriteEntry( vPartSuppsAll, iPart, vPartSupp );  
            vPartSupp = NULL;
        }
        iPart++;
    }
    // add the last one
    if ( vPart )
    {
        Vec_PtrWriteEntry( vPartsAll, iPart, vPart );  
        vPart = NULL;

        assert( vPartSupp != NULL );
702
        Vec_IntFree( (Vec_Int_t *)Vec_PtrEntry(vPartSuppsAll, iPart) );
Alan Mishchenko committed
703 704 705 706 707 708 709 710 711 712 713 714
        Vec_PtrWriteEntry( vPartSuppsAll, iPart, vPartSupp );  
        vPartSupp = NULL;
        iPart++;
    }
    Vec_PtrShrink( vPartsAll, iPart );
    Vec_PtrShrink( vPartsAll, iPart );
}

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

  Synopsis    [Perform the smart partitioning.]

Alan Mishchenko committed
715
  Description [Returns the ptr-vector of int-vectors.]
Alan Mishchenko committed
716 717 718 719 720 721
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
722
Vec_Ptr_t * Abc_NtkPartitionSmart( Abc_Ntk_t * pNtk, int nSuppSizeLimit, int fVerbose )
Alan Mishchenko committed
723
{
Alan Mishchenko committed
724 725 726
    ProgressBar * pProgress;
    Vec_Ptr_t * vPartSuppsChar;
    Vec_Ptr_t * vSupps, * vPartsAll, * vPartsAll2, * vPartSuppsAll;
Alan Mishchenko committed
727
    Vec_Int_t * vOne, * vPart, * vPartSupp, * vTemp;
728
    int i, iPart, iOut, timeFind = 0;
729
    abctime clk, clk2;
Alan Mishchenko committed
730 731

    // compute the supports for all outputs
732
clk = Abc_Clock();
Alan Mishchenko committed
733 734
//    vSupps = Abc_NtkComputeSupportsNaive( pNtk );
    vSupps = Abc_NtkComputeSupportsSmart( pNtk );
Alan Mishchenko committed
735 736
if ( fVerbose )
{
737
ABC_PRT( "Supps", Abc_Clock() - clk );
Alan Mishchenko committed
738
}
Alan Mishchenko committed
739 740
    // start char-based support representation
    vPartSuppsChar = Vec_PtrAlloc( 1000 );
Alan Mishchenko committed
741 742

    // create partitions
743
clk = Abc_Clock();
Alan Mishchenko committed
744 745
    vPartsAll = Vec_PtrAlloc( 256 );
    vPartSuppsAll = Vec_PtrAlloc( 256 );
Alan Mishchenko committed
746
    pProgress = Extra_ProgressBarStart( stdout, Vec_PtrSize(vSupps) );
747
    Vec_PtrForEachEntry( Vec_Int_t *, vSupps, vOne, i )
Alan Mishchenko committed
748
    {
Alan Mishchenko committed
749 750 751 752
        Extra_ProgressBarUpdate( pProgress, i, NULL );
//        if ( i % 1000 == 0 )
//            printf( "CIs = %6d. COs = %6d. Processed = %6d (out of %6d). Parts = %6d.\r", 
//                Abc_NtkCiNum(pNtk), Abc_NtkCoNum(pNtk), i, Vec_PtrSize(vSupps), Vec_PtrSize(vPartsAll) ); 
Alan Mishchenko committed
753 754 755
        // get the output number
        iOut = Vec_IntPop(vOne);
        // find closely matching part
756
clk2 = Abc_Clock();
Alan Mishchenko committed
757
        iPart = Abc_NtkPartitionSmartFindPart( vPartSuppsAll, vPartsAll, vPartSuppsChar, nSuppSizeLimit, vOne );
758
timeFind += Abc_Clock() - clk2;
Alan Mishchenko committed
759 760 761 762 763 764 765 766 767 768
        if ( iPart == -1 )
        {
            // create new partition
            vPart = Vec_IntAlloc( 32 );
            Vec_IntPush( vPart, iOut );
            // create new partition support
            vPartSupp = Vec_IntDup( vOne );
            // add this partition and its support
            Vec_PtrPush( vPartsAll, vPart );
            Vec_PtrPush( vPartSuppsAll, vPartSupp );
Alan Mishchenko committed
769 770

            Vec_PtrPush( vPartSuppsChar, Abc_NtkSuppCharStart(vOne, Abc_NtkCiNum(pNtk)) );
Alan Mishchenko committed
771 772 773 774
        }
        else
        {
            // add output to this partition
775
            vPart = (Vec_Int_t *)Vec_PtrEntry( vPartsAll, iPart );
Alan Mishchenko committed
776 777
            Vec_IntPush( vPart, iOut );
            // merge supports
778
            vPartSupp = (Vec_Int_t *)Vec_PtrEntry( vPartSuppsAll, iPart );
Alan Mishchenko committed
779 780 781 782
            vPartSupp = Vec_IntTwoMerge( vTemp = vPartSupp, vOne );
            Vec_IntFree( vTemp );
            // reinsert new support
            Vec_PtrWriteEntry( vPartSuppsAll, iPart, vPartSupp );
Alan Mishchenko committed
783

784
            Abc_NtkSuppCharAdd( (unsigned *)Vec_PtrEntry(vPartSuppsChar, iPart), vOne, Abc_NtkCiNum(pNtk) );
Alan Mishchenko committed
785 786
        }
    }
Alan Mishchenko committed
787 788 789
    Extra_ProgressBarStop( pProgress );

    // stop char-based support representation
790
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsChar, vTemp, i )
Alan Mishchenko committed
791
        ABC_FREE( vTemp );
Alan Mishchenko committed
792 793 794
    Vec_PtrFree( vPartSuppsChar );

//printf( "\n" );
Alan Mishchenko committed
795 796
if ( fVerbose )
{
797
ABC_PRT( "Parts", Abc_Clock() - clk );
Alan Mishchenko committed
798
//ABC_PRT( "Find ", timeFind );
Alan Mishchenko committed
799 800
}

801
clk = Abc_Clock();
Alan Mishchenko committed
802
    // remember number of supports
803
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsAll, vOne, i )
Alan Mishchenko committed
804 805 806 807 808
        Vec_IntPush( vOne, i );
    // sort the supports in the decreasing order
    Vec_VecSort( (Vec_Vec_t *)vPartSuppsAll, 1 );
    // reproduce partitions
    vPartsAll2 = Vec_PtrAlloc( 256 );
809
    Vec_PtrForEachEntry( Vec_Int_t *, vPartSuppsAll, vOne, i )
Alan Mishchenko committed
810 811 812 813 814 815
        Vec_PtrPush( vPartsAll2, Vec_PtrEntry(vPartsAll, Vec_IntPop(vOne)) );
    Vec_PtrFree( vPartsAll );
    vPartsAll = vPartsAll2;

    // compact small partitions
//    Abc_NtkPartitionPrint( pNtk, vPartsAll, vPartSuppsAll );
Alan Mishchenko committed
816 817
    Abc_NtkPartitionCompact( vPartsAll, vPartSuppsAll, nSuppSizeLimit );

Alan Mishchenko committed
818 819
if ( fVerbose )
{
820
ABC_PRT( "Comps", Abc_Clock() - clk );
Alan Mishchenko committed
821
}
Alan Mishchenko committed
822 823 824
    if ( fVerbose )
    printf( "Created %d partitions.\n", Vec_PtrSize(vPartsAll) );
//    Abc_NtkPartitionPrint( pNtk, vPartsAll, vPartSuppsAll );
Alan Mishchenko committed
825 826 827 828

    // cleanup
    Vec_VecFree( (Vec_Vec_t *)vSupps );
    Vec_VecFree( (Vec_Vec_t *)vPartSuppsAll );
Alan Mishchenko committed
829
/*
Alan Mishchenko committed
830
    // converts from intergers to nodes
831
    Vec_PtrForEachEntry( Vec_Int_t *, vPartsAll, vPart, iPart )
Alan Mishchenko committed
832 833 834 835 836 837 838
    {
        vPartPtr = Vec_PtrAlloc( Vec_IntSize(vPart) );
        Vec_IntForEachEntry( vPart, iOut, i )
            Vec_PtrPush( vPartPtr, Abc_NtkCo(pNtk, iOut) );
        Vec_IntFree( vPart );
        Vec_PtrWriteEntry( vPartsAll, iPart, vPartPtr );
    }
Alan Mishchenko committed
839 840
*/
    return vPartsAll;
Alan Mishchenko committed
841 842 843 844 845 846
}

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

  Synopsis    [Perform the naive partitioning.]

Alan Mishchenko committed
847
  Description [Returns the ptr-vector of int-vectors.]
Alan Mishchenko committed
848 849 850 851 852 853
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
854
Vec_Ptr_t * Abc_NtkPartitionNaive( Abc_Ntk_t * pNtk, int nPartSize )
Alan Mishchenko committed
855
{
Alan Mishchenko committed
856
    Vec_Ptr_t * vParts;
Alan Mishchenko committed
857 858 859
    Abc_Obj_t * pObj;
    int nParts, i;
    nParts = (Abc_NtkCoNum(pNtk) / nPartSize) + ((Abc_NtkCoNum(pNtk) % nPartSize) > 0);
Alan Mishchenko committed
860
    vParts = (Vec_Ptr_t *)Vec_VecStart( nParts );
Alan Mishchenko committed
861
    Abc_NtkForEachCo( pNtk, pObj, i )
862
        Vec_IntPush( (Vec_Int_t *)Vec_PtrEntry(vParts, i / nPartSize), i );
Alan Mishchenko committed
863 864 865 866 867
    return vParts;
}

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

Alan Mishchenko committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
  Synopsis    [Converts from intergers to pointers for the given network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkConvertCos( Abc_Ntk_t * pNtk, Vec_Int_t * vOuts, Vec_Ptr_t * vOutsPtr )
{
    int Out, i;
    Vec_PtrClear( vOutsPtr );
    Vec_IntForEachEntry( vOuts, Out, i )
        Vec_PtrPush( vOutsPtr, Abc_NtkCo(pNtk, Out) );
}

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

Alan Mishchenko committed
887 888 889 890 891 892 893 894 895 896 897 898
  Synopsis    [Returns representative of the given node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Obj_t * Abc_NtkPartStitchFindRepr_rec( Vec_Ptr_t * vEquiv, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pRepr;
899
    pRepr = (Abc_Obj_t *)Vec_PtrEntry( vEquiv, pObj->Id );
Alan Mishchenko committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    if ( pRepr == NULL || pRepr == pObj )
        return pObj;
    return Abc_NtkPartStitchFindRepr_rec( vEquiv, pRepr );
}

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

  Synopsis    [Returns the representative of the fanin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Abc_Obj_t * Abc_NtkPartStitchCopy0( Vec_Ptr_t * vEquiv, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFan = Abc_ObjFanin0( pObj );
    Abc_Obj_t * pRepr = Abc_NtkPartStitchFindRepr_rec( vEquiv, pFan );
920
    return Abc_ObjNotCond( pRepr->pCopy, pRepr->fPhase ^ pFan->fPhase ^ (int)Abc_ObjFaninC1(pObj) );
Alan Mishchenko committed
921 922 923 924 925
}
static inline Abc_Obj_t * Abc_NtkPartStitchCopy1( Vec_Ptr_t * vEquiv, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFan = Abc_ObjFanin1( pObj );
    Abc_Obj_t * pRepr = Abc_NtkPartStitchFindRepr_rec( vEquiv, pFan );
926
    return Abc_ObjNotCond( pRepr->pCopy, pRepr->fPhase ^ pFan->fPhase ^ (int)Abc_ObjFaninC1(pObj) );
Alan Mishchenko committed
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
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Hop_Obj_t * Hop_ObjChild0Next( Abc_Obj_t * pObj ) { return Hop_NotCond( (Hop_Obj_t *)Abc_ObjFanin0(pObj)->pNext, Abc_ObjFaninC0(pObj) ); }
static inline Hop_Obj_t * Hop_ObjChild1Next( Abc_Obj_t * pObj ) { return Hop_NotCond( (Hop_Obj_t *)Abc_ObjFanin1(pObj)->pNext, Abc_ObjFaninC1(pObj) ); }


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

  Synopsis    [Stitches together several networks with choice nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Man_t * Abc_NtkPartStartHop( Abc_Ntk_t * pNtk )
{
    Hop_Man_t * pMan;
    Abc_Obj_t * pObj;
    int i;
    // start the HOP package
    pMan = Hop_ManStart();
    pMan->vObjs = Vec_PtrAlloc( Abc_NtkObjNumMax(pNtk) + 1  );
    Vec_PtrPush( pMan->vObjs, Hop_ManConst1(pMan) );
    // map constant node and PIs
Alan Mishchenko committed
965
    Abc_AigConst1(pNtk)->pNext = (Abc_Obj_t *)Hop_ManConst1(pMan);
Alan Mishchenko committed
966 967 968
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->pNext = (Abc_Obj_t *)Hop_ObjCreatePi(pMan);
    // map the internal nodes
Alan Mishchenko committed
969
    Abc_AigForEachAnd( pNtk, pObj, i )
Alan Mishchenko committed
970 971 972 973 974
    {
        pObj->pNext = (Abc_Obj_t *)Hop_And( pMan, Hop_ObjChild0Next(pObj), Hop_ObjChild1Next(pObj) );
        assert( !Abc_ObjIsComplement(pObj->pNext) );
    }
    // set the choice nodes
Alan Mishchenko committed
975
    Abc_AigForEachAnd( pNtk, pObj, i )
Alan Mishchenko committed
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
    {
        if ( pObj->pCopy )
            ((Hop_Obj_t *)pObj->pNext)->pData = pObj->pCopy->pNext;
    }
    // transfer the POs
    Abc_NtkForEachCo( pNtk, pObj, i )
        Hop_ObjCreatePo( pMan, Hop_ObjChild0Next(pObj) );
    // check the new manager
    if ( !Hop_ManCheck(pMan) )
        printf( "Abc_NtkPartStartHop: HOP manager check has failed.\n" );
    return pMan;
}

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

  Synopsis    [Stitches together several networks with choice nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkPartStitchChoices( Abc_Ntk_t * pNtk, Vec_Ptr_t * vParts )
{
    extern Abc_Ntk_t * Abc_NtkHopRemoveLoops( Abc_Ntk_t * pNtk, Hop_Man_t * pMan );
    Hop_Man_t * pMan;
Alan Mishchenko committed
1004
    Vec_Ptr_t * vNodes;
Alan Mishchenko committed
1005
    Abc_Ntk_t * pNtkNew, * pNtkTemp;
Alan Mishchenko committed
1006
    Abc_Obj_t * pObj, * pFanin;
Alan Mishchenko committed
1007
    int i, k, iNodeId;
Alan Mishchenko committed
1008 1009 1010

    // start a new network similar to the original one
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
1011
    pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_STRASH, ABC_FUNC_AIG );
Alan Mishchenko committed
1012 1013

    // annotate parts to point to the new network
1014
    Vec_PtrForEachEntry( Abc_Ntk_t *, vParts, pNtkTemp, i )
Alan Mishchenko committed
1015 1016 1017 1018 1019
    {
        assert( Abc_NtkIsStrash(pNtkTemp) );
        Abc_NtkCleanCopy( pNtkTemp );

        // map the CI nodes
Alan Mishchenko committed
1020
        Abc_AigConst1(pNtkTemp)->pCopy = Abc_AigConst1(pNtkNew);
Alan Mishchenko committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
        Abc_NtkForEachCi( pNtkTemp, pObj, k )
        {
            iNodeId = Nm_ManFindIdByNameTwoTypes( pNtkNew->pManName, Abc_ObjName(pObj), ABC_OBJ_PI, ABC_OBJ_BO );
            if ( iNodeId == -1 )
            {
                printf( "Cannot find CI node %s in the original network.\n", Abc_ObjName(pObj) );
                return NULL;
            }
            pObj->pCopy = Abc_NtkObj( pNtkNew, iNodeId );
        }
Alan Mishchenko committed
1031

Alan Mishchenko committed
1032 1033
        // add the internal nodes while saving representatives
        vNodes = Abc_AigDfs( pNtkTemp, 1, 0 );
1034
        Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, k )
Alan Mishchenko committed
1035
        {
1036
            pObj->pCopy = Abc_AigAnd( (Abc_Aig_t *)pNtkNew->pManFunc, Abc_ObjChild0Copy(pObj), Abc_ObjChild1Copy(pObj) );
Alan Mishchenko committed
1037
            assert( !Abc_ObjIsComplement(pObj->pCopy) );
Alan Mishchenko committed
1038
            if ( Abc_AigNodeIsChoice(pObj) )
1039
                for ( pFanin = (Abc_Obj_t *)pObj->pData; pFanin; pFanin = (Abc_Obj_t *)pFanin->pData )
Alan Mishchenko committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
                    pFanin->pCopy->pCopy = pObj->pCopy;
        }
        Vec_PtrFree( vNodes );

        // map the CO nodes
        Abc_NtkForEachCo( pNtkTemp, pObj, k )
        {
            iNodeId = Nm_ManFindIdByNameTwoTypes( pNtkNew->pManName, Abc_ObjName(pObj), ABC_OBJ_PO, ABC_OBJ_BI );
            if ( iNodeId == -1 )
            {
                printf( "Cannot find CO node %s in the original network.\n", Abc_ObjName(pObj) );
                return NULL;
            }
            pObj->pCopy = Abc_NtkObj( pNtkNew, iNodeId );
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjChild0Copy(pObj) );
        }
    }

Alan Mishchenko committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    // connect the remaining POs
/*
    Abc_AigConst1(pNtk)->pCopy = Abc_AigConst1(pNtkNew);
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->pCopy = Abc_NtkCi( pNtkNew, i );
    Abc_NtkForEachCo( pNtk, pObj, i )
        pObj->pCopy = Abc_NtkCo( pNtkNew, i );
*/
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        if ( Abc_ObjFaninNum(pObj->pCopy) == 0 )
            Abc_ObjAddFanin( pObj->pCopy, Abc_ObjChild0Copy(pObj) );
    }
Alan Mishchenko committed
1071

Alan Mishchenko committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    // transform into the HOP manager
    pMan = Abc_NtkPartStartHop( pNtkNew );
    pNtkNew = Abc_NtkHopRemoveLoops( pNtkTemp = pNtkNew, pMan );
    Abc_NtkDelete( pNtkTemp );

    // check correctness of the new network
    if ( !Abc_NtkCheck( pNtkNew ) )
    {
        printf( "Abc_NtkPartStitchChoices: The network check has failed.\n" );
        Abc_NtkDelete( pNtkNew );
        return NULL;
    }
    return pNtkNew;
}

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

  Synopsis    [Stitches together several networks with choice nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1098
Abc_Ntk_t * Abc_NtkFraigPartitioned( Vec_Ptr_t * vStore, void * pParams )
Alan Mishchenko committed
1099
{
Alan Mishchenko committed
1100 1101 1102 1103
    Vec_Ptr_t * vParts, * vFraigs, * vOnePtr;
    Vec_Int_t * vOne;
    Abc_Ntk_t * pNtk, * pNtk2, * pNtkAig, * pNtkFraig;
    int i, k;
Alan Mishchenko committed
1104 1105

    // perform partitioning
1106
    pNtk = (Abc_Ntk_t *)Vec_PtrEntry( vStore, 0 );
Alan Mishchenko committed
1107 1108
    assert( Abc_NtkIsStrash(pNtk) );
//    vParts = Abc_NtkPartitionNaive( pNtk, 20 );
Alan Mishchenko committed
1109
    vParts = Abc_NtkPartitionSmart( pNtk, 300, 0 );
Alan Mishchenko committed
1110 1111 1112 1113

    Cmd_CommandExecute( Abc_FrameGetGlobalFrame(), "unset progressbar" );

    // fraig each partition
Alan Mishchenko committed
1114 1115
    vOnePtr = Vec_PtrAlloc( 1000 );
    vFraigs = Vec_PtrAlloc( Vec_PtrSize(vParts) );
1116
    Vec_PtrForEachEntry( Vec_Int_t *, vParts, vOne, i )
Alan Mishchenko committed
1117
    {
Alan Mishchenko committed
1118 1119 1120 1121
        // start the partition 
        Abc_NtkConvertCos( pNtk, vOne, vOnePtr );
        pNtkAig = Abc_NtkCreateConeArray( pNtk, vOnePtr, 0 );
        // add nodes to the partition
1122
        Vec_PtrForEachEntryStart( Abc_Ntk_t *, vStore, pNtk2, k, 1 )
Alan Mishchenko committed
1123 1124 1125 1126 1127 1128 1129 1130 1131
        {
            Abc_NtkConvertCos( pNtk2, vOne, vOnePtr );
            Abc_NtkAppendToCone( pNtkAig, pNtk2, vOnePtr );
        }
        printf( "Fraiging part %4d  (out of %4d)  PI = %5d. PO = %5d. And = %6d. Lev = %4d.\r", 
            i+1, Vec_PtrSize(vParts), Abc_NtkPiNum(pNtkAig), Abc_NtkPoNum(pNtkAig), 
            Abc_NtkNodeNum(pNtkAig), Abc_AigLevel(pNtkAig) );
        // fraig the partition
        pNtkFraig = Abc_NtkFraig( pNtkAig, pParams, 1, 0 );
Alan Mishchenko committed
1132 1133 1134
        Vec_PtrPush( vFraigs, pNtkFraig );
        Abc_NtkDelete( pNtkAig );
    }
Alan Mishchenko committed
1135 1136
    printf( "                                                                                          \r" );
    Vec_VecFree( (Vec_Vec_t *)vParts );
Alan Mishchenko committed
1137 1138 1139 1140

    Cmd_CommandExecute( Abc_FrameGetGlobalFrame(), "set progressbar" );

    // derive the final network
Alan Mishchenko committed
1141
    pNtkFraig = Abc_NtkPartStitchChoices( pNtk, vFraigs );
1142
    Vec_PtrForEachEntry( Abc_Ntk_t *, vFraigs, pNtkAig, i )
Alan Mishchenko committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        Abc_NtkDelete( pNtkAig );
    Vec_PtrFree( vFraigs );
    Vec_PtrFree( vOnePtr );
    return pNtkFraig;
}

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

  Synopsis    [Stitches together several networks with choice nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkFraigPartitionedTime( Abc_Ntk_t * pNtk, void * pParams )
{
    Vec_Ptr_t * vParts, * vFraigs, * vOnePtr;
    Vec_Int_t * vOne;
    Abc_Ntk_t * pNtkAig, * pNtkFraig;
    int i;
1166
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

    // perform partitioning
    assert( Abc_NtkIsStrash(pNtk) );
//    vParts = Abc_NtkPartitionNaive( pNtk, 20 );
    vParts = Abc_NtkPartitionSmart( pNtk, 300, 0 );

    Cmd_CommandExecute( Abc_FrameGetGlobalFrame(), "unset progressbar" );

    // fraig each partition
    vOnePtr = Vec_PtrAlloc( 1000 );
    vFraigs = Vec_PtrAlloc( Vec_PtrSize(vParts) );
1178
    Vec_PtrForEachEntry( Vec_Int_t *, vParts, vOne, i )
Alan Mishchenko committed
1179
    {
Alan Mishchenko committed
1180 1181 1182 1183
        Abc_NtkConvertCos( pNtk, vOne, vOnePtr );
        pNtkAig = Abc_NtkCreateConeArray( pNtk, vOnePtr, 0 );
        pNtkFraig = Abc_NtkFraig( pNtkAig, pParams, 0, 0 );
        Vec_PtrPush( vFraigs, pNtkFraig );
Alan Mishchenko committed
1184
        Abc_NtkDelete( pNtkAig );
Alan Mishchenko committed
1185 1186

        printf( "Finished part %5d (out of %5d)\r", i+1, Vec_PtrSize(vParts) );
Alan Mishchenko committed
1187
    }
Alan Mishchenko committed
1188
    Vec_VecFree( (Vec_Vec_t *)vParts );
Alan Mishchenko committed
1189

Alan Mishchenko committed
1190 1191 1192
    Cmd_CommandExecute( Abc_FrameGetGlobalFrame(), "set progressbar" );

    // derive the final network
1193
    Vec_PtrForEachEntry( Abc_Ntk_t *, vFraigs, pNtkAig, i )
Alan Mishchenko committed
1194 1195 1196
        Abc_NtkDelete( pNtkAig );
    Vec_PtrFree( vFraigs );
    Vec_PtrFree( vOnePtr );
1197
    ABC_PRT( "Partitioned fraiging time", Abc_Clock() - clk );
Alan Mishchenko committed
1198 1199
}

Alan Mishchenko committed
1200 1201 1202 1203 1204
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1205 1206
ABC_NAMESPACE_IMPL_END