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.

1660 lines
55 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddSubsetSP.c]
  3. PackageName [cudd]
  4. Synopsis [Procedure to subset the given BDD choosing the shortest paths
  5. (largest cubes) in the BDD.]
  6. Description [External procedures included in this module:
  7. <ul>
  8. <li> Cudd_SubsetShortPaths()
  9. <li> Cudd_SupersetShortPaths()
  10. </ul>
  11. Internal procedures included in this module:
  12. <ul>
  13. <li> cuddSubsetShortPaths()
  14. </ul>
  15. Static procedures included in this module:
  16. <ul>
  17. <li> BuildSubsetBdd()
  18. <li> CreatePathTable()
  19. <li> AssessPathLength()
  20. <li> CreateTopDist()
  21. <li> CreateBotDist()
  22. <li> ResizeNodeDistPages()
  23. <li> ResizeQueuePages()
  24. <li> stPathTableDdFree()
  25. </ul>
  26. ]
  27. SeeAlso [cuddSubsetHB.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. #include "util.h"
  56. #include "cuddInt.h"
  57. /*---------------------------------------------------------------------------*/
  58. /* Constant declarations */
  59. /*---------------------------------------------------------------------------*/
  60. #define DEFAULT_PAGE_SIZE 2048 /* page size to store the BFS queue element type */
  61. #define DEFAULT_NODE_DIST_PAGE_SIZE 2048 /* page size to store NodeDist_t type */
  62. #define MAXSHORTINT ((DdHalfWord) ~0) /* constant defined to store
  63. * maximum distance of a node
  64. * from the root or the constant
  65. */
  66. #define INITIAL_PAGES 128 /* number of initial pages for the
  67. * queue/NodeDist_t type */
  68. /*---------------------------------------------------------------------------*/
  69. /* Stucture declarations */
  70. /*---------------------------------------------------------------------------*/
  71. /* structure created to store subset results for each node and distances with
  72. * odd and even parity of the node from the root and sink. Main data structure
  73. * in this procedure.
  74. */
  75. struct NodeDist {
  76. DdHalfWord oddTopDist;
  77. DdHalfWord evenTopDist;
  78. DdHalfWord oddBotDist;
  79. DdHalfWord evenBotDist;
  80. DdNode *regResult;
  81. DdNode *compResult;
  82. };
  83. /* assorted information needed by the BuildSubsetBdd procedure. */
  84. struct AssortedInfo {
  85. unsigned int maxpath;
  86. int findShortestPath;
  87. int thresholdReached;
  88. st_table *maxpathTable;
  89. int threshold;
  90. };
  91. struct GlobalInfo {
  92. struct NodeDist **nodeDistPages; /* pointers to the pages */
  93. int nodeDistPageIndex; /* index to next element */
  94. int nodeDistPage; /* index to current page */
  95. int nodeDistPageSize; /* page size */
  96. int maxNodeDistPages; /* number of page pointers */
  97. struct NodeDist *currentNodeDistPage; /* current page */
  98. DdNode ***queuePages; /* pointers to the pages */
  99. int queuePageIndex; /* index to next element */
  100. int queuePage; /* index to current page */
  101. int queuePageSize; /* page size */
  102. int maxQueuePages; /* number of page pointers */
  103. DdNode **currentQueuePage; /* current page */
  104. #ifdef DD_DEBUG
  105. int numCalls;
  106. int hits;
  107. int thishit;
  108. #endif
  109. };
  110. /*---------------------------------------------------------------------------*/
  111. /* Type declarations */
  112. /*---------------------------------------------------------------------------*/
  113. typedef struct NodeDist NodeDist_t;
  114. typedef struct GlobalInfo GlobalInfo_t;
  115. /*---------------------------------------------------------------------------*/
  116. /* Variable declarations */
  117. /*---------------------------------------------------------------------------*/
  118. #ifndef lint
  119. static char rcsid[] DD_UNUSED = "$Id: cuddSubsetSP.c,v 1.36 2012/02/05 01:07:19 fabio Exp $";
  120. #endif
  121. /*---------------------------------------------------------------------------*/
  122. /* Macro declarations */
  123. /*---------------------------------------------------------------------------*/
  124. #ifdef __cplusplus
  125. extern "C" {
  126. #endif
  127. /**AutomaticStart*************************************************************/
  128. /*---------------------------------------------------------------------------*/
  129. /* Static function prototypes */
  130. /*---------------------------------------------------------------------------*/
  131. static void ResizeNodeDistPages (DdManager *dd, GlobalInfo_t *gInfo);
  132. static void ResizeQueuePages (DdManager *dd, GlobalInfo_t *gInfo);
  133. static void CreateTopDist (DdManager *dd, GlobalInfo_t *gInfo, st_table *pathTable, int parentPage, int parentQueueIndex, int topLen, DdNode **childPage, int childQueueIndex, int numParents, FILE *fp);
  134. static int CreateBotDist (DdNode *node, st_table *pathTable, unsigned int *pathLengthArray, FILE *fp);
  135. static st_table * CreatePathTable (DdManager *dd, GlobalInfo_t *gInfo, DdNode *node, unsigned int *pathLengthArray, FILE *fp);
  136. static unsigned int AssessPathLength (unsigned int *pathLengthArray, int threshold, int numVars, unsigned int *excess, FILE *fp);
  137. static DdNode * BuildSubsetBdd (DdManager *dd, GlobalInfo_t *gInfo, st_table *pathTable, DdNode *node, struct AssortedInfo *info, st_table *subsetNodeTable);
  138. static enum st_retval stPathTableDdFree (char *key, char *value, char *arg);
  139. /**AutomaticEnd***************************************************************/
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. /*---------------------------------------------------------------------------*/
  144. /* Definition of Exported functions */
  145. /*---------------------------------------------------------------------------*/
  146. /**Function********************************************************************
  147. Synopsis [Extracts a dense subset from a BDD with the shortest paths
  148. heuristic.]
  149. Description [Extracts a dense subset from a BDD. This procedure
  150. tries to preserve the shortest paths of the input BDD, because they
  151. give many minterms and contribute few nodes. This procedure may
  152. increase the number of nodes in trying to create the subset or
  153. reduce the number of nodes due to recombination as compared to the
  154. original BDD. Hence the threshold may not be strictly adhered to. In
  155. practice, recombination overshadows the increase in the number of
  156. nodes and results in small BDDs as compared to the threshold. The
  157. hardlimit specifies whether threshold needs to be strictly adhered
  158. to. If it is set to 1, the procedure ensures that result is never
  159. larger than the specified limit but may be considerably less than
  160. the threshold. Returns a pointer to the BDD for the subset if
  161. successful; NULL otherwise. The value for numVars should be as
  162. close as possible to the size of the support of f for better
  163. efficiency. However, it is safe to pass the value returned by
  164. Cudd_ReadSize for numVars. If 0 is passed, then the value returned
  165. by Cudd_ReadSize is used.]
  166. SideEffects [None]
  167. SeeAlso [Cudd_SupersetShortPaths Cudd_SubsetHeavyBranch Cudd_ReadSize]
  168. ******************************************************************************/
  169. DdNode *
  170. Cudd_SubsetShortPaths(
  171. DdManager * dd /* manager */,
  172. DdNode * f /* function to be subset */,
  173. int numVars /* number of variables in the support of f */,
  174. int threshold /* maximum number of nodes in the subset */,
  175. int hardlimit /* flag: 1 if threshold is a hard limit */)
  176. {
  177. DdNode *subset;
  178. do {
  179. dd->reordered = 0;
  180. subset = cuddSubsetShortPaths(dd, f, numVars, threshold, hardlimit);
  181. } while(dd->reordered == 1);
  182. return(subset);
  183. } /* end of Cudd_SubsetShortPaths */
  184. /**Function********************************************************************
  185. Synopsis [Extracts a dense superset from a BDD with the shortest paths
  186. heuristic.]
  187. Description [Extracts a dense superset from a BDD. The procedure is
  188. identical to the subset procedure except for the fact that it
  189. receives the complement of the given function. Extracting the subset
  190. of the complement function is equivalent to extracting the superset
  191. of the function. This procedure tries to preserve the shortest
  192. paths of the complement BDD, because they give many minterms and
  193. contribute few nodes. This procedure may increase the number of
  194. nodes in trying to create the superset or reduce the number of nodes
  195. due to recombination as compared to the original BDD. Hence the
  196. threshold may not be strictly adhered to. In practice, recombination
  197. overshadows the increase in the number of nodes and results in small
  198. BDDs as compared to the threshold. The hardlimit specifies whether
  199. threshold needs to be strictly adhered to. If it is set to 1, the
  200. procedure ensures that result is never larger than the specified
  201. limit but may be considerably less than the threshold. Returns a
  202. pointer to the BDD for the superset if successful; NULL
  203. otherwise. The value for numVars should be as close as possible to
  204. the size of the support of f for better efficiency. However, it is
  205. safe to pass the value returned by Cudd_ReadSize for numVar. If 0
  206. is passed, then the value returned by Cudd_ReadSize is used.]
  207. SideEffects [None]
  208. SeeAlso [Cudd_SubsetShortPaths Cudd_SupersetHeavyBranch Cudd_ReadSize]
  209. ******************************************************************************/
  210. DdNode *
  211. Cudd_SupersetShortPaths(
  212. DdManager * dd /* manager */,
  213. DdNode * f /* function to be superset */,
  214. int numVars /* number of variables in the support of f */,
  215. int threshold /* maximum number of nodes in the subset */,
  216. int hardlimit /* flag: 1 if threshold is a hard limit */)
  217. {
  218. DdNode *subset, *g;
  219. g = Cudd_Not(f);
  220. do {
  221. dd->reordered = 0;
  222. subset = cuddSubsetShortPaths(dd, g, numVars, threshold, hardlimit);
  223. } while(dd->reordered == 1);
  224. return(Cudd_NotCond(subset, (subset != NULL)));
  225. } /* end of Cudd_SupersetShortPaths */
  226. /*---------------------------------------------------------------------------*/
  227. /* Definition of internal functions */
  228. /*---------------------------------------------------------------------------*/
  229. /**Function********************************************************************
  230. Synopsis [The outermost procedure to return a subset of the given BDD
  231. with the shortest path lengths.]
  232. Description [The outermost procedure to return a subset of the given
  233. BDD with the largest cubes. The path lengths are calculated, the maximum
  234. allowable path length is determined and the number of nodes of this
  235. path length that can be used to build a subset. If the threshold is
  236. larger than the size of the original BDD, the original BDD is
  237. returned. ]
  238. SideEffects [None]
  239. SeeAlso [Cudd_SubsetShortPaths]
  240. ******************************************************************************/
  241. DdNode *
  242. cuddSubsetShortPaths(
  243. DdManager * dd /* DD manager */,
  244. DdNode * f /* function to be subset */,
  245. int numVars /* total number of variables in consideration */,
  246. int threshold /* maximum number of nodes allowed in the subset */,
  247. int hardlimit /* flag determining whether threshold should be respected strictly */)
  248. {
  249. GlobalInfo_t gInfo;
  250. st_table *pathTable;
  251. DdNode *N, *subset;
  252. unsigned int *pathLengthArray;
  253. unsigned int maxpath, oddLen, evenLen, pathLength, *excess;
  254. int i;
  255. NodeDist_t *nodeStat;
  256. struct AssortedInfo *info;
  257. st_table *subsetNodeTable;
  258. gInfo.nodeDistPageSize = DEFAULT_NODE_DIST_PAGE_SIZE;
  259. gInfo.queuePageSize = DEFAULT_PAGE_SIZE;
  260. if (numVars == 0) {
  261. /* set default value */
  262. numVars = Cudd_ReadSize(dd);
  263. }
  264. if (threshold > numVars) {
  265. threshold = threshold - numVars;
  266. }
  267. if (f == NULL) {
  268. fprintf(dd->err, "Cannot partition, nil object\n");
  269. dd->errorCode = CUDD_INVALID_ARG;
  270. return(NULL);
  271. }
  272. if (Cudd_IsConstant(f))
  273. return (f);
  274. pathLengthArray = ALLOC(unsigned int, numVars+1);
  275. for (i = 0; i < numVars+1; i++) pathLengthArray[i] = 0;
  276. #ifdef DD_DEBUG
  277. gInfo.numCalls = 0;
  278. #endif
  279. pathTable = CreatePathTable(dd, &gInfo, f, pathLengthArray, dd->err);
  280. if ((pathTable == NULL) || (dd->errorCode == CUDD_MEMORY_OUT)) {
  281. if (pathTable != NULL)
  282. st_free_table(pathTable);
  283. FREE(pathLengthArray);
  284. return (NIL(DdNode));
  285. }
  286. excess = ALLOC(unsigned int, 1);
  287. *excess = 0;
  288. maxpath = AssessPathLength(pathLengthArray, threshold, numVars, excess,
  289. dd->err);
  290. if (maxpath != (unsigned) (numVars + 1)) {
  291. info = ALLOC(struct AssortedInfo, 1);
  292. info->maxpath = maxpath;
  293. info->findShortestPath = 0;
  294. info->thresholdReached = *excess;
  295. info->maxpathTable = st_init_table(st_ptrcmp, st_ptrhash);
  296. info->threshold = threshold;
  297. #ifdef DD_DEBUG
  298. (void) fprintf(dd->out, "Path length array\n");
  299. for (i = 0; i < (numVars+1); i++) {
  300. if (pathLengthArray[i])
  301. (void) fprintf(dd->out, "%d ",i);
  302. }
  303. (void) fprintf(dd->out, "\n");
  304. for (i = 0; i < (numVars+1); i++) {
  305. if (pathLengthArray[i])
  306. (void) fprintf(dd->out, "%d ",pathLengthArray[i]);
  307. }
  308. (void) fprintf(dd->out, "\n");
  309. (void) fprintf(dd->out, "Maxpath = %d, Thresholdreached = %d\n",
  310. maxpath, info->thresholdReached);
  311. #endif
  312. N = Cudd_Regular(f);
  313. if (!st_lookup(pathTable, N, &nodeStat)) {
  314. fprintf(dd->err, "Something wrong, root node must be in table\n");
  315. dd->errorCode = CUDD_INTERNAL_ERROR;
  316. FREE(excess);
  317. FREE(info);
  318. return(NULL);
  319. } else {
  320. if ((nodeStat->oddTopDist != MAXSHORTINT) &&
  321. (nodeStat->oddBotDist != MAXSHORTINT))
  322. oddLen = (nodeStat->oddTopDist + nodeStat->oddBotDist);
  323. else
  324. oddLen = MAXSHORTINT;
  325. if ((nodeStat->evenTopDist != MAXSHORTINT) &&
  326. (nodeStat->evenBotDist != MAXSHORTINT))
  327. evenLen = (nodeStat->evenTopDist +nodeStat->evenBotDist);
  328. else
  329. evenLen = MAXSHORTINT;
  330. pathLength = (oddLen <= evenLen) ? oddLen : evenLen;
  331. if (pathLength > maxpath) {
  332. (void) fprintf(dd->err, "All computations are bogus, since root has path length greater than max path length within threshold %u, %u\n", maxpath, pathLength);
  333. dd->errorCode = CUDD_INTERNAL_ERROR;
  334. return(NULL);
  335. }
  336. }
  337. #ifdef DD_DEBUG
  338. gInfo.numCalls = 0;
  339. gInfo.hits = 0;
  340. gInfo.thishit = 0;
  341. #endif
  342. /* initialize a table to store computed nodes */
  343. if (hardlimit) {
  344. subsetNodeTable = st_init_table(st_ptrcmp, st_ptrhash);
  345. } else {
  346. subsetNodeTable = NIL(st_table);
  347. }
  348. subset = BuildSubsetBdd(dd, &gInfo, pathTable, f, info, subsetNodeTable);
  349. if (subset != NULL) {
  350. cuddRef(subset);
  351. }
  352. /* record the number of times a computed result for a node is hit */
  353. #ifdef DD_DEBUG
  354. (void) fprintf(dd->out, "Hits = %d, New==Node = %d, NumCalls = %d\n",
  355. gInfo.hits, gInfo.thishit, gInfo.numCalls);
  356. #endif
  357. if (subsetNodeTable != NIL(st_table)) {
  358. st_free_table(subsetNodeTable);
  359. }
  360. st_free_table(info->maxpathTable);
  361. st_foreach(pathTable, stPathTableDdFree, (char *)dd);
  362. FREE(info);
  363. } else {/* if threshold larger than size of dd */
  364. subset = f;
  365. cuddRef(subset);
  366. }
  367. FREE(excess);
  368. st_free_table(pathTable);
  369. FREE(pathLengthArray);
  370. for (i = 0; i <= gInfo.nodeDistPage; i++) FREE(gInfo.nodeDistPages[i]);
  371. FREE(gInfo.nodeDistPages);
  372. #ifdef DD_DEBUG
  373. /* check containment of subset in f */
  374. if (subset != NULL) {
  375. if (!Cudd_bddLeq(dd, subset, f)) {
  376. (void) fprintf(dd->err, "Wrong partition\n");
  377. dd->errorCode = CUDD_INTERNAL_ERROR;
  378. return(NULL);
  379. }
  380. }
  381. #endif
  382. if (subset != NULL) {
  383. cuddDeref(subset);
  384. return(subset);
  385. } else {
  386. return(NULL);
  387. }
  388. } /* end of cuddSubsetShortPaths */
  389. /*---------------------------------------------------------------------------*/
  390. /* Definition of static functions */
  391. /*---------------------------------------------------------------------------*/
  392. /**Function********************************************************************
  393. Synopsis [Resize the number of pages allocated to store the distances
  394. related to each node.]
  395. Description [Resize the number of pages allocated to store the distances
  396. related to each node. The procedure moves the counter to the
  397. next page when the end of the page is reached and allocates new
  398. pages when necessary. ]
  399. SideEffects [Changes the size of pages, page, page index, maximum
  400. number of pages freeing stuff in case of memory out. ]
  401. SeeAlso []
  402. ******************************************************************************/
  403. static void
  404. ResizeNodeDistPages(
  405. DdManager *dd /* DD manager */,
  406. GlobalInfo_t *gInfo /* global information */)
  407. {
  408. int i;
  409. NodeDist_t **newNodeDistPages;
  410. /* move to next page */
  411. gInfo->nodeDistPage++;
  412. /* If the current page index is larger than the number of pages
  413. * allocated, allocate a new page array. Page numbers are incremented by
  414. * INITIAL_PAGES
  415. */
  416. if (gInfo->nodeDistPage == gInfo->maxNodeDistPages) {
  417. newNodeDistPages = ALLOC(NodeDist_t *,gInfo->maxNodeDistPages + INITIAL_PAGES);
  418. if (newNodeDistPages == NULL) {
  419. for (i = 0; i < gInfo->nodeDistPage; i++) FREE(gInfo->nodeDistPages[i]);
  420. FREE(gInfo->nodeDistPages);
  421. dd->errorCode = CUDD_MEMORY_OUT;
  422. return;
  423. } else {
  424. for (i = 0; i < gInfo->maxNodeDistPages; i++) {
  425. newNodeDistPages[i] = gInfo->nodeDistPages[i];
  426. }
  427. /* Increase total page count */
  428. gInfo->maxNodeDistPages += INITIAL_PAGES;
  429. FREE(gInfo->nodeDistPages);
  430. gInfo->nodeDistPages = newNodeDistPages;
  431. }
  432. }
  433. /* Allocate a new page */
  434. gInfo->currentNodeDistPage = gInfo->nodeDistPages[gInfo->nodeDistPage] =
  435. ALLOC(NodeDist_t, gInfo->nodeDistPageSize);
  436. if (gInfo->currentNodeDistPage == NULL) {
  437. for (i = 0; i < gInfo->nodeDistPage; i++) FREE(gInfo->nodeDistPages[i]);
  438. FREE(gInfo->nodeDistPages);
  439. dd->errorCode = CUDD_MEMORY_OUT;
  440. return;
  441. }
  442. /* reset page index */
  443. gInfo->nodeDistPageIndex = 0;
  444. return;
  445. } /* end of ResizeNodeDistPages */
  446. /**Function********************************************************************
  447. Synopsis [Resize the number of pages allocated to store nodes in the BFS
  448. traversal of the Bdd .]
  449. Description [Resize the number of pages allocated to store nodes in the BFS
  450. traversal of the Bdd. The procedure moves the counter to the
  451. next page when the end of the page is reached and allocates new
  452. pages when necessary.]
  453. SideEffects [Changes the size of pages, page, page index, maximum
  454. number of pages freeing stuff in case of memory out. ]
  455. SeeAlso []
  456. ******************************************************************************/
  457. static void
  458. ResizeQueuePages(
  459. DdManager *dd /* DD manager */,
  460. GlobalInfo_t *gInfo /* global information */)
  461. {
  462. int i;
  463. DdNode ***newQueuePages;
  464. gInfo->queuePage++;
  465. /* If the current page index is larger than the number of pages
  466. * allocated, allocate a new page array. Page numbers are incremented by
  467. * INITIAL_PAGES
  468. */
  469. if (gInfo->queuePage == gInfo->maxQueuePages) {
  470. newQueuePages = ALLOC(DdNode **,gInfo->maxQueuePages + INITIAL_PAGES);
  471. if (newQueuePages == NULL) {
  472. for (i = 0; i < gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  473. FREE(gInfo->queuePages);
  474. dd->errorCode = CUDD_MEMORY_OUT;
  475. return;
  476. } else {
  477. for (i = 0; i < gInfo->maxQueuePages; i++) {
  478. newQueuePages[i] = gInfo->queuePages[i];
  479. }
  480. /* Increase total page count */
  481. gInfo->maxQueuePages += INITIAL_PAGES;
  482. FREE(gInfo->queuePages);
  483. gInfo->queuePages = newQueuePages;
  484. }
  485. }
  486. /* Allocate a new page */
  487. gInfo->currentQueuePage = gInfo->queuePages[gInfo->queuePage] =
  488. ALLOC(DdNode *,gInfo->queuePageSize);
  489. if (gInfo->currentQueuePage == NULL) {
  490. for (i = 0; i < gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  491. FREE(gInfo->queuePages);
  492. dd->errorCode = CUDD_MEMORY_OUT;
  493. return;
  494. }
  495. /* reset page index */
  496. gInfo->queuePageIndex = 0;
  497. return;
  498. } /* end of ResizeQueuePages */
  499. /**Function********************************************************************
  500. Synopsis [ Labels each node with its shortest distance from the root]
  501. Description [ Labels each node with its shortest distance from the root.
  502. This is done in a BFS search of the BDD. The nodes are processed
  503. in a queue implemented as pages(array) to reduce memory fragmentation.
  504. An entry is created for each node visited. The distance from the root
  505. to the node with the corresponding parity is updated. The procedure
  506. is called recursively each recusion level handling nodes at a given
  507. level from the root.]
  508. SideEffects [Creates entries in the pathTable]
  509. SeeAlso [CreatePathTable CreateBotDist]
  510. ******************************************************************************/
  511. static void
  512. CreateTopDist(
  513. DdManager *dd /* DD manager */,
  514. GlobalInfo_t *gInfo /* global information */,
  515. st_table * pathTable /* hast table to store path lengths */,
  516. int parentPage /* the pointer to the page on which the first parent in the queue is to be found. */,
  517. int parentQueueIndex /* pointer to the first parent on the page */,
  518. int topLen /* current distance from the root */,
  519. DdNode ** childPage /* pointer to the page on which the first child is to be added. */,
  520. int childQueueIndex /* pointer to the first child */,
  521. int numParents /* number of parents to process in this recursive call */,
  522. FILE *fp /* where to write messages */)
  523. {
  524. NodeDist_t *nodeStat;
  525. DdNode *N, *Nv, *Nnv, *node, *child, *regChild;
  526. int i;
  527. int processingDone, childrenCount;
  528. #ifdef DD_DEBUG
  529. gInfo->numCalls++;
  530. /* assume this procedure comes in with only the root node*/
  531. /* set queue index to the next available entry for addition */
  532. /* set queue page to page of addition */
  533. if ((gInfo->queuePages[parentPage] == childPage) && (parentQueueIndex ==
  534. childQueueIndex)) {
  535. fprintf(fp, "Should not happen that they are equal\n");
  536. }
  537. assert(gInfo->queuePageIndex == childQueueIndex);
  538. assert(gInfo->currentQueuePage == childPage);
  539. #endif
  540. /* number children added to queue is initialized , needed for
  541. * numParents in the next call
  542. */
  543. childrenCount = 0;
  544. /* process all the nodes in this level */
  545. while (numParents) {
  546. numParents--;
  547. if (parentQueueIndex == gInfo->queuePageSize) {
  548. parentPage++;
  549. parentQueueIndex = 0;
  550. }
  551. /* a parent to process */
  552. node = *(gInfo->queuePages[parentPage] + parentQueueIndex);
  553. parentQueueIndex++;
  554. /* get its children */
  555. N = Cudd_Regular(node);
  556. Nv = Cudd_T(N);
  557. Nnv = Cudd_E(N);
  558. Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
  559. Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));
  560. processingDone = 2;
  561. while (processingDone) {
  562. /* processing the THEN and the ELSE children, the THEN
  563. * child first
  564. */
  565. if (processingDone == 2) {
  566. child = Nv;
  567. } else {
  568. child = Nnv;
  569. }
  570. regChild = Cudd_Regular(child);
  571. /* dont process if the child is a constant */
  572. if (!Cudd_IsConstant(child)) {
  573. /* check is already visited, if not add a new entry in
  574. * the path Table
  575. */
  576. if (!st_lookup(pathTable, regChild, &nodeStat)) {
  577. /* if not in table, has never been visited */
  578. /* create entry for table */
  579. if (gInfo->nodeDistPageIndex == gInfo->nodeDistPageSize)
  580. ResizeNodeDistPages(dd, gInfo);
  581. if (dd->errorCode == CUDD_MEMORY_OUT) {
  582. for (i = 0; i <= gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  583. FREE(gInfo->queuePages);
  584. st_free_table(pathTable);
  585. return;
  586. }
  587. /* New entry for child in path Table is created here */
  588. nodeStat = gInfo->currentNodeDistPage + gInfo->nodeDistPageIndex;
  589. gInfo->nodeDistPageIndex++;
  590. /* Initialize fields of the node data */
  591. nodeStat->oddTopDist = MAXSHORTINT;
  592. nodeStat->evenTopDist = MAXSHORTINT;
  593. nodeStat->evenBotDist = MAXSHORTINT;
  594. nodeStat->oddBotDist = MAXSHORTINT;
  595. nodeStat->regResult = NULL;
  596. nodeStat->compResult = NULL;
  597. /* update the table entry element, the distance keeps
  598. * track of the parity of the path from the root
  599. */
  600. if (Cudd_IsComplement(child)) {
  601. nodeStat->oddTopDist = (DdHalfWord) topLen + 1;
  602. } else {
  603. nodeStat->evenTopDist = (DdHalfWord) topLen + 1;
  604. }
  605. /* insert entry element for child in the table */
  606. if (st_insert(pathTable, regChild,
  607. nodeStat) == ST_OUT_OF_MEM) {
  608. dd->errorCode = CUDD_MEMORY_OUT;
  609. for (i = 0; i <= gInfo->nodeDistPage; i++)
  610. FREE(gInfo->nodeDistPages[i]);
  611. FREE(gInfo->nodeDistPages);
  612. for (i = 0; i <= gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  613. FREE(gInfo->queuePages);
  614. st_free_table(pathTable);
  615. return;
  616. }
  617. /* Create list element for this child to process its children.
  618. * If this node has been processed already, then it appears
  619. * in the path table and hence is never added to the list
  620. * again.
  621. */
  622. if (gInfo->queuePageIndex == gInfo->queuePageSize) ResizeQueuePages(dd, gInfo);
  623. if (dd->errorCode == CUDD_MEMORY_OUT) {
  624. for (i = 0; i <= gInfo->nodeDistPage; i++)
  625. FREE(gInfo->nodeDistPages[i]);
  626. FREE(gInfo->nodeDistPages);
  627. st_free_table(pathTable);
  628. return;
  629. }
  630. *(gInfo->currentQueuePage + gInfo->queuePageIndex) = child;
  631. gInfo->queuePageIndex++;
  632. childrenCount++;
  633. } else {
  634. /* if not been met in a path with this parity before */
  635. /* put in list */
  636. if (((Cudd_IsComplement(child)) && (nodeStat->oddTopDist ==
  637. MAXSHORTINT)) || ((!Cudd_IsComplement(child)) &&
  638. (nodeStat->evenTopDist == MAXSHORTINT))) {
  639. if (gInfo->queuePageIndex == gInfo->queuePageSize) ResizeQueuePages(dd, gInfo);
  640. if (dd->errorCode == CUDD_MEMORY_OUT) {
  641. for (i = 0; i <= gInfo->nodeDistPage; i++)
  642. FREE(gInfo->nodeDistPages[i]);
  643. FREE(gInfo->nodeDistPages);
  644. st_free_table(pathTable);
  645. return;
  646. }
  647. *(gInfo->currentQueuePage + gInfo->queuePageIndex) = child;
  648. gInfo->queuePageIndex++;
  649. /* update the distance with the appropriate parity */
  650. if (Cudd_IsComplement(child)) {
  651. nodeStat->oddTopDist = (DdHalfWord) topLen + 1;
  652. } else {
  653. nodeStat->evenTopDist = (DdHalfWord) topLen + 1;
  654. }
  655. childrenCount++;
  656. }
  657. } /* end of else (not found in st_table) */
  658. } /*end of if Not constant child */
  659. processingDone--;
  660. } /*end of while processing Nv, Nnv */
  661. } /*end of while numParents */
  662. #ifdef DD_DEBUG
  663. assert(gInfo->queuePages[parentPage] == childPage);
  664. assert(parentQueueIndex == childQueueIndex);
  665. #endif
  666. if (childrenCount != 0) {
  667. topLen++;
  668. childPage = gInfo->currentQueuePage;
  669. childQueueIndex = gInfo->queuePageIndex;
  670. CreateTopDist(dd, gInfo, pathTable, parentPage, parentQueueIndex, topLen,
  671. childPage, childQueueIndex, childrenCount, fp);
  672. }
  673. return;
  674. } /* end of CreateTopDist */
  675. /**Function********************************************************************
  676. Synopsis [ Labels each node with the shortest distance from the constant.]
  677. Description [Labels each node with the shortest distance from the constant.
  678. This is done in a DFS search of the BDD. Each node has an odd
  679. and even parity distance from the sink (since there exists paths to both
  680. zero and one) which is less than MAXSHORTINT. At each node these distances
  681. are updated using the minimum distance of its children from the constant.
  682. SInce now both the length from the root and child is known, the minimum path
  683. length(length of the shortest path between the root and the constant that
  684. this node lies on) of this node can be calculated and used to update the
  685. pathLengthArray]
  686. SideEffects [Updates Path Table and path length array]
  687. SeeAlso [CreatePathTable CreateTopDist AssessPathLength]
  688. ******************************************************************************/
  689. static int
  690. CreateBotDist(
  691. DdNode * node /* current node */,
  692. st_table * pathTable /* path table with path lengths */,
  693. unsigned int * pathLengthArray /* array that stores number of nodes belonging to a particular path length. */,
  694. FILE *fp /* where to write messages */)
  695. {
  696. DdNode *N, *Nv, *Nnv;
  697. DdNode *realChild;
  698. DdNode *child, *regChild;
  699. NodeDist_t *nodeStat, *nodeStatChild;
  700. unsigned int oddLen, evenLen, pathLength;
  701. DdHalfWord botDist;
  702. int processingDone;
  703. if (Cudd_IsConstant(node))
  704. return(1);
  705. N = Cudd_Regular(node);
  706. /* each node has one table entry */
  707. /* update as you go down the min dist of each node from
  708. the root in each (odd and even) parity */
  709. if (!st_lookup(pathTable, N, &nodeStat)) {
  710. fprintf(fp, "Something wrong, the entry doesn't exist\n");
  711. return(0);
  712. }
  713. /* compute length of odd parity distances */
  714. if ((nodeStat->oddTopDist != MAXSHORTINT) &&
  715. (nodeStat->oddBotDist != MAXSHORTINT))
  716. oddLen = (nodeStat->oddTopDist + nodeStat->oddBotDist);
  717. else
  718. oddLen = MAXSHORTINT;
  719. /* compute length of even parity distances */
  720. if (!((nodeStat->evenTopDist == MAXSHORTINT) ||
  721. (nodeStat->evenBotDist == MAXSHORTINT)))
  722. evenLen = (nodeStat->evenTopDist +nodeStat->evenBotDist);
  723. else
  724. evenLen = MAXSHORTINT;
  725. /* assign pathlength to minimum of the two */
  726. pathLength = (oddLen <= evenLen) ? oddLen : evenLen;
  727. Nv = Cudd_T(N);
  728. Nnv = Cudd_E(N);
  729. /* process each child */
  730. processingDone = 0;
  731. while (processingDone != 2) {
  732. if (!processingDone) {
  733. child = Nv;
  734. } else {
  735. child = Nnv;
  736. }
  737. realChild = Cudd_NotCond(child, Cudd_IsComplement(node));
  738. regChild = Cudd_Regular(child);
  739. if (Cudd_IsConstant(realChild)) {
  740. /* Found a minterm; count parity and shortest distance
  741. ** from the constant.
  742. */
  743. if (Cudd_IsComplement(child))
  744. nodeStat->oddBotDist = 1;
  745. else
  746. nodeStat->evenBotDist = 1;
  747. } else {
  748. /* If node not in table, recur. */
  749. if (!st_lookup(pathTable, regChild, &nodeStatChild)) {
  750. fprintf(fp, "Something wrong, node in table should have been created in top dist proc.\n");
  751. return(0);
  752. }
  753. if (nodeStatChild->oddBotDist == MAXSHORTINT) {
  754. if (nodeStatChild->evenBotDist == MAXSHORTINT) {
  755. if (!CreateBotDist(realChild, pathTable, pathLengthArray, fp))
  756. return(0);
  757. } else {
  758. fprintf(fp, "Something wrong, both bot nodeStats should be there\n");
  759. return(0);
  760. }
  761. }
  762. /* Update shortest distance from the constant depending on
  763. ** parity. */
  764. if (Cudd_IsComplement(child)) {
  765. /* If parity on the edge then add 1 to even distance
  766. ** of child to get odd parity distance and add 1 to
  767. ** odd distance of child to get even parity
  768. ** distance. Change distance of current node only if
  769. ** the calculated distance is less than existing
  770. ** distance. */
  771. if (nodeStatChild->oddBotDist != MAXSHORTINT)
  772. botDist = nodeStatChild->oddBotDist + 1;
  773. else
  774. botDist = MAXSHORTINT;
  775. if (nodeStat->evenBotDist > botDist )
  776. nodeStat->evenBotDist = botDist;
  777. if (nodeStatChild->evenBotDist != MAXSHORTINT)
  778. botDist = nodeStatChild->evenBotDist + 1;
  779. else
  780. botDist = MAXSHORTINT;
  781. if (nodeStat->oddBotDist > botDist)
  782. nodeStat->oddBotDist = botDist;
  783. } else {
  784. /* If parity on the edge then add 1 to even distance
  785. ** of child to get even parity distance and add 1 to
  786. ** odd distance of child to get odd parity distance.
  787. ** Change distance of current node only if the
  788. ** calculated distance is lesser than existing
  789. ** distance. */
  790. if (nodeStatChild->evenBotDist != MAXSHORTINT)
  791. botDist = nodeStatChild->evenBotDist + 1;
  792. else
  793. botDist = MAXSHORTINT;
  794. if (nodeStat->evenBotDist > botDist)
  795. nodeStat->evenBotDist = botDist;
  796. if (nodeStatChild->oddBotDist != MAXSHORTINT)
  797. botDist = nodeStatChild->oddBotDist + 1;
  798. else
  799. botDist = MAXSHORTINT;
  800. if (nodeStat->oddBotDist > botDist)
  801. nodeStat->oddBotDist = botDist;
  802. }
  803. } /* end of else (if not constant child ) */
  804. processingDone++;
  805. } /* end of while processing Nv, Nnv */
  806. /* Compute shortest path length on the fly. */
  807. if ((nodeStat->oddTopDist != MAXSHORTINT) &&
  808. (nodeStat->oddBotDist != MAXSHORTINT))
  809. oddLen = (nodeStat->oddTopDist + nodeStat->oddBotDist);
  810. else
  811. oddLen = MAXSHORTINT;
  812. if ((nodeStat->evenTopDist != MAXSHORTINT) &&
  813. (nodeStat->evenBotDist != MAXSHORTINT))
  814. evenLen = (nodeStat->evenTopDist +nodeStat->evenBotDist);
  815. else
  816. evenLen = MAXSHORTINT;
  817. /* Update path length array that has number of nodes of a particular
  818. ** path length. */
  819. if (oddLen < pathLength ) {
  820. if (pathLength != MAXSHORTINT)
  821. pathLengthArray[pathLength]--;
  822. if (oddLen != MAXSHORTINT)
  823. pathLengthArray[oddLen]++;
  824. pathLength = oddLen;
  825. }
  826. if (evenLen < pathLength ) {
  827. if (pathLength != MAXSHORTINT)
  828. pathLengthArray[pathLength]--;
  829. if (evenLen != MAXSHORTINT)
  830. pathLengthArray[evenLen]++;
  831. }
  832. return(1);
  833. } /*end of CreateBotDist */
  834. /**Function********************************************************************
  835. Synopsis [ The outer procedure to label each node with its shortest
  836. distance from the root and constant]
  837. Description [ The outer procedure to label each node with its shortest
  838. distance from the root and constant. Calls CreateTopDist and CreateBotDist.
  839. The basis for computing the distance between root and constant is that
  840. the distance may be the sum of even distances from the node to the root
  841. and constant or the sum of odd distances from the node to the root and
  842. constant. Both CreateTopDist and CreateBotDist create the odd and
  843. even parity distances from the root and constant respectively.]
  844. SideEffects [None]
  845. SeeAlso [CreateTopDist CreateBotDist]
  846. ******************************************************************************/
  847. static st_table *
  848. CreatePathTable(
  849. DdManager *dd /* DD manager */,
  850. GlobalInfo_t *gInfo /* global information */,
  851. DdNode * node /* root of function */,
  852. unsigned int * pathLengthArray /* array of path lengths to store nodes labeled with the various path lengths */,
  853. FILE *fp /* where to write messages */)
  854. {
  855. st_table *pathTable;
  856. NodeDist_t *nodeStat;
  857. DdHalfWord topLen;
  858. DdNode *N;
  859. int i, numParents;
  860. int insertValue;
  861. DdNode **childPage;
  862. int parentPage;
  863. int childQueueIndex, parentQueueIndex;
  864. /* Creating path Table for storing data about nodes */
  865. pathTable = st_init_table(st_ptrcmp,st_ptrhash);
  866. /* initializing pages for info about each node */
  867. gInfo->maxNodeDistPages = INITIAL_PAGES;
  868. gInfo->nodeDistPages = ALLOC(NodeDist_t *, gInfo->maxNodeDistPages);
  869. if (gInfo->nodeDistPages == NULL) {
  870. goto OUT_OF_MEM;
  871. }
  872. gInfo->nodeDistPage = 0;
  873. gInfo->currentNodeDistPage = gInfo->nodeDistPages[gInfo->nodeDistPage] =
  874. ALLOC(NodeDist_t, gInfo->nodeDistPageSize);
  875. if (gInfo->currentNodeDistPage == NULL) {
  876. for (i = 0; i <= gInfo->nodeDistPage; i++) FREE(gInfo->nodeDistPages[i]);
  877. FREE(gInfo->nodeDistPages);
  878. goto OUT_OF_MEM;
  879. }
  880. gInfo->nodeDistPageIndex = 0;
  881. /* Initializing pages for the BFS search queue, implemented as an array. */
  882. gInfo->maxQueuePages = INITIAL_PAGES;
  883. gInfo->queuePages = ALLOC(DdNode **, gInfo->maxQueuePages);
  884. if (gInfo->queuePages == NULL) {
  885. goto OUT_OF_MEM;
  886. }
  887. gInfo->queuePage = 0;
  888. gInfo->currentQueuePage = gInfo->queuePages[gInfo->queuePage] =
  889. ALLOC(DdNode *, gInfo->queuePageSize);
  890. if (gInfo->currentQueuePage == NULL) {
  891. for (i = 0; i <= gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  892. FREE(gInfo->queuePages);
  893. goto OUT_OF_MEM;
  894. }
  895. gInfo->queuePageIndex = 0;
  896. /* Enter the root node into the queue to start with. */
  897. parentPage = gInfo->queuePage;
  898. parentQueueIndex = gInfo->queuePageIndex;
  899. topLen = 0;
  900. *(gInfo->currentQueuePage + gInfo->queuePageIndex) = node;
  901. gInfo->queuePageIndex++;
  902. childPage = gInfo->currentQueuePage;
  903. childQueueIndex = gInfo->queuePageIndex;
  904. N = Cudd_Regular(node);
  905. if (gInfo->nodeDistPageIndex == gInfo->nodeDistPageSize) ResizeNodeDistPages(dd, gInfo);
  906. if (dd->errorCode == CUDD_MEMORY_OUT) {
  907. for (i = 0; i <= gInfo->nodeDistPage; i++) FREE(gInfo->nodeDistPages[i]);
  908. FREE(gInfo->nodeDistPages);
  909. for (i = 0; i <= gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  910. FREE(gInfo->queuePages);
  911. st_free_table(pathTable);
  912. goto OUT_OF_MEM;
  913. }
  914. nodeStat = gInfo->currentNodeDistPage + gInfo->nodeDistPageIndex;
  915. gInfo->nodeDistPageIndex++;
  916. nodeStat->oddTopDist = MAXSHORTINT;
  917. nodeStat->evenTopDist = MAXSHORTINT;
  918. nodeStat->evenBotDist = MAXSHORTINT;
  919. nodeStat->oddBotDist = MAXSHORTINT;
  920. nodeStat->regResult = NULL;
  921. nodeStat->compResult = NULL;
  922. insertValue = st_insert(pathTable, N, nodeStat);
  923. if (insertValue == ST_OUT_OF_MEM) {
  924. dd->errorCode = CUDD_MEMORY_OUT;
  925. for (i = 0; i <= gInfo->nodeDistPage; i++) FREE(gInfo->nodeDistPages[i]);
  926. FREE(gInfo->nodeDistPages);
  927. for (i = 0; i <= gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  928. FREE(gInfo->queuePages);
  929. st_free_table(pathTable);
  930. goto OUT_OF_MEM;
  931. } else if (insertValue == 1) {
  932. fprintf(fp, "Something wrong, the entry exists but didnt show up in st_lookup\n");
  933. return(NULL);
  934. }
  935. if (Cudd_IsComplement(node)) {
  936. nodeStat->oddTopDist = 0;
  937. } else {
  938. nodeStat->evenTopDist = 0;
  939. }
  940. numParents = 1;
  941. /* call the function that counts the distance of each node from the
  942. * root
  943. */
  944. #ifdef DD_DEBUG
  945. gInfo->numCalls = 0;
  946. #endif
  947. CreateTopDist(dd, gInfo, pathTable, parentPage, parentQueueIndex, (int) topLen,
  948. childPage, childQueueIndex, numParents, fp);
  949. if (dd->errorCode == CUDD_MEMORY_OUT) {
  950. fprintf(fp, "Out of Memory and cant count path lengths\n");
  951. goto OUT_OF_MEM;
  952. }
  953. #ifdef DD_DEBUG
  954. gInfo->numCalls = 0;
  955. #endif
  956. /* call the function that counts the distance of each node from the
  957. * constant
  958. */
  959. if (!CreateBotDist(node, pathTable, pathLengthArray, fp)) return(NULL);
  960. /* free BFS queue pages as no longer required */
  961. for (i = 0; i <= gInfo->queuePage; i++) FREE(gInfo->queuePages[i]);
  962. FREE(gInfo->queuePages);
  963. return(pathTable);
  964. OUT_OF_MEM:
  965. (void) fprintf(fp, "Out of Memory, cannot allocate pages\n");
  966. dd->errorCode = CUDD_MEMORY_OUT;
  967. return(NULL);
  968. } /*end of CreatePathTable */
  969. /**Function********************************************************************
  970. Synopsis [Chooses the maximum allowable path length of nodes under the
  971. threshold.]
  972. Description [Chooses the maximum allowable path length under each node.
  973. The corner cases are when the threshold is larger than the number
  974. of nodes in the BDD iself, in which case 'numVars + 1' is returned.
  975. If all nodes of a particular path length are needed, then the
  976. maxpath returned is the next one with excess nodes = 0;]
  977. SideEffects [None]
  978. SeeAlso []
  979. ******************************************************************************/
  980. static unsigned int
  981. AssessPathLength(
  982. unsigned int * pathLengthArray /* array determining number of nodes belonging to the different path lengths */,
  983. int threshold /* threshold to determine maximum allowable nodes in the subset */,
  984. int numVars /* maximum number of variables */,
  985. unsigned int * excess /* number of nodes labeled maxpath required in the subset */,
  986. FILE *fp /* where to write messages */)
  987. {
  988. unsigned int i, maxpath;
  989. int temp;
  990. temp = threshold;
  991. i = 0;
  992. maxpath = 0;
  993. /* quit loop if i reaches max number of variables or if temp reaches
  994. * below zero
  995. */
  996. while ((i < (unsigned) numVars+1) && (temp > 0)) {
  997. if (pathLengthArray[i] > 0) {
  998. maxpath = i;
  999. temp = temp - pathLengthArray[i];
  1000. }
  1001. i++;
  1002. }
  1003. /* if all nodes of max path are needed */
  1004. if (temp >= 0) {
  1005. maxpath++; /* now maxpath becomes the next maxppath or max number
  1006. of variables */
  1007. *excess = 0;
  1008. } else { /* normal case when subset required is less than size of
  1009. original BDD */
  1010. *excess = temp + pathLengthArray[maxpath];
  1011. }
  1012. if (maxpath == 0) {
  1013. fprintf(fp, "Path Length array seems to be all zeroes, check\n");
  1014. }
  1015. return(maxpath);
  1016. } /* end of AssessPathLength */
  1017. /**Function********************************************************************
  1018. Synopsis [Builds the BDD with nodes labeled with path length less than or equal to maxpath]
  1019. Description [Builds the BDD with nodes labeled with path length
  1020. under maxpath and as many nodes labeled maxpath as determined by the
  1021. threshold. The procedure uses the path table to determine which nodes
  1022. in the original bdd need to be retained. This procedure picks a
  1023. shortest path (tie break decided by taking the child with the shortest
  1024. distance to the constant) and recurs down the path till it reaches the
  1025. constant. the procedure then starts building the subset upward from
  1026. the constant. All nodes labeled by path lengths less than the given
  1027. maxpath are used to build the subset. However, in the case of nodes
  1028. that have label equal to maxpath, as many are chosen as required by
  1029. the threshold. This number is stored in the info structure in the
  1030. field thresholdReached. This field is decremented whenever a node
  1031. labeled maxpath is encountered and the nodes labeled maxpath are
  1032. aggregated in a maxpath table. As soon as the thresholdReached count
  1033. goes to 0, the shortest path from this node to the constant is found.
  1034. The extraction of nodes with the above labeling is based on the fact
  1035. that each node, labeled with a path length, P, has at least one child
  1036. labeled P or less. So extracting all nodes labeled a given path length
  1037. P ensures complete paths between the root and the constant. Extraction
  1038. of a partial number of nodes with a given path length may result in
  1039. incomplete paths and hence the additional number of nodes are grabbed
  1040. to complete the path. Since the Bdd is built bottom-up, other nodes
  1041. labeled maxpath do lie on complete paths. The procedure may cause the
  1042. subset to have a larger or smaller number of nodes than the specified
  1043. threshold. The increase in the number of nodes is caused by the
  1044. building of a subset and the reduction by recombination. However in
  1045. most cases, the recombination overshadows the increase and the
  1046. procedure returns a result with lower number of nodes than specified.
  1047. The subsetNodeTable is NIL when there is no hard limit on the number
  1048. of nodes. Further efforts towards keeping the subset closer to the
  1049. threshold number were abandoned in favour of keeping the procedure
  1050. simple and fast.]
  1051. SideEffects [SubsetNodeTable is changed if it is not NIL.]
  1052. SeeAlso []
  1053. ******************************************************************************/
  1054. static DdNode *
  1055. BuildSubsetBdd(
  1056. DdManager * dd /* DD manager */,
  1057. GlobalInfo_t *gInfo /* global information */,
  1058. st_table * pathTable /* path table with path lengths and computed results */,
  1059. DdNode * node /* current node */,
  1060. struct AssortedInfo * info /* assorted information structure */,
  1061. st_table * subsetNodeTable /* table storing computed results */)
  1062. {
  1063. DdNode *N, *Nv, *Nnv;
  1064. DdNode *ThenBranch, *ElseBranch, *childBranch;
  1065. DdNode *child, *regChild, *regNnv, *regNv;
  1066. NodeDist_t *nodeStatNv, *nodeStat, *nodeStatNnv;
  1067. DdNode *neW, *topv, *regNew;
  1068. char *entry;
  1069. unsigned int topid;
  1070. unsigned int childPathLength, oddLen, evenLen, NnvPathLength, NvPathLength;
  1071. unsigned int NvBotDist, NnvBotDist;
  1072. int tiebreakChild;
  1073. int processingDone, thenDone, elseDone;
  1074. DdNode *zero = Cudd_Not(DD_ONE(dd));
  1075. #ifdef DD_DEBUG
  1076. gInfo->numCalls++;
  1077. #endif
  1078. if (Cudd_IsConstant(node))
  1079. return(node);
  1080. N = Cudd_Regular(node);
  1081. /* Find node in table. */
  1082. if (!st_lookup(pathTable, N, &nodeStat)) {
  1083. (void) fprintf(dd->err, "Something wrong, node must be in table \n");
  1084. dd->errorCode = CUDD_INTERNAL_ERROR;
  1085. return(NULL);
  1086. }
  1087. /* If the node in the table has been visited, then return the corresponding
  1088. ** Dd. Since a node can become a subset of itself, its
  1089. ** complement (that is te same node reached by a different parity) will
  1090. ** become a superset of the original node and result in some minterms
  1091. ** that were not in the original set. Hence two different results are
  1092. ** maintained, corresponding to the odd and even parities.
  1093. */
  1094. /* If this node is reached with an odd parity, get odd parity results. */
  1095. if (Cudd_IsComplement(node)) {
  1096. if (nodeStat->compResult != NULL) {
  1097. #ifdef DD_DEBUG
  1098. gInfo->hits++;
  1099. #endif
  1100. return(nodeStat->compResult);
  1101. }
  1102. } else {
  1103. /* if this node is reached with an even parity, get even parity
  1104. * results
  1105. */
  1106. if (nodeStat->regResult != NULL) {
  1107. #ifdef DD_DEBUG
  1108. gInfo->hits++;
  1109. #endif
  1110. return(nodeStat->regResult);
  1111. }
  1112. }
  1113. /* get children */
  1114. Nv = Cudd_T(N);
  1115. Nnv = Cudd_E(N);
  1116. Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
  1117. Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));
  1118. /* no child processed */
  1119. processingDone = 0;
  1120. /* then child not processed */
  1121. thenDone = 0;
  1122. ThenBranch = NULL;
  1123. /* else child not processed */
  1124. elseDone = 0;
  1125. ElseBranch = NULL;
  1126. /* if then child constant, branch is the child */
  1127. if (Cudd_IsConstant(Nv)) {
  1128. /*shortest path found */
  1129. if ((Nv == DD_ONE(dd)) && (info->findShortestPath)) {
  1130. info->findShortestPath = 0;
  1131. }
  1132. ThenBranch = Nv;
  1133. cuddRef(ThenBranch);
  1134. if (ThenBranch == NULL) {
  1135. return(NULL);
  1136. }
  1137. thenDone++;
  1138. processingDone++;
  1139. NvBotDist = MAXSHORTINT;
  1140. } else {
  1141. /* Derive regular child for table lookup. */
  1142. regNv = Cudd_Regular(Nv);
  1143. /* Get node data for shortest path length. */
  1144. if (!st_lookup(pathTable, regNv, &nodeStatNv) ) {
  1145. (void) fprintf(dd->err, "Something wrong, node must be in table\n");
  1146. dd->errorCode = CUDD_INTERNAL_ERROR;
  1147. return(NULL);
  1148. }
  1149. /* Derive shortest path length for child. */
  1150. if ((nodeStatNv->oddTopDist != MAXSHORTINT) &&
  1151. (nodeStatNv->oddBotDist != MAXSHORTINT)) {
  1152. oddLen = (nodeStatNv->oddTopDist + nodeStatNv->oddBotDist);
  1153. } else {
  1154. oddLen = MAXSHORTINT;
  1155. }
  1156. if ((nodeStatNv->evenTopDist != MAXSHORTINT) &&
  1157. (nodeStatNv->evenBotDist != MAXSHORTINT)) {
  1158. evenLen = (nodeStatNv->evenTopDist +nodeStatNv->evenBotDist);
  1159. } else {
  1160. evenLen = MAXSHORTINT;
  1161. }
  1162. NvPathLength = (oddLen <= evenLen) ? oddLen : evenLen;
  1163. NvBotDist = (oddLen <= evenLen) ? nodeStatNv->oddBotDist:
  1164. nodeStatNv->evenBotDist;
  1165. }
  1166. /* if else child constant, branch is the child */
  1167. if (Cudd_IsConstant(Nnv)) {
  1168. /*shortest path found */
  1169. if ((Nnv == DD_ONE(dd)) && (info->findShortestPath)) {
  1170. info->findShortestPath = 0;
  1171. }
  1172. ElseBranch = Nnv;
  1173. cuddRef(ElseBranch);
  1174. if (ElseBranch == NULL) {
  1175. return(NULL);
  1176. }
  1177. elseDone++;
  1178. processingDone++;
  1179. NnvBotDist = MAXSHORTINT;
  1180. } else {
  1181. /* Derive regular child for table lookup. */
  1182. regNnv = Cudd_Regular(Nnv);
  1183. /* Get node data for shortest path length. */
  1184. if (!st_lookup(pathTable, regNnv, &nodeStatNnv) ) {
  1185. (void) fprintf(dd->err, "Something wrong, node must be in table\n");
  1186. dd->errorCode = CUDD_INTERNAL_ERROR;
  1187. return(NULL);
  1188. }
  1189. /* Derive shortest path length for child. */
  1190. if ((nodeStatNnv->oddTopDist != MAXSHORTINT) &&
  1191. (nodeStatNnv->oddBotDist != MAXSHORTINT)) {
  1192. oddLen = (nodeStatNnv->oddTopDist + nodeStatNnv->oddBotDist);
  1193. } else {
  1194. oddLen = MAXSHORTINT;
  1195. }
  1196. if ((nodeStatNnv->evenTopDist != MAXSHORTINT) &&
  1197. (nodeStatNnv->evenBotDist != MAXSHORTINT)) {
  1198. evenLen = (nodeStatNnv->evenTopDist +nodeStatNnv->evenBotDist);
  1199. } else {
  1200. evenLen = MAXSHORTINT;
  1201. }
  1202. NnvPathLength = (oddLen <= evenLen) ? oddLen : evenLen;
  1203. NnvBotDist = (oddLen <= evenLen) ? nodeStatNnv->oddBotDist :
  1204. nodeStatNnv->evenBotDist;
  1205. }
  1206. tiebreakChild = (NvBotDist <= NnvBotDist) ? 1 : 0;
  1207. /* while both children not processed */
  1208. while (processingDone != 2) {
  1209. if (!processingDone) {
  1210. /* if no child processed */
  1211. /* pick the child with shortest path length and record which one
  1212. * picked
  1213. */
  1214. if ((NvPathLength < NnvPathLength) ||
  1215. ((NvPathLength == NnvPathLength) && (tiebreakChild == 1))) {
  1216. child = Nv;
  1217. regChild = regNv;
  1218. thenDone = 1;
  1219. childPathLength = NvPathLength;
  1220. } else {
  1221. child = Nnv;
  1222. regChild = regNnv;
  1223. elseDone = 1;
  1224. childPathLength = NnvPathLength;
  1225. } /* then path length less than else path length */
  1226. } else {
  1227. /* if one child processed, process the other */
  1228. if (thenDone) {
  1229. child = Nnv;
  1230. regChild = regNnv;
  1231. elseDone = 1;
  1232. childPathLength = NnvPathLength;
  1233. } else {
  1234. child = Nv;
  1235. regChild = regNv;
  1236. thenDone = 1;
  1237. childPathLength = NvPathLength;
  1238. } /* end of else pick the Then child if ELSE child processed */
  1239. } /* end of else one child has been processed */
  1240. /* ignore (replace with constant 0) all nodes which lie on paths larger
  1241. * than the maximum length of the path required
  1242. */
  1243. if (childPathLength > info->maxpath) {
  1244. /* record nodes visited */
  1245. childBranch = zero;
  1246. } else {
  1247. if (childPathLength < info->maxpath) {
  1248. if (info->findShortestPath) {
  1249. info->findShortestPath = 0;
  1250. }
  1251. childBranch = BuildSubsetBdd(dd, gInfo, pathTable, child, info,
  1252. subsetNodeTable);
  1253. } else { /* Case: path length of node = maxpath */
  1254. /* If the node labeled with maxpath is found in the
  1255. ** maxpathTable, use it to build the subset BDD. */
  1256. if (st_lookup(info->maxpathTable, regChild, &entry)) {
  1257. /* When a node that is already been chosen is hit,
  1258. ** the quest for a complete path is over. */
  1259. if (info->findShortestPath) {
  1260. info->findShortestPath = 0;
  1261. }
  1262. childBranch = BuildSubsetBdd(dd, gInfo, pathTable, child, info,
  1263. subsetNodeTable);
  1264. } else {
  1265. /* If node is not found in the maxpathTable and
  1266. ** the threshold has been reached, then if the
  1267. ** path needs to be completed, continue. Else
  1268. ** replace the node with a zero. */
  1269. if (info->thresholdReached <= 0) {
  1270. if (info->findShortestPath) {
  1271. if (st_insert(info->maxpathTable, regChild,
  1272. NULL) == ST_OUT_OF_MEM) {
  1273. dd->errorCode = CUDD_MEMORY_OUT;
  1274. (void) fprintf(dd->err, "OUT of memory\n");
  1275. info->thresholdReached = 0;
  1276. childBranch = zero;
  1277. } else {
  1278. info->thresholdReached--;
  1279. childBranch = BuildSubsetBdd(dd, gInfo, pathTable,
  1280. child, info,subsetNodeTable);
  1281. }
  1282. } else { /* not find shortest path, we dont need this
  1283. node */
  1284. childBranch = zero;
  1285. }
  1286. } else { /* Threshold hasn't been reached,
  1287. ** need the node. */
  1288. if (st_insert(info->maxpathTable, regChild,
  1289. NULL) == ST_OUT_OF_MEM) {
  1290. dd->errorCode = CUDD_MEMORY_OUT;
  1291. (void) fprintf(dd->err, "OUT of memory\n");
  1292. info->thresholdReached = 0;
  1293. childBranch = zero;
  1294. } else {
  1295. info->thresholdReached--;
  1296. if (info->thresholdReached <= 0) {
  1297. info->findShortestPath = 1;
  1298. }
  1299. childBranch = BuildSubsetBdd(dd, gInfo, pathTable,
  1300. child, info, subsetNodeTable);
  1301. } /* end of st_insert successful */
  1302. } /* end of threshold hasnt been reached yet */
  1303. } /* end of else node not found in maxpath table */
  1304. } /* end of if (path length of node = maxpath) */
  1305. } /* end if !(childPathLength > maxpath) */
  1306. if (childBranch == NULL) {
  1307. /* deref other stuff incase reordering has taken place */
  1308. if (ThenBranch != NULL) {
  1309. Cudd_RecursiveDeref(dd, ThenBranch);
  1310. ThenBranch = NULL;
  1311. }
  1312. if (ElseBranch != NULL) {
  1313. Cudd_RecursiveDeref(dd, ElseBranch);
  1314. ElseBranch = NULL;
  1315. }
  1316. return(NULL);
  1317. }
  1318. cuddRef(childBranch);
  1319. if (child == Nv) {
  1320. ThenBranch = childBranch;
  1321. } else {
  1322. ElseBranch = childBranch;
  1323. }
  1324. processingDone++;
  1325. } /*end of while processing Nv, Nnv */
  1326. info->findShortestPath = 0;
  1327. topid = Cudd_NodeReadIndex(N);
  1328. topv = Cudd_ReadVars(dd, topid);
  1329. cuddRef(topv);
  1330. neW = cuddBddIteRecur(dd, topv, ThenBranch, ElseBranch);
  1331. if (neW != NULL) {
  1332. cuddRef(neW);
  1333. }
  1334. Cudd_RecursiveDeref(dd, topv);
  1335. Cudd_RecursiveDeref(dd, ThenBranch);
  1336. Cudd_RecursiveDeref(dd, ElseBranch);
  1337. /* Hard Limit of threshold has been imposed */
  1338. if (subsetNodeTable != NIL(st_table)) {
  1339. /* check if a new node is created */
  1340. regNew = Cudd_Regular(neW);
  1341. /* subset node table keeps all new nodes that have been created to keep
  1342. * a running count of how many nodes have been built in the subset.
  1343. */
  1344. if (!st_lookup(subsetNodeTable, regNew, &entry)) {
  1345. if (!Cudd_IsConstant(regNew)) {
  1346. if (st_insert(subsetNodeTable, regNew,
  1347. NULL) == ST_OUT_OF_MEM) {
  1348. (void) fprintf(dd->err, "Out of memory\n");
  1349. return (NULL);
  1350. }
  1351. if (st_count(subsetNodeTable) > info->threshold) {
  1352. info->thresholdReached = 0;
  1353. }
  1354. }
  1355. }
  1356. }
  1357. if (neW == NULL) {
  1358. return(NULL);
  1359. } else {
  1360. /*store computed result in regular form*/
  1361. if (Cudd_IsComplement(node)) {
  1362. nodeStat->compResult = neW;
  1363. cuddRef(nodeStat->compResult);
  1364. /* if the new node is the same as the corresponding node in the
  1365. * original bdd then its complement need not be computed as it
  1366. * cannot be larger than the node itself
  1367. */
  1368. if (neW == node) {
  1369. #ifdef DD_DEBUG
  1370. gInfo->thishit++;
  1371. #endif
  1372. /* if a result for the node has already been computed, then
  1373. * it can only be smaller than teh node itself. hence store
  1374. * the node result in order not to break recombination
  1375. */
  1376. if (nodeStat->regResult != NULL) {
  1377. Cudd_RecursiveDeref(dd, nodeStat->regResult);
  1378. }
  1379. nodeStat->regResult = Cudd_Not(neW);
  1380. cuddRef(nodeStat->regResult);
  1381. }
  1382. } else {
  1383. nodeStat->regResult = neW;
  1384. cuddRef(nodeStat->regResult);
  1385. if (neW == node) {
  1386. #ifdef DD_DEBUG
  1387. gInfo->thishit++;
  1388. #endif
  1389. if (nodeStat->compResult != NULL) {
  1390. Cudd_RecursiveDeref(dd, nodeStat->compResult);
  1391. }
  1392. nodeStat->compResult = Cudd_Not(neW);
  1393. cuddRef(nodeStat->compResult);
  1394. }
  1395. }
  1396. cuddDeref(neW);
  1397. return(neW);
  1398. } /* end of else i.e. Subset != NULL */
  1399. } /* end of BuildSubsetBdd */
  1400. /**Function********************************************************************
  1401. Synopsis [Procedure to free te result dds stored in the NodeDist pages.]
  1402. Description [None]
  1403. SideEffects [None]
  1404. SeeAlso []
  1405. ******************************************************************************/
  1406. static enum st_retval
  1407. stPathTableDdFree(
  1408. char * key,
  1409. char * value,
  1410. char * arg)
  1411. {
  1412. NodeDist_t *nodeStat;
  1413. DdManager *dd;
  1414. nodeStat = (NodeDist_t *)value;
  1415. dd = (DdManager *)arg;
  1416. if (nodeStat->regResult != NULL) {
  1417. Cudd_RecursiveDeref(dd, nodeStat->regResult);
  1418. }
  1419. if (nodeStat->compResult != NULL) {
  1420. Cudd_RecursiveDeref(dd, nodeStat->compResult);
  1421. }
  1422. return(ST_CONTINUE);
  1423. } /* end of stPathTableFree */