aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/amd/amd_order.c
blob: 332d566378493aad19276acd31d92f3f879df71c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* ========================================================================= */
/* === AMD_order =========================================================== */
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* AMD, Copyright (c) Timothy A. Davis,                                      */
/* Patrick R. Amestoy, and Iain S. Duff.  See ../README.txt for License.     */
/* email: davis at cise.ufl.edu    CISE Department, Univ. of Florida.        */
/* web: http://www.cise.ufl.edu/research/sparse/amd                          */
/* ------------------------------------------------------------------------- */

/* User-callable AMD minimum degree ordering routine.  See amd.h for
 * documentation.
 */

#include "amd_internal.h"

/* ========================================================================= */
/* === AMD_order =========================================================== */
/* ========================================================================= */

GLOBAL Int AMD_order
(
    Int n,
    const Int Ap [ ],
    const Int Ai [ ],
    Int P [ ],
    double Control [ ],
    double Info [ ]
)
{
    Int *Len, *S, nz, i, *Pinv, info, status, *Rp, *Ri, *Cp, *Ci, ok ;
    size_t nzaat, slen ;
    double mem = 0 ;

#ifndef NDEBUG
    AMD_debug_init ("amd") ;
#endif

    /* clear the Info array, if it exists */
    info = Info != (double *) NULL ;
    if (info)
    {
        for (i = 0 ; i < AMD_INFO ; i++)
        {
            Info [i] = EMPTY ;
        }
        Info [AMD_N] = n ;
        Info [AMD_STATUS] = AMD_OK ;
    }

    /* make sure inputs exist and n is >= 0 */
    if (Ai == (Int *) NULL || Ap == (Int *) NULL || P == (Int *) NULL || n < 0)
    {
        if (info) Info [AMD_STATUS] = AMD_INVALID ;
        return (AMD_INVALID) ;      /* arguments are invalid */
    }

    if (n == 0)
    {
        return (AMD_OK) ;           /* n is 0 so there's nothing to do */
    }

    nz = Ap [n] ;
    if (info)
    {
        Info [AMD_NZ] = nz ;
    }
    if (nz < 0)
    {
        if (info) Info [AMD_STATUS] = AMD_INVALID ;
        return (AMD_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 [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
        return (AMD_OUT_OF_MEMORY) ;        /* problem too large */
    }

    /* check the input matrix:  AMD_OK, AMD_INVALID, or AMD_OK_BUT_JUMBLED */
    status = AMD_valid (n, n, Ap, Ai) ;

    if (status == AMD_INVALID)
    {
        if (info) Info [AMD_STATUS] = AMD_INVALID ;
        return (AMD_INVALID) ;      /* matrix is invalid */
    }

    /* allocate two size-n integer workspaces */
    Len = amd_malloc (n * sizeof (Int)) ;
    Pinv = amd_malloc (n * sizeof (Int)) ;
    mem += n ;
    mem += n ;
    if (!Len || !Pinv)
    {
        /* :: out of memory :: */
        amd_free (Len) ;
        amd_free (Pinv) ;
        if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
        return (AMD_OUT_OF_MEMORY) ;
    }

    if (status == AMD_OK_BUT_JUMBLED)
    {
        /* sort the input matrix and remove duplicate entries */
        AMD_DEBUG1 (("Matrix is jumbled\n")) ;
        Rp = amd_malloc ((n+1) * sizeof (Int)) ;
        Ri = amd_malloc (MAX (nz,1) * sizeof (Int)) ;
        mem += (n+1) ;
        mem += MAX (nz,1) ;
        if (!Rp || !Ri)
        {
            /* :: out of memory :: */
            amd_free (Rp) ;
            amd_free (Ri) ;
            amd_free (Len) ;
            amd_free (Pinv) ;
            if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
            return (AMD_OUT_OF_MEMORY) ;
        }
        /* use Len and Pinv as workspace to create R = A' */
        AMD_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 = AMD_aat (n, Cp, Ci, Len, P, Info) ;
    AMD_DEBUG1 (("nzaat: %g\n", (double) nzaat)) ;
    ASSERT ((MAX (nz-n, 0) <= nzaat) && (nzaat <= 2 * (size_t) nz)) ;

    /* --------------------------------------------------------------------- */
    /* allocate workspace for matrix, elbow room, and 6 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 < 7 ; i++)
    {
        ok = ((slen + n) > slen) ;      /* check for size_t overflow */
        slen += n ;                     /* size-n elbow room, 6 size-n work */
    }
    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 = amd_malloc (slen * sizeof (Int)) ;
    }
    AMD_DEBUG1 (("slen %g\n", (double) slen)) ;
    if (!S)
    {
        /* :: out of memory :: (or problem too large) */
        amd_free (Rp) ;
        amd_free (Ri) ;
        amd_free (Len) ;
        amd_free (Pinv) ;
        if (info) Info [AMD_STATUS] = AMD_OUT_OF_MEMORY ;
        return (AMD_OUT_OF_MEMORY) ;
    }
    if (info)
    {
        /* memory usage, in bytes. */
        Info [AMD_MEMORY] = mem * sizeof (Int) ;
    }

    /* --------------------------------------------------------------------- */
    /* order the matrix */
    /* --------------------------------------------------------------------- */

    AMD_1 (n, Cp, Ci, P, Pinv, Len, slen, S, Control, Info) ;

    /* --------------------------------------------------------------------- */
    /* free the workspace */
    /* --------------------------------------------------------------------- */

    amd_free (Rp) ;
    amd_free (Ri) ;
    amd_free (Len) ;
    amd_free (Pinv) ;
    amd_free (S) ;
    if (info) Info [AMD_STATUS] = status ;
    return (status) ;       /* successful ordering */
}