simSymStr.c 15.3 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    [simSymStr.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Structural detection of symmetries.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21
#include "base/abc/abc.h"
Alan Mishchenko committed
22 23
#include "sim.h"

24 25 26
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define SIM_READ_SYMMS(pNode)       ((Vec_Int_t *)pNode->pCopy) 
#define SIM_SET_SYMMS(pNode,vVect)  (pNode->pCopy = (Abc_Obj_t *)(vVect)) 

static void  Sim_SymmsStructComputeOne( Abc_Ntk_t * pNtk, Abc_Obj_t * pNode, int * pMap );
static void  Sim_SymmsBalanceCollect_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes );
static void  Sim_SymmsPartitionNodes( Vec_Ptr_t * vNodes, Vec_Ptr_t * vNodesPis0, Vec_Ptr_t * vNodesPis1, Vec_Ptr_t * vNodesOther );
static void  Sim_SymmsAppendFromGroup( Abc_Ntk_t * pNtk, Vec_Ptr_t * vNodesPi, Vec_Ptr_t * vNodesOther, Vec_Int_t * vSymms, int * pMap );
static void  Sim_SymmsAppendFromNode( Abc_Ntk_t * pNtk, Vec_Int_t * vSymms0, Vec_Ptr_t * vNodesOther, Vec_Ptr_t * vNodesPi0, Vec_Ptr_t * vNodesPi1, Vec_Int_t * vSymms, int * pMap );
static int   Sim_SymmsIsCompatibleWithNodes( Abc_Ntk_t * pNtk, unsigned uSymm, Vec_Ptr_t * vNodesOther, int * pMap );
static int   Sim_SymmsIsCompatibleWithGroup( unsigned uSymm, Vec_Ptr_t * vNodesPi, int * pMap );
static void  Sim_SymmsPrint( Vec_Int_t * vSymms );
static void  Sim_SymmsTrans( Vec_Int_t * vSymms );
Alan Mishchenko committed
43
static void  Sim_SymmsTransferToMatrix( Extra_BitMat_t * pMatSymm, Vec_Int_t * vSymms, unsigned * pSupport );
Alan Mishchenko committed
44 45 46
static int * Sim_SymmsCreateMap( Abc_Ntk_t * pNtk );

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
47
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
48 49 50 51 52 53 54 55 56 57 58 59 60
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Computes symmetries for a single output function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
61
void Sim_SymmsStructCompute( Abc_Ntk_t * pNtk, Vec_Ptr_t * vMatrs, Vec_Ptr_t * vSuppFun )
Alan Mishchenko committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
{
    Vec_Ptr_t * vNodes;
    Abc_Obj_t * pTemp;
    int * pMap, i;

    assert( Abc_NtkCiNum(pNtk) + 10 < (1<<16) );

    // get the structural support
    pNtk->vSupps = Sim_ComputeStrSupp( pNtk );
    // set elementary info for the CIs
    Abc_NtkForEachCi( pNtk, pTemp, i )
        SIM_SET_SYMMS( pTemp, Vec_IntAlloc(0) );
    // create the map of CI ids into their numbers
    pMap = Sim_SymmsCreateMap( pNtk );
    // collect the nodes in the TFI cone of this output
    vNodes = Abc_NtkDfs( pNtk, 0 );
78
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pTemp, i )
Alan Mishchenko committed
79
    {
Alan Mishchenko committed
80 81
//        if ( Abc_NodeIsConst(pTemp) )
//            continue;
Alan Mishchenko committed
82 83 84 85 86
        Sim_SymmsStructComputeOne( pNtk, pTemp, pMap );
    }
    // collect the results for the COs;
    Abc_NtkForEachCo( pNtk, pTemp, i )
    {
Alan Mishchenko committed
87
//printf( "Output %d:\n", i );
Alan Mishchenko committed
88
        pTemp = Abc_ObjFanin0(pTemp);
Alan Mishchenko committed
89
        if ( Abc_ObjIsCi(pTemp) || Abc_AigNodeIsConst(pTemp) )
Alan Mishchenko committed
90
            continue;
91
        Sim_SymmsTransferToMatrix( (Extra_BitMat_t *)Vec_PtrEntry(vMatrs, i), SIM_READ_SYMMS(pTemp), (unsigned *)Vec_PtrEntry(vSuppFun, i) );
Alan Mishchenko committed
92 93 94 95 96 97
    }
    // clean the intermediate results
    Sim_UtilInfoFree( pNtk->vSupps );
    pNtk->vSupps = NULL;
    Abc_NtkForEachCi( pNtk, pTemp, i )
        Vec_IntFree( SIM_READ_SYMMS(pTemp) );
