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.

756 lines
17 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions that manipulate the reference counts.
  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 Increases the reference count of a node, if it is not
  61. saturated.
  62. @sideeffect None
  63. @see Cudd_RecursiveDeref Cudd_Deref
  64. */
  65. void
  66. Cudd_Ref(
  67. DdNode * n)
  68. {
  69. n = Cudd_Regular(n);
  70. cuddSatInc(n->ref);
  71. } /* end of Cudd_Ref */
  72. /**
  73. @brief Decreases the reference count of node n.
  74. @details If n dies, recursively decreases the reference counts of
  75. its children. It is used to dispose of a DD that is no longer
  76. needed.
  77. @sideeffect None
  78. @see Cudd_Deref Cudd_Ref Cudd_RecursiveDerefZdd
  79. */
  80. void
  81. Cudd_RecursiveDeref(
  82. DdManager * table,
  83. DdNode * n)
  84. {
  85. DdNode *N;
  86. int ord;
  87. DdNodePtr *stack = table->stack;
  88. int SP = 1;
  89. unsigned int live = table->keys - table->dead;
  90. if (live > table->peakLiveNodes) {
  91. table->peakLiveNodes = live;
  92. }
  93. N = Cudd_Regular(n);
  94. do {
  95. #ifdef DD_DEBUG
  96. assert(N->ref != 0);
  97. #endif
  98. if (N->ref == 1) {
  99. N->ref = 0;
  100. table->dead++;
  101. #ifdef DD_STATS
  102. table->nodesDropped++;
  103. #endif
  104. if (cuddIsConstant(N)) {
  105. table->constants.dead++;
  106. N = stack[--SP];
  107. } else {
  108. ord = table->perm[N->index];
  109. stack[SP++] = Cudd_Regular(cuddE(N));
  110. table->subtables[ord].dead++;
  111. N = cuddT(N);
  112. }
  113. } else {
  114. cuddSatDec(N->ref);
  115. N = stack[--SP];
  116. }
  117. } while (SP != 0);
  118. } /* end of Cudd_RecursiveDeref */
  119. /**
  120. @brief Decreases the reference count of %BDD node n.
  121. @details If n dies, recursively decreases the reference counts of
  122. its children. It is used to dispose of a %BDD that is no longer
  123. needed. It is more efficient than Cudd_RecursiveDeref, but it cannot
  124. be used on ADDs. The greater efficiency comes from being able to
  125. assume that no constant node will ever die as a result of a call to
  126. this procedure.
  127. @sideeffect None
  128. @see Cudd_RecursiveDeref Cudd_DelayedDerefBdd
  129. */
  130. void
  131. Cudd_IterDerefBdd(
  132. DdManager * table,
  133. DdNode * n)
  134. {
  135. DdNode *N;
  136. int ord;
  137. DdNodePtr *stack = table->stack;
  138. int SP = 1;
  139. unsigned int live = table->keys - table->dead;
  140. if (live > table->peakLiveNodes) {
  141. table->peakLiveNodes = live;
  142. }
  143. N = Cudd_Regular(n);
  144. do {
  145. #ifdef DD_DEBUG
  146. assert(N->ref != 0);
  147. #endif
  148. if (N->ref == 1) {
  149. N->ref = 0;
  150. table->dead++;
  151. #ifdef DD_STATS
  152. table->nodesDropped++;
  153. #endif
  154. ord = table->perm[N->index];
  155. stack[SP++] = Cudd_Regular(cuddE(N));
  156. table->subtables[ord].dead++;
  157. N = cuddT(N);
  158. } else {
  159. cuddSatDec(N->ref);
  160. N = stack[--SP];
  161. }
  162. } while (SP != 0);
  163. } /* end of Cudd_IterDerefBdd */
  164. /**
  165. @brief Decreases the reference count of %BDD node n.
  166. @details Enqueues node n for later dereferencing. If the queue
  167. is full decreases the reference count of the oldest node N to make
  168. room for n. If N dies, recursively decreases the reference counts of
  169. its children. It is used to dispose of a %BDD that is currently not
  170. needed, but may be useful again in the near future. The dereferencing
  171. proper is done as in Cudd_IterDerefBdd.
  172. @sideeffect None
  173. @see Cudd_RecursiveDeref Cudd_IterDerefBdd
  174. */
  175. void
  176. Cudd_DelayedDerefBdd(
  177. DdManager * table,
  178. DdNode * n)
  179. {
  180. DdNode *N;
  181. int ord;
  182. DdNodePtr *stack;
  183. int SP;
  184. unsigned int live = table->keys - table->dead;
  185. if (live > table->peakLiveNodes) {
  186. table->peakLiveNodes = live;
  187. }
  188. n = Cudd_Regular(n);
  189. #ifdef DD_DEBUG
  190. assert(n->ref != 0);
  191. #endif
  192. #ifdef DD_NO_DEATH_ROW
  193. N = n;
  194. #else
  195. if (cuddIsConstant(n) || n->ref > 1) {
  196. #ifdef DD_DEBUG
  197. assert(n->ref != 1 && (!cuddIsConstant(n) || n == DD_ONE(table)));
  198. #endif
  199. cuddSatDec(n->ref);
  200. return;
  201. }
  202. N = table->deathRow[table->nextDead];
  203. if (N != NULL) {
  204. #endif
  205. #ifdef DD_DEBUG
  206. assert(!Cudd_IsComplement(N));
  207. #endif
  208. stack = table->stack;
  209. SP = 1;
  210. do {
  211. #ifdef DD_DEBUG
  212. assert(N->ref != 0);
  213. #endif
  214. if (N->ref == 1) {
  215. N->ref = 0;
  216. table->dead++;
  217. #ifdef DD_STATS
  218. table->nodesDropped++;
  219. #endif
  220. ord = table->perm[N->index];
  221. stack[SP++] = Cudd_Regular(cuddE(N));
  222. table->subtables[ord].dead++;
  223. N = cuddT(N);
  224. } else {
  225. cuddSatDec(N->ref);
  226. N = stack[--SP];
  227. }
  228. } while (SP != 0);
  229. #ifndef DD_NO_DEATH_ROW
  230. }
  231. table->deathRow[table->nextDead] = n;
  232. /* Udate insertion point. */
  233. table->nextDead++;
  234. table->nextDead &= table->deadMask;
  235. #if 0
  236. if (table->nextDead == table->deathRowDepth) {
  237. if (table->deathRowDepth < table->looseUpTo / 2) {
  238. extern void (*MMoutOfMemory)(size_t);
  239. void (*saveHandler)(size_t) = MMoutOfMemory;
  240. DdNodePtr *newRow;
  241. MMoutOfMemory = table->outOfMemCallback;
  242. newRow = REALLOC(DdNodePtr,table->deathRow,2*table->deathRowDepth);
  243. MMoutOfMemory = saveHandler;
  244. if (newRow == NULL) {
  245. table->nextDead = 0;
  246. } else {
  247. int i;
  248. table->memused += table->deathRowDepth;
  249. i = table->deathRowDepth;
  250. table->deathRowDepth <<= 1;
  251. for (; i < table->deathRowDepth; i++) {
  252. newRow[i] = NULL;
  253. }
  254. table->deadMask = table->deathRowDepth - 1;
  255. table->deathRow = newRow;
  256. }
  257. } else {
  258. table->nextDead = 0;
  259. }
  260. }
  261. #endif
  262. #endif
  263. } /* end of Cudd_DelayedDerefBdd */
  264. /**
  265. @brief Decreases the reference count of %ZDD node n.
  266. @details If n dies, recursively decreases the reference counts of
  267. its children. It is used to dispose of a %ZDD that is no longer
  268. needed.
  269. @sideeffect None
  270. @see Cudd_Deref Cudd_Ref Cudd_RecursiveDeref
  271. */
  272. void
  273. Cudd_RecursiveDerefZdd(
  274. DdManager * table,
  275. DdNode * n)
  276. {
  277. DdNode *N;
  278. int ord;
  279. DdNodePtr *stack = table->stack;
  280. int SP = 1;
  281. N = n;
  282. do {
  283. #ifdef DD_DEBUG
  284. assert(N->ref != 0);
  285. #endif
  286. cuddSatDec(N->ref);
  287. if (N->ref == 0) {
  288. table->deadZ++;
  289. #ifdef DD_STATS
  290. table->nodesDropped++;
  291. #endif
  292. #ifdef DD_DEBUG
  293. assert(!cuddIsConstant(N));
  294. #endif
  295. ord = table->permZ[N->index];
  296. stack[SP++] = cuddE(N);
  297. table->subtableZ[ord].dead++;
  298. N = cuddT(N);
  299. } else {
  300. N = stack[--SP];
  301. }
  302. } while (SP != 0);
  303. } /* end of Cudd_RecursiveDerefZdd */
  304. /**
  305. @brief Decreases the reference count of node.
  306. @details It is primarily used in recursive procedures to decrease
  307. the ref count of a result node before returning it. This
  308. accomplishes the goal of removing the protection applied by a
  309. previous Cudd_Ref.
  310. @sideeffect None
  311. @see Cudd_RecursiveDeref Cudd_RecursiveDerefZdd Cudd_Ref
  312. */
  313. void
  314. Cudd_Deref(
  315. DdNode * node)
  316. {
  317. node = Cudd_Regular(node);
  318. cuddSatDec(node->ref);
  319. } /* end of Cudd_Deref */
  320. /**
  321. @brief Checks the unique table for nodes with non-zero reference
  322. counts.
  323. @details It is normally called before Cudd_Quit to make sure that
  324. there are no memory leaks due to missing Cudd_RecursiveDeref's.
  325. Takes into account that reference counts may saturate and that the
  326. basic constants and the projection functions are referenced by the
  327. manager.
  328. @return the number of nodes with non-zero reference count.
  329. (Except for the cases mentioned above.)
  330. @sideeffect None
  331. */
  332. int
  333. Cudd_CheckZeroRef(
  334. DdManager * manager)
  335. {
  336. int size;
  337. int i, j;
  338. int remain; /* the expected number of remaining references to one */
  339. DdNodePtr *nodelist;
  340. DdNode *node;
  341. DdNode *sentinel = &(manager->sentinel);
  342. DdSubtable *subtable;
  343. int count = 0;
  344. int index;
  345. #ifndef DD_NO_DEATH_ROW
  346. cuddClearDeathRow(manager);
  347. #endif
  348. /* First look at the BDD/ADD subtables. */
  349. remain = 1; /* reference from the manager */
  350. size = manager->size;
  351. remain += 2 * size; /* reference from the BDD projection functions */
  352. for (i = 0; i < size; i++) {
  353. subtable = &(manager->subtables[i]);
  354. nodelist = subtable->nodelist;
  355. for (j = 0; (unsigned) j < subtable->slots; j++) {
  356. node = nodelist[j];
  357. while (node != sentinel) {
  358. if (node->ref != 0 && node->ref != DD_MAXREF) {
  359. index = (int) node->index;
  360. if (node != manager->vars[index]) {
  361. count++;
  362. } else {
  363. if (node->ref != 1) {
  364. count++;
  365. }
  366. }
  367. }
  368. node = node->next;
  369. }
  370. }
  371. }
  372. /* Then look at the ZDD subtables. */
  373. size = manager->sizeZ;
  374. if (size) /* references from ZDD universe */
  375. remain += 2;
  376. for (i = 0; i < size; i++) {
  377. subtable = &(manager->subtableZ[i]);
  378. nodelist = subtable->nodelist;
  379. for (j = 0; (unsigned) j < subtable->slots; j++) {
  380. node = nodelist[j];
  381. while (node != NULL) {
  382. if (node->ref != 0 && node->ref != DD_MAXREF) {
  383. index = (int) node->index;
  384. if (node == manager->univ[manager->permZ[index]]) {
  385. if (node->ref > 2) {
  386. count++;
  387. }
  388. } else {
  389. count++;
  390. }
  391. }
  392. node = node->next;
  393. }
  394. }
  395. }
  396. /* Now examine the constant table. Plusinfinity, minusinfinity, and
  397. ** zero are referenced by the manager. One is referenced by the
  398. ** manager, by the ZDD universe, and by all projection functions.
  399. ** All other nodes should have no references.
  400. */
  401. nodelist = manager->constants.nodelist;
  402. for (j = 0; (unsigned) j < manager->constants.slots; j++) {
  403. node = nodelist[j];
  404. while (node != NULL) {
  405. if (node->ref != 0 && node->ref != DD_MAXREF) {
  406. if (node == manager->one) {
  407. if ((int) node->ref != remain) {
  408. count++;
  409. }
  410. } else if (node == manager->zero ||
  411. node == manager->plusinfinity ||
  412. node == manager->minusinfinity) {
  413. if (node->ref != 1) {
  414. count++;
  415. }
  416. } else {
  417. count++;
  418. }
  419. }
  420. node = node->next;
  421. }
  422. }
  423. return(count);
  424. } /* end of Cudd_CheckZeroRef */
  425. /*---------------------------------------------------------------------------*/
  426. /* Definition of internal functions */
  427. /*---------------------------------------------------------------------------*/
  428. /**
  429. @brief Brings children of a dead node back.
  430. @sideeffect None
  431. @see cuddReclaimZdd
  432. */
  433. void
  434. cuddReclaim(
  435. DdManager * table,
  436. DdNode * n)
  437. {
  438. DdNode *N;
  439. int ord;
  440. DdNodePtr *stack = table->stack;
  441. int SP = 1;
  442. double initialDead = table->dead;
  443. N = Cudd_Regular(n);
  444. #ifdef DD_DEBUG
  445. assert(N->ref == 0);
  446. #endif
  447. do {
  448. if (N->ref == 0) {
  449. N->ref = 1;
  450. table->dead--;
  451. if (cuddIsConstant(N)) {
  452. table->constants.dead--;
  453. N = stack[--SP];
  454. } else {
  455. ord = table->perm[N->index];
  456. stack[SP++] = Cudd_Regular(cuddE(N));
  457. table->subtables[ord].dead--;
  458. N = cuddT(N);
  459. }
  460. } else {
  461. cuddSatInc(N->ref);
  462. N = stack[--SP];
  463. }
  464. } while (SP != 0);
  465. N = Cudd_Regular(n);
  466. cuddSatDec(N->ref);
  467. table->reclaimed += initialDead - table->dead;
  468. } /* end of cuddReclaim */
  469. /**
  470. @brief Brings children of a dead %ZDD node back.
  471. @sideeffect None
  472. @see cuddReclaim
  473. */
  474. void
  475. cuddReclaimZdd(
  476. DdManager * table,
  477. DdNode * n)
  478. {
  479. DdNode *N;
  480. int ord;
  481. DdNodePtr *stack = table->stack;
  482. int SP = 1;
  483. N = n;
  484. #ifdef DD_DEBUG
  485. assert(N->ref == 0);
  486. #endif
  487. do {
  488. cuddSatInc(N->ref);
  489. if (N->ref == 1) {
  490. table->deadZ--;
  491. table->reclaimed++;
  492. #ifdef DD_DEBUG
  493. assert(!cuddIsConstant(N));
  494. #endif
  495. ord = table->permZ[N->index];
  496. stack[SP++] = cuddE(N);
  497. table->subtableZ[ord].dead--;
  498. N = cuddT(N);
  499. } else {
  500. N = stack[--SP];
  501. }
  502. } while (SP != 0);
  503. cuddSatDec(n->ref);
  504. } /* end of cuddReclaimZdd */
  505. /**
  506. @brief Shrinks the death row.
  507. @details Shrinks the death row by a factor of four.
  508. @sideeffect None
  509. @see cuddClearDeathRow
  510. */
  511. void
  512. cuddShrinkDeathRow(
  513. DdManager *table)
  514. {
  515. #ifndef DD_NO_DEATH_ROW
  516. int i;
  517. if (table->deathRowDepth > 3) {
  518. for (i = table->deathRowDepth/4; i < table->deathRowDepth; i++) {
  519. if (table->deathRow[i] == NULL) break;
  520. Cudd_IterDerefBdd(table,table->deathRow[i]);
  521. table->deathRow[i] = NULL;
  522. }
  523. table->deathRowDepth /= 4;
  524. table->deadMask = table->deathRowDepth - 1;
  525. if ((unsigned) table->nextDead > table->deadMask) {
  526. table->nextDead = 0;
  527. }
  528. table->deathRow = REALLOC(DdNodePtr, table->deathRow,
  529. table->deathRowDepth);
  530. }
  531. #endif
  532. } /* end of cuddShrinkDeathRow */
  533. /**
  534. @brief Clears the death row.
  535. @sideeffect None
  536. @see Cudd_DelayedDerefBdd Cudd_IterDerefBdd Cudd_CheckZeroRef
  537. cuddGarbageCollect
  538. */
  539. void
  540. cuddClearDeathRow(
  541. DdManager *table)
  542. {
  543. #ifndef DD_NO_DEATH_ROW
  544. int i;
  545. for (i = 0; i < table->deathRowDepth; i++) {
  546. if (table->deathRow[i] == NULL) break;
  547. Cudd_IterDerefBdd(table,table->deathRow[i]);
  548. table->deathRow[i] = NULL;
  549. }
  550. #ifdef DD_DEBUG
  551. for (; i < table->deathRowDepth; i++) {
  552. assert(table->deathRow[i] == NULL);
  553. }
  554. #endif
  555. table->nextDead = 0;
  556. #endif
  557. } /* end of cuddClearDeathRow */
  558. /**
  559. @brief Checks whether a node is in the death row.
  560. @return the position of the first occurrence if the node is present;
  561. -1 otherwise.
  562. @sideeffect None
  563. @see Cudd_DelayedDerefBdd cuddClearDeathRow
  564. */
  565. int
  566. cuddIsInDeathRow(
  567. DdManager *dd,
  568. DdNode *f)
  569. {
  570. #ifndef DD_NO_DEATH_ROW
  571. int i;
  572. for (i = 0; i < dd->deathRowDepth; i++) {
  573. if (f == dd->deathRow[i]) {
  574. return(i);
  575. }
  576. }
  577. #endif
  578. return(-1);
  579. } /* end of cuddIsInDeathRow */
  580. /**
  581. @brief Counts how many times a node is in the death row.
  582. @sideeffect None
  583. @see Cudd_DelayedDerefBdd cuddClearDeathRow cuddIsInDeathRow
  584. */
  585. int
  586. cuddTimesInDeathRow(
  587. DdManager *dd,
  588. DdNode *f)
  589. {
  590. int count = 0;
  591. #ifndef DD_NO_DEATH_ROW
  592. int i;
  593. for (i = 0; i < dd->deathRowDepth; i++) {
  594. count += f == dd->deathRow[i];
  595. }
  596. #endif
  597. return(count);
  598. } /* end of cuddTimesInDeathRow */
  599. /*---------------------------------------------------------------------------*/
  600. /* Definition of static functions */
  601. /*---------------------------------------------------------------------------*/