RIZY101 / primecount

🚀 Fast prime counting function implementations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

primecount

Build Status Build Status Github Releases

primecount is a command-line program and C/C++ library that counts the primes below an integer x â‰¤ 1031 using highly optimized implementations of the combinatorial prime counting algorithms.

primecount includes implementations of all important combinatorial prime counting algorithms known up to this date all of which have been parallelized using OpenMP. primecount contains the first ever open source implementations of the Deleglise-Rivat algorithm and Xavier Gourdon's algorithm (that works). primecount also features a novel load balancer that is shared amongst all implementations and that scales up to hundreds of CPU cores. primecount has already been used to compute several prime counting function world records.

Build instructions

You need to have installed a C++ compiler and CMake. Ideally primecount should be compiled using a C++ compiler that supports both OpenMP and 128-bit integers (e.g. GCC, Clang, Intel C++ Compiler).

cmake .
make -j
sudo make install

Binaries

Below are the latest precompiled primecount binaries for Windows, Linux and macOS. These binaries are statically linked and require a CPU which supports the POPCNT instruction (2008 or later).

Usage examples

# Count the primes below 10^14
primecount 1e14

# Print progress and status information during computation
primecount 1e20 --status

# Count primes using Meissel's algorithm
primecount 2**32 --meissel

# Find the 10^14th prime using 4 threads
primecount 1e14 --nth-prime --threads=4 --time

Command-line options

Usage: primecount x [options]
Count the number of primes less than or equal to x (<= 10^31).

Options:

  -d, --deleglise-rivat  Count primes using the Deleglise-Rivat algorithm
  -g, --gourdon          Count primes using Xavier Gourdon's algorithm.
                         This is the default algorithm.
  -l, --legendre         Count primes using Legendre's formula
      --lehmer           Count primes using Lehmer's formula
      --lmo              Count primes using Lagarias-Miller-Odlyzko
  -m, --meissel          Count primes using Meissel's formula
      --Li               Approximate pi(x) using the logarithmic integral
      --Li-inverse       Approximate the nth prime using Li^-1(x)
  -n, --nth-prime        Calculate the nth prime
  -p, --primesieve       Count primes using the sieve of Eratosthenes
      --phi <X> <A>      phi(x, a) counts the numbers <= x that are not
                         divisible by any of the first a primes
      --Ri               Approximate pi(x) using Riemann R
      --Ri-inverse       Approximate the nth prime using Ri^-1(x)
  -s, --status[=NUM]     Show computation progress 1%, 2%, 3%, ...
                         Set digits after decimal point: -s1 prints 99.9%
      --test             Run various correctness tests and exit
      --time             Print the time elapsed in seconds
  -t, --threads=NUM      Set the number of threads, 1 <= NUM <= CPU cores.
                         By default primecount uses all available CPU cores.
  -v, --version          Print version and license information
  -h, --help             Print this help menu
Advanced options
Advanced options for the Deleglise-Rivat algorithm:

  -a, --alpha=NUM        Set tuning factor: y = x^(1/3) * alpha
      --P2               Compute the 2nd partial sieve function
      --S1               Compute the ordinary leaves
      --S2-trivial       Compute the trivial special leaves
      --S2-easy          Compute the easy special leaves
      --S2-hard          Compute the hard special leaves

Advanced options for Xavier Gourdon's algorithm:

      --alpha-y=NUM      Set tuning factor: y = x^(1/3) * alpha_y
      --alpha-z=NUM      Set tuning factor: z = y * alpha_z
      --AC               Compute the A + C formulas
      --B                Compute the B formula
      --D                Compute the D formula
      --Phi0             Compute the Phi0 formula
      --Sigma            Compute the 7 Sigma formulas

Benchmarks

x Prime Count Legendre Meissel Lagarias
Miller
Odlyzko
Deleglise
Rivat
Gourdon
1010 455,052,511 0.02s 0.01s 0.00s 0.00s 0.00s
1011 4,118,054,813 0.02s 0.01s 0.01s 0.01s 0.01s
1012 37,607,912,018 0.09s 0.05s 0.02s 0.01s 0.01s
1013 346,065,536,839 0.39s 0.19s 0.05s 0.03s 0.02s
1014 3,204,941,750,802 2.24s 0.96s 0.16s 0.12s 0.07s
1015 29,844,570,422,669 14.58s 5.96s 0.63s 0.38s 0.21s
1016 279,238,341,033,925 111.81s 42.91s 2.83s 1.59s 0.76s
1017 2,623,557,157,654,233 938.07s 352.39s 12.70s 4.72s 2.83s
1018 24,739,954,287,740,860 8,268.52s 3,144.72s 57.31s 18.96s 10.77s
1019 234,057,667,276,344,607 NaN NaN NaN 89.02s 45.56s
1020 2,220,819,602,560,918,840 NaN NaN NaN 385.61s 188.70s
1021 21,127,269,486,018,731,928 NaN NaN NaN 1,652.66s 800.57s
1022 201,467,286,689,315,906,290 NaN NaN NaN 7,355.69s 3,260.95s