98
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pTemp, i )
Alan Mishchenko committed
99
//        if ( !Abc_NodeIsConst(pTemp) )
Alan Mishchenko committed
100
            Vec_IntFree( SIM_READ_SYMMS(pTemp) );
Alan Mishchenko committed
101
    Vec_PtrFree( vNodes );
Alan Mishchenko committed
102
    ABC_FREE( pMap );
Alan Mishchenko committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

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

  Synopsis    [Recursively computes symmetries. ]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sim_SymmsStructComputeOne( Abc_Ntk_t * pNtk, Abc_Obj_t * pNode, int * pMap )
{
    Vec_Ptr_t * vNodes, * vNodesPi0, * vNodesPi1, * vNodesOther;
    Vec_Int_t * vSymms;
    Abc_Obj_t * pTemp;
    int i;

    // allocate the temporary arrays
    vNodes      = Vec_PtrAlloc( 10 );
    vNodesPi0   = Vec_PtrAlloc( 10 );
    vNodesPi1   = Vec_PtrAlloc( 10 );
    vNodesOther = Vec_PtrAlloc( 10 );  

    // collect the fanins of the implication supergate
    Sim_SymmsBalanceCollect_rec( pNode, vNodes );

    // sort the nodes in the implication supergate
    Sim_SymmsPartitionNodes( vNodes, vNodesPi0, vNodesPi1, vNodesOther );

    // start the resulting set
    vSymms = Vec_IntAlloc( 10 );
    // generate symmetries from the groups
    Sim_SymmsAppendFromGroup( pNtk, vNodesPi0, vNodesOther, vSymms, pMap );
    Sim_SymmsAppendFromGroup( pNtk, vNodesPi1, vNodesOther, vSymms, pMap );
    // add symmetries from other inputs
    for ( i = 0; i < vNodesOther->nSize; i++ )
    {
143
        pTemp = Abc_ObjRegular((Abc_Obj_t *)vNodesOther->pArray[i]);
Alan Mishchenko committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        Sim_SymmsAppendFromNode( pNtk, SIM_READ_SYMMS(pTemp), vNodesOther, vNodesPi0, vNodesPi1, vSymms, pMap );
    }
    Vec_PtrFree( vNodes );
    Vec_PtrFree( vNodesPi0 );
    Vec_PtrFree( vNodesPi1 );
    Vec_PtrFree( vNodesOther );

    // set the symmetry at the node
    SIM_SET_SYMMS( pNode, vSymms );
}


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

  Synopsis    [Returns the array of nodes to be combined into one multi-input AND-gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sim_SymmsBalanceCollect_rec( Abc_Obj_t * pNode, Vec_Ptr_t * vNodes )
{
    // if the new node is complemented, another gate begins
    if ( Abc_ObjIsComplement(pNode) )
    {
        Vec_PtrPushUnique( vNodes, pNode );
        return;
    }
    // if pNew is the PI node, return
    if ( Abc_ObjIsCi(pNode) )
    {
        Vec_PtrPushUnique( vNodes, pNode );
        return;    
    }
    // go through the branches
    Sim_SymmsBalanceCollect_rec( Abc_ObjChild0(pNode), vNodes );
    Sim_SymmsBalanceCollect_rec( Abc_ObjChild1(pNode), vNodes );
}

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

  Synopsis    [Divides PI variables into groups.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sim_SymmsPartitionNodes( Vec_Ptr_t * vNodes, Vec_Ptr_t * vNodesPis0, 
    Vec_Ptr_t * vNodesPis1, Vec_Ptr_t * vNodesOther )
{
    Abc_Obj_t * pNode;
    int i;
202
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
Alan Mishchenko committed
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
    {
        if ( !Abc_ObjIsCi(Abc_ObjRegular(pNode)) )
            Vec_PtrPush( vNodesOther, pNode );
        else if ( Abc_ObjIsComplement(pNode) )
            Vec_PtrPush( vNodesPis0, pNode );
        else
            Vec_PtrPush( vNodesPis1, pNode );
    }
}

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

  Synopsis    [Makes the product of two partitions.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sim_SymmsAppendFromGroup( Abc_Ntk_t * pNtk, Vec_Ptr_t * vNodesPi, Vec_Ptr_t * vNodesOther, Vec_Int_t * vSymms, int * pMap )
{
    Abc_Obj_t * pNode1, * pNode2;
    unsigned uSymm;
    int i, k;

    if ( vNodesPi->nSize == 0 )
        return;

    // go through the pairs
    for ( i = 0; i < vNodesPi->nSize; i++ )
    for ( k = i+1; k < vNodesPi->nSize; k++ )
    {
        // get the two PI nodes
238 239
        pNode1 = Abc_ObjRegular((Abc_Obj_t *)vNodesPi->pArray[i]);
        pNode2 = Abc_ObjRegular((Abc_Obj_t *)vNodesPi->pArray[k]);
Alan Mishchenko committed
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
        assert( pMap[pNode1->Id] != pMap[pNode2->Id] );
        assert( pMap[pNode1->Id] >= 0 );
        assert( pMap[pNode2->Id] >= 0 );
        // generate symmetry
        if ( pMap[pNode1->Id] < pMap[pNode2->Id] )
            uSymm = ((pMap[pNode1->Id] << 16) | pMap[pNode2->Id]);
        else
            uSymm = ((pMap[pNode2->Id] << 16) | pMap[pNode1->Id]);
        // check if symmetry belongs
        if ( Sim_SymmsIsCompatibleWithNodes( pNtk, uSymm, vNodesOther, pMap ) )
            Vec_IntPushUnique( vSymms, (int)uSymm );
    }
}

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

  Synopsis    [Add the filters symmetries from the nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sim_SymmsAppendFromNode( Abc_Ntk_t * pNtk, Vec_Int_t * vSymms0, Vec_Ptr_t * vNodesOther, 
    Vec_Ptr_t * vNodesPi0, Vec_Ptr_t * vNodesPi1, Vec_Int_t * vSymms, int * pMap )
{
    unsigned uSymm;
    int i;

    if ( vSymms0->nSize == 0 )
        return;

    // go through the pairs
    for ( i = 0; i < vSymms0->nSize; i++ )
    {
        uSymm = (unsigned)vSymms0->pArray[i];
        // check if symmetry belongs
        if ( Sim_SymmsIsCompatibleWithNodes( pNtk, uSymm, vNodesOther, pMap ) &&
             Sim_SymmsIsCompatibleWithGroup( uSymm, vNodesPi0, pMap ) && 
             Sim_SymmsIsCompatibleWithGroup( uSymm, vNodesPi1, pMap ) )
            Vec_IntPushUnique( vSymms, (int)uSymm );
    }
}

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

  Synopsis    [Returns 1 if symmetry is compatible with the group of nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sim_SymmsIsCompatibleWithNodes( Abc_Ntk_t * pNtk, unsigned uSymm, Vec_Ptr_t * vNodesOther, int * pMap )
{
    Vec_Int_t * vSymmsNode;
    Abc_Obj_t * pNode;
    int i, s, Ind1, Ind2, fIsVar1, fIsVar2;

    if ( vNodesOther->nSize == 0 )
        return 1;

    // get the indices of the PI variables
    Ind1 = (uSymm & 0xffff);
    Ind2 = (uSymm >> 16);

    // go through the nodes
    // if they do not belong to a support, it is okay
    // if one belongs, the other does not belong, quit
    // if they belong, but are not part of symmetry, quit
    for ( i = 0; i < vNodesOther->nSize; i++ )
    {
316
        pNode = Abc_ObjRegular((Abc_Obj_t *)vNodesOther->pArray[i]);
Alan Mishchenko committed
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
        fIsVar1 = Sim_SuppStrHasVar( pNtk->vSupps, pNode, Ind1 );
        fIsVar2 = Sim_SuppStrHasVar( pNtk->vSupps, pNode, Ind2 );

        if ( !fIsVar1 && !fIsVar2 )
            continue;
        if ( fIsVar1 ^ fIsVar2 )
            return 0;
        // both belong
        // check if there is a symmetry
        vSymmsNode = SIM_READ_SYMMS( pNode );
        for ( s = 0; s < vSymmsNode->nSize; s++ )
            if ( uSymm == (unsigned)vSymmsNode->pArray[s] )
                break;
        if ( s == vSymmsNode->nSize )
            return 0;
    }
    return 1;
}

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

  Synopsis    [Returns 1 if symmetry is compatible with the group of PIs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sim_SymmsIsCompatibleWithGroup( unsigned uSymm, Vec_Ptr_t * vNodesPi, int * pMap )
{
    Abc_Obj_t * pNode;
    int i, Ind1, Ind2, fHasVar1, fHasVar2;

    if ( vNodesPi->nSize == 0 )
        return 1;

    // get the indices of the PI variables
    Ind1 = (uSymm & 0xffff);
    Ind2 = (uSymm >> 16);

    // go through the PI nodes
    fHasVar1 = fHasVar2 = 0;
    for ( i = 0; i < vNodesPi->nSize; i++ )
    {
363
        pNode = Abc_ObjRegular((Abc_Obj_t *)vNodesPi->pArray[i]);
Alan Mishchenko committed
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
        if ( pMap[pNode->Id] == Ind1 )
            fHasVar1 = 1;
        else if ( pMap[pNode->Id] == Ind2 )
            fHasVar2 = 1;
    }
    return fHasVar1 == fHasVar2;
}



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

  Synopsis    [Improvements due to transitivity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sim_SymmsTrans( Vec_Int_t * vSymms )
{
    unsigned uSymm, uSymma, uSymmr;
    int i, Ind1, Ind2;
    int k, Ind1a, Ind2a;
    int j;
    int nTrans = 0;

    for ( i = 0; i < vSymms->nSize; i++ )
    {
        uSymm = (unsigned)vSymms->pArray[i];
        Ind1 = (uSymm & 0xffff);
        Ind2 = (uSymm >> 16);
        // find other symmetries that have Ind1
        for ( k = i+1; k < vSymms->nSize; k++ )
        {
            uSymma = (unsigned)vSymms->pArray[k];
            if ( uSymma == uSymm )
                continue;
            Ind1a = (uSymma & 0xffff);
            Ind2a = (uSymma >> 16);
            if ( Ind1a == Ind1 )
            {
                // find the symmetry (Ind2,Ind2a)
                if ( Ind2 < Ind2a )
                    uSymmr = ((Ind2 << 16) | Ind2a);
                else
                    uSymmr = ((Ind2a << 16) | Ind2);
                for ( j = 0; j < vSymms->nSize; j++ )
                    if ( uSymmr == (unsigned)vSymms->pArray[j] )
                        break;
                if ( j == vSymms->nSize )
                    nTrans++;
            }
        }

    }
    printf( "Trans = %d.\n", nTrans );
}


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

  Synopsis    [Transfers from the vector to the matrix.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
437
void Sim_SymmsTransferToMatrix( Extra_BitMat_t * pMatSymm, Vec_Int_t * vSymms, unsigned * pSupport )
Alan Mishchenko committed
438 439 440 441 442 443 444 445 446 447 448 449 450
{
    int i, Ind1, Ind2, nInputs;
    unsigned uSymm;
    // add diagonal elements
    nInputs = Extra_BitMatrixReadSize( pMatSymm );
    for ( i = 0; i < nInputs; i++ )
        Extra_BitMatrixInsert1( pMatSymm, i, i );
    // add non-diagonal elements
    for ( i = 0; i < vSymms->nSize; i++ )
    {
        uSymm = (unsigned)vSymms->pArray[i];
        Ind1 = (uSymm & 0xffff);
        Ind2 = (uSymm >> 16);
Alan Mishchenko committed
451
//printf( "%d,%d ", Ind1, Ind2 );
Alan Mishchenko committed
452 453 454 455
        // skip variables that are not in the true support
        assert( Sim_HasBit(pSupport, Ind1) == Sim_HasBit(pSupport, Ind2) );
        if ( !Sim_HasBit(pSupport, Ind1) || !Sim_HasBit(pSupport, Ind2) )
            continue;
Alan Mishchenko committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
        Extra_BitMatrixInsert1( pMatSymm, Ind1, Ind2 );
        Extra_BitMatrixInsert2( pMatSymm, Ind1, Ind2 );
    }
}

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

  Synopsis    [Mapping of indices into numbers.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Sim_SymmsCreateMap( Abc_Ntk_t * pNtk )
{
    int * pMap;
    Abc_Obj_t * pNode;
    int i;
Alan Mishchenko committed
477
    pMap = ABC_ALLOC( int, Abc_NtkObjNumMax(pNtk) );
Alan Mishchenko committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491
    for ( i = 0; i < Abc_NtkObjNumMax(pNtk); i++ )
        pMap[i] = -1;
    Abc_NtkForEachCi( pNtk, pNode, i )
        pMap[pNode->Id] = i;
    return pMap;
}



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


492 493
ABC_NAMESPACE_IMPL_END