amd.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* ========================================================================= */
  2. /* === AMD: approximate minimum degree ordering =========================== */
  3. /* ========================================================================= */
  4. /* ------------------------------------------------------------------------- */
  5. /* AMD Version 2.2, Copyright (c) 2007 by Timothy A. Davis, */
  6. /* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
  7. /* email: DrTimothyAldenDavis@gmail.com */
  8. /* ------------------------------------------------------------------------- */
  9. /* AMD finds a symmetric ordering P of a matrix A so that the Cholesky
  10. * factorization of P*A*P' has fewer nonzeros and takes less work than the
  11. * Cholesky factorization of A. If A is not symmetric, then it performs its
  12. * ordering on the matrix A+A'. Two sets of user-callable routines are
  13. * provided, one for int integers and the other for SuiteSparse_long integers.
  14. *
  15. * The method is based on the approximate minimum degree algorithm, discussed
  16. * in Amestoy, Davis, and Duff, "An approximate degree ordering algorithm",
  17. * SIAM Journal of Matrix Analysis and Applications, vol. 17, no. 4, pp.
  18. * 886-905, 1996. This package can perform both the AMD ordering (with
  19. * aggressive absorption), and the AMDBAR ordering (without aggressive
  20. * absorption) discussed in the above paper. This package differs from the
  21. * Fortran codes discussed in the paper:
  22. *
  23. * (1) it can ignore "dense" rows and columns, leading to faster run times
  24. * (2) it computes the ordering of A+A' if A is not symmetric
  25. * (3) it is followed by a depth-first post-ordering of the assembly tree
  26. * (or supernodal elimination tree)
  27. *
  28. * For historical reasons, the Fortran versions, amd.f and amdbar.f, have
  29. * been left (nearly) unchanged. They compute the identical ordering as
  30. * described in the above paper.
  31. */
  32. #ifndef AMD_H
  33. #define AMD_H
  34. /* make it easy for C++ programs to include AMD */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* get the definition of size_t: */
  39. #include <stddef.h>
  40. #include "SuiteSparse_config.h"
  41. int amd_order /* returns AMD_OK, AMD_OK_BUT_JUMBLED,
  42. * AMD_INVALID, or AMD_OUT_OF_MEMORY */
  43. (
  44. int n, /* A is n-by-n. n must be >= 0. */
  45. const int Ap [ ], /* column pointers for A, of size n+1 */
  46. const int Ai [ ], /* row indices of A, of size nz = Ap [n] */
  47. int P [ ], /* output permutation, of size n */
  48. double Control [ ], /* input Control settings, of size AMD_CONTROL */
  49. double Info [ ] /* output Info statistics, of size AMD_INFO */
  50. ) ;
  51. SuiteSparse_long amd_l_order /* see above for description of arguments */
  52. (
  53. SuiteSparse_long n,
  54. const SuiteSparse_long Ap [ ],
  55. const SuiteSparse_long Ai [ ],
  56. SuiteSparse_long P [ ],
  57. double Control [ ],
  58. double Info [ ]
  59. ) ;
  60. /* Input arguments (not modified):
  61. *
  62. * n: the matrix A is n-by-n.
  63. * Ap: an int/SuiteSparse_long array of size n+1, containing column
  64. * pointers of A.
  65. * Ai: an int/SuiteSparse_long array of size nz, containing the row
  66. * indices of A, where nz = Ap [n].
  67. * Control: a double array of size AMD_CONTROL, containing control
  68. * parameters. Defaults are used if Control is NULL.
  69. *
  70. * Output arguments (not defined on input):
  71. *
  72. * P: an int/SuiteSparse_long array of size n, containing the output
  73. * permutation. If row i is the kth pivot row, then P [k] = i. In
  74. * MATLAB notation, the reordered matrix is A (P,P).
  75. * Info: a double array of size AMD_INFO, containing statistical
  76. * information. Ignored if Info is NULL.
  77. *
  78. * On input, the matrix A is stored in column-oriented form. The row indices
  79. * of nonzero entries in column j are stored in Ai [Ap [j] ... Ap [j+1]-1].
  80. *
  81. * If the row indices appear in ascending order in each column, and there
  82. * are no duplicate entries, then amd_order is slightly more efficient in
  83. * terms of time and memory usage. If this condition does not hold, a copy
  84. * of the matrix is created (where these conditions do hold), and the copy is
  85. * ordered. This feature is new to v2.0 (v1.2 and earlier required this
  86. * condition to hold for the input matrix).
  87. *
  88. * Row indices must be in the range 0 to
  89. * n-1. Ap [0] must be zero, and thus nz = Ap [n] is the number of nonzeros
  90. * in A. The array Ap is of size n+1, and the array Ai is of size nz = Ap [n].
  91. * The matrix does not need to be symmetric, and the diagonal does not need to
  92. * be present (if diagonal entries are present, they are ignored except for
  93. * the output statistic Info [AMD_NZDIAG]). The arrays Ai and Ap are not
  94. * modified. This form of the Ap and Ai arrays to represent the nonzero
  95. * pattern of the matrix A is the same as that used internally by MATLAB.
  96. * If you wish to use a more flexible input structure, please see the
  97. * umfpack_*_triplet_to_col routines in the UMFPACK package, at
  98. * http://www.suitesparse.com.
  99. *
  100. * Restrictions: n >= 0. Ap [0] = 0. Ap [j] <= Ap [j+1] for all j in the
  101. * range 0 to n-1. nz = Ap [n] >= 0. Ai [0..nz-1] must be in the range 0
  102. * to n-1. Finally, Ai, Ap, and P must not be NULL. If any of these
  103. * restrictions are not met, AMD returns AMD_INVALID.
  104. *
  105. * AMD returns:
  106. *
  107. * AMD_OK if the matrix is valid and sufficient memory can be allocated to
  108. * perform the ordering.
  109. *
  110. * AMD_OUT_OF_MEMORY if not enough memory can be allocated.
  111. *
  112. * AMD_INVALID if the input arguments n, Ap, Ai are invalid, or if P is
  113. * NULL.
  114. *
  115. * AMD_OK_BUT_JUMBLED if the matrix had unsorted columns, and/or duplicate
  116. * entries, but was otherwise valid.
  117. *
  118. * The AMD routine first forms the pattern of the matrix A+A', and then
  119. * computes a fill-reducing ordering, P. If P [k] = i, then row/column i of
  120. * the original is the kth pivotal row. In MATLAB notation, the permuted
  121. * matrix is A (P,P), except that 0-based indexing is used instead of the
  122. * 1-based indexing in MATLAB.
  123. *
  124. * The Control array is used to set various parameters for AMD. If a NULL
  125. * pointer is passed, default values are used. The Control array is not
  126. * modified.
  127. *
  128. * Control [AMD_DENSE]: controls the threshold for "dense" rows/columns.
  129. * A dense row/column in A+A' can cause AMD to spend a lot of time in
  130. * ordering the matrix. If Control [AMD_DENSE] >= 0, rows/columns
  131. * with more than Control [AMD_DENSE] * sqrt (n) entries are ignored
  132. * during the ordering, and placed last in the output order. The
  133. * default value of Control [AMD_DENSE] is 10. If negative, no
  134. * rows/columns are treated as "dense". Rows/columns with 16 or
  135. * fewer off-diagonal entries are never considered "dense".
  136. *
  137. * Control [AMD_AGGRESSIVE]: controls whether or not to use aggressive
  138. * absorption, in which a prior element is absorbed into the current
  139. * element if is a subset of the current element, even if it is not
  140. * adjacent to the current pivot element (refer to Amestoy, Davis,
  141. * & Duff, 1996, for more details). The default value is nonzero,
  142. * which means to perform aggressive absorption. This nearly always
  143. * leads to a better ordering (because the approximate degrees are
  144. * more accurate) and a lower execution time. There are cases where
  145. * it can lead to a slightly worse ordering, however. To turn it off,
  146. * set Control [AMD_AGGRESSIVE] to 0.
  147. *
  148. * Control [2..4] are not used in the current version, but may be used in
  149. * future versions.
  150. *
  151. * The Info array provides statistics about the ordering on output. If it is
  152. * not present, the statistics are not returned. This is not an error
  153. * condition.
  154. *
  155. * Info [AMD_STATUS]: the return value of AMD, either AMD_OK,
  156. * AMD_OK_BUT_JUMBLED, AMD_OUT_OF_MEMORY, or AMD_INVALID.
  157. *
  158. * Info [AMD_N]: n, the size of the input matrix
  159. *
  160. * Info [AMD_NZ]: the number of nonzeros in A, nz = Ap [n]
  161. *
  162. * Info [AMD_SYMMETRY]: the symmetry of the matrix A. It is the number
  163. * of "matched" off-diagonal entries divided by the total number of
  164. * off-diagonal entries. An entry A(i,j) is matched if A(j,i) is also
  165. * an entry, for any pair (i,j) for which i != j. In MATLAB notation,
  166. * S = spones (A) ;
  167. * B = tril (S, -1) + triu (S, 1) ;
  168. * symmetry = nnz (B & B') / nnz (B) ;
  169. *
  170. * Info [AMD_NZDIAG]: the number of entries on the diagonal of A.
  171. *
  172. * Info [AMD_NZ_A_PLUS_AT]: the number of nonzeros in A+A', excluding the
  173. * diagonal. If A is perfectly symmetric (Info [AMD_SYMMETRY] = 1)
  174. * with a fully nonzero diagonal, then Info [AMD_NZ_A_PLUS_AT] = nz-n
  175. * (the smallest possible value). If A is perfectly unsymmetric
  176. * (Info [AMD_SYMMETRY] = 0, for an upper triangular matrix, for
  177. * example) with no diagonal, then Info [AMD_NZ_A_PLUS_AT] = 2*nz
  178. * (the largest possible value).
  179. *
  180. * Info [AMD_NDENSE]: the number of "dense" rows/columns of A+A' that were
  181. * removed from A prior to ordering. These are placed last in the
  182. * output order P.
  183. *
  184. * Info [AMD_MEMORY]: the amount of memory used by AMD, in bytes. In the
  185. * current version, this is 1.2 * Info [AMD_NZ_A_PLUS_AT] + 9*n
  186. * times the size of an integer. This is at most 2.4nz + 9n. This
  187. * excludes the size of the input arguments Ai, Ap, and P, which have
  188. * a total size of nz + 2*n + 1 integers.
  189. *
  190. * Info [AMD_NCMPA]: the number of garbage collections performed.
  191. *
  192. * Info [AMD_LNZ]: the number of nonzeros in L (excluding the diagonal).
  193. * This is a slight upper bound because mass elimination is combined
  194. * with the approximate degree update. It is a rough upper bound if
  195. * there are many "dense" rows/columns. The rest of the statistics,
  196. * below, are also slight or rough upper bounds, for the same reasons.
  197. * The post-ordering of the assembly tree might also not exactly
  198. * correspond to a true elimination tree postordering.
  199. *
  200. * Info [AMD_NDIV]: the number of divide operations for a subsequent LDL'
  201. * or LU factorization of the permuted matrix A (P,P).
  202. *
  203. * Info [AMD_NMULTSUBS_LDL]: the number of multiply-subtract pairs for a
  204. * subsequent LDL' factorization of A (P,P).
  205. *
  206. * Info [AMD_NMULTSUBS_LU]: the number of multiply-subtract pairs for a
  207. * subsequent LU factorization of A (P,P), assuming that no numerical
  208. * pivoting is required.
  209. *
  210. * Info [AMD_DMAX]: the maximum number of nonzeros in any column of L,
  211. * including the diagonal.
  212. *
  213. * Info [14..19] are not used in the current version, but may be used in
  214. * future versions.
  215. */
  216. /* ------------------------------------------------------------------------- */
  217. /* direct interface to AMD */
  218. /* ------------------------------------------------------------------------- */
  219. /* amd_2 is the primary AMD ordering routine. It is not meant to be
  220. * user-callable because of its restrictive inputs and because it destroys
  221. * the user's input matrix. It does not check its inputs for errors, either.
  222. * However, if you can work with these restrictions it can be faster than
  223. * amd_order and use less memory (assuming that you can create your own copy
  224. * of the matrix for AMD to destroy). Refer to AMD/Source/amd_2.c for a
  225. * description of each parameter. */
  226. void amd_2
  227. (
  228. int n,
  229. int Pe [ ],
  230. int Iw [ ],
  231. int Len [ ],
  232. int iwlen,
  233. int pfree,
  234. int Nv [ ],
  235. int Next [ ],
  236. int Last [ ],
  237. int Head [ ],
  238. int Elen [ ],
  239. int Degree [ ],
  240. int W [ ],
  241. double Control [ ],
  242. double Info [ ]
  243. ) ;
  244. void amd_l2
  245. (
  246. SuiteSparse_long n,
  247. SuiteSparse_long Pe [ ],
  248. SuiteSparse_long Iw [ ],
  249. SuiteSparse_long Len [ ],
  250. SuiteSparse_long iwlen,
  251. SuiteSparse_long pfree,
  252. SuiteSparse_long Nv [ ],
  253. SuiteSparse_long Next [ ],
  254. SuiteSparse_long Last [ ],
  255. SuiteSparse_long Head [ ],
  256. SuiteSparse_long Elen [ ],
  257. SuiteSparse_long Degree [ ],
  258. SuiteSparse_long W [ ],
  259. double Control [ ],
  260. double Info [ ]
  261. ) ;
  262. /* ------------------------------------------------------------------------- */
  263. /* amd_valid */
  264. /* ------------------------------------------------------------------------- */
  265. /* Returns AMD_OK or AMD_OK_BUT_JUMBLED if the matrix is valid as input to
  266. * amd_order; the latter is returned if the matrix has unsorted and/or
  267. * duplicate row indices in one or more columns. Returns AMD_INVALID if the
  268. * matrix cannot be passed to amd_order. For amd_order, the matrix must also
  269. * be square. The first two arguments are the number of rows and the number
  270. * of columns of the matrix. For its use in AMD, these must both equal n.
  271. *
  272. * NOTE: this routine returned TRUE/FALSE in v1.2 and earlier.
  273. */
  274. int amd_valid
  275. (
  276. int n_row, /* # of rows */
  277. int n_col, /* # of columns */
  278. const int Ap [ ], /* column pointers, of size n_col+1 */
  279. const int Ai [ ] /* row indices, of size Ap [n_col] */
  280. ) ;
  281. SuiteSparse_long amd_l_valid
  282. (
  283. SuiteSparse_long n_row,
  284. SuiteSparse_long n_col,
  285. const SuiteSparse_long Ap [ ],
  286. const SuiteSparse_long Ai [ ]
  287. ) ;
  288. /* ------------------------------------------------------------------------- */
  289. /* AMD memory manager and printf routines */
  290. /* ------------------------------------------------------------------------- */
  291. /* The user can redefine these to change the malloc, free, and printf routines
  292. * that AMD uses. */
  293. #ifndef EXTERN
  294. #define EXTERN extern
  295. #endif
  296. EXTERN void *(*amd_malloc) (size_t) ; /* pointer to malloc */
  297. EXTERN void (*amd_free) (void *) ; /* pointer to free */
  298. EXTERN void *(*amd_realloc) (void *, size_t) ; /* pointer to realloc */
  299. EXTERN void *(*amd_calloc) (size_t, size_t) ; /* pointer to calloc */
  300. EXTERN int (*amd_printf) (const char *, ...) ; /* pointer to printf */
  301. /* ------------------------------------------------------------------------- */
  302. /* AMD Control and Info arrays */
  303. /* ------------------------------------------------------------------------- */
  304. /* amd_defaults: sets the default control settings */
  305. void amd_defaults (double Control [ ]) ;
  306. void amd_l_defaults (double Control [ ]) ;
  307. /* amd_control: prints the control settings */
  308. void amd_control (double Control [ ]) ;
  309. void amd_l_control (double Control [ ]) ;
  310. /* amd_info: prints the statistics */
  311. void amd_info (double Info [ ]) ;
  312. void amd_l_info (double Info [ ]) ;
  313. #define AMD_CONTROL 5 /* size of Control array */
  314. #define AMD_INFO 20 /* size of Info array */
  315. /* contents of Control */
  316. #define AMD_DENSE 0 /* "dense" if degree > Control [0] * sqrt (n) */
  317. #define AMD_AGGRESSIVE 1 /* do aggressive absorption if Control [1] != 0 */
  318. /* default Control settings */
  319. #define AMD_DEFAULT_DENSE 10.0 /* default "dense" degree 10*sqrt(n) */
  320. #define AMD_DEFAULT_AGGRESSIVE 1 /* do aggressive absorption by default */
  321. /* contents of Info */
  322. #define AMD_STATUS 0 /* return value of amd_order and amd_l_order */
  323. #define AMD_N 1 /* A is n-by-n */
  324. #define AMD_NZ 2 /* number of nonzeros in A */
  325. #define AMD_SYMMETRY 3 /* symmetry of pattern (1 is sym., 0 is unsym.) */
  326. #define AMD_NZDIAG 4 /* # of entries on diagonal */
  327. #define AMD_NZ_A_PLUS_AT 5 /* nz in A+A' */
  328. #define AMD_NDENSE 6 /* number of "dense" rows/columns in A */
  329. #define AMD_MEMORY 7 /* amount of memory used by AMD */
  330. #define AMD_NCMPA 8 /* number of garbage collections in AMD */
  331. #define AMD_LNZ 9 /* approx. nz in L, excluding the diagonal */
  332. #define AMD_NDIV 10 /* number of fl. point divides for LU and LDL' */
  333. #define AMD_NMULTSUBS_LDL 11 /* number of fl. point (*,-) pairs for LDL' */
  334. #define AMD_NMULTSUBS_LU 12 /* number of fl. point (*,-) pairs for LU */
  335. #define AMD_DMAX 13 /* max nz. in any column of L, incl. diagonal */
  336. /* ------------------------------------------------------------------------- */
  337. /* return values of AMD */
  338. /* ------------------------------------------------------------------------- */
  339. #define AMD_OK 0 /* success */
  340. #define AMD_OUT_OF_MEMORY -1 /* malloc failed, or problem too large */
  341. #define AMD_INVALID -2 /* input arguments are not valid */
  342. #define AMD_OK_BUT_JUMBLED 1 /* input matrix is OK for amd_order, but
  343. * columns were not sorted, and/or duplicate entries were present. AMD had
  344. * to do extra work before ordering the matrix. This is a warning, not an
  345. * error. */
  346. /* ========================================================================== */
  347. /* === AMD version ========================================================== */
  348. /* ========================================================================== */
  349. /* AMD Version 1.2 and later include the following definitions.
  350. * As an example, to test if the version you are using is 1.2 or later:
  351. *
  352. * #ifdef AMD_VERSION
  353. * if (AMD_VERSION >= AMD_VERSION_CODE (1,2)) ...
  354. * #endif
  355. *
  356. * This also works during compile-time:
  357. *
  358. * #if defined(AMD_VERSION) && (AMD_VERSION >= AMD_VERSION_CODE (1,2))
  359. * printf ("This is version 1.2 or later\n") ;
  360. * #else
  361. * printf ("This is an early version\n") ;
  362. * #endif
  363. *
  364. * Versions 1.1 and earlier of AMD do not include a #define'd version number.
  365. */
  366. #define AMD_DATE "Jun 20, 2012"
  367. #define AMD_VERSION_CODE(main,sub) ((main) * 1000 + (sub))
  368. #define AMD_MAIN_VERSION 2
  369. #define AMD_SUB_VERSION 3
  370. #define AMD_SUBSUB_VERSION 1
  371. #define AMD_VERSION AMD_VERSION_CODE(AMD_MAIN_VERSION,AMD_SUB_VERSION)
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif