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.

578 lines
15 KiB

3 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Function to read a matrix in Harwell format.
  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 "util.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. /** \endcond */
  56. /*---------------------------------------------------------------------------*/
  57. /* Definition of exported functions */
  58. /*---------------------------------------------------------------------------*/
  59. /**
  60. @brief Reads in a matrix in the format of the Harwell-Boeing
  61. benchmark suite.
  62. @details The variables are ordered as follows:
  63. <blockquote>
  64. x\[0\] y\[0\] x\[1\] y\[1\] ...
  65. </blockquote>
  66. 0 is the most significant bit. On input, nx and ny hold the numbers
  67. of row and column variables already in existence.
  68. @return 1 on success; 0 otherwise.
  69. @sideeffect On output, nx and ny hold the numbers of row and column
  70. variables actually used by the matrix. m and n are set to the
  71. numbers of rows and columns of the matrix. Their values on input
  72. are immaterial. The %ADD for the sparse matrix is returned in E, and
  73. its reference count is > 0.
  74. @see Cudd_addRead Cudd_bddRead
  75. */
  76. int
  77. Cudd_addHarwell(
  78. FILE * fp /**< pointer to the input file */,
  79. DdManager * dd /**< %DD manager */,
  80. DdNode ** E /**< characteristic function of the graph */,
  81. DdNode *** x /**< array of row variables */,
  82. DdNode *** y /**< array of column variables */,
  83. DdNode *** xn /**< array of complemented row variables */,
  84. DdNode *** yn_ /**< array of complemented column variables */,
  85. int * nx /**< number or row variables */,
  86. int * ny /**< number or column variables */,
  87. int * m /**< number of rows */,
  88. int * n /**< number of columns */,
  89. int bx /**< first index of row variables */,
  90. int sx /**< step of row variables */,
  91. int by /**< first index of column variables */,
  92. int sy /**< step of column variables */,
  93. int pr /**< verbosity level */)
  94. {
  95. DdNode *one, *zero;
  96. DdNode *w;
  97. DdNode *cubex, *cubey, *minterm1;
  98. int u, v, err, i, j, nv;
  99. double val;
  100. /* local copies of x, y, xn, yn_ */
  101. DdNode **lx = NULL, **ly = NULL, **lxn = NULL, **lyn = NULL;
  102. int lnx, lny; /* local copies of nx and ny */
  103. char title[73], key[9], mxtype[4], rhstyp[4];
  104. int totcrd, ptrcrd, indcrd, valcrd, rhscrd,
  105. nrow, ncol, nnzero, neltvl,
  106. nrhs, nrhsix;
  107. int *colptr, *rowind;
  108. #if 0
  109. int nguess, nexact;
  110. int *rhsptr, *rhsind;
  111. #endif
  112. if (*nx < 0 || *ny < 0) return(0);
  113. one = DD_ONE(dd);
  114. zero = DD_ZERO(dd);
  115. /* Read the header */
  116. err = fscanf(fp, "%72c %8c", title, key);
  117. if (err == EOF) {
  118. return(0);
  119. } else if (err != 2) {
  120. return(0);
  121. }
  122. title[72] = (char) 0;
  123. key[8] = (char) 0;
  124. err = fscanf(fp, "%d %d %d %d %d", &totcrd, &ptrcrd, &indcrd,
  125. &valcrd, &rhscrd);
  126. if (err == EOF) {
  127. return(0);
  128. } else if (err != 5) {
  129. return(0);
  130. }
  131. err = fscanf(fp, "%3s %d %d %d %d", mxtype, &nrow, &ncol,
  132. &nnzero, &neltvl);
  133. if (err == EOF) {
  134. return(0);
  135. } else if (err != 5) {
  136. return(0);
  137. }
  138. /* Skip FORTRAN formats */
  139. if (rhscrd == 0) {
  140. err = fscanf(fp, "%*s %*s %*s \n");
  141. } else {
  142. err = fscanf(fp, "%*s %*s %*s %*s \n");
  143. }
  144. if (err == EOF) {
  145. return(0);
  146. } else if (err != 0) {
  147. return(0);
  148. }
  149. /* Print out some stuff if requested to be verbose */
  150. if (pr>0) {
  151. (void) fprintf(dd->out,"%s: type %s, %d rows, %d columns, %d entries\n", key,
  152. mxtype, nrow, ncol, nnzero);
  153. if (pr>1) (void) fprintf(dd->out,"%s\n", title);
  154. }
  155. /* Check matrix type */
  156. if (mxtype[0] != 'R' || mxtype[1] != 'U' || mxtype[2] != 'A') {
  157. (void) fprintf(dd->err,"%s: Illegal matrix type: %s\n",
  158. key, mxtype);
  159. return(0);
  160. }
  161. if (neltvl != 0) return(0);
  162. /* Read optional 5-th line */
  163. if (rhscrd != 0) {
  164. err = fscanf(fp, "%3c %d %d", rhstyp, &nrhs, &nrhsix);
  165. if (err == EOF) {
  166. return(0);
  167. } else if (err != 3) {
  168. return(0);
  169. }
  170. rhstyp[3] = (char) 0;
  171. if (rhstyp[0] != 'F') {
  172. (void) fprintf(dd->err,
  173. "%s: Sparse right-hand side not yet supported\n", key);
  174. return(0);
  175. }
  176. if (pr>0) (void) fprintf(dd->out,"%d right-hand side(s)\n", nrhs);
  177. } else {
  178. nrhs = 0;
  179. }
  180. /* Compute the number of variables */
  181. /* row and column numbers start from 0 */
  182. u = nrow - 1;
  183. for (i=0; u > 0; i++) {
  184. u >>= 1;
  185. }
  186. lnx = i;
  187. if (nrhs == 0) {
  188. v = ncol - 1;
  189. } else {
  190. v = 2* (ddMax(ncol, nrhs) - 1);
  191. }
  192. for (i=0; v > 0; i++) {
  193. v >>= 1;
  194. }
  195. lny = i;
  196. /* Allocate or reallocate arrays for variables as needed */
  197. if (*nx == 0) {
  198. if (lnx > 0) {
  199. *x = lx = ALLOC(DdNode *,lnx);
  200. if (lx == NULL) {
  201. dd->errorCode = CUDD_MEMORY_OUT;
  202. return(0);
  203. }
  204. *xn = lxn = ALLOC(DdNode *,lnx);
  205. if (lxn == NULL) {
  206. dd->errorCode = CUDD_MEMORY_OUT;
  207. return(0);
  208. }
  209. } else {
  210. *x = *xn = NULL;
  211. }
  212. } else if (lnx > *nx) {
  213. *x = lx = REALLOC(DdNode *, *x, lnx);
  214. if (lx == NULL) {
  215. dd->errorCode = CUDD_MEMORY_OUT;
  216. return(0);
  217. }
  218. *xn = lxn = REALLOC(DdNode *, *xn, lnx);
  219. if (lxn == NULL) {
  220. dd->errorCode = CUDD_MEMORY_OUT;
  221. return(0);
  222. }
  223. } else {
  224. lx = *x;
  225. lxn = *xn;
  226. }
  227. if (*ny == 0) {
  228. if (lny >0) {
  229. *y = ly = ALLOC(DdNode *,lny);
  230. if (ly == NULL) {
  231. dd->errorCode = CUDD_MEMORY_OUT;
  232. return(0);
  233. }
  234. *yn_ = lyn = ALLOC(DdNode *,lny);
  235. if (lyn == NULL) {
  236. dd->errorCode = CUDD_MEMORY_OUT;
  237. return(0);
  238. }
  239. } else {
  240. *y = *yn_ = NULL;
  241. }
  242. } else if (lny > *ny) {
  243. *y = ly = REALLOC(DdNode *, *y, lny);
  244. if (ly == NULL) {
  245. dd->errorCode = CUDD_MEMORY_OUT;
  246. return(0);
  247. }
  248. *yn_ = lyn = REALLOC(DdNode *, *yn_, lny);
  249. if (lyn == NULL) {
  250. dd->errorCode = CUDD_MEMORY_OUT;
  251. return(0);
  252. }
  253. } else {
  254. ly = *y;
  255. lyn = *yn_;
  256. }
  257. /* Create new variables as needed */
  258. for (i= *nx,nv=bx+(*nx)*sx; i < lnx; i++,nv+=sx) {
  259. do {
  260. dd->reordered = 0;
  261. lx[i] = cuddUniqueInter(dd, nv, one, zero);
  262. } while (dd->reordered == 1);
  263. if (lx[i] == NULL) {
  264. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  265. dd->timeoutHandler(dd, dd->tohArg);
  266. }
  267. return(0);
  268. }
  269. cuddRef(lx[i]);
  270. do {
  271. dd->reordered = 0;
  272. lxn[i] = cuddUniqueInter(dd, nv, zero, one);
  273. } while (dd->reordered == 1);
  274. if (lxn[i] == NULL) {
  275. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  276. dd->timeoutHandler(dd, dd->tohArg);
  277. }
  278. return(0);
  279. }
  280. cuddRef(lxn[i]);
  281. }
  282. for (i= *ny,nv=by+(*ny)*sy; i < lny; i++,nv+=sy) {
  283. do {
  284. dd->reordered = 0;
  285. ly[i] = cuddUniqueInter(dd, nv, one, zero);
  286. } while (dd->reordered == 1);
  287. if (ly[i] == NULL) {
  288. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  289. dd->timeoutHandler(dd, dd->tohArg);
  290. }
  291. return(0);
  292. }
  293. cuddRef(ly[i]);
  294. do {
  295. dd->reordered = 0;
  296. lyn[i] = cuddUniqueInter(dd, nv, zero, one);
  297. } while (dd->reordered == 1);
  298. if (lyn[i] == NULL) {
  299. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  300. dd->timeoutHandler(dd, dd->tohArg);
  301. }
  302. return(0);
  303. }
  304. cuddRef(lyn[i]);
  305. }
  306. /* Update matrix parameters */
  307. *nx = lnx;
  308. *ny = lny;
  309. *m = nrow;
  310. if (nrhs == 0) {
  311. *n = ncol;
  312. } else {
  313. *n = (1 << (lny - 1)) + nrhs;
  314. }
  315. /* Read structure data */
  316. colptr = ALLOC(int, ncol+1);
  317. if (colptr == NULL) {
  318. dd->errorCode = CUDD_MEMORY_OUT;
  319. return(0);
  320. }
  321. rowind = ALLOC(int, nnzero);
  322. if (rowind == NULL) {
  323. dd->errorCode = CUDD_MEMORY_OUT;
  324. return(0);
  325. }
  326. for (i=0; i<ncol+1; i++) {
  327. err = fscanf(fp, " %d ", &u);
  328. if (err == EOF){
  329. FREE(colptr);
  330. FREE(rowind);
  331. return(0);
  332. } else if (err != 1) {
  333. FREE(colptr);
  334. FREE(rowind);
  335. return(0);
  336. }
  337. colptr[i] = u - 1;
  338. }
  339. if (colptr[0] != 0) {
  340. (void) fprintf(dd->err,"%s: Unexpected colptr[0] (%d)\n",
  341. key,colptr[0]);
  342. FREE(colptr);
  343. FREE(rowind);
  344. return(0);
  345. }
  346. for (i=0; i<nnzero; i++) {
  347. err = fscanf(fp, " %d ", &u);
  348. if (err == EOF){
  349. FREE(colptr);
  350. FREE(rowind);
  351. return(0);
  352. } else if (err != 1) {
  353. FREE(colptr);
  354. FREE(rowind);
  355. return(0);
  356. }
  357. rowind[i] = u - 1;
  358. }
  359. *E = zero; cuddRef(*E);
  360. for (j=0; j<ncol; j++) {
  361. v = j;
  362. cubey = one; cuddRef(cubey);
  363. for (nv = lny - 1; nv>=0; nv--) {
  364. if (v & 1) {
  365. w = Cudd_addApply(dd, Cudd_addTimes, cubey, ly[nv]);
  366. } else {
  367. w = Cudd_addApply(dd, Cudd_addTimes, cubey, lyn[nv]);
  368. }
  369. if (w == NULL) {
  370. Cudd_RecursiveDeref(dd, cubey);
  371. FREE(colptr);
  372. FREE(rowind);
  373. return(0);
  374. }
  375. cuddRef(w);
  376. Cudd_RecursiveDeref(dd, cubey);
  377. cubey = w;
  378. v >>= 1;
  379. }
  380. for (i=colptr[j]; i<colptr[j+1]; i++) {
  381. u = rowind[i];
  382. err = fscanf(fp, " %lf ", &val);
  383. if (err == EOF || err != 1){
  384. Cudd_RecursiveDeref(dd, cubey);
  385. FREE(colptr);
  386. FREE(rowind);
  387. return(0);
  388. }
  389. /* Create new Constant node if necessary */
  390. cubex = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) val);
  391. if (cubex == NULL) {
  392. Cudd_RecursiveDeref(dd, cubey);
  393. FREE(colptr);
  394. FREE(rowind);
  395. return(0);
  396. }
  397. cuddRef(cubex);
  398. for (nv = lnx - 1; nv>=0; nv--) {
  399. if (u & 1) {
  400. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lx[nv]);
  401. } else {
  402. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lxn[nv]);
  403. }
  404. if (w == NULL) {
  405. Cudd_RecursiveDeref(dd, cubey);
  406. Cudd_RecursiveDeref(dd, cubex);
  407. FREE(colptr);
  408. FREE(rowind);
  409. return(0);
  410. }
  411. cuddRef(w);
  412. Cudd_RecursiveDeref(dd, cubex);
  413. cubex = w;
  414. u >>= 1;
  415. }
  416. minterm1 = Cudd_addApply(dd, Cudd_addTimes, cubey, cubex);
  417. if (minterm1 == NULL) {
  418. Cudd_RecursiveDeref(dd, cubey);
  419. Cudd_RecursiveDeref(dd, cubex);
  420. FREE(colptr);
  421. FREE(rowind);
  422. return(0);
  423. }
  424. cuddRef(minterm1);
  425. Cudd_RecursiveDeref(dd, cubex);
  426. w = Cudd_addApply(dd, Cudd_addPlus, *E, minterm1);
  427. if (w == NULL) {
  428. Cudd_RecursiveDeref(dd, cubey);
  429. FREE(colptr);
  430. FREE(rowind);
  431. return(0);
  432. }
  433. cuddRef(w);
  434. Cudd_RecursiveDeref(dd, minterm1);
  435. Cudd_RecursiveDeref(dd, *E);
  436. *E = w;
  437. }
  438. Cudd_RecursiveDeref(dd, cubey);
  439. }
  440. FREE(colptr);
  441. FREE(rowind);
  442. /* Read right-hand sides */
  443. for (j=0; j<nrhs; j++) {
  444. v = j + (1<< (lny-1));
  445. cubey = one; cuddRef(cubey);
  446. for (nv = lny - 1; nv>=0; nv--) {
  447. if (v & 1) {
  448. w = Cudd_addApply(dd, Cudd_addTimes, cubey, ly[nv]);
  449. } else {
  450. w = Cudd_addApply(dd, Cudd_addTimes, cubey, lyn[nv]);
  451. }
  452. if (w == NULL) {
  453. Cudd_RecursiveDeref(dd, cubey);
  454. return(0);
  455. }
  456. cuddRef(w);
  457. Cudd_RecursiveDeref(dd, cubey);
  458. cubey = w;
  459. v >>= 1;
  460. }
  461. for (i=0; i<nrow; i++) {
  462. u = i;
  463. err = fscanf(fp, " %lf ", &val);
  464. if (err == EOF || err != 1){
  465. Cudd_RecursiveDeref(dd, cubey);
  466. return(0);
  467. }
  468. /* Create new Constant node if necessary */
  469. if (val == (double) 0.0) continue;
  470. cubex = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) val);
  471. if (cubex == NULL) {
  472. Cudd_RecursiveDeref(dd, cubey);
  473. return(0);
  474. }
  475. cuddRef(cubex);
  476. for (nv = lnx - 1; nv>=0; nv--) {
  477. if (u & 1) {
  478. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lx[nv]);
  479. } else {
  480. w = Cudd_addApply(dd, Cudd_addTimes, cubex, lxn[nv]);
  481. }
  482. if (w == NULL) {
  483. Cudd_RecursiveDeref(dd, cubey);
  484. Cudd_RecursiveDeref(dd, cubex);
  485. return(0);
  486. }
  487. cuddRef(w);
  488. Cudd_RecursiveDeref(dd, cubex);
  489. cubex = w;
  490. u >>= 1;
  491. }
  492. minterm1 = Cudd_addApply(dd, Cudd_addTimes, cubey, cubex);
  493. if (minterm1 == NULL) {
  494. Cudd_RecursiveDeref(dd, cubey);
  495. Cudd_RecursiveDeref(dd, cubex);
  496. return(0);
  497. }
  498. cuddRef(minterm1);
  499. Cudd_RecursiveDeref(dd, cubex);
  500. w = Cudd_addApply(dd, Cudd_addPlus, *E, minterm1);
  501. if (w == NULL) {
  502. Cudd_RecursiveDeref(dd, cubey);
  503. return(0);
  504. }
  505. cuddRef(w);
  506. Cudd_RecursiveDeref(dd, minterm1);
  507. Cudd_RecursiveDeref(dd, *E);
  508. *E = w;
  509. }
  510. Cudd_RecursiveDeref(dd, cubey);
  511. }
  512. return(1);
  513. } /* end of Cudd_addHarwell */
  514. /*---------------------------------------------------------------------------*/
  515. /* Definition of internal functions */
  516. /*---------------------------------------------------------------------------*/
  517. /*---------------------------------------------------------------------------*/
  518. /* Definition of static functions */
  519. /*---------------------------------------------------------------------------*/