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.

694 lines
18 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Matrix multiplication 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 "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. static DdNode * addMMRecur (DdManager *dd, DdNode *A, DdNode *B, int topP, int *vars);
  56. static DdNode * addTriangleRecur (DdManager *dd, DdNode *f, DdNode *g, int *vars, DdNode *cube);
  57. static DdNode * cuddAddOuterSumRecur (DdManager *dd, DdNode *M, DdNode *r, DdNode *c);
  58. /** \endcond */
  59. /*---------------------------------------------------------------------------*/
  60. /* Definition of exported functions */
  61. /*---------------------------------------------------------------------------*/
  62. /**
  63. @brief Calculates the product of two matrices represented as
  64. ADDs.
  65. @details This procedure implements the quasiring multiplication
  66. algorithm. A is assumed to depend on variables x (rows) and z
  67. (columns). B is assumed to depend on variables z (rows) and y
  68. (columns). The product of A and B then depends on x (rows) and y
  69. (columns). Only the z variables have to be explicitly identified;
  70. they are the "summation" variables.
  71. @return a pointer to the result if successful; NULL otherwise.
  72. @sideeffect None
  73. @see Cudd_addTimesPlus Cudd_addTriangle Cudd_bddAndAbstract
  74. */
  75. DdNode *
  76. Cudd_addMatrixMultiply(
  77. DdManager * dd,
  78. DdNode * A,
  79. DdNode * B,
  80. DdNode ** z,
  81. int nz)
  82. {
  83. int i, nvars, *vars;
  84. DdNode *res;
  85. /* Array vars says what variables are "summation" variables. */
  86. nvars = dd->size;
  87. vars = ALLOC(int,nvars);
  88. if (vars == NULL) {
  89. dd->errorCode = CUDD_MEMORY_OUT;
  90. return(NULL);
  91. }
  92. for (i = 0; i < nvars; i++) {
  93. vars[i] = 0;
  94. }
  95. for (i = 0; i < nz; i++) {
  96. vars[z[i]->index] = 1;
  97. }
  98. do {
  99. dd->reordered = 0;
  100. res = addMMRecur(dd,A,B,-1,vars);
  101. } while (dd->reordered == 1);
  102. FREE(vars);
  103. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  104. dd->timeoutHandler(dd, dd->tohArg);
  105. }
  106. return(res);
  107. } /* end of Cudd_addMatrixMultiply */
  108. /**
  109. @brief Calculates the product of two matrices represented as
  110. ADDs.
  111. @details Calculates the product of two matrices, A and B,
  112. represented as ADDs, using the CMU matrix by matrix multiplication
  113. procedure by Clarke et al.. Matrix A has x's as row variables and
  114. z's as column variables, while matrix B has z's as row variables and
  115. y's as column variables. The resulting matrix has x's as row
  116. variables and y's as column variables.
  117. @return the pointer to the result if successful; NULL otherwise.
  118. @sideeffect None
  119. @see Cudd_addMatrixMultiply
  120. */
  121. DdNode *
  122. Cudd_addTimesPlus(
  123. DdManager * dd,
  124. DdNode * A,
  125. DdNode * B,
  126. DdNode ** z,
  127. int nz)
  128. {
  129. DdNode *w, *cube, *tmp, *res;
  130. int i;
  131. tmp = Cudd_addApply(dd,Cudd_addTimes,A,B);
  132. if (tmp == NULL) return(NULL);
  133. Cudd_Ref(tmp);
  134. Cudd_Ref(cube = DD_ONE(dd));
  135. for (i = nz-1; i >= 0; i--) {
  136. w = Cudd_addIte(dd,z[i],cube,DD_ZERO(dd));
  137. if (w == NULL) {
  138. Cudd_RecursiveDeref(dd,tmp);
  139. return(NULL);
  140. }
  141. Cudd_Ref(w);
  142. Cudd_RecursiveDeref(dd,cube);
  143. cube = w;
  144. }
  145. res = Cudd_addExistAbstract(dd,tmp,cube);
  146. if (res == NULL) {
  147. Cudd_RecursiveDeref(dd,tmp);
  148. Cudd_RecursiveDeref(dd,cube);
  149. return(NULL);
  150. }
  151. Cudd_Ref(res);
  152. Cudd_RecursiveDeref(dd,cube);
  153. Cudd_RecursiveDeref(dd,tmp);
  154. Cudd_Deref(res);
  155. return(res);
  156. } /* end of Cudd_addTimesPlus */
  157. /**
  158. @brief Performs the triangulation step for the shortest path
  159. computation.
  160. @details Implements the semiring multiplication algorithm used in
  161. the triangulation step for the shortest path computation. f
  162. is assumed to depend on variables x (rows) and z (columns). g is
  163. assumed to depend on variables z (rows) and y (columns). The product
  164. of f and g then depends on x (rows) and y (columns). Only the z
  165. variables have to be explicitly identified; they are the
  166. "abstraction" variables.
  167. @return a pointer to the result if successful; NULL otherwise.
  168. @sideeffect None
  169. @see Cudd_addMatrixMultiply Cudd_bddAndAbstract
  170. */
  171. DdNode *
  172. Cudd_addTriangle(
  173. DdManager * dd,
  174. DdNode * f,
  175. DdNode * g,
  176. DdNode ** z,
  177. int nz)
  178. {
  179. int i, nvars, *vars;
  180. DdNode *res, *cube;
  181. nvars = dd->size;
  182. vars = ALLOC(int, nvars);
  183. if (vars == NULL) {
  184. dd->errorCode = CUDD_MEMORY_OUT;
  185. return(NULL);
  186. }
  187. for (i = 0; i < nvars; i++) vars[i] = -1;
  188. for (i = 0; i < nz; i++) vars[z[i]->index] = i;
  189. cube = Cudd_addComputeCube(dd, z, NULL, nz);
  190. if (cube == NULL) {
  191. FREE(vars);
  192. return(NULL);
  193. }
  194. cuddRef(cube);
  195. do {
  196. dd->reordered = 0;
  197. res = addTriangleRecur(dd, f, g, vars, cube);
  198. } while (dd->reordered == 1);
  199. if (res != NULL) cuddRef(res);
  200. Cudd_RecursiveDeref(dd,cube);
  201. if (res != NULL) cuddDeref(res);
  202. FREE(vars);
  203. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  204. dd->timeoutHandler(dd, dd->tohArg);
  205. }
  206. return(res);
  207. } /* end of Cudd_addTriangle */
  208. /**
  209. @brief Takes the minimum of a matrix and the outer sum of two vectors.
  210. @details Takes the pointwise minimum of a matrix and the outer
  211. sum of two vectors. This procedure is used in the Floyd-Warshall
  212. all-pair shortest path algorithm.
  213. @return a pointer to the result if successful; NULL otherwise.
  214. @sideeffect None
  215. */
  216. DdNode *
  217. Cudd_addOuterSum(
  218. DdManager *dd,
  219. DdNode *M,
  220. DdNode *r,
  221. DdNode *c)
  222. {
  223. DdNode *res;
  224. do {
  225. dd->reordered = 0;
  226. res = cuddAddOuterSumRecur(dd, M, r, c);
  227. } while (dd->reordered == 1);
  228. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  229. dd->timeoutHandler(dd, dd->tohArg);
  230. }
  231. return(res);
  232. } /* end of Cudd_addOuterSum */
  233. /*---------------------------------------------------------------------------*/
  234. /* Definition of internal functions */
  235. /*---------------------------------------------------------------------------*/
  236. /*---------------------------------------------------------------------------*/
  237. /* Definition of static functions */
  238. /*---------------------------------------------------------------------------*/
  239. /**
  240. @brief Performs the recursive step of Cudd_addMatrixMultiply.
  241. @return a pointer to the result if successful; NULL otherwise.
  242. @sideeffect None
  243. */
  244. static DdNode *
  245. addMMRecur(
  246. DdManager * dd,
  247. DdNode * A,
  248. DdNode * B,
  249. int topP,
  250. int * vars)
  251. {
  252. DdNode *zero,
  253. *At, /* positive cofactor of first operand */
  254. *Ae, /* negative cofactor of first operand */
  255. *Bt, /* positive cofactor of second operand */
  256. *Be, /* negative cofactor of second operand */
  257. *t, /* positive cofactor of result */
  258. *e, /* negative cofactor of result */
  259. *scaled, /* scaled result */
  260. *add_scale, /* ADD representing the scaling factor */
  261. *res;
  262. int i; /* loop index */
  263. double scale; /* scaling factor */
  264. int index; /* index of the top variable */
  265. CUDD_VALUE_TYPE value;
  266. int topA, topB, topV;
  267. DD_CTFP cacheOp;
  268. statLine(dd);
  269. zero = DD_ZERO(dd);
  270. if (A == zero || B == zero) {
  271. return(zero);
  272. }
  273. if (cuddIsConstant(A) && cuddIsConstant(B)) {
  274. /* Compute the scaling factor. It is 2^k, where k is the
  275. ** number of summation variables below the current variable.
  276. ** Indeed, these constants represent blocks of 2^k identical
  277. ** constant values in both A and B.
  278. */
  279. value = cuddV(A) * cuddV(B);
  280. for (i = 0; i < dd->size; i++) {
  281. if (vars[i]) {
  282. if (dd->perm[i] > topP) {
  283. value *= (CUDD_VALUE_TYPE) 2;
  284. }
  285. }
  286. }
  287. res = cuddUniqueConst(dd, value);
  288. return(res);
  289. }
  290. /* Standardize to increase cache efficiency. Clearly, A*B != B*A
  291. ** in matrix multiplication. However, which matrix is which is
  292. ** determined by the variables appearing in the ADDs and not by
  293. ** which one is passed as first argument.
  294. */
  295. if (A > B) {
  296. DdNode *tmp = A;
  297. A = B;
  298. B = tmp;
  299. }
  300. topA = cuddI(dd,A->index); topB = cuddI(dd,B->index);
  301. topV = ddMin(topA,topB);
  302. cacheOp = (DD_CTFP) addMMRecur;
  303. res = cuddCacheLookup2(dd,cacheOp,A,B);
  304. if (res != NULL) {
  305. /* If the result is 0, there is no need to normalize.
  306. ** Otherwise we count the number of z variables between
  307. ** the current depth and the top of the ADDs. These are
  308. ** the missing variables that determine the size of the
  309. ** constant blocks.
  310. */
  311. if (res == zero) return(res);
  312. scale = 1.0;
  313. for (i = 0; i < dd->size; i++) {
  314. if (vars[i]) {
  315. if (dd->perm[i] > topP && dd->perm[i] < topV) {
  316. scale *= 2;
  317. }
  318. }
  319. }
  320. if (scale > 1.0) {
  321. cuddRef(res);
  322. add_scale = cuddUniqueConst(dd,(CUDD_VALUE_TYPE)scale);
  323. if (add_scale == NULL) {
  324. Cudd_RecursiveDeref(dd, res);
  325. return(NULL);
  326. }
  327. cuddRef(add_scale);
  328. scaled = cuddAddApplyRecur(dd,Cudd_addTimes,res,add_scale);
  329. if (scaled == NULL) {
  330. Cudd_RecursiveDeref(dd, add_scale);
  331. Cudd_RecursiveDeref(dd, res);
  332. return(NULL);
  333. }
  334. cuddRef(scaled);
  335. Cudd_RecursiveDeref(dd, add_scale);
  336. Cudd_RecursiveDeref(dd, res);
  337. res = scaled;
  338. cuddDeref(res);
  339. }
  340. return(res);
  341. }
  342. checkWhetherToGiveUp(dd);
  343. /* compute the cofactors */
  344. if (topV == topA) {
  345. At = cuddT(A);
  346. Ae = cuddE(A);
  347. } else {
  348. At = Ae = A;
  349. }
  350. if (topV == topB) {
  351. Bt = cuddT(B);
  352. Be = cuddE(B);
  353. } else {
  354. Bt = Be = B;
  355. }
  356. t = addMMRecur(dd, At, Bt, (int)topV, vars);
  357. if (t == NULL) return(NULL);
  358. cuddRef(t);
  359. e = addMMRecur(dd, Ae, Be, (int)topV, vars);
  360. if (e == NULL) {
  361. Cudd_RecursiveDeref(dd, t);
  362. return(NULL);
  363. }
  364. cuddRef(e);
  365. index = dd->invperm[topV];
  366. if (vars[index] == 0) {
  367. /* We have split on either the rows of A or the columns
  368. ** of B. We just need to connect the two subresults,
  369. ** which correspond to two submatrices of the result.
  370. */
  371. res = (t == e) ? t : cuddUniqueInter(dd,index,t,e);
  372. if (res == NULL) {
  373. Cudd_RecursiveDeref(dd, t);
  374. Cudd_RecursiveDeref(dd, e);
  375. return(NULL);
  376. }
  377. cuddRef(res);
  378. cuddDeref(t);
  379. cuddDeref(e);
  380. } else {
  381. /* we have simultaneously split on the columns of A and
  382. ** the rows of B. The two subresults must be added.
  383. */
  384. res = cuddAddApplyRecur(dd,Cudd_addPlus,t,e);
  385. if (res == NULL) {
  386. Cudd_RecursiveDeref(dd, t);
  387. Cudd_RecursiveDeref(dd, e);
  388. return(NULL);
  389. }
  390. cuddRef(res);
  391. Cudd_RecursiveDeref(dd, t);
  392. Cudd_RecursiveDeref(dd, e);
  393. }
  394. cuddCacheInsert2(dd,cacheOp,A,B,res);
  395. /* We have computed (and stored in the computed table) a minimal
  396. ** result; that is, a result that assumes no summation variables
  397. ** between the current depth of the recursion and its top
  398. ** variable. We now take into account the z variables by properly
  399. ** scaling the result.
  400. */
  401. if (res != zero) {
  402. scale = 1.0;
  403. for (i = 0; i < dd->size; i++) {
  404. if (vars[i]) {
  405. if (dd->perm[i] > topP && dd->perm[i] < topV) {
  406. scale *= 2;
  407. }
  408. }
  409. }
  410. if (scale > 1.0) {
  411. add_scale = cuddUniqueConst(dd,(CUDD_VALUE_TYPE)scale);
  412. if (add_scale == NULL) {
  413. Cudd_RecursiveDeref(dd, res);
  414. return(NULL);
  415. }
  416. cuddRef(add_scale);
  417. scaled = cuddAddApplyRecur(dd,Cudd_addTimes,res,add_scale);
  418. if (scaled == NULL) {
  419. Cudd_RecursiveDeref(dd, res);
  420. Cudd_RecursiveDeref(dd, add_scale);
  421. return(NULL);
  422. }
  423. cuddRef(scaled);
  424. Cudd_RecursiveDeref(dd, add_scale);
  425. Cudd_RecursiveDeref(dd, res);
  426. res = scaled;
  427. }
  428. }
  429. cuddDeref(res);
  430. return(res);
  431. } /* end of addMMRecur */
  432. /**
  433. @brief Performs the recursive step of Cudd_addTriangle.
  434. @return a pointer to the result if successful; NULL otherwise.
  435. @sideeffect None
  436. */
  437. static DdNode *
  438. addTriangleRecur(
  439. DdManager * dd,
  440. DdNode * f,
  441. DdNode * g,
  442. int * vars,
  443. DdNode *cube)
  444. {
  445. DdNode *fv, *fvn, *gv, *gvn, *t, *e, *res;
  446. CUDD_VALUE_TYPE value;
  447. int top, topf, topg, index;
  448. statLine(dd);
  449. if (f == DD_PLUS_INFINITY(dd) || g == DD_PLUS_INFINITY(dd)) {
  450. return(DD_PLUS_INFINITY(dd));
  451. }
  452. if (cuddIsConstant(f) && cuddIsConstant(g)) {
  453. value = cuddV(f) + cuddV(g);
  454. res = cuddUniqueConst(dd, value);
  455. return(res);
  456. }
  457. if (f < g) {
  458. DdNode *tmp = f;
  459. f = g;
  460. g = tmp;
  461. }
  462. if (f->ref != 1 || g->ref != 1) {
  463. res = cuddCacheLookup(dd, DD_ADD_TRIANGLE_TAG, f, g, cube);
  464. if (res != NULL) {
  465. return(res);
  466. }
  467. }
  468. checkWhetherToGiveUp(dd);
  469. topf = cuddI(dd,f->index); topg = cuddI(dd,g->index);
  470. top = ddMin(topf,topg);
  471. if (top == topf) {fv = cuddT(f); fvn = cuddE(f);} else {fv = fvn = f;}
  472. if (top == topg) {gv = cuddT(g); gvn = cuddE(g);} else {gv = gvn = g;}
  473. t = addTriangleRecur(dd, fv, gv, vars, cube);
  474. if (t == NULL) return(NULL);
  475. cuddRef(t);
  476. e = addTriangleRecur(dd, fvn, gvn, vars, cube);
  477. if (e == NULL) {
  478. Cudd_RecursiveDeref(dd, t);
  479. return(NULL);
  480. }
  481. cuddRef(e);
  482. index = dd->invperm[top];
  483. if (vars[index] < 0) {
  484. res = (t == e) ? t : cuddUniqueInter(dd,index,t,e);
  485. if (res == NULL) {
  486. Cudd_RecursiveDeref(dd, t);
  487. Cudd_RecursiveDeref(dd, e);
  488. return(NULL);
  489. }
  490. cuddDeref(t);
  491. cuddDeref(e);
  492. } else {
  493. res = cuddAddApplyRecur(dd,Cudd_addMinimum,t,e);
  494. if (res == NULL) {
  495. Cudd_RecursiveDeref(dd, t);
  496. Cudd_RecursiveDeref(dd, e);
  497. return(NULL);
  498. }
  499. cuddRef(res);
  500. Cudd_RecursiveDeref(dd, t);
  501. Cudd_RecursiveDeref(dd, e);
  502. cuddDeref(res);
  503. }
  504. if (f->ref != 1 || g->ref != 1) {
  505. cuddCacheInsert(dd, DD_ADD_TRIANGLE_TAG, f, g, cube, res);
  506. }
  507. return(res);
  508. } /* end of addTriangleRecur */
  509. /**
  510. @brief Performs the recursive step of Cudd_addOuterSum.
  511. @return a pointer to the result if successful; NULL otherwise.
  512. @sideeffect None
  513. */
  514. static DdNode *
  515. cuddAddOuterSumRecur(
  516. DdManager *dd,
  517. DdNode *M,
  518. DdNode *r,
  519. DdNode *c)
  520. {
  521. DdNode *P, *R, *Mt, *Me, *rt, *re, *ct, *ce, *Rt, *Re;
  522. int topM, topc, topr;
  523. int v, index;
  524. statLine(dd);
  525. /* Check special cases. */
  526. if (r == DD_PLUS_INFINITY(dd) || c == DD_PLUS_INFINITY(dd)) return(M);
  527. if (cuddIsConstant(c) && cuddIsConstant(r)) {
  528. R = cuddUniqueConst(dd,Cudd_V(c)+Cudd_V(r));
  529. cuddRef(R);
  530. if (cuddIsConstant(M)) {
  531. if (cuddV(R) <= cuddV(M)) {
  532. cuddDeref(R);
  533. return(R);
  534. } else {
  535. Cudd_RecursiveDeref(dd,R);
  536. return(M);
  537. }
  538. } else {
  539. P = Cudd_addApply(dd,Cudd_addMinimum,R,M);
  540. cuddRef(P);
  541. Cudd_RecursiveDeref(dd,R);
  542. cuddDeref(P);
  543. return(P);
  544. }
  545. }
  546. /* Check the cache. */
  547. R = cuddCacheLookup(dd,DD_ADD_OUT_SUM_TAG,M,r,c);
  548. if (R != NULL) return(R);
  549. checkWhetherToGiveUp(dd);
  550. topM = cuddI(dd,M->index); topr = cuddI(dd,r->index);
  551. topc = cuddI(dd,c->index);
  552. v = ddMin(topM,ddMin(topr,topc));
  553. /* Compute cofactors. */
  554. if (topM == v) { Mt = cuddT(M); Me = cuddE(M); } else { Mt = Me = M; }
  555. if (topr == v) { rt = cuddT(r); re = cuddE(r); } else { rt = re = r; }
  556. if (topc == v) { ct = cuddT(c); ce = cuddE(c); } else { ct = ce = c; }
  557. /* Recursively solve. */
  558. Rt = cuddAddOuterSumRecur(dd,Mt,rt,ct);
  559. if (Rt == NULL) return(NULL);
  560. cuddRef(Rt);
  561. Re = cuddAddOuterSumRecur(dd,Me,re,ce);
  562. if (Re == NULL) {
  563. Cudd_RecursiveDeref(dd, Rt);
  564. return(NULL);
  565. }
  566. cuddRef(Re);
  567. index = dd->invperm[v];
  568. R = (Rt == Re) ? Rt : cuddUniqueInter(dd,index,Rt,Re);
  569. if (R == NULL) {
  570. Cudd_RecursiveDeref(dd, Rt);
  571. Cudd_RecursiveDeref(dd, Re);
  572. return(NULL);
  573. }
  574. cuddDeref(Rt);
  575. cuddDeref(Re);
  576. /* Store the result in the cache. */
  577. cuddCacheInsert(dd,DD_ADD_OUT_SUM_TAG,M,r,c,R);
  578. return(R);
  579. } /* end of cuddAddOuterSumRecur */