1 /**CFile****************************************************************
2 
3   FileName    [giaSwitch.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Scalable AIG package.]
8 
9   Synopsis    [Computing switching activity.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: giaSwitch.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "giaAig.h"
22 #include "base/main/main.h"
23 
24 ABC_NAMESPACE_IMPL_START
25 
26 
27 ////////////////////////////////////////////////////////////////////////
28 ///                        DECLARATIONS                              ///
29 ////////////////////////////////////////////////////////////////////////
30 
31 // switching estimation parameters
32 typedef struct Gia_ParSwi_t_ Gia_ParSwi_t;
33 struct Gia_ParSwi_t_
34 {
35     // user-controlled parameters
36     int            nWords;       // the number of machine words
37     int            nIters;       // the number of timeframes
38     int            nPref;        // the number of first timeframes to skip
39     int            nRandPiFactor;   // PI trans prob (-1=3/8; 0=1/2; 1=1/4; 2=1/8, etc)
40     int            fProbOne;     // collect probability of one
41     int            fProbTrans;   // collect probatility of Swiing
42     int            fVerbose;     // enables verbose output
43 };
44 
45 typedef struct Gia_ManSwi_t_ Gia_ManSwi_t;
46 struct Gia_ManSwi_t_
47 {
48     Gia_Man_t *    pAig;
49     Gia_ParSwi_t * pPars;
50     int            nWords;
51     // simulation information
52     unsigned *     pDataSim;     // simulation data
53     unsigned *     pDataSimCis;  // simulation data for CIs
54     unsigned *     pDataSimCos;  // simulation data for COs
55     int *          pData1;       // switching data
56 };
57 
Gia_SwiData(Gia_ManSwi_t * p,int i)58 static inline unsigned * Gia_SwiData( Gia_ManSwi_t * p, int i )    { return p->pDataSim + i * p->nWords;    }
Gia_SwiDataCi(Gia_ManSwi_t * p,int i)59 static inline unsigned * Gia_SwiDataCi( Gia_ManSwi_t * p, int i )  { return p->pDataSimCis + i * p->nWords; }
Gia_SwiDataCo(Gia_ManSwi_t * p,int i)60 static inline unsigned * Gia_SwiDataCo( Gia_ManSwi_t * p, int i )  { return p->pDataSimCos + i * p->nWords; }
61 
62 ////////////////////////////////////////////////////////////////////////
63 ///                     FUNCTION DEFINITIONS                         ///
64 ////////////////////////////////////////////////////////////////////////
65 
66 /**Function*************************************************************
67 
68   Synopsis    [This procedure sets default parameters.]
69 
70   Description []
71 
72   SideEffects []
73 
74   SeeAlso     []
75 
76 ***********************************************************************/
Gia_ManSetDefaultParamsSwi(Gia_ParSwi_t * p)77 void Gia_ManSetDefaultParamsSwi( Gia_ParSwi_t * p )
78 {
79     memset( p, 0, sizeof(Gia_ParSwi_t) );
80     p->nWords        =  10;  // the number of machine words of simulatation data
81     p->nIters        =  48;  // the number of all timeframes to simulate
82     p->nPref         =  16;  // the number of first timeframes to skip when computing switching
83     p->nRandPiFactor =   0;  // primary input transition probability (-1=3/8; 0=1/2; 1=1/4; 2=1/8, etc)
84     p->fProbOne      =   0;  // compute probability of signal being one (if 0, compute probability of switching)
85     p->fProbTrans    =   1;  // compute signal transition probability (if 0, compute transition probability using probability of being one)
86     p->fVerbose      =   0;  // enables verbose output
87 }
88 
89 /**Function*************************************************************
90 
91   Synopsis    [Creates fast simulation manager.]
92 
93   Description []
94 
95   SideEffects []
96 
97   SeeAlso     []
98 
99 ***********************************************************************/
Gia_ManSwiCreate(Gia_Man_t * pAig,Gia_ParSwi_t * pPars)100 Gia_ManSwi_t * Gia_ManSwiCreate( Gia_Man_t * pAig, Gia_ParSwi_t * pPars )
101 {
102     Gia_ManSwi_t * p;
103     p = ABC_ALLOC( Gia_ManSwi_t, 1 );
104     memset( p, 0, sizeof(Gia_ManSwi_t) );
105     p->pAig   = Gia_ManFront( pAig );
106     p->pPars  = pPars;
107     p->nWords = pPars->nWords;
108     p->pDataSim = ABC_ALLOC( unsigned, p->nWords * p->pAig->nFront );
109     p->pDataSimCis = ABC_ALLOC( unsigned, p->nWords * Gia_ManCiNum(p->pAig) );
110     p->pDataSimCos = ABC_ALLOC( unsigned, p->nWords * Gia_ManCoNum(p->pAig) );
111     p->pData1 = ABC_CALLOC( int, Gia_ManObjNum(pAig) );
112     return p;
113 }
114 
115 /**Function*************************************************************
116 
117   Synopsis    []
118 
119   Description []
120 
121   SideEffects []
122 
123   SeeAlso     []
124 
125 ***********************************************************************/
Gia_ManSwiDelete(Gia_ManSwi_t * p)126 void Gia_ManSwiDelete( Gia_ManSwi_t * p )
127 {
128     Gia_ManStop( p->pAig );
129     ABC_FREE( p->pData1 );
130     ABC_FREE( p->pDataSim );
131     ABC_FREE( p->pDataSimCis );
132     ABC_FREE( p->pDataSimCos );
133     ABC_FREE( p );
134 }
135 
136 /**Function*************************************************************
137 
138   Synopsis    []
139 
140   Description []
141 
142   SideEffects []
143 
144   SeeAlso     []
145 
146 ***********************************************************************/
Gia_ManSwiSimInfoRandom(Gia_ManSwi_t * p,unsigned * pInfo,int nProbNum)147 static inline void Gia_ManSwiSimInfoRandom( Gia_ManSwi_t * p, unsigned * pInfo, int nProbNum )
148 {
149     unsigned Mask = 0;
150     int w, i;
151     if ( nProbNum == -1 )
152     { // 3/8 = 1/4 + 1/8
153         Mask = (Gia_ManRandom( 0 ) & Gia_ManRandom( 0 )) |
154                (Gia_ManRandom( 0 ) & Gia_ManRandom( 0 ) & Gia_ManRandom( 0 ));
155         for ( w = p->nWords-1; w >= 0; w-- )
156             pInfo[w] ^= Mask;
157     }
158     else if ( nProbNum > 0 )
159     {
160         Mask = Gia_ManRandom( 0 );
161         for ( i = 0; i < nProbNum; i++ )
162             Mask &= Gia_ManRandom( 0 );
163         for ( w = p->nWords-1; w >= 0; w-- )
164             pInfo[w] ^= Mask;
165     }
166     else if ( nProbNum == 0 )
167     {
168         for ( w = p->nWords-1; w >= 0; w-- )
169             pInfo[w] = Gia_ManRandom( 0 );
170     }
171     else
172         assert( 0 );
173 }
174 
175 /**Function*************************************************************
176 
177   Synopsis    []
178 
179   Description []
180 
181   SideEffects []
182 
183   SeeAlso     []
184 
185 ***********************************************************************/
Gia_ManSwiSimInfoRandomShift(Gia_ManSwi_t * p,unsigned * pInfo,int nProbNum)186 static inline void Gia_ManSwiSimInfoRandomShift( Gia_ManSwi_t * p, unsigned * pInfo, int nProbNum )
187 {
188     unsigned Mask = 0;
189     int w, i;
190     if ( nProbNum == -1 )
191     { // 3/8 = 1/4 + 1/8
192         Mask = (Gia_ManRandom( 0 ) & Gia_ManRandom( 0 )) |
193                (Gia_ManRandom( 0 ) & Gia_ManRandom( 0 ) & Gia_ManRandom( 0 ));
194     }
195     else if ( nProbNum >= 0 )
196     {
197         Mask = Gia_ManRandom( 0 );
198         for ( i = 0; i < nProbNum; i++ )
199             Mask &= Gia_ManRandom( 0 );
200     }
201     else
202         assert( 0 );
203     for ( w = p->nWords-1; w >= 0; w-- )
204         pInfo[w] = (pInfo[w] << 16) | ((pInfo[w] ^ Mask) & 0xffff);
205 }
206 
207 /**Function*************************************************************
208 
209   Synopsis    []
210 
211   Description []
212 
213   SideEffects []
214 
215   SeeAlso     []
216 
217 ***********************************************************************/
Gia_ManSwiSimInfoZero(Gia_ManSwi_t * p,unsigned * pInfo)218 static inline void Gia_ManSwiSimInfoZero( Gia_ManSwi_t * p, unsigned * pInfo )
219 {
220     int w;
221     for ( w = p->nWords-1; w >= 0; w-- )
222         pInfo[w] = 0;
223 }
224 
225 /**Function*************************************************************
226 
227   Synopsis    []
228 
229   Description []
230 
231   SideEffects []
232 
233   SeeAlso     []
234 
235 ***********************************************************************/
Gia_ManSwiSimInfoOne(Gia_ManSwi_t * p,unsigned * pInfo)236 static inline void Gia_ManSwiSimInfoOne( Gia_ManSwi_t * p, unsigned * pInfo )
237 {
238     int w;
239     for ( w = p->nWords-1; w >= 0; w-- )
240         pInfo[w] = ~0;
241 }
242 
243 /**Function*************************************************************
244 
245   Synopsis    []
246 
247   Description []
248 
249   SideEffects []
250 
251   SeeAlso     []
252 
253 ***********************************************************************/
Gia_ManSwiSimInfoCopy(Gia_ManSwi_t * p,unsigned * pInfo,unsigned * pInfo0)254 static inline void Gia_ManSwiSimInfoCopy( Gia_ManSwi_t * p, unsigned * pInfo, unsigned * pInfo0 )
255 {
256     int w;
257     for ( w = p->nWords-1; w >= 0; w-- )
258         pInfo[w] = pInfo0[w];
259 }
260 
261 /**Function*************************************************************
262 
263   Synopsis    []
264 
265   Description []
266 
267   SideEffects []
268 
269   SeeAlso     []
270 
271 ***********************************************************************/
Gia_ManSwiSimInfoCopyShift(Gia_ManSwi_t * p,unsigned * pInfo,unsigned * pInfo0)272 static inline void Gia_ManSwiSimInfoCopyShift( Gia_ManSwi_t * p, unsigned * pInfo, unsigned * pInfo0 )
273 {
274     int w;
275     for ( w = p->nWords-1; w >= 0; w-- )
276         pInfo[w] = (pInfo[w] << 16) | (pInfo0[w] & 0xffff);
277 }
278 
279 /**Function*************************************************************
280 
281   Synopsis    []
282 
283   Description []
284 
285   SideEffects []
286 
287   SeeAlso     []
288 
289 ***********************************************************************/
Gia_ManSwiSimulateCi(Gia_ManSwi_t * p,Gia_Obj_t * pObj,int iCi)290 static inline void Gia_ManSwiSimulateCi( Gia_ManSwi_t * p, Gia_Obj_t * pObj, int iCi )
291 {
292     unsigned * pInfo  = Gia_SwiData( p, Gia_ObjValue(pObj) );
293     unsigned * pInfo0 = Gia_SwiDataCi( p, iCi );
294     int w;
295     for ( w = p->nWords-1; w >= 0; w-- )
296         pInfo[w] = pInfo0[w];
297 }
298 
299 /**Function*************************************************************
300 
301   Synopsis    []
302 
303   Description []
304 
305   SideEffects []
306 
307   SeeAlso     []
308 
309 ***********************************************************************/
Gia_ManSwiSimulateCo(Gia_ManSwi_t * p,int iCo,Gia_Obj_t * pObj)310 static inline void Gia_ManSwiSimulateCo( Gia_ManSwi_t * p, int iCo, Gia_Obj_t * pObj )
311 {
312     unsigned * pInfo  = Gia_SwiDataCo( p, iCo );
313     unsigned * pInfo0 = Gia_SwiData( p, Gia_ObjDiff0(pObj) );
314     int w;
315     if ( Gia_ObjFaninC0(pObj) )
316         for ( w = p->nWords-1; w >= 0; w-- )
317             pInfo[w] = ~pInfo0[w];
318     else
319         for ( w = p->nWords-1; w >= 0; w-- )
320             pInfo[w] = pInfo0[w];
321 }
322 
323 /**Function*************************************************************
324 
325   Synopsis    []
326 
327   Description []
328 
329   SideEffects []
330 
331   SeeAlso     []
332 
333 ***********************************************************************/
Gia_ManSwiSimulateNode(Gia_ManSwi_t * p,Gia_Obj_t * pObj)334 static inline void Gia_ManSwiSimulateNode( Gia_ManSwi_t * p, Gia_Obj_t * pObj )
335 {
336     unsigned * pInfo  = Gia_SwiData( p, Gia_ObjValue(pObj) );
337     unsigned * pInfo0 = Gia_SwiData( p, Gia_ObjDiff0(pObj) );
338     unsigned * pInfo1 = Gia_SwiData( p, Gia_ObjDiff1(pObj) );
339     int w;
340     if ( Gia_ObjFaninC0(pObj) )
341     {
342         if (  Gia_ObjFaninC1(pObj) )
343             for ( w = p->nWords-1; w >= 0; w-- )
344                 pInfo[w] = ~(pInfo0[w] | pInfo1[w]);
345         else
346             for ( w = p->nWords-1; w >= 0; w-- )
347                 pInfo[w] = ~pInfo0[w] & pInfo1[w];
348     }
349     else
350     {
351         if (  Gia_ObjFaninC1(pObj) )
352             for ( w = p->nWords-1; w >= 0; w-- )
353                 pInfo[w] = pInfo0[w] & ~pInfo1[w];
354         else
355             for ( w = p->nWords-1; w >= 0; w-- )
356                 pInfo[w] = pInfo0[w] & pInfo1[w];
357     }
358 }
359 
360 /**Function*************************************************************
361 
362   Synopsis    []
363 
364   Description []
365 
366   SideEffects []
367 
368   SeeAlso     []
369 
370 ***********************************************************************/
Gia_ManSwiSimInfoInit(Gia_ManSwi_t * p)371 static inline void Gia_ManSwiSimInfoInit( Gia_ManSwi_t * p )
372 {
373     int i = 0;
374     for ( ; i < Gia_ManPiNum(p->pAig); i++ )
375         Gia_ManSwiSimInfoRandom( p, Gia_SwiDataCi(p, i), 0 );
376     for ( ; i < Gia_ManCiNum(p->pAig); i++ )
377         Gia_ManSwiSimInfoZero( p, Gia_SwiDataCi(p, i) );
378 }
379 
380 /**Function*************************************************************
381 
382   Synopsis    []
383 
384   Description []
385 
386   SideEffects []
387 
388   SeeAlso     []
389 
390 ***********************************************************************/
Gia_ManSwiSimInfoTransfer(Gia_ManSwi_t * p,int nProbNum)391 static inline void Gia_ManSwiSimInfoTransfer( Gia_ManSwi_t * p, int nProbNum )
392 {
393     int i = 0, nShift = Gia_ManPoNum(p->pAig)-Gia_ManPiNum(p->pAig);
394     for ( ; i < Gia_ManPiNum(p->pAig); i++ )
395         Gia_ManSwiSimInfoRandom( p, Gia_SwiDataCi(p, i), nProbNum );
396     for ( ; i < Gia_ManCiNum(p->pAig); i++ )
397         Gia_ManSwiSimInfoCopy( p, Gia_SwiDataCi(p, i), Gia_SwiDataCo(p, nShift+i) );
398 }
399 
400 /**Function*************************************************************
401 
402   Synopsis    []
403 
404   Description []
405 
406   SideEffects []
407 
408   SeeAlso     []
409 
410 ***********************************************************************/
Gia_ManSwiSimInfoTransferShift(Gia_ManSwi_t * p,int nProbNum)411 static inline void Gia_ManSwiSimInfoTransferShift( Gia_ManSwi_t * p, int nProbNum )
412 {
413     int i = 0, nShift = Gia_ManPoNum(p->pAig)-Gia_ManPiNum(p->pAig);
414     for ( ; i < Gia_ManPiNum(p->pAig); i++ )
415         Gia_ManSwiSimInfoRandomShift( p, Gia_SwiDataCi(p, i), nProbNum );
416     for ( ; i < Gia_ManCiNum(p->pAig); i++ )
417         Gia_ManSwiSimInfoCopyShift( p, Gia_SwiDataCi(p, i), Gia_SwiDataCo(p, nShift+i) );
418 }
419 
420 /**Function*************************************************************
421 
422   Synopsis    []
423 
424   Description []
425 
426   SideEffects []
427 
428   SeeAlso     []
429 
430 ***********************************************************************/
Gia_ManSwiSimInfoCountOnes(Gia_ManSwi_t * p,int iPlace)431 static inline int Gia_ManSwiSimInfoCountOnes( Gia_ManSwi_t * p, int iPlace )
432 {
433     unsigned * pInfo;
434     int w, Counter = 0;
435     pInfo = Gia_SwiData( p, iPlace );
436     for ( w = p->nWords-1; w >= 0; w-- )
437         Counter += Gia_WordCountOnes( pInfo[w] );
438     return Counter;
439 }
440 
441 /**Function*************************************************************
442 
443   Synopsis    []
444 
445   Description []
446 
447   SideEffects []
448 
449   SeeAlso     []
450 
451 ***********************************************************************/
Gia_ManSwiSimInfoCountTrans(Gia_ManSwi_t * p,int iPlace)452 static inline int Gia_ManSwiSimInfoCountTrans( Gia_ManSwi_t * p, int iPlace )
453 {
454     unsigned * pInfo;
455     int w, Counter = 0;
456     pInfo = Gia_SwiData( p, iPlace );
457     for ( w = p->nWords-1; w >= 0; w-- )
458         Counter += 2*Gia_WordCountOnes( (pInfo[w] ^ (pInfo[w] >> 16)) & 0xffff );
459     return Counter;
460 }
461 
462 /**Function*************************************************************
463 
464   Synopsis    []
465 
466   Description []
467 
468   SideEffects []
469 
470   SeeAlso     []
471 
472 ***********************************************************************/
Gia_ManSwiSimulateRound(Gia_ManSwi_t * p,int fCount)473 static inline void Gia_ManSwiSimulateRound( Gia_ManSwi_t * p, int fCount )
474 {
475     Gia_Obj_t * pObj;
476     int i;//, iCis = 0, iCos = 0;
477     assert( p->pAig->nFront > 0 );
478     assert( Gia_ManConst0(p->pAig)->Value == 0 );
479     Gia_ManSwiSimInfoZero( p, Gia_SwiData(p, 0) );
480     Gia_ManForEachObj1( p->pAig, pObj, i )
481     {
482         if ( Gia_ObjIsAndOrConst0(pObj) )
483         {
484             assert( Gia_ObjValue(pObj) < p->pAig->nFront );
485             Gia_ManSwiSimulateNode( p, pObj );
486         }
487         else if ( Gia_ObjIsCo(pObj) )
488         {
489             assert( Gia_ObjValue(pObj) == GIA_NONE );
490 //            Gia_ManSwiSimulateCo( p, iCos++, pObj );
491             Gia_ManSwiSimulateCo( p, Gia_ObjCioId(pObj), pObj );
492         }
493         else // if ( Gia_ObjIsCi(pObj) )
494         {
495             assert( Gia_ObjValue(pObj) < p->pAig->nFront );
496 //            Gia_ManSwiSimulateCi( p, pObj, iCis++ );
497             Gia_ManSwiSimulateCi( p, pObj, Gia_ObjCioId(pObj) );
498         }
499         if ( fCount && !Gia_ObjIsCo(pObj) )
500         {
501             if ( p->pPars->fProbTrans )
502                 p->pData1[i] += Gia_ManSwiSimInfoCountTrans( p, Gia_ObjValue(pObj) );
503             else
504                 p->pData1[i] += Gia_ManSwiSimInfoCountOnes( p, Gia_ObjValue(pObj) );
505         }
506     }
507 //    assert( Gia_ManCiNum(p->pAig) == iCis );
508 //    assert( Gia_ManCoNum(p->pAig) == iCos );
509 }
510 
511 /**Function*************************************************************
512 
513   Synopsis    [Computes switching activity of one node.]
514 
515   Description [Uses the formula: Switching = 2 * nOnes * nZeros / (nTotal ^ 2) ]
516 
517   SideEffects []
518 
519   SeeAlso     []
520 
521 ***********************************************************************/
Gia_ManSwiComputeSwitching(int nOnes,int nSimWords)522 float Gia_ManSwiComputeSwitching( int nOnes, int nSimWords )
523 {
524     int nTotal = 32 * nSimWords;
525     return (float)2.0 * nOnes / nTotal * (nTotal - nOnes) / nTotal;
526 }
527 
528 /**Function*************************************************************
529 
530   Synopsis    [Computes switching activity of one node.]
531 
532   Description [Uses the formula: Switching = 2 * nOnes * nZeros / (nTotal ^ 2) ]
533 
534   SideEffects []
535 
536   SeeAlso     []
537 
538 ***********************************************************************/
Gia_ManSwiComputeProbOne(int nOnes,int nSimWords)539 float Gia_ManSwiComputeProbOne( int nOnes, int nSimWords )
540 {
541     int nTotal = 32 * nSimWords;
542     return (float)nOnes / nTotal;
543 }
544 
545 /**Function*************************************************************
546 
547   Synopsis    []
548 
549   Description []
550 
551   SideEffects []
552 
553   SeeAlso     []
554 
555 ***********************************************************************/
Gia_ManSwiSimulate(Gia_Man_t * pAig,Gia_ParSwi_t * pPars)556 Vec_Int_t * Gia_ManSwiSimulate( Gia_Man_t * pAig, Gia_ParSwi_t * pPars )
557 {
558     Gia_ManSwi_t * p;
559     Gia_Obj_t * pObj;
560     Vec_Int_t * vSwitching;
561     float * pSwitching;
562     int i;
563     abctime clk, clkTotal = Abc_Clock();
564     if ( pPars->fProbOne && pPars->fProbTrans )
565         printf( "Conflict of options: Can either compute probability of 1, or probability of switching by observing transitions.\n" );
566     // create manager
567     clk = Abc_Clock();
568     p = Gia_ManSwiCreate( pAig, pPars );
569     if ( pPars->fVerbose )
570     {
571         printf( "Obj = %8d (%8d). F = %6d. ",
572             pAig->nObjs, Gia_ManCiNum(pAig) + Gia_ManAndNum(pAig), p->pAig->nFront );
573         printf( "AIG = %7.2f MB. F-mem = %7.2f MB. Other = %7.2f MB.  ",
574             12.0*Gia_ManObjNum(p->pAig)/(1<<20),
575             4.0*p->nWords*p->pAig->nFront/(1<<20),
576             4.0*p->nWords*(Gia_ManCiNum(p->pAig) + Gia_ManCoNum(p->pAig))/(1<<20) );
577         ABC_PRT( "Time", Abc_Clock() - clk );
578     }
579     // perform simulation
580     Gia_ManRandom( 1 );
581     Gia_ManSwiSimInfoInit( p );
582     for ( i = 0; i < pPars->nIters; i++ )
583     {
584         Gia_ManSwiSimulateRound( p, i >= pPars->nPref );
585         if ( i == pPars->nIters - 1 )
586             break;
587         if ( pPars->fProbTrans )
588             Gia_ManSwiSimInfoTransferShift( p, pPars->nRandPiFactor );
589         else
590             Gia_ManSwiSimInfoTransfer( p, pPars->nRandPiFactor );
591     }
592     if ( pPars->fVerbose )
593     {
594         printf( "Simulated %d frames with %d words. ", pPars->nIters, pPars->nWords );
595         ABC_PRT( "Simulation time", Abc_Clock() - clkTotal );
596     }
597     // derive the result
598     vSwitching = Vec_IntStart( Gia_ManObjNum(pAig) );
599     pSwitching = (float *)vSwitching->pArray;
600     if ( pPars->fProbOne )
601     {
602         Gia_ManForEachObj( pAig, pObj, i )
603             pSwitching[i] = Gia_ManSwiComputeProbOne( p->pData1[i], pPars->nWords*(pPars->nIters-pPars->nPref) );
604         Gia_ManForEachCo( pAig, pObj, i )
605         {
606             if ( Gia_ObjFaninC0(pObj) )
607                 pSwitching[Gia_ObjId(pAig,pObj)] = (float)1.0-pSwitching[Gia_ObjId(pAig,Gia_ObjFanin0(pObj))];
608             else
609                 pSwitching[Gia_ObjId(pAig,pObj)] = pSwitching[Gia_ObjId(pAig,Gia_ObjFanin0(pObj))];
610         }
611     }
612     else if ( pPars->fProbTrans )
613     {
614         Gia_ManForEachObj( pAig, pObj, i )
615             pSwitching[i] = Gia_ManSwiComputeProbOne( p->pData1[i], pPars->nWords*(pPars->nIters-pPars->nPref) );
616     }
617     else
618     {
619         Gia_ManForEachObj( pAig, pObj, i )
620             pSwitching[i] = Gia_ManSwiComputeSwitching( p->pData1[i], pPars->nWords*(pPars->nIters-pPars->nPref) );
621     }
622 /*
623     printf( "PI: " );
624     Gia_ManForEachPi( pAig, pObj, i )
625         printf( "%d=%d (%f)  ", i, p->pData1[Gia_ObjId(pAig,pObj)], pSwitching[Gia_ObjId(pAig,pObj)] );
626     printf( "\n" );
627 
628     printf( "LO: " );
629     Gia_ManForEachRo( pAig, pObj, i )
630         printf( "%d=%d (%f)  ", i, p->pData1[Gia_ObjId(pAig,pObj)], pSwitching[Gia_ObjId(pAig,pObj)] );
631     printf( "\n" );
632 
633     printf( "PO: " );
634     Gia_ManForEachPo( pAig, pObj, i )
635         printf( "%d=%d (%f)  ", i, p->pData1[Gia_ObjId(pAig,pObj)], pSwitching[Gia_ObjId(pAig,pObj)] );
636     printf( "\n" );
637 
638     printf( "LI: " );
639     Gia_ManForEachRi( pAig, pObj, i )
640         printf( "%d=%d (%f)  ", i, p->pData1[Gia_ObjId(pAig,pObj)], pSwitching[Gia_ObjId(pAig,pObj)] );
641     printf( "\n" );
642 */
643     Gia_ManSwiDelete( p );
644     return vSwitching;
645 
646 }
647 /**Function*************************************************************
648 
649   Synopsis    [Computes probability of switching (or of being 1).]
650 
651   Description []
652 
653   SideEffects []
654 
655   SeeAlso     []
656 
657 ***********************************************************************/
Gia_ManComputeSwitchProbs(Gia_Man_t * pGia,int nFrames,int nPref,int fProbOne)658 Vec_Int_t * Gia_ManComputeSwitchProbs( Gia_Man_t * pGia, int nFrames, int nPref, int fProbOne )
659 {
660     Gia_ParSwi_t Pars, * pPars = &Pars;
661     // set the default parameters
662     Gia_ManSetDefaultParamsSwi( pPars );
663     // override some of the defaults
664     pPars->nIters   = nFrames;  // set number of total timeframes
665     if ( Abc_FrameReadFlag("seqsimframes") )
666         pPars->nIters = atoi( Abc_FrameReadFlag("seqsimframes") );
667     pPars->nPref    = nPref;    // set number of first timeframes to skip
668     // decide what should be computed
669     if ( fProbOne )
670     {
671         // if the user asked to compute propability of 1, we do not need transition information
672         pPars->fProbOne   = 1;  // enable computing probabiblity of being one
673         pPars->fProbTrans = 0;  // disable computing transition probability
674     }
675     else
676     {
677         // if the user asked for transition propabability, we do not need to compute probability of 1
678         pPars->fProbOne   = 0;  // disable computing probabiblity of being one
679         pPars->fProbTrans = 1;  // enable computing transition probability
680     }
681     // perform the computation of switching activity
682     return Gia_ManSwiSimulate( pGia, pPars );
683 }
Saig_ManComputeSwitchProbs(Aig_Man_t * pAig,int nFrames,int nPref,int fProbOne)684 Vec_Int_t * Saig_ManComputeSwitchProbs( Aig_Man_t * pAig, int nFrames, int nPref, int fProbOne )
685 {
686     Vec_Int_t * vSwitching, * vResult;
687     Gia_Man_t * p;
688     Aig_Obj_t * pObj;
689     int i;
690     // translate AIG into the intermediate form (takes care of choices if present!)
691     p = Gia_ManFromAigSwitch( pAig );
692     // perform the computation of switching activity
693     vSwitching = Gia_ManComputeSwitchProbs( p, nFrames, nPref, fProbOne );
694     // transfer the computed result to the original AIG
695     vResult = Vec_IntStart( Aig_ManObjNumMax(pAig) );
696     Aig_ManForEachObj( pAig, pObj, i )
697     {
698 //        if ( Aig_ObjIsCo(pObj) )
699 //            printf( "%d=%f\n", i, Abc_Int2Float( Vec_IntEntry(vSwitching, Abc_Lit2Var(pObj->iData)) ) );
700         Vec_IntWriteEntry( vResult, i, Vec_IntEntry(vSwitching, Abc_Lit2Var(pObj->iData)) );
701     }
702     // delete intermediate results
703     Vec_IntFree( vSwitching );
704     Gia_ManStop( p );
705     return vResult;
706 }
707 
708 /**Function*************************************************************
709 
710   Synopsis    [Computes probability of switching (or of being 1).]
711 
712   Description []
713 
714   SideEffects []
715 
716   SeeAlso     []
717 
718 ***********************************************************************/
Gia_ManEvaluateSwitching(Gia_Man_t * p)719 float Gia_ManEvaluateSwitching( Gia_Man_t * p )
720 {
721     Gia_Obj_t * pObj;
722     float SwitchTotal = 0.0;
723     int i;
724     assert( p->pSwitching );
725     ABC_FREE( p->pRefs );
726     Gia_ManCreateRefs( p );
727     Gia_ManForEachObj( p, pObj, i )
728         SwitchTotal += (float)Gia_ObjRefNum(p, pObj) * p->pSwitching[i] / 255;
729     return SwitchTotal;
730 }
731 
732 /**Function*************************************************************
733 
734   Synopsis    [Computes probability of switching (or of being 1).]
735 
736   Description []
737 
738   SideEffects []
739 
740   SeeAlso     []
741 
742 ***********************************************************************/
743 /*
744 float Gia_ManComputeSwitching( Gia_Man_t * p, int nFrames, int nPref, int fProbOne )
745 {
746     Gia_Man_t * pDfs;
747     Gia_Obj_t * pObj, * pObjDfs;
748     Vec_Int_t * vSwitching;
749     float * pSwitching, Switch, SwitchTotal = 0.0;
750     int i;
751     // derives the DFS ordered AIG
752     if ( Gia_ManHasMapping(p) )
753         Gia_ManSetRefsMapped(p);
754     else
755         Gia_ManCreateRefs( p );
756 //    pDfs = Gia_ManDupOrderDfs( p );
757     pDfs = Gia_ManDup( p );
758     assert( Gia_ManObjNum(pDfs) == Gia_ManObjNum(p) );
759     // perform the computation of switching activity
760     vSwitching = Gia_ManComputeSwitchProbs( pDfs, nFrames, nPref, fProbOne );
761     // transfer the computed result to the original AIG
762     ABC_FREE( p->pSwitching );
763     p->pSwitching = ABC_CALLOC( unsigned char, Gia_ManObjNum(p) );
764     pSwitching = (float *)vSwitching->pArray;
765     Gia_ManForEachObj( p, pObj, i )
766     {
767         pObjDfs = Gia_ObjFromLit( pDfs, pObj->Value );
768         Switch = pSwitching[ Gia_ObjId(pDfs, pObjDfs) ];
769         p->pSwitching[i] = (char)((Switch >= 1.0) ? 255 : (int)((0.002 + Switch) * 255)); // 0.00196 = (1/255)/2
770         if ( Gia_ObjIsCi(pObj) || (Gia_ObjIsAnd(pObj) && (!Gia_ManHasMapping(p) || Gia_ObjIsLut(p, i))) )
771         {
772             SwitchTotal += (float)Gia_ObjRefNum(p, pObj) * p->pSwitching[i] / 255;
773 //            printf( "%d = %.2f\n", i, (float)Gia_ObjRefNum(p, pObj) * p->pSwitching[i] / 255 );
774         }
775     }
776     Vec_IntFree( vSwitching );
777     Gia_ManStop( pDfs );
778     return SwitchTotal;
779 }
780 */
Gia_ManComputeSwitching(Gia_Man_t * p,int nFrames,int nPref,int fProbOne)781 float Gia_ManComputeSwitching( Gia_Man_t * p, int nFrames, int nPref, int fProbOne )
782 {
783     Vec_Int_t * vSwitching = Gia_ManComputeSwitchProbs( p, nFrames, nPref, fProbOne );
784     float * pSwi = (float *)Vec_IntArray(vSwitching), SwiTotal = 0;
785     Gia_Obj_t * pObj;
786     int i, k, iFan;
787     if ( Gia_ManHasMapping(p) )
788     {
789         Gia_ManForEachLut( p, i )
790             Gia_LutForEachFanin( p, i, iFan, k )
791                 SwiTotal += pSwi[iFan];
792     }
793     else
794     {
795         Gia_ManForEachAnd( p, pObj, i )
796             SwiTotal += pSwi[Gia_ObjFaninId0(pObj, i)] + pSwi[Gia_ObjFaninId1(pObj, i)];
797     }
798     Vec_IntFree( vSwitching );
799     return SwiTotal;
800 }
801 
802 /**Function*************************************************************
803 
804   Synopsis    [Determine probability of being 1 at the outputs.]
805 
806   Description []
807 
808   SideEffects []
809 
810   SeeAlso     []
811 
812 ***********************************************************************/
Gia_ManPrintOutputProb(Gia_Man_t * p)813 Vec_Flt_t * Gia_ManPrintOutputProb( Gia_Man_t * p )
814 {
815     Vec_Flt_t * vSimData;
816     Gia_Man_t * pDfs = Gia_ManDup( p );
817     assert( Gia_ManObjNum(pDfs) == Gia_ManObjNum(p) );
818     vSimData = (Vec_Flt_t *)Gia_ManComputeSwitchProbs( pDfs, (Gia_ManRegNum(p) ? 16 : 1), 0, 1 );
819     Gia_ManStop( pDfs );
820     return vSimData;
821 }
822 
823 ////////////////////////////////////////////////////////////////////////
824 ///                       END OF FILE                                ///
825 ////////////////////////////////////////////////////////////////////////
826 
827 
828 ABC_NAMESPACE_IMPL_END
829 
830