Sample Construction of Sparse Matrix in Parallel

SPARSE_PARFOR is 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.

This script is a demonstration of how one can take advantage of MATLAB's parallel programming features when constructing a sparse matrix.

We construct a rectangular sparse matrix. This matrix can be thought of as a diagonal block matrix, with each diagonal block being of arbitrary size and shape.

In this example, it is assumed that the computation of the individual matrix elements is expensive, and worth doing in parallel.

On the other hand, once the elements have been computed, the "sparse()" command is called on a single processor to "assemble" the computed matrix elements into a single MATLAB sparse matrix data structure.

This problem was posed by Vitor Nunes, 18 August 2011.

Notice that there are many ways to set up a matrix, which seem reasonable, but which MATLAB's parfor command will not accept, because the matrix is not properly ``sliced''. By using cell arrays, this example is able to divide the problem into indexed segments (blocks) that the parfor and sparse commands can usefully process.


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


SPARSE_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 program 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_PARFOR, a MATLAB program which carries out a molecular dynamics simulation, running in parallel using MATLAB's "PARFOR" feature.

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.

ST, a data directory which contains examples of the Sparse Triplet format for storing sparse matrices. This format is equivalent to the form in which sparse matrix data is passed into MATLAB's sparse command (although the sparse compressed column format is used internally).


Gene Cliff, Virginia Tech


  1. John Gilbert, Cleve Moler, Robert Schreiber,
    Sparse Matrices in MATLAB: Design and Implementation,
    SIAM Journal on Matrix Analysis and Applications,
    Volume 13, Number 1, 1992, pages 333-356.
  2. Gaurav Sharma, Jos Martin,
    MATLAB: A Language for Parallel Computing,
    International Journal of Parallel Programming,
    Volume 37, Number 1, pages 3-36, February 2009.
  3. The MathWorks,
    User's Guide for the Parallel Computing Toolbox,

Source Code:

Examples and Tests:

SPARSE_POOL executes the function locally and interactively.

SPARSE_BATCH executes the function locally and noninteractively.

SPARSE_FSU executes the function on the FSU HPC cluster, noninteractively.

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

Last revised on 29 August 2011.