%http://www.mathworks.com/products/parallel-computing/demos.html

%http://www.mathworks.com/products/parallel-computing/demos.html?file=/prod

%ucts/demos/shipping/distcomp/paralleldemo_parfor_bench.html

% modified by Dustin Li, 04/20/2010

poolSize = matlabpool('size');

if poolSize == 0

error('distcomp:demo:poolClosed', ...

'This demo needs an open MATLAB pool to run.');

end

% parallelize this task

numHands = 500;

numPlayers = 6;

fprintf('Simulating each player playing %d hands of blackjack.\n', numHands);

t1 = zeros(1, poolSize);

for n = 1:poolSize

tic;

pctdemo_aux_parforbench(numHands, n*numPlayers, n);

t1(n) = toc;

fprintf('%d workers simulated %d players in %3.2f seconds.\n', ...

n, n*numPlayers, t1(n));

end

% make sure we aren't be limited by communication overhead

tic;

pctdemo_aux_parforbench(numHands, 10*numPlayers, 1);

clusterToc = toc;

fprintf('1 cluster workers simulated %d players in %3.2f seconds.\n', ...

10*numPlayers, clusterToc);

% sequential loop

% tic;

% parfor z = 0

% S = zeros(numHands, numPlayers);

% for i = 1:numPlayers

% S(:, i) = pctdemo_task_blackjack(numHands, 1);

% end

% end

% t1(1) = toc;

% fprintf('Ran in %3.2f seconds using a sequential for-loop.\n', t1(1));

tic;

S = zeros(numHands, 60);

for i = 1:60

S(:, i) = pctdemo_task_blackjack(numHands, 1);

end

localToc = toc;

fprintf('1 local worker simulated 60 players in %3.2f seconds.\n', localToc);

disp('(Old cluster: 1 worker, 60 players, 11.89 seconds.)');

speedup = (1:poolSize).*t1(1)./t1;

fig = pctdemo_setup_blackjack(1.0);

set(fig, 'Visible', 'on');

ax = axes('parent', fig);

x = plot(ax, 1:poolSize, 1:poolSize, '--', ...

1:poolSize, speedup, 's', 'MarkerFaceColor', 'b');

t = get(ax, 'XTick');

t(t ~= round(t)) = []; % Remove all non-integer x-axis ticks.

set(ax, 'XTick', t);

legend(x, 'Linear Speedup', 'Measured Speedup', 'Location', 'NorthWest');

xlabel(ax, 'Number of MATLAB workers participating in computations');

ylabel(ax, 'Speedup');

%%

% now plot single thread results

figure(2);

bar([7.37 localToc 11.89]);

set(gca,'XTickLabel',{'New cluster','Local','Old cluster'});

ylabel('Execution time (s)');

title('Single threaded performance');

set(gcf, 'Name', 'Single threaded performance');