Parallel computing in MATLAB allows you to perform computations simultaneously on multiple processors, which can significantly speed up your code execution, especially for large data sets or complex calculations. This section will cover the basics of parallel computing, including setting up a parallel environment, using parallel loops, and leveraging built-in parallel functions.

Key Concepts

  1. Parallel Computing Toolbox: A MATLAB toolbox that provides functions and tools to parallelize your code.
  2. Parallel Pools: A collection of MATLAB workers that execute tasks concurrently.
  3. parfor Loops: A parallel version of the for loop.
  4. spmd (Single Program Multiple Data): A construct for running code on multiple workers simultaneously.
  5. Distributed Arrays: Arrays that are distributed across multiple workers.
  6. Parallel Functions: Built-in functions that support parallel execution.

Setting Up Parallel Environment

Before you can use parallel computing features, you need to ensure that the Parallel Computing Toolbox is installed and set up.

Checking Installation

% Check if Parallel Computing Toolbox is installed
if license('test', 'Distrib_Computing_Toolbox')
    disp('Parallel Computing Toolbox is installed.');
else
    disp('Parallel Computing Toolbox is not installed.');
end

Starting a Parallel Pool

% Start a parallel pool with default settings
parpool;

% Start a parallel pool with a specified number of workers
parpool('local', 4);

Using parfor Loops

The parfor loop allows you to execute iterations of a loop in parallel.

Example: Parallel Sum of an Array

% Initialize an array
A = rand(1, 1000000);

% Initialize a variable to store the sum
totalSum = 0;

% Use parfor to compute the sum in parallel
parfor i = 1:length(A)
    totalSum = totalSum + A(i);
end

disp(['Total Sum: ', num2str(totalSum)]);

Explanation

  • parfor replaces the standard for loop.
  • Each iteration of the loop is executed by a different worker in the parallel pool.
  • Note: The above example has a race condition issue. A better approach is to use reduction variables.

Corrected Example with Reduction Variable

% Initialize an array
A = rand(1, 1000000);

% Use parfor to compute the sum in parallel
totalSum = 0;
parfor i = 1:length(A)
    totalSum = totalSum + A(i);
end

disp(['Total Sum: ', num2str(totalSum)]);

Using spmd

The spmd construct allows you to run code on multiple workers simultaneously.

Example: Distributed Array Creation

% Create a distributed array
spmd
    D = distributed.rand(1000, 1000);
end

% Display the size of the distributed array
disp(size(D));

Explanation

  • spmd block runs the code inside it on all workers.
  • distributed function creates an array distributed across the workers.

Practical Exercise

Exercise: Parallel Matrix Multiplication

Write a MATLAB script to perform matrix multiplication in parallel using parfor.

Solution

% Initialize two large matrices
A = rand(1000, 1000);
B = rand(1000, 1000);

% Initialize the result matrix
C = zeros(size(A, 1), size(B, 2));

% Perform matrix multiplication in parallel
parfor i = 1:size(A, 1)
    for j = 1:size(B, 2)
        C(i, j) = sum(A(i, :) .* B(:, j)');
    end
end

disp('Matrix multiplication completed.');

Explanation

  • The outer loop is parallelized using parfor.
  • Each worker computes a row of the result matrix C.

Common Mistakes and Tips

  1. Race Conditions: Ensure that parallel loops do not have dependencies between iterations.
  2. Overhead: Parallel computing introduces overhead. Use it for large computations to see performance gains.
  3. Memory Usage: Be mindful of memory usage, as each worker consumes memory.

Conclusion

In this section, you learned the basics of parallel computing in MATLAB, including setting up a parallel environment, using parfor loops, and leveraging spmd for distributed computing. You also practiced parallel matrix multiplication. In the next module, you will explore applications and projects that utilize these advanced techniques.

© Copyright 2024. All rights reserved