/* ========================================================================= */1/* === amd_internal.h ====================================================== */2/* ========================================================================= */34/* ------------------------------------------------------------------------- */5/* AMD Version 1.1 (Jan. 21, 2004), Copyright (c) 2004 by Timothy A. Davis, */6/* Patrick R. Amestoy, and Iain S. Duff. See ../README for License. */7/* email: [email protected] CISE Department, Univ. of Florida. */8/* web: http://www.cise.ufl.edu/research/sparse/amd */9/* ------------------------------------------------------------------------- */1011/* This file is for internal use in AMD itself, and does not normally need to12* be included in user code. Use amd.h instead.13*14* The following compile-time definitions affect how AMD is compiled.15*16* -DMATLAB_MEX_FILE17*18* This flag is turned on when compiling the amd mexFunction for19* use in MATLAB.20*21* -DMATHWORKS22*23* This flag is turned on when compiling amd as a built-in routine24* in MATLAB. Internal routines utMalloc, utFree, utRealloc, and25* utPrintf are used, and the MathWorks "util.h" file is included.26* This option is intended for use by The MathWorks, Inc., only.27*28* -DNDEBUG29*30* Debugging mode (if NDEBUG is not defined). The default, of course,31* is no debugging. Turning on debugging takes some work (see below).32* If you do not edit this file, then debugging is turned off anyway,33* regardless of whether or not -DNDEBUG is specified in your compiler34* options.35*36* -DALLOCATE=allocation_routine37* -DFREE=free_routine38*39* If you do not wish to use malloc or free, you can define the40* routines to be used here. You must specify both of them, or41* neither.42*43* -DPRINTF=printf_routine44*45* If you wish to use a routine other than printf, you can define it46* with -DPRINTF= followed by the name of the printf replacement.47*/4849/* ========================================================================= */50/* === NDEBUG ============================================================== */51/* ========================================================================= */5253/*54AMD will be exceedingly slow when running in debug mode. The next three55lines ensure that debugging is turned off.56*/57#ifndef NDEBUG58#define NDEBUG59#endif6061/*62To enable debugging, uncomment the following line:63#undef NDEBUG64*/6566/* ------------------------------------------------------------------------- */67/* ANSI include files */68/* ------------------------------------------------------------------------- */6970/* from stdlib.h: malloc, free, realloc (when not compiling for MATLAB) */71#include <stdlib.h>7273/* from stdio.h: printf. When in debug mode: fopen, fscanf */74#include <stdio.h>7576/* from limits.h: INT_MAX and LONG_MAX */77#include <limits.h>7879/* from math.h: sqrt */80#include <math.h>8182/* ------------------------------------------------------------------------- */83/* MATLAB include files (only if being used in or via MATLAB) */84/* ------------------------------------------------------------------------- */8586#ifdef MATHWORKS87#include "util.h"88#endif8990#ifdef MATLAB_MEX_FILE91#include "matrix.h"92#include "mex.h"93#endif9495/* ------------------------------------------------------------------------- */96/* basic definitions */97/* ------------------------------------------------------------------------- */9899#ifdef FLIP100#undef FLIP101#endif102103#ifdef MAX104#undef MAX105#endif106107#ifdef MIN108#undef MIN109#endif110111#ifdef EMPTY112#undef EMPTY113#endif114115#ifdef GLOBAL116#undef GLOBAL117#endif118119#ifdef PRIVATE120#undef PRIVATE121#endif122123/* FLIP is a "negation about -1", and is used to mark an integer i that is124* normally non-negative. FLIP (EMPTY) is EMPTY. FLIP of a number > EMPTY125* is negative, and FLIP of a number < EMTPY is positive. FLIP (FLIP (i)) = i126* for all integers i. UNFLIP (i) is >= EMPTY. */127#define EMPTY (-1)128#define FLIP(i) (-(i)-2)129#define UNFLIP(i) ((i < EMPTY) ? FLIP (i) : (i))130131/* for integer MAX/MIN, or for doubles when we don't care how NaN's behave: */132#define MAX(a,b) (((a) > (b)) ? (a) : (b))133#define MIN(a,b) (((a) < (b)) ? (a) : (b))134135/* logical expression of p implies q: */136#define IMPLIES(p,q) (!(p) || (q))137138/* Note that the IBM RS 6000 xlc predefines TRUE and FALSE in <types.h>. */139/* The Compaq Alpha also predefines TRUE and FALSE. */140#ifdef TRUE141#undef TRUE142#endif143#ifdef FALSE144#undef FALSE145#endif146147#define TRUE (1)148#define FALSE (0)149#define PRIVATE static150#define GLOBAL151#define EMPTY (-1)152153/* Note that Linux's gcc 2.96 defines NULL as ((void *) 0), but other */154/* compilers (even gcc 2.95.2 on Solaris) define NULL as 0 or (0). We */155/* need to use the ANSI standard value of 0. */156#ifdef NULL157#undef NULL158#endif159160#define NULL 0161162/* ------------------------------------------------------------------------- */163/* integer type for AMD: int or long */164/* ------------------------------------------------------------------------- */165166#if defined (DLONG) || defined (ZLONG)167168#define Int long169#define ID "%ld"170#define Int_MAX LONG_MAX171#define Int_MIN LONG_MIN172173#define AMD_order amd_l_order174#define AMD_defaults amd_l_defaults175#define AMD_control amd_l_control176#define AMD_info amd_l_info177#define AMD_1 amd_l1178#define AMD_2 amd_l2179#define AMD_valid amd_l_valid180#define AMD_aat amd_l_aat181#define AMD_postorder amd_l_postorder182#define AMD_post_tree amd_l_post_tree183#define AMD_dump amd_l_dump184#define AMD_debug amd_l_debug185#define AMD_debug_init amd_l_debug_init186#define AMD_wpreprocess amd_l_wpreprocess187#define AMD_preprocess amd_l_preprocess188#define AMD_preprocess_valid amd_l_preprocess_valid189190#else191192#define Int int193#define ID "%d"194#define Int_MAX INT_MAX195#define Int_MIN INT_MIN196197#define AMD_order amd_order198#define AMD_defaults amd_defaults199#define AMD_control amd_control200#define AMD_info amd_info201#define AMD_1 amd_1202#define AMD_2 amd_2203#define AMD_valid amd_valid204#define AMD_aat amd_aat205#define AMD_postorder amd_postorder206#define AMD_post_tree amd_post_tree207#define AMD_dump amd_dump208#define AMD_debug amd_debug209#define AMD_debug_init amd_debug_init210#define AMD_wpreprocess amd_wpreprocess211#define AMD_preprocess amd_preprocess212#define AMD_preprocess_valid amd_preprocess_valid213214#endif215216/* ========================================================================= */217/* === Memory allocator ==================================================== */218/* ========================================================================= */219220/* The MATLAB mexFunction uses MATLAB's memory manager, while the C-callable */221/* AMD routine uses the ANSI C malloc, free, and realloc routines. */222223#ifndef ALLOCATE224#ifdef MATLAB_MEX_FILE225#define ALLOCATE mxMalloc226#define FREE mxFree227#else228#ifdef MATHWORKS229/* Compiling as a built-in routine. Since out-of-memory conditions are checked230* after every allocation, we can use ut* routines here. */231#define ALLOCATE utMalloc232#define FREE utFree233#else234/* use the ANSI C memory allocation routines */235#define ALLOCATE malloc236#define FREE free237#endif238#endif239#endif240241242/* ========================================================================= */243/* === PRINTF macro ======================================================== */244/* ========================================================================= */245246/* All output goes through the PRINTF macro. */247248#ifndef PRINTF249#ifdef MATLAB_MEX_FILE250#define PRINTF(params) { (void) mexPrintf params ; }251#else252#ifdef MATHWORKS253#define PRINTF(params) { (void) utPrintf params ; }254#else255#define PRINTF(params) { (void) printf params ; }256#endif257#endif258#endif259260/* ------------------------------------------------------------------------- */261/* AMD routine definitions (user-callable) */262/* ------------------------------------------------------------------------- */263264#include "amd.h"265266/* ------------------------------------------------------------------------- */267/* AMD routine definitions (not user-callable) */268/* ------------------------------------------------------------------------- */269270GLOBAL Int AMD_valid271(272Int n_row,273Int n_col,274const Int Ap [ ],275const Int Ai [ ]276) ;277278GLOBAL Int AMD_aat279(280Int n,281const Int Ap [ ],282const Int Ai [ ],283Int Len [ ],284Int Tp [ ],285double Info [ ]286) ;287288GLOBAL void AMD_1289(290Int n,291const Int Ap [ ],292const Int Ai [ ],293Int P [ ],294Int Pinv [ ],295Int Len [ ],296Int slen,297Int S [ ],298double Control [ ],299double Info [ ]300) ;301302GLOBAL void AMD_2 (303Int n,304Int Pe [ ],305Int Iw [ ],306Int Len [ ],307Int iwlen,308Int pfree,309Int Nv [ ],310Int Next [ ],311Int Last [ ],312Int Head [ ],313Int Elen [ ],314Int Degree [ ],315Int W [ ],316double Control [ ],317double Info [ ]318) ;319320GLOBAL void AMD_postorder321(322Int nn,323Int Parent [ ],324Int Npiv [ ],325Int Fsize [ ],326Int Order [ ],327Int Child [ ],328Int Sibling [ ],329Int Stack [ ]330) ;331332GLOBAL Int AMD_post_tree333(334Int root,335Int k,336Int Child [ ],337const Int Sibling [ ],338Int Order [ ],339Int Stack [ ]340#ifndef NDEBUG341, Int nn342#endif343) ;344345GLOBAL void AMD_wpreprocess346(347Int n,348const Int Ap [ ],349const Int Ai [ ],350Int Rp [ ],351Int Ri [ ],352Int W [ ],353Int Flag [ ]354) ;355356GLOBAL Int AMD_preprocess_valid357(358Int n,359const Int Ap [ ],360const Int Ai [ ]361) ;362363/* ------------------------------------------------------------------------- */364/* debugging definitions */365/* ------------------------------------------------------------------------- */366367/* from assert.h: assert macro */368#if !defined (MATHWORKS) && !defined (MATLAB_MEX_FILE)369#include <assert.h>370#endif371372#ifndef NDEBUG373374GLOBAL Int AMD_debug ;375376GLOBAL void AMD_debug_init ( char *s ) ;377378GLOBAL void AMD_dump (379Int n,380Int Pe [ ],381Int Iw [ ],382Int Len [ ],383Int iwlen,384Int pfree,385Int Nv [ ],386Int Next [ ],387Int Last [ ],388Int Head [ ],389Int Elen [ ],390Int Degree [ ],391Int W [ ],392Int nel393) ;394395#ifdef ASSERT396#undef ASSERT397#endif398399#ifdef MATLAB_MEX_FILE400#define ASSERT(expression) (mxAssert ((expression), ""))401#else402#ifdef MATHWORKS403#define ASSERT(expression) (utAssert (expression))404#else405#define ASSERT(expression) (assert (expression))406#endif407#endif /* MATLAB_MEX_FILE */408409#define AMD_DEBUG0(params) { PRINTF (params) ; }410#define AMD_DEBUG1(params) { if (AMD_debug >= 1) PRINTF (params) ; }411#define AMD_DEBUG2(params) { if (AMD_debug >= 2) PRINTF (params) ; }412#define AMD_DEBUG3(params) { if (AMD_debug >= 3) PRINTF (params) ; }413#define AMD_DEBUG4(params) { if (AMD_debug >= 4) PRINTF (params) ; }414415#else416417#define AMD_DEBUG0(params)418#define AMD_DEBUG1(params)419#define AMD_DEBUG2(params)420#define AMD_DEBUG3(params)421#define AMD_DEBUG4(params)422423#define ASSERT(expression)424425#endif426427428