The benchmarks above were run on a system with an Intel Xeon Platinum 8275CL CPU from 2019 using 8 CPU cores (16 threads) clocked at 3.00GHz. Note that Jan Büthe mentions in [11] that he computed pi(1025) in 40,000 CPU core hours using the analytic prime counting function algorithm. Büthe also mentions that by using additional zeros of the zeta function the runtime could have potentially been reduced to 4,000 CPU core hours. However using primecount and Xavier Gourdon's algorithm pi(1025) can be computed in only 460 CPU core hours on an AMD Ryzen 3950X CPU!

Performance tips

primecount uses the OpenMP multi-threading library. In OpenMP waiting threads are usually busy-waiting for a short amount of time (by spinning) before being put to sleep. This setting can be altered using the OMP_WAIT_POLICY environment variable. For primecount it is best to set OMP_WAIT_POLICY to PASSIVE in order to prevent the threads from busy waiting. This setting can provide a large speedup for small to medium sized computations below 1020.

export OMP_WAIT_POLICY=PASSIVE

By default primecount scales nicely up until 1020 on current x64 CPUs. For larger values primecount's large memory usage causes many TLB (translation lookaside buffer) cache misses that significantly deteriorate primecount's performance. Fortunately the Linux kernel allows to enable transparent huge pages so that large memory allocations will automatically be done using huge pages instead of ordinary pages which dramatically reduces the number of TLB cache misses.

# Enable transparent huge pages until next reboot
sudo bash -c 'echo always > /sys/kernel/mm/transparent_hugepage/enabled'

Algorithms

Legendre's Formula
Meissel's Formula
Lehmer's Formula
LMO Formula

Up until the early 19th century the most efficient known method for counting primes was the sieve of Eratosthenes which has a running time of operations. The first improvement to this bound was Legendre's formula (1830) which uses the inclusion-exclusion principle to calculate the number of primes below x without enumerating the individual primes. Legendre's formula has a running time of operations and uses space. In 1870 E. D. F. Meissel improved Legendre's formula by setting and by adding the correction term . Meissel's formula has a running time of operations and uses space. In 1959 D. H. Lehmer extended Meissel's formula and slightly improved the running time to operations and space. In 1985 J. C. Lagarias, V. S. Miller and A. M. Odlyzko published a new algorithm based on Meissel's formula which has a lower runtime complexity of operations and which uses only space.

primecount's Legendre, Meissel and Lehmer implementations are based on Hans Riesel's book [5], its Lagarias-Miller-Odlyzko and Deleglise-Rivat implementations are based on Tomás Oliveira's paper [9] and the implementation of Xavier Gourdon's algorithm is based on Xavier Gourdon's paper [7]. primecount's implementation of the special leaves formula is different from the algorithms that have been described in any of the combinatorial prime counting papers so far. Instead of using a binary indexed tree for counting which is very cache inefficient primecount uses a linear counters array in combination with the POPCNT instruction which is more cache efficient and much faster. The Special-Leaves.md document contains more information.

Fast nth prime calculation

The most efficient known method for calculating the nth prime is a combination of the prime counting function and a prime sieve. The idea is to closely approximate the nth prime (e.g. using the inverse logarithmic integral or the inverse Riemann R function ) and then count the primes up to this guess using the prime counting function. Once this is done one starts sieving (e.g. using the segmented sieve of Eratosthenes) from there on until one finds the actual nth prime. The author has implemented primecount::nth_prime(n) this way (option: --nth-prime), it finds the nth prime in operations using space.

C API

Include the <primecount.h> header to use primecount's C API. All functions that are part of primecount's C API return -1 in case an error occurs and print the corresponding error message to the standard error stream.

#include <primecount.h>
#include <stdio.h>

int main()
{
    int64_t pix = primecount_pi(1000);
    printf("primes below 1000 = %ld\n", pix);

    return 0;
}

C++ API

Include the <primecount.hpp> header to use primecount's C++ API. All functions that are part of primecount's C++ API throw a primecount_error exception (which is derived from std::exception) in case an error occurs.

#include <primecount.hpp>
#include <iostream>

int main()
{
    int64_t pix = primecount::pi(1000);
    std::cout << "primes below 1000 = " << pix << std::endl;

    return 0;
}

About

🚀 Fast prime counting function implementations

License:BSD 2-Clause "Simplified" License


Languages

Language:C++ 90.2%Language:Shell 4.4%Language:CMake 2.9%Language:C 1.8%Language:Roff 0.5%Language:QMake 0.2%