-
Notifications
You must be signed in to change notification settings - Fork 6
/
sdpa_chordal.cpp
464 lines (401 loc) · 12.8 KB
/
sdpa_chordal.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
/* -------------------------------------------------------------
This file is a component of SDPA-C
Copyright (C) 2004 SDPA Project
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
------------------------------------------------------------- */
#define UseMETIS 0
#define PrintSparsity 0
#define OrderOnlyByMDO 1
#include <sdpa_chordal.h>
namespace sdpa {
Chordal::Chordal() { initialize(); }
Chordal::~Chordal() {
//
}
void Chordal::initialize() {
// condition of sparse computation
// m_threshold < mDim,
// b_threshold < nBlock,
// aggregate_threshold >= aggrigated sparsity ratio
// extend_threshold >= extended sparsity ratio
m_threshold = 100;
b_threshold = 5;
aggregate_threshold = 0.25;
extend_threshold = 0.4;
#if 0 // DENSE computation for debugging
m_threshold = 10000000;
b_threshold = 1000000;
aggregate_threshold = 0.0;
extend_threshold = 0.0;
#endif
#if 0 // SPARSE computation for debugging
m_threshold = 0;
b_threshold = 0;
aggregate_threshold = 2.0;
extend_threshold = 2.0;
#endif
/* indicates the used ordering method */
/* 0: METIS 4.0.1 - nested dissection <--- not support */
/* 1: Spooles 2.2 - mininum degree */
/* 2: Spooles 2.2 - generalized nested dissection */
/* 3: Spooles 2.2 - multisection */
/* 4: Spooles 2.2 - better of 2 and 3 */
#if OrderOnlyByMDO
Method[0] = 0;
Method[1] = 1;
Method[2] = 0;
Method[3] = 0;
Method[4] = 0;
#else
Method[0] = 0;
Method[1] = 1;
Method[2] = 1;
Method[3] = 1;
Method[4] = 1;
#endif
best = -1;
}
void Chordal::terminate() {
if (Method[0]) {
rError("no support for METIS");
}
if (Method[1] > 1) {
IV_free(newToOldIV_MMD);
IVL_free(symbfacIVL_MMD);
}
if (Method[2] > 1) {
IV_free(newToOldIV_ND);
IVL_free(symbfacIVL_ND);
}
if (Method[3] > 1) {
IV_free(newToOldIV_MS);
IVL_free(symbfacIVL_MS);
}
if (Method[4] > 1) {
IV_free(newToOldIV_NDMS);
IVL_free(symbfacIVL_NDMS);
}
}
// marge array1 to array2
void Chordal::margeArray(int na1, int *array1, int na2, int *array2) {
int ptr = na1 + na2 - 1;
int ptr1 = na1 - 1;
int ptr2 = na2 - 1;
int idx1, idx2;
while ((ptr1 >= 0) || (ptr2 >= 0)) {
if (ptr1 >= 0) {
idx1 = array1[ptr1];
} else {
idx1 = -1;
}
if (ptr2 >= 0) {
idx2 = array2[ptr2];
} else {
idx2 = -1;
}
if (idx1 > idx2) {
array2[ptr] = idx1;
ptr1--;
} else {
array2[ptr] = idx2;
ptr2--;
}
ptr--;
}
// error check
if (ptr != -1) {
rMessage("Chordal::margeArray:: program bug");
}
}
// make aggrigate sparsity pattern
void Chordal::makeGraph(InputData &inputData, int m) {
int i, j, k, l;
int SDP_nBlock = inputData.SDP_nBlock;
int SOCP_nBlock = inputData.SOCP_nBlock;
int LP_nBlock = inputData.LP_nBlock;
int *counter;
counter = new int[m];
for (int i = 0; i < m; i++) {
counter[i] = 0;
}
// count maximum mumber of index
for (l = 0; l < SDP_nBlock; l++) {
int SDP_nConstraint = inputData.SDP_nConstraint[l];
for (k = 0; k < SDP_nConstraint; k++) {
i = inputData.SDP_constraint[l][k];
counter[i] += SDP_nConstraint;
}
}
for (l = 0; l < SOCP_nBlock; l++) {
int SOCP_nConstraint = inputData.SOCP_nConstraint[l];
for (k = 0; k < SOCP_nConstraint; k++) {
i = inputData.SOCP_constraint[l][k];
counter[i] += SOCP_nConstraint;
}
}
for (l = 0; l < LP_nBlock; l++) {
int LP_nConstraint = inputData.LP_nConstraint[l];
for (k = 0; k < LP_nConstraint; k++) {
i = inputData.LP_constraint[l][k];
counter[i] += LP_nConstraint;
}
}
// allocate temporaly workspace
int **tmp;
tmp = new int *[m];
for (i = 0; i < m; i++) {
tmp[i] = new int[counter[i]];
}
// merge index
for (int i = 0; i < m; i++) {
counter[i] = 0;
}
// marge index of for SDP
for (l = 0; l < SDP_nBlock; l++) {
for (k = 0; k < inputData.SDP_nConstraint[l]; k++) {
i = inputData.SDP_constraint[l][k];
margeArray(inputData.SDP_nConstraint[l], inputData.SDP_constraint[l], counter[i], tmp[i]);
counter[i] += inputData.SDP_nConstraint[l];
}
}
// marge index of for SOCP
for (l = 0; l < SOCP_nBlock; l++) {
for (k = 0; k < inputData.SOCP_nConstraint[l]; k++) {
i = inputData.SOCP_constraint[l][k];
margeArray(inputData.SOCP_nConstraint[l], inputData.SOCP_constraint[l], counter[i], tmp[i]);
counter[i] += inputData.SOCP_nConstraint[l];
}
}
// marge index of for LP
for (l = 0; l < LP_nBlock; l++) {
for (k = 0; k < inputData.LP_nConstraint[l]; k++) {
i = inputData.LP_constraint[l][k];
margeArray(inputData.LP_nConstraint[l], inputData.LP_constraint[l], counter[i], tmp[i]);
counter[i] += inputData.LP_nConstraint[l];
}
}
// construct adjacency list of SPOOLES
IVL_init1(adjIVL, IVL_CHUNKED, m);
int isize, previous;
int *ivec;
ivec = new int[m];
for (i = 0; i < m; i++) {
isize = 0;
previous = -1;
for (j = 0; j < counter[i]; j++) {
if (tmp[i][j] != previous) {
ivec[isize] = tmp[i][j];
previous = ivec[isize];
isize++;
}
}
IVL_setList(adjIVL, i, isize, ivec);
}
// constract graph of SPOOLES
Graph_init2(graph, 0, m, 0, IVL_tsize(adjIVL), m, IVL_tsize(adjIVL), adjIVL, NULL, NULL);
delete[] counter;
for (int i = 0; i < m; i++) {
delete[] tmp[i];
}
delete[] tmp;
delete[] ivec;
}
int Chordal::countNonZero(int m, IVL *symbfacIVL) {
int nonzeros = 0;
bool *bnode;
// count non-zero element
rNewCheck();
bnode = new bool[m];
if (bnode == NULL) {
rError("Newton::initialize_sparse_bMat memory exhausted ");
}
for (int i = 0; i < m; i++) {
bnode[i] = false;
}
int nClique = IVL_nlist(symbfacIVL);
int psize;
int *pivec;
for (int l = nClique - 1; l >= 0; l--) {
IVL_listAndSize(symbfacIVL, l, &psize, &pivec);
for (int i = 0; i < psize; i++) {
int ii = pivec[i];
if (bnode[ii] == false) {
nonzeros += psize - i;
bnode[ii] = true;
}
}
}
delete[] bnode;
return nonzeros;
}
int Chordal::Spooles_MMD(int m) {
int seed = 0, msglvl = 0;
FILE *fp = NULL;
// rMessage("orderViaMMD:start");
etree = orderViaMMD(graph, seed, msglvl, fp);
// rMessage("orderViaMMD:end");
newToOldIV_MMD = ETree_newToOldVtxPerm(etree);
symbfacIVL_MMD = SymbFac_initFromGraph(etree, graph);
// IVL_writeForHumanEye(symbfacIVL_MMD,stdout);
int nonzeros = countNonZero(m, symbfacIVL_MMD);
return nonzeros * 2 - m;
}
int Chordal::Spooles_NDMS(int m) {
int seed = 0, msglvl = 0;
FILE *fp = NULL;
int maxdomainsize = m / 16 + 1;
int maxzeros = m / 10 + 1;
int maxsize = 64;
// rMessage("orderViaMMD:start");
etree = orderViaBestOfNDandMS(graph, maxdomainsize, maxzeros, maxsize, seed, msglvl, fp);
// rMessage("orderViaMMD:end");
newToOldIV_NDMS = ETree_newToOldVtxPerm(etree);
symbfacIVL_NDMS = SymbFac_initFromGraph(etree, graph);
// IVL_writeForHumanEye(symbfacIVL_NDMS,stdout);
int nonzeros = countNonZero(m, symbfacIVL_NDMS);
return nonzeros * 2 - m;
}
int Chordal::Spooles_ND(int m) {
int seed = 0, msglvl = 0;
FILE *fp = NULL;
bool *bnode;
int maxdomainsize = m / 16 + 1;
// rMessage("orderViaMMD:start");
etree = orderViaND(graph, maxdomainsize, seed, msglvl, fp);
// rMessage("orderViaMMD:end");
newToOldIV_ND = ETree_newToOldVtxPerm(etree);
symbfacIVL_ND = SymbFac_initFromGraph(etree, graph);
// IVL_writeForHumanEye(symbfacIVL_ND,stdout);
int nonzeros = countNonZero(m, symbfacIVL_ND);
return nonzeros * 2 - m;
}
int Chordal::Spooles_MS(int m) {
int seed = 0, msglvl = 0;
FILE *fp = NULL;
bool *bnode;
int maxdomainsize = m / 16 + 1;
// rMessage("orderViaMMD:start");
etree = orderViaMS(graph, maxdomainsize, seed, msglvl, fp);
// rMessage("orderViaMMD:end");
newToOldIV_MS = ETree_newToOldVtxPerm(etree);
symbfacIVL_MS = SymbFac_initFromGraph(etree, graph);
// IVL_writeForHumanEye(symbfacIVL_MS,stdout);
int nonzeros = countNonZero(m, symbfacIVL_MS);
return nonzeros * 2 - m;
}
void Chordal::ordering_bMat(int m, int nBlock, InputData &inputData, FILE *fpOut) {
if ((m <= m_threshold) || (nBlock <= b_threshold)) {
best = -1;
return;
}
for (int b = 0; b < inputData.SDP_nBlock; b++) {
if (inputData.SDP_nConstraint[b] > m * sqrt(aggregate_threshold)) {
best = -1;
return;
}
}
for (int b = 0; b < inputData.SOCP_nBlock; b++) {
if (inputData.SOCP_nConstraint[b] > m * sqrt(aggregate_threshold)) {
best = -1;
return;
}
}
for (int b = 0; b < inputData.LP_nBlock; b++) {
if (inputData.LP_nConstraint[b] > m * sqrt(aggregate_threshold)) {
best = -1;
return;
}
}
adjIVL = IVL_new();
graph = Graph_new();
makeGraph(inputData, m);
if (IVL_tsize(adjIVL) > aggregate_threshold * m * m) {
best = -1;
Graph_free(graph);
return;
}
#if PrintSparsity
/* print sparsity information */
printf("dense matrix :\t\t\t%14d elements\n", m * m);
fprintf(fpOut, "dense matrix :\t\t\t%14d elements\n", m * m);
printf("aggregate sparsity pattern :\t\t\t%14d elements\n", IVL_tsize(adjIVL));
fprintf(fpOut, "aggregate sparsity pattern :\t\t\t%14d elements\n", IVL_tsize(adjIVL));
#endif
/* Uses METIS */
if (Method[0]) {
rError("no support for METIS");
}
/* Uses Spooles */
if (Method[1]) { /* Spooles 2.2 - minimum degree */
Method[1] = Spooles_MMD(m);
ETree_free(etree);
#if PrintSparsity
printf("\tSpooles2.2 (minimum degree)\t\t%14d elements\n", Method[1]);
fprintf(fpOut, "\tSpooles2.2 (minimum degree)\t\t%14d elements\n", Method[1]);
#endif
}
if (Method[2]) { /* Spooles 2.2 - generalized nested dissection */
Method[2] = Spooles_ND(m);
ETree_free(etree);
#if PrintSparsity
printf("\tSpooles2.2 (generalized nested dissection)%12d elements\n", Method[2]);
fprintf(fpOut, "\tSpooles2.2 (generalized nested dissection)%12d elements\n", Method[2]);
#endif
}
if (Method[3]) { /* Spooles 2.2 - multisection */
Method[3] = Spooles_MS(m);
ETree_free(etree);
#if PrintSparsity
printf("\tSpooles2.2 (multisection)\t\t%14d elements\n", Method[3]);
fprintf(fpOut, "\tSpooles2.2 (multisection)\t\t%14d elements\n", Method[3]);
#endif
}
if (Method[4]) { /* Spooles 2.2 - best between nested
dissection and multisection */
Method[4] = Spooles_NDMS(m);
ETree_free(etree);
#if PrintSparsity
printf("\tSpooles2.2 (best of ND and MS)\t\t%14d elements\n", Method[4]);
fprintf(fpOut, "\tSpooles2.2 (best of ND and MS)\t\t%14d elements\n", Method[4]);
#endif
}
/* Select the best ordering */
Graph_free(graph);
best = Best_Ordering(Method);
if (Method[best] > extend_threshold * m * m) {
best = -1;
}
}
int Chordal::Best_Ordering(int *Method)
/************************************************************************
Determine the best ordering.
************************************************************************/
{
int i, best;
for (i = 0; Method[i] == 0; i++)
;
best = i++;
while (i < 5) {
for (; i < 5; i++) {
if (Method[i] != 0)
break;
}
if (i < 5) {
if (Method[i] < Method[best])
best = i;
i++;
}
}
return best;
}
} // namespace sdpa