-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBCD_size3_mex_27_sparse.cpp
479 lines (478 loc) · 18.6 KB
/
CBCD_size3_mex_27_sparse.cpp
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <math.h>
#include "mex.h"
#include <time.h>
#define EPSILON 2.220446e-16
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//input args
//here the lower and upper bounds are set in the program
//not set by the input parameters
double *in_A;
mwIndex *irs;// for sparse matrix
mwIndex *jcs;// for sparse matrix
double *in_b;
int in_d;
int in_max_iter;
//ouput args
double *out_x;// the minimizer
//parameters in the function
int i,j,epoch;//loop
double residual,df;
//get input args
in_A = mxGetPr(prhs[0]);if(in_A==NULL){mexErrMsgTxt("pointer in_A is null"); return;}
irs = mxGetIr(prhs[0]);if(irs==NULL){mexErrMsgTxt("pointer irs is null"); return;}
jcs = mxGetJc(prhs[0]);if(jcs==NULL){mexErrMsgTxt("pointer jcs is null"); return;}
in_b = mxGetPr(prhs[1]);if(in_b==NULL){mexErrMsgTxt("pointer in_b is null"); return;}
in_d = mxGetScalar(prhs[2]);if(in_d==NULL){mexErrMsgTxt("pointer in_d is null"); return;}
in_max_iter = mxGetScalar(prhs[3]);if(in_max_iter==NULL){mexErrMsgTxt("pointer in_max_iter is null"); return;}
/* Non-Zero elements, is the value of last entry of jcs
* lengths of in_A and irs are both NZmax
* length of jcs is in_d + 1, and the last entry of jcs has value NZmax
*/
int NZmax = jcs[in_d];
mexPrintf("CBCD size 3.cpp...NZmax = %d.\n",NZmax);
//allocate output, and init as all 0s
plhs[0] = mxCreateDoubleMatrix(in_d,1,mxREAL);
out_x = mxGetPr(plhs[0]);if(out_x==NULL){mexErrMsgTxt("pointer out_x is null"); return;}
for (i=0;i<in_d;i++){
out_x[i] = 0;
}
//pre-allocate output of residual, length as max_iter
double* out_r=new double[in_max_iter]; if(out_r==NULL){mexErrMsgTxt("pointer out_r is null"); return;}
//allocate gradient, will delete later
double* grad=new double[in_d]; if(grad==NULL){mexErrMsgTxt("pointer grad is null"); return;}
/*allotace diagonal, for solving small block problem,
* diag_A0 is the main diagonal, diag_A1/2 are the one below(and above, as symmetric)
* although the length of diag_A1/2 is in_d-1/in_d-2, set them as in_d,
* the in_d th element will be 0 and not be used.
*/
double* diag_A0=new double[in_d]; if(diag_A0==NULL){mexErrMsgTxt("pointer diag_A0 is null"); return;}
double* diag_A1=new double[in_d]; if(diag_A1==NULL){mexErrMsgTxt("pointer diag_A1 is null"); return;}
double* diag_A2=new double[in_d]; if(diag_A2==NULL){mexErrMsgTxt("pointer diag_A2 is null"); return;}
/*grad and residual of init in one loop
*out_x is initialized as all 0s, so grad is 0 vector
*residual calculation is simplified
*in the loop the elements from the diagonal are also extracted
*/
residual = 0;
for (i=0;i<in_d;i++){
//for initial x=0, g[i]=0
grad[i]=0;
// i th residual
df = -in_b[i];
if (df<0){
residual += df*df;
}
// i th element in diagonal and below
diag_A0[i]=0;
diag_A1[i]=0;
diag_A2[i]=0;
for (j=jcs[i];j<jcs[i+1];j++){
if (irs[j]==i){
diag_A0[i]=in_A[j];
//mexPrintf("diag_A0[%d]=%.5f;\n",i,in_A[j]);
}
else if (irs[j]==i+1){
diag_A1[i]=in_A[j];
//mexPrintf("diag_A1[%d]=%.5f;\n",i,in_A[j]);
}
else if (irs[j]==i+2){
diag_A2[i]=in_A[j];
//mexPrintf("diag_A2[%d]=%.5f;\n",i,in_A[j]);
}
}
}
residual = sqrt(residual);
out_r[0] = residual;
mexPrintf("init: 0, residual=%.15f\n",residual);
epoch=1;
// parameters for size 3 matrix
double a11,a12,a13,a21,a22,a23,a31,a32,a33;
double b1,b2,b3,x1,x2,x3,detA;
bool FLAG;
//clock_t time0,time1,time2,time3,time4;////////time//////////
//double dt1=0; double dt2=0; double dt3=0; double dt4=0;////////time//////////
while ((residual>1E-13)&&(epoch<in_max_iter)){
for (i=0;i<in_d-2;i=i+3){
//calc temporal grad
//time0 = clock();////////time//////////
// three for loops to reuse current memory
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] -= in_A[j]*out_x[i];
}
for (j=jcs[i+1];j<jcs[i+2];j++){
grad[irs[j]] -= in_A[j]*out_x[i+1];
}
for (j=jcs[i+2];j<jcs[i+3];j++){
grad[irs[j]] -= in_A[j]*out_x[i+2];
}
//time1 = clock();////////time//////////
// update x(i)
// define size 3 block
a11=diag_A0[i ]; a12=diag_A1[i ]; a13=diag_A2[i ];
a21=diag_A1[i ]; a22=diag_A0[i+1]; a23=diag_A1[i+1];
a31=diag_A2[i ]; a32=diag_A1[i+1]; a33=diag_A0[i+2];
b1 =in_b[i] -grad[i];
b2 =in_b[i+1]-grad[i+1];
b3 =in_b[i+2]-grad[i+2];
// decission tree
FLAG = false;
//time2 = clock();
// first discuss three 0 or 1, 8 cases
if (b1<=0 && b2<=0 && b3<=0){
out_x[i]=0;
out_x[i+1]=0;
out_x[i+2]=0;
FLAG=true;
}
else if (a13>=b1 && a23>=b2 && a33<=b3){
out_x[i]=0;
out_x[i+1]=0;
out_x[i+2]=1;
FLAG=true;
}
else if (a12>=b1 && a22<=b2 && a32>=b3){
out_x[i]=0;
out_x[i+1]=1;
out_x[i+2]=0;
FLAG=true;
}
else if (a12+a13>=b1 && a22+a23<=b2 && a32+a33<=b3){
out_x[i]=0;
out_x[i+1]=1;
out_x[i+2]=1;
FLAG=true;
}
else if (a11<=b1 && a12>=b2 && a13>=b3){
out_x[i]=1;
out_x[i+1]=0;
out_x[i+2]=0;
FLAG=true;
}
else if (a11+a13<=b1 && a21+a23>=b2 && a31+a33<=b3){
out_x[i]=1;
out_x[i+1]=0;
out_x[i+2]=1;
FLAG=true;
}
else if (a11+a12<=b1 && a21+a22<=b2 && a31+a32>=b3){
out_x[i]=1;
out_x[i+1]=1;
out_x[i+2]=0;
FLAG=true;
}
else if (a11+a12+a13<=b1 && a21+a22+a23<=b2 && a31+a32+a33<=b3){
out_x[i]=1;
out_x[i+1]=1;
out_x[i+2]=1;
FLAG=true;
}
// second discuss two 0s and two 1s, 6 cases
if (FLAG==false){
x3 = b3/a33;
if (x3>=0 && x3<=1 && a13*x3>=b1 && a23*x3>=b2){
out_x[i]=0;
out_x[i+1]=0;
out_x[i+2]=x3;
FLAG=true;
}
if (FLAG==false){
x2 = b2/a22;
if (x2>=0 && x2<=1 && a12*x2>=b1 && a32*x2>=b3){
out_x[i]=0;
out_x[i+1]=x2;
out_x[i+2]=0;
FLAG=true;
}
if (FLAG==false){
x1 = b1/a11;
if (x1>=0 && x1<=1 && a21*x1>=b2 && a31*x1>=b3){
out_x[i]=x1;
out_x[i+1]=0;
out_x[i+2]=0;
FLAG=true;
}
if (FLAG==false){
x1=(b1-a12-a13)/a11;
if (x1>=0 && x1<=1 && a21*x1+a22+a23<=b2 && a31*x1+a32+a33<=b3){
out_x[i]=x1;
out_x[i+1]=1;
out_x[i+2]=1;
FLAG=true;
}
if (FLAG==false){
x2=(b2-a21-a23)/a22;
if (x2>=0 && x2<=1 && a11+a12*x2+a13<=b1 && a31+a32*x2+a33<=b3){
out_x[i]=1;
out_x[i+1]=x2;
out_x[i+2]=1;
FLAG=true;
}
if (FLAG==false){
x3=(b3-a31-a32)/a33;
if (x3>=0 && x3<=1 && a11+a12+a13*x3<=b1 && a21+a22+a23*x3<=b2){
out_x[i]=1;
out_x[i+1]=1;
out_x[i+2]=x3;
FLAG=true;
}
}
}
}
}
}
}
// third discuss one 0 and one 1
if (FLAG==false){
x1=(b1-a13)/a11;
if (x1>=0 && x1<=1 && a21*x1+a23>=b2 && a31*x1+a33<=b3){
out_x[i]=x1;
out_x[i+1]=0;
out_x[i+2]=1;
FLAG=true;
}
if (FLAG==false){
x1 = (b1-a12)/a11;
if (x1>=0 && x1<=1 && a21*x1+a22<=b2 && a31*x1+a32>=b3){
out_x[i]=x1;
out_x[i+1]=1;
out_x[i+2]=0;
FLAG=true;
}
if (FLAG==false){
x2 = (b2-a23)/a22;
if (x2>=0 && x2<=1 && a12*x2+a13>=b1 && a32*x2+a33<=b3){
out_x[i]=0;
out_x[i+1]=x2;
out_x[i+2]=1;
FLAG=true;
}
if (FLAG==false){
x2=(b2-a21)/a22;
if (x2>=0 && x2<=1 && a11+a12*x2<=b1 && a31+a32*x2>=b3){
out_x[i]=1;
out_x[i+1]=x2;
out_x[i+2]=0;
FLAG=true;
}
if (FLAG==false){
x3=(b3-a32)/a33;
if (x3>=0 && x3<=1 && a12+a13*x3>=b1 && a22+a23*x3<=b2){
out_x[i]=0;
out_x[i+1]=1;
out_x[i+2]=x3;
FLAG=true;
}
if (FLAG==false){
x3=(b3-a31)/a33;
if (x3>=0 && x3<=1 && a11+a13*x3<=b1 && a21+a23*x3>=b2){
out_x[i]=1;
out_x[i+1]=0;
out_x[i+2]=x3;
FLAG=true;
}
}
}
}
}
}
}
// fourth discuss one 1 or one 0
if (FLAG==false){
detA = a22*a33-a23*a32;
x2 = (a33*b2-a23*b3)/detA;
x3 = (a22*b3-a32*b2)/detA;
if (x2>=0 && x2<=1 && x3>=0 && x3<=1 && a12*x2+a13*x3>=b1){
out_x[i]=0;
out_x[i+1]=x2;
out_x[i+2]=x3;
FLAG=true;
}
if (FLAG==false){
x2 = (a33*(b2-a21)-a23*(b3-a31))/detA;
x3 = (a22*(b3-a31)-a32*(b2-a21))/detA;
if (x2>=0 && x2<=1 && x3>=0 && x3<=1 && a11+a12*x2+a13*x3<=b1){
out_x[i]=1;
out_x[i+1]=x2;
out_x[i+2]=x3;
FLAG=true;
}
if (FLAG==false){
detA = a11*a33-a13*a31;
x1 = (a33*b1-a13*b3)/detA;
x3 = (a11*b3-a31*b1)/detA;
if (x1>=0 && x1<=1 && x3>=0 && x3<=1 && a21*x1+a23*x3>=b2){
out_x[i]=x1;
out_x[i+1]=0;
out_x[i+2]=x3;
FLAG=true;
}
if (FLAG==false){
x1 = (a33*(b1-a12)-a13*(b3-a32))/detA;
x3 = (a11*(b3-a32)-a31*(b1-a12))/detA;
if (x1>=0 && x1<=1 && x3>=0 && x3<=1 && a21*x1+a22+a23*x3<=b2){
out_x[i]=x1;
out_x[i+1]=1;
out_x[i+2]=x3;
FLAG=true;
}
if (FLAG==false){
detA = a11*a22-a12*a21;
x1 = (a22*b1-a12*b2)/detA;
x2 = (a11*b2-a21*b1)/detA;
if (x1>=0 && x1<=1 && x2>=0 && x2<=1 && a31*x1+a32*x2>=b3){
out_x[i]=x1;
out_x[i+1]=x2;
out_x[i+2]=0;
FLAG=true;
}
if (FLAG==false){
x1 = (a22*(b1-a13)-a12*(b2-a23))/detA;
x2 = (a11*(b2-a23)-a21*(b1-a13))/detA;
if (x1>=0 && x1<=1 && x2>=0 && x2<=1 && a31*x1+a32*x2+a33<=b3){
out_x[i]=x1;
out_x[i+1]=x2;
out_x[i+2]=1;
FLAG=true;
}
}
}
}
}
}
}
// last case, no 0 or 1
if (FLAG==false){
// solve 3 dim linear system
detA = a11*a22*a33+a21*a32*a13+a31*a12*a23-a11*a32*a23-a22*a13*a31-a33*a12*a21;
x1 = ( (a22*a33-a32*a23)*b1-(a12*a33-a32*a13)*b2+(a12*a23-a22*a13)*b3)/detA;
x2 = (-(a21*a33-a31*a23)*b1+(a11*a33-a31*a13)*b2-(a11*a23-a21*a13)*b3)/detA;
x3 = ( (a21*a32-a31*a22)*b1-(a11*a32-a31*a12)*b2+(a11*a22-a21*a12)*b3)/detA;
if (x1>=0 && x1<=1 && x2>=0 && x2<=1 && x3>=0 && x3<=1){
out_x[i]=x1;
out_x[i+1]=x2;
out_x[i+2]=x3;
FLAG=true;
}
else {
mexPrintf("no update, check code\n");
}
}
//update temporal grad
//time2 = clock();////////time//////////
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] += in_A[j]*out_x[i];
}
for (j=jcs[i+1];j<jcs[i+2];j++){
grad[irs[j]] += in_A[j]*out_x[i+1];
}
for (j=jcs[i+2];j<jcs[i+3];j++){
grad[irs[j]] += in_A[j]*out_x[i+2];
}
/*time3 = clock();
dt1+=(double)(time1-time0);///((clock_t)1000);
dt2+=(double)(time2-time1);
dt3+=(double)(time3-time2);
dt4+=(double)(time3-time0);*/
}
if (in_d%3==1){
i=in_d-1;
//calc temporal grad
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] -= in_A[j]*out_x[i];
}
//descent
out_x[i] = (in_b[i]-grad[i])/diag_A0[i];
//bounds
if (out_x[i]>1){
out_x[i] = 1;
}
if (out_x[i]<0){
out_x[i] = 0;
}
//update temporal grad
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] += in_A[j]*out_x[i];
}
}
else if (in_d%3==2){
i=in_d-2;
//calc temporal grad
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] -= in_A[j]*out_x[i];
}
//descent
out_x[i] = (in_b[i]-grad[i])/diag_A0[i];
//bounds
if (out_x[i]>1){
out_x[i] = 1;
}
if (out_x[i]<0){
out_x[i] = 0;
}
//update temporal grad
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] += in_A[j]*out_x[i];
}
i++;
//calc temporal grad
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] -= in_A[j]*out_x[i];
}
//descent
out_x[i] = (in_b[i]-grad[i])/diag_A0[i];
//bounds
if (out_x[i]>1){
out_x[i] = 1;
}
if (out_x[i]<0){
out_x[i] = 0;
}
//update temporal grad, actually not needed
for (j=jcs[i];j<jcs[i+1];j++){
grad[irs[j]] += in_A[j]*out_x[i];
}
}
//init gradient as 0s
for (i=0;i<in_d;i++){
grad[i]=0;
}
//update true gradient
for (j=0;j<in_d;j++){
for (i=jcs[j];i<jcs[j+1];i++){
grad[irs[i]] += in_A[i]*out_x[j];
}
}
//get the residual
residual = 0;
for (i=0;i<in_d;i++){
// i th residual
df = grad[i]-in_b[i];
if (out_x[i]<=0+2*EPSILON){
if (df<0){
residual += df*df;
}
}
else if (out_x[i]>=1-2*EPSILON){
if (df>0){
residual += df*df;
}
}
else {
residual += df*df;
}
}
residual = sqrt(residual);
out_r[epoch] = residual;
//mexPrintf("epoch:%5d, residual=%.15f\n",epoch,residual);
epoch++;
}
plhs[1] = mxCreateDoubleMatrix(epoch,1,mxREAL);
double *r = mxGetPr(plhs[1]);if(r==NULL){mexErrMsgTxt("pointer r is null"); return;}
for (i=0;i<epoch;i++){
r[i]=out_r[i];
}
delete grad; delete out_r; delete diag_A0; delete diag_A1; delete diag_A2;
//mexPrintf("dt1 = %.5f, dt2 = %.5f, dt3 = %.5f, dt4 = %.5f\n",dt1,dt2,dt3,dt4);
mexPrintf("epoch:%5d, residual=%.15f\nEnd of CBCD size 3.cpp\n",epoch-1,residual);
}