abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuddInit.c
Go to the documentation of this file.
1 /**CFile***********************************************************************
2 
3  FileName [cuddInit.c]
4 
5  PackageName [cudd]
6 
7  Synopsis [Functions to initialize and shut down the DD manager.]
8 
9  Description [External procedures included in this module:
10  <ul>
11  <li> Cudd_Init()
12  <li> Cudd_Quit()
13  </ul>
14  Internal procedures included in this module:
15  <ul>
16  <li> cuddZddInitUniv()
17  <li> cuddZddFreeUniv()
18  </ul>
19  ]
20 
21  SeeAlso []
22 
23  Author [Fabio Somenzi]
24 
25  Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
26 
27  All rights reserved.
28 
29  Redistribution and use in source and binary forms, with or without
30  modification, are permitted provided that the following conditions
31  are met:
32 
33  Redistributions of source code must retain the above copyright
34  notice, this list of conditions and the following disclaimer.
35 
36  Redistributions in binary form must reproduce the above copyright
37  notice, this list of conditions and the following disclaimer in the
38  documentation and/or other materials provided with the distribution.
39 
40  Neither the name of the University of Colorado nor the names of its
41  contributors may be used to endorse or promote products derived from
42  this software without specific prior written permission.
43 
44  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
47  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
48  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
49  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
50  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
54  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55  POSSIBILITY OF SUCH DAMAGE.]
56 
57 ******************************************************************************/
58 
59 #include "misc/util/util_hack.h"
60 #include "cuddInt.h"
61 
63 
64 
65 
66 /*---------------------------------------------------------------------------*/
67 /* Constant declarations */
68 /*---------------------------------------------------------------------------*/
69 
70 
71 /*---------------------------------------------------------------------------*/
72 /* Stucture declarations */
73 /*---------------------------------------------------------------------------*/
74 
75 
76 /*---------------------------------------------------------------------------*/
77 /* Type declarations */
78 /*---------------------------------------------------------------------------*/
79 
80 
81 /*---------------------------------------------------------------------------*/
82 /* Variable declarations */
83 /*---------------------------------------------------------------------------*/
84 
85 #ifndef lint
86 static char rcsid[] DD_UNUSED = "$Id: cuddInit.c,v 1.33 2007/07/01 05:10:50 fabio Exp $";
87 #endif
88 
89 /*---------------------------------------------------------------------------*/
90 /* Macro declarations */
91 /*---------------------------------------------------------------------------*/
92 
93 
94 /**AutomaticStart*************************************************************/
95 
96 /*---------------------------------------------------------------------------*/
97 /* Static function prototypes */
98 /*---------------------------------------------------------------------------*/
99 
100 
101 /**AutomaticEnd***************************************************************/
102 
103 
104 /*---------------------------------------------------------------------------*/
105 /* Definition of exported functions */
106 /*---------------------------------------------------------------------------*/
107 
108 /**Function********************************************************************
109 
110  Synopsis [Creates a new DD manager.]
111 
112  Description [Creates a new DD manager, initializes the table, the
113  basic constants and the projection functions. If maxMemory is 0,
114  Cudd_Init decides suitable values for the maximum size of the cache
115  and for the limit for fast unique table growth based on the available
116  memory. Returns a pointer to the manager if successful; NULL
117  otherwise.]
118 
119  SideEffects [None]
120 
121  SeeAlso [Cudd_Quit]
122 
123 ******************************************************************************/
124 DdManager *
126  unsigned int numVars /* initial number of BDD variables (i.e., subtables) */,
127  unsigned int numVarsZ /* initial number of ZDD variables (i.e., subtables) */,
128  unsigned int numSlots /* initial size of the unique tables */,
129  unsigned int cacheSize /* initial size of the cache */,
130  unsigned long maxMemory /* target maximum memory occupation */)
131 {
132  DdManager *unique;
133  int i,result;
134  DdNode *one, *zero;
135  unsigned int maxCacheSize;
136  unsigned int looseUpTo;
137  extern DD_OOMFP MMoutOfMemory;
138  DD_OOMFP saveHandler;
139 
140  if (maxMemory == 0) {
141  maxMemory = getSoftDataLimit();
142  }
143  looseUpTo = (unsigned int) ((maxMemory / sizeof(DdNode)) /
145  unique = cuddInitTable(numVars,numVarsZ,numSlots,looseUpTo);
146  if (unique == NULL) return(NULL);
147  unique->maxmem = (unsigned long) maxMemory / 10 * 9;
148  maxCacheSize = (unsigned int) ((maxMemory / sizeof(DdCache)) /
150  result = cuddInitCache(unique,cacheSize,maxCacheSize);
151  if (result == 0) return(NULL);
152 
153  saveHandler = MMoutOfMemory;
154  MMoutOfMemory = Cudd_OutOfMem;
155  unique->stash = ABC_ALLOC(char,(maxMemory / DD_STASH_FRACTION) + 4);
156  MMoutOfMemory = saveHandler;
157  if (unique->stash == NULL) {
158  (void) fprintf(unique->err,"Unable to set aside memory\n");
159  }
160 
161  /* Initialize constants. */
162  unique->one = cuddUniqueConst(unique,1.0);
163  if (unique->one == NULL) return(0);
164  cuddRef(unique->one);
165  unique->zero = cuddUniqueConst(unique,0.0);
166  if (unique->zero == NULL) return(0);
167  cuddRef(unique->zero);
168 #ifdef HAVE_IEEE_754
169  if (DD_PLUS_INF_VAL != DD_PLUS_INF_VAL * 3 ||
171  (void) fprintf(unique->err,"Warning: Crippled infinite values\n");
172  (void) fprintf(unique->err,"Recompile without -DHAVE_IEEE_754\n");
173  }
174 #endif
175  unique->plusinfinity = cuddUniqueConst(unique,DD_PLUS_INF_VAL);
176  if (unique->plusinfinity == NULL) return(0);
177  cuddRef(unique->plusinfinity);
179  if (unique->minusinfinity == NULL) return(0);
180  cuddRef(unique->minusinfinity);
181  unique->background = unique->zero;
182 
183  /* The logical zero is different from the CUDD_VALUE_TYPE zero! */
184  one = unique->one;
185  zero = Cudd_Not(one);
186  /* Create the projection functions. */
187  unique->vars = ABC_ALLOC(DdNodePtr,unique->maxSize);
188  if (unique->vars == NULL) {
189  unique->errorCode = CUDD_MEMORY_OUT;
190  return(NULL);
191  }
192  for (i = 0; i < unique->size; i++) {
193  unique->vars[i] = cuddUniqueInter(unique,i,one,zero);
194  if (unique->vars[i] == NULL) return(0);
195  cuddRef(unique->vars[i]);
196  }
197 
198  if (unique->sizeZ)
199  cuddZddInitUniv(unique);
200 
201  unique->memused += sizeof(DdNode *) * unique->maxSize;
202 
203  unique->bFunc = NULL;
204  unique->bFunc2 = NULL;
205  unique->TimeStop = 0;
206  return(unique);
207 
208 } /* end of Cudd_Init */
209 
210 
211 /**Function********************************************************************
212 
213  Synopsis [Deletes resources associated with a DD manager.]
214 
215  Description [Deletes resources associated with a DD manager and
216  resets the global statistical counters. (Otherwise, another manaqger
217  subsequently created would inherit the stats of this one.)]
218 
219  SideEffects [None]
220 
221  SeeAlso [Cudd_Init]
222 
223 ******************************************************************************/
224 void
226  DdManager * unique)
227 {
228  if (unique->stash != NULL) ABC_FREE(unique->stash);
229  cuddFreeTable(unique);
230 
231 } /* end of Cudd_Quit */
232 
233 
234 /*---------------------------------------------------------------------------*/
235 /* Definition of internal functions */
236 /*---------------------------------------------------------------------------*/
237 
238 
239 /**Function********************************************************************
240 
241  Synopsis [Initializes the ZDD universe.]
242 
243  Description [Initializes the ZDD universe. Returns 1 if successful; 0
244  otherwise.]
245 
246  SideEffects [None]
247 
248  SeeAlso [cuddZddFreeUniv]
249 
250 ******************************************************************************/
251 int
253  DdManager * zdd)
254 {
255  DdNode *p, *res;
256  int i;
257 
258  zdd->univ = ABC_ALLOC(DdNodePtr, zdd->sizeZ);
259  if (zdd->univ == NULL) {
260  zdd->errorCode = CUDD_MEMORY_OUT;
261  return(0);
262  }
263 
264  res = DD_ONE(zdd);
265  cuddRef(res);
266  for (i = zdd->sizeZ - 1; i >= 0; i--) {
267  unsigned int index = zdd->invpermZ[i];
268  p = res;
269  res = cuddUniqueInterZdd(zdd, index, p, p);
270  if (res == NULL) {
271  Cudd_RecursiveDerefZdd(zdd,p);
272  ABC_FREE(zdd->univ);
273  return(0);
274  }
275  cuddRef(res);
276  cuddDeref(p);
277  zdd->univ[i] = res;
278  }
279 
280 #ifdef DD_VERBOSE
281  cuddZddP(zdd, zdd->univ[0]);
282 #endif
283 
284  return(1);
285 
286 } /* end of cuddZddInitUniv */
287 
288 
289 /**Function********************************************************************
290 
291  Synopsis [Frees the ZDD universe.]
292 
293  Description [Frees the ZDD universe.]
294 
295  SideEffects [None]
296 
297  SeeAlso [cuddZddInitUniv]
298 
299 ******************************************************************************/
300 void
302  DdManager * zdd)
303 {
304  if (zdd->univ) {
305  Cudd_RecursiveDerefZdd(zdd, zdd->univ[0]);
306  ABC_FREE(zdd->univ);
307  }
308 
309 } /* end of cuddZddFreeUniv */
310 
311 
312 /*---------------------------------------------------------------------------*/
313 /* Definition of static functions */
314 /*---------------------------------------------------------------------------*/
315 
316 
318 
319 
#define cuddRef(n)
Definition: cuddInt.h:584
void Cudd_RecursiveDerefZdd(DdManager *table, DdNode *n)
Definition: cuddRef.c:385
void(* DD_OOMFP)(long)
Definition: cudd.h:324
int cuddZddInitUniv(DdManager *zdd)
Definition: cuddInit.c:252
#define cuddDeref(n)
Definition: cuddInt.h:604
void Cudd_OutOfMem(long size)
Definition: cuddUtil.c:2837
abctime TimeStop
Definition: cuddInt.h:489
Definition: cudd.h:278
#define Cudd_Not(node)
Definition: cudd.h:367
#define DD_MAX_CACHE_FRACTION
Definition: cuddInt.h:140
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int * invpermZ
Definition: cuddInt.h:389
void Cudd_Quit(DdManager *unique)
Definition: cuddInit.c:225
DdNode * cuddUniqueConst(DdManager *unique, CUDD_VALUE_TYPE value)
Definition: cuddTable.c:1450
int size
Definition: cuddInt.h:361
#define DD_STASH_FRACTION
Definition: cuddInt.h:142
DdNode * zero
Definition: cuddInt.h:346
#define DD_MAX_LOOSE_FRACTION
Definition: cuddInt.h:138
FILE * err
Definition: cuddInt.h:442
DdNode * bFunc
Definition: cuddInt.h:487
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define DD_PLUS_INF_VAL
Definition: cuddInt.h:117
int cuddInitCache(DdManager *unique, unsigned int cacheSize, unsigned int maxCacheSize)
Definition: cuddCache.c:136
DdManager * Cudd_Init(unsigned int numVars, unsigned int numVarsZ, unsigned int numSlots, unsigned int cacheSize, unsigned long maxMemory)
Definition: cuddInit.c:125
EXTERN long getSoftDataLimit()
static ABC_NAMESPACE_IMPL_START char rcsid[] DD_UNUSED
Definition: cuddInit.c:86
char * stash
Definition: cuddInt.h:399
static DdNode * one
Definition: cuddDecomp.c:112
int maxSize
Definition: cuddInt.h:363
void cuddZddFreeUniv(DdManager *zdd)
Definition: cuddInit.c:301
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
DdNode * bFunc2
Definition: cuddInt.h:488
DdNode * cuddUniqueInterZdd(DdManager *unique, int index, DdNode *T, DdNode *E)
Definition: cuddTable.c:1343
DdManager * cuddInitTable(unsigned int numVars, unsigned int numVarsZ, unsigned int numSlots, unsigned int looseUpTo)
Definition: cuddTable.c:351
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
unsigned long memused
Definition: cuddInt.h:449
int sizeZ
Definition: cuddInt.h:362
#define ABC_FREE(obj)
Definition: abc_global.h:232
DdNode ** vars
Definition: cuddInt.h:390
DdNode * one
Definition: cuddInt.h:345
DdNode * plusinfinity
Definition: cuddInt.h:347
static int result
Definition: cuddGenetic.c:125
unsigned long maxmem
Definition: cuddInt.h:450
DdNode * minusinfinity
Definition: cuddInt.h:348
void cuddFreeTable(DdManager *unique)
Definition: cuddTable.c:659
#define DD_ONE(dd)
Definition: cuddInt.h:911
#define DD_MINUS_INF_VAL
Definition: cuddInt.h:121
Cudd_ErrorType errorCode
Definition: cuddInt.h:447
DdNode * cuddUniqueInter(DdManager *unique, int index, DdNode *T, DdNode *E)
Definition: cuddTable.c:1128
static DdNode * zero
Definition: cuddApa.c:100
int cuddZddP(DdManager *zdd, DdNode *f)
Definition: cuddZddUtil.c:822
DdNode * background
Definition: cuddInt.h:349
#define MMoutOfMemory
Definition: util_hack.h:38
DdNode ** univ
Definition: cuddInt.h:392