PRIME_NUMBER_PARALLEL
Parallel Program to Count Primes


PRIME_NUMBER_PARALLEL is a MATLAB program which counts the number of primes between 1 and N.

The algorithm is completely naive. For each integer I, it simply checks whether any smaller J evenly divides it. The total amount of work for a given N is thus roughly proportional to 1/2*N^2.

There is very little memory traffic and no communication, so this program is a good test for the pure computational speedup offered by MATLAB's Parallel Programming Toolbox.

Here are the counts of the number of primes for some selected values of N:
NNumber of Primes
53
5015
50095
5,000669
50,0005,133
500,00041,538

On a single computer, using the MATLAB Parallel Toolbox, the following results were observed for the elapsed time using 1 client and 0, 1, 2 and 4 "labs" or "workers":
N1+01+11+21+4
500.0670.1790.1760.278
5000.0080.0230.0270.032
5,0000.1000.1420.0970.061
50,0007.6949.8115.2512.719
500,000609.764826.534432.233222.284

On the Ithaca cluster with the MATLAB Parallel Toolbox version 4.1, running in "local" mode, the following results were observed:
N1+01+11+21+41+8
5 0.000 0.325 0.270 0.338 0.363
50 0.000 0.039 0.043 0.083 0.285
500 0.002 0.046 0.038 0.045 0.083
5,000 0.125 0.126 0.108 0.096 0.075
50,000 6.335 6.537 3.425 2.243 1.164
500,000517.494530.206277.850178.036 91.553

On the Ithaca cluster with the MATLAB Parallel Toolbox version 4.1, running with the "ithaca" configuration across multiple nodes, the following results were observed:
N1+81+161+321+64
5 0.377 0.357 0.360 0.412
50 0.221 0.433 0.430 0.504
500 0.056 0.121 0.199 0.416
5,000 0.058 0.087 0.215 0.428
50,000 1.080 0.564 0.326 0.357
500,000 81.649 39.987 19.237 9.808

Licensing:

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

Related Data and Programs:

BIRTHDAY_REMOTE, a MATLAB program which runs a Monte Carlo simulation of the birthday paradox, and includes instructions on how to run the job, via MATLAB's BATCH facility, on a remote system such as Virginia Tech's ITHACA cluster.

COLLATZ_PARALLEL is a MATLAB program which seeks the maximum Collatz sequence between 1 and N; it runs in parallel using MATLAB's "parfor" facility.

FDI_OPT, a MATLAB program which demonstrates the use of MATLAB's FMINCON constrained minimization function, taking advantage of MATLAB's Parallel Computing Toolbox for faster execution.

JTB_CODIST, a MATLAB program which demonstrates how the linear system associated with a finite element problem can be treated as a codistributed array whose entries are assigned to different MATLAB workers, so that the matrix is assembled in parallel.

LINEAR_SOLVE_DISTRIBUTED is a MATLAB program which solves a linear system A*x=b using MATLAB's spmd facility, so that the matrix A is "distributed" across multiple MATLAB workers.

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

MD_PARALLEL is a MATLAB program which carries out a molecular dynamics simulation in parallel, using MATLAB's "PARFOR" feature.

PRIME_NUMBER is a MATLAB program which counts the number of primes between 1 and N.

PRIME_NUMBER_OPEN_MP is a C program which counts the number of primes between 1 and N, using OpenMP for parallel execution.

PRIME_NUMBER_SPMD is a MATLAB program which counts the number of primes between 1 and N; running in parallel using MATLAB's "SPMD" feature.

QUAD_SPMD is a MATLAB program which estimates an integral using quadrature; running in parallel using MATLAB's "SPMD" feature.

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

TIMING_PARALLEL is a directory of MATLAB programs which illustrates how to time a parallel MATLAB program.

Reference:

  1. Jeremy Kepner,
    Parallel MATLAB for Multicore and Multinode Computers,
    SIAM, 2009,
    ISBN13: 978-0-898716-73-3,
    LC: QA76.58.K46.
  2. The MathWorks,
    Parallel Computing Toolbox 4,
    User's Guide.

Source Code:

Examples and Tests:

PRIME_NUMBER_LOCAL_RUN runs the program on a single machine, in "local" mode. A sample use might be

prime_number_local_run ( 5, 500000, 10, 4 )
to run the program in local mode with 4 workers.

PRIME_NUMBER_ITHACA_RUN runs the program on the Ithaca cluster, in "interactive" mode. A sample use might be

prime_number_ithaca_run ( 5, 500000, 10, 16 )
to run the program with 16 workers.

PRIME_NUMBER_BATCH_RUN runs the program on the Ithaca cluster, using the "batch" command. A sample use might be

batch ( 'prime_number_batch_run.m', 'Configuration', 'ithaca', 'matlabpool', 4 )
where the 'ithaca' configuration must be set up and validated first.

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


Last revised on 22 April 2009.