A Parallel MATLAB Function Using PARFOR

MD_PARFOR is a directory which illustrates how a MATLAB function using the PARFOR statement can be run in parallel.

The MATLAB program is a version of the MD molecular dynamics program. A single for statement in the compute function was replaced by a parfor. Also, the program was simplified so that it takes no input arguments (the former input quantities are hard-coded internally) and returns three numbers as function results, namely the values, at the final time step, of the potential energy, kinetic energy, and the relative energy error.

Thus, the basic function now has the form:

function [ pe, ke, e_lost ] = md_fun ( )
where and the function also prints out a summary table of these values every 50 time steps.

Depending on the situation, the function could be executed in parallel:


The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.


MD_PARFOR is available in a MATLAB version.

Related Data and Programs:

COLLATZ_PARFOR, a MATLAB program which seeks the maximum Collatz sequence between 1 and N, running in parallel using MATLAB's parfor feature.

HEATED_PLATE_PARFOR, a MATLAB program which solves the steady (time independent) heat equation in a 2D rectangular region, using MATLAB's parfor facility to run in parallel.

HELLO_PARFOR, a MATLAB program which prints out "Hello, world!" multiple times, using MATLAB's parfor command for parallel execution.

HIGH_CARD_PARFOR, a MATLAB program which uses the parfor statement to compute in parallel the statistics for a card game in which you are required to guess the location of the highest card.

MATLAB_PARALLEL, MATLAB programs which illustrate "local" parallel programming on a single computer with MATLAB's Parallel Computing Toolbox.

MATLAB_RANDOM_PARALLEL, MATLAB programs which illustrate the use of Matlab's random number generator (RNG) functions when using parallel features such as parfor or spmd.

MATRIX_ASSEMBLE_PARFOR, a MATLAB program which demonstrates the parfor parallel programming feature by assembling the Hilbert matrix in a parallel loop.

MD, a MATLAB program which carries out a molecular dynamics simulation, and is intended as a starting point for implementing a parallel version.

ODE_SWEEP_PARFOR, a MATLAB program which demonstrates how the PARFOR command can be used to parallelize the computation of a grid of solutions to a parameterized system of ODE's.

PRIME_PARFOR, a MATLAB program which counts the number of primes between 1 and N; running in parallel using MATLAB's "PARFOR" feature.

QUAD_PARFOR, a MATLAB program which estimates an integral using quadrature; running in parallel using MATLAB's "PARFOR" feature.

SATISFY_PARFOR, a MATLAB program which demonstrates, for a particular circuit, an exhaustive search for solutions of the circuit satisfiability problem, running in parallel using MATLAB's "PARFOR" feature.

SPARSE_PARFOR, a MATLAB library which demonstrates how a sparse matrix can be constructed by evaluating individual blocks in parallel with the parfor command, and then assembled (on a single processor) using the sparse() command.


The User's Guide for the Parallel Computing Toolbox is available at

Source Code:

Examples and Tests:

MD_POOL runs the function locally and interactively.

MD_BATCH runs the function locally and noninteractively.

MD_FSU runs the function remotely on the FSU HPC cluster.

MD_ITHACA runs the function remotely on the Virginia Tech Ithaca cluster.

You can go up one level to the MATLAB source codes.

Last revised on 23 May 2012.