% This Matlab script loads a data file ('cache_performance_N16384_L1') containing the hit rate measurements for different cache sizes of two direct-mapped cache with 64bit line width. The script produces a 3D 'skyscraper' plot, i.e. a grid of coloured bars. Each bar corresponds to the hit rate measured at the particular cache size configuration. The plot is saved in the file 'surf.pdf'. For questions, please email f.winterstein12@imperial.ac.uk

function plot_cache_performance

tmp=load('cache_performance_N16384_L1','-ascii');
tmp=tmp(1:end-1);
tmp2=reshape(tmp,3,[]);

l=size(tmp2,2);

cache0=tmp2(1:2,1:l/2)
cache1=tmp2(1:2,(l/2+1):l)

hits0=cache0(1,:);
hits1=cache1(1,:);
misses0=cache0(2,:);
misses1=cache1(2,:);

x=1:1:l/2;
y=1:1:l/2;
y=l/2:-1:1;
[X,Y]=meshgrid(x,y);



set(0,'DefaultAxesFontSize', 12);

RC=2200;

Zmask = (2.^(X+2) + 2.^(Y+2)) <= RC;
Zmask_inv = (2.^(X+2) + 2.^(Y+2)) > RC;

Z=(hits0(X)+hits1(Y))./(hits0(X)+misses0(X)+hits1(Y)+misses1(Y));


%surf(X,Y,Z);

Zt_orig = Z-0.75;
Z= Z .* Zmask;

Zt=Z-0.75+(Zmask_inv*0.75);


horig = bar3(Zt_orig);
cdata = cell(1,length(horig));
for jj = 1:length(horig)
    zd = get(horig(jj),'zdata');
    cdata{jj} = zd;
end
delete(horig);

h=bar3(Zt);
zlim([0,0.25]);
% Add these lines to your code after the ‘bar3’ call:
ztiks = floor(linspace(0.75, 1, 6)*100)/100;
set(gca, 'ZTickLabel', ztiks');
% Delete or comment-out this line:
%set(gca, 'ZLim', [min(min(Z)) max(max(Z))]);

%rotate3d;
%colormap(none);




for jj = 1:length(h) 
    
    zd = get(h(jj),'zdata');
     
    zd = cdata{jj};
    
    set(h(jj),'CData',zd);
    set(h(jj),'FaceColor','interp');

%     delete(h(jj))    
%     idx = [0;find(all(isnan(xd),2))];
%     if jj == 1
%         S = zeros(length(h)*(length(idx)-1),1);
%         dv = floor(size(cm,1)/length(S));
%     end
%     for ii = 1:length(idx)-1
%         cnt = cnt + 1;
%         S(cnt) = surface(xd(idx(ii)+1:idx(ii+1)-1,:),...
%                          yd(idx(ii)+1:idx(ii+1)-1,:),...
%                          zd(idx(ii)+1:idx(ii+1)-1,:),...
%                          'facecolor',cm((cnt-1)*dv+1,:));
%     end
end


for i = 1:numel(h)
  index = logical(kron(Zt(:,i)==0,ones(6,1)));
  zData = get(h(i),'ZData');
  zData(index,:) = nan;
  set(h(i),'ZData',zData);
end

grid on;



xlabel('Cache size 0 [KB]','FontSize',14);
ylabel('Cache size 1 [KB]','FontSize',14);
zlabel('Aggregate hit rate','FontSize',14);

ax = gca;

%get(ax,'XTick')


set(ax,'XTick',[1 2 3 4 5 6 7 8 9 10]);
set(ax,'XTickLabel',{'8', '16', '32', '64','128', '256', '512', '1024', '2048', '4096'});

set(ax,'YTick',[1 2 3 4 5 6 7 8 9 10]);
set(ax,'YTickLabel',{'4096', '2048', '1024', '512', '256',  '128', '64', '32', '16', '8'});


%% export
h=gcf;

%set(h,'PaperOrientation','landscape');

set(h,'PaperUnits','normalized');

set(h,'PaperPosition', [0 0 1 1]);

print(gcf, '-dpdf', 'surf.pdf');


end
