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.

1580 lines
39 KiB

3 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Procedures for dynamic variable ordering of ZDDs.
  5. @author Hyong-Kyoon Shin, In-Ho Moon
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include "util.h"
  35. #include "mtrInt.h"
  36. #include "cuddInt.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. #define DD_MAX_SUBTABLE_SPARSITY 8
  41. /*---------------------------------------------------------------------------*/
  42. /* Stucture declarations */
  43. /*---------------------------------------------------------------------------*/
  44. /*---------------------------------------------------------------------------*/
  45. /* Type declarations */
  46. /*---------------------------------------------------------------------------*/
  47. /*---------------------------------------------------------------------------*/
  48. /* Variable declarations */
  49. /*---------------------------------------------------------------------------*/
  50. /*---------------------------------------------------------------------------*/
  51. /* Macro declarations */
  52. /*---------------------------------------------------------------------------*/
  53. /** \cond */
  54. /*---------------------------------------------------------------------------*/
  55. /* Static function prototypes */
  56. /*---------------------------------------------------------------------------*/
  57. static Move * zddSwapAny (DdManager *table, int x, int y);
  58. static int cuddZddSiftingAux (DdManager *table, int x, int x_low, int x_high);
  59. static Move * cuddZddSiftingUp (DdManager *table, int x, int x_low, int initial_size);
  60. static Move * cuddZddSiftingDown (DdManager *table, int x, int x_high, int initial_size);
  61. static int cuddZddSiftingBackward (DdManager *table, Move *moves, int size);
  62. static void zddReorderPreprocess (DdManager *table);
  63. static int zddReorderPostprocess (DdManager *table);
  64. static int zddShuffle (DdManager *table, int *permutation);
  65. static int zddSiftUp (DdManager *table, int x, int xLow);
  66. static void zddFixTree (DdManager *table, MtrNode *treenode);
  67. /** \endcond */
  68. /*---------------------------------------------------------------------------*/
  69. /* Definition of exported functions */
  70. /*---------------------------------------------------------------------------*/
  71. /**
  72. @brief Main dynamic reordering routine for ZDDs.
  73. @details@parblock
  74. Calls one of the possible reordering procedures:
  75. <ul>
  76. <li>Swapping
  77. <li>Sifting
  78. <li>Symmetric Sifting
  79. </ul>
  80. For sifting and symmetric sifting it is possible to request reordering
  81. to convergence.
  82. The core of all methods is the reordering procedure
  83. cuddZddSwapInPlace() which swaps two adjacent variables.
  84. @endparblock
  85. @return 1 in case of success; 0 otherwise. In the case of symmetric
  86. sifting (with and without convergence) returns 1 plus the number of
  87. symmetric variables, in case of success.
  88. @sideeffect Changes the variable order for all ZDDs and clears
  89. the cache.
  90. */
  91. int
  92. Cudd_zddReduceHeap(
  93. DdManager * table /**< DD manager */,
  94. Cudd_ReorderingType heuristic /**< method used for reordering */,
  95. int minsize /**< bound below which no reordering occurs */)
  96. {
  97. DdHook *hook;
  98. int result;
  99. unsigned int nextDyn;
  100. #ifdef DD_STATS
  101. unsigned int initialSize;
  102. unsigned int finalSize;
  103. #endif
  104. unsigned long localTime;
  105. /* Don't reorder if there are too many dead nodes. */
  106. if (table->keysZ - table->deadZ < (unsigned) minsize)
  107. return(1);
  108. if (heuristic == CUDD_REORDER_SAME) {
  109. heuristic = table->autoMethodZ;
  110. }
  111. if (heuristic == CUDD_REORDER_NONE) {
  112. return(1);
  113. }
  114. /* This call to Cudd_zddReduceHeap does initiate reordering. Therefore
  115. ** we count it.
  116. */
  117. table->reorderings++;
  118. localTime = util_cpu_time();
  119. /* Run the hook functions. */
  120. hook = table->preReorderingHook;
  121. while (hook != NULL) {
  122. int res = (hook->f)(table, "ZDD", (void *)heuristic);
  123. if (res == 0) return(0);
  124. hook = hook->next;
  125. }
  126. /* Clear the cache and collect garbage. */
  127. zddReorderPreprocess(table);
  128. table->zddTotalNumberSwapping = 0;
  129. #ifdef DD_STATS
  130. initialSize = table->keysZ;
  131. switch(heuristic) {
  132. case CUDD_REORDER_RANDOM:
  133. case CUDD_REORDER_RANDOM_PIVOT:
  134. (void) fprintf(table->out,"#:I_RANDOM ");
  135. break;
  136. case CUDD_REORDER_SIFT:
  137. case CUDD_REORDER_SIFT_CONVERGE:
  138. case CUDD_REORDER_SYMM_SIFT:
  139. case CUDD_REORDER_SYMM_SIFT_CONV:
  140. (void) fprintf(table->out,"#:I_SIFTING ");
  141. break;
  142. case CUDD_REORDER_LINEAR:
  143. case CUDD_REORDER_LINEAR_CONVERGE:
  144. (void) fprintf(table->out,"#:I_LINSIFT ");
  145. break;
  146. default:
  147. (void) fprintf(table->err,"Unsupported ZDD reordering method\n");
  148. return(0);
  149. }
  150. (void) fprintf(table->out,"%8d: initial size",initialSize);
  151. #endif
  152. result = cuddZddTreeSifting(table,heuristic);
  153. #ifdef DD_STATS
  154. (void) fprintf(table->out,"\n");
  155. finalSize = table->keysZ;
  156. (void) fprintf(table->out,"#:F_REORDER %8d: final size\n",finalSize);
  157. (void) fprintf(table->out,"#:T_REORDER %8g: total time (sec)\n",
  158. ((double)(util_cpu_time() - localTime)/1000.0));
  159. (void) fprintf(table->out,"#:N_REORDER %8d: total swaps\n",
  160. table->zddTotalNumberSwapping);
  161. #endif
  162. if (result == 0)
  163. return(0);
  164. if (!zddReorderPostprocess(table))
  165. return(0);
  166. if (table->realignZ) {
  167. if (!cuddBddAlignToZdd(table))
  168. return(0);
  169. }
  170. nextDyn = table->keysZ * DD_DYN_RATIO;
  171. if (table->reorderings < 20 || nextDyn > table->nextDyn)
  172. table->nextDyn = nextDyn;
  173. else
  174. table->nextDyn += 20;
  175. table->reordered = 1;
  176. /* Run hook functions. */
  177. hook = table->postReorderingHook;
  178. while (hook != NULL) {
  179. int res = (hook->f)(table, "ZDD", (void *)(ptruint)localTime);
  180. if (res == 0) return(0);
  181. hook = hook->next;
  182. }
  183. /* Update cumulative reordering time. */
  184. table->reordTime += util_cpu_time() - localTime;
  185. return(result);
  186. } /* end of Cudd_zddReduceHeap */
  187. /**
  188. @brief Reorders %ZDD variables according to given permutation.
  189. @details The i-th entry of the permutation array contains the index
  190. of the variable that should be brought to the i-th level. The size
  191. of the array should be equal or greater to the number of variables
  192. currently in use.
  193. @return 1 in case of success; 0 otherwise.
  194. @sideeffect Changes the %ZDD variable order for all diagrams and clears
  195. the cache.
  196. @see Cudd_zddReduceHeap
  197. */
  198. int
  199. Cudd_zddShuffleHeap(
  200. DdManager * table /**< DD manager */,
  201. int * permutation /**< required variable permutation */)
  202. {
  203. int result;
  204. zddReorderPreprocess(table);
  205. result = zddShuffle(table,permutation);
  206. if (!zddReorderPostprocess(table)) return(0);
  207. return(result);
  208. } /* end of Cudd_zddShuffleHeap */
  209. /*---------------------------------------------------------------------------*/
  210. /* Definition of internal functions */
  211. /*---------------------------------------------------------------------------*/
  212. /**
  213. @brief Reorders %ZDD variables according to the order of the %BDD
  214. variables.
  215. @details This function can be called at the end of %BDD reordering to
  216. insure that the order of the %ZDD variables is consistent with the
  217. order of the %BDD variables. The number of %ZDD variables must be a
  218. multiple of the number of %BDD variables. Let <code>M</code> be the
  219. ratio of the two numbers. cuddZddAlignToBdd then considers the %ZDD
  220. variables from <code>M*i</code> to <code>(M+1)*i-1</code> as
  221. corresponding to %BDD variable <code>i</code>. This function should
  222. be normally called from Cudd_ReduceHeap, which clears the cache.
  223. @return 1 in case of success; 0 otherwise.
  224. @sideeffect Changes the %ZDD variable order for all diagrams and performs
  225. garbage collection of the %ZDD unique table.
  226. @see Cudd_zddShuffleHeap Cudd_ReduceHeap
  227. */
  228. int
  229. cuddZddAlignToBdd(
  230. DdManager * table /**< %DD manager */)
  231. {
  232. int *invpermZ; /* permutation array */
  233. int M; /* ratio of ZDD variables to BDD variables */
  234. int i,j; /* loop indices */
  235. int result; /* return value */
  236. /* We assume that a ratio of 0 is OK. */
  237. if (table->sizeZ == 0)
  238. return(1);
  239. M = table->sizeZ / table->size;
  240. /* Check whether the number of ZDD variables is a multiple of the
  241. ** number of BDD variables.
  242. */
  243. if (M * table->size != table->sizeZ)
  244. return(0);
  245. /* Create and initialize the inverse permutation array. */
  246. invpermZ = ALLOC(int,table->sizeZ);
  247. if (invpermZ == NULL) {
  248. table->errorCode = CUDD_MEMORY_OUT;
  249. return(0);
  250. }
  251. for (i = 0; i < table->size; i++) {
  252. int index = table->invperm[i];
  253. int indexZ = index * M;
  254. int levelZ = table->permZ[indexZ];
  255. levelZ = (levelZ / M) * M;
  256. for (j = 0; j < M; j++) {
  257. invpermZ[M * i + j] = table->invpermZ[levelZ + j];
  258. }
  259. }
  260. /* Eliminate dead nodes. Do not scan the cache again, because we
  261. ** assume that Cudd_ReduceHeap has already cleared it.
  262. */
  263. cuddGarbageCollect(table,0);
  264. result = zddShuffle(table, invpermZ);
  265. FREE(invpermZ);
  266. /* Fix the ZDD variable group tree. */
  267. zddFixTree(table,table->treeZ);
  268. return(result);
  269. } /* end of cuddZddAlignToBdd */
  270. /**
  271. @brief Finds the next subtable with a larger index.
  272. @return the index.
  273. @sideeffect None
  274. */
  275. int
  276. cuddZddNextHigh(
  277. DdManager * table,
  278. int x)
  279. {
  280. (void) table; /* avoid warning */
  281. return(x + 1);
  282. } /* end of cuddZddNextHigh */
  283. /**
  284. @brief Finds the next subtable with a smaller index.
  285. @return the index.
  286. @sideeffect None
  287. */
  288. int
  289. cuddZddNextLow(
  290. DdManager * table,
  291. int x)
  292. {
  293. (void) table; /* avoid warning */
  294. return(x - 1);
  295. } /* end of cuddZddNextLow */
  296. /**
  297. @brief Comparison function used by qsort.
  298. @details Comparison function used by qsort to order the variables
  299. according to the number of keys in the subtables.
  300. @return the difference in number of keys between the two variables
  301. being compared.
  302. @sideeffect None
  303. */
  304. int
  305. cuddZddUniqueCompare(
  306. void const * ptr_x,
  307. void const * ptr_y)
  308. {
  309. IndexKey const * pX = (IndexKey const *) ptr_x;
  310. IndexKey const * pY = (IndexKey const *) ptr_y;
  311. return(pY->keys - pX->keys);
  312. } /* end of cuddZddUniqueCompare */
  313. /**
  314. @brief Swaps two adjacent variables.
  315. @details It assumes that no dead nodes are present on entry to this
  316. procedure. The procedure then guarantees that no dead nodes will be
  317. present when it terminates. cuddZddSwapInPlace assumes that x &lt; y.
  318. @return the number of keys in the table if successful; 0 otherwise.
  319. @sideeffect None
  320. */
  321. int
  322. cuddZddSwapInPlace(
  323. DdManager * table,
  324. int x,
  325. int y)
  326. {
  327. DdNodePtr *xlist, *ylist;
  328. int xindex, yindex;
  329. int xslots, yslots;
  330. int xshift, yshift;
  331. int oldxkeys, oldykeys;
  332. int newxkeys, newykeys;
  333. int i;
  334. int posn;
  335. DdNode *f, *f1, *f0, *f11, *f10, *f01, *f00;
  336. DdNode *newf1 = NULL, *newf0 = NULL, *next;
  337. DdNodePtr g, *lastP, *previousP;
  338. DdNode *empty = table->zero;
  339. #ifdef DD_DEBUG
  340. assert(x < y);
  341. assert(cuddZddNextHigh(table,x) == y);
  342. assert(table->subtableZ[x].keys != 0);
  343. assert(table->subtableZ[y].keys != 0);
  344. assert(table->subtableZ[x].dead == 0);
  345. assert(table->subtableZ[y].dead == 0);
  346. #endif
  347. table->zddTotalNumberSwapping++;
  348. /* Get parameters of x subtable. */
  349. xindex = table->invpermZ[x];
  350. xlist = table->subtableZ[x].nodelist;
  351. oldxkeys = table->subtableZ[x].keys;
  352. xslots = table->subtableZ[x].slots;
  353. xshift = table->subtableZ[x].shift;
  354. newxkeys = 0;
  355. yindex = table->invpermZ[y];
  356. ylist = table->subtableZ[y].nodelist;
  357. oldykeys = table->subtableZ[y].keys;
  358. yslots = table->subtableZ[y].slots;
  359. yshift = table->subtableZ[y].shift;
  360. newykeys = oldykeys;
  361. /* The nodes in the x layer that don't depend on y directly
  362. ** will stay there; the others are put in a chain.
  363. ** The chain is handled as a FIFO; g points to the beginning and
  364. ** last points to the end.
  365. */
  366. g = NULL;
  367. lastP = &g;
  368. for (i = 0; i < xslots; i++) {
  369. previousP = &(xlist[i]);
  370. f = *previousP;
  371. while (f != NULL) {
  372. next = f->next;
  373. f1 = cuddT(f); f0 = cuddE(f);
  374. if ((f1->index != (DdHalfWord) yindex) &&
  375. (f0->index != (DdHalfWord) yindex)) { /* stays */
  376. newxkeys++;
  377. *previousP = f;
  378. previousP = &(f->next);
  379. } else {
  380. f->index = yindex;
  381. *lastP = f;
  382. lastP = &(f->next);
  383. }
  384. f = next;
  385. } /* while there are elements in the collision chain */
  386. *previousP = NULL;
  387. } /* for each slot of the x subtable */
  388. *lastP = NULL;
  389. #ifdef DD_COUNT
  390. table->swapSteps += oldxkeys - newxkeys;
  391. #endif
  392. /* Take care of the x nodes that must be re-expressed.
  393. ** They form a linked list pointed by g. Their index has been
  394. ** changed to yindex already.
  395. */
  396. f = g;
  397. while (f != NULL) {
  398. next = f->next;
  399. /* Find f1, f0, f11, f10, f01, f00. */
  400. f1 = cuddT(f);
  401. if ((int) f1->index == yindex) {
  402. f11 = cuddT(f1); f10 = cuddE(f1);
  403. } else {
  404. f11 = empty; f10 = f1;
  405. }
  406. f0 = cuddE(f);
  407. if ((int) f0->index == yindex) {
  408. f01 = cuddT(f0); f00 = cuddE(f0);
  409. } else {
  410. f01 = empty; f00 = f0;
  411. }
  412. /* Decrease ref count of f1. */
  413. cuddSatDec(f1->ref);
  414. /* Create the new T child. */
  415. if (f11 == empty) {
  416. if (f01 != empty) {
  417. newf1 = f01;
  418. cuddSatInc(newf1->ref);
  419. }
  420. /* else case was already handled when finding nodes
  421. ** with both children below level y
  422. */
  423. } else {
  424. /* Check xlist for triple (xindex, f11, f01). */
  425. posn = ddHash(f11, f01, xshift);
  426. /* For each element newf1 in collision list xlist[posn]. */
  427. newf1 = xlist[posn];
  428. while (newf1 != NULL) {
  429. if (cuddT(newf1) == f11 && cuddE(newf1) == f01) {
  430. cuddSatInc(newf1->ref);
  431. break; /* match */
  432. }
  433. newf1 = newf1->next;
  434. } /* while newf1 */
  435. if (newf1 == NULL) { /* no match */
  436. newf1 = cuddDynamicAllocNode(table);
  437. if (newf1 == NULL)
  438. goto zddSwapOutOfMem;
  439. newf1->index = xindex; newf1->ref = 1;
  440. cuddT(newf1) = f11;
  441. cuddE(newf1) = f01;
  442. /* Insert newf1 in the collision list xlist[pos];
  443. ** increase the ref counts of f11 and f01
  444. */
  445. newxkeys++;
  446. newf1->next = xlist[posn];
  447. xlist[posn] = newf1;
  448. cuddSatInc(f11->ref);
  449. cuddSatInc(f01->ref);
  450. }
  451. }
  452. cuddT(f) = newf1;
  453. /* Do the same for f0. */
  454. /* Decrease ref count of f0. */
  455. cuddSatDec(f0->ref);
  456. /* Create the new E child. */
  457. if (f10 == empty) {
  458. newf0 = f00;
  459. cuddSatInc(newf0->ref);
  460. } else {
  461. /* Check xlist for triple (xindex, f10, f00). */
  462. posn = ddHash(f10, f00, xshift);
  463. /* For each element newf0 in collision list xlist[posn]. */
  464. newf0 = xlist[posn];
  465. while (newf0 != NULL) {
  466. if (cuddT(newf0) == f10 && cuddE(newf0) == f00) {
  467. cuddSatInc(newf0->ref);
  468. break; /* match */
  469. }
  470. newf0 = newf0->next;
  471. } /* while newf0 */
  472. if (newf0 == NULL) { /* no match */
  473. newf0 = cuddDynamicAllocNode(table);
  474. if (newf0 == NULL)
  475. goto zddSwapOutOfMem;
  476. newf0->index = xindex; newf0->ref = 1;
  477. cuddT(newf0) = f10; cuddE(newf0) = f00;
  478. /* Insert newf0 in the collision list xlist[posn];
  479. ** increase the ref counts of f10 and f00.
  480. */
  481. newxkeys++;
  482. newf0->next = xlist[posn];
  483. xlist[posn] = newf0;
  484. cuddSatInc(f10->ref);
  485. cuddSatInc(f00->ref);
  486. }
  487. }
  488. cuddE(f) = newf0;
  489. /* Insert the modified f in ylist.
  490. ** The modified f does not already exists in ylist.
  491. ** (Because of the uniqueness of the cofactors.)
  492. */
  493. posn = ddHash(newf1, newf0, yshift);
  494. newykeys++;
  495. f->next = ylist[posn];
  496. ylist[posn] = f;
  497. f = next;
  498. } /* while f != NULL */
  499. /* GC the y layer. */
  500. /* For each node f in ylist. */
  501. for (i = 0; i < yslots; i++) {
  502. previousP = &(ylist[i]);
  503. f = *previousP;
  504. while (f != NULL) {
  505. next = f->next;
  506. if (f->ref == 0) {
  507. cuddSatDec(cuddT(f)->ref);
  508. cuddSatDec(cuddE(f)->ref);
  509. cuddDeallocNode(table, f);
  510. newykeys--;
  511. } else {
  512. *previousP = f;
  513. previousP = &(f->next);
  514. }
  515. f = next;
  516. } /* while f */
  517. *previousP = NULL;
  518. } /* for i */
  519. /* Set the appropriate fields in table. */
  520. table->subtableZ[x].nodelist = ylist;
  521. table->subtableZ[x].slots = yslots;
  522. table->subtableZ[x].shift = yshift;
  523. table->subtableZ[x].keys = newykeys;
  524. table->subtableZ[x].maxKeys = yslots * DD_MAX_SUBTABLE_DENSITY;
  525. table->subtableZ[y].nodelist = xlist;
  526. table->subtableZ[y].slots = xslots;
  527. table->subtableZ[y].shift = xshift;
  528. table->subtableZ[y].keys = newxkeys;
  529. table->subtableZ[y].maxKeys = xslots * DD_MAX_SUBTABLE_DENSITY;
  530. table->permZ[xindex] = y; table->permZ[yindex] = x;
  531. table->invpermZ[x] = yindex; table->invpermZ[y] = xindex;
  532. table->keysZ += newxkeys + newykeys - oldxkeys - oldykeys;
  533. /* Update univ section; univ[x] remains the same. */
  534. table->univ[y] = cuddT(table->univ[x]);
  535. return (table->keysZ);
  536. zddSwapOutOfMem:
  537. (void) fprintf(table->err, "Error: cuddZddSwapInPlace out of memory\n");
  538. return (0);
  539. } /* end of cuddZddSwapInPlace */
  540. /**
  541. @brief Reorders variables by a sequence of (non-adjacent) swaps.
  542. @details Implementation of Plessier's algorithm that reorders
  543. variables by a sequence of (non-adjacent) swaps.
  544. <ol>
  545. <li> Select two variables (RANDOM or HEURISTIC).
  546. <li> Permute these variables.
  547. <li> If the nodes have decreased accept the permutation.
  548. <li> Otherwise reconstruct the original heap.
  549. <li> Loop.
  550. </ol>
  551. @return 1 in case of success; 0 otherwise.
  552. @sideeffect None
  553. */
  554. int
  555. cuddZddSwapping(
  556. DdManager * table,
  557. int lower,
  558. int upper,
  559. Cudd_ReorderingType heuristic)
  560. {
  561. int i, j;
  562. int max, keys;
  563. int nvars;
  564. int x, y;
  565. int iterate;
  566. int previousSize;
  567. Move *moves, *move;
  568. int pivot = 0;
  569. int modulo;
  570. int result;
  571. #ifdef DD_DEBUG
  572. /* Sanity check */
  573. assert(lower >= 0 && upper < table->sizeZ && lower <= upper);
  574. #endif
  575. nvars = upper - lower + 1;
  576. iterate = nvars;
  577. for (i = 0; i < iterate; i++) {
  578. if (heuristic == CUDD_REORDER_RANDOM_PIVOT) {
  579. /* Find pivot <= id with maximum keys. */
  580. for (max = -1, j = lower; j <= upper; j++) {
  581. if ((keys = table->subtableZ[j].keys) > max) {
  582. max = keys;
  583. pivot = j;
  584. }
  585. }
  586. modulo = upper - pivot;
  587. if (modulo == 0) {
  588. y = pivot; /* y = nvars-1 */
  589. } else {
  590. /* y = random # from {pivot+1 .. nvars-1} */
  591. y = pivot + 1 + (int) (Cudd_Random(table) % modulo);
  592. }
  593. modulo = pivot - lower - 1;
  594. if (modulo < 1) { /* if pivot = 1 or 0 */
  595. x = lower;
  596. } else {
  597. do { /* x = random # from {0 .. pivot-2} */
  598. x = (int) Cudd_Random(table) % modulo;
  599. } while (x == y);
  600. /* Is this condition really needed, since x and y
  601. are in regions separated by pivot? */
  602. }
  603. } else {
  604. x = (int) (Cudd_Random(table) % nvars) + lower;
  605. do {
  606. y = (int) (Cudd_Random(table) % nvars) + lower;
  607. } while (x == y);
  608. }
  609. previousSize = table->keysZ;
  610. moves = zddSwapAny(table, x, y);
  611. if (moves == NULL)
  612. goto cuddZddSwappingOutOfMem;
  613. result = cuddZddSiftingBackward(table, moves, previousSize);
  614. if (!result)
  615. goto cuddZddSwappingOutOfMem;
  616. while (moves != NULL) {
  617. move = moves->next;
  618. cuddDeallocMove(table, moves);
  619. moves = move;
  620. }
  621. #ifdef DD_STATS
  622. if (table->keysZ < (unsigned) previousSize) {
  623. (void) fprintf(table->out,"-");
  624. } else if (table->keysZ > (unsigned) previousSize) {
  625. (void) fprintf(table->out,"+"); /* should never happen */
  626. } else {
  627. (void) fprintf(table->out,"=");
  628. }
  629. fflush(table->out);
  630. #endif
  631. }
  632. return(1);
  633. cuddZddSwappingOutOfMem:
  634. while (moves != NULL) {
  635. move = moves->next;
  636. cuddDeallocMove(table, moves);
  637. moves = move;
  638. }
  639. return(0);
  640. } /* end of cuddZddSwapping */
  641. /**
  642. @brief Implementation of Rudell's sifting algorithm.
  643. @details Assumes that no dead nodes are present.
  644. <ol>
  645. <li> Order all the variables according to the number of entries
  646. in each unique table.
  647. <li> Sift the variable up and down, remembering each time the
  648. total size of the %DD heap.
  649. <li> Select the best permutation.
  650. <li> Repeat 3 and 4 for all variables.
  651. </ol>
  652. @return 1 if successful; 0 otherwise.
  653. @sideeffect None
  654. */
  655. int
  656. cuddZddSifting(
  657. DdManager * table,
  658. int lower,
  659. int upper)
  660. {
  661. int i;
  662. IndexKey *var;
  663. int size;
  664. int x;
  665. int result;
  666. #ifdef DD_STATS
  667. int previousSize;
  668. #endif
  669. size = table->sizeZ;
  670. /* Find order in which to sift variables. */
  671. var = ALLOC(IndexKey, size);
  672. if (var == NULL) {
  673. table->errorCode = CUDD_MEMORY_OUT;
  674. goto cuddZddSiftingOutOfMem;
  675. }
  676. for (i = 0; i < size; i++) {
  677. x = table->permZ[i];
  678. var[i].index = i;
  679. var[i].keys = table->subtableZ[x].keys;
  680. }
  681. util_qsort(var, size, sizeof(IndexKey), cuddZddUniqueCompare);
  682. /* Now sift. */
  683. for (i = 0; i < ddMin(table->siftMaxVar, size); i++) {
  684. if (table->zddTotalNumberSwapping >= table->siftMaxSwap)
  685. break;
  686. if (util_cpu_time() - table->startTime > table->timeLimit) {
  687. table->autoDynZ = 0; /* prevent further reordering */
  688. break;
  689. }
  690. if (table->terminationCallback != NULL &&
  691. table->terminationCallback(table->tcbArg)) {
  692. table->autoDynZ = 0; /* prevent further reordering */
  693. break;
  694. }
  695. x = table->permZ[var[i].index];
  696. if (x < lower || x > upper) continue;
  697. #ifdef DD_STATS
  698. previousSize = table->keysZ;
  699. #endif
  700. result = cuddZddSiftingAux(table, x, lower, upper);
  701. if (!result)
  702. goto cuddZddSiftingOutOfMem;
  703. #ifdef DD_STATS
  704. if (table->keysZ < (unsigned) previousSize) {
  705. (void) fprintf(table->out,"-");
  706. } else if (table->keysZ > (unsigned) previousSize) {
  707. (void) fprintf(table->out,"+"); /* should never happen */
  708. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ , var[i].index);
  709. } else {
  710. (void) fprintf(table->out,"=");
  711. }
  712. fflush(table->out);
  713. #endif
  714. }
  715. FREE(var);
  716. return(1);
  717. cuddZddSiftingOutOfMem:
  718. if (var != NULL) FREE(var);
  719. return(0);
  720. } /* end of cuddZddSifting */
  721. /*---------------------------------------------------------------------------*/
  722. /* Definition of static functions */
  723. /*---------------------------------------------------------------------------*/
  724. /**
  725. @brief Swaps any two variables.
  726. @return the set of moves.
  727. @sideeffect None
  728. */
  729. static Move *
  730. zddSwapAny(
  731. DdManager * table,
  732. int x,
  733. int y)
  734. {
  735. Move *move, *moves;
  736. int tmp, size;
  737. int x_ref, y_ref;
  738. int x_next, y_next;
  739. int limit_size;
  740. if (x > y) { /* make x precede y */
  741. tmp = x; x = y; y = tmp;
  742. }
  743. x_ref = x; y_ref = y;
  744. x_next = cuddZddNextHigh(table, x);
  745. y_next = cuddZddNextLow(table, y);
  746. moves = NULL;
  747. limit_size = table->keysZ;
  748. for (;;) {
  749. if (x_next == y_next) { /* x < x_next = y_next < y */
  750. size = cuddZddSwapInPlace(table, x, x_next);
  751. if (size == 0)
  752. goto zddSwapAnyOutOfMem;
  753. move = (Move *) cuddDynamicAllocNode(table);
  754. if (move == NULL)
  755. goto zddSwapAnyOutOfMem;
  756. move->x = x;
  757. move->y = x_next;
  758. move->size = size;
  759. move->next = moves;
  760. moves = move;
  761. size = cuddZddSwapInPlace(table, y_next, y);
  762. if (size == 0)
  763. goto zddSwapAnyOutOfMem;
  764. move = (Move *)cuddDynamicAllocNode(table);
  765. if (move == NULL)
  766. goto zddSwapAnyOutOfMem;
  767. move->x = y_next;
  768. move->y = y;
  769. move->size = size;
  770. move->next = moves;
  771. moves = move;
  772. size = cuddZddSwapInPlace(table, x, x_next);
  773. if (size == 0)
  774. goto zddSwapAnyOutOfMem;
  775. move = (Move *)cuddDynamicAllocNode(table);
  776. if (move == NULL)
  777. goto zddSwapAnyOutOfMem;
  778. move->x = x;
  779. move->y = x_next;
  780. move->size = size;
  781. move->next = moves;
  782. moves = move;
  783. tmp = x; x = y; y = tmp;
  784. } else if (x == y_next) { /* x = y_next < y = x_next */
  785. size = cuddZddSwapInPlace(table, x, x_next);
  786. if (size == 0)
  787. goto zddSwapAnyOutOfMem;
  788. move = (Move *)cuddDynamicAllocNode(table);
  789. if (move == NULL)
  790. goto zddSwapAnyOutOfMem;
  791. move->x = x;
  792. move->y = x_next;
  793. move->size = size;
  794. move->next = moves;
  795. moves = move;
  796. tmp = x; x = y; y = tmp;
  797. } else {
  798. size = cuddZddSwapInPlace(table, x, x_next);
  799. if (size == 0)
  800. goto zddSwapAnyOutOfMem;
  801. move = (Move *)cuddDynamicAllocNode(table);
  802. if (move == NULL)
  803. goto zddSwapAnyOutOfMem;
  804. move->x = x;
  805. move->y = x_next;
  806. move->size = size;
  807. move->next = moves;
  808. moves = move;
  809. size = cuddZddSwapInPlace(table, y_next, y);
  810. if (size == 0)
  811. goto zddSwapAnyOutOfMem;
  812. move = (Move *)cuddDynamicAllocNode(table);
  813. if (move == NULL)
  814. goto zddSwapAnyOutOfMem;
  815. move->x = y_next;
  816. move->y = y;
  817. move->size = size;
  818. move->next = moves;
  819. moves = move;
  820. x = x_next; y = y_next;
  821. }
  822. x_next = cuddZddNextHigh(table, x);
  823. y_next = cuddZddNextLow(table, y);
  824. if (x_next > y_ref)
  825. break; /* if x == y_ref */
  826. if ((double) size > table->maxGrowth * (double) limit_size)
  827. break;
  828. if (size < limit_size)
  829. limit_size = size;
  830. }
  831. if (y_next >= x_ref) {
  832. size = cuddZddSwapInPlace(table, y_next, y);
  833. if (size == 0)
  834. goto zddSwapAnyOutOfMem;
  835. move = (Move *)cuddDynamicAllocNode(table);
  836. if (move == NULL)
  837. goto zddSwapAnyOutOfMem;
  838. move->x = y_next;
  839. move->y = y;
  840. move->size = size;
  841. move->next = moves;
  842. moves = move;
  843. }
  844. return(moves);
  845. zddSwapAnyOutOfMem:
  846. while (moves != NULL) {
  847. move = moves->next;
  848. cuddDeallocMove(table, moves);
  849. moves = move;
  850. }
  851. return(NULL);
  852. } /* end of zddSwapAny */
  853. /**
  854. @brief Given xLow <= x <= xHigh moves x up and down between the
  855. boundaries.
  856. @details Finds the best position and does the required changes.
  857. @return 1 if successful; 0 otherwise.
  858. @sideeffect None
  859. */
  860. static int
  861. cuddZddSiftingAux(
  862. DdManager * table,
  863. int x,
  864. int x_low,
  865. int x_high)
  866. {
  867. Move *move;
  868. Move *moveUp; /* list of up move */
  869. Move *moveDown; /* list of down move */
  870. int initial_size;
  871. int result;
  872. initial_size = table->keysZ;
  873. #ifdef DD_DEBUG
  874. assert(table->subtableZ[x].keys > 0);
  875. #endif
  876. moveDown = NULL;
  877. moveUp = NULL;
  878. if (x == x_low) {
  879. moveDown = cuddZddSiftingDown(table, x, x_high, initial_size);
  880. /* after that point x --> x_high */
  881. if (moveDown == NULL)
  882. goto cuddZddSiftingAuxOutOfMem;
  883. result = cuddZddSiftingBackward(table, moveDown,
  884. initial_size);
  885. /* move backward and stop at best position */
  886. if (!result)
  887. goto cuddZddSiftingAuxOutOfMem;
  888. }
  889. else if (x == x_high) {
  890. moveUp = cuddZddSiftingUp(table, x, x_low, initial_size);
  891. /* after that point x --> x_low */
  892. if (moveUp == NULL)
  893. goto cuddZddSiftingAuxOutOfMem;
  894. result = cuddZddSiftingBackward(table, moveUp, initial_size);
  895. /* move backward and stop at best position */
  896. if (!result)
  897. goto cuddZddSiftingAuxOutOfMem;
  898. }
  899. else if ((x - x_low) > (x_high - x)) {
  900. /* must go down first:shorter */
  901. moveDown = cuddZddSiftingDown(table, x, x_high, initial_size);
  902. /* after that point x --> x_high */
  903. if (moveDown == NULL)
  904. goto cuddZddSiftingAuxOutOfMem;
  905. moveUp = cuddZddSiftingUp(table, moveDown->y, x_low,
  906. initial_size);
  907. if (moveUp == NULL)
  908. goto cuddZddSiftingAuxOutOfMem;
  909. result = cuddZddSiftingBackward(table, moveUp, initial_size);
  910. /* move backward and stop at best position */
  911. if (!result)
  912. goto cuddZddSiftingAuxOutOfMem;
  913. }
  914. else {
  915. moveUp = cuddZddSiftingUp(table, x, x_low, initial_size);
  916. /* after that point x --> x_high */
  917. if (moveUp == NULL)
  918. goto cuddZddSiftingAuxOutOfMem;
  919. moveDown = cuddZddSiftingDown(table, moveUp->x, x_high,
  920. initial_size);
  921. /* then move up */
  922. if (moveDown == NULL)
  923. goto cuddZddSiftingAuxOutOfMem;
  924. result = cuddZddSiftingBackward(table, moveDown,
  925. initial_size);
  926. /* move backward and stop at best position */
  927. if (!result)
  928. goto cuddZddSiftingAuxOutOfMem;
  929. }
  930. while (moveDown != NULL) {
  931. move = moveDown->next;
  932. cuddDeallocMove(table, moveDown);
  933. moveDown = move;
  934. }
  935. while (moveUp != NULL) {
  936. move = moveUp->next;
  937. cuddDeallocMove(table, moveUp);
  938. moveUp = move;
  939. }
  940. return(1);
  941. cuddZddSiftingAuxOutOfMem:
  942. while (moveDown != NULL) {
  943. move = moveDown->next;
  944. cuddDeallocMove(table, moveDown);
  945. moveDown = move;
  946. }
  947. while (moveUp != NULL) {
  948. move = moveUp->next;
  949. cuddDeallocMove(table, moveUp);
  950. moveUp = move;
  951. }
  952. return(0);
  953. } /* end of cuddZddSiftingAux */
  954. /**
  955. @brief Sifts a variable up.
  956. @details Moves y up until either it reaches the bound (x_low) or the
  957. size of the %ZDD heap increases too much.
  958. @return the set of moves in case of success; NULL if memory is full.
  959. @sideeffect None
  960. */
  961. static Move *
  962. cuddZddSiftingUp(
  963. DdManager * table,
  964. int x,
  965. int x_low,
  966. int initial_size)
  967. {
  968. Move *moves;
  969. Move *move;
  970. int y;
  971. int size;
  972. int limit_size = initial_size;
  973. moves = NULL;
  974. y = cuddZddNextLow(table, x);
  975. while (y >= x_low) {
  976. size = cuddZddSwapInPlace(table, y, x);
  977. if (size == 0)
  978. goto cuddZddSiftingUpOutOfMem;
  979. move = (Move *)cuddDynamicAllocNode(table);
  980. if (move == NULL)
  981. goto cuddZddSiftingUpOutOfMem;
  982. move->x = y;
  983. move->y = x;
  984. move->size = size;
  985. move->next = moves;
  986. moves = move;
  987. if ((double)size > (double)limit_size * table->maxGrowth)
  988. break;
  989. if (size < limit_size)
  990. limit_size = size;
  991. x = y;
  992. y = cuddZddNextLow(table, x);
  993. }
  994. return(moves);
  995. cuddZddSiftingUpOutOfMem:
  996. while (moves != NULL) {
  997. move = moves->next;
  998. cuddDeallocMove(table, moves);
  999. moves = move;
  1000. }
  1001. return(NULL);
  1002. } /* end of cuddZddSiftingUp */
  1003. /**
  1004. @brief Sifts a variable down.
  1005. @details Moves x down until either it reaches the bound (x_high) or
  1006. the size of the %ZDD heap increases too much.
  1007. @return the set of moves in case of success; NULL if memory is
  1008. full.
  1009. @sideeffect None
  1010. */
  1011. static Move *
  1012. cuddZddSiftingDown(
  1013. DdManager * table,
  1014. int x,
  1015. int x_high,
  1016. int initial_size)
  1017. {
  1018. Move *moves;
  1019. Move *move;
  1020. int y;
  1021. int size;
  1022. int limit_size = initial_size;
  1023. moves = NULL;
  1024. y = cuddZddNextHigh(table, x);
  1025. while (y <= x_high) {
  1026. size = cuddZddSwapInPlace(table, x, y);
  1027. if (size == 0)
  1028. goto cuddZddSiftingDownOutOfMem;
  1029. move = (Move *)cuddDynamicAllocNode(table);
  1030. if (move == NULL)
  1031. goto cuddZddSiftingDownOutOfMem;
  1032. move->x = x;
  1033. move->y = y;
  1034. move->size = size;
  1035. move->next = moves;
  1036. moves = move;
  1037. if ((double)size > (double)limit_size * table->maxGrowth)
  1038. break;
  1039. if (size < limit_size)
  1040. limit_size = size;
  1041. x = y;
  1042. y = cuddZddNextHigh(table, x);
  1043. }
  1044. return(moves);
  1045. cuddZddSiftingDownOutOfMem:
  1046. while (moves != NULL) {
  1047. move = moves->next;
  1048. cuddDeallocMove(table, moves);
  1049. moves = move;
  1050. }
  1051. return(NULL);
  1052. } /* end of cuddZddSiftingDown */
  1053. /**
  1054. @brief Given a set of moves, returns the %ZDD heap to the position
  1055. giving the minimum size.
  1056. @details In case of ties, returns to the closest position giving the
  1057. minimum size.
  1058. @return 1 in case of success; 0 otherwise.
  1059. @sideeffect None
  1060. */
  1061. static int
  1062. cuddZddSiftingBackward(
  1063. DdManager * table,
  1064. Move * moves,
  1065. int size)
  1066. {
  1067. int i;
  1068. int i_best;
  1069. Move *move;
  1070. int res;
  1071. /* Find the minimum size among moves. */
  1072. i_best = -1;
  1073. for (move = moves, i = 0; move != NULL; move = move->next, i++) {
  1074. if (move->size < size) {
  1075. i_best = i;
  1076. size = move->size;
  1077. }
  1078. }
  1079. for (move = moves, i = 0; move != NULL; move = move->next, i++) {
  1080. if (i == i_best)
  1081. break;
  1082. res = cuddZddSwapInPlace(table, move->x, move->y);
  1083. if (!res)
  1084. return(0);
  1085. if (i_best == -1 && res == size)
  1086. break;
  1087. }
  1088. return(1);
  1089. } /* end of cuddZddSiftingBackward */
  1090. /**
  1091. @brief Prepares the %ZDD heap for dynamic reordering.
  1092. @details Does garbage collection, to guarantee that there are no
  1093. dead nodes; and clears the cache, which is invalidated by dynamic
  1094. reordering.
  1095. @sideeffect None
  1096. */
  1097. static void
  1098. zddReorderPreprocess(
  1099. DdManager * table)
  1100. {
  1101. /* Clear the cache. */
  1102. cuddCacheFlush(table);
  1103. /* Eliminate dead nodes. Do not scan the cache again. */
  1104. cuddGarbageCollect(table,0);
  1105. return;
  1106. } /* end of ddReorderPreprocess */
  1107. /**
  1108. @brief Shrinks almost empty %ZDD subtables at the end of reordering
  1109. to guarantee that they have a reasonable load factor.
  1110. @details However, if there many nodes are being reclaimed, then no
  1111. resizing occurs.
  1112. @return 1 in case of success; 0 otherwise.
  1113. @sideeffect None
  1114. */
  1115. static int
  1116. zddReorderPostprocess(
  1117. DdManager * table)
  1118. {
  1119. int i, j, posn;
  1120. DdNodePtr *nodelist, *oldnodelist;
  1121. DdNode *node, *next;
  1122. unsigned int slots, oldslots;
  1123. extern DD_OOMFP MMoutOfMemory;
  1124. DD_OOMFP saveHandler;
  1125. #ifdef DD_VERBOSE
  1126. (void) fflush(table->out);
  1127. #endif
  1128. /* If we have very many reclaimed nodes, we do not want to shrink
  1129. ** the subtables, because this will lead to more garbage
  1130. ** collections. More garbage collections mean shorter mean life for
  1131. ** nodes with zero reference count; hence lower probability of finding
  1132. ** a result in the cache.
  1133. */
  1134. if (table->reclaimed > table->allocated * 0.5) return(1);
  1135. /* Resize subtables. */
  1136. for (i = 0; i < table->sizeZ; i++) {
  1137. int shift;
  1138. oldslots = table->subtableZ[i].slots;
  1139. if (oldslots < table->subtableZ[i].keys * DD_MAX_SUBTABLE_SPARSITY ||
  1140. oldslots <= table->initSlots) continue;
  1141. oldnodelist = table->subtableZ[i].nodelist;
  1142. slots = oldslots >> 1;
  1143. saveHandler = MMoutOfMemory;
  1144. MMoutOfMemory = table->outOfMemCallback;
  1145. nodelist = ALLOC(DdNodePtr, slots);
  1146. MMoutOfMemory = saveHandler;
  1147. if (nodelist == NULL) {
  1148. return(1);
  1149. }
  1150. table->subtableZ[i].nodelist = nodelist;
  1151. table->subtableZ[i].slots = slots;
  1152. table->subtableZ[i].shift++;
  1153. table->subtableZ[i].maxKeys = slots * DD_MAX_SUBTABLE_DENSITY;
  1154. #ifdef DD_VERBOSE
  1155. (void) fprintf(table->err,
  1156. "shrunk layer %d (%d keys) from %d to %d slots\n",
  1157. i, table->subtableZ[i].keys, oldslots, slots);
  1158. #endif
  1159. for (j = 0; (unsigned) j < slots; j++) {
  1160. nodelist[j] = NULL;
  1161. }
  1162. shift = table->subtableZ[i].shift;
  1163. for (j = 0; (unsigned) j < oldslots; j++) {
  1164. node = oldnodelist[j];
  1165. while (node != NULL) {
  1166. next = node->next;
  1167. posn = ddHash(cuddT(node), cuddE(node), shift);
  1168. node->next = nodelist[posn];
  1169. nodelist[posn] = node;
  1170. node = next;
  1171. }
  1172. }
  1173. FREE(oldnodelist);
  1174. table->memused += (slots - oldslots) * sizeof(DdNode *);
  1175. table->slots += slots - oldslots;
  1176. table->minDead = (unsigned) (table->gcFrac * (double) table->slots);
  1177. table->cacheSlack = (int) ddMin(table->maxCacheHard,
  1178. DD_MAX_CACHE_TO_SLOTS_RATIO*table->slots) -
  1179. 2 * (int) table->cacheSlots;
  1180. }
  1181. /* We don't look at the constant subtable, because it is not
  1182. ** affected by reordering.
  1183. */
  1184. return(1);
  1185. } /* end of zddReorderPostprocess */
  1186. /**
  1187. @brief Reorders %ZDD variables according to a given permutation.
  1188. @details The i-th permutation array contains the index of the
  1189. variable that should be brought to the i-th level. zddShuffle
  1190. assumes that no dead nodes are present. The reordering is achieved
  1191. by a series of upward sifts.
  1192. @return 1 if successful; 0 otherwise.
  1193. @sideeffect None
  1194. */
  1195. static int
  1196. zddShuffle(
  1197. DdManager * table,
  1198. int * permutation)
  1199. {
  1200. int index;
  1201. int level;
  1202. int position;
  1203. int numvars;
  1204. int result;
  1205. #ifdef DD_STATS
  1206. unsigned long localTime;
  1207. int initialSize;
  1208. int finalSize;
  1209. int previousSize;
  1210. #endif
  1211. table->zddTotalNumberSwapping = 0;
  1212. #ifdef DD_STATS
  1213. localTime = util_cpu_time();
  1214. initialSize = table->keysZ;
  1215. (void) fprintf(table->out,"#:I_SHUFFLE %8d: initial size\n",
  1216. initialSize);
  1217. #endif
  1218. numvars = table->sizeZ;
  1219. for (level = 0; level < numvars; level++) {
  1220. index = permutation[level];
  1221. position = table->permZ[index];
  1222. #ifdef DD_STATS
  1223. previousSize = table->keysZ;
  1224. #endif
  1225. result = zddSiftUp(table,position,level);
  1226. if (!result) return(0);
  1227. #ifdef DD_STATS
  1228. if (table->keysZ < (unsigned) previousSize) {
  1229. (void) fprintf(table->out,"-");
  1230. } else if (table->keysZ > (unsigned) previousSize) {
  1231. (void) fprintf(table->out,"+"); /* should never happen */
  1232. } else {
  1233. (void) fprintf(table->out,"=");
  1234. }
  1235. fflush(table->out);
  1236. #endif
  1237. }
  1238. #ifdef DD_STATS
  1239. (void) fprintf(table->out,"\n");
  1240. finalSize = table->keysZ;
  1241. (void) fprintf(table->out,"#:F_SHUFFLE %8d: final size\n",finalSize);
  1242. (void) fprintf(table->out,"#:T_SHUFFLE %8g: total time (sec)\n",
  1243. ((double)(util_cpu_time() - localTime)/1000.0));
  1244. (void) fprintf(table->out,"#:N_SHUFFLE %8d: total swaps\n",
  1245. table->zddTotalNumberSwapping);
  1246. #endif
  1247. return(1);
  1248. } /* end of zddShuffle */
  1249. /**
  1250. @brief Moves one %ZDD variable up.
  1251. @details Takes a %ZDD variable from position x and sifts it up to
  1252. position xLow; xLow should be less than or equal to x.
  1253. @return 1 if successful; 0 otherwise
  1254. @sideeffect None
  1255. */
  1256. static int
  1257. zddSiftUp(
  1258. DdManager * table,
  1259. int x,
  1260. int xLow)
  1261. {
  1262. int y;
  1263. int size;
  1264. y = cuddZddNextLow(table,x);
  1265. while (y >= xLow) {
  1266. size = cuddZddSwapInPlace(table,y,x);
  1267. if (size == 0) {
  1268. return(0);
  1269. }
  1270. x = y;
  1271. y = cuddZddNextLow(table,x);
  1272. }
  1273. return(1);
  1274. } /* end of zddSiftUp */
  1275. /**
  1276. @brief Fixes the %ZDD variable group tree after a shuffle.
  1277. @details Assumes that the order of the variables in a terminal node
  1278. has not been changed.
  1279. @sideeffect Changes the %ZDD variable group tree.
  1280. */
  1281. static void
  1282. zddFixTree(
  1283. DdManager * table,
  1284. MtrNode * treenode)
  1285. {
  1286. if (treenode == NULL) return;
  1287. treenode->low = ((int) treenode->index < table->sizeZ) ?
  1288. (MtrHalfWord) table->permZ[treenode->index] : treenode->index;
  1289. if (treenode->child != NULL) {
  1290. zddFixTree(table, treenode->child);
  1291. }
  1292. if (treenode->younger != NULL)
  1293. zddFixTree(table, treenode->younger);
  1294. if (treenode->parent != NULL && treenode->low < treenode->parent->low) {
  1295. treenode->parent->low = treenode->low;
  1296. treenode->parent->index = treenode->index;
  1297. }
  1298. return;
  1299. } /* end of zddFixTree */