-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdpa_exe.cpp
More file actions
430 lines (394 loc) · 13.1 KB
/
sdpa_exe.cpp
File metadata and controls
430 lines (394 loc) · 13.1 KB
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
/* -------------------------------------------------------------
This file is a component of SDPA
Copyright (C) 2004-2020 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
------------------------------------------------------------- */
#ifndef _MAIN_
#define _MAIN_
#endif
#include <sdpa_io.h>
#include <sdpa_call.h>
#if UseMETIS
#ifdef __cplusplus
extern "C" {
#endif
#include "metis.h"
#ifdef __cplusplus
}
#endif
#endif
using namespace sdpa;
#define USER_PARAMETER_FILE ((char *)"./param.sdpa")
#define DEFAULT_PARAMETER_FILE ((char *)"/usr/share/sdpa/param.sdpa")
// PARAMETER_FILE is decided by the following priority
// 1: The file assigned by '-p' option of 'option type 2'.
// For 'option type1', this is skipped.
// 2: USER_PARAMETER_FILE
// For 'option type2', this is skipped.
// 3: DEFAULT_PARAMETER_FILE
// 4: Default parameter
static void message(char* argv0)
{
cout << endl;
cout << "*** Please assign data file and output file.***" << endl;
cout << endl;
cout << "---- option type 1 ------------" << endl;
cout << argv0 <<" DataFile OutputFile [InitialPtFile]"
" [-pt parameters]"<< endl;
cout << "parameters = 0 default, 1 fast (unstable),"
" 2 slow (stable)" << endl;
cout << "example1-1: " << argv0
<< " example1.dat example1.result" << endl;
cout << "example1-2: " << argv0
<< " example1.dat-s example1.result" << endl;
cout << "example1-3: " << argv0
<< " example1.dat example1.result example1.ini" << endl;
cout << "example1-4: " << argv0
<< " example1.dat example1.result -pt 2" << endl;
cout << endl;
cout << "---- option type 2 ------------" << endl;
cout << argv0 << " [option filename]+ " << endl;
cout << " -dd : data dense :: -ds : data sparse " << endl;
cout << " -id : init dense :: -is : init sparse " << endl;
cout << " -o : output :: -p : parameter " << endl;
cout << " -pt : parameters , 0 default, 1 fast (unstable)" << endl;
cout << " 2 slow (stable) " << endl;
// cout << " -k : Kappa(RealValue)" << endl;
cout << "example2-1: " << argv0
<< " -o example1.result -dd example1.dat" << endl;
cout << "example2-2: " << argv0
<< " -ds example1.dat-s -o example2.result "
<< "-p param.sdpa" << endl;
cout << "example2-3: " << argv0
<< " -ds example1.dat-s -o example3.result "
<< "-pt 2" << endl;
exit(1);
}
static void argumentAnalysis(SDPA& Problem1,
int argc, char** argv,
char*& inputFileName, // comes from `-dd`, it's the `dataFile` in GMP
char*& resultFileName, // comes from `-o`, it's the `outFile` in GMP
char*& initFileName, // comes from `-id` or `-is`, it's the `initFile` in GMP
char*& paramFileName, // comes from `-p`, it's the `paraFile` in GMP
SDPA::SparseType& isInputSparse,
SDPA::SparseType& isInitSparse,
SDPA::ParameterType& parameterType,
bool& isDimacs, int& numThreads)
{
if (argc == 1) {
message(argv[0]);
}
if (strcmp(argv[1],"--version") == 0) {
fprintf(stdout,"====\n");
fprintf(stdout,"SDPA Multiprecision (SemiDefinite Programming Algorithm) %s\n",sdpa_version);
// fprintf(stdout," %s\n",sdpa_right);
fprintf(stdout,"====\n");
exit(0);
}
if (argv[1][0] == '-') {
// rsdpa argument
for (int index = 0; index < argc; ++index) {
char* target = argv[index];
if (strcmp(target,"-dd")==0 && index+1 < argc) {
inputFileName = argv[index+1];
isInputSparse = SDPA::DENSE;
index++;
continue;
}
if (strcmp(target,"-ds")==0 && index+1 < argc) {
inputFileName = argv[index+1];
isInputSparse = SDPA::SPARSE;
continue;
}
if (strcmp(target,"-id")==0 && index+1 < argc) {
initFileName = argv[index+1];
isInitSparse = SDPA::DENSE;
continue;
}
if (strcmp(target,"-is")==0 && index+1 < argc) {
initFileName = argv[index+1];
isInitSparse = SDPA::SPARSE;
index++;
continue;
}
if (strcmp(target,"-o")==0 && index+1 < argc) {
resultFileName = argv[index+1];
index++;
continue;
}
if (strcmp(target,"-p")==0 && index+1 < argc) {
paramFileName = argv[index+1];
index++;
continue;
}
if (strcmp(target,"-k")==0 && index+1 < argc) {
double KAPPA = atof(argv[index+1]);
Problem1.setKappa(KAPPA);
rMessage("Kappa = " << KAPPA);
index++;
continue;
}
if (strcmp(target,"-dimacs")==0) {
isDimacs = true;
continue;
}
if (strcmp(target,"-pt")==0 && index+1 < argc) {
int tmp = atoi(argv[index+1]);
switch (tmp) {
case 0:
parameterType = SDPA::PARAMETER_DEFAULT;
break;
case 1:
parameterType = SDPA::PARAMETER_UNSTABLE_BUT_FAST;
break;
case 2:
parameterType = SDPA::PARAMETER_STABLE_BUT_SLOW;
break;
default:
parameterType = SDPA::PARAMETER_DEFAULT;
}
index++;
paramFileName = NULL;
continue;
}
}
}
else { // SDPA argument
inputFileName = argv[1];
int len = strlen(inputFileName);
if (inputFileName[len-1] == 's'
&& inputFileName[len-2] == '-') {
isInputSparse = SDPA::SPARSE;
}
resultFileName = argv[2];
paramFileName = USER_PARAMETER_FILE;
for (int index=3; index<argc; ++index) {
if (strcmp(argv[index],"-pt")==0 && index+1 < argc) {
int tmp = atoi(argv[index+1]);
switch (tmp) {
case 0:
parameterType = SDPA::PARAMETER_DEFAULT;
break;
case 1:
parameterType = SDPA::PARAMETER_UNSTABLE_BUT_FAST;
break;
case 2:
parameterType = SDPA::PARAMETER_STABLE_BUT_SLOW;
break;
default:
parameterType = SDPA::PARAMETER_DEFAULT;
}
index++;
paramFileName = NULL;
} // end of "-pt"
else {
initFileName = argv[index];
int len = strlen(initFileName);
if (initFileName[len-1] == 's'
&& initFileName[len-2] == '-') {
isInitSparse = SDPA::SPARSE;
}
}
} // end of 'for'
}
if (paramFileName != NULL) {
// check the availability for paramFileName,
// usually USER_PARAMETER_FILE
FILE* fptmp = NULL;
if ((fptmp=fopen(paramFileName,"r"))==NULL) {
// we try DEFAULT_PARAMETER_FILE
if ((fptmp=fopen(DEFAULT_PARAMETER_FILE,"r"))==NULL) {
rMessage("Cannot Open user parameter File " << paramFileName
<< " and default parameter file " << DEFAULT_PARAMETER_FILE);
rMessage("Default parameter will be used.");
paramFileName = NULL;
}
else {
paramFileName = DEFAULT_PARAMETER_FILE;
}
}
} // end of 'if (paramFileName != NULL)'
if (inputFileName == NULL || resultFileName == NULL) {
message(argv[0]);
}
}
int main(int argc, char** argv)
{
TimeStart(ALL_START1);
/*
January 2024 (Usama Muneeb)
We handle all command line arguments later in `argumentAnalysis` function,
however the `-pt` argument (or `precision` in the parameter file) must be read
in advance. This is because `mpf_set_default_prec` must be called before an
SDPA object is instantiated (so that the precision of `mpf_class` objects in
SDPA class can be set according to it).
If using as a library, `mpf_set_default_prec` can be called before creating
an SDPA instance.
*/
if (argc == 1) {
message(argv[0]);
}
char* paramFileName = USER_PARAMETER_FILE;
for (int index = 0; index < argc; ++index) {
char* target = argv[index];
if (strcmp(target,"-p")==0 && index+1 < argc) {
paramFileName = argv[index+1];
index++;
continue;
}
if (strcmp(target,"-pt")==0 && index+1 < argc) {
int tmp = atoi(argv[index+1]);
// rMessage("mpf_set_default_prec() called with precision of -pt="<<tmp);
switch (tmp) {
case 0: mpf_set_default_prec(200); break; // DEFAULT
case 1: mpf_set_default_prec(100); break; // UNSTABLE_BUT_FAST
case 2: mpf_set_default_prec(300); break; // STABLE_BUT_SLOW
default: mpf_set_default_prec(200); // DEFAULT
index++;
}
/*
If `-pt` flag is set, any parameter file will be ignored.
Note: `-pt` flag is like a preset for all parameters (and not just
the precision). Other parameters will be set later by
`Parameter::setDefaultParameter` routine (which will be called later).
*/
paramFileName = NULL;
continue;
}
}
if (paramFileName != NULL) {
FILE* fptmp = NULL;
if ((fptmp=fopen(paramFileName,"r"))==NULL) {
// In case user specified file cannot be read try DEFAULT_PARAMETER_FILE
if ((fptmp=fopen(DEFAULT_PARAMETER_FILE,"r"))==NULL) {
/*
If DEFAULT_PARAMETER_FILE can also not be read, do not read any file
and use the default precision of 200
*/
paramFileName = NULL;
// rMessage("mpf_set_default_prec(200)");
mpf_set_default_prec(200);
}
else {
paramFileName = DEFAULT_PARAMETER_FILE;
}
}
}
/*
If paramFileName is "still" not NULL it means `-pt` was not set by user
AND some parameter file can be read (user specified or default)
*/
if (paramFileName != NULL) {
FILE* parameterFile = fopen(paramFileName,"r");
int precision;
double ignore;
for (int i=0; i < 10; ++i) {
// ignore `maxIteration` through `epsilonDash`
fscanf(parameterFile,"%d%*[^\n]",&ignore);
}
fscanf(parameterFile,"%d%*[^\n]",&precision);
fclose(parameterFile);
// rMessage("mpf_set_default_prec(" << precision << ")");
mpf_set_default_prec(precision);
}
/*
Completed in-advance processing of `-pt` parameter (or parameter file) for
setting `mpf_set_default_prec`.
*/
SDPA Problem1;
time_t ltime;
time(<ime);
char string_time[1024];
strcpy(string_time,ctime(<ime));
string_time[strlen(string_time)-1]='\0';
fprintf(stdout,"SDPA Multiprecision (Version %s) start at [%s]\n", sdpa_version, string_time);
// cout << "let me see your ..." << endl;
if (argc == 1) {
message(argv[0]);
}
char* inputFileName = NULL;
char* resultFileName = NULL;
char* initFileName = NULL;
paramFileName = NULL;
SDPA::SparseType isInputSparse = SDPA::DENSE;
SDPA::SparseType isInitSparse = SDPA::DENSE;
SDPA::ParameterType parameterType = SDPA::PARAMETER_DEFAULT;
bool isDimacs = false;
int numThreads = 0; // 0 means automatic computation
argumentAnalysis(Problem1, argc, argv,
inputFileName, resultFileName, initFileName,
paramFileName, isInputSparse, isInitSparse,
parameterType, isDimacs, numThreads);
Problem1.setDisplay(stdout);
FILE* fpresult;
if ((fpresult = fopen(resultFileName,"w")) == NULL) {
rError("Cannot Open Result File : " << resultFileName);
}
fprintf(fpresult,"SDPA Multiprecision start at [%s]\n",string_time);
Problem1.setResultFile(fpresult);
if (paramFileName == NULL) {
if (parameterType == SDPA::PARAMETER_DEFAULT) {
fprintf(stdout ,"set is DEFAULT\n");
fprintf(fpresult,"set is DEFAULT\n");
}
else if (parameterType == SDPA::PARAMETER_UNSTABLE_BUT_FAST) {
fprintf(stdout ,"set is UNSTABLE_BUT_FAST\n");
fprintf(fpresult,"set is UNSTABLE_BUT_FAST\n");
}
else if (parameterType == SDPA::PARAMETER_STABLE_BUT_SLOW) {
fprintf(stdout ,"set is STABLE_BUT_SLOW\n");
fprintf(fpresult,"set is STABLE_BUT_SLOW\n");
}
Problem1.setParameterType(parameterType);
}
if (paramFileName) {
fprintf(stdout ,"param is %s\n", paramFileName);
Problem1.readParameter(paramFileName,fpresult);
}
fprintf(stdout ,"data is %s", inputFileName);
if (isInputSparse == SDPA::SPARSE) {
fprintf(stdout ," : sparse\n");
}
else {
fprintf(stdout ," : dense\n");
}
Problem1.readInput(inputFileName, fpresult, isInputSparse);
if (initFileName) {
Problem1.setInitPoint(true);
fprintf(stdout ,"init is %s", initFileName);
if (isInitSparse == SDPA::SPARSE) {
fprintf(stdout ," : sparse\n");
}
else {
fprintf(stdout ," : dense\n");
}
Problem1.readInit(initFileName, fpresult, isInitSparse);
}
fprintf(stdout ,"out is %s\n", resultFileName);
fprintf(fpresult,"out is %s\n", resultFileName);
// Problem1.setNumThreads(numThreads); // not needed as we don't have multiple threads in GMP
Problem1.initializeSolve();
Problem1.solve();
Problem1.terminate();
time(<ime);
strcpy(string_time,ctime(<ime));
string_time[strlen(string_time)-1]='\0';
fprintf(stdout ,"SDPA end at [%s]\n",string_time);
fprintf(fpresult,"SDPA end at [%s]\n",string_time);
TimeEnd(ALL_END1);
double all_time = TimeCal(ALL_START1,ALL_END1);
fprintf(stdout ,"ALL TIME = %.6lf\n", all_time);
fprintf(fpresult,"ALL TIME = %.6lf\n", all_time);
fclose(fpresult);
return 0;
}