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.

456 lines
13 KiB

  1. /**
  2. @file
  3. @ingroup nanotrav
  4. @brief %ZDD test functions.
  5. @author Fabio Somenzi
  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 "ntr.h"
  35. #include "cuddInt.h"
  36. /*---------------------------------------------------------------------------*/
  37. /* Constant declarations */
  38. /*---------------------------------------------------------------------------*/
  39. /*---------------------------------------------------------------------------*/
  40. /* Stucture declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Type declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. static int reorderZdd (BnetNetwork *net, DdManager *dd, NtrOptions *option);
  56. /** \endcond */
  57. /*---------------------------------------------------------------------------*/
  58. /* Definition of exported functions */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. @brief Tests ZDDs.
  62. @return 1 if successful; 0 otherwise.
  63. @sideeffect Creates %ZDD variables in the manager.
  64. */
  65. int
  66. Ntr_testZDD(
  67. DdManager * dd,
  68. BnetNetwork * net,
  69. NtrOptions * option)
  70. {
  71. DdNode **zdd; /* array of converted outputs */
  72. int nz; /* actual number of ZDDs */
  73. int result;
  74. int i, j;
  75. BnetNode *node; /* auxiliary pointer to network node */
  76. int pr = option->verb;
  77. int level; /* aux. var. used to print variable orders */
  78. char **names; /* array used to print variable orders */
  79. int nvars;
  80. /* Build an array of ZDDs for the output functions or for the
  81. ** specified node. */
  82. Cudd_AutodynDisable(dd);
  83. Cudd_AutodynDisableZdd(dd);
  84. zdd = ALLOC(DdNode *,net->noutputs);
  85. result = Cudd_zddVarsFromBddVars(dd,1);
  86. if (result == 0) return(0);
  87. if (option->node == NULL) {
  88. for (nz = 0; nz < net->noutputs; nz++) {
  89. if (!st_lookup(net->hash,net->outputs[nz],(void **)&node)) {
  90. return(0);
  91. }
  92. zdd[nz] = Cudd_zddPortFromBdd(dd, node->dd);
  93. if (zdd[nz]) {
  94. Cudd_Ref(zdd[nz]);
  95. (void) printf("%s", node->name);
  96. result = Cudd_zddPrintDebug(dd,zdd[nz],Cudd_ReadZddSize(dd),pr);
  97. if (result == 0) return(0);
  98. } else {
  99. (void) printf("Conversion to ZDD failed.\n");
  100. }
  101. }
  102. } else {
  103. if (!st_lookup(net->hash,option->node,(void **)&node)) {
  104. return(0);
  105. }
  106. zdd[0] = Cudd_zddPortFromBdd(dd, node->dd);
  107. if (zdd[0]) {
  108. Cudd_Ref(zdd[0]);
  109. (void) printf("%s", node->name);
  110. result = Cudd_zddPrintDebug(dd,zdd[0],Cudd_ReadZddSize(dd),pr);
  111. if (result == 0) return(0);
  112. } else {
  113. (void) printf("Conversion to ZDD failed.\n");
  114. }
  115. nz = 1;
  116. }
  117. #ifdef DD_DEBUG
  118. result = Cudd_CheckKeys(dd);
  119. if (result != 0) {
  120. (void) fprintf(stderr,"Error reported by Cudd_CheckKeys\n");
  121. return(0);
  122. }
  123. #endif
  124. if (option->autoDyn & 1) {
  125. Cudd_AutodynEnable(dd,CUDD_REORDER_SAME);
  126. }
  127. if (option->autoDyn & 2) {
  128. Cudd_AutodynEnableZdd(dd,CUDD_REORDER_SAME);
  129. }
  130. /* Convert the ZDDs back to BDDs and check identity. */
  131. for (i = 0; i < nz; i++) {
  132. DdNode *checkBdd;
  133. checkBdd = Cudd_zddPortToBdd(dd,zdd[i]);
  134. if (checkBdd) {
  135. Cudd_Ref(checkBdd);
  136. if (option->node == NULL) {
  137. if (!st_lookup(net->hash,net->outputs[i],(void **)&node)) {
  138. return(0);
  139. }
  140. } else {
  141. if (!st_lookup(net->hash,option->node,(void **)&node)) {
  142. return(0);
  143. }
  144. }
  145. if (checkBdd != node->dd) {
  146. (void) fprintf(stdout,"Equivalence failed at node %s",
  147. node->name);
  148. result = Cudd_PrintDebug(dd,checkBdd,Cudd_ReadZddSize(dd),pr);
  149. if (result == 0) return(0);
  150. }
  151. Cudd_RecursiveDeref(dd,checkBdd);
  152. } else {
  153. (void) printf("Conversion to BDD failed.\n");
  154. }
  155. }
  156. #ifdef DD_DEBUG
  157. result = Cudd_CheckKeys(dd);
  158. if (result != 0) {
  159. (void) fprintf(stderr,"Error reported by Cudd_CheckKeys\n");
  160. return(0);
  161. }
  162. #endif
  163. /* Play with the ZDDs a little. */
  164. if (nz > 2) {
  165. DdNode *f;
  166. DdNode *g1, *g2, *g;
  167. f = Cudd_zddIte(dd,zdd[0],zdd[1],zdd[2]);
  168. if (f == NULL) return(0);
  169. cuddRef(f);
  170. g1 = Cudd_zddIntersect(dd,zdd[0],zdd[1]);
  171. if (g1 == NULL) {
  172. Cudd_RecursiveDerefZdd(dd,f);
  173. return(0);
  174. }
  175. cuddRef(g1);
  176. g2 = Cudd_zddDiff(dd,zdd[2],zdd[0]);
  177. if (g2 == NULL) {
  178. Cudd_RecursiveDerefZdd(dd,f);
  179. Cudd_RecursiveDerefZdd(dd,g1);
  180. return(0);
  181. }
  182. cuddRef(g2);
  183. g = Cudd_zddUnion(dd,g1,g2);
  184. if (g == NULL) {
  185. Cudd_RecursiveDerefZdd(dd,f);
  186. Cudd_RecursiveDerefZdd(dd,g1);
  187. Cudd_RecursiveDerefZdd(dd,g2);
  188. return(0);
  189. }
  190. cuddRef(g);
  191. Cudd_RecursiveDerefZdd(dd,g1);
  192. Cudd_RecursiveDerefZdd(dd,g2);
  193. if (g != f) {
  194. (void) fprintf(stderr,"f != g!\n");
  195. }
  196. Cudd_RecursiveDerefZdd(dd,g);
  197. Cudd_RecursiveDerefZdd(dd,f);
  198. }
  199. #ifdef DD_DEBUG
  200. result = Cudd_CheckKeys(dd);
  201. if (result != 0) {
  202. (void) fprintf(stderr,"Error reported by Cudd_CheckKeys\n");
  203. return(0);
  204. }
  205. #endif
  206. /* Perform ZDD reordering. */
  207. result = reorderZdd(net,dd,option);
  208. if (result == 0) {
  209. (void) fprintf(stderr,"Error during ZDD reordering\n");
  210. return(0);
  211. }
  212. /* Print final ZDD order. */
  213. nvars = Cudd_ReadZddSize(dd);
  214. names = ALLOC(char *, nvars);
  215. if (names == NULL) return(0);
  216. for (i = 0; i < nvars; i++) {
  217. names[i] = NULL;
  218. }
  219. if (option->reordering != CUDD_REORDER_NONE) {
  220. for (i = 0; i < net->npis; i++) {
  221. if (!st_lookup(net->hash,net->inputs[i],(void **)&node)) {
  222. FREE(names);
  223. return(0);
  224. }
  225. level = Cudd_ReadPermZdd(dd,node->var);
  226. names[level] = node->name;
  227. }
  228. for (i = 0; i < net->nlatches; i++) {
  229. if (!st_lookup(net->hash,net->latches[i][1],(void **)&node)) {
  230. FREE(names);
  231. return(0);
  232. }
  233. level = Cudd_ReadPermZdd(dd,node->var);
  234. names[level] = node->name;
  235. }
  236. (void) printf("New order\n");
  237. for (i = 0, j = 0; i < nvars; i++) {
  238. if (names[i] == NULL) continue;
  239. if((j%8 == 0)&&j) (void) printf("\n");
  240. (void) printf("%s ",names[i]);
  241. j++;
  242. }
  243. (void) printf("\n");
  244. }
  245. FREE(names);
  246. /* Dispose of ZDDs. */
  247. for (i = 0; i < nz; i++) {
  248. Cudd_RecursiveDerefZdd(dd,zdd[i]);
  249. }
  250. FREE(zdd);
  251. return(1);
  252. } /* end of Ntr_testZDD */
  253. /**
  254. @brief Builds %ZDD covers.
  255. @sideeffect Creates %ZDD variables in the manager.
  256. */
  257. int
  258. Ntr_testISOP(
  259. DdManager * dd,
  260. BnetNetwork * net,
  261. NtrOptions * option)
  262. {
  263. DdNode **zdd; /* array of converted outputs */
  264. DdNode *bdd; /* return value of Cudd_zddIsop */
  265. int nz; /* actual number of ZDDs */
  266. int result;
  267. int i;
  268. BnetNode *node; /* auxiliary pointer to network node */
  269. int pr = option->verb;
  270. /* Build an array of ZDDs for the output functions or the specified
  271. ** node. */
  272. Cudd_zddRealignEnable(dd);
  273. Cudd_AutodynDisableZdd(dd);
  274. zdd = ALLOC(DdNode *,net->noutputs);
  275. result = Cudd_zddVarsFromBddVars(dd,2);
  276. if (result == 0) return(0);
  277. if (option->node == NULL) {
  278. nz = net->noutputs;
  279. for (i = 0; i < nz; i++) {
  280. if (!st_lookup(net->hash,net->outputs[i],(void **)&node)) {
  281. return(0);
  282. }
  283. bdd = Cudd_zddIsop(dd, node->dd, node->dd, &zdd[i]);
  284. if (bdd != node->dd) return(0);
  285. Cudd_Ref(bdd);
  286. Cudd_RecursiveDeref(dd,bdd);
  287. if (zdd[i]) {
  288. Cudd_Ref(zdd[i]);
  289. (void) printf("%s", node->name);
  290. result = Cudd_zddPrintDebug(dd,zdd[i],Cudd_ReadZddSize(dd),pr);
  291. if (result == 0) return(0);
  292. if (option->printcover) {
  293. int *path;
  294. DdGen *gen;
  295. char *str = ALLOC(char,Cudd_ReadSize(dd)+1);
  296. if (str == NULL) return(0);
  297. (void) printf("Testing iterator on ZDD paths:\n");
  298. Cudd_zddForeachPath(dd,zdd[i],gen,path) {
  299. str = Cudd_zddCoverPathToString(dd,path,str);
  300. (void) printf("%s 1\n", str);
  301. }
  302. (void) printf("\n");
  303. FREE(str);
  304. result = Cudd_zddPrintCover(dd,zdd[i]);
  305. if (result == 0) return(0);
  306. }
  307. } else {
  308. (void) printf("Conversion to ISOP failed.\n");
  309. return(0);
  310. }
  311. }
  312. } else {
  313. nz = 1;
  314. if (!st_lookup(net->hash,option->node,(void **)&node)) {
  315. return(0);
  316. }
  317. bdd = Cudd_zddIsop(dd, node->dd, node->dd, &zdd[0]);
  318. if (bdd != node->dd) return(0);
  319. Cudd_Ref(bdd);
  320. Cudd_RecursiveDeref(dd,bdd);
  321. if (zdd[0]) {
  322. Cudd_Ref(zdd[0]);
  323. (void) printf("%s", node->name);
  324. result = Cudd_zddPrintDebug(dd,zdd[0],Cudd_ReadZddSize(dd),pr);
  325. if (result == 0) return(0);
  326. if (option->printcover) {
  327. int *path;
  328. DdGen *gen;
  329. char *str = ALLOC(char,Cudd_ReadSize(dd)+1);
  330. if (str == NULL) return(0);
  331. (void) printf("Testing iterator on ZDD paths:\n");
  332. Cudd_zddForeachPath(dd,zdd[0],gen,path) {
  333. str = Cudd_zddCoverPathToString(dd,path,str);
  334. (void) printf("%s 1\n", str);
  335. }
  336. (void) printf("\n");
  337. FREE(str);
  338. result = Cudd_zddPrintCover(dd,zdd[0]);
  339. if (result == 0) return(0);
  340. }
  341. } else {
  342. (void) printf("Conversion to ISOP failed.\n");
  343. return(0);
  344. }
  345. }
  346. if (option->autoDyn) {
  347. Cudd_AutodynEnableZdd(dd,CUDD_REORDER_SAME);
  348. }
  349. /* Perform ZDD reordering. */
  350. result = reorderZdd(net,dd,option);
  351. if (result == 0) return(0);
  352. /* Dispose of ZDDs. */
  353. for (i = 0; i < nz; i++) {
  354. Cudd_RecursiveDerefZdd(dd,zdd[i]);
  355. }
  356. FREE(zdd);
  357. return(1);
  358. } /* end of Ntr_testISOP */
  359. /*---------------------------------------------------------------------------*/
  360. /* Definition of internal functions */
  361. /*---------------------------------------------------------------------------*/
  362. /*---------------------------------------------------------------------------*/
  363. /* Definition of static functions */
  364. /*---------------------------------------------------------------------------*/
  365. /**
  366. @brief Applies reordering to the ZDDs.
  367. @details Explicitly applies reordering to the ZDDs.
  368. @return 1 if successful; 0 otherwise.
  369. @sideeffect None
  370. */
  371. static int
  372. reorderZdd(
  373. BnetNetwork * net /**< boolean network */,
  374. DdManager * dd /**< DD Manager */,
  375. NtrOptions * option /**< options */)
  376. {
  377. int result; /* return value from functions */
  378. /* Perform the final reordering. */
  379. if (option->reordering != CUDD_REORDER_NONE) {
  380. (void) printf("Number of inputs = %d\n",net->ninputs);
  381. dd->siftMaxVar = 1000000;
  382. result = Cudd_zddReduceHeap(dd,option->reordering,1);
  383. if (result == 0) return(0);
  384. /* Print symmetry stats if pertinent */
  385. if (option->reordering == CUDD_REORDER_SYMM_SIFT ||
  386. option->reordering == CUDD_REORDER_SYMM_SIFT_CONV)
  387. Cudd_zddSymmProfile(dd, 0, dd->sizeZ - 1);
  388. }
  389. return(1);
  390. } /* end of reorderZdd */