From 8b46dcbba6c117e80bb1765282cf5972ac65b29e Mon Sep 17 00:00:00 2001 From: Michael Grant Date: Wed, 10 Apr 2024 11:43:38 -0500 Subject: [PATCH] cleanups --- .github/workflows/main.yaml | 13 +++ Examples/{graph.m => adjmat.m} | 7 +- Solver/sqlpdemo.m | 47 ++++++++--- install_sdpt3.m | 141 +++++++++++++++++---------------- 4 files changed, 124 insertions(+), 84 deletions(-) rename Examples/{graph.m => adjmat.m} (78%) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index a3a01ba..526ed4c 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -28,6 +28,10 @@ jobs: uses: matlab-actions/run-command@v2 with: command: install_sdpt3 -rebuild + - name: Run test + uses: matlab-actions/run-command@v2 + with: + command: "install_sdpt3; sqlpdemo -noplot -nopause -exitiferror" - name: Upload MATLAB MEX files uses: actions/upload-artifact@v4 with: @@ -67,6 +71,15 @@ jobs: which octave-cli.exe octave-cli.exe --no-gui --eval "install_sdpt3 -rebuild" if %errorlevel% neq 0 exit /b %errorlevel% + - name: Run test + if: matrix.os != 'windows-latest' + run: octave --eval "install_sdpt3; sqlpdemo -noplot -nopause -exitiferror" + - name: Run test + if: matrix.os == 'windows-latest' + shell: cmd + run: | + set PATH=C:\ProgramData\chocolatey\bin;%PATH% + octave-cli.exe --no-gui --eval "install_sdpt3; sqlpdemo -noplot -nopause -exitiferror" - name: Upload Octave MEX files uses: actions/upload-artifact@v4 with: diff --git a/Examples/graph.m b/Examples/adjmat.m similarity index 78% rename from Examples/graph.m rename to Examples/adjmat.m index 4e80376..4537a50 100755 --- a/Examples/graph.m +++ b/Examples/adjmat.m @@ -1,7 +1,8 @@ %%****************************************************** -%% graph: generate random adjacency matrix. +%% adjmat: generate random adjacency matrix. +%% renamed from "graph" by Michael Grant %% -%% B = graph(n,prob); +%% B = adjmat(n,prob); %% %% see maxcut.m %% @@ -11,7 +12,7 @@ %% Last modified: 2 Feb 01 %%****************************************************** - function B = graph(n,prob); + function B = adjmat(n,prob); B = zeros(n,n); if nargin <= 1; prob = 0.5; end; diff --git a/Solver/sqlpdemo.m b/Solver/sqlpdemo.m index 44413a6..224f5e2 100755 --- a/Solver/sqlpdemo.m +++ b/Solver/sqlpdemo.m @@ -13,16 +13,29 @@ %% Last Modified: 16 Sep 2004 %%***************************************************************** -function sqlpdemo +function sqlpdemo( varargin ) randn('seed',0); rand('seed',0); %#ok -feas = input('using feasible starting point? [yes = 1, no = 0] '); -if (feas) - fprintf('\n using feasible starting point\n\n'); + +if isempty(varargin) + feas = input('using feasible starting point? [yes = 1, no = 0] '); + if (feas) + fprintf('\n using feasible starting point\n\n'); + else + fprintf('\n using infeasible starting point\n\n'); + end + do_plot = true; + do_pause = true; + exit_if_error = false; else - fprintf('\n using infeasible starting point\n\n'); + feas = any( strcmp( varargin, '-feas') ); + do_plot = ~any( strcmp( varargin, '-noplot') ); + do_pause = ~any( strcmp( varargin, '-nopause') ); + exit_if_error = ~any( strcmp( varargin, '-exitiferror' ) ); +end +if do_pause; + pause(1); end -pause(1); ntrials = 1; % iterm = zeros(2,6); infom = zeros(2,6); timem = zeros(2,6); @@ -45,7 +58,7 @@ elseif (eg == 3); disp('******** Max-cut *********'); N = 10; - B = graph(N); + B = adjmat(N); [blk,At,C,b,X0,y0,Z0] = maxcut(B,feas); text = 'Maxcut'; elseif (eg == 4); @@ -57,7 +70,7 @@ elseif (eg == 5); disp('**** Lovasz theta function ****') N = 10; - B = graph(N); + B = adjmat(N); [blk,At,C,b,X0,y0,Z0] = thetaproblem(B,feas); text = 'Lovasz theta fn.'; elseif (eg == 6); @@ -86,11 +99,21 @@ if (vers==1); legendtext{end+1} = 'HKM'; %#ok elseif (vers==2); legendtext{end+1} = 'NT'; %#ok end; + if exit_if_error, + if infoall.termcode ~= 0, + error('Unexpected solver fail'); + end + end end; - h = plotgap(Gap,Feas); - xlabel(text); - legend(h(h~=0),legendtext{:}); - fprintf('\n**** press enter to continue ****\n'); pause + if do_plot + h = plotgap(Gap,Feas); + xlabel(text); + legend(h(h~=0),legendtext{:}); + end + if do_pause + fprintf('\n**** press enter to continue ****\n'); + pause + end end end %% diff --git a/install_sdpt3.m b/install_sdpt3.m index 3f2768b..9afe045 100644 --- a/install_sdpt3.m +++ b/install_sdpt3.m @@ -18,13 +18,30 @@ function install_sdpt3( varargin ) mpath = mfilename('fullpath'); mpath = mpath( 1 : max([1,strfind(mpath,fs)]) - 1 ); mbase = [ mpath, fs, 'Solver', fs, 'Mexfun' ]; -mdir = ''; -ISOCTAVE = exist('OCTAVE_VERSION','builtin'); +% 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'); -if ISOCTAVE, prog = 'Octave'; else prog = 'Matlab'; end COMPUTER = computer; -IS64BIT = strcmp(COMPUTER(end-1:end), '64'); -mext = mexext; +mdir = ''; +ISOCTAVE = exist('OCTAVE_VERSION','builtin'); +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'; + end + mext = 'mex'; +else + prog = 'Matlab'; + mext = mexext; +end % % We don't want to rebuild the binaries if they're already present, unless @@ -32,54 +49,33 @@ function install_sdpt3( varargin ) % guarantee that rebuilding will work. % -if ISOCTAVE, - page_output_immediately( true, 'local' ); -end - - line = '---------------------------------------------------------------------------'; fprintf( '\n%s\nSDPT3 installation script\n Directory: %s\n %s %s on %s\n%s\n', ... line, mpath, prog, version, COMPUTER, line ); -if ~need_rebuild, +if ~need_rebuild fprintf( 'Looking for existing binaries...' ); - mdir = ''; - if ISOCTAVE && VERSION >= 4, - 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'; - end - end - if ~isempty(mdir) && ~exist( [ mbase, fs, mdir ], 'dir' ), - mdir = ''; - end nfound = [ 0, 0 ]; - for k = 1 : length(targets64), + for k = 1 : length(targets64) targ = [ targets64{k}, '.', mext ]; - if exist( [ mbase, fs, targ ], 'file' ), + if exist( [ mbase, fs, targ ], 'file' ) nfound(1) = nfound(1) + 1; - elseif ~isempty(mdir) && exist( [ mbase, fs, mdir, fs, targ ], 'file' ), + elseif ~isempty(mdir) && exist( [ mbase, fs, mdir, fs, targ ], 'file' ) nfound(2) = nfound(2) + 1; end end - if sum(nfound) == 0, + if sum(nfound) == 0 fprintf( 'none found; building...\n' ); need_rebuild = true; - elseif sum(nfound) < length(targets64), + elseif sum(nfound) < length(targets64) fprintf( 'incomplete set found.\n' ); disp( line ); - warning(['Some of the binaries for this platform were found, but some', char(10), ... - 'were missing as well. This may mean your download was corrupt;', char(10), ... - 'consider downloading and unpacking SDPT3 again. Otherwise, to', char(10), ... - 'try rebuilding the MEX files yourself, run this command:', char(10), ... - ' install_sdpt3 -rebuild', char(10), ... - line, char(10), char(10)]); + warning(['Some of the binaries for this platform were found, but some', newline, ... + 'were missing as well. This may mean your download was corrupt;', newline, ... + 'consider downloading and unpacking SDPT3 again. Otherwise, to', newline, ... + 'try rebuilding the MEX files yourself, run this command:', newline, ... + ' install_sdpt3 -rebuild', newline, ... + line, newline, newline]); return; else fprintf( 'found!\n' ); @@ -87,57 +83,60 @@ function install_sdpt3( varargin ) fprintf( ' install_sdpt3 -rebuild\n' ); end else - nfound = [1,0]; + nfound = [ 1, 0 ]; end -if need_rebuild, +if need_rebuild disp( 'Attempting to recompile the SDPT3 binaries:' ); - % 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. + mdir = ''; libs = {}; flags = {}; - if ISOCTAVE, + if ISOCTAVE % Octave has mwSize and mwIndex hardcoded in mex.h as ints. % There is no definition for mwSignedIndex so include it here. % This means that Octave ignores the -largeArrayDims flag. cmd = 'mkoctfile'; flags{end+1} = '--mex'; - if VERSION < 3.08, + if VERSION < 3.08 flags{end+1} = '-DmwSignedIndex=int'; end else cmd = 'mex'; flags = {'-O'}; - if ispc, + if ispc flags{end+1} = '-DPC'; - elseif isunix, + elseif isunix flags{end+1} = '-DUNIX'; end - if strcmp(COMPUTER(end-1:end),'64') && ( VERSION >= 7.03 ), + if strcmp(COMPUTER(end-1:end),'64') && ( VERSION >= 7.03 ) flags{end+1} = '-largeArrayDims'; - elseif VERSION < 7.03, + elseif VERSION < 7.03 flags{end+1} = '-DmwIndex=int'; flags{end+1} = '-DmwSize=int'; flags{end+1} = '-DmwSignedIndex=int'; end - if VERSION >= 7 && ispc, - if IS64BIT, dirval = 'win64'; else dirval = 'win32'; end + if VERSION >= 7 && ispc + if strcmp(COMPUTER(end-1:end), '64') + dirval = 'win64'; + else + dirval = 'win32'; + end libdir = [ matlabroot, fs, 'extern', fs, 'lib', fs, dirval, fs ]; - if exist( [ libdir, 'microsoft' ], 'dir' ), + if exist( [ libdir, 'microsoft' ], 'dir' ) libdir = [ libdir, 'microsoft' ]; found = true; - elseif exist( [ libdir, 'msvc60' ], 'dir' ), + elseif exist( [ libdir, 'msvc60' ], 'dir' ) libdir = [ libdir, 'msvc60' ]; found = true; - elseif exist( [ libdir, 'lcc' ], 'dir' ), + elseif exist( [ libdir, 'lcc' ], 'dir' ) libdir = [ libdir, 'lcc' ]; found = true; end - if found, + if found libs{end+1} = [ '-L"', libdir, '"' ]; end end - if VERSION >= 7.05, + if VERSION >= 7.05 libs{end+1} = '-lmwblas'; else libs{end+1} = '-lmwlapack'; @@ -147,19 +146,23 @@ function install_sdpt3( varargin ) cd( mbase ); failed = false; fprintf( 'Template: %s%s %s\n', cmd, sprintf( ' %s', flags{:} ), sprintf( ' %s', libs{:} ) ); - for i=1:length(targets64), + for i=1:length(targets64) targ = [ targets64{i}, '.', mext ]; sfile = [ targets64{i}, '.c' ]; fprintf( ' %s\n', targ ); - if ISOCTAVE, - mkoctfile(flags{:}, sfile, libs{:}); - else - mex(flags{:}, sfile, libs{:}); + try + if ISOCTAVE + mkoctfile(flags{:}, sfile, libs{:}); + else + mex(flags{:}, sfile, libs{:}); + end + catch + failed = true; end end cd( olddir ); - if failed, - fprintf( 'At least one compilation failure occurred.\n' ); %#ok + if failed + fprintf( 'At least one compilation failure occurred.\n' ); nfound = [0,0]; else fprintf( 'Compilation successful.\n' ); @@ -167,11 +170,11 @@ function install_sdpt3( varargin ) end end -if ~any(nfound), +if ~any(nfound) disp( line ); - error(['SDPT3 was not successfully installed.', char(10), ... + error(['SDPT3 was not successfully installed.', newline, ... 'Please attempt to correct the errors and try again.']); -elseif ~no_path, +elseif ~no_path disp( line ); fprintf( 'Adding SDPT3 to the %s path:\n', prog ); paths = { 'Base', 'Solver', 'HSDSolver', 'Binaries', 'Binaries', 'Examples' ; ... @@ -181,14 +184,14 @@ function install_sdpt3( varargin ) ps = pathsep; pp = [ ps, path, ps ]; already = true; - for k = 1 : size(paths,2), + for k = 1 : size(paths,2) if paths{3,k} ~= 0 && nfound(paths{3,k}) == 0, continue; end fprintf( ' %s...', paths{1,k} ); t_dir = mpath; - if ~isempty(paths{2,k}), + if ~isempty(paths{2,k}) t_dir = [ t_dir, fs, paths{2,k} ]; %#ok end - if ~any(strfind(pp,[ps,t_dir,ps])), + if ~any(strfind(pp,[ps,t_dir,ps])) already = false; pp = [ pp, t_dir, ps ]; %#ok fprintf( 'added.\n' ); @@ -196,7 +199,7 @@ function install_sdpt3( varargin ) fprintf( 'already there.\n' ); end end - if ~already, + if ~already path(pp); fprintf( 'Please save the %s path if you want to use SDPT3 from any directory.\n', prog ); end