cecIso.c 11.1 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6
/**CFile****************************************************************

  FileName    [cecIso.c]

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7
  PackageName [Combinational equivalence checking.]
Alan Mishchenko committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  Synopsis    [Detection of structural isomorphism.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "cecInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static inline unsigned * Cec_ManIsoInfo( unsigned * pStore, int nWords, int Id ) { return pStore + nWords * Id; }

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

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

  Synopsis    [Computes simulation info for one node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManIsoSimulate( Gia_Obj_t * pObj, int Id, unsigned * pStore, int nWords )
{
    unsigned * pInfo  = Cec_ManIsoInfo( pStore, nWords, Id );
    unsigned * pInfo0 = Cec_ManIsoInfo( pStore, nWords, Gia_ObjFaninId0(pObj, Id) );
    unsigned * pInfo1 = Cec_ManIsoInfo( pStore, nWords, Gia_ObjFaninId1(pObj, Id) );
    int w;
    if ( Gia_ObjFaninC0(pObj) )
    {
        if (  Gia_ObjFaninC1(pObj) )
            for ( w = 0; w < nWords; w++ )
                pInfo[w] = ~(pInfo0[w] | pInfo1[w]);
        else 
            for ( w = 0; w < nWords; w++ )
                pInfo[w] = ~pInfo0[w] & pInfo1[w];
    }
    else 
    {
        if (  Gia_ObjFaninC1(pObj) )
            for ( w = 0; w < nWords; w++ )
                pInfo[w] = pInfo0[w] & ~pInfo1[w];
        else 
            for ( w = 0; w < nWords; w++ )
                pInfo[w] = pInfo0[w] & pInfo1[w];
    }
}

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

  Synopsis    [Copies simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManIsoCopy( int IdDest, int IdSour, unsigned * pStore, int nWords )
{
    unsigned * pInfo0 = Cec_ManIsoInfo( pStore, nWords, IdDest );
    unsigned * pInfo1 = Cec_ManIsoInfo( pStore, nWords, IdSour );
    int w;
    for ( w = 0; w < nWords; w++ )
        pInfo0[w] = pInfo1[w];
}

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

  Synopsis    [Compares simulation info of two nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Gia_ManIsoEqual( int Id0, int Id1, unsigned * pStore, int nWords )
{
    unsigned * pInfo0 = Cec_ManIsoInfo( pStore, nWords, Id0 );
    unsigned * pInfo1 = Cec_ManIsoInfo( pStore, nWords, Id1 );
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pInfo0[w] != pInfo1[w] )
            return 0;
    return 1;
}

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

  Synopsis    [Generates random simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManIsoRandom( int Id, unsigned * pStore, int nWords )
{
    unsigned * pInfo0 = Cec_ManIsoInfo( pStore, nWords, Id );
    int w;
    for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
131
        pInfo0[w] = Gia_ManRandom( 0 );
Alan Mishchenko committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

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

  Synopsis    [Computes hash key of the simuation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Gia_ManIsoHashKey( int Id, unsigned * pStore, int nWords, int nTableSize )
{
    static int s_Primes[16] = { 
        1291, 1699, 1999, 2357, 2953, 3313, 3907, 4177, 
        4831, 5147, 5647, 6343, 6899, 7103, 7873, 8147 };
    unsigned * pInfo0 = Cec_ManIsoInfo( pStore, nWords, Id );
    unsigned uHash = 0;
    int i;
    for ( i = 0; i < nWords; i++ )
        uHash ^= pInfo0[i] * s_Primes[i & 0xf];
    return (int)(uHash % nTableSize);

}

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

  Synopsis    [Adds node to the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManIsoTableAdd( Gia_Man_t * p, int Id, unsigned * pStore, int nWords, int * pTable, int nTableSize )
{
    Gia_Obj_t * pTemp;
173
    int Key, Ent, Color = Gia_ObjColors( p, Id );
Alan Mishchenko committed
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 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
    assert( Color == 1 || Color == 2 );
    Key = Gia_ManIsoHashKey( Id, pStore, nWords, nTableSize );
    for ( Ent = pTable[Key],  pTemp = (Ent ?  Gia_ManObj(p, Ent) : NULL);  pTemp;
          Ent = pTemp->Value, pTemp = (Ent ?  Gia_ManObj(p, Ent) : NULL) )
    {
        if ( Gia_ObjColors( p, Ent ) != Color )
            continue;
        if ( !Gia_ManIsoEqual( Id, Ent, pStore, nWords ) )
            continue;
        // found node with the same color and signature - mark it and do not add new node
        pTemp->fMark0 = 1;
        return;
    }
    // did not find the node with the same color and signature - add new node
    pTemp = Gia_ManObj( p, Id );
    assert( pTemp->Value == 0 );
    assert( pTemp->fMark0 == 0 );
    pTemp->Value = pTable[Key];
    pTable[Key] = Id;
}

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

  Synopsis    [Extracts equivalence class candidates from one bin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Gia_ManIsoExtractClasses( Gia_Man_t * p, int Bin, unsigned * pStore, int nWords, Vec_Int_t * vNodesA, Vec_Int_t * vNodesB )
{
    Gia_Obj_t * pTemp;
    int Ent;
    Vec_IntClear( vNodesA );
    Vec_IntClear( vNodesB );
    for ( Ent = Bin,          pTemp = (Ent ?  Gia_ManObj(p, Ent) : NULL);  pTemp;
          Ent = pTemp->Value, pTemp = (Ent ?  Gia_ManObj(p, Ent) : NULL) )
    {
        if ( pTemp->fMark0 )
        {
            pTemp->fMark0 = 0;
            continue;
        }
        if ( Gia_ObjColors( p, Ent ) == 1 )
            Vec_IntPush( vNodesA, Ent );
        else
            Vec_IntPush( vNodesB, Ent );
    }
    return Vec_IntSize(vNodesA) > 0 && Vec_IntSize(vNodesB) > 0;    
}

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

  Synopsis    [Matches nodes in the extacted classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Gia_ManIsoMatchNodes( int * pIso, unsigned * pStore, int nWords, Vec_Int_t * vNodesA, Vec_Int_t * vNodesB )
{
    int k0, k1, IdA, IdB;
    Vec_IntForEachEntry( vNodesA, IdA, k0 )
    Vec_IntForEachEntry( vNodesB, IdB, k1 )
    {
        if ( Gia_ManIsoEqual( IdA, IdB, pStore, nWords ) )
        {
            assert( pIso[IdA] == 0 );
            assert( pIso[IdB] == 0 );
            assert( IdA != IdB );
            pIso[IdA] = IdB;
            pIso[IdB] = IdA;
            continue;
        }
    }
}

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

  Synopsis    [Transforms iso into equivalence classes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cec_ManTransformClasses( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i;
    assert( p->pReprs && p->pNexts && p->pIso );
    memset( p->pReprs, 0, sizeof(int) * Gia_ManObjNum(p) );
    memset( p->pNexts, 0, sizeof(int) * Gia_ManObjNum(p) );
    Gia_ManForEachObj( p, pObj, i )
    {
        p->pReprs[i].iRepr = GIA_VOID;
        if ( p->pIso[i] && p->pIso[i] < i )
        {
            p->pReprs[i].iRepr = p->pIso[i];
            p->pNexts[p->pIso[i]] = i;
        }
    }
}

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

  Synopsis    [Finds node correspondences in the miter.]

  Description [Assumes that the colors are assigned.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Cec_ManDetectIsomorphism( Gia_Man_t * p )
{
    int nWords = 2;
    Gia_Obj_t * pObj;
    Vec_Int_t * vNodesA, * vNodesB;
    unsigned * pStore, Counter;
    int i, * pIso, * pTable, nTableSize;
    // start equivalence classes
    pIso = ABC_CALLOC( int, Gia_ManObjNum(p) );
    Gia_ManForEachObj( p, pObj, i )
    {
        if ( Gia_ObjIsCo(pObj) )
        {
            assert( Gia_ObjColors(p, i) == 0 );
            continue;
        }
        assert( Gia_ObjColors(p, i) );
        if ( Gia_ObjColors(p, i) == 3 )
            pIso[i] = i;
    }
    // start simulation info
    pStore = ABC_ALLOC( unsigned, Gia_ManObjNum(p) * nWords );
    // simulate and create table
320
    nTableSize = Abc_PrimeCudd( 100 + Gia_ManObjNum(p)/2 );
Alan Mishchenko committed
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
    pTable = ABC_CALLOC( int, nTableSize );
    Gia_ManCleanValue( p );
    Gia_ManForEachObj1( p, pObj, i )
    {
        if ( Gia_ObjIsCo(pObj) )
            continue;
        if ( pIso[i] == 0 ) // simulate
            Gia_ManIsoSimulate( pObj, i, pStore, nWords );
        else if ( pIso[i] < i ) // copy
            Gia_ManIsoCopy( i, pIso[i], pStore, nWords );
        else // generate
            Gia_ManIsoRandom( i, pStore, nWords );
        if ( pIso[i] == 0 )
            Gia_ManIsoTableAdd( p, i, pStore, nWords, pTable, nTableSize );
    }
    // create equivalence classes
    vNodesA = Vec_IntAlloc( 100 );
    vNodesB = Vec_IntAlloc( 100 );
    for ( i = 0; i < nTableSize; i++ )
        if ( Gia_ManIsoExtractClasses( p, pTable[i], pStore, nWords, vNodesA, vNodesB ) )
            Gia_ManIsoMatchNodes( pIso, pStore, nWords, vNodesA, vNodesB );
    Vec_IntFree( vNodesA );
    Vec_IntFree( vNodesB );
    // collect info
    Counter = 0;
    Gia_ManForEachObj1( p, pObj, i )
    {
        Counter += (pIso[i] && pIso[i] < i);
/*
        if ( pIso[i] && pIso[i] < i )
        {
            if ( (Gia_ObjIsHead(p,pIso[i]) && Gia_ObjRepr(p,i)==pIso[i]) || 
                 (Gia_ObjIsClass(p,pIso[i]) && Gia_ObjRepr(p,i)==Gia_ObjRepr(p,pIso[i])) )
354
                 Abc_Print( 1, "1" );
Alan Mishchenko committed
355
            else
356
                Abc_Print( 1, "0" );
Alan Mishchenko committed
357 358 359
        }
*/
    }
360
    Abc_Print( 1, "Computed %d pairs of structurally equivalent nodes.\n", Counter );
Alan Mishchenko committed
361 362 363 364 365 366 367 368 369 370 371 372 373
//    p->pIso = pIso;
//    Cec_ManTransformClasses( p );

    ABC_FREE( pTable );
    ABC_FREE( pStore );
    return pIso;
}

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


374 375
ABC_NAMESPACE_IMPL_END