abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cnfMap.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [cnfMap.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [AIG-to-CNF conversion.]
8 
9  Synopsis []
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - April 28, 2007.]
16 
17  Revision [$Id: cnfMap.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "cnf.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Computes area flow of the cut.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
45 void Cnf_CutAssignAreaFlow( Cnf_Man_t * p, Dar_Cut_t * pCut, int * pAreaFlows )
46 {
47  Aig_Obj_t * pLeaf;
48  int i;
49  pCut->Value = 0;
50 // pCut->uSign = 100 * Cnf_CutSopCost( p, pCut );
51  pCut->uSign = 10 * Cnf_CutSopCost( p, pCut );
52  Dar_CutForEachLeaf( p->pManAig, pCut, pLeaf, i )
53  {
54  pCut->Value += pLeaf->nRefs;
55  if ( !Aig_ObjIsNode(pLeaf) )
56  continue;
57  assert( pLeaf->nRefs > 0 );
58  pCut->uSign += pAreaFlows[pLeaf->Id] / (pLeaf->nRefs? pLeaf->nRefs : 1);
59  }
60 }
61 
62 /**Function*************************************************************
63 
64  Synopsis [Computes area flow of the supergate.]
65 
66  Description []
67 
68  SideEffects []
69 
70  SeeAlso []
71 
72 ***********************************************************************/
73 int Cnf_CutSuperAreaFlow( Vec_Ptr_t * vSuper, int * pAreaFlows )
74 {
75  Aig_Obj_t * pLeaf;
76  int i, nAreaFlow;
77  nAreaFlow = 100 * (Vec_PtrSize(vSuper) + 1);
78  Vec_PtrForEachEntry( Aig_Obj_t *, vSuper, pLeaf, i )
79  {
80  pLeaf = Aig_Regular(pLeaf);
81  if ( !Aig_ObjIsNode(pLeaf) )
82  continue;
83  assert( pLeaf->nRefs > 0 );
84  nAreaFlow += pAreaFlows[pLeaf->Id] / (pLeaf->nRefs? pLeaf->nRefs : 1);
85  }
86  return nAreaFlow;
87 }
88 
89 /**Function*************************************************************
90 
91  Synopsis []
92 
93  Description []
94 
95  SideEffects []
96 
97  SeeAlso []
98 
99 ***********************************************************************/
101 {
102  Vec_Ptr_t * vSuper;
103  Aig_Obj_t * pObj;
104  Dar_Cut_t * pCut, * pCutBest;
105  int i, k, AreaFlow, * pAreaFlows;
106  // allocate area flows
107  pAreaFlows = ABC_ALLOC( int, Aig_ManObjNumMax(p->pManAig) );
108  memset( pAreaFlows, 0, sizeof(int) * Aig_ManObjNumMax(p->pManAig) );
109  // visit the nodes in the topological order and update their best cuts
110  vSuper = Vec_PtrAlloc( 100 );
111  Aig_ManForEachNode( p->pManAig, pObj, i )
112  {
113  // go through the cuts
114  pCutBest = NULL;
115  Dar_ObjForEachCut( pObj, pCut, k )
116  {
117  pCut->fBest = 0;
118  if ( k == 0 )
119  continue;
120  Cnf_CutAssignAreaFlow( p, pCut, pAreaFlows );
121  if ( pCutBest == NULL || pCutBest->uSign > pCut->uSign ||
122  (pCutBest->uSign == pCut->uSign && pCutBest->Value < pCut->Value) )
123  pCutBest = pCut;
124  }
125  // check the big cut
126 // Aig_ObjCollectSuper( pObj, vSuper );
127  // get the area flow of this cut
128 // AreaFlow = Cnf_CutSuperAreaFlow( vSuper, pAreaFlows );
129  AreaFlow = ABC_INFINITY;
130  if ( AreaFlow >= (int)pCutBest->uSign )
131  {
132  pAreaFlows[pObj->Id] = pCutBest->uSign;
133  pCutBest->fBest = 1;
134  }
135  else
136  {
137  pAreaFlows[pObj->Id] = AreaFlow;
138  pObj->fMarkB = 1; // mark the special node
139  }
140  }
141  Vec_PtrFree( vSuper );
142  ABC_FREE( pAreaFlows );
143 
144 /*
145  // compute the area of mapping
146  AreaFlow = 0;
147  Aig_ManForEachCo( p->pManAig, pObj, i )
148  AreaFlow += Dar_ObjBestCut(Aig_ObjFanin0(pObj))->uSign / 100 / Aig_ObjFanin0(pObj)->nRefs;
149  printf( "Area of the network = %d.\n", AreaFlow );
150 */
151 }
152 
153 
154 
155 #if 0
156 
157 /**Function*************************************************************
158 
159  Synopsis [Computes area of the first level.]
160 
161  Description [The cut need to be derefed.]
162 
163  SideEffects []
164 
165  SeeAlso []
166 
167 ***********************************************************************/
168 void Aig_CutDeref( Aig_Man_t * p, Dar_Cut_t * pCut )
169 {
170  Aig_Obj_t * pLeaf;
171  int i;
172  Dar_CutForEachLeaf( p, pCut, pLeaf, i )
173  {
174  assert( pLeaf->nRefs > 0 );
175  if ( --pLeaf->nRefs > 0 || !Aig_ObjIsAnd(pLeaf) )
176  continue;
177  Aig_CutDeref( p, Aig_ObjBestCut(pLeaf) );
178  }
179 }
180 
181 /**Function*************************************************************
182 
183  Synopsis [Computes area of the first level.]
184 
185  Description [The cut need to be derefed.]
186 
187  SideEffects []
188 
189  SeeAlso []
190 
191 ***********************************************************************/
192 int Aig_CutRef( Aig_Man_t * p, Dar_Cut_t * pCut )
193 {
194  Aig_Obj_t * pLeaf;
195  int i, Area = pCut->Value;
196  Dar_CutForEachLeaf( p, pCut, pLeaf, i )
197  {
198  assert( pLeaf->nRefs >= 0 );
199  if ( pLeaf->nRefs++ > 0 || !Aig_ObjIsAnd(pLeaf) )
200  continue;
201  Area += Aig_CutRef( p, Aig_ObjBestCut(pLeaf) );
202  }
203  return Area;
204 }
205 
206 /**Function*************************************************************
207 
208  Synopsis [Computes exact area of the cut.]
209 
210  Description []
211 
212  SideEffects []
213 
214  SeeAlso []
215 
216 ***********************************************************************/
217 int Cnf_CutArea( Aig_Man_t * p, Dar_Cut_t * pCut )
218 {
219  int Area;
220  Area = Aig_CutRef( p, pCut );
221  Aig_CutDeref( p, pCut );
222  return Area;
223 }
224 
225 /**Function*************************************************************
226 
227  Synopsis [Returns 1 if the second cut is better.]
228 
229  Description []
230 
231  SideEffects []
232 
233  SeeAlso []
234 
235 ***********************************************************************/
236 static inline int Cnf_CutCompare( Dar_Cut_t * pC0, Dar_Cut_t * pC1 )
237 {
238  if ( pC0->Area < pC1->Area - 0.0001 )
239  return -1;
240  if ( pC0->Area > pC1->Area + 0.0001 ) // smaller area flow is better
241  return 1;
242 // if ( pC0->NoRefs < pC1->NoRefs )
243 // return -1;
244 // if ( pC0->NoRefs > pC1->NoRefs ) // fewer non-referenced fanins is better
245 // return 1;
246 // if ( pC0->FanRefs / pC0->nLeaves > pC1->FanRefs / pC1->nLeaves )
247 // return -1;
248 // if ( pC0->FanRefs / pC0->nLeaves < pC1->FanRefs / pC1->nLeaves )
249 // return 1; // larger average fanin ref-counter is better
250 // return 0;
251  return 1;
252 }
253 
254 /**Function*************************************************************
255 
256  Synopsis [Returns the cut with the smallest area flow.]
257 
258  Description []
259 
260  SideEffects []
261 
262  SeeAlso []
263 
264 ***********************************************************************/
265 Dar_Cut_t * Cnf_ObjFindBestCut( Aig_Obj_t * pObj )
266 {
267  Dar_Cut_t * pCut, * pCutBest;
268  int i;
269  pCutBest = NULL;
270  Dar_ObjForEachCut( pObj, pCut, i )
271  if ( pCutBest == NULL || Cnf_CutCompare(pCutBest, pCut) == 1 )
272  pCutBest = pCut;
273  return pCutBest;
274 }
275 
276 /**Function*************************************************************
277 
278  Synopsis [Computes area flow of the cut.]
279 
280  Description []
281 
282  SideEffects []
283 
284  SeeAlso []
285 
286 ***********************************************************************/
287 void Cnf_CutAssignArea( Cnf_Man_t * p, Dar_Cut_t * pCut )
288 {
289  Aig_Obj_t * pLeaf;
290  int i;
291  pCut->Area = (float)pCut->Cost;
292  pCut->NoRefs = 0;
293  pCut->FanRefs = 0;
294  Dar_CutForEachLeaf( p->pManAig, pCut, pLeaf, i )
295  {
296  if ( !Aig_ObjIsNode(pLeaf) )
297  continue;
298  if ( pLeaf->nRefs == 0 )
299  {
300  pCut->Area += Aig_ObjBestCut(pLeaf)->Cost;
301  pCut->NoRefs++;
302  }
303  else
304  {
305  if ( pCut->FanRefs + pLeaf->nRefs > 15 )
306  pCut->FanRefs = 15;
307  else
308  pCut->FanRefs += pLeaf->nRefs;
309  }
310  }
311 }
312 
313 /**Function*************************************************************
314 
315  Synopsis [Performs one round of "area recovery" using exact local area.]
316 
317  Description []
318 
319  SideEffects []
320 
321  SeeAlso []
322 
323 ***********************************************************************/
324 int Cnf_ManMapForCnf( Cnf_Man_t * p )
325 {
326  Aig_Obj_t * pObj;
327  Dar_Cut_t * pCut, * pCutBest;
328  int i, k;
329  // visit the nodes in the topological order and update their best cuts
330  Aig_ManForEachNode( p->pManAig, pObj, i )
331  {
332  // find the old best cut
333  pCutBest = Aig_ObjBestCut(pObj);
334  Dar_ObjClearBestCut(pCutBest);
335  // if the node is used, dereference its cut
336  if ( pObj->nRefs )
337  Aig_CutDeref( p->pManAig, pCutBest );
338 
339  // evaluate the cuts of this node
340  Dar_ObjForEachCut( pObj, pCut, k )
341 // Cnf_CutAssignAreaFlow( p, pCut );
342  pCut->Area = (float)Cnf_CutArea( p->pManAig, pCut );
343 
344  // find the new best cut
345  pCutBest = Cnf_ObjFindBestCut(pObj);
346  Dar_ObjSetBestCut( pCutBest );
347  // if the node is used, reference its cut
348  if ( pObj->nRefs )
349  Aig_CutRef( p->pManAig, pCutBest );
350  }
351  return 1;
352 }
353 
354 #endif
355 
356 ////////////////////////////////////////////////////////////////////////
357 /// END OF FILE ///
358 ////////////////////////////////////////////////////////////////////////
359 
360 
362 
char * memset()
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
Definition: aig.h:50
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static int Cnf_CutSopCost(Cnf_Man_t *p, Dar_Cut_t *pCut)
Definition: cnf.h:99
unsigned int fMarkB
Definition: aig.h:80
unsigned fBest
Definition: darInt.h:60
static Aig_Obj_t * Aig_Regular(Aig_Obj_t *p)
Definition: aig.h:246
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
#define Dar_ObjForEachCut(pObj, pCut, i)
Definition: darInt.h:121
static int Aig_ObjIsNode(Aig_Obj_t *pObj)
Definition: aig.h:280
#define Aig_ManForEachNode(p, pObj, i)
Definition: aig.h:413
int Cnf_CutSuperAreaFlow(Vec_Ptr_t *vSuper, int *pAreaFlows)
Definition: cnfMap.c:73
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
if(last==0)
Definition: sparse_int.h:34
int Cnf_ManMapForCnf(Cnf_Man_t *p)
ABC_NAMESPACE_IMPL_START void Cnf_CutAssignAreaFlow(Cnf_Man_t *p, Dar_Cut_t *pCut, int *pAreaFlows)
DECLARATIONS ///.
Definition: cnfMap.c:45
Definition: aig.h:69
static int Aig_ManObjNumMax(Aig_Man_t *p)
Definition: aig.h:259
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define Dar_CutForEachLeaf(p, pCut, pLeaf, i)
Definition: darInt.h:124
void Cnf_DeriveMapping(Cnf_Man_t *p)
Definition: cnfMap.c:100
#define ABC_INFINITY
MACRO DEFINITIONS ///.
Definition: abc_global.h:216
unsigned uSign
Definition: darInt.h:57
#define assert(ex)
Definition: util_old.h:213
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
typedefABC_NAMESPACE_HEADER_START struct Cnf_Man_t_ Cnf_Man_t
INCLUDES ///.
Definition: cnf.h:51
unsigned Value
Definition: darInt.h:59
int Id
Definition: aig.h:85
unsigned int nRefs
Definition: aig.h:81
static int Aig_ObjIsAnd(Aig_Obj_t *pObj)
Definition: aig.h:278
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223