-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathblkchol.c
440 lines (430 loc) · 18.7 KB
/
blkchol.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
% [L.L, L.d, skip, diagadd] = blkchol(L,ADA [,pars [,absd]])
% BLKCHOL Computes sparse lower-triangular Cholesky factor L,
% L*L' = P(perm,perm)
% Input Parameter L is typically generated by symbchol.
% Parameters: pars.canceltol and pars.maxu.
% Optional: absd to force adding diag if drops below canceltol*absd.
%
% There are important differences with standard CHOL(P(L.perm,L.perm))':
%
% - BLKCHOL uses the supernodal partition XSUPER, possibly splitted
% by SPLIT, to use dense linear algebra on dense subblocks.
% Much faster than CHOL.
%
% - BLKCHOL never fails. It only sees the lower triangular part of P;
% if during the elimination, a diagonal entry becomes negative
% (due to massive cancelation), the corresponding d[k] is set to 0.
% If d[k] suffers from cancelation and norm(L(:,k)) becomes too big, then
% the column is skipped, and listed in (skip, Lskip).
%
% SEE ALSO sparchol, fwblkslv, bwblkslv
% This file is part of SeDuMi 1.1 by Imre Polik and Oleksandr Romanko
% Copyright (C) 2005 McMaster University, Hamilton, CANADA (since 1.1)
%
% Copyright (C) 2001 Jos F. Sturm (up to 1.05R5)
% Dept. Econometrics & O.R., Tilburg University, the Netherlands.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% Affiliation SeDuMi 1.03 and 1.04Beta (2000):
% Dept. Quantitative Economics, Maastricht University, the Netherlands.
%
% Affiliations up to SeDuMi 1.02 (AUG1998):
% CRL, McMaster University, Canada.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
% 02110-1301, USA
*/
#include <string.h>
#include "mex.h"
#include "blksdp.h"
#define L_OUT myplhs[0]
#define D_OUT myplhs[1]
#define SKIP_OUT myplhs[2]
#define DIAGADD_OUT myplhs[3]
#define NPAROUT 4
#define L_IN prhs[0]
#define P_IN prhs[1]
#define NPARINMIN 2
#define PARS_IN prhs[2]
#define ABSD_IN prhs[3]
#define NPARIN 4
/* ------------------------------------------------------------
PROTOTYPES:
------------------------------------------------------------ */
mwIndex blkLDL(const mwIndex neqns, const mwIndex nsuper, const mwIndex *xsuper,
const mwIndex *snode, const mwIndex *xlindx, const mwIndex *lindx,
double *lb,
const mwIndex *ljc, double *lpr, double *d, const mwIndex *perm,
const double ub, const double maxu, mwIndex *skipIr,
mwIndex iwsiz, mwIndex *iwork, mwIndex fwsiz, double *fwork);
/* ============================================================
SUBROUTINES:
============================================================*/
/* ------------------------------------------------------------
PERMUTEP - Let L = tril(P(perm,perm))
INPUT
Ljc, Lir - sparsity structure of output matrix L = tril(P(perm,perm)).
Pjc, Pir, Ppr - Input matrix, before ordering.
perm - length m pivot ordering.
m - order: P is m x m.
WORKING ARRAY
Pj - Length m float work array.
IMPORTANT: L, P and PERM in C style.
------------------------------------------------------------ */
void permuteP(const mwIndex *Ljc,const mwIndex *Lir,double *Lpr,
const mwIndex *Pjc,const mwIndex *Pir,const double *Ppr,
const mwIndex *perm, double *Pj, const mwIndex m)
{
mwIndex j,inz,jcol;
/* ------------------------------------------------------------
Let Pj = all-0
------------------------------------------------------------ */
fzeros(Pj,m);
/* ------------------------------------------------------------
For each column j, let
Pj(:) = P(:,PERM(j)) and L(:,j) = Pj(PERM(:)) (L sparse)
------------------------------------------------------------ */
for(j = 0; j < m; j++){
jcol = perm[j];
for(inz = Pjc[jcol]; inz < Pjc[jcol+1]; inz++)
Pj[Pir[inz]] = Ppr[inz];
for(inz = Ljc[j]; inz < Ljc[j+1]; inz++)
Lpr[inz] = Pj[perm[Lir[inz]]];
/* ------------------------------------------------------------
Let Pj = all-0
------------------------------------------------------------ */
for(inz = Pjc[jcol]; inz < Pjc[jcol+1]; inz++)
Pj[Pir[inz]] = 0.0;
}
}
/* ************************************************************
SPCHOL - calls the block cholesky blkLDL.
INPUT:
m - Order of L: L is m x m, ne.At is N x m.
nsuper - Number of supernodes (blocks).
xsuper - Length nsuper+1: first simple-node of each supernode
snode - Length neqns: snode(node) is the supernode containing "node".
ljc - Length neqns+1: start of the columns of L.
abstol - minimum diagonal value. If 0, then no absolute threshold.
canceltol - Force d >= canceltol * orgd (by adding low-rank diag).
maxu - Maximal allowed max(abs(L(:,k)))..
If L gets to big in these columns, we skip the pivots.
iwsiz, fwsiz - size of integer and floating-point working storage.
See "WORKING ARRAYS" for required amount.
UPDATED:
lindx - row indices. On INPUT: for each column (by ljc),
on OUTPUT: for each supernode (by xlindx).
Lpr - On input, contains tril(X), on output
such that X = L*diag(d)*L'.
lb - Length neqns. INPUT: diag entries BEFORE cancelation;
OUTPUT: lb(skipIr) are values of low rank diag. matrix that is
added before factorization.
OUTPUT
xlindx - Length nsuper+1: Start of sparsity structure in lindx,
for each supernode (all simple nodes in a supernode have the
same nonzero-structure).
snode - Length m: snode(node) is the supernode containing "node".
d - length neqns vector, diagonal in L*diag(d)*L'.
skipIr - length nskip (<= neqns) array. skipIr(1:nskip) lists the
columns that have been skipped in the Cholesky. d[skipIr] = 0.
WORKING ARRAYS:
iwork - Length iwsiz working array; iwsiz = 2*m + 2 * nsuper.
fwork - Length fwsiz working vector; fwsiz = L.tmpsiz.
RETURNS - nskip (<=neqns), number of skipped nodes. Length of skipIr.
*********************************************************************** */
mwIndex spchol(const mwIndex m, const mwIndex nsuper, const mwIndex *xsuper,
mwIndex *snode, mwIndex *xlindx, mwIndex *lindx, double *lb,
const mwIndex *ljc, double *lpr, double *d, const mwIndex *perm,
const double abstol,
const double canceltol, const double maxu, mwIndex *skipIr,
mwIndex *pnadd,
const mwIndex iwsiz, mwIndex *iwork, const mwIndex fwsiz, double *fwork)
{
mwIndex jsup,j,ix,jcol,collen, nskip, nadd;
double ub, dj;
/* ------------------------------------------------------------
Let ub = max(diag(L)) / maxu^2
------------------------------------------------------------ */
ub = 0.0;
for(j = 0; j < m; j++)
if((dj = lpr[ljc[j]]) > ub)
ub = dj;
ub /= SQR(maxu);
/* ------------------------------------------------------------
Let lb = MAX(abstol, canceltol * lbIN), where lbIN is diag(L),
or those quantities before being affected by cancelation.
------------------------------------------------------------ */
for(j = 0; j < m; j++)
if((dj = canceltol * lb[j]) > abstol)
lb[j] = dj;
else
lb[j] = abstol;
/* ------------------------------------------------------------
SNODE: map each column to the supernode containing it
------------------------------------------------------------ */
j = xsuper[0];
for(jsup = 0; jsup < nsuper; jsup++){
while(j < xsuper[jsup + 1])
snode[j++] = jsup;
}
/* ------------------------------------------------------------
COMPRESS SUBSCRIPTS:
Let (xlindx,lindx) = ljc(xsuper(:)), i.e store only once
for each snode, instead of once per column.
------------------------------------------------------------ */
for(ix = 0, jsup = 0; jsup < nsuper; jsup++){
xlindx[jsup] = ix;
jcol = xsuper[jsup];
collen = ljc[jcol+1] - ljc[jcol];
memmove(lindx + ix, lindx + ljc[jcol], collen * sizeof(mwIndex));
ix += collen;
}
xlindx[nsuper] = ix;
/* ------------------------------------------------------------
Do the block sparse Cholesky L*D*L'
------------------------------------------------------------ */
nskip = blkLDL(m, nsuper, xsuper, snode, xlindx, lindx, lb,
ljc, lpr, d, perm,
ub, maxu, skipIr, iwsiz, iwork, fwsiz, fwork);
if(nskip == (mwIndex)-1)
return nskip;
/* ------------------------------------------------------------
Let iwork = diag-adding indices. Viz. where d(skipIr)>0.0.
Let skipIr = skipIr except diag-adding indices. Hence d(skipIr)=0.
------------------------------------------------------------ */
if(iwsiz < nskip)
return (mwIndex)-1;
ix = 0;
nadd = 0;
for(j = 0; j < nskip; j++){
jsup = skipIr[j];
if(d[jsup] > 0.0)
iwork[nadd++] = jsup; /* diagonal adding */
else
skipIr[ix++] = jsup; /* pivot skipping */
}
*pnadd = nadd;
return ix;
}
/* ============================================================
MAIN: MEXFUNCTION
============================================================ */
/* ************************************************************
PROCEDURE mexFunction - Entry for Matlab
************************************************************ */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
const mxArray *L_FIELD;
mxArray *myplhs[NPAROUT];
mwIndex m, i, j, iwsiz, nsuper, tmpsiz, fwsiz, nskip, nadd, m1;
double *fwork, *d, *skipPr, *orgd;
const double *permPr,*xsuperPr,*Ppr,*absd;
mwIndex *perm, *snode, *xsuper, *iwork, *xlindx, *skip, *skipJc;
const mwIndex *LINir, *Pjc, *Pir;
double canceltol, maxu, abstol;
jcir L;
char useAbsd, useDelay;
/* ------------------------------------------------------------
Check for proper number of arguments
blkchol(L,P, pars,absd) with nparinmin=2.
------------------------------------------------------------ */
mxAssert(nrhs >= NPARINMIN, "blkchol requires more input arguments");
mxAssert(nlhs <= NPAROUT, "blkchol produces less output arguments");
/* ------------------------------------------------------------
Get input matrix P to be factored.
------------------------------------------------------------ */
m = mxGetM(P_IN);
mxAssert( m == mxGetN(P_IN), "P must be square");
mxAssert(mxIsSparse(P_IN), "P must be sparse");
Pjc = mxGetJc(P_IN);
Pir = mxGetIr(P_IN);
Ppr = mxGetPr(P_IN);
/* ------------------------------------------------------------
Disassemble block Cholesky structure L
------------------------------------------------------------ */
mxAssert(mxIsStruct(L_IN), "Parameter `L' should be a structure.");
L_FIELD = mxGetField(L_IN,(mwIndex)0,"perm"); /* L.perm */
mxAssert( L_FIELD != NULL, "Missing field L.perm.");
mxAssert(m == mxGetM(L_FIELD) * mxGetN(L_FIELD), "perm size mismatch");
permPr = mxGetPr(L_FIELD);
L_FIELD = mxGetField(L_IN,(mwIndex)0,"L"); /* L.L */
mxAssert( L_FIELD != NULL, "Missing field L.L.");
mxAssert( m == mxGetM(L_FIELD) && m == mxGetN(L_FIELD), "Size L.L mismatch.");
mxAssert(mxIsSparse(L_FIELD), "L.L should be sparse.");
L.jc = mxGetJc(L_FIELD);
LINir = mxGetIr(L_FIELD);
L_FIELD = mxGetField(L_IN,(mwIndex)0,"xsuper"); /* L.xsuper */
mxAssert( L_FIELD != NULL, "Missing field L.xsuper.");
nsuper = mxGetM(L_FIELD) * mxGetN(L_FIELD) - 1;
mxAssert( nsuper <= m, "Size L.xsuper mismatch.");
xsuperPr = mxGetPr(L_FIELD);
L_FIELD = mxGetField(L_IN,(mwIndex)0,"tmpsiz"); /* L.tmpsiz */
mxAssert( L_FIELD != NULL, "Missing field L.tmpsiz.");
tmpsiz = (mwIndex) mxGetScalar(L_FIELD);
/* ------------------------------------------------------------
Disassemble pars structure: canceltol, maxu
------------------------------------------------------------ */
canceltol = 1E-12; /* supply with defaults */
maxu = 5E2;
abstol = 1E-20;
useAbsd = 0;
useDelay = 0;
if(nrhs >= NPARINMIN + 1){ /* 3rd argument = pars */
mxAssert(mxIsStruct(PARS_IN), "Parameter `pars' should be a structure.");
if( (L_FIELD = mxGetField(PARS_IN,(mwIndex)0,"canceltol")) != NULL)
canceltol = mxGetScalar(L_FIELD); /* pars.canceltol */
if( (L_FIELD = mxGetField(PARS_IN,(mwIndex)0,"maxu")) != NULL)
maxu = mxGetScalar(L_FIELD); /* pars.maxu */
if( (L_FIELD = mxGetField(PARS_IN,(mwIndex)0,"abstol")) != NULL){
abstol = mxGetScalar(L_FIELD); /* pars.abstol */
abstol = MAX(abstol, 0.0);
}
if( (L_FIELD = mxGetField(PARS_IN,(mwIndex)0,"delay")) != NULL)
useDelay = (char) mxGetScalar(L_FIELD); /* pars.delay */
/* ------------------------------------------------------------
Get optional vector absd
------------------------------------------------------------ */
if(nrhs >= NPARIN){
useAbsd = 1;
absd = mxGetPr(ABSD_IN);
mxAssert(m == mxGetM(ABSD_IN) * mxGetN(ABSD_IN), "absd size mismatch");
}
}
/* ------------------------------------------------------------
Create sparse output matrix L(m x m).
------------------------------------------------------------ */
L_OUT = mxCreateSparse(m,m, L.jc[m],mxREAL);
L.ir = mxGetIr(L_OUT);
L.pr = mxGetPr(L_OUT);
memcpy(mxGetJc(L_OUT), L.jc, (m+1) * sizeof(mwIndex));
memcpy(L.ir, LINir, L.jc[m] * sizeof(mwIndex));
/* ------------------------------------------------------------
Create ouput vector d(m).
------------------------------------------------------------ */
D_OUT = mxCreateDoubleMatrix(m,(mwSize)1,mxREAL);
d = mxGetPr(D_OUT);
/* ------------------------------------------------------------
Compute required sizes of working arrays:
iwsiz = 2*(m + nsuper).
fwsiz = tmpsiz.
------------------------------------------------------------ */
iwsiz = MAX(2*(m+nsuper), 1);
fwsiz = MAX(tmpsiz, 1);
/* ------------------------------------------------------------
Allocate working arrays:
integer: perm(m), snode(m), xsuper(nsuper+1),
iwork(iwsiz), xlindx(m+1), skip(m),
double: orgd(m), fwork(fwsiz).
------------------------------------------------------------ */
m1 = MAX(m,1); /* avoid alloc to 0 */
perm = (mwIndex *) mxCalloc(m1,sizeof(mwIndex));
snode = (mwIndex *) mxCalloc(m1,sizeof(mwIndex));
xsuper = (mwIndex *) mxCalloc(nsuper+1,sizeof(mwIndex));
iwork = (mwIndex *) mxCalloc(iwsiz,sizeof(mwIndex));
xlindx = (mwIndex *) mxCalloc(m+1,sizeof(mwIndex));
skip = (mwIndex *) mxCalloc(m1, sizeof(mwIndex));
orgd = (double *) mxCalloc(m1,sizeof(double));
fwork = (double *) mxCalloc(fwsiz,sizeof(double));
/* ------------------------------------------------------------
Convert PERM, XSUPER to integer and C-Style
------------------------------------------------------------ */
for(i = 0; i < m; i++){
j = (mwIndex) permPr[i];
mxAssert(j>0,"");
perm[i] = --j;
}
for(i = 0; i <= nsuper; i++){
j = (mwIndex) xsuperPr[i];
mxAssert(j>0,"");
xsuper[i] = --j;
}
/* ------------------------------------------------------------
Let L = tril(P(PERM,PERM)), uses orgd(m) as temp working storage.
------------------------------------------------------------ */
permuteP(L.jc,L.ir,L.pr, Pjc,Pir,Ppr, perm, orgd, m);
/* ------------------------------------------------------------
If no orgd has been supplied, take orgd = diag(L on input)
Otherwise, let orgd = absd(perm).
------------------------------------------------------------ */
if(useAbsd)
for(j = 0; j < m; j++)
orgd[j] = absd[perm[j]];
else
for(j = 0; j < m; j++)
orgd[j] = L.pr[L.jc[j]];
/* ------------------------------------------------------------
Create "snode" and "xlindx"; change L.ir to the compact subscript
array (with xlindx), and do BLOCK SPARSE CHOLESKY.
------------------------------------------------------------ */
nskip = spchol(m, nsuper, xsuper, snode, xlindx,
L.ir, orgd, L.jc, L.pr, d, perm, abstol,
canceltol, maxu, skip, &nadd, iwsiz, iwork, fwsiz, fwork);
mxAssert(nskip >= 0, "Insufficient workspace in pblkchol");
/* ------------------------------------------------------------
Copy original row-indices from LINir to L.ir.
------------------------------------------------------------ */
memcpy(L.ir, LINir, L.jc[m] * sizeof(mwIndex));
/* ------------------------------------------------------------
Create output matrices skip = sparse([],[],[],m,1,nskip),
diagadd = sparse([],[],[],m,1,nadd),
------------------------------------------------------------ */
SKIP_OUT = mxCreateSparse(m,(mwSize)1, MAX(1,nskip),mxREAL);
memcpy(mxGetIr(SKIP_OUT), skip, nskip * sizeof(mwIndex));
skipJc = mxGetJc(SKIP_OUT);
skipJc[0] = 0; skipJc[1] = nskip;
skipPr = mxGetPr(SKIP_OUT);
/* ------------------------------------------------------------
useDelay = 1 then L(:,i) is i-th column before ith pivot; useful
for pivot-delaying strategy. (Fwslv(L, L(:,i)) still required.)
------------------------------------------------------------ */
if(useDelay == 1)
for(j = 0; j < nskip; j++)
skipPr[j] = 1.0;
else
for(j = 0; j < nskip; j++){
i = skip[j];
skipPr[j] = L.pr[L.jc[i]]; /* Set skipped l(:,i)=ei. */
L.pr[L.jc[i]] = 1.0;
fzeros(L.pr+L.jc[i]+1,L.jc[i+1]-L.jc[i]-1);
}
DIAGADD_OUT = mxCreateSparse(m,(mwSize)1, MAX(1,nadd),mxREAL);
memcpy(mxGetIr(DIAGADD_OUT), iwork, nadd * sizeof(mwIndex));
skipJc = mxGetJc(DIAGADD_OUT);
skipJc[0] = 0; skipJc[1] = nadd;
skipPr = mxGetPr(DIAGADD_OUT);
for(j = 0; j < nadd; j++)
skipPr[j] = orgd[iwork[j]];
/* ------------------------------------------------------------
Release working arrays.
------------------------------------------------------------ */
mxFree(fwork);
mxFree(orgd);
mxFree(skip);
mxFree(xlindx);
mxFree(iwork);
mxFree(xsuper);
mxFree(snode);
mxFree(perm);
/* ------------------------------------------------------------
Copy requested output parameters (at least 1), release others.
------------------------------------------------------------ */
i = MAX(nlhs, 1);
memcpy(plhs,myplhs, i * sizeof(mxArray *));
for(; i < NPAROUT; i++)
mxDestroyArray(myplhs[i]);
}