File: lsubsolve.c

package info (click to toggle)
suitesparse 1%3A7.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 258,172 kB
  • sloc: ansic: 1,153,566; cpp: 48,145; makefile: 4,997; fortran: 2,087; java: 1,826; sh: 1,113; ruby: 725; python: 676; asm: 371; sed: 166; awk: 44
file content (266 lines) | stat: -rw-r--r-- 8,765 bytes parent folder | download | duplicates (3)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//------------------------------------------------------------------------------
// CHOLMOD/MATLAB/lsubsolve: MATLAB interface to CHOLMOD solver
//------------------------------------------------------------------------------

// CHOLMOD/MATLAB Module.  Copyright (C) 2005-2023, Timothy A. Davis.
// All Rights Reserved.
// SPDX-License-Identifier: GPL-2.0+

//------------------------------------------------------------------------------

// [x xset] = lsubsolve (L,kind,P,b,system)
//
// L is a sparse lower triangular matrix, of size n-by-n.  kind = 0 if
// L is from an LL' factorization, 1 if from LDL' (in which case L contains
// L in the lower part and D on the diagonal).  P is a permutation vector, or
// empty (which means P is identity).  b is a sparse column vector.
// system is a number between 0 and 8:
//
// Given L or LD, a permutation P, and a sparse right-hand size b,
// solve one of the following systems:
//
//      Ax=b        0: CHOLMOD_A        also applies the permutation L->Perm
//      LDL'x=b     1: CHOLMOD_LDLt     does not apply L->Perm
//      LDx=b       2: CHOLMOD_LD
//      DL'x=b      3: CHOLMOD_DLt
//      Lx=b        4: CHOLMOD_L
//      L'x=b       5: CHOLMOD_Lt
//      Dx=b        6: CHOLMOD_D
//      x=Pb        7: CHOLMOD_P        apply a permutation (P is L->Perm)
//      x=P'b       8: CHOLMOD_Pt       apply an inverse permutation
//
// The solution x is a dense vector, but it is a subset of the entire solution,
// x is zero except where xset is 1.  xset is reach of b (or P*b) in the graph
// of L.  If P is empty then it is treated as the identity permutation.
//
// No zeros can be dropped from the stucture of the Cholesky factorization
// because this function gets its elimination tree from L itself.  See ldlchol
// for a method of constructing a sparse L with explicit zeros that are
// normally dropped by MATLAB.
//
// This function is only meant for testing the cholmod_solve2 function.  The
// cholmod_solve2 function takes O(flops) time, but the setup time in this
// mexFunction wrapper can dominate that time.

