Amesos Package Browser (Single Doxygen Collection) Development
amesos_cholmod_colamd.c
Go to the documentation of this file.
00001 /* ========================================================================== */
00002 /* === Cholesky/cholmod_colamd ============================================== */
00003 /* ========================================================================== */
00004 
00005 /* -----------------------------------------------------------------------------
00006  * CHOLMOD/Cholesky Module.  Copyright (C) 2005-2006, Timothy A. Davis
00007  * The CHOLMOD/Cholesky Module is licensed under Version 2.1 of the GNU
00008  * Lesser General Public License.  See lesser.txt for a text of the license.
00009  * CHOLMOD is also available under other licenses; contact authors for details.
00010  * http://www.cise.ufl.edu/research/sparse
00011  * -------------------------------------------------------------------------- */
00012 
00013 /* CHOLMOD interface to the COLAMD ordering routine (version 2.4 or later).
00014  * Finds a permutation p such that the Cholesky factorization of PAA'P' is
00015  * sparser than AA' using colamd.  If the postorder input parameter is TRUE,
00016  * the column etree is found and postordered, and the colamd ordering is then
00017  * combined with its postordering.  A must be unsymmetric.
00018  *
00019  * There can be no duplicate entries in f.
00020  * f can be length 0 to n if A is m-by-n.
00021  *
00022  * workspace: Iwork (4*nrow+ncol), Head (nrow+1), Flag (nrow)
00023  *  Allocates a copy of its input matrix, which
00024  *  is then used as CCOLAMD's workspace.
00025  *
00026  * Supports any xtype (pattern, real, complex, or zomplex)
00027  */
00028 
00029 #ifndef NCHOLESKY
00030 
00031 #include "amesos_cholmod_internal.h"
00032 #include "amesos_colamd.h"
00033 #include "amesos_cholmod_cholesky.h"
00034 
00035 #if (!defined (COLAMD_VERSION) || (COLAMD_VERSION < COLAMD_VERSION_CODE (2,5)))
00036 #error "COLAMD v2.5 or later is required"
00037 #endif
00038 
00039 /* ========================================================================== */
00040 /* === cholmod_colamd ======================================================= */
00041 /* ========================================================================== */
00042 
00043 int CHOLMOD(colamd)
00044 (
00045     /* ---- input ---- */
00046     cholmod_sparse *A,  /* matrix to order */
00047     Int *fset,    /* subset of 0:(A->ncol)-1 */
00048     size_t fsize, /* size of fset */
00049     int postorder,  /* if TRUE, follow with a coletree postorder */
00050     /* ---- output --- */
00051     Int *Perm,    /* size A->nrow, output permutation */
00052     /* --------------- */
00053     cholmod_common *Common
00054 )
00055 {
00056     double knobs [COLAMD_KNOBS] ;
00057     cholmod_sparse *C ;
00058     Int *NewPerm, *Parent, *Post, *Work2n ;
00059     Int k, nrow, ncol ;
00060     size_t s, alen ;
00061     int ok = TRUE ;
00062 
00063     /* ---------------------------------------------------------------------- */
00064     /* check inputs */
00065     /* ---------------------------------------------------------------------- */
00066 
00067     RETURN_IF_NULL_COMMON (FALSE) ;
00068     RETURN_IF_NULL (A, FALSE) ;
00069     RETURN_IF_NULL (Perm, FALSE) ;
00070     RETURN_IF_XTYPE_INVALID (A, CHOLMOD_PATTERN, CHOLMOD_ZOMPLEX, FALSE) ;
00071     if (A->stype != 0)
00072     {
00073   ERROR (CHOLMOD_INVALID, "matrix must be unsymmetric") ;
00074   return (FALSE) ;
00075     }
00076     Common->status = CHOLMOD_OK ;
00077 
00078     /* ---------------------------------------------------------------------- */
00079     /* get inputs */
00080     /* ---------------------------------------------------------------------- */
00081 
00082     nrow = A->nrow ;
00083     ncol = A->ncol ;
00084 
00085     /* ---------------------------------------------------------------------- */
00086     /* allocate workspace */
00087     /* ---------------------------------------------------------------------- */
00088 
00089     /* Note: this is less than the space used in cholmod_analyze, so if
00090      * cholmod_colamd is being called by that routine, no space will be
00091      * allocated.
00092      */
00093 
00094     /* s = 4*nrow + ncol */
00095     s = CHOLMOD(mult_size_t) (nrow, 4, &ok) ;
00096     s = CHOLMOD(add_size_t) (s, ncol, &ok) ;
00097 
00098 #ifdef LONG
00099     alen = amesos_colamd_l_recommended (A->nzmax, ncol, nrow) ;
00100     amesos_colamd_l_set_defaults (knobs) ;
00101 #else
00102     alen = amesos_colamd_recommended (A->nzmax, ncol, nrow) ;
00103     amesos_colamd_set_defaults (knobs) ;
00104 #endif
00105 
00106     if (!ok || alen == 0)
00107     {
00108   ERROR (CHOLMOD_TOO_LARGE, "matrix invalid or too large") ;
00109   return (FALSE) ;
00110     }
00111 
00112     CHOLMOD(allocate_work) (0, s, 0, Common) ;
00113     if (Common->status < CHOLMOD_OK)
00114     {
00115   return (FALSE) ;
00116     }
00117 
00118     /* ---------------------------------------------------------------------- */
00119     /* allocate COLAMD workspace */
00120     /* ---------------------------------------------------------------------- */
00121 
00122     /* colamd_printf is only available in colamd v2.4 or later */
00123     amesos_colamd_printf = Common->print_function ;
00124 
00125     C = CHOLMOD(allocate_sparse) (ncol, nrow, alen, TRUE, TRUE, 0,
00126       CHOLMOD_PATTERN, Common) ;
00127 
00128     /* ---------------------------------------------------------------------- */
00129     /* copy (and transpose) the input matrix A into the colamd workspace */
00130     /* ---------------------------------------------------------------------- */
00131 
00132     /* C = A (:,f)', which also packs A if needed. */
00133     /* workspace: Iwork (nrow if no fset; MAX (nrow,ncol) if fset) */
00134     ok = CHOLMOD(transpose_unsym) (A, 0, NULL, fset, fsize, C, Common) ;
00135 
00136     /* ---------------------------------------------------------------------- */
00137     /* order the matrix (destroys the contents of C->i and C->p) */
00138     /* ---------------------------------------------------------------------- */
00139 
00140     /* get parameters */
00141     if (Common->current < 0 || Common->current >= CHOLMOD_MAXMETHODS)
00142     {
00143   /* this is the CHOLMOD default, not the COLAMD default */
00144   knobs [COLAMD_DENSE_ROW] = -1 ;
00145     }
00146     else
00147     {
00148   /* get the knobs from the Common parameters */
00149   knobs [COLAMD_DENSE_COL] = Common->method[Common->current].prune_dense ;
00150   knobs [COLAMD_DENSE_ROW] = Common->method[Common->current].prune_dense2;
00151   knobs [COLAMD_AGGRESSIVE] = Common->method[Common->current].aggressive ;
00152     }
00153 
00154     if (ok)
00155     {
00156   Int *Cp ;
00157   Int stats [COLAMD_STATS] ;
00158   Cp = C->p ;
00159 
00160 #ifdef LONG
00161   amesos_colamd_l (ncol, nrow, alen, C->i, Cp, knobs, stats) ;
00162 #else
00163   amesos_colamd (ncol, nrow, alen, C->i, Cp, knobs, stats) ;
00164 #endif
00165 
00166   ok = stats [COLAMD_STATUS] ;
00167   ok = (ok == COLAMD_OK || ok == COLAMD_OK_BUT_JUMBLED) ;
00168   /* permutation returned in C->p, if the ordering succeeded */
00169   for (k = 0 ; k < nrow ; k++)
00170   {
00171       Perm [k] = Cp [k] ;
00172   }
00173     }
00174 
00175     CHOLMOD(free_sparse) (&C, Common) ;
00176 
00177     /* ---------------------------------------------------------------------- */
00178     /* column etree postordering */
00179     /* ---------------------------------------------------------------------- */
00180 
00181     if (postorder)
00182     {
00183   /* use the last 2*n space in Iwork for Parent and Post */
00184   Work2n = Common->Iwork ;
00185   Work2n += 2*((size_t) nrow) + ncol ;
00186   Parent = Work2n ;   /* size nrow (i/i/l) */
00187   Post   = Work2n + nrow ;  /* size nrow (i/i/l) */
00188 
00189   /* workspace: Iwork (2*nrow+ncol), Flag (nrow), Head (nrow+1) */
00190   ok = ok && CHOLMOD(analyze_ordering) (A, CHOLMOD_COLAMD, Perm, fset,
00191     fsize, Parent, Post, NULL, NULL, NULL, Common) ;
00192 
00193   /* combine the colamd permutation with its postordering */
00194   if (ok)
00195   {
00196       NewPerm = Common->Iwork ;   /* size nrow (i/i/l) */
00197       for (k = 0 ; k < nrow ; k++)
00198       {
00199     NewPerm [k] = Perm [Post [k]] ;
00200       }
00201       for (k = 0 ; k < nrow ; k++)
00202       {
00203     Perm [k] = NewPerm [k] ;
00204       }
00205   }
00206     }
00207 
00208     return (ok) ;
00209 }
00210 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines