-
Notifications
You must be signed in to change notification settings - Fork 248
Prime Number Functions #400
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
4015fbc
3ee737b
9512bb6
d762398
5375a1d
a684dbd
3e4db8a
dd8a61c
4debd0d
2a7e031
a68910e
386cc4d
d79eddb
3fdf917
6ca245b
7d3a520
a1ac504
35d2aa1
243a299
2eadeff
173ce0d
23fba36
f7b45fd
d687d5e
e51d727
1245d27
2052081
9f81e0d
55ea045
31a2105
f545928
d780db5
2639bed
94fc1ac
6ebb906
0521854
b233a80
f95c2cf
8e2e29a
1a24f16
c63f1f1
b7d4256
fbc38c8
2e46b81
6759ede
1b40403
97244be
fa04133
c4a89c8
91836f6
6d6b19f
81e4a6c
0b8f1d5
5ba0a1d
8e240f7
7c0cbbf
3d9b77c
302fb5f
9792a23
84a69f0
5dc3523
0dbe69c
eee2c86
f5d789a
f2277e3
1d2f03c
0d9d31b
c361cde
66c2642
de1f331
eaea5f9
e8196f3
cb5d978
830ccc4
4dc3eb2
90be100
a772782
2d1461f
e7cdb32
29eef88
b5a28b5
6c26b53
980bfe7
07e6f58
86b9e5a
f19149e
1ad0d51
5f6d06a
670b06d
7c7a491
e507ba5
c9cb41c
e8b71a0
d649f65
ed98892
c3b3934
e9aa05d
7c5d792
2bbfad2
ddc5c8a
f517c00
a356b47
2212e45
b12e2dc
f39a4d8
eafbefc
cb36b89
16c2354
0b1a690
8c883d1
f357e0f
a4f2d89
d16e562
91be2c3
d9ae8c2
7d22010
b541987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,17 @@ | |
| #include <iterator> | ||
| #include <cmath> | ||
| #include <thread> | ||
| #include <memory> | ||
|
|
||
| #ifdef _MSVC_LANG | ||
| #if _MSVC_LANG >= 201703 // _MSVC_LANG == __cplusplus: https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/ | ||
| #include <execution> | ||
| #endif | ||
| #else | ||
| #if __cplusplus >= 201703 | ||
| #include <execution> | ||
| #endif | ||
| #endif | ||
|
|
||
| namespace boost { namespace math { namespace detail | ||
| { | ||
|
|
@@ -28,7 +35,7 @@ template<class Z, class Container> | |
| void linear_sieve(Z upper_bound, Container &c) | ||
|
||
| { | ||
| size_t least_divisors_size{static_cast<size_t>(upper_bound + 1)}; | ||
| Z *least_divisors{new Z[least_divisors_size]{0}}; | ||
| std::unique_ptr<Z[]> least_divisors{new Z[least_divisors_size]{0}}; | ||
|
||
|
|
||
| for (Z i{2}; i <= upper_bound; ++i) | ||
| { | ||
|
|
@@ -61,8 +68,6 @@ void linear_sieve(Z upper_bound, Container &c) | |
| } | ||
| } | ||
mborland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| delete[] least_divisors; | ||
| } | ||
|
|
||
| template<class Z, class Container> | ||
|
|
@@ -85,20 +90,14 @@ void mask_sieve(Z lower_bound, Z upper_bound, const PrimeContainer& primes, Cont | |
| { | ||
| Z limit {static_cast<Z>(std::floor(std::sqrt(static_cast<double>(upper_bound)))) + 1}; | ||
|
|
||
| size_t primes_size; | ||
| size_t primes_size {}; | ||
| auto it{primes.begin()}; | ||
| while(*it < limit && it != primes.end()) | ||
| while(it != primes.end() && *it < limit) | ||
| { | ||
| ++primes_size; | ||
| ++it; | ||
| } | ||
|
|
||
| // Needed for thread sanitizer. Throws FPE without this check. | ||
| if(primes_size > primes.size()) | ||
| { | ||
| primes_size = primes.size(); | ||
| } | ||
|
|
||
| // Faster and safer than std::vector<bool> which does not behave like all other vectors | ||
| const size_t n {static_cast<size_t>(upper_bound - lower_bound + 1)}; | ||
| bool* is_prime {new bool[n]}; | ||
|
|
@@ -132,7 +131,7 @@ template<typename Z, class Container> | |
| void mask_sieve(Z lower_bound, Z upper_bound, Container &c) | ||
| { | ||
| Z limit{static_cast<Z>(std::floor(std::sqrt(static_cast<double>(upper_bound)))) + 1}; | ||
| std::vector<Z> primes; | ||
| std::vector<Z> primes {}; | ||
| primes.reserve(limit / std::log(limit)); | ||
|
|
||
| boost::math::detail::linear_sieve(limit, primes); | ||
|
|
@@ -146,7 +145,7 @@ template <typename Z, class PrimesContainer, class Container> | |
| void segmented_sieve(Z lower_bound, Z upper_bound, const PrimesContainer &primes, Container &c) | ||
| { | ||
| const Z L1_SIZE {32648}; | ||
| const Z interval {static_cast<Z>(std::floor(L1_SIZE / sizeof(Z)))}; | ||
| const Z interval {L1_SIZE * 4}; | ||
| Z current_lower_bound{lower_bound}; | ||
| Z current_upper_bound{current_lower_bound + interval}; | ||
|
|
||
|
|
@@ -169,7 +168,7 @@ template <typename Z, class Container> | |
| void segmented_sieve(Z lower_bound, Z upper_bound, Container &c) | ||
| { | ||
| Z limit{static_cast<Z>(std::floor(std::sqrt(static_cast<double>(upper_bound)))) + 1}; | ||
| std::vector<Z> primes; | ||
| std::vector<Z> primes {}; | ||
| primes.reserve(limit / std::log(limit)); | ||
|
|
||
| // Prepare for max value so you do not have to calculate this again | ||
|
|
@@ -194,7 +193,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
| static_assert(std::is_integral<Z>::value, "No primes for floating point types"); | ||
| BOOST_ASSERT_MSG(upper_bound + 1 < std::numeric_limits<Z>::max(), "Type Overflow"); | ||
|
|
||
| std::vector<Z> primes; | ||
| std::vector<Z> primes {}; | ||
| primes.reserve(upper_bound / std::log(static_cast<double>(upper_bound))); | ||
|
|
||
| // Range for when the linear sieve is no longer faster than threading | ||
|
|
@@ -219,7 +218,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
| processor_count = 2; | ||
| } | ||
|
|
||
| std::vector<Z> small_primes; | ||
| std::vector<Z> small_primes {}; | ||
| small_primes.reserve(1000); | ||
|
|
||
| // Threshold for when 2 thread performance begins to be non-linear, or when the system can only support two threads | ||
|
|
@@ -243,7 +242,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
| { | ||
| //Pre-generate all of the primes so that each thread does not have to | ||
| Z limit{static_cast<Z>(std::floor(std::sqrt(static_cast<double>(upper_bound)))) + 1}; | ||
| std::vector<Z> pre_generated_primes; | ||
| std::vector<Z> pre_generated_primes {}; | ||
| pre_generated_primes.reserve(limit / std::log(limit)); | ||
|
|
||
| if(limit < 8192) | ||
|
|
@@ -265,7 +264,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
| pre_generated_primes.insert(pre_generated_primes.begin(), small_primes.begin(), small_primes.end()); | ||
| } | ||
|
|
||
| std::vector<std::thread> thread_manager; | ||
| std::vector<std::thread> thread_manager {}; | ||
| std::vector<std::vector<Z>> prime_vectors(processor_count - 1); | ||
| const Z range_per_thread = upper_bound / (processor_count - 1); | ||
| Z current_lower_bound {8192}; | ||
|
|
@@ -275,7 +274,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
|
|
||
| for(size_t i{1}; i < processor_count - 1; ++i) | ||
| { | ||
| std::vector<Z> temp; | ||
| std::vector<Z> temp {}; | ||
mborland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| temp.reserve(primes_in_range); | ||
| prime_vectors.emplace_back(temp); | ||
|
|
||
|
|
@@ -292,7 +291,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
| current_lower_bound / std::log(static_cast<double>(current_lower_bound))); | ||
| } | ||
|
|
||
| std::vector<Z> temp; | ||
| std::vector<Z> temp {}; | ||
| temp.reserve(primes_in_range); | ||
| prime_vectors.emplace_back(temp); | ||
|
|
||
|
|
@@ -320,7 +319,7 @@ auto prime_sieve(ExecutionPolicy&& policy, Z upper_bound, OutputIterator output) | |
| template<class ExecutionPolicy, class Z, class OutputIterator> | ||
| auto prime_range(ExecutionPolicy&& policy, Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) | ||
| { | ||
| std::vector<Z> primes; | ||
| std::vector<Z> primes {}; | ||
| primes.reserve(upper_bound / std::log(static_cast<double>(upper_bound))); | ||
|
|
||
| boost::math::prime_sieve(policy, upper_bound, std::back_inserter(primes)); | ||
|
|
@@ -341,7 +340,7 @@ auto prime_sieve(Z upper_bound, OutputIterator output) -> decltype(output) | |
| static_assert(std::is_integral<Z>::value, "No primes for floating point types"); | ||
| BOOST_ASSERT_MSG(upper_bound + 1 < std::numeric_limits<Z>::max(), "Type Overflow"); | ||
|
|
||
| std::vector<Z> primes; | ||
| std::vector<Z> primes{}; | ||
| primes.reserve(upper_bound / std::log(upper_bound)); | ||
|
|
||
| if (upper_bound <= 8192) | ||
|
|
@@ -351,7 +350,7 @@ auto prime_sieve(Z upper_bound, OutputIterator output) -> decltype(output) | |
|
|
||
| else | ||
| { | ||
| boost::math::detail::mask_sieve(static_cast<Z>(2), upper_bound, primes); | ||
| boost::math::detail::segmented_sieve(static_cast<Z>(2), upper_bound, primes); | ||
| } | ||
|
|
||
| return std::move(primes.begin(), primes.end(), output); | ||
|
|
@@ -360,7 +359,7 @@ auto prime_sieve(Z upper_bound, OutputIterator output) -> decltype(output) | |
| template<class Z, class OutputIterator> | ||
| auto prime_range(Z lower_bound, Z upper_bound, OutputIterator output) -> decltype(output) | ||
|
||
| { | ||
| std::vector<Z> primes; | ||
| std::vector<Z> primes{}; | ||
| primes.reserve(upper_bound / std::log(static_cast<double>(upper_bound))); | ||
|
|
||
| boost::math::prime_sieve(upper_bound, std::back_inserter(primes)); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.