prime_openmp


prime_openmp, a C++ code which counts the number of primes between 1 and N, using OpenMP to carry out the calculation in parallel.

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.

This program is mainly a starting point for investigations into parallelization.

Here are the counts of the number of primes for some selected values of N:
NPi(N), Number of Primes
1 0
2 1
4 2
8 4
16 6
32 11
64 18
128 31
256 54
512 97
1024 172
2048 309
4096 564
8192 1028
16384 1900
32768 3512
65536 6542
131072 12251

Usage:

In the BASH shell, the program could be run with 2 threads using the commands:

        export OMP_NUM_THREADS=2
        ./prime_openmp
      

Licensing:

The information on this web page is distributed under the MIT license.

Languages:

prime_openmp is available in a C version and a C++ version and a Fortran90 version.

Related Data and Programs:

prime_openmp_test

openmp_test, a C++ code which uses the OpenMP application program interface for parallel computations in a shared memory environment.

prime, a C++ code which counts the number of primes between 1 and N, intended as a starting point for the creation of a parallel version.

prime_mpi, a C++ code which counts the number of primes between 1 and N, using MPI for parallel execution.

Reference:

  1. Eratosthenes,
    A Method For Finding Prime Numbers,
    Papyrus 487.
  2. Michael Quinn,
    Parallel Programming in C with MPI and OpenMP,
    McGraw-Hill, 2004,
    ISBN13: 978-0071232654,
    LC: QA76.73.C15.Q55.

Source Code:


Last revised on 01 April 2020.