## Documentation Center |

On this page… |
---|

Run Parallel for-Loops (parfor) |

Many applications involve multiple segments of code, some of which are repetitive. Often you can use for-loops to solve these cases. The ability to execute code in parallel, on one computer or on a cluster of computers, can significantly improve performance in many cases:

Parameter sweep applications

Many iterations — A sweep might take a long time because it comprises many iterations. Each iteration by itself might not take long to execute, but to complete thousands or millions of iterations in serial could take a long time.

Long iterations — A sweep might not have a lot of iterations, but each iteration could take a long time to run.

Typically, the only difference between iterations is defined by different input data. In these cases, the ability to run separate sweep iterations simultaneously can improve performance. Evaluating such iterations in parallel is an ideal way to sweep through large or multiple data sets. The only restriction on parallel loops is that no iterations be allowed to depend on any other iterations.

Test suites with independent segments — For applications that run a series of unrelated tasks, you can run these tasks simultaneously on separate resources. You might not have used a

`for`-loop for a case such as this comprising distinctly different tasks, but a`parfor`-loop could offer an appropriate solution.

Parallel Computing Toolbox™ software improves the performance
of such loop execution by allowing several MATLAB^{®} workers to
execute individual loop iterations simultaneously. For example, a
loop of 100 iterations could run on a cluster of 20 MATLAB workers,
so that simultaneously, the workers each execute only five iterations
of the loop. You might not get quite 20 times improvement in speed
because of communications overhead and network traffic, but the speedup
should be significant. Even running local workers all on the same
machine as the client, you might see significant performance improvement
on a multicore/multiprocessor machine. So whether your loop takes
a long time to run because it has many iterations or because each
iteration takes a long time, you can improve your loop speed by distributing
iterations to MATLAB workers.

When working interactively in a MATLAB session, you can offload work to a MATLAB worker session to run as a batch job. The command to perform this job is asynchronous, which means that your client MATLAB session is not blocked, and you can continue your own interactive session while the MATLAB worker is busy evaluating your code. The MATLAB worker can run either on the same machine as the client, or if using MATLAB Distributed Computing Server™, on a remote cluster machine.

If you have an array that is too large for your computer's memory, it cannot be easily handled in a single MATLAB session. Parallel Computing Toolbox software allows you to distribute that array among multiple MATLAB workers, so that each worker contains only a part of the array. Yet you can operate on the entire array as a single entity. Each worker operates only on its part of the array, and workers automatically transfer data between themselves when necessary, as, for example, in matrix multiplication. A large number of matrix operations and functions have been enhanced to work directly with these arrays without further modification; see MATLAB Functions on Distributed and Codistributed Arrays and Using MATLAB Constructor Functions.

Was this topic helpful?