%% Benchmark Cluster Workers
% This example shows how to run the MATLAB benchmark on your cluster workers. The benchmark measures the execution speed of several MATLAB computations. You can plot these results and compare the performance of the client and workers.
% This example uses pbench, a function that runs a subset of the tests in bench, the MATLAB benchmark. The tests in this subset are LU, FFT, ODE, and Sparse. For details on these tests, see bench.
% Run the MATLAB benchmark on the client.

tClient = pbench

% Create a parallel pool p using the parpool function. By default, parpool starts a parallel pool with workers on your default cluster. Select your default cluster on the MATLAB Home tab, in the Environment area, in Parallel > Select a Default Cluster.

p = parpool();

% Run the MATLAB benchmark on the workers using the parfevalOnAll function. parfevalOnAll offloads the execution of a function to all the workers in the pool, and returns a parallel.FevalOnAllFuture object to hold the results when they are ready. To obtain the results from the workers, use fetchOutputs on the future object.

f = parfevalOnAll(@pbench,1);
tWorkers = fetchOutputs(f);

%
% Combine the results of the client and workers, and plot them using a bar plot. Compare the relative performances of the workers and client.

tClientAndWorkers = [tClient;tWorkers];
bar(tClientAndWorkers');
xticklabels({'LU','FFT','ODE','Sparse'});
xlabel("Benchmark type");
ylabel("Benchmark execution time (seconds)");
workerNames = strcat("Worker ",string(1:size(tWorkers,1)));
legend(["Client",workerNames],'Location','bestoutside');

% By default, the MATLAB client is enabled for multithreading. Multithreading enables MATLAB numerical functions, such as lu or fft, to run on multiple cores using multiple computational threads. The workers use a single computational thread by default, because they are typically associated with a single core. Therefore, the LU test, for example, runs faster on the MATLAB client than on the workers. Other problems, such as ODEs, cannot benefit from multithreading, so they perform the same on the MATLAB client and workers. Consider this difference when deciding whether to distribute computations to MATLAB parallel workers, such as with parfor. For more details, see Deciding When to Use parfor. For more information on multithreading, see Run MATLAB on multicore and multiprocessor machines.
% Copyright 2020 The MathWorks, Inc.