# COLLATZ_PARFOR A Parallel MATLAB Function Using PARFOR

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

A Collatz sequence starting at the integer I computes the next term by halving the number if it is even, or tripling and adding 1 if it is odd. The sequence stops when the value 1 is reached.

The number of steps required to reach 1 varies greatly. No formula has been determined, and in fact, it hasn't actually been proved that the value of 1 will always be reached.

An interesting question, suitable for parallel execution, is to ask for the length of the longest Collatz sequence between 1 and N.

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

• interactively, and locally, using the matlabpool command;
• indirectly, and locally, using the batch command;
• indirectly, and on the VT Ithaca cluster, using the batch command;
• indirectly, and on the FSU HPC cluster, using the fsuClusterMatlab command;

### Usage:

The basic calculation is done by collatz_fun and has the form:

j_max = collatz_fun ( n )
where
• n is the largest integer to check.
• j_max is the longest sequence encountered.

### Languages:

COLLATZ_PARFOR is available in a MATLAB version.

### Related Data and Programs:

COLLATZ, a MATLAB library which computes and analyzes the Collatz sequence (or "hailstone" sequence or "3n+1 sequence");

COLLATZ_RECURSIVE, a MATLAB library which demonstrates recursive programming by considering the simple Collatz 3n+1 problem.

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_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.

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.

### Reference:

The User's Guide for the Parallel Computing Toolbox is available at http://www.mathworks.com/access/helpdesk/help/pdf_doc/distcomp/distcomp.pdf

1. Gaurav Sharma, Jos Martin,
MATLAB: A Language for Parallel Computing,
International Journal of Parallel Programming,
Volume 37, Number 1, pages 3-36, February 2009.
2. Michael Quinn,
Parallel Programming in C with MPI and OpenMP,
McGraw-Hill, 2004,
ISBN13: 978-0071232654,
LC: QA76.73.C15.Q55.
3. Eric Weisstein,
"The Collatz Problem",
CRC Concise Encyclopedia of Mathematics,
CRC Press, 2002,
Second edition,
ISBN: 1584883472,
LC: QA5.W45.

### Examples and Tests:

COLLATZ_POOL executes the function locally and interactively.

COLLATZ_BATCH executes the function locally and noninteractively.

COLLATZ_BATCH_ITHACA executes the function remotely on the Virginia Tech Ithaca cluster.

• collatz_batch_ithaca.m a script which uses the BATCH command to run the function indirectly and remotely, on the Virginia Tech Ithaca cluster.

COLLATZ_FSU executes the function on the FSU HPC cluster.

• collatz_fsu.m a script which uses the fsuClusterMatlab command to run the function indirectly on the FSU HPC cluster.