-
Notifications
You must be signed in to change notification settings - Fork 91
/
install_sedumi.m
360 lines (344 loc) · 11.7 KB
/
install_sedumi.m
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
function install_sedumi( varargin )
%SeDuMi installation script
%
% install_sedumi
%
% Basic usage.
%
% install_sedumi -nopath
%
% Do not add SeDuMi directories to the search path.
%
% install_sedumi -rebuild
%
% Rebuild binary mex-files SeDuMi.
%
% install_sedumi -rebuild 'mex -O -largeArrayDims %s -lmwblas'
%
% Rebuild binary mex-files SeDuMi using that mex command template
% including ' %s ' as placeholder for input files.
%
%
% Heavy rewrite by Michael C. Grant for SeDuMi 1.3.4
% Now detects if binaries are already
%
% 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
need_rebuild = any (strcmp (varargin, '-rebuild'));
no_path = any (strcmp (varargin, '-nopath'));
mex_template = strncmp (varargin, 'mex ', 4);
if (any (mex_template))
mex_template = varargin{mex_template};
% Minimal validation.
% "contains" would be better, but introduced in Matlab R2016b.
if (isempty (strfind (mex_template, ' %s '))) %#ok<STREMP>
error (['The mex compilation command must contain '' %s '' as ', ...
'placeholder for the source files.']);
end
else
mex_template = '';
end
targets64 = {...
'bwblkslv.c sdmauxFill.c sdmauxRdot.c',...
'choltmpsiz.c',...
'cholsplit.c',...
['dpr1fact.c auxfwdpr1.c sdmauxCone.c sdmauxCmp.c sdmauxFill.c', ...
' sdmauxScalarmul.c sdmauxRdot.c blkaux.c'],...
'symfctmex.c symfct.c',...
'ordmmdmex.c ordmmd.c',...
'quadadd.c',...
'sqrtinv.c sdmauxCone.c',...
'givensrot.c auxgivens.c sdmauxCone.c',...
'urotorder.c auxgivens.c sdmauxCone.c sdmauxTriu.c sdmauxRdot.c',...
['psdframeit.c reflect.c sdmauxCone.c sdmauxRdot.c sdmauxTriu.c', ...
' sdmauxScalarmul.c'],...
['psdinvjmul.c reflect.c sdmauxCone.c sdmauxRdot.c sdmauxTriu.c', ...
' sdmauxScalarmul.c blkaux.c'],...
'bwdpr1.c sdmauxCone.c sdmauxRdot.c',...
'fwdpr1.c auxfwdpr1.c sdmauxCone.c sdmauxScalarmul.c',...
'fwblkslv.c sdmauxScalarmul.c',...
'qblkmul.c sdmauxScalarmul.c',...
'blkchol.c blkchol2.c sdmauxFill.c sdmauxScalarmul.c',...
'vecsym.c sdmauxCone.c',...
'qrK.c sdmauxCone.c sdmauxRdot.c sdmauxScalarmul.c',...
'finsymbden.c sdmauxCmp.c',...
'symbfwblk.c',...
'ddot.c sdmauxCone.c sdmauxRdot.c sdmauxScalarmul.c',...
'partitA.c sdmauxCmp.c',...
'getada1.c sdmauxFill.c',...
'getada2.c sdmauxCone.c sdmauxRdot.c sdmauxFill.c',...
['getada3.c spscale.c sdmauxCone.c sdmauxRdot.c sdmauxScalarmul.c', ...
' sdmauxCmp.c'],...
'adendotd.c sdmauxCone.c',...
'adenscale.c',...
'extractA.c',...
'sortnnz.c sdmauxCmp.c',...
'iswnbr.c',...
'incorder.c',...
'findblks.c sdmauxCone.c sdmauxCmp.c',...
['invcholfac.c triuaux.c sdmauxCone.c sdmauxRdot.c sdmauxTriu.c', ...
' sdmauxScalarmul.c blkaux.c'],...
};
% Get first file of "targets64" and replace ".c" with mex-extension.
% Keep order of "targets64".
mext = mexext;
mex_binaries = cellfun (@strtok, targets64, 'UniformOutput', false);
mex_binaries = cellfun (@(x) strrep (x, '.c', ['.', mext]), ...
mex_binaries, 'UniformOutput', false);
fs = filesep();
sedumi_path = mfilename ('fullpath');
sedumi_path = sedumi_path(1:max ([1, strfind(sedumi_path, fs)]) - 1);
% Note the use of 0.01 here. That's because version 7 had more than 10
% minor releases, so 7.10-7.14 need to be ordered after 7.01-7.09.
VERSION = [1, 0.01] * sscanf (version (), '%d.%d');
COMPUTER = computer ();
ISOCTAVE = (exist ('OCTAVE_VERSION', 'builtin') == 5);
mdir = '';
if (ISOCTAVE)
prog = 'Octave';
page_output_immediately (true, 'local');
switch COMPUTER
case 'x86_64-pc-linux-gnu'
mdir = 'o_lin';
case 'x86_64-apple-darwin21.6.0'
mdir = 'o_maci';
case 'aarch64-apple-darwin23.4.0'
mdir = 'o_maca';
case 'i686-w64-mingw32'
mdir = 'o_win';
case 'x86_64-w64-mingw32'
mdir = 'o_win';
otherwise
error(sprintf('Unexpected computer type: %s', COMPUTER));
end
else
prog = 'Matlab';
end
line_sep = repmat ('-', 1, 75);
fprintf ('\n%s\nSeDuMi installation script\n', line_sep);
fprintf (' Directory: %s\n', sedumi_path);
fprintf (' %s %s on %s\n', prog, version (), computer ());
disp (line_sep);
% We don't want to rebuild the binaries if they're already present, unless
% the user has specifically asked for a rebuild. Frankly, we can't
% guarantee that rebuilding will work.
if (~need_rebuild)
fprintf ('Looking for existing binaries...');
nfound = [0, 0];
for k = 1 : length(targets64)
targ = mex_binaries{k};
if exist( [ sedumi_path, fs, targ ], 'file' )
nfound(1) = nfound(1) + 1;
elseif ~isempty(mdir) && exist( [ sedumi_path, fs, mdir, fs, targ ], 'file' )
nfound(2) = nfound(2) + 1;
end
end
if sum(nfound) == 0
fprintf ('none found; building...\n');
need_rebuild = true;
elseif sum(nfound) < length(targets64)
fprintf ('incomplete set found.\n');
disp (line_sep);
error (['%s\n', ...
'Some of the binaries for this platform were found, but some\n', ...
'were missing as well. This may mean your download was corrupt;\n', ...
'consider downloading and unpacking SeDuMi again. Otherwise, to\n', ...
'try rebuilding the MEX files yourself, run this command:\n\n', ...
' install_sedumi -rebuild\n%s\n\n'], line_sep, line_sep);
else
fprintf ('found!\n');
fprintf (' If for some reason you need to rebuild the binaries,');
fprintf (' use this command:\n');
fprintf (' install_sedumi -rebuild\n');
end
end
if (need_rebuild)
mdir = '';
disp ('Attempting to recompile the SeDuMi binaries:');
% Customization by providing a mex template.
flags = {};
libs = {};
if ~isempty(mex_template)
found = false;
[cmd, remain] = strtok(mex_template);
while ~isempty(remain)
[tok, remain] = strtok(remain);
if found
libs{end+1} = tok;
elseif tok == '%s'
found = true;
else
flags{end+1} = tok;
end
end
elseif (ISOCTAVE)
% Matlab mex optimization '-O' corresponds to gcc '-O2',
% whereas calling '-O' would result in gcc '-O1'.
cmd = 'mkoctfile';
flags{end+1} = '--mex';
flags{end+1} = '-O2';
flags{end+1} = '-DOCTAVE';
flags{end+1} = '-Wall';
if (ismac ())
% Assume Homebrew (https://brew.sh/) installation.
% https://stackoverflow.com/questions/50634727/dyld-library-not-loaded-usr-local-opt-openblas-lib-libopenblasp-r0-2-20-dylib
% this glob covers /opt/homebrew and /usr/local installations
homebrew = glob('/*/*/Cellar/openblas/*/include');
flags{end+1} = ['-I', homebrew{1}];
libs{end+1} = ['-L', strrep(homebrew{1}, '/include', '/lib'), ' -lopenblas'];
elseif (ispc ())
libs{end+1} = '-lopenblas';
else
% Including the default OpenBLAS path works for most Octave
% installations and does not harm if not present.
flags{end+1} = '-I/usr/include/openblas';
end
else % Matlab
cmd = 'mex';
% The last Matlab release with a 32 bit version was R2015b.
IS64BIT = strcmp (COMPUTER(end-1:end), '64');
flags{end+1} = '-O'; % optimize
if (IS64BIT && (VERSION >= 7.03)) % R2006b
flags{end+1} = '-largeArrayDims';
elseif (VERSION < 7.03) % R2006b
flags{end+1} = '-DmwIndex=int';
flags{end+1} = '-DmwSize=int';
flags{end+1} = '-DmwSignedIndex=int';
flags{end+1} = '-DFWRAPPER';
end
if ((VERSION >= 7) && ispc ()) % R14 (2004)
if IS64BIT
dirval = 'win64';
else
dirval = 'win32';
end
libdir = fullfile (matlabroot (), 'extern', 'lib', dirval);
if exist (fullfile (libdir, 'microsoft'), 'dir')
libdir = fullfile (libdir, 'microsoft');
found = true;
elseif exist (fullfile (libdir, 'msvc60'), 'dir')
libdir = fullfile (libdir, 'msvc60');
found = true;
elseif exist (fullfile (libdir, 'lcc'), 'dir')
libdir = fullfile (libdir, 'lcc');
found = true;
end
if found
libs{end+1} = ['-L''', strrep(libdir, '\', '\\'), ''''];
end
end
if (VERSION >= 7.05) % R2007b
libs{end+1} = '-lmwblas';
else
libs{end+1} = '-lmwlapack';
end
end
mex_template = sprintf(' %s', cmd, flags{:}, '%s', libs{:});
fprintf ('Template:%s\n', mex_template);
% Move to SeDuMi root directory and compile.
failed = false;
olddir = cd (sedumi_path);
for i = 1:length(targets64)
files = {};
remain = targets64{i};
while ~isempty(remain),
[fname, remain] = strtok(remain);
files{end+1} = fname;
end
fprintf(' %s: %s\n', mex_binaries{i}, targets64{i});
try
if ISOCTAVE
mkoctfile(flags{:}, files{:}, libs{:});
else
mex(flags{:}, files{:}, libs{:})
end
catch
failed = true;
end
end
cd (olddir);
if failed
error (['%s\nSeDuMi was not successfully installed.\n', ...
'Please attempt to correct the errors and try again.\n\n', ...
'For example, try setting the compilation command:\n\n', ...
' install_sedumi -rebuild ''%s'''], line_sep, mex_template);
end
elseif (~isempty(mdir) && ~exist(fullfile(sedumi_path, mdir)))
mdir = '';
end
% Add SeDuMi to search path, if not forbidden by "-nopath".
if (~no_path)
disp (line_sep);
disp ('Adding SeDuMi to the search path:');
search_path_cstr = regexp (path (), pathsep (), 'split');
if (ispc ()) % MS Windows is not case-sensitive.
is_on_search_path = @(x) any (strcmpi (x, search_path_cstr));
else
is_on_search_path = @(x) any (strcmp (x, search_path_cstr));
end
fprintf (' Base directory... ');
if (~is_on_search_path (sedumi_path))
addpath (sedumi_path);
fprintf ('added.\n');
else
fprintf ('already there.\n');
end
if (~isempty(mdir))
fprintf (' MEX directory... ');
spath = fullfile(sedumi_path, mdir);
if (~exist(spath, 'dir'))
fprintf('not present.\n');
elseif (~is_on_search_path (spath))
addpath (spath);
fprintf ('added.\n');
else
fprintf ('already there.\n');
end
end
fprintf (' Conversion directory... ');
if (~is_on_search_path (fullfile (sedumi_path, 'conversion')))
addpath (fullfile (sedumi_path, 'conversion'));
fprintf ('added.\n');
else
fprintf ('already there.\n');
end
fprintf (' Examples directory... ');
if (~is_on_search_path (fullfile (sedumi_path, 'examples')))
addpath (fullfile (sedumi_path, 'examples'));
fprintf ('added.\n');
else
fprintf ('already there.\n');
end
end
disp (line_sep);
disp ('SeDuMi has been successfully installed.');
disp ('For more information, type "help sedumi" or see the user guide.');
disp (line_sep);
disp ('');
end