amd_2.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /* ========================================================================= */
  2. /* === AMD_2 =============================================================== */
  3. /* ========================================================================= */
  4. /* ------------------------------------------------------------------------- */
  5. /* AMD, Copyright (c) Timothy A. Davis, */
  6. /* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
  7. /* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
  8. /* web: http://www.cise.ufl.edu/research/sparse/amd */
  9. /* ------------------------------------------------------------------------- */
  10. /* AMD_2: performs the AMD ordering on a symmetric sparse matrix A, followed
  11. * by a postordering (via depth-first search) of the assembly tree using the
  12. * AMD_postorder routine.
  13. */
  14. #include "amd_internal.h"
  15. /* ========================================================================= */
  16. /* === clear_flag ========================================================== */
  17. /* ========================================================================= */
  18. static Int clear_flag (Int wflg, Int wbig, Int W [ ], Int n)
  19. {
  20. Int x ;
  21. if (wflg < 2 || wflg >= wbig)
  22. {
  23. for (x = 0 ; x < n ; x++)
  24. {
  25. if (W [x] != 0) W [x] = 1 ;
  26. }
  27. wflg = 2 ;
  28. }
  29. /* at this point, W [0..n-1] < wflg holds */
  30. return (wflg) ;
  31. }
  32. /* ========================================================================= */
  33. /* === AMD_2 =============================================================== */
  34. /* ========================================================================= */
  35. GLOBAL void AMD_2
  36. (
  37. Int n, /* A is n-by-n, where n > 0 */
  38. Int Pe [ ], /* Pe [0..n-1]: index in Iw of row i on input */
  39. Int Iw [ ], /* workspace of size iwlen. Iw [0..pfree-1]
  40. * holds the matrix on input */
  41. Int Len [ ], /* Len [0..n-1]: length for row/column i on input */
  42. Int iwlen, /* length of Iw. iwlen >= pfree + n */
  43. Int pfree, /* Iw [pfree ... iwlen-1] is empty on input */
  44. /* 7 size-n workspaces, not defined on input: */
  45. Int Nv [ ], /* the size of each supernode on output */
  46. Int Next [ ], /* the output inverse permutation */
  47. Int Last [ ], /* the output permutation */
  48. Int Head [ ],
  49. Int Elen [ ], /* the size columns of L for each supernode */
  50. Int Degree [ ],
  51. Int W [ ],
  52. /* control parameters and output statistics */
  53. double Control [ ], /* array of size AMD_CONTROL */
  54. double Info [ ] /* array of size AMD_INFO */
  55. )
  56. {
  57. /*
  58. * Given a representation of the nonzero pattern of a symmetric matrix, A,
  59. * (excluding the diagonal) perform an approximate minimum (UMFPACK/MA38-style)
  60. * degree ordering to compute a pivot order such that the introduction of
  61. * nonzeros (fill-in) in the Cholesky factors A = LL' is kept low. At each
  62. * step, the pivot selected is the one with the minimum UMFAPACK/MA38-style
  63. * upper-bound on the external degree. This routine can optionally perform
  64. * aggresive absorption (as done by MC47B in the Harwell Subroutine
  65. * Library).
  66. *
  67. * The approximate degree algorithm implemented here is the symmetric analog of
  68. * the degree update algorithm in MA38 and UMFPACK (the Unsymmetric-pattern
  69. * MultiFrontal PACKage, both by Davis and Duff). The routine is based on the
  70. * MA27 minimum degree ordering algorithm by Iain Duff and John Reid.
  71. *
  72. * This routine is a translation of the original AMDBAR and MC47B routines,
  73. * in Fortran, with the following modifications:
  74. *
  75. * (1) dense rows/columns are removed prior to ordering the matrix, and placed
  76. * last in the output order. The presence of a dense row/column can
  77. * increase the ordering time by up to O(n^2), unless they are removed
  78. * prior to ordering.
  79. *
  80. * (2) the minimum degree ordering is followed by a postordering (depth-first
  81. * search) of the assembly tree. Note that mass elimination (discussed
  82. * below) combined with the approximate degree update can lead to the mass
  83. * elimination of nodes with lower exact degree than the current pivot
  84. * element. No additional fill-in is caused in the representation of the
  85. * Schur complement. The mass-eliminated nodes merge with the current
  86. * pivot element. They are ordered prior to the current pivot element.
  87. * Because they can have lower exact degree than the current element, the
  88. * merger of two or more of these nodes in the current pivot element can
  89. * lead to a single element that is not a "fundamental supernode". The
  90. * diagonal block can have zeros in it. Thus, the assembly tree used here
  91. * is not guaranteed to be the precise supernodal elemination tree (with
  92. * "funadmental" supernodes), and the postordering performed by this
  93. * routine is not guaranteed to be a precise postordering of the
  94. * elimination tree.
  95. *
  96. * (3) input parameters are added, to control aggressive absorption and the
  97. * detection of "dense" rows/columns of A.
  98. *
  99. * (4) additional statistical information is returned, such as the number of
  100. * nonzeros in L, and the flop counts for subsequent LDL' and LU
  101. * factorizations. These are slight upper bounds, because of the mass
  102. * elimination issue discussed above.
  103. *
  104. * (5) additional routines are added to interface this routine to MATLAB
  105. * to provide a simple C-callable user-interface, to check inputs for
  106. * errors, compute the symmetry of the pattern of A and the number of
  107. * nonzeros in each row/column of A+A', to compute the pattern of A+A',
  108. * to perform the assembly tree postordering, and to provide debugging
  109. * ouput. Many of these functions are also provided by the Fortran
  110. * Harwell Subroutine Library routine MC47A.
  111. *
  112. * (6) both int and UF_long versions are provided. In the descriptions below
  113. * and integer is and int or UF_long depending on which version is
  114. * being used.
  115. **********************************************************************
  116. ***** CAUTION: ARGUMENTS ARE NOT CHECKED FOR ERRORS ON INPUT. ******
  117. **********************************************************************
  118. ** If you want error checking, a more versatile input format, and a **
  119. ** simpler user interface, use amd_order or amd_l_order instead. **
  120. ** This routine is not meant to be user-callable. **
  121. **********************************************************************
  122. * ----------------------------------------------------------------------------
  123. * References:
  124. * ----------------------------------------------------------------------------
  125. *
  126. * [1] Timothy A. Davis and Iain Duff, "An unsymmetric-pattern multifrontal
  127. * method for sparse LU factorization", SIAM J. Matrix Analysis and
  128. * Applications, vol. 18, no. 1, pp. 140-158. Discusses UMFPACK / MA38,
  129. * which first introduced the approximate minimum degree used by this
  130. * routine.
  131. *
  132. * [2] Patrick Amestoy, Timothy A. Davis, and Iain S. Duff, "An approximate
  133. * minimum degree ordering algorithm," SIAM J. Matrix Analysis and
  134. * Applications, vol. 17, no. 4, pp. 886-905, 1996. Discusses AMDBAR and
  135. * MC47B, which are the Fortran versions of this routine.
  136. *
  137. * [3] Alan George and Joseph Liu, "The evolution of the minimum degree
  138. * ordering algorithm," SIAM Review, vol. 31, no. 1, pp. 1-19, 1989.
  139. * We list below the features mentioned in that paper that this code
  140. * includes:
  141. *
  142. * mass elimination:
  143. * Yes. MA27 relied on supervariable detection for mass elimination.
  144. *
  145. * indistinguishable nodes:
  146. * Yes (we call these "supervariables"). This was also in the MA27
  147. * code - although we modified the method of detecting them (the
  148. * previous hash was the true degree, which we no longer keep track
  149. * of). A supervariable is a set of rows with identical nonzero
  150. * pattern. All variables in a supervariable are eliminated together.
  151. * Each supervariable has as its numerical name that of one of its
  152. * variables (its principal variable).
  153. *
  154. * quotient graph representation:
  155. * Yes. We use the term "element" for the cliques formed during
  156. * elimination. This was also in the MA27 code. The algorithm can
  157. * operate in place, but it will work more efficiently if given some
  158. * "elbow room."
  159. *
  160. * element absorption:
  161. * Yes. This was also in the MA27 code.
  162. *
  163. * external degree:
  164. * Yes. The MA27 code was based on the true degree.
  165. *
  166. * incomplete degree update and multiple elimination:
  167. * No. This was not in MA27, either. Our method of degree update
  168. * within MC47B is element-based, not variable-based. It is thus
  169. * not well-suited for use with incomplete degree update or multiple
  170. * elimination.
  171. *
  172. * Authors, and Copyright (C) 2004 by:
  173. * Timothy A. Davis, Patrick Amestoy, Iain S. Duff, John K. Reid.
  174. *
  175. * Acknowledgements: This work (and the UMFPACK package) was supported by the
  176. * National Science Foundation (ASC-9111263, DMS-9223088, and CCR-0203270).
  177. * The UMFPACK/MA38 approximate degree update algorithm, the unsymmetric analog
  178. * which forms the basis of AMD, was developed while Tim Davis was supported by
  179. * CERFACS (Toulouse, France) in a post-doctoral position. This C version, and
  180. * the etree postorder, were written while Tim Davis was on sabbatical at
  181. * Stanford University and Lawrence Berkeley National Laboratory.
  182. * ----------------------------------------------------------------------------
  183. * INPUT ARGUMENTS (unaltered):
  184. * ----------------------------------------------------------------------------
  185. * n: The matrix order. Restriction: n >= 1.
  186. *
  187. * iwlen: The size of the Iw array. On input, the matrix is stored in
  188. * Iw [0..pfree-1]. However, Iw [0..iwlen-1] should be slightly larger
  189. * than what is required to hold the matrix, at least iwlen >= pfree + n.
  190. * Otherwise, excessive compressions will take place. The recommended
  191. * value of iwlen is 1.2 * pfree + n, which is the value used in the
  192. * user-callable interface to this routine (amd_order.c). The algorithm
  193. * will not run at all if iwlen < pfree. Restriction: iwlen >= pfree + n.
  194. * Note that this is slightly more restrictive than the actual minimum
  195. * (iwlen >= pfree), but AMD_2 will be very slow with no elbow room.
  196. * Thus, this routine enforces a bare minimum elbow room of size n.
  197. *
  198. * pfree: On input the tail end of the array, Iw [pfree..iwlen-1], is empty,
  199. * and the matrix is stored in Iw [0..pfree-1]. During execution,
  200. * additional data is placed in Iw, and pfree is modified so that
  201. * Iw [pfree..iwlen-1] is always the unused part of Iw.
  202. *
  203. * Control: A double array of size AMD_CONTROL containing input parameters
  204. * that affect how the ordering is computed. If NULL, then default
  205. * settings are used.
  206. *
  207. * Control [AMD_DENSE] is used to determine whether or not a given input
  208. * row is "dense". A row is "dense" if the number of entries in the row
  209. * exceeds Control [AMD_DENSE] times sqrt (n), except that rows with 16 or
  210. * fewer entries are never considered "dense". To turn off the detection
  211. * of dense rows, set Control [AMD_DENSE] to a negative number, or to a
  212. * number larger than sqrt (n). The default value of Control [AMD_DENSE]
  213. * is AMD_DEFAULT_DENSE, which is defined in amd.h as 10.
  214. *
  215. * Control [AMD_AGGRESSIVE] is used to determine whether or not aggressive
  216. * absorption is to be performed. If nonzero, then aggressive absorption
  217. * is performed (this is the default).
  218. * ----------------------------------------------------------------------------
  219. * INPUT/OUPUT ARGUMENTS:
  220. * ----------------------------------------------------------------------------
  221. *
  222. * Pe: An integer array of size n. On input, Pe [i] is the index in Iw of
  223. * the start of row i. Pe [i] is ignored if row i has no off-diagonal
  224. * entries. Thus Pe [i] must be in the range 0 to pfree-1 for non-empty
  225. * rows.
  226. *
  227. * During execution, it is used for both supervariables and elements:
  228. *
  229. * Principal supervariable i: index into Iw of the description of
  230. * supervariable i. A supervariable represents one or more rows of
  231. * the matrix with identical nonzero pattern. In this case,
  232. * Pe [i] >= 0.
  233. *
  234. * Non-principal supervariable i: if i has been absorbed into another
  235. * supervariable j, then Pe [i] = FLIP (j), where FLIP (j) is defined
  236. * as (-(j)-2). Row j has the same pattern as row i. Note that j
  237. * might later be absorbed into another supervariable j2, in which
  238. * case Pe [i] is still FLIP (j), and Pe [j] = FLIP (j2) which is
  239. * < EMPTY, where EMPTY is defined as (-1) in amd_internal.h.
  240. *
  241. * Unabsorbed element e: the index into Iw of the description of element
  242. * e, if e has not yet been absorbed by a subsequent element. Element
  243. * e is created when the supervariable of the same name is selected as
  244. * the pivot. In this case, Pe [i] >= 0.
  245. *
  246. * Absorbed element e: if element e is absorbed into element e2, then
  247. * Pe [e] = FLIP (e2). This occurs when the pattern of e (which we
  248. * refer to as Le) is found to be a subset of the pattern of e2 (that
  249. * is, Le2). In this case, Pe [i] < EMPTY. If element e is "null"
  250. * (it has no nonzeros outside its pivot block), then Pe [e] = EMPTY,
  251. * and e is the root of an assembly subtree (or the whole tree if
  252. * there is just one such root).
  253. *
  254. * Dense variable i: if i is "dense", then Pe [i] = EMPTY.
  255. *
  256. * On output, Pe holds the assembly tree/forest, which implicitly
  257. * represents a pivot order with identical fill-in as the actual order
  258. * (via a depth-first search of the tree), as follows. If Nv [i] > 0,
  259. * then i represents a node in the assembly tree, and the parent of i is
  260. * Pe [i], or EMPTY if i is a root. If Nv [i] = 0, then (i, Pe [i])
  261. * represents an edge in a subtree, the root of which is a node in the
  262. * assembly tree. Note that i refers to a row/column in the original
  263. * matrix, not the permuted matrix.
  264. *
  265. * Info: A double array of size AMD_INFO. If present, (that is, not NULL),
  266. * then statistics about the ordering are returned in the Info array.
  267. * See amd.h for a description.
  268. * ----------------------------------------------------------------------------
  269. * INPUT/MODIFIED (undefined on output):
  270. * ----------------------------------------------------------------------------
  271. *
  272. * Len: An integer array of size n. On input, Len [i] holds the number of
  273. * entries in row i of the matrix, excluding the diagonal. The contents
  274. * of Len are undefined on output.
  275. *
  276. * Iw: An integer array of size iwlen. On input, Iw [0..pfree-1] holds the
  277. * description of each row i in the matrix. The matrix must be symmetric,
  278. * and both upper and lower triangular parts must be present. The
  279. * diagonal must not be present. Row i is held as follows:
  280. *
  281. * Len [i]: the length of the row i data structure in the Iw array.
  282. * Iw [Pe [i] ... Pe [i] + Len [i] - 1]:
  283. * the list of column indices for nonzeros in row i (simple
  284. * supervariables), excluding the diagonal. All supervariables
  285. * start with one row/column each (supervariable i is just row i).
  286. * If Len [i] is zero on input, then Pe [i] is ignored on input.
  287. *
  288. * Note that the rows need not be in any particular order, and there
  289. * may be empty space between the rows.
  290. *
  291. * During execution, the supervariable i experiences fill-in. This is
  292. * represented by placing in i a list of the elements that cause fill-in
  293. * in supervariable i:
  294. *
  295. * Len [i]: the length of supervariable i in the Iw array.
  296. * Iw [Pe [i] ... Pe [i] + Elen [i] - 1]:
  297. * the list of elements that contain i. This list is kept short
  298. * by removing absorbed elements.
  299. * Iw [Pe [i] + Elen [i] ... Pe [i] + Len [i] - 1]:
  300. * the list of supervariables in i. This list is kept short by
  301. * removing nonprincipal variables, and any entry j that is also
  302. * contained in at least one of the elements (j in Le) in the list
  303. * for i (e in row i).
  304. *
  305. * When supervariable i is selected as pivot, we create an element e of
  306. * the same name (e=i):
  307. *
  308. * Len [e]: the length of element e in the Iw array.
  309. * Iw [Pe [e] ... Pe [e] + Len [e] - 1]:
  310. * the list of supervariables in element e.
  311. *
  312. * An element represents the fill-in that occurs when supervariable i is
  313. * selected as pivot (which represents the selection of row i and all
  314. * non-principal variables whose principal variable is i). We use the
  315. * term Le to denote the set of all supervariables in element e. Absorbed
  316. * supervariables and elements are pruned from these lists when
  317. * computationally convenient.
  318. *
  319. * CAUTION: THE INPUT MATRIX IS OVERWRITTEN DURING COMPUTATION.
  320. * The contents of Iw are undefined on output.
  321. * ----------------------------------------------------------------------------
  322. * OUTPUT (need not be set on input):
  323. * ----------------------------------------------------------------------------
  324. *
  325. * Nv: An integer array of size n. During execution, ABS (Nv [i]) is equal to
  326. * the number of rows that are represented by the principal supervariable
  327. * i. If i is a nonprincipal or dense variable, then Nv [i] = 0.
  328. * Initially, Nv [i] = 1 for all i. Nv [i] < 0 signifies that i is a
  329. * principal variable in the pattern Lme of the current pivot element me.
  330. * After element me is constructed, Nv [i] is set back to a positive
  331. * value.
  332. *
  333. * On output, Nv [i] holds the number of pivots represented by super
  334. * row/column i of the original matrix, or Nv [i] = 0 for non-principal
  335. * rows/columns. Note that i refers to a row/column in the original
  336. * matrix, not the permuted matrix.
  337. *
  338. * Elen: An integer array of size n. See the description of Iw above. At the
  339. * start of execution, Elen [i] is set to zero for all rows i. During
  340. * execution, Elen [i] is the number of elements in the list for
  341. * supervariable i. When e becomes an element, Elen [e] = FLIP (esize) is
  342. * set, where esize is the size of the element (the number of pivots, plus
  343. * the number of nonpivotal entries). Thus Elen [e] < EMPTY.
  344. * Elen (i) = EMPTY set when variable i becomes nonprincipal.
  345. *
  346. * For variables, Elen (i) >= EMPTY holds until just before the
  347. * postordering and permutation vectors are computed. For elements,
  348. * Elen [e] < EMPTY holds.
  349. *
  350. * On output, Elen [i] is the degree of the row/column in the Cholesky
  351. * factorization of the permuted matrix, corresponding to the original row
  352. * i, if i is a super row/column. It is equal to EMPTY if i is
  353. * non-principal. Note that i refers to a row/column in the original
  354. * matrix, not the permuted matrix.
  355. *
  356. * Note that the contents of Elen on output differ from the Fortran
  357. * version (Elen holds the inverse permutation in the Fortran version,
  358. * which is instead returned in the Next array in this C version,
  359. * described below).
  360. *
  361. * Last: In a degree list, Last [i] is the supervariable preceding i, or EMPTY
  362. * if i is the head of the list. In a hash bucket, Last [i] is the hash
  363. * key for i.
  364. *
  365. * Last [Head [hash]] is also used as the head of a hash bucket if
  366. * Head [hash] contains a degree list (see the description of Head,
  367. * below).
  368. *
  369. * On output, Last [0..n-1] holds the permutation. That is, if
  370. * i = Last [k], then row i is the kth pivot row (where k ranges from 0 to
  371. * n-1). Row Last [k] of A is the kth row in the permuted matrix, PAP'.
  372. *
  373. * Next: Next [i] is the supervariable following i in a link list, or EMPTY if
  374. * i is the last in the list. Used for two kinds of lists: degree lists
  375. * and hash buckets (a supervariable can be in only one kind of list at a
  376. * time).
  377. *
  378. * On output Next [0..n-1] holds the inverse permutation. That is, if
  379. * k = Next [i], then row i is the kth pivot row. Row i of A appears as
  380. * the (Next[i])-th row in the permuted matrix, PAP'.
  381. *
  382. * Note that the contents of Next on output differ from the Fortran
  383. * version (Next is undefined on output in the Fortran version).
  384. * ----------------------------------------------------------------------------
  385. * LOCAL WORKSPACE (not input or output - used only during execution):
  386. * ----------------------------------------------------------------------------
  387. *
  388. * Degree: An integer array of size n. If i is a supervariable, then
  389. * Degree [i] holds the current approximation of the external degree of
  390. * row i (an upper bound). The external degree is the number of nonzeros
  391. * in row i, minus ABS (Nv [i]), the diagonal part. The bound is equal to
  392. * the exact external degree if Elen [i] is less than or equal to two.
  393. *
  394. * We also use the term "external degree" for elements e to refer to
  395. * |Le \ Lme|. If e is an element, then Degree [e] is |Le|, which is the
  396. * degree of the off-diagonal part of the element e (not including the
  397. * diagonal part).
  398. *
  399. * Head: An integer array of size n. Head is used for degree lists.
  400. * Head [deg] is the first supervariable in a degree list. All
  401. * supervariables i in a degree list Head [deg] have the same approximate
  402. * degree, namely, deg = Degree [i]. If the list Head [deg] is empty then
  403. * Head [deg] = EMPTY.
  404. *
  405. * During supervariable detection Head [hash] also serves as a pointer to
  406. * a hash bucket. If Head [hash] >= 0, there is a degree list of degree
  407. * hash. The hash bucket head pointer is Last [Head [hash]]. If
  408. * Head [hash] = EMPTY, then the degree list and hash bucket are both
  409. * empty. If Head [hash] < EMPTY, then the degree list is empty, and
  410. * FLIP (Head [hash]) is the head of the hash bucket. After supervariable
  411. * detection is complete, all hash buckets are empty, and the
  412. * (Last [Head [hash]] = EMPTY) condition is restored for the non-empty
  413. * degree lists.
  414. *
  415. * W: An integer array of size n. The flag array W determines the status of
  416. * elements and variables, and the external degree of elements.
  417. *
  418. * for elements:
  419. * if W [e] = 0, then the element e is absorbed.
  420. * if W [e] >= wflg, then W [e] - wflg is the size of the set
  421. * |Le \ Lme|, in terms of nonzeros (the sum of ABS (Nv [i]) for
  422. * each principal variable i that is both in the pattern of
  423. * element e and NOT in the pattern of the current pivot element,
  424. * me).
  425. * if wflg > W [e] > 0, then e is not absorbed and has not yet been
  426. * seen in the scan of the element lists in the computation of
  427. * |Le\Lme| in Scan 1 below.
  428. *
  429. * for variables:
  430. * during supervariable detection, if W [j] != wflg then j is
  431. * not in the pattern of variable i.
  432. *
  433. * The W array is initialized by setting W [i] = 1 for all i, and by
  434. * setting wflg = 2. It is reinitialized if wflg becomes too large (to
  435. * ensure that wflg+n does not cause integer overflow).
  436. * ----------------------------------------------------------------------------
  437. * LOCAL INTEGERS:
  438. * ----------------------------------------------------------------------------
  439. */
  440. Int deg, degme, dext, lemax, e, elenme, eln, i, ilast, inext, j,
  441. jlast, jnext, k, knt1, knt2, knt3, lenj, ln, me, mindeg, nel, nleft,
  442. nvi, nvj, nvpiv, slenme, wbig, we, wflg, wnvi, ok, ndense, ncmpa,
  443. dense, aggressive ;
  444. unsigned Int hash ; /* unsigned, so that hash % n is well defined.*/
  445. /*
  446. * deg: the degree of a variable or element
  447. * degme: size, |Lme|, of the current element, me (= Degree [me])
  448. * dext: external degree, |Le \ Lme|, of some element e
  449. * lemax: largest |Le| seen so far (called dmax in Fortran version)
  450. * e: an element
  451. * elenme: the length, Elen [me], of element list of pivotal variable
  452. * eln: the length, Elen [...], of an element list
  453. * hash: the computed value of the hash function
  454. * i: a supervariable
  455. * ilast: the entry in a link list preceding i
  456. * inext: the entry in a link list following i
  457. * j: a supervariable
  458. * jlast: the entry in a link list preceding j
  459. * jnext: the entry in a link list, or path, following j
  460. * k: the pivot order of an element or variable
  461. * knt1: loop counter used during element construction
  462. * knt2: loop counter used during element construction
  463. * knt3: loop counter used during compression
  464. * lenj: Len [j]
  465. * ln: length of a supervariable list
  466. * me: current supervariable being eliminated, and the current
  467. * element created by eliminating that supervariable
  468. * mindeg: current minimum degree
  469. * nel: number of pivots selected so far
  470. * nleft: n - nel, the number of nonpivotal rows/columns remaining
  471. * nvi: the number of variables in a supervariable i (= Nv [i])
  472. * nvj: the number of variables in a supervariable j (= Nv [j])
  473. * nvpiv: number of pivots in current element
  474. * slenme: number of variables in variable list of pivotal variable
  475. * wbig: = INT_MAX - n for the int version, UF_long_max - n for the
  476. * UF_long version. wflg is not allowed to be >= wbig.
  477. * we: W [e]
  478. * wflg: used for flagging the W array. See description of Iw.
  479. * wnvi: wflg - Nv [i]
  480. * x: either a supervariable or an element
  481. *
  482. * ok: true if supervariable j can be absorbed into i
  483. * ndense: number of "dense" rows/columns
  484. * dense: rows/columns with initial degree > dense are considered "dense"
  485. * aggressive: true if aggressive absorption is being performed
  486. * ncmpa: number of garbage collections
  487. * ----------------------------------------------------------------------------
  488. * LOCAL DOUBLES, used for statistical output only (except for alpha):
  489. * ----------------------------------------------------------------------------
  490. */
  491. double f, r, ndiv, s, nms_lu, nms_ldl, dmax, alpha, lnz, lnzme ;
  492. /*
  493. * f: nvpiv
  494. * r: degme + nvpiv
  495. * ndiv: number of divisions for LU or LDL' factorizations
  496. * s: number of multiply-subtract pairs for LU factorization, for the
  497. * current element me
  498. * nms_lu number of multiply-subtract pairs for LU factorization
  499. * nms_ldl number of multiply-subtract pairs for LDL' factorization
  500. * dmax: the largest number of entries in any column of L, including the
  501. * diagonal
  502. * alpha: "dense" degree ratio
  503. * lnz: the number of nonzeros in L (excluding the diagonal)
  504. * lnzme: the number of nonzeros in L (excl. the diagonal) for the
  505. * current element me
  506. * ----------------------------------------------------------------------------
  507. * LOCAL "POINTERS" (indices into the Iw array)
  508. * ----------------------------------------------------------------------------
  509. */
  510. Int p, p1, p2, p3, p4, pdst, pend, pj, pme, pme1, pme2, pn, psrc ;
  511. /*
  512. * Any parameter (Pe [...] or pfree) or local variable starting with "p" (for
  513. * Pointer) is an index into Iw, and all indices into Iw use variables starting
  514. * with "p." The only exception to this rule is the iwlen input argument.
  515. *
  516. * p: pointer into lots of things
  517. * p1: Pe [i] for some variable i (start of element list)
  518. * p2: Pe [i] + Elen [i] - 1 for some variable i
  519. * p3: index of first supervariable in clean list
  520. * p4:
  521. * pdst: destination pointer, for compression
  522. * pend: end of memory to compress
  523. * pj: pointer into an element or variable
  524. * pme: pointer into the current element (pme1...pme2)
  525. * pme1: the current element, me, is stored in Iw [pme1...pme2]
  526. * pme2: the end of the current element
  527. * pn: pointer into a "clean" variable, also used to compress
  528. * psrc: source pointer, for compression
  529. */
  530. /* ========================================================================= */
  531. /* INITIALIZATIONS */
  532. /* ========================================================================= */
  533. /* Note that this restriction on iwlen is slightly more restrictive than
  534. * what is actually required in AMD_2. AMD_2 can operate with no elbow
  535. * room at all, but it will be slow. For better performance, at least
  536. * size-n elbow room is enforced. */
  537. ASSERT (iwlen >= pfree + n) ;
  538. ASSERT (n > 0) ;
  539. /* initialize output statistics */
  540. lnz = 0 ;
  541. ndiv = 0 ;
  542. nms_lu = 0 ;
  543. nms_ldl = 0 ;
  544. dmax = 1 ;
  545. me = EMPTY ;
  546. mindeg = 0 ;
  547. ncmpa = 0 ;
  548. nel = 0 ;
  549. lemax = 0 ;
  550. /* get control parameters */
  551. if (Control != (double *) NULL)
  552. {
  553. alpha = Control [AMD_DENSE] ;
  554. aggressive = (Control [AMD_AGGRESSIVE] != 0) ;
  555. }
  556. else
  557. {
  558. alpha = AMD_DEFAULT_DENSE ;
  559. aggressive = AMD_DEFAULT_AGGRESSIVE ;
  560. }
  561. /* Note: if alpha is NaN, this is undefined: */
  562. if (alpha < 0)
  563. {
  564. /* only remove completely dense rows/columns */
  565. dense = n-2 ;
  566. }
  567. else
  568. {
  569. dense = alpha * sqrt ((double) n) ;
  570. }
  571. dense = MAX (16, dense) ;
  572. dense = MIN (n, dense) ;
  573. AMD_DEBUG1 (("\n\nAMD (debug), alpha %g, aggr. "ID"\n",
  574. alpha, aggressive)) ;
  575. for (i = 0 ; i < n ; i++)
  576. {
  577. Last [i] = EMPTY ;
  578. Head [i] = EMPTY ;
  579. Next [i] = EMPTY ;
  580. /* if separate Hhead array is used for hash buckets: *
  581. Hhead [i] = EMPTY ;
  582. */
  583. Nv [i] = 1 ;
  584. W [i] = 1 ;
  585. Elen [i] = 0 ;
  586. Degree [i] = Len [i] ;
  587. }
  588. #ifndef NDEBUG
  589. AMD_DEBUG1 (("\n======Nel "ID" initial\n", nel)) ;
  590. AMD_dump (n, Pe, Iw, Len, iwlen, pfree, Nv, Next, Last,
  591. Head, Elen, Degree, W, -1) ;
  592. #endif
  593. /* initialize wflg */
  594. wbig = Int_MAX - n ;
  595. wflg = clear_flag (0, wbig, W, n) ;
  596. /* --------------------------------------------------------------------- */
  597. /* initialize degree lists and eliminate dense and empty rows */
  598. /* --------------------------------------------------------------------- */
  599. ndense = 0 ;
  600. for (i = 0 ; i < n ; i++)
  601. {
  602. deg = Degree [i] ;
  603. ASSERT (deg >= 0 && deg < n) ;
  604. if (deg == 0)
  605. {
  606. /* -------------------------------------------------------------
  607. * we have a variable that can be eliminated at once because
  608. * there is no off-diagonal non-zero in its row. Note that
  609. * Nv [i] = 1 for an empty variable i. It is treated just
  610. * the same as an eliminated element i.
  611. * ------------------------------------------------------------- */
  612. Elen [i] = FLIP (1) ;
  613. nel++ ;
  614. Pe [i] = EMPTY ;
  615. W [i] = 0 ;
  616. }
  617. else if (deg > dense)
  618. {
  619. /* -------------------------------------------------------------
  620. * Dense variables are not treated as elements, but as unordered,
  621. * non-principal variables that have no parent. They do not take
  622. * part in the postorder, since Nv [i] = 0. Note that the Fortran
  623. * version does not have this option.
  624. * ------------------------------------------------------------- */
  625. AMD_DEBUG1 (("Dense node "ID" degree "ID"\n", i, deg)) ;
  626. ndense++ ;
  627. Nv [i] = 0 ; /* do not postorder this node */
  628. Elen [i] = EMPTY ;
  629. nel++ ;
  630. Pe [i] = EMPTY ;
  631. }
  632. else
  633. {
  634. /* -------------------------------------------------------------
  635. * place i in the degree list corresponding to its degree
  636. * ------------------------------------------------------------- */
  637. inext = Head [deg] ;
  638. ASSERT (inext >= EMPTY && inext < n) ;
  639. if (inext != EMPTY) Last [inext] = i ;
  640. Next [i] = inext ;
  641. Head [deg] = i ;
  642. }
  643. }
  644. /* ========================================================================= */
  645. /* WHILE (selecting pivots) DO */
  646. /* ========================================================================= */
  647. while (nel < n)
  648. {
  649. #ifndef NDEBUG
  650. AMD_DEBUG1 (("\n======Nel "ID"\n", nel)) ;
  651. if (AMD_debug >= 2)
  652. {
  653. AMD_dump (n, Pe, Iw, Len, iwlen, pfree, Nv, Next,
  654. Last, Head, Elen, Degree, W, nel) ;
  655. }
  656. #endif
  657. /* ========================================================================= */
  658. /* GET PIVOT OF MINIMUM DEGREE */
  659. /* ========================================================================= */
  660. /* ----------------------------------------------------------------- */
  661. /* find next supervariable for elimination */
  662. /* ----------------------------------------------------------------- */
  663. ASSERT (mindeg >= 0 && mindeg < n) ;
  664. for (deg = mindeg ; deg < n ; deg++)
  665. {
  666. me = Head [deg] ;
  667. if (me != EMPTY) break ;
  668. }
  669. mindeg = deg ;
  670. ASSERT (me >= 0 && me < n) ;
  671. AMD_DEBUG1 (("=================me: "ID"\n", me)) ;
  672. /* ----------------------------------------------------------------- */
  673. /* remove chosen variable from link list */
  674. /* ----------------------------------------------------------------- */
  675. inext = Next [me] ;
  676. ASSERT (inext >= EMPTY && inext < n) ;
  677. if (inext != EMPTY) Last [inext] = EMPTY ;
  678. Head [deg] = inext ;
  679. /* ----------------------------------------------------------------- */
  680. /* me represents the elimination of pivots nel to nel+Nv[me]-1. */
  681. /* place me itself as the first in this set. */
  682. /* ----------------------------------------------------------------- */
  683. elenme = Elen [me] ;
  684. nvpiv = Nv [me] ;
  685. ASSERT (nvpiv > 0) ;
  686. nel += nvpiv ;
  687. /* ========================================================================= */
  688. /* CONSTRUCT NEW ELEMENT */
  689. /* ========================================================================= */
  690. /* -----------------------------------------------------------------
  691. * At this point, me is the pivotal supervariable. It will be
  692. * converted into the current element. Scan list of the pivotal
  693. * supervariable, me, setting tree pointers and constructing new list
  694. * of supervariables for the new element, me. p is a pointer to the
  695. * current position in the old list.
  696. * ----------------------------------------------------------------- */
  697. /* flag the variable "me" as being in Lme by negating Nv [me] */
  698. Nv [me] = -nvpiv ;
  699. degme = 0 ;
  700. ASSERT (Pe [me] >= 0 && Pe [me] < iwlen) ;
  701. if (elenme == 0)
  702. {
  703. /* ------------------------------------------------------------- */
  704. /* construct the new element in place */
  705. /* ------------------------------------------------------------- */
  706. pme1 = Pe [me] ;
  707. pme2 = pme1 - 1 ;
  708. for (p = pme1 ; p <= pme1 + Len [me] - 1 ; p++)
  709. {
  710. i = Iw [p] ;
  711. ASSERT (i >= 0 && i < n && Nv [i] >= 0) ;
  712. nvi = Nv [i] ;
  713. if (nvi > 0)
  714. {
  715. /* ----------------------------------------------------- */
  716. /* i is a principal variable not yet placed in Lme. */
  717. /* store i in new list */
  718. /* ----------------------------------------------------- */
  719. /* flag i as being in Lme by negating Nv [i] */
  720. degme += nvi ;
  721. Nv [i] = -nvi ;
  722. Iw [++pme2] = i ;
  723. /* ----------------------------------------------------- */
  724. /* remove variable i from degree list. */
  725. /* ----------------------------------------------------- */
  726. ilast = Last [i] ;
  727. inext = Next [i] ;
  728. ASSERT (ilast >= EMPTY && ilast < n) ;
  729. ASSERT (inext >= EMPTY && inext < n) ;
  730. if (inext != EMPTY) Last [inext] = ilast ;
  731. if (ilast != EMPTY)
  732. {
  733. Next [ilast] = inext ;
  734. }
  735. else
  736. {
  737. /* i is at the head of the degree list */
  738. ASSERT (Degree [i] >= 0 && Degree [i] < n) ;
  739. Head [Degree [i]] = inext ;
  740. }
  741. }
  742. }
  743. }
  744. else
  745. {
  746. /* ------------------------------------------------------------- */
  747. /* construct the new element in empty space, Iw [pfree ...] */
  748. /* ------------------------------------------------------------- */
  749. p = Pe [me] ;
  750. pme1 = pfree ;
  751. slenme = Len [me] - elenme ;
  752. for (knt1 = 1 ; knt1 <= elenme + 1 ; knt1++)
  753. {
  754. if (knt1 > elenme)
  755. {
  756. /* search the supervariables in me. */
  757. e = me ;
  758. pj = p ;
  759. ln = slenme ;
  760. AMD_DEBUG2 (("Search sv: "ID" "ID" "ID"\n", me,pj,ln)) ;
  761. }
  762. else
  763. {
  764. /* search the elements in me. */
  765. e = Iw [p++] ;
  766. ASSERT (e >= 0 && e < n) ;
  767. pj = Pe [e] ;
  768. ln = Len [e] ;
  769. AMD_DEBUG2 (("Search element e "ID" in me "ID"\n", e,me)) ;
  770. ASSERT (Elen [e] < EMPTY && W [e] > 0 && pj >= 0) ;
  771. }
  772. ASSERT (ln >= 0 && (ln == 0 || (pj >= 0 && pj < iwlen))) ;
  773. /* ---------------------------------------------------------
  774. * search for different supervariables and add them to the
  775. * new list, compressing when necessary. this loop is
  776. * executed once for each element in the list and once for
  777. * all the supervariables in the list.
  778. * --------------------------------------------------------- */
  779. for (knt2 = 1 ; knt2 <= ln ; knt2++)
  780. {
  781. i = Iw [pj++] ;
  782. ASSERT (i >= 0 && i < n && (i == me || Elen [i] >= EMPTY));
  783. nvi = Nv [i] ;
  784. AMD_DEBUG2 ((": "ID" "ID" "ID" "ID"\n",
  785. i, Elen [i], Nv [i], wflg)) ;
  786. if (nvi > 0)
  787. {
  788. /* ------------------------------------------------- */
  789. /* compress Iw, if necessary */
  790. /* ------------------------------------------------- */
  791. if (pfree >= iwlen)
  792. {
  793. AMD_DEBUG1 (("GARBAGE COLLECTION\n")) ;
  794. /* prepare for compressing Iw by adjusting pointers
  795. * and lengths so that the lists being searched in
  796. * the inner and outer loops contain only the
  797. * remaining entries. */
  798. Pe [me] = p ;
  799. Len [me] -= knt1 ;
  800. /* check if nothing left of supervariable me */
  801. if (Len [me] == 0) Pe [me] = EMPTY ;
  802. Pe [e] = pj ;
  803. Len [e] = ln - knt2 ;
  804. /* nothing left of element e */
  805. if (Len [e] == 0) Pe [e] = EMPTY ;
  806. ncmpa++ ; /* one more garbage collection */
  807. /* store first entry of each object in Pe */
  808. /* FLIP the first entry in each object */
  809. for (j = 0 ; j < n ; j++)
  810. {
  811. pn = Pe [j] ;
  812. if (pn >= 0)
  813. {
  814. ASSERT (pn >= 0 && pn < iwlen) ;
  815. Pe [j] = Iw [pn] ;
  816. Iw [pn] = FLIP (j) ;
  817. }
  818. }
  819. /* psrc/pdst point to source/destination */
  820. psrc = 0 ;
  821. pdst = 0 ;
  822. pend = pme1 - 1 ;
  823. while (psrc <= pend)
  824. {
  825. /* search for next FLIP'd entry */
  826. j = FLIP (Iw [psrc++]) ;
  827. if (j >= 0)
  828. {
  829. AMD_DEBUG2 (("Got object j: "ID"\n", j)) ;
  830. Iw [pdst] = Pe [j] ;
  831. Pe [j] = pdst++ ;
  832. lenj = Len [j] ;
  833. /* copy from source to destination */
  834. for (knt3 = 0 ; knt3 <= lenj - 2 ; knt3++)
  835. {
  836. Iw [pdst++] = Iw [psrc++] ;
  837. }
  838. }
  839. }
  840. /* move the new partially-constructed element */
  841. p1 = pdst ;
  842. for (psrc = pme1 ; psrc <= pfree-1 ; psrc++)
  843. {
  844. Iw [pdst++] = Iw [psrc] ;
  845. }
  846. pme1 = p1 ;
  847. pfree = pdst ;
  848. pj = Pe [e] ;
  849. p = Pe [me] ;
  850. }
  851. /* ------------------------------------------------- */
  852. /* i is a principal variable not yet placed in Lme */
  853. /* store i in new list */
  854. /* ------------------------------------------------- */
  855. /* flag i as being in Lme by negating Nv [i] */
  856. degme += nvi ;
  857. Nv [i] = -nvi ;
  858. Iw [pfree++] = i ;
  859. AMD_DEBUG2 ((" s: "ID" nv "ID"\n", i, Nv [i]));
  860. /* ------------------------------------------------- */
  861. /* remove variable i from degree link list */
  862. /* ------------------------------------------------- */
  863. ilast = Last [i] ;
  864. inext = Next [i] ;
  865. ASSERT (ilast >= EMPTY && ilast < n) ;
  866. ASSERT (inext >= EMPTY && inext < n) ;
  867. if (inext != EMPTY) Last [inext] = ilast ;
  868. if (ilast != EMPTY)
  869. {
  870. Next [ilast] = inext ;
  871. }
  872. else
  873. {
  874. /* i is at the head of the degree list */
  875. ASSERT (Degree [i] >= 0 && Degree [i] < n) ;
  876. Head [Degree [i]] = inext ;
  877. }
  878. }
  879. }
  880. if (e != me)
  881. {
  882. /* set tree pointer and flag to indicate element e is
  883. * absorbed into new element me (the parent of e is me) */
  884. AMD_DEBUG1 ((" Element "ID" => "ID"\n", e, me)) ;
  885. Pe [e] = FLIP (me) ;
  886. W [e] = 0 ;
  887. }
  888. }
  889. pme2 = pfree - 1 ;
  890. }
  891. /* ----------------------------------------------------------------- */
  892. /* me has now been converted into an element in Iw [pme1..pme2] */
  893. /* ----------------------------------------------------------------- */
  894. /* degme holds the external degree of new element */
  895. Degree [me] = degme ;
  896. Pe [me] = pme1 ;
  897. Len [me] = pme2 - pme1 + 1 ;
  898. ASSERT (Pe [me] >= 0 && Pe [me] < iwlen) ;
  899. Elen [me] = FLIP (nvpiv + degme) ;
  900. /* FLIP (Elen (me)) is now the degree of pivot (including
  901. * diagonal part). */
  902. #ifndef NDEBUG
  903. AMD_DEBUG2 (("New element structure: length= "ID"\n", pme2-pme1+1)) ;
  904. for (pme = pme1 ; pme <= pme2 ; pme++) AMD_DEBUG3 ((" "ID"", Iw[pme]));
  905. AMD_DEBUG3 (("\n")) ;
  906. #endif
  907. /* ----------------------------------------------------------------- */
  908. /* make sure that wflg is not too large. */
  909. /* ----------------------------------------------------------------- */
  910. /* With the current value of wflg, wflg+n must not cause integer
  911. * overflow */
  912. wflg = clear_flag (wflg, wbig, W, n) ;
  913. /* ========================================================================= */
  914. /* COMPUTE (W [e] - wflg) = |Le\Lme| FOR ALL ELEMENTS */
  915. /* ========================================================================= */
  916. /* -----------------------------------------------------------------
  917. * Scan 1: compute the external degrees of previous elements with
  918. * respect to the current element. That is:
  919. * (W [e] - wflg) = |Le \ Lme|
  920. * for each element e that appears in any supervariable in Lme. The
  921. * notation Le refers to the pattern (list of supervariables) of a
  922. * previous element e, where e is not yet absorbed, stored in
  923. * Iw [Pe [e] + 1 ... Pe [e] + Len [e]]. The notation Lme
  924. * refers to the pattern of the current element (stored in
  925. * Iw [pme1..pme2]). If aggressive absorption is enabled, and
  926. * (W [e] - wflg) becomes zero, then the element e will be absorbed
  927. * in Scan 2.
  928. * ----------------------------------------------------------------- */
  929. AMD_DEBUG2 (("me: ")) ;
  930. for (pme = pme1 ; pme <= pme2 ; pme++)
  931. {
  932. i = Iw [pme] ;
  933. ASSERT (i >= 0 && i < n) ;
  934. eln = Elen [i] ;
  935. AMD_DEBUG3 ((""ID" Elen "ID": \n", i, eln)) ;
  936. if (eln > 0)
  937. {
  938. /* note that Nv [i] has been negated to denote i in Lme: */
  939. nvi = -Nv [i] ;
  940. ASSERT (nvi > 0 && Pe [i] >= 0 && Pe [i] < iwlen) ;
  941. wnvi = wflg - nvi ;
  942. for (p = Pe [i] ; p <= Pe [i] + eln - 1 ; p++)
  943. {
  944. e = Iw [p] ;
  945. ASSERT (e >= 0 && e < n) ;
  946. we = W [e] ;
  947. AMD_DEBUG4 ((" e "ID" we "ID" ", e, we)) ;
  948. if (we >= wflg)
  949. {
  950. /* unabsorbed element e has been seen in this loop */
  951. AMD_DEBUG4 ((" unabsorbed, first time seen")) ;
  952. we -= nvi ;
  953. }
  954. else if (we != 0)
  955. {
  956. /* e is an unabsorbed element */
  957. /* this is the first we have seen e in all of Scan 1 */
  958. AMD_DEBUG4 ((" unabsorbed")) ;
  959. we = Degree [e] + wnvi ;
  960. }
  961. AMD_DEBUG4 (("\n")) ;
  962. W [e] = we ;
  963. }
  964. }
  965. }
  966. AMD_DEBUG2 (("\n")) ;
  967. /* ========================================================================= */
  968. /* DEGREE UPDATE AND ELEMENT ABSORPTION */
  969. /* ========================================================================= */
  970. /* -----------------------------------------------------------------
  971. * Scan 2: for each i in Lme, sum up the degree of Lme (which is
  972. * degme), plus the sum of the external degrees of each Le for the
  973. * elements e appearing within i, plus the supervariables in i.
  974. * Place i in hash list.
  975. * ----------------------------------------------------------------- */
  976. for (pme = pme1 ; pme <= pme2 ; pme++)
  977. {
  978. i = Iw [pme] ;
  979. ASSERT (i >= 0 && i < n && Nv [i] < 0 && Elen [i] >= 0) ;
  980. AMD_DEBUG2 (("Updating: i "ID" "ID" "ID"\n", i, Elen[i], Len [i]));
  981. p1 = Pe [i] ;
  982. p2 = p1 + Elen [i] - 1 ;
  983. pn = p1 ;
  984. hash = 0 ;
  985. deg = 0 ;
  986. ASSERT (p1 >= 0 && p1 < iwlen && p2 >= -1 && p2 < iwlen) ;
  987. /* ------------------------------------------------------------- */
  988. /* scan the element list associated with supervariable i */
  989. /* ------------------------------------------------------------- */
  990. /* UMFPACK/MA38-style approximate degree: */
  991. if (aggressive)
  992. {
  993. for (p = p1 ; p <= p2 ; p++)
  994. {
  995. e = Iw [p] ;
  996. ASSERT (e >= 0 && e < n) ;
  997. we = W [e] ;
  998. if (we != 0)
  999. {
  1000. /* e is an unabsorbed element */
  1001. /* dext = | Le \ Lme | */
  1002. dext = we - wflg ;
  1003. if (dext > 0)
  1004. {
  1005. deg += dext ;
  1006. Iw [pn++] = e ;
  1007. hash += e ;
  1008. AMD_DEBUG4 ((" e: "ID" hash = "ID"\n",e,hash)) ;
  1009. }
  1010. else
  1011. {
  1012. /* external degree of e is zero, absorb e into me*/
  1013. AMD_DEBUG1 ((" Element "ID" =>"ID" (aggressive)\n",
  1014. e, me)) ;
  1015. ASSERT (dext == 0) ;
  1016. Pe [e] = FLIP (me) ;
  1017. W [e] = 0 ;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. else
  1023. {
  1024. for (p = p1 ; p <= p2 ; p++)
  1025. {
  1026. e = Iw [p] ;
  1027. ASSERT (e >= 0 && e < n) ;
  1028. we = W [e] ;
  1029. if (we != 0)
  1030. {
  1031. /* e is an unabsorbed element */
  1032. dext = we - wflg ;
  1033. ASSERT (dext >= 0) ;
  1034. deg += dext ;
  1035. Iw [pn++] = e ;
  1036. hash += e ;
  1037. AMD_DEBUG4 ((" e: "ID" hash = "ID"\n",e,hash)) ;
  1038. }
  1039. }
  1040. }
  1041. /* count the number of elements in i (including me): */
  1042. Elen [i] = pn - p1 + 1 ;
  1043. /* ------------------------------------------------------------- */
  1044. /* scan the supervariables in the list associated with i */
  1045. /* ------------------------------------------------------------- */
  1046. /* The bulk of the AMD run time is typically spent in this loop,
  1047. * particularly if the matrix has many dense rows that are not
  1048. * removed prior to ordering. */
  1049. p3 = pn ;
  1050. p4 = p1 + Len [i] ;
  1051. for (p = p2 + 1 ; p < p4 ; p++)
  1052. {
  1053. j = Iw [p] ;
  1054. ASSERT (j >= 0 && j < n) ;
  1055. nvj = Nv [j] ;
  1056. if (nvj > 0)
  1057. {
  1058. /* j is unabsorbed, and not in Lme. */
  1059. /* add to degree and add to new list */
  1060. deg += nvj ;
  1061. Iw [pn++] = j ;
  1062. hash += j ;
  1063. AMD_DEBUG4 ((" s: "ID" hash "ID" Nv[j]= "ID"\n",
  1064. j, hash, nvj)) ;
  1065. }
  1066. }
  1067. /* ------------------------------------------------------------- */
  1068. /* update the degree and check for mass elimination */
  1069. /* ------------------------------------------------------------- */
  1070. /* with aggressive absorption, deg==0 is identical to the
  1071. * Elen [i] == 1 && p3 == pn test, below. */
  1072. ASSERT (IMPLIES (aggressive, (deg==0) == (Elen[i]==1 && p3==pn))) ;
  1073. if (Elen [i] == 1 && p3 == pn)
  1074. {
  1075. /* --------------------------------------------------------- */
  1076. /* mass elimination */
  1077. /* --------------------------------------------------------- */
  1078. /* There is nothing left of this node except for an edge to
  1079. * the current pivot element. Elen [i] is 1, and there are
  1080. * no variables adjacent to node i. Absorb i into the
  1081. * current pivot element, me. Note that if there are two or
  1082. * more mass eliminations, fillin due to mass elimination is
  1083. * possible within the nvpiv-by-nvpiv pivot block. It is this
  1084. * step that causes AMD's analysis to be an upper bound.
  1085. *
  1086. * The reason is that the selected pivot has a lower
  1087. * approximate degree than the true degree of the two mass
  1088. * eliminated nodes. There is no edge between the two mass
  1089. * eliminated nodes. They are merged with the current pivot
  1090. * anyway.
  1091. *
  1092. * No fillin occurs in the Schur complement, in any case,
  1093. * and this effect does not decrease the quality of the
  1094. * ordering itself, just the quality of the nonzero and
  1095. * flop count analysis. It also means that the post-ordering
  1096. * is not an exact elimination tree post-ordering. */
  1097. AMD_DEBUG1 ((" MASS i "ID" => parent e "ID"\n", i, me)) ;
  1098. Pe [i] = FLIP (me) ;
  1099. nvi = -Nv [i] ;
  1100. degme -= nvi ;
  1101. nvpiv += nvi ;
  1102. nel += nvi ;
  1103. Nv [i] = 0 ;
  1104. Elen [i] = EMPTY ;
  1105. }
  1106. else
  1107. {
  1108. /* --------------------------------------------------------- */
  1109. /* update the upper-bound degree of i */
  1110. /* --------------------------------------------------------- */
  1111. /* the following degree does not yet include the size
  1112. * of the current element, which is added later: */
  1113. Degree [i] = MIN (Degree [i], deg) ;
  1114. /* --------------------------------------------------------- */
  1115. /* add me to the list for i */
  1116. /* --------------------------------------------------------- */
  1117. /* move first supervariable to end of list */
  1118. Iw [pn] = Iw [p3] ;
  1119. /* move first element to end of element part of list */
  1120. Iw [p3] = Iw [p1] ;
  1121. /* add new element, me, to front of list. */
  1122. Iw [p1] = me ;
  1123. /* store the new length of the list in Len [i] */
  1124. Len [i] = pn - p1 + 1 ;
  1125. /* --------------------------------------------------------- */
  1126. /* place in hash bucket. Save hash key of i in Last [i]. */
  1127. /* --------------------------------------------------------- */
  1128. /* NOTE: this can fail if hash is negative, because the ANSI C
  1129. * standard does not define a % b when a and/or b are negative.
  1130. * That's why hash is defined as an unsigned Int, to avoid this
  1131. * problem. */
  1132. hash = hash % n ;
  1133. ASSERT (((Int) hash) >= 0 && ((Int) hash) < n) ;
  1134. /* if the Hhead array is not used: */
  1135. j = Head [hash] ;
  1136. if (j <= EMPTY)
  1137. {
  1138. /* degree list is empty, hash head is FLIP (j) */
  1139. Next [i] = FLIP (j) ;
  1140. Head [hash] = FLIP (i) ;
  1141. }
  1142. else
  1143. {
  1144. /* degree list is not empty, use Last [Head [hash]] as
  1145. * hash head. */
  1146. Next [i] = Last [j] ;
  1147. Last [j] = i ;
  1148. }
  1149. /* if a separate Hhead array is used: *
  1150. Next [i] = Hhead [hash] ;
  1151. Hhead [hash] = i ;
  1152. */
  1153. Last [i] = hash ;
  1154. }
  1155. }
  1156. Degree [me] = degme ;
  1157. /* ----------------------------------------------------------------- */
  1158. /* Clear the counter array, W [...], by incrementing wflg. */
  1159. /* ----------------------------------------------------------------- */
  1160. /* make sure that wflg+n does not cause integer overflow */
  1161. lemax = MAX (lemax, degme) ;
  1162. wflg += lemax ;
  1163. wflg = clear_flag (wflg, wbig, W, n) ;
  1164. /* at this point, W [0..n-1] < wflg holds */
  1165. /* ========================================================================= */
  1166. /* SUPERVARIABLE DETECTION */
  1167. /* ========================================================================= */
  1168. AMD_DEBUG1 (("Detecting supervariables:\n")) ;
  1169. for (pme = pme1 ; pme <= pme2 ; pme++)
  1170. {
  1171. i = Iw [pme] ;
  1172. ASSERT (i >= 0 && i < n) ;
  1173. AMD_DEBUG2 (("Consider i "ID" nv "ID"\n", i, Nv [i])) ;
  1174. if (Nv [i] < 0)
  1175. {
  1176. /* i is a principal variable in Lme */
  1177. /* ---------------------------------------------------------
  1178. * examine all hash buckets with 2 or more variables. We do
  1179. * this by examing all unique hash keys for supervariables in
  1180. * the pattern Lme of the current element, me
  1181. * --------------------------------------------------------- */
  1182. /* let i = head of hash bucket, and empty the hash bucket */
  1183. ASSERT (Last [i] >= 0 && Last [i] < n) ;
  1184. hash = Last [i] ;
  1185. /* if Hhead array is not used: */
  1186. j = Head [hash] ;
  1187. if (j == EMPTY)
  1188. {
  1189. /* hash bucket and degree list are both empty */
  1190. i = EMPTY ;
  1191. }
  1192. else if (j < EMPTY)
  1193. {
  1194. /* degree list is empty */
  1195. i = FLIP (j) ;
  1196. Head [hash] = EMPTY ;
  1197. }
  1198. else
  1199. {
  1200. /* degree list is not empty, restore Last [j] of head j */
  1201. i = Last [j] ;
  1202. Last [j] = EMPTY ;
  1203. }
  1204. /* if separate Hhead array is used: *
  1205. i = Hhead [hash] ;
  1206. Hhead [hash] = EMPTY ;
  1207. */
  1208. ASSERT (i >= EMPTY && i < n) ;
  1209. AMD_DEBUG2 (("----i "ID" hash "ID"\n", i, hash)) ;
  1210. while (i != EMPTY && Next [i] != EMPTY)
  1211. {
  1212. /* -----------------------------------------------------
  1213. * this bucket has one or more variables following i.
  1214. * scan all of them to see if i can absorb any entries
  1215. * that follow i in hash bucket. Scatter i into w.
  1216. * ----------------------------------------------------- */
  1217. ln = Len [i] ;
  1218. eln = Elen [i] ;
  1219. ASSERT (ln >= 0 && eln >= 0) ;
  1220. ASSERT (Pe [i] >= 0 && Pe [i] < iwlen) ;
  1221. /* do not flag the first element in the list (me) */
  1222. for (p = Pe [i] + 1 ; p <= Pe [i] + ln - 1 ; p++)
  1223. {
  1224. ASSERT (Iw [p] >= 0 && Iw [p] < n) ;
  1225. W [Iw [p]] = wflg ;
  1226. }
  1227. /* ----------------------------------------------------- */
  1228. /* scan every other entry j following i in bucket */
  1229. /* ----------------------------------------------------- */
  1230. jlast = i ;
  1231. j = Next [i] ;
  1232. ASSERT (j >= EMPTY && j < n) ;
  1233. while (j != EMPTY)
  1234. {
  1235. /* ------------------------------------------------- */
  1236. /* check if j and i have identical nonzero pattern */
  1237. /* ------------------------------------------------- */
  1238. AMD_DEBUG3 (("compare i "ID" and j "ID"\n", i,j)) ;
  1239. /* check if i and j have the same Len and Elen */
  1240. ASSERT (Len [j] >= 0 && Elen [j] >= 0) ;
  1241. ASSERT (Pe [j] >= 0 && Pe [j] < iwlen) ;
  1242. ok = (Len [j] == ln) && (Elen [j] == eln) ;
  1243. /* skip the first element in the list (me) */
  1244. for (p = Pe [j] + 1 ; ok && p <= Pe [j] + ln - 1 ; p++)
  1245. {
  1246. ASSERT (Iw [p] >= 0 && Iw [p] < n) ;
  1247. if (W [Iw [p]] != wflg) ok = 0 ;
  1248. }
  1249. if (ok)
  1250. {
  1251. /* --------------------------------------------- */
  1252. /* found it! j can be absorbed into i */
  1253. /* --------------------------------------------- */
  1254. AMD_DEBUG1 (("found it! j "ID" => i "ID"\n", j,i));
  1255. Pe [j] = FLIP (i) ;
  1256. /* both Nv [i] and Nv [j] are negated since they */
  1257. /* are in Lme, and the absolute values of each */
  1258. /* are the number of variables in i and j: */
  1259. Nv [i] += Nv [j] ;
  1260. Nv [j] = 0 ;
  1261. Elen [j] = EMPTY ;
  1262. /* delete j from hash bucket */
  1263. ASSERT (j != Next [j]) ;
  1264. j = Next [j] ;
  1265. Next [jlast] = j ;
  1266. }
  1267. else
  1268. {
  1269. /* j cannot be absorbed into i */
  1270. jlast = j ;
  1271. ASSERT (j != Next [j]) ;
  1272. j = Next [j] ;
  1273. }
  1274. ASSERT (j >= EMPTY && j < n) ;
  1275. }
  1276. /* -----------------------------------------------------
  1277. * no more variables can be absorbed into i
  1278. * go to next i in bucket and clear flag array
  1279. * ----------------------------------------------------- */
  1280. wflg++ ;
  1281. i = Next [i] ;
  1282. ASSERT (i >= EMPTY && i < n) ;
  1283. }
  1284. }
  1285. }
  1286. AMD_DEBUG2 (("detect done\n")) ;
  1287. /* ========================================================================= */
  1288. /* RESTORE DEGREE LISTS AND REMOVE NONPRINCIPAL SUPERVARIABLES FROM ELEMENT */
  1289. /* ========================================================================= */
  1290. p = pme1 ;
  1291. nleft = n - nel ;
  1292. for (pme = pme1 ; pme <= pme2 ; pme++)
  1293. {
  1294. i = Iw [pme] ;
  1295. ASSERT (i >= 0 && i < n) ;
  1296. nvi = -Nv [i] ;
  1297. AMD_DEBUG3 (("Restore i "ID" "ID"\n", i, nvi)) ;
  1298. if (nvi > 0)
  1299. {
  1300. /* i is a principal variable in Lme */
  1301. /* restore Nv [i] to signify that i is principal */
  1302. Nv [i] = nvi ;
  1303. /* --------------------------------------------------------- */
  1304. /* compute the external degree (add size of current element) */
  1305. /* --------------------------------------------------------- */
  1306. deg = Degree [i] + degme - nvi ;
  1307. deg = MIN (deg, nleft - nvi) ;
  1308. ASSERT (IMPLIES (aggressive, deg > 0) && deg >= 0 && deg < n) ;
  1309. /* --------------------------------------------------------- */
  1310. /* place the supervariable at the head of the degree list */
  1311. /* --------------------------------------------------------- */
  1312. inext = Head [deg] ;
  1313. ASSERT (inext >= EMPTY && inext < n) ;
  1314. if (inext != EMPTY) Last [inext] = i ;
  1315. Next [i] = inext ;
  1316. Last [i] = EMPTY ;
  1317. Head [deg] = i ;
  1318. /* --------------------------------------------------------- */
  1319. /* save the new degree, and find the minimum degree */
  1320. /* --------------------------------------------------------- */
  1321. mindeg = MIN (mindeg, deg) ;
  1322. Degree [i] = deg ;
  1323. /* --------------------------------------------------------- */
  1324. /* place the supervariable in the element pattern */
  1325. /* --------------------------------------------------------- */
  1326. Iw [p++] = i ;
  1327. }
  1328. }
  1329. AMD_DEBUG2 (("restore done\n")) ;
  1330. /* ========================================================================= */
  1331. /* FINALIZE THE NEW ELEMENT */
  1332. /* ========================================================================= */
  1333. AMD_DEBUG2 (("ME = "ID" DONE\n", me)) ;
  1334. Nv [me] = nvpiv ;
  1335. /* save the length of the list for the new element me */
  1336. Len [me] = p - pme1 ;
  1337. if (Len [me] == 0)
  1338. {
  1339. /* there is nothing left of the current pivot element */
  1340. /* it is a root of the assembly tree */
  1341. Pe [me] = EMPTY ;
  1342. W [me] = 0 ;
  1343. }
  1344. if (elenme != 0)
  1345. {
  1346. /* element was not constructed in place: deallocate part of */
  1347. /* it since newly nonprincipal variables may have been removed */
  1348. pfree = p ;
  1349. }
  1350. /* The new element has nvpiv pivots and the size of the contribution
  1351. * block for a multifrontal method is degme-by-degme, not including
  1352. * the "dense" rows/columns. If the "dense" rows/columns are included,
  1353. * the frontal matrix is no larger than
  1354. * (degme+ndense)-by-(degme+ndense).
  1355. */
  1356. if (Info != (double *) NULL)
  1357. {
  1358. f = nvpiv ;
  1359. r = degme + ndense ;
  1360. dmax = MAX (dmax, f + r) ;
  1361. /* number of nonzeros in L (excluding the diagonal) */
  1362. lnzme = f*r + (f-1)*f/2 ;
  1363. lnz += lnzme ;
  1364. /* number of divide operations for LDL' and for LU */
  1365. ndiv += lnzme ;
  1366. /* number of multiply-subtract pairs for LU */
  1367. s = f*r*r + r*(f-1)*f + (f-1)*f*(2*f-1)/6 ;
  1368. nms_lu += s ;
  1369. /* number of multiply-subtract pairs for LDL' */
  1370. nms_ldl += (s + lnzme)/2 ;
  1371. }
  1372. #ifndef NDEBUG
  1373. AMD_DEBUG2 (("finalize done nel "ID" n "ID"\n ::::\n", nel, n)) ;
  1374. for (pme = Pe [me] ; pme <= Pe [me] + Len [me] - 1 ; pme++)
  1375. {
  1376. AMD_DEBUG3 ((" "ID"", Iw [pme])) ;
  1377. }
  1378. AMD_DEBUG3 (("\n")) ;
  1379. #endif
  1380. }
  1381. /* ========================================================================= */
  1382. /* DONE SELECTING PIVOTS */
  1383. /* ========================================================================= */
  1384. if (Info != (double *) NULL)
  1385. {
  1386. /* count the work to factorize the ndense-by-ndense submatrix */
  1387. f = ndense ;
  1388. dmax = MAX (dmax, (double) ndense) ;
  1389. /* number of nonzeros in L (excluding the diagonal) */
  1390. lnzme = (f-1)*f/2 ;
  1391. lnz += lnzme ;
  1392. /* number of divide operations for LDL' and for LU */
  1393. ndiv += lnzme ;
  1394. /* number of multiply-subtract pairs for LU */
  1395. s = (f-1)*f*(2*f-1)/6 ;
  1396. nms_lu += s ;
  1397. /* number of multiply-subtract pairs for LDL' */
  1398. nms_ldl += (s + lnzme)/2 ;
  1399. /* number of nz's in L (excl. diagonal) */
  1400. Info [AMD_LNZ] = lnz ;
  1401. /* number of divide ops for LU and LDL' */
  1402. Info [AMD_NDIV] = ndiv ;
  1403. /* number of multiply-subtract pairs for LDL' */
  1404. Info [AMD_NMULTSUBS_LDL] = nms_ldl ;
  1405. /* number of multiply-subtract pairs for LU */
  1406. Info [AMD_NMULTSUBS_LU] = nms_lu ;
  1407. /* number of "dense" rows/columns */
  1408. Info [AMD_NDENSE] = ndense ;
  1409. /* largest front is dmax-by-dmax */
  1410. Info [AMD_DMAX] = dmax ;
  1411. /* number of garbage collections in AMD */
  1412. Info [AMD_NCMPA] = ncmpa ;
  1413. /* successful ordering */
  1414. Info [AMD_STATUS] = AMD_OK ;
  1415. }
  1416. /* ========================================================================= */
  1417. /* POST-ORDERING */
  1418. /* ========================================================================= */
  1419. /* -------------------------------------------------------------------------
  1420. * Variables at this point:
  1421. *
  1422. * Pe: holds the elimination tree. The parent of j is FLIP (Pe [j]),
  1423. * or EMPTY if j is a root. The tree holds both elements and
  1424. * non-principal (unordered) variables absorbed into them.
  1425. * Dense variables are non-principal and unordered.
  1426. *
  1427. * Elen: holds the size of each element, including the diagonal part.
  1428. * FLIP (Elen [e]) > 0 if e is an element. For unordered
  1429. * variables i, Elen [i] is EMPTY.
  1430. *
  1431. * Nv: Nv [e] > 0 is the number of pivots represented by the element e.
  1432. * For unordered variables i, Nv [i] is zero.
  1433. *
  1434. * Contents no longer needed:
  1435. * W, Iw, Len, Degree, Head, Next, Last.
  1436. *
  1437. * The matrix itself has been destroyed.
  1438. *
  1439. * n: the size of the matrix.
  1440. * No other scalars needed (pfree, iwlen, etc.)
  1441. * ------------------------------------------------------------------------- */
  1442. /* restore Pe */
  1443. for (i = 0 ; i < n ; i++)
  1444. {
  1445. Pe [i] = FLIP (Pe [i]) ;
  1446. }
  1447. /* restore Elen, for output information, and for postordering */
  1448. for (i = 0 ; i < n ; i++)
  1449. {
  1450. Elen [i] = FLIP (Elen [i]) ;
  1451. }
  1452. /* Now the parent of j is Pe [j], or EMPTY if j is a root. Elen [e] > 0
  1453. * is the size of element e. Elen [i] is EMPTY for unordered variable i. */
  1454. #ifndef NDEBUG
  1455. AMD_DEBUG2 (("\nTree:\n")) ;
  1456. for (i = 0 ; i < n ; i++)
  1457. {
  1458. AMD_DEBUG2 ((" "ID" parent: "ID" ", i, Pe [i])) ;
  1459. ASSERT (Pe [i] >= EMPTY && Pe [i] < n) ;
  1460. if (Nv [i] > 0)
  1461. {
  1462. /* this is an element */
  1463. e = i ;
  1464. AMD_DEBUG2 ((" element, size is "ID"\n", Elen [i])) ;
  1465. ASSERT (Elen [e] > 0) ;
  1466. }
  1467. AMD_DEBUG2 (("\n")) ;
  1468. }
  1469. AMD_DEBUG2 (("\nelements:\n")) ;
  1470. for (e = 0 ; e < n ; e++)
  1471. {
  1472. if (Nv [e] > 0)
  1473. {
  1474. AMD_DEBUG3 (("Element e= "ID" size "ID" nv "ID" \n", e,
  1475. Elen [e], Nv [e])) ;
  1476. }
  1477. }
  1478. AMD_DEBUG2 (("\nvariables:\n")) ;
  1479. for (i = 0 ; i < n ; i++)
  1480. {
  1481. Int cnt ;
  1482. if (Nv [i] == 0)
  1483. {
  1484. AMD_DEBUG3 (("i unordered: "ID"\n", i)) ;
  1485. j = Pe [i] ;
  1486. cnt = 0 ;
  1487. AMD_DEBUG3 ((" j: "ID"\n", j)) ;
  1488. if (j == EMPTY)
  1489. {
  1490. AMD_DEBUG3 ((" i is a dense variable\n")) ;
  1491. }
  1492. else
  1493. {
  1494. ASSERT (j >= 0 && j < n) ;
  1495. while (Nv [j] == 0)
  1496. {
  1497. AMD_DEBUG3 ((" j : "ID"\n", j)) ;
  1498. j = Pe [j] ;
  1499. AMD_DEBUG3 ((" j:: "ID"\n", j)) ;
  1500. cnt++ ;
  1501. if (cnt > n) break ;
  1502. }
  1503. e = j ;
  1504. AMD_DEBUG3 ((" got to e: "ID"\n", e)) ;
  1505. }
  1506. }
  1507. }
  1508. #endif
  1509. /* ========================================================================= */
  1510. /* compress the paths of the variables */
  1511. /* ========================================================================= */
  1512. for (i = 0 ; i < n ; i++)
  1513. {
  1514. if (Nv [i] == 0)
  1515. {
  1516. /* -------------------------------------------------------------
  1517. * i is an un-ordered row. Traverse the tree from i until
  1518. * reaching an element, e. The element, e, was the principal
  1519. * supervariable of i and all nodes in the path from i to when e
  1520. * was selected as pivot.
  1521. * ------------------------------------------------------------- */
  1522. AMD_DEBUG1 (("Path compression, i unordered: "ID"\n", i)) ;
  1523. j = Pe [i] ;
  1524. ASSERT (j >= EMPTY && j < n) ;
  1525. AMD_DEBUG3 ((" j: "ID"\n", j)) ;
  1526. if (j == EMPTY)
  1527. {
  1528. /* Skip a dense variable. It has no parent. */
  1529. AMD_DEBUG3 ((" i is a dense variable\n")) ;
  1530. continue ;
  1531. }
  1532. /* while (j is a variable) */
  1533. while (Nv [j] == 0)
  1534. {
  1535. AMD_DEBUG3 ((" j : "ID"\n", j)) ;
  1536. j = Pe [j] ;
  1537. AMD_DEBUG3 ((" j:: "ID"\n", j)) ;
  1538. ASSERT (j >= 0 && j < n) ;
  1539. }
  1540. /* got to an element e */
  1541. e = j ;
  1542. AMD_DEBUG3 (("got to e: "ID"\n", e)) ;
  1543. /* -------------------------------------------------------------
  1544. * traverse the path again from i to e, and compress the path
  1545. * (all nodes point to e). Path compression allows this code to
  1546. * compute in O(n) time.
  1547. * ------------------------------------------------------------- */
  1548. j = i ;
  1549. /* while (j is a variable) */
  1550. while (Nv [j] == 0)
  1551. {
  1552. jnext = Pe [j] ;
  1553. AMD_DEBUG3 (("j "ID" jnext "ID"\n", j, jnext)) ;
  1554. Pe [j] = e ;
  1555. j = jnext ;
  1556. ASSERT (j >= 0 && j < n) ;
  1557. }
  1558. }
  1559. }
  1560. /* ========================================================================= */
  1561. /* postorder the assembly tree */
  1562. /* ========================================================================= */
  1563. AMD_postorder (n, Pe, Nv, Elen,
  1564. W, /* output order */
  1565. Head, Next, Last) ; /* workspace */
  1566. /* ========================================================================= */
  1567. /* compute output permutation and inverse permutation */
  1568. /* ========================================================================= */
  1569. /* W [e] = k means that element e is the kth element in the new
  1570. * order. e is in the range 0 to n-1, and k is in the range 0 to
  1571. * the number of elements. Use Head for inverse order. */
  1572. for (k = 0 ; k < n ; k++)
  1573. {
  1574. Head [k] = EMPTY ;
  1575. Next [k] = EMPTY ;
  1576. }
  1577. for (e = 0 ; e < n ; e++)
  1578. {
  1579. k = W [e] ;
  1580. ASSERT ((k == EMPTY) == (Nv [e] == 0)) ;
  1581. if (k != EMPTY)
  1582. {
  1583. ASSERT (k >= 0 && k < n) ;
  1584. Head [k] = e ;
  1585. }
  1586. }
  1587. /* construct output inverse permutation in Next,
  1588. * and permutation in Last */
  1589. nel = 0 ;
  1590. for (k = 0 ; k < n ; k++)
  1591. {
  1592. e = Head [k] ;
  1593. if (e == EMPTY) break ;
  1594. ASSERT (e >= 0 && e < n && Nv [e] > 0) ;
  1595. Next [e] = nel ;
  1596. nel += Nv [e] ;
  1597. }
  1598. ASSERT (nel == n - ndense) ;
  1599. /* order non-principal variables (dense, & those merged into supervar's) */
  1600. for (i = 0 ; i < n ; i++)
  1601. {
  1602. if (Nv [i] == 0)
  1603. {
  1604. e = Pe [i] ;
  1605. ASSERT (e >= EMPTY && e < n) ;
  1606. if (e != EMPTY)
  1607. {
  1608. /* This is an unordered variable that was merged
  1609. * into element e via supernode detection or mass
  1610. * elimination of i when e became the pivot element.
  1611. * Place i in order just before e. */
  1612. ASSERT (Next [i] == EMPTY && Nv [e] > 0) ;
  1613. Next [i] = Next [e] ;
  1614. Next [e]++ ;
  1615. }
  1616. else
  1617. {
  1618. /* This is a dense unordered variable, with no parent.
  1619. * Place it last in the output order. */
  1620. Next [i] = nel++ ;
  1621. }
  1622. }
  1623. }
  1624. ASSERT (nel == n) ;
  1625. AMD_DEBUG2 (("\n\nPerm:\n")) ;
  1626. for (i = 0 ; i < n ; i++)
  1627. {
  1628. k = Next [i] ;
  1629. ASSERT (k >= 0 && k < n) ;
  1630. Last [k] = i ;
  1631. AMD_DEBUG2 ((" perm ["ID"] = "ID"\n", k, i)) ;
  1632. }
  1633. }