Parallel Computing in MATLAB
Parallel computing allows MATLAB users to perform computations simultaneously across multiple cores, processors, or machines. This significantly speeds up operations and is essential for large datasets or computationally intensive tasks.
What is Parallel Computing?
Parallel computing refers to the simultaneous execution of multiple computations. MATLAB simplifies this process using its Parallel Computing Toolbox. It is especially helpful for:
- Running iterative tasks faster.
- Solving problems with large datasets.
- Utilizing modern multi-core CPUs and GPUs efficiently.
Starting with Parallel Computing in MATLAB
Ensure the Parallel Computing Toolbox is installed. To check if it is available, use:
% Check if Parallel Computing Toolbox is available
hasToolbox = license('test', 'Distrib_Computing_Toolbox');
disp(hasToolbox); % Outputs 1 if available, 0 otherwise
Creating a Parallel Pool
A parallel pool is a set of MATLAB workers for parallel computations. Use the following command to start a pool:
% Start a parallel pool
parpool;
By default, MATLAB uses all available cores. You can specify the number of workers:
% Start a pool with 4 workers
parpool(4);
Parallel For Loops (parfor)
The parfor
loop allows you to divide iterations across multiple workers:
% Using parfor for parallel processing
N = 1000;
result = zeros(1, N);
parfor i = 1:N
result(i) = sqrt(i); % Example computation
end
disp(result);
Parallel Execution of Independent Tasks
Use the parfeval
function to run independent tasks asynchronously:
% Parallel execution using parfeval
futures = cell(1, 4);
for i = 1:4
futures{i} = parfeval(@rand, 1, 3, 3); % Generate random 3x3 matrices
end
results = cellfun(@fetchOutputs, futures, 'UniformOutput', false);
disp(results);
GPU Computing
MATLAB can also utilize GPUs for parallel computations:
% GPU computations
A = rand(1000, 'gpuArray'); % Create a matrix on the GPU
B = A.^2; % Perform operations on the GPU
disp(gather(B)); % Retrieve data from the GPU
Example Problems Related to Parallel Computing in MATLAB
Here are some problems you can solve using parallel computing using MATLAB:
Example 1: Monte Carlo Simulation
Estimate π using random points:
% Monte Carlo simulation
N = 1e6;
inside = 0;
parfor i = 1:N
x = rand;
y = rand;
if x^2 + y^2 <= 1
inside = inside + 1;
end
end
pi_estimate = 4 * inside / N;
disp(pi_estimate);
Example 2: Matrix Multiplication
Perform large matrix multiplication in parallel:
% Parallel matrix multiplication
A = rand(1000);
B = rand(1000);
parfor i = 1:1000
C(i, :) = A(i, :) * B;
end
disp(C);
Advance Problems Related to Parallel Computing in MATLAB
These advanced problems demonstrate the power of parallel computing in MATLAB:
Example 3: Solving a System of Linear Equations in Parallel
Use MATLAB to solve a large system of linear equations in parallel:
% Solving Ax = B in parallel
N = 1000;
A = rand(N);
B = rand(N, 1);
x = zeros(N, 1);
parfor i = 1:N
% Decompose A into rows and solve for each element of x
x(i) = B(i) / A(i, i);
end
disp(x);
Example 4: Image Processing in Parallel
Apply a Gaussian filter to an image using parallel loops:
% Image processing in parallel
img = imread('example.jpg'); % Load image
imgGray = rgb2gray(img); % Convert to grayscale
filteredImg = zeros(size(imgGray));
% Apply Gaussian filter in parallel
kernel = fspecial('gaussian', [3 3], 0.5);
parfor i = 2:size(imgGray, 1)-1
for j = 2:size(imgGray, 2)-1
region = imgGray(i-1:i+1, j-1:j+1);
filteredImg(i, j) = sum(sum(region .* kernel));
end
end
imshow(uint8(filteredImg));
Example 5: Financial Simulations in Parallel
Simulate stock prices using the Black-Scholes model:
% Black-Scholes model for stock simulation
S0 = 100; % Initial stock price
mu = 0.05; % Mean return
sigma = 0.2; % Volatility
T = 1; % Time in years
N = 100000; % Number of simulations
dt = 1/252; % Time step (daily)
parfor sim = 1:N
S = S0;
for t = 1:(T/dt)
S = S * exp((mu - 0.5 * sigma^2) * dt + sigma * sqrt(dt) * randn);
end
finalPrices(sim) = S;
end
histogram(finalPrices, 50); % Plot histogram of final prices
Example 6: Parallel Optimization with Genetic Algorithms
Optimize a mathematical function using MATLAB’s genetic algorithm in parallel:
% Parallel optimization using genetic algorithm
fitnessFunc = @(x) x(1)^2 + x(2)^2 + x(3)^2; % Minimize x^2 + y^2 + z^2
options = optimoptions('ga', 'UseParallel', true, 'PopulationSize', 100);
[x_opt, fval] = ga(fitnessFunc, 3, [], [], [], [], [-10 -10 -10], [10 10 10], [], options);
disp(['Optimal solution: ', num2str(x_opt)]);
disp(['Function value: ', num2str(fval)]);
Example 7: Numerical Integration Using Parallel Workers
Compute the integral of a function using Monte Carlo methods in parallel:
% Monte Carlo integration in parallel
f = @(x) exp(-x.^2); % Function to integrate
a = 0; b = 1; % Integration limits
N = 1e7; % Number of points
parfor i = 1:N
x = a + (b - a) * rand;
y(i) = f(x);
end
integralEstimate = (b - a) * mean(y);
disp(['Estimated integral: ', num2str(integralEstimate)]);
Useful MATLAB Functions for Parallel Computing
Practice Questions
Test Yourself
1. Use parfor
to compute the sum of squares of the first 106 natural numbers.
2. Create a parallel pool with 8 workers and perform a task of your choice.
3. Implement a basic GPU computation to calculate the exponential of a large matrix.
4. Write a parfor
loop to compute the eigenvalues of 1000 randomly generated matrices.
5. Implement a basic parallel simulation for a queuing system where customers arrive at random intervals.
6. Use parallel workers to process multiple images and apply a median filter to each.