amd_preprocess.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* ========================================================================= */
  2. /* === AMD_preprocess ====================================================== */
  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. /* Sorts, removes duplicate entries, and transposes from the nonzero pattern of
  11. * a column-form matrix A, to obtain the matrix R. The input matrix can have
  12. * duplicate entries and/or unsorted columns (AMD_valid (n,Ap,Ai) must not be
  13. * AMD_INVALID).
  14. *
  15. * This input condition is NOT checked. This routine is not user-callable.
  16. */
  17. #include "amd_internal.h"
  18. /* ========================================================================= */
  19. /* === AMD_preprocess ====================================================== */
  20. /* ========================================================================= */
  21. /* AMD_preprocess does not check its input for errors or allocate workspace.
  22. * On input, the condition (AMD_valid (n,n,Ap,Ai) != AMD_INVALID) must hold.
  23. */
  24. GLOBAL void AMD_preprocess
  25. (
  26. Int n, /* input matrix: A is n-by-n */
  27. const Int Ap [ ], /* size n+1 */
  28. const Int Ai [ ], /* size nz = Ap [n] */
  29. /* output matrix R: */
  30. Int Rp [ ], /* size n+1 */
  31. Int Ri [ ], /* size nz (or less, if duplicates present) */
  32. Int W [ ], /* workspace of size n */
  33. Int Flag [ ] /* workspace of size n */
  34. )
  35. {
  36. /* --------------------------------------------------------------------- */
  37. /* local variables */
  38. /* --------------------------------------------------------------------- */
  39. Int i, j, p, p2 ;
  40. ASSERT (AMD_valid (n, n, Ap, Ai) != AMD_INVALID) ;
  41. /* --------------------------------------------------------------------- */
  42. /* count the entries in each row of A (excluding duplicates) */
  43. /* --------------------------------------------------------------------- */
  44. for (i = 0 ; i < n ; i++)
  45. {
  46. W [i] = 0 ; /* # of nonzeros in row i (excl duplicates) */
  47. Flag [i] = EMPTY ; /* Flag [i] = j if i appears in column j */
  48. }
  49. for (j = 0 ; j < n ; j++)
  50. {
  51. p2 = Ap [j+1] ;
  52. for (p = Ap [j] ; p < p2 ; p++)
  53. {
  54. i = Ai [p] ;
  55. if (Flag [i] != j)
  56. {
  57. /* row index i has not yet appeared in column j */
  58. W [i]++ ; /* one more entry in row i */
  59. Flag [i] = j ; /* flag row index i as appearing in col j*/
  60. }
  61. }
  62. }
  63. /* --------------------------------------------------------------------- */
  64. /* compute the row pointers for R */
  65. /* --------------------------------------------------------------------- */
  66. Rp [0] = 0 ;
  67. for (i = 0 ; i < n ; i++)
  68. {
  69. Rp [i+1] = Rp [i] + W [i] ;
  70. }
  71. for (i = 0 ; i < n ; i++)
  72. {
  73. W [i] = Rp [i] ;
  74. Flag [i] = EMPTY ;
  75. }
  76. /* --------------------------------------------------------------------- */
  77. /* construct the row form matrix R */
  78. /* --------------------------------------------------------------------- */
  79. /* R = row form of pattern of A */
  80. for (j = 0 ; j < n ; j++)
  81. {
  82. p2 = Ap [j+1] ;
  83. for (p = Ap [j] ; p < p2 ; p++)
  84. {
  85. i = Ai [p] ;
  86. if (Flag [i] != j)
  87. {
  88. /* row index i has not yet appeared in column j */
  89. Ri [W [i]++] = j ; /* put col j in row i */
  90. Flag [i] = j ; /* flag row index i as appearing in col j*/
  91. }
  92. }
  93. }
  94. #ifndef NDEBUG
  95. ASSERT (AMD_valid (n, n, Rp, Ri) == AMD_OK) ;
  96. for (j = 0 ; j < n ; j++)
  97. {
  98. ASSERT (W [j] == Rp [j+1]) ;
  99. }
  100. #endif
  101. }