The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

281 lines
8.4 KiB

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