Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental prime sieves #438

Draft
wants to merge 93 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
4015fbc
Initial Commit
mborland Jul 9, 2020
3ee737b
Changed init of least_divisors
mborland Jul 10, 2020
9512bb6
Cleanup
mborland Jul 10, 2020
d762398
Cleanup
mborland Jul 10, 2020
5375a1d
Added additional tests, benchmarks, and overflow checks
mborland Jul 10, 2020
a684dbd
Fix include guard naming
mborland Jul 10, 2020
3e4db8a
Complete revamp of algorithm. Hide implementation behind detail names…
mborland Jul 12, 2020
dd8a61c
Re-added support and tests for boost::multiprecision::cpp_int [CI SKIP]
mborland Jul 12, 2020
4debd0d
Changed benchmarks to support threading [CI SKIP]
mborland Jul 13, 2020
2a7e031
Added execution policies. Increased performance for dynamically linke…
mborland Jul 14, 2020
a68910e
Added massively parallel section to prime_sieve. Increased length of …
mborland Jul 14, 2020
386cc4d
Add test for multi-threading section and add to Jamfile [CI SKIP]
mborland Jul 15, 2020
d79eddb
Added prime sieve to existing prime numbers documentation [CI SKIP]
mborland Jul 15, 2020
3fdf917
Revisions to documentation and send to CI
mborland Jul 15, 2020
6ca245b
Changed include guards to be compatible with C++11 and 14.
mborland Jul 15, 2020
7d3a520
Fixed doc, and ensured that primes are sorted
mborland Jul 15, 2020
a1ac504
Fixed documentation. Complete re-design of mask_sieve algo. Pre-gener…
mborland Jul 16, 2020
35d2aa1
All vectors now init {}. Change include guards. Replace raw pointer.
mborland Jul 17, 2020
243a299
Changed from [lower_bound, upper_bound] to [lower_bound, upper_bound)…
mborland Jul 18, 2020
2eadeff
Fixed documentation [CI SKIP]
mborland Jul 18, 2020
173ce0d
segmented_sieve now runs using std::async. [CI SKIP]
mborland Jul 18, 2020
23fba36
Removed extraneous operations [CI SKIP]
mborland Jul 19, 2020
f7b45fd
Cleanup style, and delete unused function. Enable par_unseq mask_siev…
mborland Jul 19, 2020
d687d5e
Small Cleanup and limit reduction for sieving methods.
mborland Jul 20, 2020
e51d727
Add pritchard's sub-linear algo [WIP][CI SKIP]
mborland Jul 25, 2020
1245d27
Added pritchards segmented sieve [WIP][CI SKIP]
mborland Jul 26, 2020
2052081
Implemented binary search in SetS [WIP][CI SKIP]
mborland Jul 27, 2020
9f81e0d
Tests and performance compairsons [WIP][CI SKIP]
mborland Jul 27, 2020
55ea045
More tests. Segmented sieve small cases fix
mborland Jul 27, 2020
31a2105
Imporve segmented performance [WIP][CI SKIP]
mborland Jul 29, 2020
f545928
Replaced SetS members with stl algos [WIP][CI SKIP]
mborland Jul 29, 2020
d780db5
Replace searching with tracked index. General performance improvement…
mborland Jul 31, 2020
2639bed
Minor change to SetS remove [WIP][CI SKIP]
mborland Aug 1, 2020
94fc1ac
Pritchard segmented performance improvements [WIP][CI SKIP]
mborland Aug 1, 2020
6ebb906
Fixed failed test [WIP][CI SKIP]
mborland Aug 1, 2020
0521854
Various performance improvements [WIP][CI SKIP]
mborland Aug 2, 2020
b233a80
Build primes in situ [WIP][CI SKIP]
mborland Aug 2, 2020
f95c2cf
Refactoring. Now requires C++17
mborland Aug 2, 2020
8e2e29a
Add segmented bit sieve [WIP][CI SKIP]
mborland Aug 4, 2020
1a24f16
Removed segmented bit sieve and excess headers [CI SKIP]
mborland Aug 6, 2020
c63f1f1
Added wheel class [WIP][CI SKIP]
Aug 15, 2020
b7d4256
Added fixed mod 210 wheel [WIP][CI SKIP]
Aug 23, 2020
fbc38c8
New segmented sieve algorithm [WIP][CI SKIP]
Aug 23, 2020
2e46b81
Added interval sieve to performance test [CI SKIP]
Aug 23, 2020
6759ede
Added unit tests [WIP][CI SKIP]
Aug 23, 2020
1b40403
Implemented interval sieve [CI SKIP]
Aug 23, 2020
97244be
Performance improvements and bug fixes [CI SKIP]
Aug 24, 2020
fa04133
Significant refactoring [WIP][CI SKIP]
Aug 25, 2020
c4a89c8
Seq policy actually sequential [CI SKIP]
Aug 25, 2020
91836f6
Fixes for multiprecision and policies [CI SKIP]
Aug 25, 2020
6d6b19f
cpp_int now passes all tests [CI SKIP]
Aug 27, 2020
81e4a6c
mpz_int passes unit tests [CI SKIP]
Aug 27, 2020
0b8f1d5
Documentation edits [CI SKIP]
mborland Aug 30, 2020
5ba0a1d
Replace magic number w variable template [CI SKIP]
mborland Aug 31, 2020
8e240f7
Better prime_range implementation [WIP][CI SKIP]
mborland Aug 31, 2020
7c0cbbf
Merge branch 'develop' into prime_functions
mborland Sep 1, 2020
3d9b77c
Minor changes and doc updates
mborland Sep 2, 2020
302fb5f
Merge branch 'develop' into prime_functions
mborland Sep 5, 2020
9792a23
Minor change to policy handling [CI SKIP]
mborland Sep 6, 2020
84a69f0
diffs from @jzmaddock [CI SKIP]
mborland Sep 8, 2020
5dc3523
Implemented linear sieve with iterators
mborland Sep 9, 2020
0dbe69c
Sanitize linear prime sieve and add wheel
mborland Sep 9, 2020
eee2c86
Added container method
mborland Sep 26, 2020
f5d789a
Improved Linear Algo and testing
mborland Sep 27, 2020
f2277e3
Merge branch 'prime_iterator' into mborland/prime_iterator
mborland Sep 27, 2020
1d2f03c
Linear sieve with output iterator and refactoring [CI SKIP][WIP]
mborland Sep 27, 2020
0d9d31b
Merge branch 'prime_functions' into mborland/prime_functions
mborland Sep 27, 2020
c361cde
Linear output iterator and refactoring [CI SKIP]
mborland Sep 27, 2020
66c2642
Added prime approximation function
mborland Sep 27, 2020
de1f331
prime approximation function for a range
mborland Sep 27, 2020
eaea5f9
Added interval sieve for output iterators
mborland Sep 27, 2020
e8196f3
Merge remote-tracking branch 'origin/develop' into prime_iterator
mborland Sep 27, 2020
58a1c7a
Merge branch 'prime_functions' of https://github.com/mborland/math in…
jzmaddock Sep 27, 2020
44ae862
Experimental prime_sieve.
jzmaddock Sep 27, 2020
cb5d978
Interval sieve with OI passes unit tests [CI SKIP]
mborland Sep 27, 2020
830ccc4
Merge branch 'prime_iterator' into prime_functions
mborland Sep 27, 2020
4dc3eb2
Add sequential composite sieve with OI [CI SKIP]
mborland Sep 30, 2020
90be100
Add threaded method. Currently INOP [CI SKIP]
mborland Sep 30, 2020
a772782
Threaded method completed and validated [CI SKIP]
mborland Oct 1, 2020
2d1461f
Resolves issue #439 [CI SKIP]
mborland Oct 1, 2020
e7cdb32
Updated benchmarks [CI SKIP]
mborland Oct 2, 2020
24a1e6c
Merge branch 'prime_functions' of https://github.com/mborland/math in…
jzmaddock Oct 2, 2020
263c7a2
Add experimental threaded support.
jzmaddock Oct 2, 2020
29eef88
Correct benchmark memory allocation [CI SKIP]
mborland Oct 3, 2020
b5a28b5
Add linear sieve direct from stepanov [CI SKIP]
mborland Oct 4, 2020
6c26b53
First draft of eratosthenes w/ wheel [CI SKIP]
mborland Oct 4, 2020
980bfe7
Fixes for multiprecision types [CI SKIP]
mborland Oct 4, 2020
07e6f58
wheel sieve passes standard battery [CI SKIP]
mborland Oct 4, 2020
86b9e5a
Add prime sieve wrapper [CI SKIP]
mborland Oct 5, 2020
f19149e
Implement dual interface for prime sieve [CI SKIP]
mborland Oct 7, 2020
1ad0d51
Implement dual interface for prime_range [CI SKIP]
mborland Oct 7, 2020
ae08996
Merge branch 'prime_functions' of https://github.com/mborland/math in…
jzmaddock Oct 13, 2020
157a5a8
Fix some GCC performance issues in jm::prime_sieve.
jzmaddock Oct 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ inspect.html
test/cuda
*.DS_Store
/**/*.dSYM/
build/*
*.json

4 changes: 2 additions & 2 deletions doc/math.qbk
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[book Math Toolkit
[quickbook 1.7]
[copyright 2006-2020 Nikhar Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle Walker and Xiaogang Zhang]
[copyright 2006-2020 Nikhar Agrawal, Anton Bikineev, Matthew Borland, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle Walker and Xiaogang Zhang]
[/purpose ISBN 0-9504833-2-X 978-0-9504833-2-0, Classification 519.2-dc22]
[license
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
[@http://www.boost.org/LICENSE_1_0.txt])
]
[authors [Agrawal, Nikhar], [Bikineev, Anton], [Bristow, Paul A.], [Holin, Hubert], [Guazzone, Marco], [Kormanyos, Christopher], [Lalande, Bruno], [Maddock, John], [Miller, Evan], [Murphy, Jeremy W.], [Pulver, Matthew], [Råde, Johan], [Sobotta, Benjamin], [Sewani, Gautam], [Thompson, Nicholas], [van den Berg, Thijs], [Walker, Daryle], [Zhang, Xiaogang]]
[authors [Agrawal, Nikhar], [Bikineev, Anton], [Borland, Matthew], [Bristow, Paul A.], [Holin, Hubert], [Guazzone, Marco], [Kormanyos, Christopher], [Lalande, Bruno], [Maddock, John], [Miller, Evan], [Murphy, Jeremy W.], [Pulver, Matthew], [Råde, Johan], [Sobotta, Benjamin], [Sewani, Gautam], [Thompson, Nicholas], [van den Berg, Thijs], [Walker, Daryle], [Zhang, Xiaogang]]
[/last-revision $Date$]
[version 2.12.0]
]
Expand Down
81 changes: 79 additions & 2 deletions doc/sf/number_series.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,86 @@ Passing a value greater than `max_prime` results in a __domain_error being raise

This function is `constexpr` only if the compiler supports C++14 constexpr functions.

[endsect] [/section:primes]
[endsect] [/section:primes Prime Numbers]

[endsect] [/Number Series]
[section:prime_sieve Prime Sieve]

[h4 Synopsis]

``
#include <boost/math/special_functions/prime_sieve.hpp>
``

namespace boost { namespace math {

template<class ExecutionPolicy, typename Integer, class Container>
void prime_sieve(ExecutionPolicy&& policy, Integer upper_bound, Container &resultant_primes)

template<class ExecutionPolicy, class Integer, class Container>
void prime_range(ExecutionPolicy&& policy, Integer lower_bound, Integer upper_bound, Container &resultant_primes)

template<class Integer, class Container>
void prime_sieve(Integer upper_bound, Container &resultant_primes)

template<class Integer, class Container>
void prime_range(Integer lower_bound, Integer upper_bound, Container &resultant_primes)

template<class Integer>
constexpr void prime_reserve(Integer upper_bound, std::vector<Integer> &prime_container)


}} // End namespaces

[h4 Description]

There are two sets of prime sieveing functions available: `prime_sieve` and `prime_range`.
`prime_sieve` will return all primes in the range [2, `upper_bound`).
`prime_range` will return all primes in the range [lower_bound, upper_bound).
Both `prime_sieve` and `prime_range` can perform arbitrary precision calculations using `boost::multiprecision::cpp_int` or `boost::multiprecision::mpz_int`.

`prime_sieve` and `prime_range` both have a parameter for an execution policy.
The policies `std::execution::par` or `std::execution::par_unseq` will enable internal multi-threading.
All other policies or no policy will result in the `prime_sieve` and `prime_range` executing sequentially.

`prime_reserve` uses the prime number theorem to reserve for approximately the correct number of primes given `upper_bound`.

[h4 Examples]
// To reserve space and calculate primes [2, 1,000,000) in parallel
std::vector<int64_t> primes;
boost::math::prime_reserve(1'000'000, primes);
boost::math::prime_sieve(std::execution::par, 1'000'000, primes);

// To calculate primes [100, 1,000) sequentially
std::vector<int64_t> primes;
boost::math::prime_range(100, 1'000, primes);

[h4 Complexity]
These functions were tested for complexity using [@https://github.com/google/benchmark google benchmark] on a system with the following specifications:
* CPU: Intel i5-10500
* OS: Red Hat Enterprise Linux 8.2
* Compiler: gcc-g++ 10.2.0 (Compiled from source with GMP 6.2.0, MPC 1.2.0, and MPFR 4.1.0 using RHEL gcc-toolset-9)
* Compiler flags: -Ofast -MMD -march=native -g -lbenchmark -lpthread -lgmp

Range of test upper_bound: 2[super 1] - 2[super 30]

[pre'''
[table:id Complexity Calculations `bigo[](N)`
[[Type] [Time Complexity] [Time Complexity RMS] [CPU Complexity] [CPU Complexity RMS]]
[[int32_t] [0.72N] [5%] [0.02N] [13%]]
[[int64_t] [0.78N] [16%] [0.03N] [6%]]
[[uint32_t] [0.71N] [10%] [0.02N] [14%]]
[[cpp_int] [6.24N] [44%] [0.35N] [4%]]
[[mpz_int] [9.27N] [11%] [0.69N] [8%]]
]
''']

[h4 References]
* Sorensen, Jonathan [@https://research.cs.wisc.edu/techreports/1990/TR909.pdf An Introduction to Prime Number Sieves]
* Gries, David and Misra, Jayadev [@https://www.cs.utexas.edu/users/misra/scannedPdf.dir/linearSieve.pdf A Linear Sieve Algorithm for Finding Prime Numbers]

[endsect] [/section:primes Prime Sieve]

[endsect] [/section:number_series Number Series]

[/
Copyright 2013, 2014 Nikhar Agrawal, Christopher Kormanyos, John Maddock, Paul A. Bristow.
Expand Down
Loading