echo on
%%%%% lab03_hwork.m %%%%%

disp(sprintf('**** MatLab solution of HW01 ****\n'));

%input data%
tc=0.01 * 10^-6;
ts=70 * 10^-6;
tw=0.3 * 10^-6;
N=1000;
NN=N*N;
P=[1:1000];
%results%
Tcomp=(tc*NN*5)./P;
Tcomm2=4*(ts+(sqrt(NN./P)).*tw);	%2-D decomposition%
Texe2=Tcomp+Tcomm2;
Tcomm1=2*(ts + N*tw);	%1-D decomposition%
Texe1=Tcomp+Tcomm1;
figure(1);
loglog(P,Tcomp,'k-', P,Tcomm2, 'r-', P,Tcomm1, '+b-', P,Texe2, '.r-', P, Texe1, '.b-');
title('Comput. commun. & execut. time as a function of P');
%axis([0  1000  0  10^-3])

%input data%
P=32;
N=[1:1000];
NN=N.*N;
%results%
Tcomp=(tc*NN*5)/P;
Tcomm2=4*(ts+sqrt(NN/P)*tw);	%2-D decomposition%
Texe2=Tcomp+Tcomm2;
Tcomm1=2*(ts + N*tw);	%1-D decomposition%
Texe1=Tcomp+Tcomm1;
figure(2);
plot(N,Tcomp,'k-', N,Tcomm2, 'r-', N,Tcomm1, 'b-', N,Texe2, '.r-', N, Texe1, '.b-');
%semilogx(N,Tcomp,'.k-', N,Tcomm2, '.r-', N,Tcomm1, '.b-', N,Texe2, '+r', N, Texe1, 'ob');
title('Comput. commun. & execut. time as a function of NN');
%axis([0  1000  0  10^-3])

disp(sprintf('**** Excercise 3 *'));
%solved by pencil%

disp(sprintf('**** Excercise 8 *****'));
%input data%
P=1000;
N=[1:1000];
NN=N.*N;
Z=[1:1000];
%Z=100;
NNZ=NN.*Z;
%NNZ=NN*Z;
tc=0.01 * 10^-6; %if tc increasses the communication time becomes less important%
ts=1 * 10^-6; %ts has no big impact on the execution time%
tw=0.04 * 10^-6; %with greather tw, communication time increases%

%results%
Tcomp=(tc*(NNZ)*7)/P;

Tcomm1=2*(ts + (N.*Z)*tw);	%1-D decomposition, x-z plane%
Texe1=Tcomp+Tcomm1;

Tcomm2=4*(ts+ ((NN/P).^(1/2).*Z)*tw);	%2-D decomposition, x-y plane%
Texe2=Tcomp+Tcomm2;

Tcomm3=6*(ts+ ((NNZ/P).^(2/3))*tw);	%3-D decomposition, square boxes%
Texe3=Tcomp+Tcomm3;

figure(3);
%semilogx(NNZ,Tcomm1,'.b-', NNZ, Tcomm2, '.r-',NNZ, Tcomm3, '.k-');
%title('Communication time as a function of NNZ');
%axis([0  10^5  0  2*10^-5])
%semilogx(NNZ,Texe1,'.b-', NNZ, Texe2, '.r-',NNZ, Texe3, '.k-');
plot(NNZ,Texe1,'.b-', NNZ, Texe2, '.r-',NNZ, Texe3, '.k-');
title('Execution time as a function of NNZ');
axis([0  5*10^4 0 2*10^(-5)])

disp(sprintf('**** Excercise 10 *****'));
%solved by pencil%

disp(sprintf('**** Excercise 11 *****'));
%solved by pencil%

