Amesos Package Browser (Single Doxygen Collection) Development
amesos_amd_1.c
Go to the documentation of this file.
```00001 /* ========================================================================= */
00002 /* === AMD_1 =============================================================== */
00003 /* ========================================================================= */
00004
00005 /* ------------------------------------------------------------------------- */
00006 /* AMD, Copyright (c) Timothy A. Davis,              */
00007 /* Patrick R. Amestoy, and Iain S. Duff.  See ../README.txt for License.     */
00008 /* email: davis at cise.ufl.edu    CISE Department, Univ. of Florida.        */
00009 /* web: http://www.cise.ufl.edu/research/sparse/amd                          */
00010 /* ------------------------------------------------------------------------- */
00011
00012 /* AMD_1: Construct A+A' for a sparse matrix A and perform the AMD ordering.
00013  *
00014  * The n-by-n sparse matrix A can be unsymmetric.  It is stored in MATLAB-style
00015  * compressed-column form, with sorted row indices in each column, and no
00016  * duplicate entries.  Diagonal entries may be present, but they are ignored.
00017  * Row indices of column j of A are stored in Ai [Ap [j] ... Ap [j+1]-1].
00018  * Ap [0] must be zero, and nz = Ap [n] is the number of entries in A.  The
00019  * size of the matrix, n, must be greater than or equal to zero.
00020  *
00021  * This routine must be preceded by a call to AMD_aat, which computes the
00022  * number of entries in each row/column in A+A', excluding the diagonal.
00023  * Len [j], on input, is the number of entries in row/column j of A+A'.  This
00024  * routine constructs the matrix A+A' and then calls AMD_2.  No error checking
00025  * is performed (this was done in AMD_valid).
00026  */
00027
00028 #include "amesos_amd_internal.h"
00029
00030 GLOBAL void AMD_1
00031 (
00032     Int n,    /* n > 0 */
00033     const Int Ap [ ], /* input of size n+1, not modified */
00034     const Int Ai [ ], /* input of size nz = Ap [n], not modified */
00035     Int P [ ],    /* size n output permutation */
00036     Int Pinv [ ], /* size n output inverse permutation */
00037     Int Len [ ],  /* size n input, undefined on output */
00038     Int slen,   /* slen >= sum (Len [0..n-1]) + 7n,
00039        * ideally slen = 1.2 * sum (Len) + 8n */
00040     Int S [ ],    /* size slen workspace */
00041     double Control [ ], /* input array of size AMD_CONTROL */
00042     double Info [ ] /* output array of size AMD_INFO */
00043 )
00044 {
00045     Int i, j, k, p, pfree, iwlen, pj, p1, p2, pj2, *Iw, *Pe, *Nv, *Head,
00046   *Elen, *Degree, *s, *W, *Sp, *Tp ;
00047
00048     /* --------------------------------------------------------------------- */
00049     /* construct the matrix for AMD_2 */
00050     /* --------------------------------------------------------------------- */
00051
00052     ASSERT (n > 0) ;
00053
00054     iwlen = slen - 6*n ;
00055     s = S ;
00056     Pe = s ;      s += n ;
00057     Nv = s ;      s += n ;
00058     Head = s ;      s += n ;
00059     Elen = s ;      s += n ;
00060     Degree = s ;    s += n ;
00061     W = s ;     s += n ;
00062     Iw = s ;      s += iwlen ;
00063
00064     ASSERT (AMD_valid (n, n, Ap, Ai) == AMD_OK) ;
00065
00066     /* construct the pointers for A+A' */
00067     Sp = Nv ;     /* use Nv and W as workspace for Sp and Tp [ */
00068     Tp = W ;
00069     pfree = 0 ;
00070     for (j = 0 ; j < n ; j++)
00071     {
00072   Pe [j] = pfree ;
00073   Sp [j] = pfree ;
00074   pfree += Len [j] ;
00075     }
00076
00077     /* Note that this restriction on iwlen is slightly more restrictive than
00078      * what is strictly required in AMD_2.  AMD_2 can operate with no elbow
00079      * room at all, but it will be very slow.  For better performance, at
00080      * least size-n elbow room is enforced. */
00081     ASSERT (iwlen >= pfree + n) ;
00082
00083 #ifndef NDEBUG
00084     for (p = 0 ; p < iwlen ; p++) Iw [p] = EMPTY ;
00085 #endif
00086
00087     for (k = 0 ; k < n ; k++)
00088     {
00089   AMD_DEBUG1 (("Construct row/column k= "ID" of A+A'\n", k))  ;
00090   p1 = Ap [k] ;
00091   p2 = Ap [k+1] ;
00092
00093   /* construct A+A' */
00094   for (p = p1 ; p < p2 ; )
00095   {
00096       /* scan the upper triangular part of A */
00097       j = Ai [p] ;
00098       ASSERT (j >= 0 && j < n) ;
00099       if (j < k)
00100       {
00101     /* entry A (j,k) in the strictly upper triangular part */
00102     ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
00103     ASSERT (Sp [k] < (k == n-1 ? pfree : Pe [k+1])) ;
00104     Iw [Sp [j]++] = k ;
00105     Iw [Sp [k]++] = j ;
00106     p++ ;
00107       }
00108       else if (j == k)
00109       {
00110     /* skip the diagonal */
00111     p++ ;
00112     break ;
00113       }
00114       else /* j > k */
00115       {
00116     /* first entry below the diagonal */
00117     break ;
00118       }
00119       /* scan lower triangular part of A, in column j until reaching
00120        * row k.  Start where last scan left off. */
00121       ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ;
00122       pj2 = Ap [j+1] ;
00123       for (pj = Tp [j] ; pj < pj2 ; )
00124       {
00125     i = Ai [pj] ;
00126     ASSERT (i >= 0 && i < n) ;
00127     if (i < k)
00128     {
00129         /* A (i,j) is only in the lower part, not in upper */
00130         ASSERT (Sp [i] < (i == n-1 ? pfree : Pe [i+1])) ;
00131         ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
00132         Iw [Sp [i]++] = j ;
00133         Iw [Sp [j]++] = i ;
00134         pj++ ;
00135     }
00136     else if (i == k)
00137     {
00138         /* entry A (k,j) in lower part and A (j,k) in upper */
00139         pj++ ;
00140         break ;
00141     }
00142     else /* i > k */
00143     {
00144         /* consider this entry later, when k advances to i */
00145         break ;
00146     }
00147       }
00148       Tp [j] = pj ;
00149   }
00150   Tp [k] = p ;
00151     }
00152
00153     /* clean up, for remaining mismatched entries */
00154     for (j = 0 ; j < n ; j++)
00155     {
00156   for (pj = Tp [j] ; pj < Ap [j+1] ; pj++)
00157   {
00158       i = Ai [pj] ;
00159       ASSERT (i >= 0 && i < n) ;
00160       /* A (i,j) is only in the lower part, not in upper */
00161       ASSERT (Sp [i] < (i == n-1 ? pfree : Pe [i+1])) ;
00162       ASSERT (Sp [j] < (j == n-1 ? pfree : Pe [j+1])) ;
00163       Iw [Sp [i]++] = j ;
00164       Iw [Sp [j]++] = i ;
00165   }
00166     }
00167
00168 #ifndef NDEBUG
00169     for (j = 0 ; j < n-1 ; j++) ASSERT (Sp [j] == Pe [j+1]) ;
00170     ASSERT (Sp [n-1] == pfree) ;
00171 #endif
00172
00173     /* Tp and Sp no longer needed ] */
00174
00175     /* --------------------------------------------------------------------- */
00176     /* order the matrix */
00177     /* --------------------------------------------------------------------- */
00178
00179     AMD_2 (n, Pe, Iw, Len, iwlen, pfree,
00180   Nv, Pinv, P, Head, Elen, Degree, W, Control, Info) ;
00181 }
```