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.

568 lines
15 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddHarwell.c]
  3. PackageName [cudd]
  4. Synopsis [Function to read a matrix in Harwell format.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_addHarwell()
  8. </ul>
  9. ]
  10. Author [Fabio Somenzi]
  11. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions
  15. are met:
  16. Redistributions of source code must retain the above copyright
  17. notice, this list of conditions and the following disclaimer.
  18. Redistributions in binary form must reproduce the above copyright
  19. notice, this list of conditions and the following disclaimer in the
  20. documentation and/or other materials provided with the distribution.
  21. Neither the name of the University of Colorado nor the names of its
  22. contributors may be used to endorse or promote products derived from
  23. this software without specific prior written permission.
  24. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. POSSIBILITY OF SUCH DAMAGE.]
  36. ******************************************************************************/
  37. #include "util.h"
  38. #include "cuddInt.h"
  39. /*---------------------------------------------------------------------------*/
  40. /* Constant declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Stucture declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Type declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Variable declarations */
  50. /*---------------------------------------------------------------------------*/
  51. #ifndef lint
  52. static char rcsid[] DD_UNUSED = "$Id: cuddHarwell.c,v 1.10 2012/02/05 01:07:19 fabio Exp $";
  53. #endif
  54. /*---------------------------------------------------------------------------*/
  55. /* Macro declarations */
  56. /*---------------------------------------------------------------------------*/
  57. /**AutomaticStart*************************************************************/
  58. /*---------------------------------------------------------------------------*/
  59. /* Static function prototypes */
  60. /*---------------------------------------------------------------------------*/
  61. /**AutomaticEnd***************************************************************/
  62. /*---------------------------------------------------------------------------*/
  63. /* Definition of exported functions */
  64. /*---------------------------------------------------------------------------*/
  65. /**Function********************************************************************
  66. Synopsis [Reads in a matrix in the format of the Harwell-Boeing
  67. benchmark suite.]
  68. Description [Reads in a matrix in the format of the Harwell-Boeing
  69. benchmark suite. The variables are ordered as follows:
  70. <blockquote>
  71. x\[0\] y\[0\] x\[1\] y\[1\] ...
  72. </blockquote>
  73. 0 is the most significant bit. On input, nx and ny hold the numbers
  74. of row and column variables already in existence. On output, they
  75. hold the numbers of row and column variables actually used by the
  76. matrix. m and n are set to the numbers of rows and columns of the
  77. matrix. Their values on input are immaterial. Returns 1 on
  78. success; 0 otherwise. The ADD for the sparse matrix is returned in
  79. E, and its reference count is > 0.]
  80. SideEffects [None]
  81. SeeAlso [Cudd_addRead Cudd_bddRead]
  82. ******************************************************************************/
  83. int
  84. Cudd_addHarwell(
  85. FILE * fp /* pointer to the input file */,
  86. DdManager * dd /* DD manager */,
  87. DdNode ** E /* characteristic function of the graph */,
  88. DdNode *** x /* array of row variables */,
  89. DdNode *** y /* array of column variables */,
  90. DdNode *** xn /* array of complemented row variables */,
  91. DdNode *** yn_ /* array of complemented column variables */,
  92. int * nx /* number or row variables */,
  93. int * ny /* number or column variables */,
  94. int * m /* number of rows */,
  95. int * n /* number of columns */,
  96. int bx /* first index of row variables */,
  97. int sx /* step of row variables */,
  98. int by /* first index of column variables */,
  99. int sy /* step of column variables */,
  100. int pr /* verbosity level */)
  101. {
  102. DdNode *one, *zero;
  103. DdNode *w;
  104. DdNode *cubex, *cubey, *minterm1;
  105. int u, v, err, i, j, nv;
  106. double val;
  107. DdNode **lx, **ly, **lxn, **lyn; /* local copies of x, y, xn, yn_ */
  108. int lnx, lny; /* local copies of nx and ny */
  109. char title[73], key[9], mxtype[4], rhstyp[4];
  110. int totcrd, ptrcrd, indcrd, valcrd, rhscrd,
  111. nrow, ncol, nnzero, neltvl,
  112. nrhs, nrhsix;
  113. int *colptr, *rowind;
  114. #if 0
  115. int nguess, nexact;
  116. int *rhsptr, *rhsind;
  117. #endif
  118. if (*nx < 0 || *ny < 0) return(0);
  119. one = DD_ONE(dd);
  120. zero = DD_ZERO(dd);
  121. /* Read the header */
  122. err = fscanf(fp, "%72c %8c", title, key);
  123. if (err == EOF) {
  124. return(0);
  125. } else if (err != 2) {
  126. return(0);
  127. }
  128. title[72] = (char) 0;
  129. key[8] = (char) 0;
  130. err = fscanf(fp, "%d %d %d %d %d", &totcrd, &ptrcrd, &indcrd,
  131. &valcrd, &rhscrd);
  132. if (err == EOF) {
  133. return(0);
  134. } else if (err != 5) {
  135. return(0);
  136. }
  137. err = fscanf(fp, "%3s %d %d %d %d", mxtype, &nrow, &ncol,
  138. &nnzero, &neltvl);
  139. if (err == EOF) {
  140. return(0);
  141. } else if (err != 5) {
  142. return(0);
  143. }
  144. /* Skip FORTRAN formats */
  145. if (rhscrd == 0) {
  146. err = fscanf(fp, "%*s %*s %*s \n");
  147. } else {
  148. err = fscanf(fp, "%*s %*s %*s %*s \n");
  149. }
  150. if (err == EOF) {
  151. return(0);
  152. } else if (err != 0) {
  153. return(0);
  154. }
  155. /* Print out some stuff if requested to be verbose */
  156. if (pr>0) {
  157. (void) fprintf(dd->out,"%s: type %s, %d rows, %d columns, %d entries\n", key,
  158. mxtype, nrow, ncol, nnzero);
  159. if (pr>1) (void) fprintf(dd->out,"%s\n", title);
  160. }
  161. /* Check matrix type */
  162. if (mxtype[0] != 'R' || mxtype[1] != 'U' || mxtype[2] != 'A') {
  163. (void) fprintf(dd->err,"%s: Illegal matrix type: %s\n",
  164. key, mxtype);
  165. return(0);
  166. }
  167. if (neltvl != 0) return(0);
  168. /* Read optional 5-th line */
  169. if (rhscrd != 0) {
  170. err = fscanf(fp, "%3c %d %d", rhstyp, &nrhs, &nrhsix);
  171. if (err == EOF) {
  172. return(0);
  173. } else if (err != 3) {
  174. return(0);
  175. }
  176. rhstyp[3] = (char) 0;
  177. if (rhstyp[0] != 'F') {
  178. (void) fprintf(dd->err,
  179. "%s: Sparse right-hand side not yet supported\n", key);
  180. return(0);
  181. }
  182. if (pr>0) (void) fprintf(dd->out,"%d right-hand side(s)\n", nrhs);
  183. } else {
  184. nrhs = 0;
  185. }
  186. /* Compute the number of variables */
  187. /* row and column numbers start from 0 */
  188. u = nrow - 1;
  189. for (i=0; u > 0; i++) {
  190. u >>= 1;
  191. }
  192. lnx = i;
  193. if (nrhs == 0) {
  194. v = ncol - 1;
  195. } else {
  196. v = 2* (ddMax(ncol, nrhs) - 1);
  197. }
  198. for (i=0; v > 0; i++) {
  199. v >>= 1;
  200. }
  201. lny = i;
  202. /* Allocate or reallocate arrays for variables as needed */
  203. if (*nx == 0) {
  204. if (lnx > 0) {
  205. *x = lx = ALLOC(DdNode *,lnx);
  206. if (lx == NULL) {
  207. dd->errorCode = CUDD_MEMORY_OUT;
  208. return(0);
  209. }
  210. *xn = lxn = ALLOC(DdNode *,lnx);
  211. if (lxn == NULL) {
  212. dd->errorCode = CUDD_MEMORY_OUT;
  213. return(0);
  214. }
  215. } else {
  216. *x = *xn = NULL;
  217. }
  218. } else if (lnx > *nx) {
  219. *x = lx = REALLOC(DdNode *, *x, lnx);
  220. if (lx == NULL) {
  221. dd->errorCode = CUDD_MEMORY_OUT;
  222. return(0);
  223. }
  224. *xn = lxn = REALLOC(DdNode *, *xn, lnx);
  225. if (lxn == NULL) {
  226. dd->errorCode = CUDD_MEMORY_OUT;
  227. return(0);
  228. }
  229. } else {
  230. lx = *x;
  231. lxn = *xn;
  232. }
  233. if (*ny == 0) {
  234. if (lny >0) {
  235. *y = ly = ALLOC(DdNode *,lny);
  236. if (ly == NULL) {
  237. dd->errorCode = CUDD_MEMORY_OUT;
  238. return(0);
  239. }
  240. *yn_ = lyn = ALLOC(DdNode *,lny);
  241. if (lyn == NULL) {
  242. dd->errorCode = CUDD_MEMORY_OUT;
  243. return(0);
  244. }
  245. } else {
  246. *y = *yn_ = NULL;
  247. }
  248. } else if (lny > *ny) {
  249. *y = ly = REALLOC(DdNode *, *y, lny);
  250. if (ly == NULL) {
  251. dd->errorCode = CUDD_MEMORY_OUT;
  252. return(0);
  253. }
  254. *yn_ = lyn = REALLOC(DdNode *, *yn_, lny);
  255. if (lyn == NULL) {
  256. dd->errorCode = CUDD_MEMORY_OUT;
  257. return(0);
  258. }
  259. } else {
  260. ly = *y;
  261. lyn = *yn_;
  262. }
  263. /* Create new variables as needed */
  264. for (i= *nx,nv=bx+(*nx)*sx; i < lnx; i++,nv+=sx) {
  265. do {
  266. dd->reordered = 0;
  267. lx[i] = cuddUniqueInter(dd, nv, one, zero);
  268. } while (dd->reordered == 1);
  269. if (lx[i] == NULL) return(0);
  270. cuddRef(lx[i]);
  271. do {
  272. dd->reordered = 0;
  273. lxn[i] = cuddUniqueInter(dd, nv, zero, one);
  274. } while (dd->reordered == 1);
  275. if (lxn[i] == NULL) return(0);
  276. cuddRef(lxn[i]);
  277. }
  278. for (i= *ny,nv=by+(*ny)*sy; i < lny; i++,nv+=sy) {
  279. do {
  280. dd->reordered = 0;
  281. ly[i] = cuddUniqueInter(dd, nv, one, zero);
  282. } while (dd->reordered == 1);
  283. if (ly[i] == NULL) return(0);
  284. cuddRef(ly[i]);
  285. do {
  286. dd->reordered = 0;
  287. lyn[i] = cuddUniqueInter(dd, nv, zero, one);
  288. } while (dd->reordered == 1);
  289. if (lyn[i] == NULL) return(0);
  290. cuddRef(lyn[i]);
  291. }
  292. /* Update matrix parameters */
  293. *nx = lnx;
  294. *ny = lny;
  295. *m = nrow;
  296. if (nrhs == 0) {
  297. *n = ncol;
  298. } else {
  299. *n = (1 << (lny - 1)) + nrhs;
  300. }
  301. /* Read structure data */
  302. colptr = ALLOC(int, ncol+1);
  303. if (colptr == NULL) {
  304. dd->errorCode = CUDD_MEMORY_OUT;
  305. return(0);
  306. }
  307. rowind = ALLOC(int, nnzero);
  308. if (rowind == NULL) {
  309. dd->errorCode = CUDD_MEMORY_OUT;
  310. return(0);
  311. }
  312. for (i=0; i<ncol+1; i++) {
  313. err = fscanf(fp, " %d ", &u);
  314. if (err == EOF){
  315. FREE(colptr);
  316. FREE(rowind);
  317. return(0);
  318. } else if (err != 1) {
  319. FREE(colptr);
  320. FREE(rowind);
  321. return(0);
  322. }
  323. colptr[i] = u - 1;
  324. }
  325. if (colptr[0] != 0) {
  326. (void) fprintf(dd->err,"%s: Unexpected colptr[0] (%d)\n",
  327. key,colptr[0]);
  328. FREE(colptr);
  329. FREE(rowind);
  330. return(0);
  331. }
  332. for (i=0; i<nnzero; i++) {
  333. err = fscanf(fp, " %d ", &u);
  334. if (err == EOF){
  335. FREE(colptr);
  336. FREE(rowind);
  337. return(0);
  338. } else if (err != 1) {
  339. FREE(colptr);
  340. FREE(rowind);
  341. return(0);
  342. }
  343. rowind[i] = u - 1;
  344. }
  345. *E = zero; cuddRef(*E);
  346. for (j=0; j<ncol; j++) {
  347. v = j;
  348. cubey = one; cuddRef(cubey);
  349. for (nv = lny - 1; nv>=0; nv--) {
  350. if (v & 1) {
  351. w = Cudd_addApply(dd, Cudd_addTimes, cubey, ly[nv]);
  352. } else {
  353. w = Cudd_addApply(dd, Cudd_addTimes, cubey, lyn[nv]);
  354. }
  355. if (w == NULL) {
  356. Cudd_RecursiveDeref(dd, cubey);
  357. FREE(colptr);
  358. FREE(rowind);
  359. return(0);
  360. }
  361. cuddRef(w);
  362. Cudd_RecursiveDeref(dd, cubey);
  363. cubey = w;
  364. v >>= 1;
  365. }
  366. for (i=colptr[j]; i<colptr[j+1]; i++) {
  367. u = rowind[i];
  368. err = fscanf(fp, " %lf ", &val);
  369. if (err == EOF || err != 1){
  370. Cudd_RecursiveDeref(dd, cubey);
  371. FREE(colptr);
  372. FREE(rowind);
  373. return(0);
  374. }
  375. /* Create new Constant node if necessary */
  376. cubex = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) val);
  377. if (cubex == NULL) {
  378. Cudd_RecursiveDeref(dd, cubey);
  379. FREE(colptr);
  380. FREE(rowind);
  381. return(0);
  382. }
  383. cuddRef(cubex);
  384. for (nv = lnx - 1; nv>=0; nv--) {
  385. if (u & 1) {
  386. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lx[nv]);
  387. } else {
  388. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lxn[nv]);
  389. }
  390. if (w == NULL) {
  391. Cudd_RecursiveDeref(dd, cubey);
  392. Cudd_RecursiveDeref(dd, cubex);
  393. FREE(colptr);
  394. FREE(rowind);
  395. return(0);
  396. }
  397. cuddRef(w);
  398. Cudd_RecursiveDeref(dd, cubex);
  399. cubex = w;
  400. u >>= 1;
  401. }
  402. minterm1 = Cudd_addApply(dd, Cudd_addTimes, cubey, cubex);
  403. if (minterm1 == NULL) {
  404. Cudd_RecursiveDeref(dd, cubey);
  405. Cudd_RecursiveDeref(dd, cubex);
  406. FREE(colptr);
  407. FREE(rowind);
  408. return(0);
  409. }
  410. cuddRef(minterm1);
  411. Cudd_RecursiveDeref(dd, cubex);
  412. w = Cudd_addApply(dd, Cudd_addPlus, *E, minterm1);
  413. if (w == NULL) {
  414. Cudd_RecursiveDeref(dd, cubey);
  415. FREE(colptr);
  416. FREE(rowind);
  417. return(0);
  418. }
  419. cuddRef(w);
  420. Cudd_RecursiveDeref(dd, minterm1);
  421. Cudd_RecursiveDeref(dd, *E);
  422. *E = w;
  423. }
  424. Cudd_RecursiveDeref(dd, cubey);
  425. }
  426. FREE(colptr);
  427. FREE(rowind);
  428. /* Read right-hand sides */
  429. for (j=0; j<nrhs; j++) {
  430. v = j + (1<< (lny-1));
  431. cubey = one; cuddRef(cubey);
  432. for (nv = lny - 1; nv>=0; nv--) {
  433. if (v & 1) {
  434. w = Cudd_addApply(dd, Cudd_addTimes, cubey, ly[nv]);
  435. } else {
  436. w = Cudd_addApply(dd, Cudd_addTimes, cubey, lyn[nv]);
  437. }
  438. if (w == NULL) {
  439. Cudd_RecursiveDeref(dd, cubey);
  440. return(0);
  441. }
  442. cuddRef(w);
  443. Cudd_RecursiveDeref(dd, cubey);
  444. cubey = w;
  445. v >>= 1;
  446. }
  447. for (i=0; i<nrow; i++) {
  448. u = i;
  449. err = fscanf(fp, " %lf ", &val);
  450. if (err == EOF || err != 1){
  451. Cudd_RecursiveDeref(dd, cubey);
  452. return(0);
  453. }
  454. /* Create new Constant node if necessary */
  455. if (val == (double) 0.0) continue;
  456. cubex = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) val);
  457. if (cubex == NULL) {
  458. Cudd_RecursiveDeref(dd, cubey);
  459. return(0);
  460. }
  461. cuddRef(cubex);
  462. for (nv = lnx - 1; nv>=0; nv--) {
  463. if (u & 1) {
  464. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lx[nv]);
  465. } else {
  466. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lxn[nv]);
  467. }
  468. if (w == NULL) {
  469. Cudd_RecursiveDeref(dd, cubey);
  470. Cudd_RecursiveDeref(dd, cubex);
  471. return(0);
  472. }
  473. cuddRef(w);
  474. Cudd_RecursiveDeref(dd, cubex);
  475. cubex = w;
  476. u >>= 1;
  477. }
  478. minterm1 = Cudd_addApply(dd, Cudd_addTimes, cubey, cubex);
  479. if (minterm1 == NULL) {
  480. Cudd_RecursiveDeref(dd, cubey);
  481. Cudd_RecursiveDeref(dd, cubex);
  482. return(0);
  483. }
  484. cuddRef(minterm1);
  485. Cudd_RecursiveDeref(dd, cubex);
  486. w = Cudd_addApply(dd, Cudd_addPlus, *E, minterm1);
  487. if (w == NULL) {
  488. Cudd_RecursiveDeref(dd, cubey);
  489. return(0);
  490. }
  491. cuddRef(w);
  492. Cudd_RecursiveDeref(dd, minterm1);
  493. Cudd_RecursiveDeref(dd, *E);
  494. *E = w;
  495. }
  496. Cudd_RecursiveDeref(dd, cubey);
  497. }
  498. return(1);
  499. } /* end of Cudd_addHarwell */
  500. /*---------------------------------------------------------------------------*/
  501. /* Definition of internal functions */
  502. /*---------------------------------------------------------------------------*/
  503. /*---------------------------------------------------------------------------*/
  504. /* Definition of static functions */
  505. /*---------------------------------------------------------------------------*/