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.

1310 lines
40 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Procedure to subset the given %BDD by choosing the heavier
  5. branches.
  6. @see cuddSubsetSP.c
  7. @author Kavita Ravi
  8. @copyright@parblock
  9. Copyright (c) 1995-2015, Regents of the University of Colorado
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions
  13. are met:
  14. Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. Neither the name of the University of Colorado nor the names of its
  20. contributors may be used to endorse or promote products derived from
  21. this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. POSSIBILITY OF SUCH DAMAGE.
  34. @endparblock
  35. */
  36. #ifdef __STDC__
  37. #include <float.h>
  38. #else
  39. #define DBL_MAX_EXP 1024
  40. #endif
  41. #include "util.h"
  42. #include "cuddInt.h"
  43. /*---------------------------------------------------------------------------*/
  44. /* Constant declarations */
  45. /*---------------------------------------------------------------------------*/
  46. #define DEFAULT_PAGE_SIZE 2048
  47. #define DEFAULT_NODE_DATA_PAGE_SIZE 1024
  48. #define INITIAL_PAGES 128
  49. #undef max
  50. /*---------------------------------------------------------------------------*/
  51. /* Type declarations */
  52. /*---------------------------------------------------------------------------*/
  53. typedef struct NodeData NodeData_t;
  54. typedef struct SubsetInfo SubsetInfo_t;
  55. /*---------------------------------------------------------------------------*/
  56. /* Stucture declarations */
  57. /*---------------------------------------------------------------------------*/
  58. /**
  59. * @brief Data structure to store the information on each node.
  60. * @details It keeps the number of minterms represented by the DAG
  61. * rooted at this node in terms of the number of variables specified
  62. * by the user, number of nodes in this DAG and the number of nodes of
  63. * its child with lesser number of minterms that are not shared by the
  64. * child with more minterms.
  65. */
  66. struct NodeData {
  67. double *mintermPointer;
  68. int *nodesPointer;
  69. int *lightChildNodesPointer;
  70. };
  71. /**
  72. * @brief Miscellaneous info.
  73. */
  74. struct SubsetInfo {
  75. DdNode *zero, *one; /**< constant functions */
  76. double **mintermPages; /**< pointers to the pages */
  77. int **nodePages; /**< pointers to the pages */
  78. int **lightNodePages; /**< pointers to the pages */
  79. double *currentMintermPage; /**< pointer to the current page */
  80. double max; /**< to store the 2^n value of the number of variables */
  81. int *currentNodePage; /**< pointer to the current page */
  82. int *currentLightNodePage; /**< pointer to the current page */
  83. int pageIndex; /**< index to next element */
  84. int page; /**< index to current page */
  85. int pageSize; /**< page size */
  86. int maxPages; /**< number of page pointers */
  87. NodeData_t *currentNodeDataPage; /**< pointer to the current page */
  88. int nodeDataPage; /**< index to next element */
  89. int nodeDataPageIndex; /**< index to next element */
  90. NodeData_t **nodeDataPages; /**< index to current page */
  91. int nodeDataPageSize; /**< page size */
  92. int maxNodeDataPages; /**< number of page pointers */
  93. int memOut;
  94. #ifdef DEBUG
  95. int num_calls;
  96. #endif
  97. };
  98. /*---------------------------------------------------------------------------*/
  99. /* Variable declarations */
  100. /*---------------------------------------------------------------------------*/
  101. /*---------------------------------------------------------------------------*/
  102. /* Macro declarations */
  103. /*---------------------------------------------------------------------------*/
  104. /** \cond */
  105. /*---------------------------------------------------------------------------*/
  106. /* Static function prototypes */
  107. /*---------------------------------------------------------------------------*/
  108. static void ResizeNodeDataPages (SubsetInfo_t * info);
  109. static void ResizeCountMintermPages (SubsetInfo_t * info);
  110. static void ResizeCountNodePages (SubsetInfo_t * info);
  111. static double SubsetCountMintermAux (DdNode *node, double max, st_table *table, SubsetInfo_t * info);
  112. static st_table * SubsetCountMinterm (DdNode *node, int nvars, SubsetInfo_t * info);
  113. static int SubsetCountNodesAux (DdNode *node, st_table *table, double max, SubsetInfo_t * info);
  114. static int SubsetCountNodes (DdNode *node, st_table *table, int nvars, SubsetInfo_t * info);
  115. static void StoreNodes (st_table *storeTable, DdManager *dd, DdNode *node);
  116. static DdNode * BuildSubsetBdd (DdManager *dd, DdNode *node, int *size, st_table *visitedTable, int threshold, st_table *storeTable, st_table *approxTable, SubsetInfo_t * info);
  117. /** \endcond */
  118. /*---------------------------------------------------------------------------*/
  119. /* Definition of exported functions */
  120. /*---------------------------------------------------------------------------*/
  121. /**
  122. @brief Extracts a dense subset from a %BDD with the heavy branch
  123. heuristic.
  124. @details This procedure builds a subset by throwing away one of the
  125. children of each node, starting from the root, until the result is
  126. small enough. The child that is eliminated from the result is the
  127. one that contributes the fewer minterms. The parameter numVars is
  128. the maximum number of variables to be used in minterm calculation
  129. and node count calculation. The optimal number should be as close
  130. as possible to the size of the support of f. However, it is safe to
  131. pass the value returned by Cudd_ReadSize for numVars when the number
  132. of variables is under 1023. If numVars is larger than 1023, it will
  133. overflow. If a 0 parameter is passed then the procedure will compute
  134. a value which will avoid overflow but will cause underflow with 2046
  135. variables or more.
  136. @return a pointer to the %BDD of the subset if successful. NULL if
  137. the procedure runs out of memory.
  138. @sideeffect None
  139. @see Cudd_SubsetShortPaths Cudd_SupersetHeavyBranch Cudd_ReadSize
  140. */
  141. DdNode *
  142. Cudd_SubsetHeavyBranch(
  143. DdManager * dd /**< manager */,
  144. DdNode * f /**< function to be subset */,
  145. int numVars /**< number of variables in the support of f */,
  146. int threshold /**< maximum number of nodes in the subset */)
  147. {
  148. DdNode *subset;
  149. do {
  150. dd->reordered = 0;
  151. subset = cuddSubsetHeavyBranch(dd, f, numVars, threshold);
  152. } while (dd->reordered == 1);
  153. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  154. dd->timeoutHandler(dd, dd->tohArg);
  155. }
  156. return(subset);
  157. } /* end of Cudd_SubsetHeavyBranch */
  158. /**
  159. @brief Extracts a dense superset from a %BDD with the heavy branch
  160. heuristic.
  161. @details The procedure is identical to the subset procedure except
  162. for the fact that it receives the complement of the given
  163. function. Extracting the subset of the complement function is
  164. equivalent to extracting the superset of the function. This
  165. procedure builds a superset by throwing away one of the children of
  166. each node starting from the root of the complement function, until
  167. the result is small enough. The child that is eliminated from the
  168. result is the one that contributes the fewer minterms. The
  169. parameter numVars is the maximum number of variables to be used in
  170. minterm calculation and node count calculation. The optimal number
  171. should be as close as possible to the size of the support of f.
  172. However, it is safe to pass the value returned by Cudd_ReadSize for
  173. numVars when the number of variables is under 1023. If numVars is
  174. larger than 1023, it will overflow. If a 0 parameter is passed then
  175. the procedure will compute a value which will avoid overflow but
  176. will cause underflow with 2046 variables or more.
  177. @return a pointer to the %BDD of the superset if successful. NULL if
  178. intermediate result causes the procedure to run out of memory.
  179. @sideeffect None
  180. @see Cudd_SubsetHeavyBranch Cudd_SupersetShortPaths Cudd_ReadSize
  181. */
  182. DdNode *
  183. Cudd_SupersetHeavyBranch(
  184. DdManager * dd /**< manager */,
  185. DdNode * f /**< function to be superset */,
  186. int numVars /**< number of variables in the support of f */,
  187. int threshold /**< maximum number of nodes in the superset */)
  188. {
  189. DdNode *subset, *g;
  190. g = Cudd_Not(f);
  191. do {
  192. dd->reordered = 0;
  193. subset = cuddSubsetHeavyBranch(dd, g, numVars, threshold);
  194. } while (dd->reordered == 1);
  195. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  196. dd->timeoutHandler(dd, dd->tohArg);
  197. }
  198. return(Cudd_NotCond(subset, (subset != NULL)));
  199. } /* end of Cudd_SupersetHeavyBranch */
  200. /*---------------------------------------------------------------------------*/
  201. /* Definition of internal functions */
  202. /*---------------------------------------------------------------------------*/
  203. /**
  204. @brief The main procedure that returns a subset by choosing the heavier
  205. branch in the %BDD.
  206. @details Here a subset %BDD is built by throwing away one of the
  207. children. Starting at root, annotate each node with the number of
  208. minterms (in terms of the total number of variables specified -
  209. numVars), number of nodes taken by the DAG rooted at this node and
  210. number of additional nodes taken by the child that has the lesser
  211. minterms. The child with the lower number of minterms is thrown away
  212. and a dyanmic count of the nodes of the subset is kept. Once the
  213. threshold is reached the subset is returned to the calling
  214. procedure.
  215. @sideeffect None
  216. @see Cudd_SubsetHeavyBranch
  217. */
  218. DdNode *
  219. cuddSubsetHeavyBranch(
  220. DdManager * dd /**< %DD manager */,
  221. DdNode * f /**< current %DD */,
  222. int numVars /**< maximum number of variables */,
  223. int threshold /**< threshold size for the subset */)
  224. {
  225. int i, *size;
  226. st_table *visitedTable;
  227. int numNodes;
  228. NodeData_t *currNodeQual;
  229. DdNode *subset;
  230. st_table *storeTable, *approxTable;
  231. DdNode *key, *value;
  232. st_generator *stGen;
  233. SubsetInfo_t info;
  234. if (f == NULL) {
  235. fprintf(dd->err, "Cannot subset, nil object\n");
  236. dd->errorCode = CUDD_INVALID_ARG;
  237. return(NULL);
  238. }
  239. /* If user does not know numVars value, set it to the maximum
  240. * exponent that the pow function can take. The -1 is due to the
  241. * discrepancy in the value that pow takes and the value that
  242. * log gives.
  243. */
  244. if (numVars == 0) {
  245. /* set default value */
  246. numVars = DBL_MAX_EXP - 1;
  247. }
  248. if (Cudd_IsConstantInt(f)) {
  249. return(f);
  250. }
  251. info.one = Cudd_ReadOne(dd);
  252. info.zero = Cudd_Not(info.one);
  253. info.mintermPages = NULL;
  254. info.nodePages = info.lightNodePages = NULL;
  255. info.currentMintermPage = NULL;
  256. info.max = pow(2.0, (double)numVars);
  257. info.currentNodePage = info.currentLightNodePage = NULL;
  258. info.pageIndex = info.page = 0;
  259. info.pageSize = DEFAULT_PAGE_SIZE;
  260. info.maxPages = 0;
  261. info.currentNodeDataPage = NULL;
  262. info.nodeDataPage = info.nodeDataPageIndex = 0;
  263. info.nodeDataPages = NULL;
  264. info.nodeDataPageSize = DEFAULT_NODE_DATA_PAGE_SIZE;
  265. info.maxNodeDataPages = 0;
  266. info.memOut = 0;
  267. #ifdef DEBUG
  268. info.num_calls = 0;
  269. #endif
  270. /* Create visited table where structures for node data are allocated and
  271. stored in a st_table */
  272. visitedTable = SubsetCountMinterm(f, numVars, &info);
  273. if ((visitedTable == NULL) || info.memOut) {
  274. (void) fprintf(dd->err, "Out-of-memory; Cannot subset\n");
  275. dd->errorCode = CUDD_MEMORY_OUT;
  276. return(0);
  277. }
  278. numNodes = SubsetCountNodes(f, visitedTable, numVars, &info);
  279. if (info.memOut) {
  280. (void) fprintf(dd->err, "Out-of-memory; Cannot subset\n");
  281. dd->errorCode = CUDD_MEMORY_OUT;
  282. return(0);
  283. }
  284. if (st_lookup(visitedTable, f, (void **) &currNodeQual) == 0) {
  285. fprintf(dd->err,
  286. "Something is wrong, ought to be node quality table\n");
  287. dd->errorCode = CUDD_INTERNAL_ERROR;
  288. }
  289. size = ALLOC(int, 1);
  290. if (size == NULL) {
  291. dd->errorCode = CUDD_MEMORY_OUT;
  292. return(NULL);
  293. }
  294. *size = numNodes;
  295. /* table to store nodes being created. */
  296. storeTable = st_init_table(st_ptrcmp, st_ptrhash);
  297. /* insert the constant */
  298. cuddRef(info.one);
  299. if (st_insert(storeTable, Cudd_ReadOne(dd), NULL) ==
  300. ST_OUT_OF_MEM) {
  301. fprintf(dd->out, "Something wrong, st_table insert failed\n");
  302. }
  303. /* table to store approximations of nodes */
  304. approxTable = st_init_table(st_ptrcmp, st_ptrhash);
  305. subset = (DdNode *)BuildSubsetBdd(dd, f, size, visitedTable, threshold,
  306. storeTable, approxTable, &info);
  307. if (subset != NULL) {
  308. cuddRef(subset);
  309. }
  310. if (info.memOut) {
  311. dd->errorCode = CUDD_MEMORY_OUT;
  312. dd->reordered = 0;
  313. }
  314. stGen = st_init_gen(approxTable);
  315. if (stGen == NULL) {
  316. st_free_table(approxTable);
  317. return(NULL);
  318. }
  319. while(st_gen(stGen, (void **) &key, (void **) &value)) {
  320. Cudd_RecursiveDeref(dd, value);
  321. }
  322. st_free_gen(stGen); stGen = NULL;
  323. st_free_table(approxTable);
  324. stGen = st_init_gen(storeTable);
  325. if (stGen == NULL) {
  326. st_free_table(storeTable);
  327. return(NULL);
  328. }
  329. while(st_gen(stGen, (void **) &key, (void **) &value)) {
  330. Cudd_RecursiveDeref(dd, key);
  331. }
  332. st_free_gen(stGen); stGen = NULL;
  333. st_free_table(storeTable);
  334. for (i = 0; i <= info.page; i++) {
  335. FREE(info.mintermPages[i]);
  336. }
  337. FREE(info.mintermPages);
  338. for (i = 0; i <= info.page; i++) {
  339. FREE(info.nodePages[i]);
  340. }
  341. FREE(info.nodePages);
  342. for (i = 0; i <= info.page; i++) {
  343. FREE(info.lightNodePages[i]);
  344. }
  345. FREE(info.lightNodePages);
  346. for (i = 0; i <= info.nodeDataPage; i++) {
  347. FREE(info.nodeDataPages[i]);
  348. }
  349. FREE(info.nodeDataPages);
  350. st_free_table(visitedTable);
  351. FREE(size);
  352. #if 0
  353. (void) Cudd_DebugCheck(dd);
  354. (void) Cudd_CheckKeys(dd);
  355. #endif
  356. if (subset != NULL) {
  357. #ifdef DD_DEBUG
  358. if (!Cudd_bddLeq(dd, subset, f)) {
  359. fprintf(dd->err, "Wrong subset\n");
  360. dd->errorCode = CUDD_INTERNAL_ERROR;
  361. return(NULL);
  362. }
  363. #endif
  364. cuddDeref(subset);
  365. }
  366. return(subset);
  367. } /* end of cuddSubsetHeavyBranch */
  368. /*---------------------------------------------------------------------------*/
  369. /* Definition of static functions */
  370. /*---------------------------------------------------------------------------*/
  371. /**
  372. @brief Resize the number of pages allocated to store the node data.
  373. @details The procedure moves the counter to the next page when the
  374. end of the page is reached and allocates new pages when necessary.
  375. @sideeffect Changes the size of pages, page, page index, maximum
  376. number of pages freeing stuff in case of memory out.
  377. */
  378. static void
  379. ResizeNodeDataPages(SubsetInfo_t * info)
  380. {
  381. int i;
  382. NodeData_t **newNodeDataPages;
  383. info->nodeDataPage++;
  384. /* If the current page index is larger than the number of pages
  385. * allocated, allocate a new page array. Page numbers are incremented by
  386. * INITIAL_PAGES
  387. */
  388. if (info->nodeDataPage == info->maxNodeDataPages) {
  389. newNodeDataPages = ALLOC(NodeData_t *, info->maxNodeDataPages + INITIAL_PAGES);
  390. if (newNodeDataPages == NULL) {
  391. for (i = 0; i < info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  392. FREE(info->nodeDataPages);
  393. info->memOut = 1;
  394. return;
  395. } else {
  396. for (i = 0; i < info->maxNodeDataPages; i++) {
  397. newNodeDataPages[i] = info->nodeDataPages[i];
  398. }
  399. /* Increase total page count */
  400. info->maxNodeDataPages += INITIAL_PAGES;
  401. FREE(info->nodeDataPages);
  402. info->nodeDataPages = newNodeDataPages;
  403. }
  404. }
  405. /* Allocate a new page */
  406. info->currentNodeDataPage = info->nodeDataPages[info->nodeDataPage] =
  407. ALLOC(NodeData_t ,info->nodeDataPageSize);
  408. if (info->currentNodeDataPage == NULL) {
  409. for (i = 0; i < info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  410. FREE(info->nodeDataPages);
  411. info->memOut = 1;
  412. return;
  413. }
  414. /* reset page index */
  415. info->nodeDataPageIndex = 0;
  416. return;
  417. } /* end of ResizeNodeDataPages */
  418. /**
  419. @brief Resize the number of pages allocated to store the minterm
  420. counts.
  421. @details The procedure moves the counter to the next page when the
  422. end of the page is reached and allocates new pages when necessary.
  423. @sideeffect Changes the size of minterm pages, page, page index, maximum
  424. number of pages freeing stuff in case of memory out.
  425. */
  426. static void
  427. ResizeCountMintermPages(SubsetInfo_t * info)
  428. {
  429. int i;
  430. double **newMintermPages;
  431. info->page++;
  432. /* If the current page index is larger than the number of pages
  433. * allocated, allocate a new page array. Page numbers are incremented by
  434. * INITIAL_PAGES
  435. */
  436. if (info->page == info->maxPages) {
  437. newMintermPages = ALLOC(double *, info->maxPages + INITIAL_PAGES);
  438. if (newMintermPages == NULL) {
  439. for (i = 0; i < info->page; i++) FREE(info->mintermPages[i]);
  440. FREE(info->mintermPages);
  441. info->memOut = 1;
  442. return;
  443. } else {
  444. for (i = 0; i < info->maxPages; i++) {
  445. newMintermPages[i] = info->mintermPages[i];
  446. }
  447. /* Increase total page count */
  448. info->maxPages += INITIAL_PAGES;
  449. FREE(info->mintermPages);
  450. info->mintermPages = newMintermPages;
  451. }
  452. }
  453. /* Allocate a new page */
  454. info->currentMintermPage = info->mintermPages[info->page] = ALLOC(double,info->pageSize);
  455. if (info->currentMintermPage == NULL) {
  456. for (i = 0; i < info->page; i++) FREE(info->mintermPages[i]);
  457. FREE(info->mintermPages);
  458. info->memOut = 1;
  459. return;
  460. }
  461. /* reset page index */
  462. info->pageIndex = 0;
  463. return;
  464. } /* end of ResizeCountMintermPages */
  465. /**
  466. @brief Resize the number of pages allocated to store the node counts.
  467. @details The procedure moves the counter to the next page when the
  468. end of the page is reached and allocates new pages when necessary.
  469. @sideeffect Changes the size of pages, page, page index, maximum
  470. number of pages freeing stuff in case of memory out.
  471. */
  472. static void
  473. ResizeCountNodePages(SubsetInfo_t * info)
  474. {
  475. int i;
  476. int **newNodePages;
  477. info->page++;
  478. /* If the current page index is larger than the number of pages
  479. * allocated, allocate a new page array. The number of pages is incremented
  480. * by INITIAL_PAGES.
  481. */
  482. if (info->page == info->maxPages) {
  483. newNodePages = ALLOC(int *, info->maxPages + INITIAL_PAGES);
  484. if (newNodePages == NULL) {
  485. for (i = 0; i < info->page; i++) FREE(info->nodePages[i]);
  486. FREE(info->nodePages);
  487. for (i = 0; i < info->page; i++) FREE(info->lightNodePages[i]);
  488. FREE(info->lightNodePages);
  489. info->memOut = 1;
  490. return;
  491. } else {
  492. for (i = 0; i < info->maxPages; i++) {
  493. newNodePages[i] = info->nodePages[i];
  494. }
  495. FREE(info->nodePages);
  496. info->nodePages = newNodePages;
  497. }
  498. newNodePages = ALLOC(int *, info->maxPages + INITIAL_PAGES);
  499. if (newNodePages == NULL) {
  500. for (i = 0; i < info->page; i++) FREE(info->nodePages[i]);
  501. FREE(info->nodePages);
  502. for (i = 0; i < info->page; i++) FREE(info->lightNodePages[i]);
  503. FREE(info->lightNodePages);
  504. info->memOut = 1;
  505. return;
  506. } else {
  507. for (i = 0; i < info->maxPages; i++) {
  508. newNodePages[i] = info->lightNodePages[i];
  509. }
  510. FREE(info->lightNodePages);
  511. info->lightNodePages = newNodePages;
  512. }
  513. /* Increase total page count */
  514. info->maxPages += INITIAL_PAGES;
  515. }
  516. /* Allocate a new page */
  517. info->currentNodePage = info->nodePages[info->page] = ALLOC(int,info->pageSize);
  518. if (info->currentNodePage == NULL) {
  519. for (i = 0; i < info->page; i++) FREE(info->nodePages[i]);
  520. FREE(info->nodePages);
  521. for (i = 0; i < info->page; i++) FREE(info->lightNodePages[i]);
  522. FREE(info->lightNodePages);
  523. info->memOut = 1;
  524. return;
  525. }
  526. /* Allocate a new page */
  527. info->currentLightNodePage = info->lightNodePages[info->page]
  528. = ALLOC(int,info->pageSize);
  529. if (info->currentLightNodePage == NULL) {
  530. for (i = 0; i <= info->page; i++) FREE(info->nodePages[i]);
  531. FREE(info->nodePages);
  532. for (i = 0; i < info->page; i++) FREE(info->lightNodePages[i]);
  533. FREE(info->lightNodePages);
  534. info->memOut = 1;
  535. return;
  536. }
  537. /* reset page index */
  538. info->pageIndex = 0;
  539. return;
  540. } /* end of ResizeCountNodePages */
  541. /**
  542. @brief Recursively counts minterms of each node in the DAG.
  543. @details Similar to the cuddCountMintermAux which recursively counts
  544. the number of minterms for the dag rooted at each node in terms of
  545. the total number of variables (max). This procedure creates the node
  546. data structure and stores the minterm count as part of the node data
  547. structure.
  548. @sideeffect Creates structures of type node quality and fills the st_table
  549. @see SubsetCountMinterm
  550. */
  551. static double
  552. SubsetCountMintermAux(
  553. DdNode * node /**< function to analyze */,
  554. double max /**< number of minterms of constant 1 */,
  555. st_table * table /**< visitedTable table */,
  556. SubsetInfo_t * info /**< miscellaneous info */)
  557. {
  558. DdNode *N,*Nv,*Nnv; /* nodes to store cofactors */
  559. double min,*pmin; /* minterm count */
  560. double min1, min2; /* minterm count */
  561. NodeData_t *dummy;
  562. NodeData_t *newEntry;
  563. int i;
  564. #ifdef DEBUG
  565. info->num_calls++;
  566. #endif
  567. /* Constant case */
  568. if (Cudd_IsConstantInt(node)) {
  569. if (node == info->zero) {
  570. return(0.0);
  571. } else {
  572. return(max);
  573. }
  574. } else {
  575. /* check if entry for this node exists */
  576. if (st_lookup(table, node, (void **) &dummy)) {
  577. min = *(dummy->mintermPointer);
  578. return(min);
  579. }
  580. /* Make the node regular to extract cofactors */
  581. N = Cudd_Regular(node);
  582. /* store the cofactors */
  583. Nv = Cudd_T(N);
  584. Nnv = Cudd_E(N);
  585. Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
  586. Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));
  587. min1 = SubsetCountMintermAux(Nv, max,table,info)/2.0;
  588. if (info->memOut) return(0.0);
  589. min2 = SubsetCountMintermAux(Nnv,max,table,info)/2.0;
  590. if (info->memOut) return(0.0);
  591. min = (min1+min2);
  592. /* if page index is at the bottom, then create a new page */
  593. if (info->pageIndex == info->pageSize) ResizeCountMintermPages(info);
  594. if (info->memOut) {
  595. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  596. FREE(info->nodeDataPages);
  597. st_free_table(table);
  598. return(0.0);
  599. }
  600. /* point to the correct location in the page */
  601. pmin = info->currentMintermPage + info->pageIndex;
  602. info->pageIndex++;
  603. /* store the minterm count of this node in the page */
  604. *pmin = min;
  605. /* Note I allocate the struct here. Freeing taken care of later */
  606. if (info->nodeDataPageIndex == info->nodeDataPageSize)
  607. ResizeNodeDataPages(info);
  608. if (info->memOut) {
  609. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  610. FREE(info->mintermPages);
  611. st_free_table(table);
  612. return(0.0);
  613. }
  614. newEntry = info->currentNodeDataPage + info->nodeDataPageIndex;
  615. info->nodeDataPageIndex++;
  616. /* points to the correct location in the page */
  617. newEntry->mintermPointer = pmin;
  618. /* initialize this field of the Node Quality structure */
  619. newEntry->nodesPointer = NULL;
  620. /* insert entry for the node in the table */
  621. if (st_insert(table,node, newEntry) == ST_OUT_OF_MEM) {
  622. info->memOut = 1;
  623. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  624. FREE(info->mintermPages);
  625. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  626. FREE(info->nodeDataPages);
  627. st_free_table(table);
  628. return(0.0);
  629. }
  630. return(min);
  631. }
  632. } /* end of SubsetCountMintermAux */
  633. /**
  634. @brief Counts minterms of each node in the DAG
  635. @details Similar to the Cudd_CountMinterm procedure except this
  636. returns the minterm count for all the nodes in the bdd in an
  637. st_table.
  638. @sideeffect none
  639. @see SubsetCountMintermAux
  640. */
  641. static st_table *
  642. SubsetCountMinterm(
  643. DdNode * node /**< function to be analyzed */,
  644. int nvars /**< number of variables node depends on */,
  645. SubsetInfo_t * info /**< miscellaneous info */)
  646. {
  647. st_table *table;
  648. int i;
  649. #ifdef DEBUG
  650. info->num_calls = 0;
  651. #endif
  652. info->max = pow(2.0,(double) nvars);
  653. table = st_init_table(st_ptrcmp,st_ptrhash);
  654. if (table == NULL) goto OUT_OF_MEM;
  655. info->maxPages = INITIAL_PAGES;
  656. info->mintermPages = ALLOC(double *,info->maxPages);
  657. if (info->mintermPages == NULL) {
  658. st_free_table(table);
  659. goto OUT_OF_MEM;
  660. }
  661. info->page = 0;
  662. info->currentMintermPage = ALLOC(double,info->pageSize);
  663. info->mintermPages[info->page] = info->currentMintermPage;
  664. if (info->currentMintermPage == NULL) {
  665. FREE(info->mintermPages);
  666. st_free_table(table);
  667. goto OUT_OF_MEM;
  668. }
  669. info->pageIndex = 0;
  670. info->maxNodeDataPages = INITIAL_PAGES;
  671. info->nodeDataPages = ALLOC(NodeData_t *, info->maxNodeDataPages);
  672. if (info->nodeDataPages == NULL) {
  673. for (i = 0; i <= info->page ; i++) FREE(info->mintermPages[i]);
  674. FREE(info->mintermPages);
  675. st_free_table(table);
  676. goto OUT_OF_MEM;
  677. }
  678. info->nodeDataPage = 0;
  679. info->currentNodeDataPage = ALLOC(NodeData_t ,info->nodeDataPageSize);
  680. info->nodeDataPages[info->nodeDataPage] = info->currentNodeDataPage;
  681. if (info->currentNodeDataPage == NULL) {
  682. for (i = 0; i <= info->page ; i++) FREE(info->mintermPages[i]);
  683. FREE(info->mintermPages);
  684. FREE(info->nodeDataPages);
  685. st_free_table(table);
  686. goto OUT_OF_MEM;
  687. }
  688. info->nodeDataPageIndex = 0;
  689. (void) SubsetCountMintermAux(node,info->max,table,info);
  690. if (info->memOut) goto OUT_OF_MEM;
  691. return(table);
  692. OUT_OF_MEM:
  693. info->memOut = 1;
  694. return(NULL);
  695. } /* end of SubsetCountMinterm */
  696. /**
  697. @brief Recursively counts the number of nodes under the dag.
  698. Also counts the number of nodes under the lighter child of
  699. this node.
  700. @details Note that the same dag may be the lighter child of two
  701. different nodes and have different counts. As with the minterm
  702. counts, the node counts are stored in pages to be space efficient
  703. and the address for these node counts are stored in an st_table
  704. associated to each node.
  705. @sideeffect Updates the node data table with node counts
  706. @see SubsetCountNodes
  707. */
  708. static int
  709. SubsetCountNodesAux(
  710. DdNode * node /**< current node */,
  711. st_table * table /**< table to update node count, also serves as visited table. */,
  712. double max /**< maximum number of variables */,
  713. SubsetInfo_t * info)
  714. {
  715. int tval, eval, i;
  716. DdNode *N, *Nv, *Nnv;
  717. double minNv, minNnv;
  718. NodeData_t *dummyN, *dummyNv, *dummyNnv, *dummyNBar;
  719. int *pmin, *pminBar, *val;
  720. if ((node == NULL) || Cudd_IsConstantInt(node))
  721. return(0);
  722. /* if this node has been processed do nothing */
  723. if (st_lookup(table, node, (void **) &dummyN) == 1) {
  724. val = dummyN->nodesPointer;
  725. if (val != NULL)
  726. return(0);
  727. } else {
  728. return(0);
  729. }
  730. N = Cudd_Regular(node);
  731. Nv = Cudd_T(N);
  732. Nnv = Cudd_E(N);
  733. Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
  734. Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));
  735. /* find the minterm counts for the THEN and ELSE branches */
  736. if (Cudd_IsConstantInt(Nv)) {
  737. if (Nv == info->zero) {
  738. minNv = 0.0;
  739. } else {
  740. minNv = max;
  741. }
  742. } else {
  743. if (st_lookup(table, Nv, (void **) &dummyNv) == 1)
  744. minNv = *(dummyNv->mintermPointer);
  745. else {
  746. return(0);
  747. }
  748. }
  749. if (Cudd_IsConstantInt(Nnv)) {
  750. if (Nnv == info->zero) {
  751. minNnv = 0.0;
  752. } else {
  753. minNnv = max;
  754. }
  755. } else {
  756. if (st_lookup(table, Nnv, (void **) &dummyNnv) == 1) {
  757. minNnv = *(dummyNnv->mintermPointer);
  758. }
  759. else {
  760. return(0);
  761. }
  762. }
  763. /* recur based on which has larger minterm, */
  764. if (minNv >= minNnv) {
  765. tval = SubsetCountNodesAux(Nv, table, max, info);
  766. if (info->memOut) return(0);
  767. eval = SubsetCountNodesAux(Nnv, table, max, info);
  768. if (info->memOut) return(0);
  769. /* store the node count of the lighter child. */
  770. if (info->pageIndex == info->pageSize) ResizeCountNodePages(info);
  771. if (info->memOut) {
  772. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  773. FREE(info->mintermPages);
  774. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  775. FREE(info->nodeDataPages);
  776. st_free_table(table);
  777. return(0);
  778. }
  779. pmin = info->currentLightNodePage + info->pageIndex;
  780. *pmin = eval; /* Here the ELSE child is lighter */
  781. dummyN->lightChildNodesPointer = pmin;
  782. } else {
  783. eval = SubsetCountNodesAux(Nnv, table, max, info);
  784. if (info->memOut) return(0);
  785. tval = SubsetCountNodesAux(Nv, table, max, info);
  786. if (info->memOut) return(0);
  787. /* store the node count of the lighter child. */
  788. if (info->pageIndex == info->pageSize) ResizeCountNodePages(info);
  789. if (info->memOut) {
  790. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  791. FREE(info->mintermPages);
  792. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  793. FREE(info->nodeDataPages);
  794. st_free_table(table);
  795. return(0);
  796. }
  797. pmin = info->currentLightNodePage + info->pageIndex;
  798. *pmin = tval; /* Here the THEN child is lighter */
  799. dummyN->lightChildNodesPointer = pmin;
  800. }
  801. /* updating the page index for node count storage. */
  802. pmin = info->currentNodePage + info->pageIndex;
  803. *pmin = tval + eval + 1;
  804. dummyN->nodesPointer = pmin;
  805. /* pageIndex is parallel page index for count_nodes and count_lightNodes */
  806. info->pageIndex++;
  807. /* if this node has been reached first, it belongs to a heavier
  808. branch. Its complement will be reached later on a lighter branch.
  809. Hence the complement has zero node count. */
  810. if (st_lookup(table, Cudd_Not(node), (void **) &dummyNBar) == 1) {
  811. if (info->pageIndex == info->pageSize) ResizeCountNodePages(info);
  812. if (info->memOut) {
  813. for (i = 0; i < info->page; i++) FREE(info->mintermPages[i]);
  814. FREE(info->mintermPages);
  815. for (i = 0; i < info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  816. FREE(info->nodeDataPages);
  817. st_free_table(table);
  818. return(0);
  819. }
  820. pminBar = info->currentLightNodePage + info->pageIndex;
  821. *pminBar = 0;
  822. dummyNBar->lightChildNodesPointer = pminBar;
  823. /* The lighter child has less nodes than the parent.
  824. * So if parent 0 then lighter child zero
  825. */
  826. if (info->pageIndex == info->pageSize) ResizeCountNodePages(info);
  827. if (info->memOut) {
  828. for (i = 0; i < info->page; i++) FREE(info->mintermPages[i]);
  829. FREE(info->mintermPages);
  830. for (i = 0; i < info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  831. FREE(info->nodeDataPages);
  832. st_free_table(table);
  833. return(0);
  834. }
  835. pminBar = info->currentNodePage + info->pageIndex;
  836. *pminBar = 0;
  837. dummyNBar->nodesPointer = pminBar ; /* maybe should point to zero */
  838. info->pageIndex++;
  839. }
  840. return(*pmin);
  841. } /*end of SubsetCountNodesAux */
  842. /**
  843. @brief Counts the nodes under the current node and its lighter child.
  844. @details Calls a recursive procedure to count the number of nodes of
  845. a DAG rooted at a particular node and the number of nodes taken by
  846. its lighter child.
  847. @sideeffect None
  848. @see SubsetCountNodesAux
  849. */
  850. static int
  851. SubsetCountNodes(
  852. DdNode * node /**< function to be analyzed */,
  853. st_table * table /**< node quality table */,
  854. int nvars /**< number of variables node depends on */,
  855. SubsetInfo_t * info /**< miscellaneous info */)
  856. {
  857. int num;
  858. int i;
  859. #ifdef DEBUG
  860. info->num_calls = 0;
  861. #endif
  862. info->max = pow(2.0,(double) nvars);
  863. info->maxPages = INITIAL_PAGES;
  864. info->nodePages = ALLOC(int *, info->maxPages);
  865. if (info->nodePages == NULL) {
  866. goto OUT_OF_MEM;
  867. }
  868. info->lightNodePages = ALLOC(int *, info->maxPages);
  869. if (info->lightNodePages == NULL) {
  870. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  871. FREE(info->mintermPages);
  872. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  873. FREE(info->nodeDataPages);
  874. FREE(info->nodePages);
  875. goto OUT_OF_MEM;
  876. }
  877. info->page = 0;
  878. info->currentNodePage = info->nodePages[info->page] =
  879. ALLOC(int,info->pageSize);
  880. if (info->currentNodePage == NULL) {
  881. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  882. FREE(info->mintermPages);
  883. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  884. FREE(info->nodeDataPages);
  885. FREE(info->lightNodePages);
  886. FREE(info->nodePages);
  887. goto OUT_OF_MEM;
  888. }
  889. info->currentLightNodePage = info->lightNodePages[info->page] =
  890. ALLOC(int,info->pageSize);
  891. if (info->currentLightNodePage == NULL) {
  892. for (i = 0; i <= info->page; i++) FREE(info->mintermPages[i]);
  893. FREE(info->mintermPages);
  894. for (i = 0; i <= info->nodeDataPage; i++) FREE(info->nodeDataPages[i]);
  895. FREE(info->nodeDataPages);
  896. FREE(info->currentNodePage);
  897. FREE(info->lightNodePages);
  898. FREE(info->nodePages);
  899. goto OUT_OF_MEM;
  900. }
  901. info->pageIndex = 0;
  902. num = SubsetCountNodesAux(node,table,info->max,info);
  903. if (info->memOut) goto OUT_OF_MEM;
  904. return(num);
  905. OUT_OF_MEM:
  906. info->memOut = 1;
  907. return(0);
  908. } /* end of SubsetCountNodes */
  909. /**
  910. @brief Procedure to recursively store nodes that are retained in the subset.
  911. @sideeffect None
  912. @see StoreNodes
  913. */
  914. static void
  915. StoreNodes(
  916. st_table * storeTable,
  917. DdManager * dd,
  918. DdNode * node)
  919. {
  920. DdNode *N, *Nt, *Ne;
  921. if (Cudd_IsConstantInt(dd)) {
  922. return;
  923. }
  924. N = Cudd_Regular(node);
  925. if (st_is_member(storeTable, N)) {
  926. return;
  927. }
  928. cuddRef(N);
  929. if (st_insert(storeTable, N, NULL) == ST_OUT_OF_MEM) {
  930. fprintf(dd->err,"Something wrong, st_table insert failed\n");
  931. }
  932. Nt = Cudd_T(N);
  933. Ne = Cudd_E(N);
  934. StoreNodes(storeTable, dd, Nt);
  935. StoreNodes(storeTable, dd, Ne);
  936. return;
  937. }
  938. /**
  939. @brief Builds the subset %BDD using the heavy branch method.
  940. @details The procedure carries out the building of the subset %BDD
  941. starting at the root. Using the three different counts labelling each node,
  942. the procedure chooses the heavier branch starting from the root and keeps
  943. track of the number of nodes it discards at each step, thus keeping count
  944. of the size of the subset %BDD dynamically. Once the threshold is satisfied,
  945. the procedure then calls ITE to build the %BDD.
  946. @sideeffect None
  947. */
  948. static DdNode *
  949. BuildSubsetBdd(
  950. DdManager * dd /**< %DD manager */,
  951. DdNode * node /**< current node */,
  952. int * size /**< current size of the subset */,
  953. st_table * visitedTable /**< visited table storing all node data */,
  954. int threshold /**< subsetting threshold */,
  955. st_table * storeTable /**< store table */,
  956. st_table * approxTable /**< approximation table */,
  957. SubsetInfo_t * info /**< miscellaneous info */)
  958. {
  959. DdNode *Nv, *Nnv, *N, *topv, *neW;
  960. double minNv, minNnv;
  961. NodeData_t *currNodeQual;
  962. NodeData_t *currNodeQualT;
  963. NodeData_t *currNodeQualE;
  964. DdNode *ThenBranch, *ElseBranch;
  965. int topid;
  966. void *dummy;
  967. #ifdef DEBUG
  968. info->num_calls++;
  969. #endif
  970. /*If the size of the subset is below the threshold, dont do
  971. anything. */
  972. if ((*size) <= threshold) {
  973. /* store nodes below this, so we can recombine if possible */
  974. StoreNodes(storeTable, dd, node);
  975. return(node);
  976. }
  977. if (Cudd_IsConstantInt(node))
  978. return(node);
  979. /* Look up minterm count for this node. */
  980. if (!st_lookup(visitedTable, node, (void **) &currNodeQual)) {
  981. fprintf(dd->err,
  982. "Something is wrong, ought to be in node quality table\n");
  983. }
  984. /* Get children. */
  985. N = Cudd_Regular(node);
  986. Nv = Cudd_T(N);
  987. Nnv = Cudd_E(N);
  988. /* complement if necessary */
  989. Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
  990. Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));
  991. if (!Cudd_IsConstantInt(Nv)) {
  992. /* find out minterms and nodes contributed by then child */
  993. if (!st_lookup(visitedTable, Nv, (void **) &currNodeQualT)) {
  994. fprintf(dd->out,"Something wrong, couldnt find nodes in node quality table\n");
  995. dd->errorCode = CUDD_INTERNAL_ERROR;
  996. return(NULL);
  997. }
  998. else {
  999. minNv = *(((NodeData_t *)currNodeQualT)->mintermPointer);
  1000. }
  1001. } else {
  1002. if (Nv == info->zero) {
  1003. minNv = 0;
  1004. } else {
  1005. minNv = info->max;
  1006. }
  1007. }
  1008. if (!Cudd_IsConstantInt(Nnv)) {
  1009. /* find out minterms and nodes contributed by else child */
  1010. if (!st_lookup(visitedTable, Nnv, (void **) &currNodeQualE)) {
  1011. fprintf(dd->out,"Something wrong, couldnt find nodes in node quality table\n");
  1012. dd->errorCode = CUDD_INTERNAL_ERROR;
  1013. return(NULL);
  1014. } else {
  1015. minNnv = *(((NodeData_t *)currNodeQualE)->mintermPointer);
  1016. }
  1017. } else {
  1018. if (Nnv == info->zero) {
  1019. minNnv = 0;
  1020. } else {
  1021. minNnv = info->max;
  1022. }
  1023. }
  1024. /* keep track of size of subset by subtracting the number of
  1025. * differential nodes contributed by lighter child
  1026. */
  1027. *size = (*(size)) - (int)*(currNodeQual->lightChildNodesPointer);
  1028. if (minNv >= minNnv) { /*SubsetCountNodesAux procedure takes
  1029. the Then branch in case of a tie */
  1030. /* recur with the Then branch */
  1031. ThenBranch = (DdNode *)BuildSubsetBdd(dd, Nv, size, visitedTable,
  1032. threshold, storeTable,
  1033. approxTable, info);
  1034. if (ThenBranch == NULL) {
  1035. return(NULL);
  1036. }
  1037. cuddRef(ThenBranch);
  1038. /* The Else branch is either a node that already exists in the
  1039. * subset, or one whose approximation has been computed, or
  1040. * Zero.
  1041. */
  1042. if (st_lookup(storeTable, Cudd_Regular(Nnv), &dummy)) {
  1043. ElseBranch = Nnv;
  1044. cuddRef(ElseBranch);
  1045. } else {
  1046. if (st_lookup(approxTable, Nnv, &dummy)) {
  1047. ElseBranch = (DdNode *)dummy;
  1048. cuddRef(ElseBranch);
  1049. } else {
  1050. ElseBranch = info->zero;
  1051. cuddRef(ElseBranch);
  1052. }
  1053. }
  1054. }
  1055. else {
  1056. /* recur with the Else branch */
  1057. ElseBranch = (DdNode *)BuildSubsetBdd(dd, Nnv, size, visitedTable,
  1058. threshold, storeTable,
  1059. approxTable, info);
  1060. if (ElseBranch == NULL) {
  1061. return(NULL);
  1062. }
  1063. cuddRef(ElseBranch);
  1064. /* The Then branch is either a node that already exists in the
  1065. * subset, or one whose approximation has been computed, or
  1066. * Zero.
  1067. */
  1068. if (st_lookup(storeTable, Cudd_Regular(Nv), &dummy)) {
  1069. ThenBranch = Nv;
  1070. cuddRef(ThenBranch);
  1071. } else {
  1072. if (st_lookup(approxTable, Nv, &dummy)) {
  1073. ThenBranch = (DdNode *)dummy;
  1074. cuddRef(ThenBranch);
  1075. } else {
  1076. ThenBranch = info->zero;
  1077. cuddRef(ThenBranch);
  1078. }
  1079. }
  1080. }
  1081. /* construct the Bdd with the top variable and the two children */
  1082. topid = Cudd_NodeReadIndex(N);
  1083. topv = Cudd_ReadVars(dd, topid);
  1084. cuddRef(topv);
  1085. neW = cuddBddIteRecur(dd, topv, ThenBranch, ElseBranch);
  1086. if (neW != NULL) {
  1087. cuddRef(neW);
  1088. }
  1089. Cudd_RecursiveDeref(dd, topv);
  1090. Cudd_RecursiveDeref(dd, ThenBranch);
  1091. Cudd_RecursiveDeref(dd, ElseBranch);
  1092. if (neW == NULL)
  1093. return(NULL);
  1094. else {
  1095. /* store this node in the store table */
  1096. if (!st_lookup(storeTable, Cudd_Regular(neW), &dummy)) {
  1097. cuddRef(neW);
  1098. if (st_insert(storeTable, Cudd_Regular(neW), NULL) ==
  1099. ST_OUT_OF_MEM)
  1100. return (NULL);
  1101. }
  1102. /* store the approximation for this node */
  1103. if (N != Cudd_Regular(neW)) {
  1104. if (st_lookup(approxTable, node, &dummy)) {
  1105. fprintf(dd->err, "This node should not be in the approximated table\n");
  1106. } else {
  1107. cuddRef(neW);
  1108. if (st_insert(approxTable, node, neW) ==
  1109. ST_OUT_OF_MEM)
  1110. return(NULL);
  1111. }
  1112. }
  1113. cuddDeref(neW);
  1114. return(neW);
  1115. }
  1116. } /* end of BuildSubsetBdd */