#include "sputil2.h"

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    double dummy = 0, *Px ;
    int64_t *Lp, *Lnz, *Xp, *Xi, xnz, *Perm, *Lprev, *Lnext ;
    cholmod_sparse *Bset, Bmatrix, *Xset ;
    cholmod_dense *Bdense, *X, *Y, *E ;
    cholmod_factor *L ;
    cholmod_common Common, *cm ;
    int64_t k, j, n, head, tail, xsetlen ;
    int sys, kind ;

    //--------------------------------------------------------------------------
    // start CHOLMOD and set parameters
    //--------------------------------------------------------------------------

    cm = &Common ;
    cholmod_l_start (cm) ;
    sputil2_config (SPUMONI, cm) ;

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    if (nargin != 5 || nargout > 2)
    {
        mexErrMsgTxt ("usage: [x xset] = lsubsolve (L,kind,P,b,system)") ;
    }

    n = mxGetN (pargin [0]) ;
    if (!mxIsSparse (pargin [0]) || n != mxGetM (pargin [0]))
    {
        mexErrMsgTxt ("lsubsolve: L must be sparse and square") ;
    }
    if (mxGetNumberOfElements (pargin [1]) != 1)
    {
        mexErrMsgTxt ("lsubsolve: kind must be a scalar") ;
    }

    if (mxIsSparse (pargin [2]) ||
       !(mxIsEmpty (pargin [2]) || mxGetNumberOfElements (pargin [2]) == n))
    {
        mexErrMsgTxt ("lsubsolve: P must be size n, or empty") ;
    }

    if (mxGetM (pargin [3]) != n || mxGetN (pargin [3]) != 1)
    {
        mexErrMsgTxt ("lsubsolve: b wrong dimension") ;
    }
    if (!mxIsSparse (pargin [3]))
    {
        mexErrMsgTxt ("lxbpattern: b must be sparse") ;
    }
    if (mxGetNumberOfElements (pargin [4]) != 1)
    {
        mexErrMsgTxt ("lsubsolve: system must be a scalar") ;
    }

    //--------------------------------------------------------------------------
    // get the inputs
    //--------------------------------------------------------------------------

    kind = (int) mxGetScalar (pargin [1]) ;
    sys  = (int) mxGetScalar (pargin [4]) ;

    //--------------------------------------------------------------------------
    // get the sparse b
    //--------------------------------------------------------------------------

    // get sparse matrix B (unsymmetric)
    size_t Bset_xsize = 0 ;
    Bset = sputil2_get_sparse (pargin [3], 0, CHOLMOD_DOUBLE, &Bmatrix,
        &Bset_xsize, cm) ;

    Bdense = cholmod_l_sparse_to_dense (Bset, cm) ;
    Bset->x = NULL ;
    Bset->z = NULL ;
    Bset->xtype = CHOLMOD_PATTERN ;

    //--------------------------------------------------------------------------
    // construct a shallow copy of the input sparse matrix L
    //--------------------------------------------------------------------------

    // the construction of the CHOLMOD takes O(n) time and memory

    // allocate the CHOLMOD symbolic L
    L = cholmod_l_allocate_factor (n, cm) ;
    L->ordering = CHOLMOD_NATURAL ;

    // get the MATLAB L
    L->p = mxGetJc (pargin [0]) ;
    L->i = mxGetIr (pargin [0]) ;
    L->x = mxGetData (pargin [0]) ;
    L->z = NULL ;

    // allocate and initialize the rest of L
    L->nz = cholmod_l_malloc (n, sizeof (int64_t), cm) ;
    Lp = L->p ;
    Lnz = L->nz ;
    for (j = 0 ; j < n ; j++)
    {
        Lnz [j] = Lp [j+1] - Lp [j] ;
    }

    // these pointers are not accessed in cholmod_solve2
    L->prev = cholmod_l_malloc (n+2, sizeof (int64_t), cm) ;
    L->next = cholmod_l_malloc (n+2, sizeof (int64_t), cm) ;
    Lprev = L->prev ;
    Lnext = L->next ;

    head = n+1 ;
    tail = n ;
    Lnext [head] = 0 ;
    Lprev [head] = -1 ;
    Lnext [tail] = -1 ;
    Lprev [tail] = n-1 ;
    for (j = 0 ; j < n ; j++)
    {
        Lnext [j] = j+1 ;
        Lprev [j] = j-1 ;
    }
    Lprev [0] = head ;

    L->xtype = (mxIsComplex (pargin [0])) ? CHOLMOD_COMPLEX : CHOLMOD_REAL ;
    L->nzmax = Lp [n] ;

    // get the permutation
    if (mxIsEmpty (pargin [2]))
    {
        L->Perm = NULL ;
        Perm = NULL ;
    }
    else
    {
        // convert from double to int64_t, and from 1-based to 0-based
        L->ordering = CHOLMOD_GIVEN ;
        L->Perm = cholmod_l_malloc (n, sizeof (int64_t), cm) ;
        Perm = L->Perm ;
        Px = (double *) mxGetData (pargin [2]) ;
        for (k = 0 ; k < n ; k++)
        {
            Perm [k] = ((int64_t) Px [k]) - 1 ;
        }
    }

    // set the kind, LL' or LDL'
    L->is_ll = (kind == 0) ;

    //--------------------------------------------------------------------------
    // solve the system
    //--------------------------------------------------------------------------

    X = cholmod_l_zeros (n, 1, L->xtype, cm) ;
    Xset = NULL ;
    Y = NULL ;
    E = NULL ;

    cholmod_l_solve2 (sys, L, Bdense, Bset, &X, &Xset, &Y, &E, cm) ;

    cholmod_l_free_dense (&Y, cm) ;
    cholmod_l_free_dense (&E, cm) ;

    //--------------------------------------------------------------------------
    // return result
    //--------------------------------------------------------------------------

    pargout [0] = sputil2_put_dense (&X, mxDOUBLE_CLASS, cm) ;

    // fill numerical values of Xset with one's
    if (Xset == NULL)
    {
        // Px=b and P'x=b do not return Xset.  Create Xset = 0:n-1
        Xset = cholmod_l_spzeros (n, 1, n, CHOLMOD_REAL, cm) ;
        int64_t *Xsetp = Xset->p ;
        int64_t *Xseti = Xset->i ;
        double  *Xsetx = Xset->x ;
        Xsetp [0] = 0 ;
        Xsetp [1] = n ;
        for (k = 0 ; k < n ; k++)
        {
            Xseti [k] = k ;
            Xsetx [k] = 1 ;
        }
    }
    else
    {
        // Xset is returned, but needs to be converted from PATTERN to REAL
        int64_t *Xsetp = Xset->p ;
        xsetlen = Xsetp [1] ;
        Xset->x = cholmod_l_malloc (xsetlen, sizeof (double), cm) ;
        double *Xsetx = Xset->x ;
        for (k = 0 ; k < xsetlen ; k++)
        {
            Xsetx [k] = 1 ;
        }
        Xset->xtype = CHOLMOD_REAL ;
    }

    pargout [1] = sputil2_put_sparse (&Xset, mxDOUBLE_CLASS,
        /* Xset is binary so it has no zeros to drop */ false, cm) ;

    //--------------------------------------------------------------------------
    // free workspace and the CHOLMOD L, except for what is copied to MATLAB
    //--------------------------------------------------------------------------

    L->p = NULL ;
    L->i = NULL ;
    L->x = NULL ;
    L->z = NULL ;
    cholmod_l_free_factor (&L, cm) ;
    sputil2_free_sparse (&Bset, &Bmatrix, Bset_xsize, cm) ;
    cholmod_l_finish (cm) ;
    if (SPUMONI > 0) cholmod_l_print_common (" ", cm) ;
}