You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.9 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddInit.c]
  3. PackageName [cudd]
  4. Synopsis [Functions to initialize and shut down the DD manager.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_Init()
  8. <li> Cudd_Quit()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. <li> cuddZddInitUniv()
  13. <li> cuddZddFreeUniv()
  14. </ul>
  15. ]
  16. SeeAlso []
  17. Author [Fabio Somenzi]
  18. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  19. All rights reserved.
  20. Redistribution and use in source and binary forms, with or without
  21. modification, are permitted provided that the following conditions
  22. are met:
  23. Redistributions of source code must retain the above copyright
  24. notice, this list of conditions and the following disclaimer.
  25. Redistributions in binary form must reproduce the above copyright
  26. notice, this list of conditions and the following disclaimer in the
  27. documentation and/or other materials provided with the distribution.
  28. Neither the name of the University of Colorado nor the names of its
  29. contributors may be used to endorse or promote products derived from
  30. this software without specific prior written permission.
  31. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  37. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  41. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. POSSIBILITY OF SUCH DAMAGE.]
  43. ******************************************************************************/
  44. #include "util.h"
  45. #include "cuddInt.h"
  46. /*---------------------------------------------------------------------------*/
  47. /* Constant declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Stucture declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Type declarations */
  54. /*---------------------------------------------------------------------------*/
  55. /*---------------------------------------------------------------------------*/
  56. /* Variable declarations */
  57. /*---------------------------------------------------------------------------*/
  58. #ifndef lint
  59. static char rcsid[] DD_UNUSED = "$Id: cuddInit.c,v 1.34 2012/02/05 01:07:19 fabio Exp $";
  60. #endif
  61. /*---------------------------------------------------------------------------*/
  62. /* Macro declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /**AutomaticStart*************************************************************/
  65. /*---------------------------------------------------------------------------*/
  66. /* Static function prototypes */
  67. /*---------------------------------------------------------------------------*/
  68. /**AutomaticEnd***************************************************************/
  69. /*---------------------------------------------------------------------------*/
  70. /* Definition of exported functions */
  71. /*---------------------------------------------------------------------------*/
  72. /**Function********************************************************************
  73. Synopsis [Creates a new DD manager.]
  74. Description [Creates a new DD manager, initializes the table, the
  75. basic constants and the projection functions. If maxMemory is 0,
  76. Cudd_Init decides suitable values for the maximum size of the cache
  77. and for the limit for fast unique table growth based on the available
  78. memory. Returns a pointer to the manager if successful; NULL
  79. otherwise.]
  80. SideEffects [None]
  81. SeeAlso [Cudd_Quit]
  82. ******************************************************************************/
  83. DdManager *
  84. Cudd_Init(
  85. unsigned int numVars /* initial number of BDD variables (i.e., subtables) */,
  86. unsigned int numVarsZ /* initial number of ZDD variables (i.e., subtables) */,
  87. unsigned int numSlots /* initial size of the unique tables */,
  88. unsigned int cacheSize /* initial size of the cache */,
  89. unsigned long maxMemory /* target maximum memory occupation */)
  90. {
  91. DdManager *unique;
  92. int i,result;
  93. DdNode *one, *zero;
  94. unsigned int maxCacheSize;
  95. unsigned int looseUpTo;
  96. extern DD_OOMFP MMoutOfMemory;
  97. DD_OOMFP saveHandler;
  98. if (maxMemory == 0) {
  99. maxMemory = getSoftDataLimit();
  100. }
  101. looseUpTo = (unsigned int) ((maxMemory / sizeof(DdNode)) /
  102. DD_MAX_LOOSE_FRACTION);
  103. unique = cuddInitTable(numVars,numVarsZ,numSlots,looseUpTo);
  104. if (unique == NULL) return(NULL);
  105. unique->maxmem = (unsigned long) maxMemory / 10 * 9;
  106. maxCacheSize = (unsigned int) ((maxMemory / sizeof(DdCache)) /
  107. DD_MAX_CACHE_FRACTION);
  108. result = cuddInitCache(unique,cacheSize,maxCacheSize);
  109. if (result == 0) return(NULL);
  110. saveHandler = MMoutOfMemory;
  111. MMoutOfMemory = Cudd_OutOfMem;
  112. unique->stash = ALLOC(char,(maxMemory / DD_STASH_FRACTION) + 4);
  113. MMoutOfMemory = saveHandler;
  114. if (unique->stash == NULL) {
  115. (void) fprintf(unique->err,"Unable to set aside memory\n");
  116. }
  117. /* Initialize constants. */
  118. unique->one = cuddUniqueConst(unique,1.0);
  119. if (unique->one == NULL) return(0);
  120. cuddRef(unique->one);
  121. unique->zero = cuddUniqueConst(unique,0.0);
  122. if (unique->zero == NULL) return(0);
  123. cuddRef(unique->zero);
  124. #ifdef HAVE_IEEE_754
  125. if (DD_PLUS_INF_VAL != DD_PLUS_INF_VAL * 3 ||
  126. DD_PLUS_INF_VAL != DD_PLUS_INF_VAL / 3) {
  127. (void) fprintf(unique->err,"Warning: Crippled infinite values\n");
  128. (void) fprintf(unique->err,"Recompile without -DHAVE_IEEE_754\n");
  129. }
  130. #endif
  131. unique->plusinfinity = cuddUniqueConst(unique,DD_PLUS_INF_VAL);
  132. if (unique->plusinfinity == NULL) return(0);
  133. cuddRef(unique->plusinfinity);
  134. unique->minusinfinity = cuddUniqueConst(unique,DD_MINUS_INF_VAL);
  135. if (unique->minusinfinity == NULL) return(0);
  136. cuddRef(unique->minusinfinity);
  137. unique->background = unique->zero;
  138. /* The logical zero is different from the CUDD_VALUE_TYPE zero! */
  139. one = unique->one;
  140. zero = Cudd_Not(one);
  141. /* Create the projection functions. */
  142. unique->vars = ALLOC(DdNodePtr,unique->maxSize);
  143. if (unique->vars == NULL) {
  144. unique->errorCode = CUDD_MEMORY_OUT;
  145. return(NULL);
  146. }
  147. for (i = 0; i < unique->size; i++) {
  148. unique->vars[i] = cuddUniqueInter(unique,i,one,zero);
  149. if (unique->vars[i] == NULL) return(0);
  150. cuddRef(unique->vars[i]);
  151. }
  152. if (unique->sizeZ)
  153. cuddZddInitUniv(unique);
  154. unique->memused += sizeof(DdNode *) * unique->maxSize;
  155. return(unique);
  156. } /* end of Cudd_Init */
  157. /**Function********************************************************************
  158. Synopsis [Deletes resources associated with a DD manager.]
  159. Description [Deletes resources associated with a DD manager and
  160. resets the global statistical counters. (Otherwise, another manaqger
  161. subsequently created would inherit the stats of this one.)]
  162. SideEffects [None]
  163. SeeAlso [Cudd_Init]
  164. ******************************************************************************/
  165. void
  166. Cudd_Quit(
  167. DdManager * unique)
  168. {
  169. if (unique->stash != NULL) FREE(unique->stash);
  170. cuddFreeTable(unique);
  171. } /* end of Cudd_Quit */
  172. /*---------------------------------------------------------------------------*/
  173. /* Definition of internal functions */
  174. /*---------------------------------------------------------------------------*/
  175. /**Function********************************************************************
  176. Synopsis [Initializes the ZDD universe.]
  177. Description [Initializes the ZDD universe. Returns 1 if successful; 0
  178. otherwise.]
  179. SideEffects [None]
  180. SeeAlso [cuddZddFreeUniv]
  181. ******************************************************************************/
  182. int
  183. cuddZddInitUniv(
  184. DdManager * zdd)
  185. {
  186. DdNode *p, *res;
  187. int i;
  188. zdd->univ = ALLOC(DdNodePtr, zdd->sizeZ);
  189. if (zdd->univ == NULL) {
  190. zdd->errorCode = CUDD_MEMORY_OUT;
  191. return(0);
  192. }
  193. res = DD_ONE(zdd);
  194. cuddRef(res);
  195. for (i = zdd->sizeZ - 1; i >= 0; i--) {
  196. unsigned int index = zdd->invpermZ[i];
  197. p = res;
  198. res = cuddUniqueInterZdd(zdd, index, p, p);
  199. if (res == NULL) {
  200. Cudd_RecursiveDerefZdd(zdd,p);
  201. FREE(zdd->univ);
  202. return(0);
  203. }
  204. cuddRef(res);
  205. cuddDeref(p);
  206. zdd->univ[i] = res;
  207. }
  208. #ifdef DD_VERBOSE
  209. cuddZddP(zdd, zdd->univ[0]);
  210. #endif
  211. return(1);
  212. } /* end of cuddZddInitUniv */
  213. /**Function********************************************************************
  214. Synopsis [Frees the ZDD universe.]
  215. Description [Frees the ZDD universe.]
  216. SideEffects [None]
  217. SeeAlso [cuddZddInitUniv]
  218. ******************************************************************************/
  219. void
  220. cuddZddFreeUniv(
  221. DdManager * zdd)
  222. {
  223. if (zdd->univ) {
  224. Cudd_RecursiveDerefZdd(zdd, zdd->univ[0]);
  225. FREE(zdd->univ);
  226. }
  227. } /* end of cuddZddFreeUniv */
  228. /*---------------------------------------------------------------------------*/
  229. /* Definition of static functions */
  230. /*---------------------------------------------------------------------------*/