abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sfmCore.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [sfmCore.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT-based optimization using internal don't-cares.]
8 
9  Synopsis [Core procedures.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: sfmCore.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "sfmInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Setup parameter structure.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
46 {
47  memset( pPars, 0, sizeof(Sfm_Par_t) );
48  pPars->nTfoLevMax = 2; // the maximum fanout levels
49  pPars->nFanoutMax = 30; // the maximum number of fanouts
50  pPars->nDepthMax = 20; // the maximum depth to try
51  pPars->nWinSizeMax = 300; // the maximum window size
52  pPars->nGrowthLevel = 0; // the maximum allowed growth in level
53  pPars->nBTLimit = 5000; // the maximum number of conflicts in one SAT run
54  pPars->fRrOnly = 0; // perform redundancy removal
55  pPars->fArea = 0; // performs optimization for area
56  pPars->fMoreEffort = 0; // performs high-affort minimization
57  pPars->fVerbose = 0; // enable basic stats
58  pPars->fVeryVerbose = 0; // enable detailed stats
59 }
60 
61 /**Function*************************************************************
62 
63  Synopsis [Prints statistics.]
64 
65  Description []
66 
67  SideEffects []
68 
69  SeeAlso []
70 
71 ***********************************************************************/
73 {
74  p->timeOther = p->timeTotal - p->timeWin - p->timeDiv - p->timeCnf - p->timeSat;
75  printf( "Nodes = %d. Try = %d. Resub = %d. Div = %d. SAT calls = %d. Timeouts = %d. MaxDivs = %d.\n",
76  Sfm_NtkNodeNum(p), p->nNodesTried, p->nRemoves + p->nResubs, p->nTotalDivs, p->nSatCalls, p->nTimeOuts, p->nMaxDivs );
77 
78  printf( "Attempts : " );
79  printf( "Remove %6d out of %6d (%6.2f %%) ", p->nRemoves, p->nTryRemoves, 100.0*p->nRemoves/Abc_MaxInt(1, p->nTryRemoves) );
80  printf( "Resub %6d out of %6d (%6.2f %%) ", p->nResubs, p->nTryResubs, 100.0*p->nResubs /Abc_MaxInt(1, p->nTryResubs) );
81  printf( "\n" );
82 
83  printf( "Reduction: " );
84  printf( "Nodes %6d out of %6d (%6.2f %%) ", p->nTotalNodesBeg-p->nTotalNodesEnd, p->nTotalNodesBeg, 100.0*(p->nTotalNodesBeg-p->nTotalNodesEnd)/Abc_MaxInt(1, p->nTotalNodesBeg) );
85  printf( "Edges %6d out of %6d (%6.2f %%) ", p->nTotalEdgesBeg-p->nTotalEdgesEnd, p->nTotalEdgesBeg, 100.0*(p->nTotalEdgesBeg-p->nTotalEdgesEnd)/Abc_MaxInt(1, p->nTotalEdgesBeg) );
86  printf( "\n" );
87 
88  ABC_PRTP( "Win", p->timeWin , p->timeTotal );
89  ABC_PRTP( "Div", p->timeDiv , p->timeTotal );
90  ABC_PRTP( "Cnf", p->timeCnf , p->timeTotal );
91  ABC_PRTP( "Sat", p->timeSat , p->timeTotal );
92  ABC_PRTP( "Oth", p->timeOther, p->timeTotal );
93  ABC_PRTP( "ALL", p->timeTotal, p->timeTotal );
94 // ABC_PRTP( " ", p->time1 , p->timeTotal );
95 }
96 
97 /**Function*************************************************************
98 
99  Synopsis [Performs resubstitution for the node.]
100 
101  Description []
102 
103  SideEffects []
104 
105  SeeAlso []
106 
107 ***********************************************************************/
108 int Sfm_NodeResubSolve( Sfm_Ntk_t * p, int iNode, int f, int fRemoveOnly )
109 {
110  int fSkipUpdate = 0;
111  int fVeryVerbose = 0;//p->pPars->fVeryVerbose && Vec_IntSize(p->vDivs) < 200;// || pNode->Id == 556;
112  int i, iFanin, iVar = -1;
113  word uTruth, uSign, uMask;
114  abctime clk;
115  assert( Sfm_ObjIsNode(p, iNode) );
116  assert( f >= 0 && f < Sfm_ObjFaninNum(p, iNode) );
117  p->nTryRemoves++;
118  // report init stats
119  if ( p->pPars->fVeryVerbose )
120  printf( "%5d : Lev =%3d. Leaf =%3d. Node =%3d. Div=%3d. Fanin =%4d (%d/%d). MFFC = %d\n",
121  iNode, Sfm_ObjLevel(p, iNode), 0, Vec_IntSize(p->vNodes), Vec_IntSize(p->vDivs),
122  Sfm_ObjFanin(p, iNode, f), f, Sfm_ObjFaninNum(p, iNode), Sfm_ObjMffcSize(p, Sfm_ObjFanin(p, iNode, f)) );
123  // clean simulation info
124  p->nCexes = 0;
125  Vec_WrdFill( p->vDivCexes, Vec_IntSize(p->vDivs), 0 );
126  // try removing the critical fanin
127  Vec_IntClear( p->vDivIds );
128  Sfm_ObjForEachFanin( p, iNode, iFanin, i )
129  if ( i != f )
130  Vec_IntPush( p->vDivIds, Sfm_ObjSatVar(p, iFanin) );
131 clk = Abc_Clock();
132  uTruth = Sfm_ComputeInterpolant( p );
133 p->timeSat += Abc_Clock() - clk;
134  // analyze outcomes
135  if ( uTruth == SFM_SAT_UNDEC )
136  {
137  p->nTimeOuts++;
138  return 0;
139  }
140  if ( uTruth != SFM_SAT_SAT )
141  goto finish;
142  if ( fRemoveOnly || p->pPars->fRrOnly || Vec_IntSize(p->vDivs) == 0 )
143  return 0;
144 
145  p->nTryResubs++;
146  if ( fVeryVerbose )
147  {
148  for ( i = 0; i < 9; i++ )
149  printf( " " );
150  for ( i = 0; i < Vec_IntSize(p->vDivs); i++ )
151  printf( "%d", i % 10 );
152  printf( "\n" );
153  }
154  while ( 1 )
155  {
156  if ( fVeryVerbose )
157  {
158  printf( "%3d: %3d ", p->nCexes, iVar );
159  Vec_WrdForEachEntry( p->vDivCexes, uSign, i )
160  printf( "%d", Abc_InfoHasBit((unsigned *)&uSign, p->nCexes-1) );
161  printf( "\n" );
162  }
163  // find the next divisor to try
164  uMask = (~(word)0) >> (64 - p->nCexes);
165  Vec_WrdForEachEntry( p->vDivCexes, uSign, iVar )
166  if ( uSign == uMask )
167  break;
168  if ( iVar == Vec_IntSize(p->vDivs) )
169  return 0;
170  // try replacing the critical fanin
171  Vec_IntPush( p->vDivIds, Sfm_ObjSatVar(p, Vec_IntEntry(p->vDivs, iVar)) );
172 clk = Abc_Clock();
173  uTruth = Sfm_ComputeInterpolant( p );
174 p->timeSat += Abc_Clock() - clk;
175  // analyze outcomes
176  if ( uTruth == SFM_SAT_UNDEC )
177  {
178  p->nTimeOuts++;
179  return 0;
180  }
181  if ( uTruth != SFM_SAT_SAT )
182  goto finish;
183  if ( p->nCexes == 64 )
184  return 0;
185  // remove the last variable
186  Vec_IntPop( p->vDivIds );
187  }
188 finish:
189  if ( p->pPars->fVeryVerbose )
190  {
191  if ( iVar == -1 )
192  printf( "Node %d: Fanin %d (%d) can be removed. ", iNode, f, Sfm_ObjFanin(p, iNode, f) );
193  else
194  printf( "Node %d: Fanin %d (%d) can be replaced by divisor %d (%d). ",
195  iNode, f, Sfm_ObjFanin(p, iNode, f), iVar, Vec_IntEntry(p->vDivs, iVar) );
196  Kit_DsdPrintFromTruth( (unsigned *)&uTruth, Vec_IntSize(p->vDivIds) ); printf( "\n" );
197  }
198  if ( iVar == -1 )
199  p->nRemoves++;
200  else
201  p->nResubs++;
202  if ( fSkipUpdate )
203  return 0;
204  // update the network
205  Sfm_NtkUpdate( p, iNode, f, (iVar == -1 ? iVar : Vec_IntEntry(p->vDivs, iVar)), uTruth );
206  return 1;
207  }
208 int Sfm_NodeResub( Sfm_Ntk_t * p, int iNode )
209 {
210  int i, iFanin;
211  p->nNodesTried++;
212  // prepare SAT solver
213  if ( !Sfm_NtkCreateWindow( p, iNode, p->pPars->fVeryVerbose ) )
214  return 0;
215  if ( !Sfm_NtkWindowToSolver( p ) )
216  return 0;
217  // try replacing area critical fanins
218  Sfm_ObjForEachFanin( p, iNode, iFanin, i )
219  if ( Sfm_ObjIsNode(p, iFanin) && Sfm_ObjFanoutNum(p, iFanin) == 1 )
220  {
221  if ( Sfm_NodeResubSolve( p, iNode, i, 0 ) )
222  return 1;
223  }
224  if ( p->pPars->fArea )
225  return 0;
226  // try removing redundant edges
227  Sfm_ObjForEachFanin( p, iNode, iFanin, i )
228  if ( !(Sfm_ObjIsNode(p, iFanin) && Sfm_ObjFanoutNum(p, iFanin) == 1) )
229  {
230  if ( Sfm_NodeResubSolve( p, iNode, i, 1 ) )
231  return 1;
232  }
233 /*
234  // try replacing area critical fanins while adding two new fanins
235  if ( Sfm_ObjFaninNum(p, iNode) < p->nFaninMax )
236  Abc_ObjForEachFanin( pNode, pFanin, i )
237  if ( !Abc_ObjIsCi(pFanin) && Abc_ObjFanoutNum(pFanin) == 1 )
238  {
239  if ( Abc_NtkMfsSolveSatResub2( p, pNode, i, -1 ) )
240  return 1;
241  }
242 */
243  return 0;
244 }
245 
246 /**Function*************************************************************
247 
248  Synopsis []
249 
250  Description []
251 
252  SideEffects []
253 
254  SeeAlso []
255 
256 ***********************************************************************/
258 {
259  int i, k, Counter = 0;
260  p->timeTotal = Abc_Clock();
261  if ( pPars->fVerbose )
262  {
263  int nFixed = p->vFixed ? Vec_StrSum(p->vFixed) : 0;
264  int nEmpty = p->vEmpty ? Vec_StrSum(p->vEmpty) : 0;
265  printf( "Performing MFS with %d PIs, %d POs, %d nodes (%d flexible, %d fixed, %d empty).\n",
266  p->nPis, p->nPos, p->nNodes, p->nNodes-nFixed, nFixed, nEmpty );
267  }
268  p->pPars = pPars;
269  Sfm_NtkPrepare( p );
270 // Sfm_ComputeInterpolantCheck( p );
271 // return 0;
272  p->nTotalNodesBeg = Vec_WecSizeUsedLimits( &p->vFanins, Sfm_NtkPiNum(p), Vec_WecSize(&p->vFanins) - Sfm_NtkPoNum(p) );
273  p->nTotalEdgesBeg = Vec_WecSizeSize(&p->vFanins) - Sfm_NtkPoNum(p);
274  Sfm_NtkForEachNode( p, i )
275  {
276  if ( Sfm_ObjIsFixed( p, i ) )
277  continue;
278  if ( p->pPars->nDepthMax && Sfm_ObjLevel(p, i) > p->pPars->nDepthMax )
279  continue;
280  if ( Sfm_ObjFaninNum(p, i) < 2 || Sfm_ObjFaninNum(p, i) > 6 )
281  continue;
282  for ( k = 0; Sfm_NodeResub(p, i); k++ )
283  {
284 // Counter++;
285 // break;
286  }
287  Counter += (k > 0);
288  if ( pPars->nNodesMax && Counter >= pPars->nNodesMax )
289  break;
290  }
291  p->nTotalNodesEnd = Vec_WecSizeUsedLimits( &p->vFanins, Sfm_NtkPiNum(p), Vec_WecSize(&p->vFanins) - Sfm_NtkPoNum(p) );
292  p->nTotalEdgesEnd = Vec_WecSizeSize(&p->vFanins) - Sfm_NtkPoNum(p);
293  p->timeTotal = Abc_Clock() - p->timeTotal;
294  if ( pPars->fVerbose )
295  Sfm_NtkPrintStats( p );
296  return Counter;
297 }
298 
299 ////////////////////////////////////////////////////////////////////////
300 /// END OF FILE ///
301 ////////////////////////////////////////////////////////////////////////
302 
303 
305 
char * memset()
int fRrOnly
Definition: sfm.h:53
int Sfm_NtkWindowToSolver(Sfm_Ntk_t *p)
FUNCTION DEFINITIONS ///.
Definition: sfmSat.c:54
void Sfm_NtkUpdate(Sfm_Ntk_t *p, int iNode, int f, int iFaninNew, word uTruth)
Definition: sfmNtk.c:314
#define Sfm_ObjForEachFanin(p, Node, Fan, i)
Definition: sfmInt.h:172
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static int Abc_InfoHasBit(unsigned *p, int i)
Definition: abc_global.h:258
ABC_NAMESPACE_IMPL_START void Sfm_ParSetDefault(Sfm_Par_t *pPars)
DECLARATIONS ///.
Definition: sfmCore.c:45
int nNodesMax
Definition: sfm.h:51
static int Vec_StrSum(Vec_Str_t *p)
Definition: vecStr.h:689
static int Sfm_ObjFaninNum(Sfm_Ntk_t *p, int i)
Definition: sfmInt.h:139
typedefABC_NAMESPACE_HEADER_START struct Sfm_Ntk_t_ Sfm_Ntk_t
INCLUDES ///.
Definition: sfm.h:41
void Kit_DsdPrintFromTruth(unsigned *pTruth, int nVars)
Definition: kitDsd.c:490
int nBTLimit
Definition: sfm.h:50
static abctime Abc_Clock()
Definition: abc_global.h:279
static int Abc_MaxInt(int a, int b)
Definition: abc_global.h:238
#define Vec_WrdForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecWrd.h:54
Definition: sfm.h:43
int fArea
Definition: sfm.h:54
static int Vec_WecSize(Vec_Wec_t *p)
Definition: vecWec.h:193
static int Sfm_ObjIsNode(Sfm_Ntk_t *p, int i)
Definition: sfmInt.h:131
word Sfm_ComputeInterpolant(Sfm_Ntk_t *p)
Definition: sfmSat.c:162
static int Sfm_ObjLevel(Sfm_Ntk_t *p, int iObj)
Definition: sfmInt.h:153
int fMoreEffort
Definition: sfm.h:55
#define ABC_PRTP(a, t, T)
Definition: abc_global.h:223
int Sfm_NtkCreateWindow(Sfm_Ntk_t *p, int iNode, int fVerbose)
Definition: sfmWin.c:334
static int Sfm_ObjFanin(Sfm_Ntk_t *p, int i, int k)
Definition: sfmInt.h:145
int nDepthMax
Definition: sfm.h:47
int nGrowthLevel
Definition: sfm.h:49
static int Sfm_ObjSatVar(Sfm_Ntk_t *p, int iObj)
Definition: sfmInt.h:148
static void Vec_WrdFill(Vec_Wrd_t *p, int nSize, word Fill)
Definition: vecWrd.h:489
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
int Sfm_NtkPerform(Sfm_Ntk_t *p, Sfm_Par_t *pPars)
Definition: sfmCore.c:257
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Sfm_NtkPrintStats(Sfm_Ntk_t *p)
Definition: sfmCore.c:72
static int Vec_IntPop(Vec_Int_t *p)
#define SFM_SAT_SAT
Definition: sfmInt.h:46
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
static int Counter
int nTfoLevMax
Definition: sfm.h:45
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
int Sfm_NodeResub(Sfm_Ntk_t *p, int iNode)
Definition: sfmCore.c:208
int Sfm_NodeResubSolve(Sfm_Ntk_t *p, int iNode, int f, int fRemoveOnly)
Definition: sfmCore.c:108
int fVerbose
Definition: sfm.h:56
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
static int Sfm_NtkPoNum(Sfm_Ntk_t *p)
Definition: sfmInt.h:126
int fVeryVerbose
Definition: sfm.h:57
static int Sfm_ObjFanoutNum(Sfm_Ntk_t *p, int i)
Definition: sfmInt.h:140
void Sfm_NtkPrepare(Sfm_Ntk_t *p)
Definition: sfmNtk.c:195
#define assert(ex)
Definition: util_old.h:213
static int Sfm_ObjIsFixed(Sfm_Ntk_t *p, int i)
Definition: sfmInt.h:132
#define Sfm_NtkForEachNode(p, i)
Definition: sfmInt.h:170
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
ABC_INT64_T abctime
Definition: abc_global.h:278
int nWinSizeMax
Definition: sfm.h:48
#define SFM_SAT_UNDEC
Definition: sfmInt.h:45
static int Vec_WecSizeUsedLimits(Vec_Wec_t *p, int iStart, int iStop)
Definition: vecWec.h:218
static int Sfm_NtkNodeNum(Sfm_Ntk_t *p)
Definition: sfmInt.h:127
int nFanoutMax
Definition: sfm.h:46
static int Vec_WecSizeSize(Vec_Wec_t *p)
Definition: vecWec.h:202
static int Sfm_NtkPiNum(Sfm_Ntk_t *p)
Definition: sfmInt.h:125
int Sfm_ObjMffcSize(Sfm_Ntk_t *p, int iObj)
Definition: sfmWin.c:89