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.

1331 lines
40 KiB

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