Learn about MATLAB ® and Parallel Computing Toolbox™ Choose a Parallel Computing Solution. Discover the most important functionalities offered by MATLAB and Parallel Computing Toolbox to solve your parallel computing problem. Interactively Run a Loop in Parallel Using parfor. Convert a slow for-loop into a faster parfor-loop. Run Batch Parallel Jobs. Use batch to offload work from your. Parallel Computing Toolbox enables you. Scale Parallel MATLAB Applications. There are many ways to start using Parallel Computing Toolbox. Download a free.

  1. Matlab Toolbox List
  2. Matlab Parallel Computing Toolbox Guide
  3. Matlab Parallel Cloud
Active8 years, 10 months ago

I am working on a time series based calculation. Each iteration of the calculation is independent. Could anyone share some tips / online primers on using utilising parallel processing in Matlab? How can this be specified inside the actual code?

3263 gold badges5 silver badges11 bronze badges

3 Answers

Since you have access to the Parallel toolbox, I suggest that you first check whether you can do it the easy way.

Basically, instead of writing

You write


Then, you use matlabpool to create a number of workers (you can have a maximum of 8 on your local machine with the toolbox, and tons on a remote cluster if you also have a Distributed Computing Server license), and you run the code, and see nice speed gains when your iterations are run by 8 cores instead of one.

Even though the parfor route is the easiest, it may not work right out of the box, since you might do your indexing wrong, or you may be referencing an array in a problematic way etc. Look at the mlint warnings in the editor, read the documentation, and rely on good old trial and error, and you should figure it out reasonably fast. If you have nested loops, it's often best parallelize only the innermost one and ensure it does tons of iterations - this is not only good design, it also reduces the amount of code that could give you trouble.

Note that especially if you run the code on a local machine, you may run into memory issues (which might manifest in really slow execution in parallel mode because you're paging): Every worker gets a copy of the workspace, so if your calculation involves creating a 500MB array, 8 workers will need a total 4GB of RAM - and then you haven't even started counting the RAM of the parent process! In addition, it can be good to only use N-1 cores on your machine, so that there is still one core left for other processes that may run on the computer (such as a mandatory antivirus..).

72k9 gold badges127 silver badges170 bronze badges

Mathworks offers its own parallel computing toolbox. If you do not want to purchase that, there a few options

  • You could write your own mex file and use pthreads or OpenMP.
  • However make sure you do not call any Mex api in the parallel part of the code, because they arent thread safe
  • If you want coarser grained parallelism via MPI you can try pmatlab
  • Same with parmatlab

Edit: Adding link Parallel MATLAB with openmp mex files

I have only tried the first.


Don't forget that many Matlab functions are already multithreaded. By careful programming you may be able to take advantage of them -- check the documentation for your version as the Mathworks seem to be increasing the range and number of multithreaded functions with each new release. For example, it seems that 2010a has multithreaded ffts which may be useful for time series processing.

If the intrinsic multithreading is not what you need, then as @srean suggests, the Parallel Computing Toolbox is available. For my money (or rather, my employers' money) it's the way to go, allowing you to program in parallel in Matlab, rather than having to bolt things on. I have to admit, too, that I'm quite impressed by the toolbox and the facilities it offers.

High Performance MarkHigh Performance Mark
70.3k7 gold badges88 silver badges138 bronze badges

Not the answer you're looking for? Browse other questions tagged matlabparallel-processing or ask your own question.

From FarmShare

Jump to: navigation, search
  • 2parallel matlab
    • 2.1batch
    • 2.2PCT (Parallel Computing Toolbox aka matlabpool)


  • CLI options for MATLAB: http://www.mathworks.com/help/matlab/ref/matlabunix.html

parallel matlab

Parallel matlab comes in two forms.

  • Batch style where many matlab jobs are submitted and run on the Barley cluster. foo is an example of this.
  • Parallelism within matlab by use of matlabpools and parallel matlab constructs such as parfor. See below for an example.

Of course, these two can be combined to run many batch jobs, each with a matlabpool of workers.


example single matlab file run via qsub

Here's our helloworld.m:

Here's a command to run that non-interactively. (note we pass -singleCompThread for best performance):

We want to run this same command via the job scheduling system. Let's write a job script (save this as matlab_example.script)

Submit the script:

Look at the job status:

You should get output file like matlab_example.oXXXXX

example second matlab file run via qsub

Now we want to make sure we avoid AFS.

Here's our helloworld.m:

Here's a command to run that non-interactively:

We want to run this same command via the job scheduling system. Let's write a job script.

Submit the script:

Look at the job status:

You should get output file like matlab_example.oXXXXX in your current directory

PCT (Parallel Computing Toolbox aka matlabpool)

We have the Parallel Computing Toolbox, you can use that to parallelize your job across multiple cores in a single machine.

Here's how to write a job using MDCS: http://docs.uabgrid.uab.edu/wiki/MatLab_CLI#Parallel_MATLAB

You can use the 'maxNumCompThreads' command (deprecated) to see how many parallel threads you can run. I get '24' on barley, or '8' on corn.

simple PCT run on corn

matlabpool of size 0 and size 1 are effectively the same, except the latter uses a PCT toolbox license.

Here are some training slides and example code that I copied from http://www.osc.edu/~samsi/sc11edu/

batch matlab with matlabpools on the barley

A good way to speed up your matlab code on barley is to submit your matlab job with the shm parallel environment. The parallel environment is specified with -pe shm 4 below. The number you specify after shm will be the number of cores allocated to your job (all from the same host). In this case we are asking for 4 cores, which then become workers in a matlab pool.

There is a problem that frequently occurs with matlab if you run many jobs which all use matlab pools. Matlab is naive and does not handle having multiple instances running at the same time. To allow many matlabpools to be run simultaneously, use the following code to setup your pool. You may substitute any number between 1 and 12 for the number of cores (12 being the max matlab will let us use with current license).

To see if the matlabpools are running correctly, we will submit some jobs and check the output. In this case, its worth noting that 6 jobs ran at same time on 6 different hosts. They all ran to completion and gave same output.

Matlab Toolbox List

cat matlabfft.o1232753


The example above shows how to submit a job in grid engine to run MATLAB on multiple cores but on one compute node.

There is also a way to write MATLAB code to split work across multiple machines simultaneously. The example code is in MATLAB help for 'parallel.Cluster/createJob.'. Please contact us if your code looks like the example there and we can help set up the MATLAB cluster parameters for your parcluster. It needs to be configured to run under a 'generic scheduler' and your code needs to define the tasks and jobs.

We can use the 'shared' example code from here: https://www.mathworks.com/matlabcentral/fileexchange/52816-parallel-computing-toolbox-integration-for-matlab-distributed-computing-server-with-grid-engine

We have a TOMLAB license. To start it up:

  1. launch matlab r2013a
  2. cd to /farmshare/software/non-free/MATLAB-R2013a/tomlab
  3. startup

Same with R2013b, inside matlab:

example session:

Matlab Parallel Computing Toolbox Guide

Search the farmshare-discuss archives for posts about Matlab.

Matlab Parallel Cloud

Retrieved from 'https://web.stanford.edu/group/farmshare/cgi-bin/wiki/index.php/Matlab-parallel'