abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
reoShuffle.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [reoShuffle.c]
4 
5  PackageName [REO: A specialized DD reordering engine.]
6 
7  Synopsis [Implementation of the two-variable swap.]
8 
9  Author [Alan Mishchenko]
10 
11  Affiliation [UC Berkeley]
12 
13  Date [Ver. 1.0. Started - October 15, 2002.]
14 
15  Revision [$Id: reoShuffle.c,v 1.0 2002/15/10 03:00:00 alanmi Exp $]
16 
17 ***********************************************************************/
18 
19 #include "reo.h"
20 
22 
23 
24 ////////////////////////////////////////////////////////////////////////
25 /// DECLARATIONS ///
26 ////////////////////////////////////////////////////////////////////////
27 
28 ////////////////////////////////////////////////////////////////////////
29 /// FUNCTION DEFINITIONS ///
30 ////////////////////////////////////////////////////////////////////////
31 
32 /**Function*************************************************************
33 
34  Synopsis [This procedure is similar to Cudd_ShuffleHeap() and Cudd_bddPermute().]
35 
36  Description [The first argument is the REO manager. The 2nd/3d
37  arguments are the function and its CUDD manager. The last argument
38  is the permutation to be implemented. The i-th entry of the permutation
39  array contains the index of the variable that should be brought to the
40  i-th level. The size of the array should be equal or greater to
41  the number of variables currently in use (that is, the size of CUDD
42  manager and the size of REO manager).]
43 
44  SideEffects [Note that the resulting BDD is not referenced.]
45 
46  SeeAlso []
47 
48 ***********************************************************************/
49 DdNode * reoShuffle( reo_man * p, DdManager * dd, DdNode * bFunc, int * pPerm, int * pPermInv )
50 {
51  DdNode * bFuncRes = NULL;
52  int i, k, v;
53 
54  if ( Cudd_IsConstant(bFunc) )
55  return bFunc;
56 
57  // set the initial parameters
58  p->dd = dd;
59  p->nSupp = Cudd_SupportSize( dd, bFunc );
60  p->nTops = 1;
61 // p->nNodesBeg = Cudd_DagSize( bFunc );
62 
63  // set the starting permutation
64  for ( i = 0; i < p->nSupp; i++ )
65  {
66  p->pOrderInt[i] = i;
67  p->pMapToPlanes[ dd->invperm[i] ] = i;
68  p->pMapToDdVarsFinal[i] = dd->invperm[i];
69  }
70 
71  // set the initial parameters
72  p->nUnitsUsed = 0;
73  p->nNodesCur = 0;
74  p->fThisIsAdd = 0;
75  p->Signature++;
76 
77  // transfer the function from the CUDD package into REO's internal data structure
78  p->pTops[0] = reoTransferNodesToUnits_rec( p, bFunc );
79 // assert( p->nNodesBeg == p->nNodesCur );
80 
81  // reorder one variable at a time
82  for ( i = 0; i < p->nSupp; i++ )
83  {
84  if ( p->pOrderInt[i] == pPerm[i] )
85  continue;
86  // find where is variable number pPerm[i]
87  for ( k = i + 1; k < p->nSupp; k++ )
88  if ( pPerm[i] == p->pOrderInt[k] )
89  break;
90  if ( k == p->nSupp )
91  {
92  printf( "reoShuffle() Error: Cannot find a variable.\n" );
93  goto finish;
94  }
95  // move the variable up
96  for ( v = k - 1; v >= i; v-- )
97  {
98  reoReorderSwapAdjacentVars( p, v, 1 );
99  // check if the number of nodes is not too large
100  if ( p->nNodesCur > 10000 )
101  {
102  printf( "reoShuffle() Error: BDD size is too large.\n" );
103  goto finish;
104  }
105  }
106  assert( p->pOrderInt[i] == pPerm[i] );
107  }
108 
109  // set the initial parameters
110  p->nRefNodes = 0;
111  p->nNodesCur = 0;
112  p->Signature++;
113  // transfer the BDDs from REO's internal data structure to CUDD
114  bFuncRes = reoTransferUnitsToNodes_rec( p, p->pTops[0] ); Cudd_Ref( bFuncRes );
115  // undo the DDs referenced for storing in the cache
116  for ( i = 0; i < p->nRefNodes; i++ )
117  Cudd_RecursiveDeref( dd, p->pRefNodes[i] );
118 
119  // verify zero refs of the terminal nodes
120 // assert( reoRecursiveDeref( p->pTops[0] ) );
121 // assert( reoCheckZeroRefs( &(p->pPlanes[p->nSupp]) ) );
122 
123  // perform verification
124  if ( p->fVerify )
125  {
126  DdNode * bFuncPerm;
127  bFuncPerm = Cudd_bddPermute( dd, bFunc, pPermInv ); Cudd_Ref( bFuncPerm );
128  if ( bFuncPerm != bFuncRes )
129  {
130  printf( "REO: Internal verification has failed!\n" );
131  fflush( stdout );
132  }
133  Cudd_RecursiveDeref( dd, bFuncPerm );
134  }
135 
136  // recycle the data structure
137  for ( i = 0; i <= p->nSupp; i++ )
138  reoUnitsRecycleUnitList( p, p->pPlanes + i );
139 
140 finish :
141  if ( bFuncRes )
142  Cudd_Deref( bFuncRes );
143  return bFuncRes;
144 }
145 
146 
147 
148 /**Function*************************************************************
149 
150  Synopsis [Reorders the DD using REO and CUDD.]
151 
152  Description [This function can be used to test the performance of the reordering package.]
153 
154  SideEffects []
155 
156  SeeAlso []
157 
158 ***********************************************************************/
159 void Extra_ShuffleTest( reo_man * pReo, DdManager * dd, DdNode * Func )
160 {
161 // extern int runtime1, runtime2;
162 
163  DdNode * Temp, * bRemap;
164  int nSuppSize, OffSet, Num, i;
165  abctime clk;
166  int pOrder[1000], pOrderInv[1000];
167  assert( dd->size < 1000 );
168 
169  srand( 0x12341234 );
170  nSuppSize = Cudd_SupportSize( dd, Func );
171  if ( nSuppSize < 2 )
172  return;
173 
174  for ( i = 0; i < nSuppSize; i++ )
175  pOrder[i] = i;
176  for ( i = 0; i < 120; i++ )
177  {
178  OffSet = rand() % (nSuppSize - 1);
179  Num = pOrder[OffSet];
180  pOrder[OffSet] = pOrder[OffSet+1];
181  pOrder[OffSet+1] = Num;
182  }
183  for ( i = 0; i < nSuppSize; i++ )
184  pOrderInv[pOrder[i]] = i;
185 
186 /*
187  printf( "Permutation: " );
188  for ( i = 0; i < nSuppSize; i++ )
189  printf( "%d ", pOrder[i] );
190  printf( "\n" );
191  printf( "Inverse permutation: " );
192  for ( i = 0; i < nSuppSize; i++ )
193  printf( "%d ", pOrderInv[i] );
194  printf( "\n" );
195 */
196 
197  // create permutation
198 // Extra_ReorderSetVerification( pReo, 1 );
199  bRemap = Extra_bddRemapUp( dd, Func ); Cudd_Ref( bRemap );
200 
201 clk = Abc_Clock();
202  Temp = reoShuffle( pReo, dd, bRemap, pOrder, pOrderInv ); Cudd_Ref( Temp );
203 //runtime1 += Abc_Clock() - clk;
204 
205 //printf( "Initial = %d. Final = %d.\n", Cudd_DagSize(bRemap), Cudd_DagSize(Temp) );
206 
207  {
208  DdNode * bFuncPerm;
209 clk = Abc_Clock();
210  bFuncPerm = Cudd_bddPermute( dd, bRemap, pOrderInv ); Cudd_Ref( bFuncPerm );
211 //runtime2 += Abc_Clock() - clk;
212  if ( bFuncPerm != Temp )
213  {
214  printf( "REO: Internal verification has failed!\n" );
215  fflush( stdout );
216  }
217  Cudd_RecursiveDeref( dd, bFuncPerm );
218  }
219 
220  Cudd_RecursiveDeref( dd, Temp );
221  Cudd_RecursiveDeref( dd, bRemap );
222 }
223 
224 
225 ////////////////////////////////////////////////////////////////////////
226 /// END OF FILE ///
227 ////////////////////////////////////////////////////////////////////////
228 
230 
void Extra_ShuffleTest(reo_man *pReo, DdManager *dd, DdNode *Func)
Definition: reoShuffle.c:159
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
static Llb_Mgr_t * p
Definition: llb3Image.c:950
void Cudd_Deref(DdNode *node)
Definition: cuddRef.c:438
int size
Definition: cuddInt.h:361
int nTops
Definition: reo.h:145
#define Cudd_IsConstant(node)
Definition: cudd.h:352
DdManager * dd
Definition: reo.h:112
reo_unit * reoTransferNodesToUnits_rec(reo_man *p, DdNode *F)
DECLARATIONS ///.
Definition: reoTransfer.c:43
int * pMapToPlanes
Definition: reo.h:137
int Signature
Definition: reo.h:151
int nNodesCur
Definition: reo.h:127
int nUnitsUsed
Definition: reo.h:164
int fThisIsAdd
Definition: reo.h:116
ABC_NAMESPACE_IMPL_START DdNode * reoShuffle(reo_man *p, DdManager *dd, DdNode *bFunc, int *pPerm, int *pPermInv)
DECLARATIONS ///.
Definition: reoShuffle.c:49
int nSupp
Definition: reo.h:119
static abctime Abc_Clock()
Definition: abc_global.h:279
DdNode * reoTransferUnitsToNodes_rec(reo_man *p, reo_unit *pUnit)
Definition: reoTransfer.c:120
void reoUnitsRecycleUnitList(reo_man *p, reo_plane *pPlane)
Definition: reoUnits.c:87
int nRefNodes
Definition: reo.h:156
int * pMapToDdVarsFinal
Definition: reo.h:139
DdNode ** pRefNodes
Definition: reo.h:155
DdNode * Cudd_bddPermute(DdManager *manager, DdNode *node, int *permut)
Definition: cuddCompose.c:332
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
reo_unit ** pTops
Definition: reo.h:144
DdNode * Extra_bddRemapUp(DdManager *dd, DdNode *bF)
Definition: extraBddMisc.c:145
static int pPerm[13719]
Definition: rwrTemp.c:32
double reoReorderSwapAdjacentVars(reo_man *p, int Level, int fMovingUp)
FUNCTION DEFINITIONS ///.
Definition: reoSwap.c:46
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
reo_plane * pPlanes
Definition: reo.h:142
#define assert(ex)
Definition: util_old.h:213
int fVerify
Definition: reo.h:107
int * invperm
Definition: cuddInt.h:388
void Cudd_Ref(DdNode *n)
Definition: cuddRef.c:129
ABC_INT64_T abctime
Definition: abc_global.h:278
int Cudd_SupportSize(DdManager *dd, DdNode *f)
Definition: cuddUtil.c:857
int * pOrderInt
Definition: reo.h:120
Definition: reo.h:101