Simple Benchmarking of PARFOR Using Blackjack

This example benchmarks the parfor construct by repeatedly playing the card game of blackjack, also known as 21. We use parfor to play the card game multiple times in parallel, varying the number of MATLAB® workers, but always using the same number of players and hands.

Parallel Version

The basic parallel algorithm uses the parfor construct to execute independent passes through a loop. It is a part of the MATLAB® language, but behaves essentially like a regular for-loop if you do not have access to the Parallel Computing Toolbox™ product. Thus, our initial step is to convert a loop of the form

for i = 1:numPlayers
S(:, i) = playBlackjack();
end

into the equivalent parfor loop:

parfor i = 1:numPlayers
S(:, i) = playBlackjack();
end

We modify this slightly by specifying an optional argument to parfor, instructing it to limit to n the number of workers it uses for the computations. The actual code is as follows:

Check the Status of the Parallel Pool

We will use the parallel pool to allow the body of the parfor loop to run in parallel, so we start by checking whether the pool is open. We will then run the benchmark using anywhere between 2 and poolSize workers from this pool.

Plot the Speedup

We compare the speedup using parfor with different numbers of workers to the perfectly linear speedup curve. The speedup achieved by using parfor depends on the problem size as well as the underlying hardware and networking infrastructure.

Benchmark has run 20 out of 100 times.
Benchmark has run 40 out of 100 times.
Benchmark has run 60 out of 100 times.
Benchmark has run 80 out of 100 times.
Benchmark has run 100 out of 100 times.

Plot the Speedup Distribution

We take a close look at the speedup of our simple parallel program when using the maximum number of workers. The histogram of the speedup allows us to distinguish between outliers and the average speedup.