Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_amd_l_order.c
Go to the documentation of this file.
1 /* ========================================================================= */
2 /* === AMD_order =========================================================== */
3 /* ========================================================================= */
4 
5 /* ------------------------------------------------------------------------- */
6 /* AMD, Copyright (c) Timothy A. Davis, */
7 /* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
8 /* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
9 /* web: http://www.cise.ufl.edu/research/sparse/amd */
10 /* ------------------------------------------------------------------------- */
11 
12 /* User-callable AMD minimum degree ordering routine. See amd.h for
13  * documentation.
14  */
15 
16 /* This file should make the long int version of AMD */
17 #define DLONG 1
18 
19 #include "amesos_amd_internal.h"
20 
21 /* ========================================================================= */
22 /* === AMD_order =========================================================== */
23 /* ========================================================================= */
24 
26 (
27  Int n,
28  const Int Ap [ ],
29  const Int Ai [ ],
30  Int P [ ],
31  double Control [ ],
32  double Info [ ]
33 )
34 {
35  Int *Len, *S, nz, i, *Pinv, info, status, *Rp, *Ri, *Cp, *Ci, ok ;
36  size_t nzaat, slen ;
37  double mem = 0 ;
38 
39 #ifndef NDEBUG
40  AMD_debug_init ("amd") ;
41 #endif
42 
43  /* clear the Info array, if it exists */
44  info = Info != (double *) NULL ;
45  if (info)
46  {
47  for (i = 0 ; i < AMD_INFO ; i++)
48  {
49  Info [i] = EMPTY ;
50  }
51  Info [AMD_N] = n ;
52  Info [AMD_STATUS] = AMD_OK ;
53  }
54 
55  /* make sure inputs exist and n is >= 0 */
56  if (Ai == (Int *) NULL || Ap == (Int *) NULL || P == (Int *) NULL || n < 0)
57  {
58  if (info) Info [AMD_STATUS] = AMD_INVALID ;
59  return (AMD_INVALID) ; /* arguments are invalid */
60  }
61 
62  if (n == 0)
63  {
64  return (AMD_OK) ; /* n is 0 so there's nothing to do */
65  }
66 
67  nz = Ap [n] ;
68  if (info)
69  {
70  Info [AMD_NZ] = nz ;
71  }
72  if (nz < 0)
73  {
74  if (info) Info [AMD_STATUS] = AMD_INVALID ;
75  return (AMD_INVALID) ;
76  }
77 
78  /* check if n or nz will cause size_t overflow */
79  if (((size_t) n) >= SIZE_T_MAX / sizeof (Int)
80  || ((size_t) nz) >= SIZE_T_MAX / sizeof (Int))
81  {
82  if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
83  return (AMD_OUT_OF_MEMORY) ; /* problem too large */
84  }
85 
86  /* check the input matrix: AMD_OK, AMD_INVALID, or AMD_OK_BUT_JUMBLED */
87  status = AMD_valid (n, n, Ap, Ai) ;
88 
89  if (status == AMD_INVALID)
90  {
91  if (info) Info [AMD_STATUS] = AMD_INVALID ;
92  return (AMD_INVALID) ; /* matrix is invalid */
93  }
94 
95  /* allocate two size-n integer workspaces */
96  Len = amesos_amd_malloc (n * sizeof (Int)) ;
97  Pinv = amesos_amd_malloc (n * sizeof (Int)) ;
98  mem += n ;
99  mem += n ;
100  if (!Len || !Pinv)
101  {
102  /* :: out of memory :: */
103  amesos_amd_free (Len) ;
104  amesos_amd_free (Pinv) ;
105  if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
106  return (AMD_OUT_OF_MEMORY) ;
107  }
108 
109  if (status == AMD_OK_BUT_JUMBLED)
110  {
111  /* sort the input matrix and remove duplicate entries */
112  AMD_DEBUG1 (("Matrix is jumbled\n")) ;
113  Rp = amesos_amd_malloc ((n+1) * sizeof (Int)) ;
114  Ri = amesos_amd_malloc (MAX (nz,1) * sizeof (Int)) ;
115  mem += (n+1) ;
116  mem += MAX (nz,1) ;
117  if (!Rp || !Ri)
118  {
119  /* :: out of memory :: */
120  amesos_amd_free (Rp) ;
121  amesos_amd_free (Ri) ;
122  amesos_amd_free (Len) ;
123  amesos_amd_free (Pinv) ;
124  if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
125  return (AMD_OUT_OF_MEMORY) ;
126  }
127  /* use Len and Pinv as workspace to create R = A' */
128  AMD_preprocess (n, Ap, Ai, Rp, Ri, Len, Pinv) ;
129  Cp = Rp ;
130  Ci = Ri ;
131  }
132  else
133  {
134  /* order the input matrix as-is. No need to compute R = A' first */
135  Rp = NULL ;
136  Ri = NULL ;
137  Cp = (Int *) Ap ;
138  Ci = (Int *) Ai ;
139  }
140 
141  /* --------------------------------------------------------------------- */
142  /* determine the symmetry and count off-diagonal nonzeros in A+A' */
143  /* --------------------------------------------------------------------- */
144 
145  nzaat = AMD_aat (n, Cp, Ci, Len, P, Info) ;
146  AMD_DEBUG1 (("nzaat: %g\n", (double) nzaat)) ;
147  ASSERT ((MAX (nz-n, 0) <= nzaat) && (nzaat <= 2 * (size_t) nz)) ;
148 
149  /* --------------------------------------------------------------------- */
150  /* allocate workspace for matrix, elbow room, and 6 size-n vectors */
151  /* --------------------------------------------------------------------- */
152 
153  S = NULL ;
154  slen = nzaat ; /* space for matrix */
155  ok = ((slen + nzaat/5) >= slen) ; /* check for size_t overflow */
156  slen += nzaat/5 ; /* add elbow room */
157  for (i = 0 ; ok && i < 7 ; i++)
158  {
159  ok = ((slen + n) > slen) ; /* check for size_t overflow */
160  slen += n ; /* size-n elbow room, 6 size-n work */
161  }
162  mem += slen ;
163  ok = ok && (slen < SIZE_T_MAX / sizeof (Int)) ; /* check for overflow */
164  ok = ok && (slen < Int_MAX) ; /* S[i] for Int i must be OK */
165  if (ok)
166  {
167  S = amesos_amd_malloc (slen * sizeof (Int)) ;
168  }
169  AMD_DEBUG1 (("slen %g\n", (double) slen)) ;
170  if (!S)
171  {
172  /* :: out of memory :: (or problem too large) */
173  amesos_amd_free (Rp) ;
174  amesos_amd_free (Ri) ;
175  amesos_amd_free (Len) ;
176  amesos_amd_free (Pinv) ;
177  if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
178  return (AMD_OUT_OF_MEMORY) ;
179  }
180  if (info)
181  {
182  /* memory usage, in bytes. */
183  Info [AMD_MEMORY] = mem * sizeof (Int) ;
184  }
185 
186  /* --------------------------------------------------------------------- */
187  /* order the matrix */
188  /* --------------------------------------------------------------------- */
189 
190  AMD_1 (n, Cp, Ci, P, Pinv, Len, slen, S, Control, Info) ;
191 
192  /* --------------------------------------------------------------------- */
193  /* free the workspace */
194  /* --------------------------------------------------------------------- */
195 
196  amesos_amd_free (Rp) ;
197  amesos_amd_free (Ri) ;
198  amesos_amd_free (Len) ;
199  amesos_amd_free (Pinv) ;
200  amesos_amd_free (S) ;
201  if (info) Info [AMD_STATUS] = status ;
202  return (status) ; /* successful ordering */
203 }
#define AMD_1
#define EMPTY
GLOBAL Int AMD_order(Int n, const Int Ap[], const Int Ai[], Int P[], double Control[], double Info[])
#define GLOBAL
#define AMD_DEBUG1(params)
#define Int
#define P(k)
EXTERN void(* amesos_amd_free)(void *)
Definition: amesos_amd.h:319
#define AMD_STATUS
Definition: amesos_amd.h:352
#define MAX(a, b)
#define NULL
#define ASSERT(expression)
#define AMD_MEMORY
Definition: amesos_amd.h:359
#define AMD_preprocess
#define AMD_INVALID
Definition: amesos_amd.h:373
#define AMD_aat
#define Int_MAX
#define AMD_OK
Definition: amesos_amd.h:371
#define AMD_valid
#define AMD_N
Definition: amesos_amd.h:353
#define SIZE_T_MAX
#define AMD_NZ
Definition: amesos_amd.h:354
#define AMD_OK_BUT_JUMBLED
Definition: amesos_amd.h:374
#define AMD_OUT_OF_MEMORY
Definition: amesos_amd.h:372
#define AMD_debug_init
#define AMD_INFO
Definition: amesos_amd.h:341
EXTERN void *(* amesos_amd_malloc)(size_t)
Definition: amesos_amd.h:318