Skip to content

Instantly share code, notes, and snippets.

@ajaykumarsampath
Last active June 29, 2017 08:19
Show Gist options
  • Select an option

  • Save ajaykumarsampath/2289f6219979f75e10082cec7b94bb77 to your computer and use it in GitHub Desktop.

Select an option

Save ajaykumarsampath/2289f6219979f75e10082cec7b94bb77 to your computer and use it in GitHub Desktop.
comparing the total number of flops required with various implementations 1)LBFGS-FBE 2) CG-FBE 3)APG
%% In this scipt we calcualte the maximum and
% average flop counts required with different
% algorithms
clear all;
close all;
clc;
%% Reevaluating the Average gradient and Hessian counts
% LBFGS
AverGrad(1,:)=[84.0426 68.7755 129.750 80.4286...
82.6383 84.9556 80.5000 128.140 77.9200 78.3400];
AverHessian(1,:)=[43.5957 35.4898 67.6042 41.2653 42.8511...
44.2222 41.5833 67.3000 39.7800 40.3400];
Averiter(1,:)=[20.1277 16.8163 31.2083 19.7755...
20.0213 20.4444 19.5208 30.4400 19.1000 18.9400];
AverLessCounts(1,:)=[35.70, 22.30,40.96,24.38,35.84,...
35.38,35.16,42.30,18.14,18.95];
%APG
AverGrad(2,:)=[373.70 311.59 445.041 361.32 332.44...
353.66 340.12 420.04 335.30 329.84];
Averiter(2,:)=[187.34 156.30 223.10 181.18 166.70...
177.33 170.56 210.58 168.14 165.42];
% CG method
AverGrad(3,:)=[333.3289, 299.6634 356.0896 295.1123...
293.53 263.85 388.80 369.20 288.27 269.39];
Averiter(3,:)=[120.55 90.42 149.62 119.78 133.10...
105.72 112.72 137.80 107.06 114.30];
AverHessian(3,:)=[202.6 180.40 235.9 217.12...
227.32 195.98 173.54 164.81 184.47 201.48];
AverLessCounts(2,:)=[92.32 70.29 123.00 109.21...
123.24 118.90 98.27 84.67 78.18 91.81];
%% Modified average gradient and Hessian counts.
% Final number after caching the gradient
%LBFGS
ModiGrad(1,:)=AverGrad(1,:)-Averiter(1,:)-AverLessCounts(1,:);
ModiHess(1,:)=AverHessian(1,:)-Averiter(1,:);
%APG
ModiGrad(2,:)=AverGrad(2,:)-Averiter(2,:)+1;
%CG
ModiGrad(3,:)=AverGrad(3,:)-Averiter(3,:)-AverLessCounts(2,:);
ModiHess(3,:)=AverHessian(3,:)-Averiter(3,:);
%% Maximum Gradient and Hessian calls
% LBFGS
MaxGrad(:,1)=[279 464 1177 628 356 458 406 1142 203 274];
MaxHessian(:,1)=[145 236 621 322 186 254 208 626 103 140];
Maxiter(:,1)=[67 114 278 153 85 102 99 261 50 67];
MaxLessCounts(:,1)=[ 43 49 278 200 48 43 49 248 43 40];
% APG
MaxGrad(:,2)=[ 1119 1680 2668 2468 869 995 1165....
2420 817 1057];
Maxiter(:,2)=[560 840 1334 1234 435 498....
583 1210 409 529];
MaxGrad(:,3)=[1137 1428 2277 1756 1424 1016 939....
1922 812 1096];
MaxHessian(:,3)=[1180 1789 1923 1822 986 854 1288 2226 784 920];
Maxiter(:,3)=[545 883 1021 992 412 435 537 1097 390 499];
MaxLessCounts(:,2)=[33 19 326 421 281 41 51 48 33 35];
%% Modifed maximum gradient and Hessian calls
ModiMaxGrad(:,1)=MaxGrad(:,1)-Maxiter(:,1)-MaxLessCounts(:,1);
ModiMaxHess(:,1)=MaxHessian(:,1)-Maxiter(:,1);
ModiMaxGrad(:,2)=MaxGrad(:,2)-Maxiter(:,2);
ModiMaxGrad(:,3)=MaxGrad(:,3)-Maxiter(:,3)-MaxLessCounts(:,2);
ModiMaxHess(:,3)=MaxHessian(:,3)-Maxiter(:,3);
%% Number of flops
nx=10;nu=4;
Ns=[2 4 8 16 32 64 128 256 512 1024];
NumNodes=zeros(1,10);
% number of nodes
for i=1:10
NumNodes(i)=2^i*(11-i);
for j=1:i
NumNodes(i)=NumNodes(i)+Ns(j);
end
end
% final number of flops
FlpsPerScAlg=zeros(3,10);% 1)FBE-LBFGS, 2)FBE-CG 3)APG
ratioAvg=zeros(2,10); % 1)FBE-LBFGS/APG 2)FBE-CG/APG
% For Gradient
FlpsPerSc(:,1)=(2*nx+2*nu-1)*...
(nx+nu)+nu*(nx+1)+2*(nx+nu)*(2*nx+1);
% For Hessian
FlpsPerSc(:,2)=(2*nx+2*nu-1)*...
(nx+nu)+nu*(nx+1)+2*(nx+nu)*(2*nx+1)-nx;
%Np=11;
for i=1:10
FlpsPerScAlg(1,i)=NumNodes(i)*(ModiGrad(1,i)*FlpsPerSc(1,1)+...
ModiHess(1,i)*FlpsPerSc(2));
FlpsPerScAlg(2,i)=NumNodes(i)*(ModiGrad(2,i)*FlpsPerSc(1,1)+...
ModiHess(2,i)*FlpsPerSc(2));
FlpsPerScAlg(3,i)=NumNodes(i)*(ModiGrad(3,i)*FlpsPerSc(1,1)+...
ModiHess(3,i)*FlpsPerSc(2));
end
% Maximum
FlpsPerScMax=zeros(3,10);% 1)FBE-LBFGS, 2)FBE-CG 3) APG
ratioMax=zeros(2,10); % 1)FBE-LBFGS/APG 2)FBE-CG/APG
Np=11;
for i=1:10
FlpsPerScMax(1,i)=(ModiMaxGrad(i,1)*FlpsPerSc(1)+ModiMaxHess(i,1)*FlpsPerSc(2))....
*NumNodes(i);
FlpsPerScMax(2,i)=(ModiMaxGrad(i,3)*FlpsPerSc(1)+ModiMaxHess(i,2)*FlpsPerSc(2))....
*NumNodes(i);
FlpsPerScMax(3,i)=(ModiMaxGrad(i,3)*FlpsPerSc(1)+ModiMaxHess(i,3)*FlpsPerSc(2))....
*NumNodes(i);
end
%% plots
% average
figure
semilogy(Ns,FlpsPerScAlg(1,:),'*-')
hold all;
semilogy(Ns,FlpsPerScAlg(2,:),'*-')
semilogy(Ns,FlpsPerScAlg(3,:),'*-')
axis tight;
grid on;
legend('LBFGS-FBE','PR+','APG')
%title('average')
%
% maximum
figure
semilogy(Ns,FlpsPerScMax(1,:),'*-')
hold all;
semilogy(Ns,FlpsPerScMax(2,:),'*-')
semilogy(Ns,FlpsPerScMax(3,:),'*-')
axis tight;
grid on;
legend('LBFGS-FBE','PR+','APG')
%legend('LBFGS-FBE','CG-FBE','APG','Max-LBFGS-FBE',...
% 'Max-CG-FBE','Max-APG')
title('maximum')
%%
% iterations
figure
subplot(2,1,1)
semilogx(Ns,Averiter(1,:));
hold all;
semilogx(Ns,Averiter(2,:));
semilogx(Ns,Averiter(3,:));
axis tight;
grid on;
tick = Ns(6:end);
set(gca, ...
'XTick', tick,...
'XTickLabel', arrayfun(@(s)num2str(s,'%g'), tick, 'UniformOutput', false),...
'XMinorTick', 'off', ...
'TickDir', 'out')
legend('LBFGS-FBE','APG','PR+')
xlabel('# scenarios')
ylabel('iterations')
title('average')
%
subplot(2,1,2)
semilogx(Ns,Maxiter(:,1));
hold all;
semilogx(Ns,Maxiter(:,2));
semilogx(Ns,Maxiter(:,3));
axis tight;
grid on;
legend('LBFGS-FBE','APG','PR+')
set(gca, ...
'XTick', tick,...
'XTickLabel', arrayfun(@(s)num2str(s,'%g'), tick, 'UniformOutput', false),...
'XMinorTick', 'off', ...
'TickDir', 'out')
xlabel('# scenarios')
ylabel('iterations')
title('maximum')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment