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.

1053 lines
27 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddCache.c]
  3. PackageName [cudd]
  4. Synopsis [Functions for cache insertion and lookup.]
  5. Description [Internal procedures included in this module:
  6. <ul>
  7. <li> cuddInitCache()
  8. <li> cuddCacheInsert()
  9. <li> cuddCacheInsert2()
  10. <li> cuddCacheLookup()
  11. <li> cuddCacheLookupZdd()
  12. <li> cuddCacheLookup2()
  13. <li> cuddCacheLookup2Zdd()
  14. <li> cuddConstantLookup()
  15. <li> cuddCacheProfile()
  16. <li> cuddCacheResize()
  17. <li> cuddCacheFlush()
  18. <li> cuddComputeFloorLog2()
  19. </ul>
  20. Static procedures included in this module:
  21. <ul>
  22. </ul> ]
  23. SeeAlso []
  24. Author [Fabio Somenzi]
  25. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  26. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions
  29. are met:
  30. Redistributions of source code must retain the above copyright
  31. notice, this list of conditions and the following disclaimer.
  32. Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. Neither the name of the University of Colorado nor the names of its
  36. contributors may be used to endorse or promote products derived from
  37. this software without specific prior written permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  41. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  42. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  43. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  44. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  48. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  49. POSSIBILITY OF SUCH DAMAGE.]
  50. ******************************************************************************/
  51. #include "util.h"
  52. #include "cuddInt.h"
  53. /*---------------------------------------------------------------------------*/
  54. /* Constant declarations */
  55. /*---------------------------------------------------------------------------*/
  56. #ifdef DD_CACHE_PROFILE
  57. #define DD_HYSTO_BINS 8
  58. #endif
  59. /*---------------------------------------------------------------------------*/
  60. /* Stucture declarations */
  61. /*---------------------------------------------------------------------------*/
  62. /*---------------------------------------------------------------------------*/
  63. /* Type declarations */
  64. /*---------------------------------------------------------------------------*/
  65. /*---------------------------------------------------------------------------*/
  66. /* Variable declarations */
  67. /*---------------------------------------------------------------------------*/
  68. #ifndef lint
  69. static char rcsid[] DD_UNUSED = "$Id: cuddCache.c,v 1.36 2012/02/05 01:07:18 fabio Exp $";
  70. #endif
  71. /*---------------------------------------------------------------------------*/
  72. /* Macro declarations */
  73. /*---------------------------------------------------------------------------*/
  74. /**AutomaticStart*************************************************************/
  75. /*---------------------------------------------------------------------------*/
  76. /* Static function prototypes */
  77. /*---------------------------------------------------------------------------*/
  78. /**AutomaticEnd***************************************************************/
  79. /*---------------------------------------------------------------------------*/
  80. /* Definition of exported functions */
  81. /*---------------------------------------------------------------------------*/
  82. /*---------------------------------------------------------------------------*/
  83. /* Definition of internal functions */
  84. /*---------------------------------------------------------------------------*/
  85. /**Function********************************************************************
  86. Synopsis [Initializes the computed table.]
  87. Description [Initializes the computed table. It is called by
  88. Cudd_Init. Returns 1 in case of success; 0 otherwise.]
  89. SideEffects [None]
  90. SeeAlso [Cudd_Init]
  91. ******************************************************************************/
  92. int
  93. cuddInitCache(
  94. DdManager * unique /* unique table */,
  95. unsigned int cacheSize /* initial size of the cache */,
  96. unsigned int maxCacheSize /* cache size beyond which no resizing occurs */)
  97. {
  98. int i;
  99. unsigned int logSize;
  100. #ifndef DD_CACHE_PROFILE
  101. DdNodePtr *mem;
  102. ptruint offset;
  103. #endif
  104. /* Round cacheSize to largest power of 2 not greater than the requested
  105. ** initial cache size. */
  106. logSize = cuddComputeFloorLog2(ddMax(cacheSize,unique->slots/2));
  107. cacheSize = 1 << logSize;
  108. unique->acache = ALLOC(DdCache,cacheSize+1);
  109. if (unique->acache == NULL) {
  110. unique->errorCode = CUDD_MEMORY_OUT;
  111. return(0);
  112. }
  113. /* If the size of the cache entry is a power of 2, we want to
  114. ** enforce alignment to that power of two. This happens when
  115. ** DD_CACHE_PROFILE is not defined. */
  116. #ifdef DD_CACHE_PROFILE
  117. unique->cache = unique->acache;
  118. unique->memused += (cacheSize) * sizeof(DdCache);
  119. #else
  120. mem = (DdNodePtr *) unique->acache;
  121. offset = (ptruint) mem & (sizeof(DdCache) - 1);
  122. mem += (sizeof(DdCache) - offset) / sizeof(DdNodePtr);
  123. unique->cache = (DdCache *) mem;
  124. assert(((ptruint) unique->cache & (sizeof(DdCache) - 1)) == 0);
  125. unique->memused += (cacheSize+1) * sizeof(DdCache);
  126. #endif
  127. unique->cacheSlots = cacheSize;
  128. unique->cacheShift = sizeof(int) * 8 - logSize;
  129. unique->maxCacheHard = maxCacheSize;
  130. /* If cacheSlack is non-negative, we can resize. */
  131. unique->cacheSlack = (int) ddMin(maxCacheSize,
  132. DD_MAX_CACHE_TO_SLOTS_RATIO*unique->slots) -
  133. 2 * (int) cacheSize;
  134. Cudd_SetMinHit(unique,DD_MIN_HIT);
  135. /* Initialize to avoid division by 0 and immediate resizing. */
  136. unique->cacheMisses = (double) (int) (cacheSize * unique->minHit + 1);
  137. unique->cacheHits = 0;
  138. unique->totCachehits = 0;
  139. /* The sum of cacheMisses and totCacheMisses is always correct,
  140. ** even though cacheMisses is larger than it should for the reasons
  141. ** explained above. */
  142. unique->totCacheMisses = -unique->cacheMisses;
  143. unique->cachecollisions = 0;
  144. unique->cacheinserts = 0;
  145. unique->cacheLastInserts = 0;
  146. unique->cachedeletions = 0;
  147. /* Initialize the cache */
  148. for (i = 0; (unsigned) i < cacheSize; i++) {
  149. unique->cache[i].h = 0; /* unused slots */
  150. unique->cache[i].data = NULL; /* invalid entry */
  151. #ifdef DD_CACHE_PROFILE
  152. unique->cache[i].count = 0;
  153. #endif
  154. }
  155. return(1);
  156. } /* end of cuddInitCache */
  157. /**Function********************************************************************
  158. Synopsis [Inserts a result in the cache for a function with three
  159. operands.]
  160. Description [Inserts a result in the cache for a function with three
  161. operands. The operator tag (see cuddInt.h for details) is split and stored
  162. into unused bits of the first two pointers.]
  163. SideEffects [None]
  164. SeeAlso [cuddCacheInsert2 cuddCacheInsert1]
  165. ******************************************************************************/
  166. void
  167. cuddCacheInsert(
  168. DdManager * table,
  169. ptruint op,
  170. DdNode * f,
  171. DdNode * g,
  172. DdNode * h,
  173. DdNode * data)
  174. {
  175. int posn;
  176. register DdCache *entry;
  177. ptruint uf, ug, uh;
  178. uf = (ptruint) f | (op & 0xe);
  179. ug = (ptruint) g | (op >> 4);
  180. uh = (ptruint) h;
  181. posn = ddCHash2(uh,uf,ug,table->cacheShift);
  182. entry = &table->cache[posn];
  183. table->cachecollisions += entry->data != NULL;
  184. table->cacheinserts++;
  185. entry->f = (DdNode *) uf;
  186. entry->g = (DdNode *) ug;
  187. entry->h = uh;
  188. entry->data = data;
  189. #ifdef DD_CACHE_PROFILE
  190. entry->count++;
  191. #endif
  192. } /* end of cuddCacheInsert */
  193. /**Function********************************************************************
  194. Synopsis [Inserts a result in the cache for a function with two
  195. operands.]
  196. Description []
  197. SideEffects [None]
  198. SeeAlso [cuddCacheInsert cuddCacheInsert1]
  199. ******************************************************************************/
  200. void
  201. cuddCacheInsert2(
  202. DdManager * table,
  203. DD_CTFP op,
  204. DdNode * f,
  205. DdNode * g,
  206. DdNode * data)
  207. {
  208. int posn;
  209. register DdCache *entry;
  210. posn = ddCHash2(op,f,g,table->cacheShift);
  211. entry = &table->cache[posn];
  212. if (entry->data != NULL) {
  213. table->cachecollisions++;
  214. }
  215. table->cacheinserts++;
  216. entry->f = f;
  217. entry->g = g;
  218. entry->h = (ptruint) op;
  219. entry->data = data;
  220. #ifdef DD_CACHE_PROFILE
  221. entry->count++;
  222. #endif
  223. } /* end of cuddCacheInsert2 */
  224. /**Function********************************************************************
  225. Synopsis [Inserts a result in the cache for a function with two
  226. operands.]
  227. Description []
  228. SideEffects [None]
  229. SeeAlso [cuddCacheInsert cuddCacheInsert2]
  230. ******************************************************************************/
  231. void
  232. cuddCacheInsert1(
  233. DdManager * table,
  234. DD_CTFP1 op,
  235. DdNode * f,
  236. DdNode * data)
  237. {
  238. int posn;
  239. register DdCache *entry;
  240. posn = ddCHash2(op,f,f,table->cacheShift);
  241. entry = &table->cache[posn];
  242. if (entry->data != NULL) {
  243. table->cachecollisions++;
  244. }
  245. table->cacheinserts++;
  246. entry->f = f;
  247. entry->g = f;
  248. entry->h = (ptruint) op;
  249. entry->data = data;
  250. #ifdef DD_CACHE_PROFILE
  251. entry->count++;
  252. #endif
  253. } /* end of cuddCacheInsert1 */
  254. /**Function********************************************************************
  255. Synopsis [Looks up in the cache for the result of op applied to f,
  256. g, and h.]
  257. Description [Returns the result if found; it returns NULL if no
  258. result is found.]
  259. SideEffects [None]
  260. SeeAlso [cuddCacheLookup2 cuddCacheLookup1]
  261. ******************************************************************************/
  262. DdNode *
  263. cuddCacheLookup(
  264. DdManager * table,
  265. ptruint op,
  266. DdNode * f,
  267. DdNode * g,
  268. DdNode * h)
  269. {
  270. int posn;
  271. DdCache *en,*cache;
  272. DdNode *data;
  273. ptruint uf, ug, uh;
  274. uf = (ptruint) f | (op & 0xe);
  275. ug = (ptruint) g | (op >> 4);
  276. uh = (ptruint) h;
  277. cache = table->cache;
  278. #ifdef DD_DEBUG
  279. if (cache == NULL) {
  280. return(NULL);
  281. }
  282. #endif
  283. posn = ddCHash2(uh,uf,ug,table->cacheShift);
  284. en = &cache[posn];
  285. if (en->data != NULL && en->f==(DdNodePtr)uf && en->g==(DdNodePtr)ug &&
  286. en->h==uh) {
  287. data = Cudd_Regular(en->data);
  288. table->cacheHits++;
  289. if (data->ref == 0) {
  290. cuddReclaim(table,data);
  291. }
  292. return(en->data);
  293. }
  294. /* Cache miss: decide whether to resize. */
  295. table->cacheMisses++;
  296. if (table->cacheSlack >= 0 &&
  297. table->cacheHits > table->cacheMisses * table->minHit) {
  298. cuddCacheResize(table);
  299. }
  300. return(NULL);
  301. } /* end of cuddCacheLookup */
  302. /**Function********************************************************************
  303. Synopsis [Looks up in the cache for the result of op applied to f,
  304. g, and h.]
  305. Description [Returns the result if found; it returns NULL if no
  306. result is found.]
  307. SideEffects [None]
  308. SeeAlso [cuddCacheLookup2Zdd cuddCacheLookup1Zdd]
  309. ******************************************************************************/
  310. DdNode *
  311. cuddCacheLookupZdd(
  312. DdManager * table,
  313. ptruint op,
  314. DdNode * f,
  315. DdNode * g,
  316. DdNode * h)
  317. {
  318. int posn;
  319. DdCache *en,*cache;
  320. DdNode *data;
  321. ptruint uf, ug, uh;
  322. uf = (ptruint) f | (op & 0xe);
  323. ug = (ptruint) g | (op >> 4);
  324. uh = (ptruint) h;
  325. cache = table->cache;
  326. #ifdef DD_DEBUG
  327. if (cache == NULL) {
  328. return(NULL);
  329. }
  330. #endif
  331. posn = ddCHash2(uh,uf,ug,table->cacheShift);
  332. en = &cache[posn];
  333. if (en->data != NULL && en->f==(DdNodePtr)uf && en->g==(DdNodePtr)ug &&
  334. en->h==uh) {
  335. data = Cudd_Regular(en->data);
  336. table->cacheHits++;
  337. if (data->ref == 0) {
  338. cuddReclaimZdd(table,data);
  339. }
  340. return(en->data);
  341. }
  342. /* Cache miss: decide whether to resize. */
  343. table->cacheMisses++;
  344. if (table->cacheSlack >= 0 &&
  345. table->cacheHits > table->cacheMisses * table->minHit) {
  346. cuddCacheResize(table);
  347. }
  348. return(NULL);
  349. } /* end of cuddCacheLookupZdd */
  350. /**Function********************************************************************
  351. Synopsis [Looks up in the cache for the result of op applied to f
  352. and g.]
  353. Description [Returns the result if found; it returns NULL if no
  354. result is found.]
  355. SideEffects [None]
  356. SeeAlso [cuddCacheLookup cuddCacheLookup1]
  357. ******************************************************************************/
  358. DdNode *
  359. cuddCacheLookup2(
  360. DdManager * table,
  361. DD_CTFP op,
  362. DdNode * f,
  363. DdNode * g)
  364. {
  365. int posn;
  366. DdCache *en,*cache;
  367. DdNode *data;
  368. cache = table->cache;
  369. #ifdef DD_DEBUG
  370. if (cache == NULL) {
  371. return(NULL);
  372. }
  373. #endif
  374. posn = ddCHash2(op,f,g,table->cacheShift);
  375. en = &cache[posn];
  376. if (en->data != NULL && en->f==f && en->g==g && en->h==(ptruint)op) {
  377. data = Cudd_Regular(en->data);
  378. table->cacheHits++;
  379. if (data->ref == 0) {
  380. cuddReclaim(table,data);
  381. }
  382. return(en->data);
  383. }
  384. /* Cache miss: decide whether to resize. */
  385. table->cacheMisses++;
  386. if (table->cacheSlack >= 0 &&
  387. table->cacheHits > table->cacheMisses * table->minHit) {
  388. cuddCacheResize(table);
  389. }
  390. return(NULL);
  391. } /* end of cuddCacheLookup2 */
  392. /**Function********************************************************************
  393. Synopsis [Looks up in the cache for the result of op applied to f.]
  394. Description [Returns the result if found; it returns NULL if no
  395. result is found.]
  396. SideEffects [None]
  397. SeeAlso [cuddCacheLookup cuddCacheLookup2]
  398. ******************************************************************************/
  399. DdNode *
  400. cuddCacheLookup1(
  401. DdManager * table,
  402. DD_CTFP1 op,
  403. DdNode * f)
  404. {
  405. int posn;
  406. DdCache *en,*cache;
  407. DdNode *data;
  408. cache = table->cache;
  409. #ifdef DD_DEBUG
  410. if (cache == NULL) {
  411. return(NULL);
  412. }
  413. #endif
  414. posn = ddCHash2(op,f,f,table->cacheShift);
  415. en = &cache[posn];
  416. if (en->data != NULL && en->f==f && en->h==(ptruint)op) {
  417. data = Cudd_Regular(en->data);
  418. table->cacheHits++;
  419. if (data->ref == 0) {
  420. cuddReclaim(table,data);
  421. }
  422. return(en->data);
  423. }
  424. /* Cache miss: decide whether to resize. */
  425. table->cacheMisses++;
  426. if (table->cacheSlack >= 0 &&
  427. table->cacheHits > table->cacheMisses * table->minHit) {
  428. cuddCacheResize(table);
  429. }
  430. return(NULL);
  431. } /* end of cuddCacheLookup1 */
  432. /**Function********************************************************************
  433. Synopsis [Looks up in the cache for the result of op applied to f
  434. and g.]
  435. Description [Returns the result if found; it returns NULL if no
  436. result is found.]
  437. SideEffects [None]
  438. SeeAlso [cuddCacheLookupZdd cuddCacheLookup1Zdd]
  439. ******************************************************************************/
  440. DdNode *
  441. cuddCacheLookup2Zdd(
  442. DdManager * table,
  443. DD_CTFP op,
  444. DdNode * f,
  445. DdNode * g)
  446. {
  447. int posn;
  448. DdCache *en,*cache;
  449. DdNode *data;
  450. cache = table->cache;
  451. #ifdef DD_DEBUG
  452. if (cache == NULL) {
  453. return(NULL);
  454. }
  455. #endif
  456. posn = ddCHash2(op,f,g,table->cacheShift);
  457. en = &cache[posn];
  458. if (en->data != NULL && en->f==f && en->g==g && en->h==(ptruint)op) {
  459. data = Cudd_Regular(en->data);
  460. table->cacheHits++;
  461. if (data->ref == 0) {
  462. cuddReclaimZdd(table,data);
  463. }
  464. return(en->data);
  465. }
  466. /* Cache miss: decide whether to resize. */
  467. table->cacheMisses++;
  468. if (table->cacheSlack >= 0 &&
  469. table->cacheHits > table->cacheMisses * table->minHit) {
  470. cuddCacheResize(table);
  471. }
  472. return(NULL);
  473. } /* end of cuddCacheLookup2Zdd */
  474. /**Function********************************************************************
  475. Synopsis [Looks up in the cache for the result of op applied to f.]
  476. Description [Returns the result if found; it returns NULL if no
  477. result is found.]
  478. SideEffects [None]
  479. SeeAlso [cuddCacheLookupZdd cuddCacheLookup2Zdd]
  480. ******************************************************************************/
  481. DdNode *
  482. cuddCacheLookup1Zdd(
  483. DdManager * table,
  484. DD_CTFP1 op,
  485. DdNode * f)
  486. {
  487. int posn;
  488. DdCache *en,*cache;
  489. DdNode *data;
  490. cache = table->cache;
  491. #ifdef DD_DEBUG
  492. if (cache == NULL) {
  493. return(NULL);
  494. }
  495. #endif
  496. posn = ddCHash2(op,f,f,table->cacheShift);
  497. en = &cache[posn];
  498. if (en->data != NULL && en->f==f && en->h==(ptruint)op) {
  499. data = Cudd_Regular(en->data);
  500. table->cacheHits++;
  501. if (data->ref == 0) {
  502. cuddReclaimZdd(table,data);
  503. }
  504. return(en->data);
  505. }
  506. /* Cache miss: decide whether to resize. */
  507. table->cacheMisses++;
  508. if (table->cacheSlack >= 0 &&
  509. table->cacheHits > table->cacheMisses * table->minHit) {
  510. cuddCacheResize(table);
  511. }
  512. return(NULL);
  513. } /* end of cuddCacheLookup1Zdd */
  514. /**Function********************************************************************
  515. Synopsis [Looks up in the cache for the result of op applied to f,
  516. g, and h.]
  517. Description [Looks up in the cache for the result of op applied to f,
  518. g, and h. Assumes that the calling procedure (e.g.,
  519. Cudd_bddIteConstant) is only interested in whether the result is
  520. constant or not. Returns the result if found (possibly
  521. DD_NON_CONSTANT); otherwise it returns NULL.]
  522. SideEffects [None]
  523. SeeAlso [cuddCacheLookup]
  524. ******************************************************************************/
  525. DdNode *
  526. cuddConstantLookup(
  527. DdManager * table,
  528. ptruint op,
  529. DdNode * f,
  530. DdNode * g,
  531. DdNode * h)
  532. {
  533. int posn;
  534. DdCache *en,*cache;
  535. ptruint uf, ug, uh;
  536. uf = (ptruint) f | (op & 0xe);
  537. ug = (ptruint) g | (op >> 4);
  538. uh = (ptruint) h;
  539. cache = table->cache;
  540. #ifdef DD_DEBUG
  541. if (cache == NULL) {
  542. return(NULL);
  543. }
  544. #endif
  545. posn = ddCHash2(uh,uf,ug,table->cacheShift);
  546. en = &cache[posn];
  547. /* We do not reclaim here because the result should not be
  548. * referenced, but only tested for being a constant.
  549. */
  550. if (en->data != NULL &&
  551. en->f == (DdNodePtr)uf && en->g == (DdNodePtr)ug && en->h == uh) {
  552. table->cacheHits++;
  553. return(en->data);
  554. }
  555. /* Cache miss: decide whether to resize. */
  556. table->cacheMisses++;
  557. if (table->cacheSlack >= 0 &&
  558. table->cacheHits > table->cacheMisses * table->minHit) {
  559. cuddCacheResize(table);
  560. }
  561. return(NULL);
  562. } /* end of cuddConstantLookup */
  563. /**Function********************************************************************
  564. Synopsis [Computes and prints a profile of the cache usage.]
  565. Description [Computes and prints a profile of the cache usage.
  566. Returns 1 if successful; 0 otherwise.]
  567. SideEffects [None]
  568. SeeAlso []
  569. ******************************************************************************/
  570. int
  571. cuddCacheProfile(
  572. DdManager * table,
  573. FILE * fp)
  574. {
  575. DdCache *cache = table->cache;
  576. int slots = table->cacheSlots;
  577. int nzeroes = 0;
  578. int i, retval;
  579. double exUsed;
  580. #ifdef DD_CACHE_PROFILE
  581. double count, mean, meansq, stddev, expected;
  582. long max, min;
  583. int imax, imin;
  584. double *hystogramQ, *hystogramR; /* histograms by quotient and remainder */
  585. int nbins = DD_HYSTO_BINS;
  586. int bin;
  587. long thiscount;
  588. double totalcount, exStddev;
  589. meansq = mean = expected = 0.0;
  590. max = min = (long) cache[0].count;
  591. imax = imin = 0;
  592. totalcount = 0.0;
  593. hystogramQ = ALLOC(double, nbins);
  594. if (hystogramQ == NULL) {
  595. table->errorCode = CUDD_MEMORY_OUT;
  596. return(0);
  597. }
  598. hystogramR = ALLOC(double, nbins);
  599. if (hystogramR == NULL) {
  600. FREE(hystogramQ);
  601. table->errorCode = CUDD_MEMORY_OUT;
  602. return(0);
  603. }
  604. for (i = 0; i < nbins; i++) {
  605. hystogramQ[i] = 0;
  606. hystogramR[i] = 0;
  607. }
  608. for (i = 0; i < slots; i++) {
  609. thiscount = (long) cache[i].count;
  610. if (thiscount > max) {
  611. max = thiscount;
  612. imax = i;
  613. }
  614. if (thiscount < min) {
  615. min = thiscount;
  616. imin = i;
  617. }
  618. if (thiscount == 0) {
  619. nzeroes++;
  620. }
  621. count = (double) thiscount;
  622. mean += count;
  623. meansq += count * count;
  624. totalcount += count;
  625. expected += count * (double) i;
  626. bin = (i * nbins) / slots;
  627. hystogramQ[bin] += (double) thiscount;
  628. bin = i % nbins;
  629. hystogramR[bin] += (double) thiscount;
  630. }
  631. mean /= (double) slots;
  632. meansq /= (double) slots;
  633. /* Compute the standard deviation from both the data and the
  634. ** theoretical model for a random distribution. */
  635. stddev = sqrt(meansq - mean*mean);
  636. exStddev = sqrt((1 - 1/(double) slots) * totalcount / (double) slots);
  637. retval = fprintf(fp,"Cache average accesses = %g\n", mean);
  638. if (retval == EOF) return(0);
  639. retval = fprintf(fp,"Cache access standard deviation = %g ", stddev);
  640. if (retval == EOF) return(0);
  641. retval = fprintf(fp,"(expected = %g)\n", exStddev);
  642. if (retval == EOF) return(0);
  643. retval = fprintf(fp,"Cache max accesses = %ld for slot %d\n", max, imax);
  644. if (retval == EOF) return(0);
  645. retval = fprintf(fp,"Cache min accesses = %ld for slot %d\n", min, imin);
  646. if (retval == EOF) return(0);
  647. exUsed = 100.0 * (1.0 - exp(-totalcount / (double) slots));
  648. retval = fprintf(fp,"Cache used slots = %.2f%% (expected %.2f%%)\n",
  649. 100.0 - (double) nzeroes * 100.0 / (double) slots,
  650. exUsed);
  651. if (retval == EOF) return(0);
  652. if (totalcount > 0) {
  653. expected /= totalcount;
  654. retval = fprintf(fp,"Cache access hystogram for %d bins", nbins);
  655. if (retval == EOF) return(0);
  656. retval = fprintf(fp," (expected bin value = %g)\nBy quotient:",
  657. expected);
  658. if (retval == EOF) return(0);
  659. for (i = nbins - 1; i>=0; i--) {
  660. retval = fprintf(fp," %.0f", hystogramQ[i]);
  661. if (retval == EOF) return(0);
  662. }
  663. retval = fprintf(fp,"\nBy residue: ");
  664. if (retval == EOF) return(0);
  665. for (i = nbins - 1; i>=0; i--) {
  666. retval = fprintf(fp," %.0f", hystogramR[i]);
  667. if (retval == EOF) return(0);
  668. }
  669. retval = fprintf(fp,"\n");
  670. if (retval == EOF) return(0);
  671. }
  672. FREE(hystogramQ);
  673. FREE(hystogramR);
  674. #else
  675. for (i = 0; i < slots; i++) {
  676. nzeroes += cache[i].h == 0;
  677. }
  678. exUsed = 100.0 *
  679. (1.0 - exp(-(table->cacheinserts - table->cacheLastInserts) /
  680. (double) slots));
  681. retval = fprintf(fp,"Cache used slots = %.2f%% (expected %.2f%%)\n",
  682. 100.0 - (double) nzeroes * 100.0 / (double) slots,
  683. exUsed);
  684. if (retval == EOF) return(0);
  685. #endif
  686. return(1);
  687. } /* end of cuddCacheProfile */
  688. /**Function********************************************************************
  689. Synopsis [Resizes the cache.]
  690. Description []
  691. SideEffects [None]
  692. SeeAlso []
  693. ******************************************************************************/
  694. void
  695. cuddCacheResize(
  696. DdManager * table)
  697. {
  698. DdCache *cache, *oldcache, *oldacache, *entry, *old;
  699. int i;
  700. int posn, shift;
  701. unsigned int slots, oldslots;
  702. double offset;
  703. int moved = 0;
  704. extern DD_OOMFP MMoutOfMemory;
  705. DD_OOMFP saveHandler;
  706. #ifndef DD_CACHE_PROFILE
  707. ptruint misalignment;
  708. DdNodePtr *mem;
  709. #endif
  710. oldcache = table->cache;
  711. oldacache = table->acache;
  712. oldslots = table->cacheSlots;
  713. slots = table->cacheSlots = oldslots << 1;
  714. #ifdef DD_VERBOSE
  715. (void) fprintf(table->err,"Resizing the cache from %d to %d entries\n",
  716. oldslots, slots);
  717. (void) fprintf(table->err,
  718. "\thits = %g\tmisses = %g\thit ratio = %5.3f\n",
  719. table->cacheHits, table->cacheMisses,
  720. table->cacheHits / (table->cacheHits + table->cacheMisses));
  721. #endif
  722. saveHandler = MMoutOfMemory;
  723. MMoutOfMemory = Cudd_OutOfMem;
  724. table->acache = cache = ALLOC(DdCache,slots+1);
  725. MMoutOfMemory = saveHandler;
  726. /* If we fail to allocate the new table we just give up. */
  727. if (cache == NULL) {
  728. #ifdef DD_VERBOSE
  729. (void) fprintf(table->err,"Resizing failed. Giving up.\n");
  730. #endif
  731. table->cacheSlots = oldslots;
  732. table->acache = oldacache;
  733. /* Do not try to resize again. */
  734. table->maxCacheHard = oldslots - 1;
  735. table->cacheSlack = - (int) (oldslots + 1);
  736. return;
  737. }
  738. /* If the size of the cache entry is a power of 2, we want to
  739. ** enforce alignment to that power of two. This happens when
  740. ** DD_CACHE_PROFILE is not defined. */
  741. #ifdef DD_CACHE_PROFILE
  742. table->cache = cache;
  743. #else
  744. mem = (DdNodePtr *) cache;
  745. misalignment = (ptruint) mem & (sizeof(DdCache) - 1);
  746. mem += (sizeof(DdCache) - misalignment) / sizeof(DdNodePtr);
  747. table->cache = cache = (DdCache *) mem;
  748. assert(((ptruint) table->cache & (sizeof(DdCache) - 1)) == 0);
  749. #endif
  750. shift = --(table->cacheShift);
  751. table->memused += (slots - oldslots) * sizeof(DdCache);
  752. table->cacheSlack -= slots; /* need these many slots to double again */
  753. /* Clear new cache. */
  754. for (i = 0; (unsigned) i < slots; i++) {
  755. cache[i].data = NULL;
  756. cache[i].h = 0;
  757. #ifdef DD_CACHE_PROFILE
  758. cache[i].count = 0;
  759. #endif
  760. }
  761. /* Copy from old cache to new one. */
  762. for (i = 0; (unsigned) i < oldslots; i++) {
  763. old = &oldcache[i];
  764. if (old->data != NULL) {
  765. posn = ddCHash2(old->h,old->f,old->g,shift);
  766. entry = &cache[posn];
  767. entry->f = old->f;
  768. entry->g = old->g;
  769. entry->h = old->h;
  770. entry->data = old->data;
  771. #ifdef DD_CACHE_PROFILE
  772. entry->count = 1;
  773. #endif
  774. moved++;
  775. }
  776. }
  777. FREE(oldacache);
  778. /* Reinitialize measurements so as to avoid division by 0 and
  779. ** immediate resizing.
  780. */
  781. offset = (double) (int) (slots * table->minHit + 1);
  782. table->totCacheMisses += table->cacheMisses - offset;
  783. table->cacheMisses = offset;
  784. table->totCachehits += table->cacheHits;
  785. table->cacheHits = 0;
  786. table->cacheLastInserts = table->cacheinserts - (double) moved;
  787. } /* end of cuddCacheResize */
  788. /**Function********************************************************************
  789. Synopsis [Flushes the cache.]
  790. Description []
  791. SideEffects [None]
  792. SeeAlso []
  793. ******************************************************************************/
  794. void
  795. cuddCacheFlush(
  796. DdManager * table)
  797. {
  798. int i, slots;
  799. DdCache *cache;
  800. slots = table->cacheSlots;
  801. cache = table->cache;
  802. for (i = 0; i < slots; i++) {
  803. table->cachedeletions += cache[i].data != NULL;
  804. cache[i].data = NULL;
  805. }
  806. table->cacheLastInserts = table->cacheinserts;
  807. return;
  808. } /* end of cuddCacheFlush */
  809. /**Function********************************************************************
  810. Synopsis [Returns the floor of the logarithm to the base 2.]
  811. Description [Returns the floor of the logarithm to the base 2.
  812. The input value is assumed to be greater than 0.]
  813. SideEffects [None]
  814. SeeAlso []
  815. ******************************************************************************/
  816. int
  817. cuddComputeFloorLog2(
  818. unsigned int value)
  819. {
  820. int floorLog = 0;
  821. #ifdef DD_DEBUG
  822. assert(value > 0);
  823. #endif
  824. while (value > 1) {
  825. floorLog++;
  826. value >>= 1;
  827. }
  828. return(floorLog);
  829. } /* end of cuddComputeFloorLog2 */
  830. /*---------------------------------------------------------------------------*/
  831. /* Definition of static functions */
  832. /*---------------------------------------------------------------------------*/