GLOBAL Int CAMD_order ( Int n, const Int Ap [ ], const Int Ai [ ], Int P [ ], double Control [ ], double Info [ ], const Int C [ ] ) { Int *Len, *S, nz, i, *Pinv, info, status, *Rp, *Ri, *Cp, *Ci, ok ; size_t nzaat, slen ; double mem = 0 ; #ifndef NDEBUG CAMD_debug_init ("camd") ; #endif /* clear the Info array, if it exists */ info = Info != (double *) NULL ; if (info) { for (i = 0 ; i < CAMD_INFO ; i++) { Info [i] = EMPTY ; } Info [CAMD_N] = n ; Info [CAMD_STATUS] = CAMD_OK ; } /* make sure inputs exist and n is >= 0 */ if (Ai == (Int *) NULL || Ap == (Int *) NULL || P == (Int *) NULL || n < 0) { if (info) Info [CAMD_STATUS] = CAMD_INVALID ; return (CAMD_INVALID) ; /* arguments are invalid */ } if (n == 0) { return (CAMD_OK) ; /* n is 0 so there's nothing to do */ } nz = Ap [n] ; if (info) { Info [CAMD_NZ] = nz ; } if (nz < 0) { if (info) Info [CAMD_STATUS] = CAMD_INVALID ; return (CAMD_INVALID) ; } /* check if n or nz will cause size_t overflow */ if ((size_t) n >= SIZE_T_MAX / sizeof (Int) || (size_t) nz >= SIZE_T_MAX / sizeof (Int)) { if (info) Info [CAMD_STATUS] = CAMD_OUT_OF_MEMORY ; return (CAMD_OUT_OF_MEMORY) ; /* problem too large */ } /* check the input matrix: CAMD_OK, CAMD_INVALID, or CAMD_OK_BUT_JUMBLED */ status = CAMD_valid (n, n, Ap, Ai) ; if (status == CAMD_INVALID) { if (info) Info [CAMD_STATUS] = CAMD_INVALID ; return (CAMD_INVALID) ; /* matrix is invalid */ } /* allocate two size-n integer workspaces */ Len = camd_malloc (n * sizeof (Int)) ; Pinv = camd_malloc (n * sizeof (Int)) ; mem += n ; mem += n ; if (!Len || !Pinv) { /* :: out of memory :: */ camd_free (Len) ; camd_free (Pinv) ; if (info) Info [CAMD_STATUS] = CAMD_OUT_OF_MEMORY ; return (CAMD_OUT_OF_MEMORY) ; } if (status == CAMD_OK_BUT_JUMBLED) { /* sort the input matrix and remove duplicate entries */ CAMD_DEBUG1 (("Matrix is jumbled\n")) ; Rp = camd_malloc ((n+1) * sizeof (Int)) ; Ri = camd_malloc (MAX (nz,1) * sizeof (Int)) ; mem += (n+1) ; mem += MAX (nz,1) ; if (!Rp || !Ri) { /* :: out of memory :: */ camd_free (Rp) ; camd_free (Ri) ; camd_free (Len) ; camd_free (Pinv) ; if (info) Info [CAMD_STATUS] = CAMD_OUT_OF_MEMORY ; return (CAMD_OUT_OF_MEMORY) ; } /* use Len and Pinv as workspace to create R = A' */ CAMD_preprocess (n, Ap, Ai, Rp, Ri, Len, Pinv) ; Cp = Rp ; Ci = Ri ; } else { /* order the input matrix as-is. No need to compute R = A' first */ Rp = NULL ; Ri = NULL ; Cp = (Int *) Ap ; Ci = (Int *) Ai ; } /* --------------------------------------------------------------------- */ /* determine the symmetry and count off-diagonal nonzeros in A+A' */ /* --------------------------------------------------------------------- */ nzaat = CAMD_aat (n, Cp, Ci, Len, P, Info) ; CAMD_DEBUG1 (("nzaat: %g\n", (double) nzaat)) ; ASSERT ((MAX (nz-n, 0) <= nzaat) && (nzaat <= 2 * (size_t) nz)) ; /* --------------------------------------------------------------------- */ /* allocate workspace for matrix, elbow room, and 7 size-n vectors */ /* --------------------------------------------------------------------- */ S = NULL ; slen = nzaat ; /* space for matrix */ ok = ((slen + nzaat/5) >= slen) ; /* check for size_t overflow */ slen += nzaat/5 ; /* add elbow room */ for (i = 0 ; ok && i < 8 ; i++) { ok = ((slen + n+1) > slen) ; /* check for size_t overflow */ slen += (n+1) ; /* size-n elbow room, 7 size-(n+1) workspace */ } mem += slen ; ok = ok && (slen < SIZE_T_MAX / sizeof (Int)) ; /* check for overflow */ ok = ok && (slen < Int_MAX) ; /* S[i] for Int i must be OK */ if (ok) { S = camd_malloc (slen * sizeof (Int)) ; } CAMD_DEBUG1 (("slen %g\n", (double) slen)) ; if (!S) { /* :: out of memory :: (or problem too large) */ camd_free (Rp) ; camd_free (Ri) ; camd_free (Len) ; camd_free (Pinv) ; if (info) Info [CAMD_STATUS] = CAMD_OUT_OF_MEMORY ; return (CAMD_OUT_OF_MEMORY) ; } if (info) { /* memory usage, in bytes. */ Info [CAMD_MEMORY] = mem * sizeof (Int) ; } /* --------------------------------------------------------------------- */ /* order the matrix */ /* --------------------------------------------------------------------- */ CAMD_1 (n, Cp, Ci, P, Pinv, Len, slen, S, Control, Info, C) ; /* --------------------------------------------------------------------- */ /* free the workspace */ /* --------------------------------------------------------------------- */ camd_free (Rp) ; camd_free (Ri) ; camd_free (Len) ; camd_free (Pinv) ; camd_free (S) ; if (info) Info [CAMD_STATUS] = status ; return (status) ; /* successful ordering */ }
GLOBAL size_t CAMD(aat) /* returns nz in A+A' */ ( Int n, const Int Ap [ ], const Int Ai [ ], Int Len [ ], /* Len [j]: length of column j of A+A', excl diagonal*/ Int Tp [ ], /* workspace of size n */ double Info [ ] ) { Int p1, p2, p, i, j, pj, pj2, k, nzdiag, nzboth, nz ; double sym ; size_t nzaat ; #ifndef NDEBUG CAMD_debug_init ("CAMD AAT") ; for (k = 0 ; k < n ; k++) Tp [k] = EMPTY ; ASSERT (CAMD(valid) (n, n, Ap, Ai) == CAMD_OK) ; #endif if (Info != (double *) NULL) { /* clear the Info array, if it exists */ for (i = 0 ; i < CAMD_INFO ; i++) { Info [i] = EMPTY ; } Info [CAMD_STATUS] = CAMD_OK ; } for (k = 0 ; k < n ; k++) { Len [k] = 0 ; } nzdiag = 0 ; nzboth = 0 ; nz = Ap [n] ; for (k = 0 ; k < n ; k++) { p1 = Ap [k] ; p2 = Ap [k+1] ; CAMD_DEBUG2 (("\nAAT Column: "ID" p1: "ID" p2: "ID"\n", k, p1, p2)) ; /* construct A+A' */ for (p = p1 ; p < p2 ; ) { /* scan the upper triangular part of A */ j = Ai [p] ; if (j < k) { /* entry A (j,k) is in the strictly upper triangular part, * add both A (j,k) and A (k,j) to the matrix A+A' */ Len [j]++ ; Len [k]++ ; CAMD_DEBUG3 ((" upper ("ID","ID") ("ID","ID")\n", j,k, k,j)); p++ ; } else if (j == k) { /* skip the diagonal */ p++ ; nzdiag++ ; break ; } else /* j > k */ { /* first entry below the diagonal */ break ; } /* scan lower triangular part of A, in column j until reaching * row k. Start where last scan left off. */ ASSERT (Tp [j] != EMPTY) ; ASSERT (Ap [j] <= Tp [j] && Tp [j] <= Ap [j+1]) ; pj2 = Ap [j+1] ; for (pj = Tp [j] ; pj < pj2 ; ) { i = Ai [pj] ; if (i < k) { /* A (i,j) is only in the lower part, not in upper. * add both A (i,j) and A (j,i) to the matrix A+A' */ Len [i]++ ; Len [j]++ ; CAMD_DEBUG3 ((" lower ("ID","ID") ("ID","ID")\n", i,j, j,i)) ; pj++ ; } else if (i == k) { /* entry A (k,j) in lower part and A (j,k) in upper */ pj++ ; nzboth++ ; break ; } else /* i > k */ { /* consider this entry later, when k advances to i */ break ; } } Tp [j] = pj ; } /* Tp [k] points to the entry just below the diagonal in column k */ Tp [k] = p ; } /* clean up, for remaining mismatched entries */ for (j = 0 ; j < n ; j++) { for (pj = Tp [j] ; pj < Ap [j+1] ; pj++) { i = Ai [pj] ; /* A (i,j) is only in the lower part, not in upper. * add both A (i,j) and A (j,i) to the matrix A+A' */ Len [i]++ ; Len [j]++ ; CAMD_DEBUG3 ((" lower cleanup ("ID","ID") ("ID","ID")\n", i,j, j,i)) ; } } /* --------------------------------------------------------------------- */ /* compute the symmetry of the nonzero pattern of A */ /* --------------------------------------------------------------------- */ /* Given a matrix A, the symmetry of A is: * B = tril (spones (A), -1) + triu (spones (A), 1) ; * sym = nnz (B & B') / nnz (B) ; * or 1 if nnz (B) is zero. */ if (nz == nzdiag) { sym = 1 ; } else { sym = (2 * (double) nzboth) / ((double) (nz - nzdiag)) ; } nzaat = 0 ; for (k = 0 ; k < n ; k++) { nzaat += Len [k] ; } CAMD_DEBUG1 (("CAMD nz in A+A', excluding diagonal (nzaat) = %g\n", (double) nzaat)) ; CAMD_DEBUG1 ((" nzboth: "ID" nz: "ID" nzdiag: "ID" symmetry: %g\n", nzboth, nz, nzdiag, sym)) ; if (Info != (double *) NULL) { Info [CAMD_STATUS] = CAMD_OK ; Info [CAMD_N] = n ; Info [CAMD_NZ] = nz ; Info [CAMD_SYMMETRY] = sym ; /* symmetry of pattern of A */ Info [CAMD_NZDIAG] = nzdiag ; /* nonzeros on diagonal of A */ Info [CAMD_NZ_A_PLUS_AT] = nzaat ; /* nonzeros in A+A' */ } return (nzaat) ; }