MATLAB Parallel Acceleration: parfor, spmd, or gpuArray
Decide between parfor, spmd, and gpuArray in MATLAB based on iteration independence, data size, and available hardware.
16 Nov 2025, 15:30 UTC

The decision and your constraints
Your MATLAB simulation takes hours. The loop body is simple, but the data is large, and you have Parallel Computing Toolbox installed. Before rewriting code, you must decide whether to use parfor, spmd, or gpuArray. Choosing the wrong option can add communication overhead without reducing runtime.
Useful takeaway: use parfor for independent loop iterations on one machine, spmd when you need explicit control across multiple workers or a cluster, and gpuArray for dense array math on a CUDA GPU. Confirm the license, worker pool, and hardware first; otherwise fall back to serial execution.
A worker is a MATLAB computational engine that runs code independently of the main session. A parpool is a pool of workers started locally or on a cluster. gpuArray stores data on a CUDA-capable GPU so GPU-enabled functions operate on it. Embarrassingly parallel means each loop iteration can run without waiting for or writing to results from another iteration.
Compare the three options
| Option | Best suited for | Data movement | Scaling limit | Primary risk |
|---|---|---|---|---|
parfor | Independent loop iterations on one machine | Shared memory; MATLAB slices variables automatically | Local workers on one machine | Non-independent iterations or incorrect slicing cause errors or silently wrong results |
spmd | Explicit multi-worker algorithms and message passing | You distribute and gather data explicitly | Cluster or multiple machines | Communication overhead; code must manage data distribution manually |
gpuArray | Dense matrix and array math on a GPU | Host-to-device transfer before and after computation | One GPU per MATLAB session | Transfer cost can dominate small problems; not all functions support gpuArray |
Trade-offs that matter in practice
parfor is the simplest entry point. MATLAB handles work distribution, so you only need to confirm that iterations are independent. It does not scale beyond the workers in your local pool, so very large problems may still run slowly.
spmd gives you explicit control over which worker holds which data. That control is useful for domain decomposition or custom message passing, but you must write the distribution and collection logic yourself. The extra code often outweighs the benefit unless you are running on a cluster.
gpuArray delivers high throughput for dense linear algebra and element-wise array math. The penalty is the host-to-device transfer: copying a large array to the GPU and back can consume more time than the computation itself, especially for small arrays. GPU support is also function-specific; an unsupported call may error or silently run on the CPU depending on your release.
Implementation and validation
Step 1: Confirm the toolbox license
Run this in the MATLAB Command Window. No special permissions are required. It is a read-only check.
license('test','Parallel_Computing_Toolbox')
Expected result: 1 means the toolbox is licensed; 0 means it is not. You can also run ver and confirm Parallel Computing Toolbox appears in the version list.
Step 2: Start a local worker pool
Run this in the MATLAB Command Window.
parpool;
Expected result: MATLAB starts a pool of local workers and displays pool information. Risk: starting the pool consumes worker licenses. If no workers are available or the license count is exceeded, MATLAB returns an error and no pool is created. To shut the pool down later, run delete(gcp('nocreate')).
Step 3: Convert an independent loop to parfor
Run this script in the MATLAB Editor or Command Window. Replace n with a size that represents your production workload.
% Serial baseline
n = 1000000;
serialResult = zeros(1, n);
tic
for i = 1:n
serialResult(i) = sin(i);
end
serialTime = toc;
% Parallel attempt
parallelResult = zeros(1, n);
tic
parfor i = 1:n
parallelResult(i) = sin(i);
end
parallelTime = toc;
% Validate correctness
match = isequal(serialResult, parallelResult);
Expected checks: match should return logical 1. If it returns 0, the loop body likely contains a race condition or unsupported indexed assignment; do not use parfor until it is fixed. Compare serialTime and parallelTime on data sizes that match your real workload; parallel is not automatically faster.
Step 4: Check and use the GPU
Run this in the MATLAB Command Window.
try
gpuDevice; % Errors if no CUDA-capable GPU is available
a = gpuArray(rand(2000, 2000));
b = sin(a);
c = gather(b); % Copy result back to host memory
catch ME
warning('GPU path unavailable: %s', ME.message);
c = sin(rand(2000, 2000));
end
Expected checks: gpuDevice returns a GPU object when hardware is present. gather copies data from the GPU to the host; time this transfer separately because it can dominate small problems. If your specific function does not support gpuArray, MATLAB may error or fall back to CPU behavior depending on your release.
Limitations and practical check
parforrequires loop iterations to be independent. Incorrect variable slicing can produce errors or silently wrong results.spmdconsumes workers and licenses; exceeding the available count prevents pool startup.- Not all MATLAB functions support
gpuArray. Unsupported calls may error or run on the CPU depending on your release. - GPU host-to-device transfer may outweigh computation for small arrays. Benchmark before deploying.
Practical check: run your serial and candidate parallel versions on identical production-sized data, compare outputs with isequal, time both with tic and toc, and use the MATLAB Profiler if the parallel version is slower than expected. Function support for gpuArray and parfor behavior can vary by MATLAB release; verify against your installed release documentation before deploying to production.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.