summaryrefslogtreecommitdiffstats
path: root/src/aig/cec2/cecSim.c
blob: 4fedbddc5c62ec7e6edb2d19f593a1549da17969 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
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
447
/**CFile****************************************************************

  FileName    [cecSim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Combinatinoal equivalence checking.]

  Synopsis    [AIG simulation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "cecInt.h"

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

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

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

  Synopsis    [Creates fast simulation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Caig_Man_t * Caig_ManCreate( Aig_Man_t * pAig )
{
    Caig_Man_t * p;
    Aig_Obj_t * pObj;
    int i;
    assert( Aig_ManHasNoGaps(pAig) );
    Aig_ManCleanData( pAig );
    p = (Caig_Man_t *)ABC_ALLOC( Caig_Man_t, 1 );
    memset( p, 0, sizeof(Caig_Man_t) );
    p->pAig = pAig;
    p->nPis = Aig_ManPiNum(pAig);
    p->nPos = Aig_ManPoNum(pAig);
    p->nNodes = Aig_ManNodeNum(pAig);
    p->nObjs  = p->nPis + p->nPos + p->nNodes + 1;
    p->pFans0 = ABC_ALLOC( int, p->nObjs );
    p->pFans1 = ABC_ALLOC( int, p->nObjs );
    p->pRefs  = ABC_ALLOC( int, p->nObjs );
    p->pSims  = ABC_CALLOC( unsigned, p->nObjs );
    // add objects
    Aig_ManForEachObj( pAig, pObj, i )
    {
        p->pRefs[i] = Aig_ObjRefs(pObj);
        if ( Aig_ObjIsNode(pObj) )
        {
            p->pFans0[i] = (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj);
            p->pFans1[i] = (Aig_ObjFaninId1(pObj) << 1) | Aig_ObjFaninC1(pObj);
        }
        else if ( Aig_ObjIsPo(pObj) )
        {
            p->pFans0[i] = (Aig_ObjFaninId0(pObj) << 1) | Aig_ObjFaninC0(pObj);
            p->pFans1[i] = -1;
        }
        else 
        {
            assert( Aig_ObjIsPi(pObj) || Aig_ObjIsConst1(pObj) );
            p->pFans0[i] = -1;
            p->pFans1[i] = -1;
        }
    }
    // temporaries
    p->vClassOld = Vec_IntAlloc( 1000 );
    p->vClassNew = Vec_IntAlloc( 1000 );
    p->vRefinedC = Vec_IntAlloc( 10000 );
    p->vSims     = Vec_PtrAlloc( 1000 );
    return p;
}

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

  Synopsis    [Creates fast simulation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Caig_ManDelete( Caig_Man_t * p )
{
    Vec_IntFree( p->vClassOld );
    Vec_IntFree( p->vClassNew );
    Vec_IntFree( p->vRefinedC );
    Vec_PtrFree( p->vSims );
    ABC_FREE( p->pFans0 );
    ABC_FREE( p->pFans1 );
    ABC_FREE( p->pRefs );
    ABC_FREE( p->pSims );
    ABC_FREE( p->pMems );
    ABC_FREE( p->pReprs );
    ABC_FREE( p->pNexts );
    ABC_FREE( p );
}

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

  Synopsis    [References simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Caig_ManSimMemRelink( Caig_Man_t * p )
{
    int * pPlace, Ent;
    pPlace = &p->MemFree;
    for ( Ent = p->nMems * (p->nWords + 1); 
          Ent + p->nWords + 1 < p->nWordsAlloc; 
          Ent += p->nWords + 1 )
    {
        *pPlace = Ent;
        pPlace = p->pMems + Ent;
    }
    *pPlace = 0;
}

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

  Synopsis    [References simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Caig_ManSimRead( Caig_Man_t * p, int i )
{
    assert( i && p->pSims[i] > 0 );
    return p->pMems + p->pSims[i];
}

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

  Synopsis    [References simulation info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Caig_ManSimRef( Caig_Man_t * p, int i )
{
    unsigned * pSim;
    assert( p->pSims[i] == 0 );
    if ( p->MemFree == 0 )
    {
        if ( p->nWordsAlloc == 0 )
        {
            assert( p->pMems == NULL );
            p->nWordsAlloc = (1<<17); // -> 1Mb
            p->nMems = 1;
        }
        p->nWordsAlloc *= 2;
        p->pMems = ABC_REALLOC( unsigned, p->pMems, p->nWordsAlloc );
        Caig_ManSimMemRelink( p );
    }
    p->pSims[i] = p->MemFree;
    pSim = p->pMems + p->MemFree;
    p->MemFree = pSim[0];
    pSim[0] = p->pRefs[i];
    p->nMems++;
    if ( p->nMemsMax < p->nMems )
        p->nMemsMax = p->nMems;
    return pSim;
}

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

  Synopsis    [Dereference simulaton info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Caig_ManSimDeref( Caig_Man_t * p, int i )
{
    unsigned * pSim;
    assert( p->pSims[i] > 0 );
    pSim = p->pMems + p->pSims[i];
    if ( --pSim[0] == 0 )
    {
        pSim[0] = p->MemFree;
        p->MemFree = p->pSims[i];
        p->pSims[i] = 0;
        p->nMems--;
    }
    return pSim;
}

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

  Synopsis    [Simulates one round.]

  Description [Returns the number of PO entry if failed; 0 otherwise.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Caig_ManSimulateRound( Caig_Man_t * p, Vec_Ptr_t * vInfoCis, Vec_Ptr_t * vInfoCos )
{
    unsigned * pRes0, * pRes1, * pRes;
    int i, w, iCiId = 0, iCoId = 0;
    int nWords = Vec_PtrReadWordsSimInfo( vInfoCis );
    assert( vInfoCos == NULL || nWords == Vec_PtrReadWordsSimInfo(vInfoCos) );
    Vec_IntClear( p->vRefinedC );
    if ( p->pRefs[0] )
    {
        pRes = Caig_ManSimRef( p, 0 );
        for ( w = 1; w <= nWords; w++ )
            pRes[w] = ~0;
    }
    for ( i = 1; i < p->nObjs; i++ )
    {
        if ( p->pFans0[i] == -1 ) // ci always has zero first fanin
        {
            if ( p->pRefs[i] == 0 )
            {
                iCiId++;
                continue;
            }
            pRes = Caig_ManSimRef( p, i );
            pRes0 = Vec_PtrEntry( vInfoCis, iCiId++ );
            for ( w = 1; w <= nWords; w++ )
                pRes[w] = pRes0[w-1];
            goto references;
        }
        if ( p->pFans1[i] == -1 ) // co always has non-zero 1st fanin and zero 2nd fanin
        {
            pRes0 = Caig_ManSimDeref( p, Cec_Lit2Var(p->pFans0[i]) );
            if ( vInfoCos )
            {
                pRes = Vec_PtrEntry( vInfoCos, iCoId++ );
                if ( Cec_LitIsCompl(p->pFans0[i]) )
                    for ( w = 1; w <= nWords; w++ )
                        pRes[w-1] = ~pRes0[w];
                else 
                    for ( w = 1; w <= nWords; w++ )
                        pRes[w-1] = pRes0[w];
            }
            continue;
        }
        assert( p->pRefs[i] );
        pRes  = Caig_ManSimRef( p, i );
        pRes0 = Caig_ManSimDeref( p, Cec_Lit2Var(p->pFans0[i]) );
        pRes1 = Caig_ManSimDeref( p, Cec_Lit2Var(p->pFans1[i]) );
        if ( Cec_LitIsCompl(p->pFans0[i]) )
        {
            if ( Cec_LitIsCompl(p->pFans1[i]) )
                for ( w = 1; w <= nWords; w++ )
                    pRes[w] = ~(pRes0[w] | pRes1[w]);
            else
                for ( w = 1; w <= nWords; w++ )
                    pRes[w] = ~pRes0[w] & pRes1[w];
        }
        else
        {
            if ( Cec_LitIsCompl(p->pFans1[i]) )
                for ( w = 1; w <= nWords; w++ )
                    pRes[w] = pRes0[w] & ~pRes1[w];
            else
                for ( w = 1; w <= nWords; w++ )
                    pRes[w] = pRes0[w] & pRes1[w];
        }
references:
        if ( p->pReprs == NULL )
            continue;
        // if this node is candidate constant, collect it
        if ( p->pReprs[i] == 0 && !Caig_ManCompareConst(pRes + 1, nWords) )
        {
            pRes[0]++;
            Vec_IntPush( p->vRefinedC, i );
        }
        // if the node belongs to a class, save it
        if ( p->pReprs[i] > 0 || p->pNexts[i] > 0 )
            pRes[0]++;
        // if this is the last node of the class, process it
        if ( p->pReprs[i] > 0 && p->pNexts[i] == 0 )
        {
            Caig_ManCollectSimsNormal( p, p->pReprs[i] );
            Caig_ManClassRefineOne( p, p->pReprs[i], p->vSims );
        }
    }
    if ( Vec_IntSize(p->vRefinedC) > 0 )
        Caig_ManProcessRefined( p, p->vRefinedC );
    assert( iCiId == p->nPis );
    assert( vInfoCos == NULL || iCoId == p->nPos );
    assert( p->nMems == 1 );
/*
    if ( p->nMems > 1 )
    {
        for ( i = 1; i < p->nObjs; i++ )
        if ( p->pSims[i] )
        {
            int x = 0;
        }
    }
*/
}

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

  Synopsis    [Returns the number of PO that failed.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ManFindPo( Vec_Ptr_t * vInfo, int nWords )
{
    unsigned * pInfo;
    int i, w;
    Vec_PtrForEachEntry( vInfo, pInfo, i )
        for ( w = 0; w < nWords; w++ )
            if ( pInfo[w] != 0 )
                return i;
    return -1;
}

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

  Synopsis    [Returns 1 if the bug is detected, 0 otherwise.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ManSimulate( Aig_Man_t * pAig, int nWords, int nIters, int TimeLimit, int fMiter, int fVerbose )
{
    Caig_Man_t * p;
    Cec_MtrStatus_t Status;
    Vec_Ptr_t * vInfoCis, * vInfoCos = NULL;
    int i, RetValue = 0, clk, clkTotal = clock();
/*
    p = Caig_ManClassesPrepare( pAig, nWords, nIters );
//    if ( fVerbose )
    printf( "Maxcut = %6d.  AIG mem = %8.3f Mb.  Sim mem = %8.3f Mb.\n", 
        p->nMemsMax, 
        1.0*(p->nObjs * 14)/(1<<20), 
        1.0*(p->nMemsMax * (nWords+1))/(1<<20) );
    Caig_ManDelete( p );
    return 0;
*/
    if ( fMiter )
    {
        Status = Cec_MiterStatus( pAig );
        if ( Status.nSat > 0 )
        {
            printf( "Miter is trivially satisfiable (output %d).\n", Status.iOut );
            return 1;
        }
        if ( Status.nUndec == 0 )
        {
            printf( "Miter is trivially unsatisfiable.\n" );
            return 0;
        }
    }
    Aig_ManRandom( 1 );
    p = Caig_ManCreate( pAig );
    p->nWords = nWords;
    if ( fMiter )
        vInfoCos = Vec_PtrAllocSimInfo( Aig_ManPiNum(pAig), nWords );
    for ( i = 0; i < nIters; i++ )
    {
        clk = clock();
        vInfoCis = Vec_PtrAllocSimInfo( Aig_ManPiNum(pAig), nWords );
        Aig_ManRandomInfo( vInfoCis, 0, nWords );
        Caig_ManSimulateRound( p, vInfoCis, vInfoCos );
        Vec_PtrFree( vInfoCis );
        if ( fVerbose )
        {
            printf( "Iter %3d out of %3d and timeout %3d sec. ", i+1, nIters, TimeLimit );
            printf("Time = %7.2f sec\r", (1.0*clock()-clkTotal)/CLOCKS_PER_SEC);
        }
        if ( fMiter )
        {
            int iOut = Cec_ManFindPo( vInfoCos, nWords );
            if ( iOut >= 0 )
            {
                if ( fVerbose )
                printf( "Miter is satisfiable after simulation (output %d).\n", iOut );
                RetValue = 1;
                break;
            }
        }
        if ( (clock() - clk)/CLOCKS_PER_SEC >= TimeLimit )
        {
            printf( "No bug detected after %d rounds with time limit %d seconds.\n", i+1, TimeLimit );
            break;
        }
    }
    if ( fVerbose )
    printf( "Maxcut = %6d.  AIG mem = %8.3f Mb.  Sim mem = %8.3f Mb.\n", 
        p->nMemsMax, 
        1.0*(p->nObjs * 14)/(1<<20), 
        1.0*(p->nMemsMax * 4 * (nWords+1))/(1<<20) );
    Caig_ManDelete( p );
    if ( vInfoCos )
        Vec_PtrFree( vInfoCos );
    return RetValue > 0;
}



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