-
Notifications
You must be signed in to change notification settings - Fork 90
/
ddot.c
308 lines (299 loc) · 13 KB
/
ddot.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
/*
% ddotX = ddot(d,X,blkstart [, Xblkjc])
% DDOT Given N x m matrix X, creates (blkstart(end)-blkstart(1)) x m matrix
% ddotX, having entries d[i]'* xj[i] for each (Lorentz norm bound) block
% blkstart(i):blkstart(i+1)-1. If X is sparse, then Xblkjc(:,2:3) should
% point to first and 1-beyond-last nonzero in blkstart range for each column.
%
% SEE ALSO sedumi, partitA.
% ********** INTERNAL FUNCTION OF SEDUMI **********
function ddotX = ddot(d,X,blkstart, Xblkjc)
% 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 DDOTX_OUT plhs[0]
#define NPAROUT 1
#define D_IN prhs[0]
#define X_IN prhs[1]
#define BLKSTART_IN prhs[2]
#define NPARINMIN 3
#define XBLKJC_IN prhs[3]
#define NPARIN 4
/* ************************************************************
PROCEDURE ddotxj -Compute y[k]= d[k]'*xpr[k] for each lorentz block k.
INPUT
d - qDim scaling vector with qDim := blkstart[nblk]-blkstart[0].
xpr - qDim data vector.
blkstart - length nblk+1 array, listing 1st subscript per block.
NOTE: should have blkstart[0] == 0.
nblk - Number of blocks.
OUTPUT
ypr - nblk vector. Gives d[k]'*xj[k] for each block.
************************************************************ */
void ddotxj(double *ypr, const double *d, const double *xpr,
const mwIndex *blkstart, const mwIndex nblk)
{
mwIndex k;
mxAssert(blkstart[0] == 0,"");
for(k = 0; k < nblk; k++)
ypr[k] = realdot(d+blkstart[k],xpr+blkstart[k], blkstart[k+1]-blkstart[k]);
}
/* ************************************************************
PROCEDURE spddotxj - Compute y[k] = d_k'*xj_k for each nonzero
block in xj.
INPUT
d - qDim scaling vector with qDim := blkstart[nblk]-blkstart[0].
xir, xpr - sparse matrix. We compute d[k]'*xj[k] for each (lorentz) block
where the column xj has nonzeros.
xjc0, xjc1 - Length m arrays, subscripts of column j in blkstart-range
are between xjc0(j) and xjc1(j).
blkstart - length nblk+1 array. Lorentz block k has subscripts
blkstart[k]:blkstart[k+1]-1.
xblk - length qDim array, with k = xblk(i-blkstart[0]) iff
blkstart[k] <= i < blkstart[k+1], k=0:nblk-1.
OUTPUT
y - sparse nblk x m matrix, with y.jc[m] <= sum(xjc1-xjc0).
y(k,j) = d[k]'*xj[k]
************************************************************ */
void spddotxj(jcir y, const double *d,
const mwIndex *xir, const double *xpr, const mwIndex *xjc0,
const mwIndex *xjc1, const mwIndex *xblk, const mwIndex *blkstart,
const mwIndex nblk, const mwIndex m)
{
mwIndex knz, nexti, inz, i, j, k, lend;
double yk;
/* ------------------------------------------------------------
INIT: Let blkstart[0] point to 1st nonzero in d and xblk, and
let knz poin to 1st available entry in y.
Let lend := blkstart[nblk] be 1 beyond valid subscripts.
------------------------------------------------------------ */
d -= blkstart[0]; /* Make d=d(blkstart[0]:blkstart[lorN]) */
xblk -= blkstart[0];
knz = 0;
lend = blkstart[nblk];
for(j = 0; j < m; j++){
y.jc[j] = knz;
/* ------------------------------------------------------------
Process column only if nonzero subscripts in blkstart[0:nblk].
------------------------------------------------------------ */
if((inz = xjc0[j]) < xjc1[j])
if( (i = xir[inz]) < lend){
/* ------------------------------------------------------------
Open initial block k; current block has subscripts smaller than nexti.
Accumulate yk = ddotxj[k].
------------------------------------------------------------ */
k = xblk[i];
nexti = blkstart[k + 1];
yk = d[i] * xpr[inz];
/* ------------------------------------------------------------
Browse through nonzeros in xj
------------------------------------------------------------ */
for(++inz; inz < xjc1[j]; inz++)
if( (i = xir[inz]) < nexti)
yk += d[i] * xpr[inz];
else if(i < lend){
/* ------------------------------------------------------------
If we finished the previous nonzero Lorentz block, then write entry,
and initialize new block.
------------------------------------------------------------ */
y.ir[knz] = k; /* yir lists Lorentz blocks */
y.pr[knz++] = yk;
k = xblk[i]; /* init new Lorentz block */
nexti = blkstart[k + 1];
yk = d[i] * xpr[inz];
}
else /* finished with all Lorentz blocks */
break;
/* ------------------------------------------------------------
Write last yk = ddotxj[k] entry into y(:,j).
------------------------------------------------------------ */
y.ir[knz] = k; /* yir lists Lorentz blocks */
y.pr[knz++] = yk;
} /* If column j has valid nonzeros */
} /* j=0:m-1 */
/* ------------------------------------------------------------
Close last column of y
------------------------------------------------------------ */
y.jc[m] = knz;
}
/* ============================================================
MEXFUNCTION
============================================================ */
/* ************************************************************
PROCEDURE mexFunction - Entry for Matlab
************************************************************ */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mwIndex i, j, k, m, nrows, maxnnz, nblk, qDim;
const double *d, *XjcPr, *blkstartPr;
mwIndex *xjc1, *xblk, *blkstart;
jcir X, ddotx;
/* ------------------------------------------------------------
Check for proper number of arguments
------------------------------------------------------------ */
mxAssert(nrhs >= NPARINMIN, "ddot requires more input arguments.");
mxAssert(nlhs <= NPAROUT, "ddot generates less output arguments.");
/* ------------------------------------------------------------
Get INPUTS d, X, blkstart.
------------------------------------------------------------ */
d = mxGetPr(D_IN);
qDim = mxGetM(D_IN) * mxGetN(D_IN);
nrows = mxGetM(X_IN);
m = mxGetN(X_IN);
X.pr = mxGetPr(X_IN);
blkstartPr = mxGetPr(BLKSTART_IN);
nblk = mxGetM(BLKSTART_IN) * mxGetN(BLKSTART_IN) - 1;
mxAssert(nblk >= 0, "blkstart size mismatch.");
/* ------------------------------------------------------------
Allocate mwIndex working array blkstart(nblk+1).
------------------------------------------------------------ */
blkstart = (mwIndex *) mxCalloc(nblk + 1, sizeof(mwIndex));
/* ------------------------------------------------------------
Convert Fortran double to C mwIndex
------------------------------------------------------------ */
for(i = 0; i <= nblk; i++){
j = (mwIndex) blkstartPr[i]; /* double to mwIndex */
mxAssert(j>0,"");
blkstart[i] = --j;
}
if(qDim != blkstart[nblk] - blkstart[0]){
mxAssert(qDim >= blkstart[nblk], "d size mismatch.");
d += blkstart[0]; /* Point to Lorentz norm-bound */
qDim = blkstart[nblk] - blkstart[0];
}
/* ------------------------------------------------------------
CASE THAT X IS FULL:
------------------------------------------------------------ */
if(!mxIsSparse(X_IN)){
if(nrows != qDim) {
if(nrows < blkstart[nblk]){
mxAssert(nrows == nblk + qDim, "X size mismatch");
X.pr += nblk; /* Lorentz tr + norm bound */
}
else { /* LP, Lorentz, PSD */
X.pr += blkstart[0]; /* Point to Lorentz norm-bound */
}
}
/* ------------------------------------------------------------
DDOTX is full nblk x m.
------------------------------------------------------------ */
DDOTX_OUT = mxCreateDoubleMatrix(nblk, m, mxREAL);
ddotx.pr = mxGetPr(DDOTX_OUT);
/* ------------------------------------------------------------
Let blkstart -= blkstart[0], so that blkstart[0] = 0.
------------------------------------------------------------ */
j = blkstart[0];
for(i = 0; i <= nblk; i++)
blkstart[i] -= j;
/* ------------------------------------------------------------
Compute d[k]'*x[k,i] for all Lorentz blocks k.
------------------------------------------------------------ */
for(i = 0; i < m; i++){
ddotxj(ddotx.pr, d, X.pr, blkstart, nblk);
ddotx.pr += nblk;
X.pr += nrows; /* to next column */
}
}
else{
/* ------------------------------------------------------------
The CASE that X is SPARSE:
------------------------------------------------------------ */
mxAssert(nrows >= blkstart[nblk], "X size mismatch");
X.jc = mxGetJc(X_IN);
X.ir = mxGetIr(X_IN);
/* ------------------------------------------------------------
Get XqjcPr, pointing to start of Lorentz blocks in X.
------------------------------------------------------------ */
mxAssert(nrhs >= NPARIN, "ddot with sparse X requires more input arguments.");
mxAssert(mxGetM(XBLKJC_IN) == m && mxGetN(XBLKJC_IN) >= 3, "Xjc size mismatch");
XjcPr = mxGetPr(XBLKJC_IN) + m; /* Point to Xjc(:,2) */
/* ------------------------------------------------------------
Allocate working arrays:
mwIndex xjc1(2*m), xblk(qDim).
------------------------------------------------------------ */
xjc1 = (mwIndex *) mxCalloc(MAX(2*m,1), sizeof(mwIndex) );
xblk = (mwIndex *) mxCalloc(MAX(qDim,1), sizeof(mwIndex) );
/* ------------------------------------------------------------
Convert double to mwIndex:
------------------------------------------------------------ */
for(i = 0; i < 2*m; i++)
xjc1[i] = (mwIndex) XjcPr[i]; /* double to mwIndex */
/* ------------------------------------------------------------
Let k = xblk(j-blkstart[0]) iff
blkstart[k] <= j < blkstart[k+1], k=0:nblk-1.
------------------------------------------------------------ */
j = 0;
for(k = 0; k < nblk; k++){
i = blkstart[k+1] - blkstart[0];
while(j < i)
xblk[j++] = k;
}
/* ------------------------------------------------------------
Let maxnnz := sum(xjc1(:,2)-xjc1(:,1)).
Create sparse output ddotX(nblk,m,maxnnz)
------------------------------------------------------------ */
maxnnz = 0;
for(i = 0; i < m; i++)
maxnnz += xjc1[m+i] - xjc1[i];
maxnnz = MAX(1, maxnnz);
DDOTX_OUT = mxCreateSparse(nblk,m, maxnnz,mxREAL);
ddotx.jc = mxGetJc(DDOTX_OUT);
ddotx.ir = mxGetIr(DDOTX_OUT);
ddotx.pr = mxGetPr(DDOTX_OUT);
/* ------------------------------------------------------------
The real job:
------------------------------------------------------------ */
spddotxj(ddotx, d, X.ir, X.pr,xjc1,xjc1+m, xblk,blkstart,nblk,m);
/* ------------------------------------------------------------
REALLOC (shrink) ddotx to ddotx.jc[m] nonzeros.
------------------------------------------------------------ */
maxnnz = MAX(1,ddotx.jc[m]);
if((ddotx.ir = (mwIndex *) mxRealloc(ddotx.ir, maxnnz * sizeof(mwIndex))) == NULL)
mexErrMsgTxt("Memory allocation error");
if((ddotx.pr = (double *) mxRealloc(ddotx.pr, maxnnz*sizeof(double)))
== NULL)
mexErrMsgTxt("Memory allocation error");
mxSetPr(DDOTX_OUT,ddotx.pr);
mxSetIr(DDOTX_OUT,ddotx.ir);
mxSetNzmax(DDOTX_OUT,maxnnz);
/* ------------------------------------------------------------
Release working arrays (SPARSE PART).
------------------------------------------------------------ */
mxFree(xjc1);
mxFree(xblk);
}
/* ------------------------------------------------------------
Release common working arrays.
------------------------------------------------------------ */
mxFree(blkstart);
}