-
Notifications
You must be signed in to change notification settings - Fork 91
/
choltmpsiz.c
173 lines (164 loc) · 7.5 KB
/
choltmpsiz.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
/*
L.tmpsiz = choltmpsiz(L)
% 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
*/
#define TMPSIZ_OUT plhs[0]
#define NPAROUT 1
#define L_IN prhs[0] /* symbolic Cholesky structure: {L.L, L.xsuper} */
#define NPARIN 1
#include "mex.h"
/* ************************************************************
PROCEDURE gettmpsiz - Compute "fwork"-size in PRECORRECT of
BLKCHOL2. Since fwork = -(Lk * inv(Dk) * Lk')_{snode j}, we have
tmpsiz = MAX_{k,j in SUPER} mk * q - q(q-1)/2,
with q := ncolup(k,j) = #nz-rows in L(:,k) corresponding to
subnodes of j.
mk := #nz-rows in L(:,k) corresponding to subnodes of j : nsuper.
INPUT
ljc,lir - sparsity structure of m x m matrix L (not compressed).
xsuper,nsuper - supernodal partition of nodes 1:m.
snode - maps nodes 1:m to supernode containing it.
RETURNS tmpsiz.
************************************************************ */
mwIndex gettmpsiz(const mwIndex *ljc,const mwIndex *lir,const mwIndex *xsuper,
const mwIndex nsuper, const mwIndex *snode)
{
mwIndex tmpsiz, ksup,k,j,i,nextj, mk,inz,ncolup, sizkj,ubsiz;
tmpsiz = 0;
/* ------------------------------------------------------------
For each supernode ksup = 1:nsuper, and affected supernode snode[j]:
ncolup = #nz-rows in L(:,k) corresponding to subnodes of snode[j].
mk := #nz-rows in L(:,k) corresponding to subnodes of snode[j] : nsuper.
------------------------------------------------------------ */
for(ksup = 0; ksup < nsuper; ksup++){
k = xsuper[ksup];
/* ------------------------------------------------------------
Let mk be number of below-diag-block(k) nonzeros. This
is upper bound on both q and mk.
------------------------------------------------------------ */
inz = ljc[k] + (xsuper[ksup+1] - k); /* points below diag-block */
mk = ljc[k+1] - inz;
ubsiz = mk * (mk + 1) / 2; /* ubound on tmpsiz(k) */
i = lir[ljc[k+1]-1]; /* last subscript in k */
/* ------------------------------------------------------------
Browse through all affected supernodes snode[j], as long as
they're worth considering (i.e. ubsiz > tmpsiz).
------------------------------------------------------------ */
while((inz < ljc[k+1]) && (ubsiz > tmpsiz)){
j = lir[inz]; /* 1st affected column */
nextj = xsuper[snode[j] + 1]; /* beyond supernode of j */
if(i < nextj){
ncolup = mk; /* last affected supernode */
}
else{
ncolup = 1; /* Compute #affected cols in j */
for(++inz; lir[inz] < nextj; inz++)
ncolup++;
}
sizkj = mk * ncolup - ncolup*(ncolup-1)/2;
if(sizkj > tmpsiz)
tmpsiz = sizkj;
mk -= ncolup; /* proceed beyond snode[j] */
ubsiz = mk * (mk + 1) / 2; /* ubound on tmpsiz(k,[j+1,:]) */
} /* inz in column L(:,k) AND ubsiz > tmpsiz */
} /* for all supernodes ksup */
return tmpsiz;
}
/* ============================================================
MEXFUNCTION
============================================================ */
/* ************************************************************
PROCEDURE mexFunction - Entry for Matlab
************************************************************ */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
const mxArray *L_FIELD;
mwIndex i,j, nsuper,m, jsup,tmpsiz;
const mwIndex *ljc,*lir;
mwIndex *xsuper, *snode;
const double *xsuperPr;
/* ------------------------------------------------------------
Check for proper number of arguments
------------------------------------------------------------ */
mxAssert(nrhs >= NPARIN, "choltmpsiz requires more input arguments.");
mxAssert(nlhs <= NPAROUT, "choltmpsiz produces less output arguments.");
/* ------------------------------------------------------------
Disassemble block Cholesky structure L
------------------------------------------------------------ */
mxAssert(mxIsStruct(L_IN), "Parameter `L' should be a structure.");
L_FIELD = mxGetField(L_IN,(mwIndex)0,"L"); /* L.L */
mxAssert( L_FIELD != NULL, "Missing field L.L.");
m = mxGetM(L_FIELD);
mxAssert(m == mxGetN(L_FIELD), "L.L must be square.");
mxAssert(mxIsSparse(L_FIELD), "L.L should be sparse.");
ljc = mxGetJc(L_FIELD);
lir = 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);
/* ------------------------------------------------------------
Allocate working arrays:
------------------------------------------------------------ */
xsuper = (mwIndex *) mxCalloc(nsuper+1,sizeof(mwIndex));
snode = (mwIndex *) mxCalloc(m,sizeof(mwIndex));
/* ------------------------------------------------------------
Convert XSUPER to integer and C-Style
------------------------------------------------------------ */
for(i = 0; i <= nsuper; i++){
j = (mwIndex) xsuperPr[i];
mxAssert(j>0,"");
xsuper[i] = --j;
}
/* ------------------------------------------------------------
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;
}
/* ------------------------------------------------------------
The main job: compute (upper bound on) blkchol-tmpsiz.
------------------------------------------------------------ */
tmpsiz = gettmpsiz(ljc,lir,xsuper,nsuper, snode);
/* ------------------------------------------------------------
return OUTPUT variable tmpsiz
------------------------------------------------------------ */
TMPSIZ_OUT = mxCreateDoubleMatrix((mwSize)1,(mwSize)1,mxREAL); /* L.tmpsiz */
*mxGetPr(TMPSIZ_OUT) = (double) tmpsiz;
/* ------------------------------------------------------------
Release working arrays.
------------------------------------------------------------ */
mxFree(snode);
mxFree(xsuper);
}