Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CPU vs GPU test using Matlab
%gpuDevice() %info about GPU
clear
gpuDevice(1); %reset GPU device
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%test matrix multiplication
try
%n= 100;
for m=100:100:10000
n=m;
%host
Mh= rand(m,n);
%Mh= rand(m,n,'single');
tic;
resh= Mh'*Mh;
th(m)= toc;
%device
Md = gpuArray.rand(m,n);
%Md = gpuArray.rand(m,n,'single');
tic;
resd= Md'*Md;
td(m)= toc;
clear resd;
clear Md;
end
catch
end
%blue CPU, red GPU
figure('Name','CPU vs GPU');
plot(1:length(th),th,'b', 1:length(td), td, 'r');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %test SVD
% for i=1:10
% clear;
% m= 10000;
% n= 100;
% %host
% Mh= rand(m,n,'single');
% %Mh= rand(m,n);
% tic
% [Uh,Sh,Vh]= svd(Mh);
% %svd(Mh);
% toc
% %device
% Md = gpuArray.rand(m,n,'single');
% %Md = gpuArray.rand(m,n);
% tic
% [Ud,Sd,Vd]= svd(Md);
% %svd(Md);
% toc
% end
% try
% %for n=10:10:1000
% for n=100:100:10000
% %host
% Mh= rand(n);
% tic;
% [Uh,Sh,Vh]= svd(Mh);
% th(n)= toc;
% %device
% Md = gpuArray.rand(n);
% tic;
% [Ud,Sd,Vd]= svd(Md);
% td(n)= toc;
% end
% catch
% end
%
% %blue CPU, red GPU
% figure('Name','CPU vs GPU');
% plot(1:length(th),th,'b', 1:length(td), td, 'r');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%test linear system of equations solver
% %for n=10:10:1000
% for n=100:100:10000
% %host
% tic;
% Mh= rand(n);
% bh= rand(n,1);
% xh= Mh\bh;
% th(n)= toc;
% %device
% tic;
% Md = gpuArray.rand(n);
% bd = gpuArray.rand(n,1);
% xd= Md\bd;
% td(n)= toc;
% end
%
% %blue CPU, red GPU
% plot(1:length(th),th,'b', 1:length(td), td, 'r');
% % figure('Name','CPU(host)');
% % plot(th);
% % figure('Name','GPU(device)');
% % plot(td);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %test random number generation
% try
% %for n=10:10:1000
% for n=100:100:10000
% %host
% tic;
% Mh= rand(n);
% th(n)= toc;
% %device
% tic;
% Md = gpuArray.rand(n);
% td(n)= toc;
% end
% catch
% end
%
% %blue CPU, red GPU
% figure('Name','CPU vs GPU');
% plot(1:length(th),th,'b', 1:length(td), td, 'r');
%
% % figure('Name','CPU(host)');
% % plot(th);
% % figure('Name','GPU(device)');
% % plot(td);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.