Skip to content

Commit

Permalink
Tune next iterator distance
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Jan 29, 2019
1 parent f212a93 commit 944995e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
6 changes: 3 additions & 3 deletions include/primesieve/config.hpp
Expand Up @@ -2,7 +2,7 @@
/// @file config.hpp
/// @brief primesieve compile time constants.
///
/// Copyright (C) 2018 Kim Walisch, <kim.walisch@gmail.com>
/// Copyright (C) 2019 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
Expand Down Expand Up @@ -37,13 +37,13 @@ enum {
///
MAX_ALLOC_BYTES = (1 << 20) * 16,

/// primesieve::iterator caches at least MIN_CACHE_ITERATOR
/// iterator::prev_prime() caches at least MIN_CACHE_ITERATOR
/// bytes of primes. Larger is usually faster but also
/// requires more memory.
///
MIN_CACHE_ITERATOR = (1 << 20) * 8,

/// primesieve::iterator maximum cache size in bytes, used
/// iterator::prev_prime() maximum cache size in bytes, used
/// if pi(sqrt(n)) * 8 bytes > MAX_CACHE_ITERATOR.
///
MAX_CACHE_ITERATOR = (1 << 20) * 1024
Expand Down
45 changes: 22 additions & 23 deletions src/IteratorHelper.cpp
Expand Up @@ -31,41 +31,40 @@ uint64_t getNextDist(uint64_t n, uint64_t dist)
x = sqrt(x) / log(log(x));

uint64_t minDist = (uint64_t) x;
uint64_t limit = numeric_limits<uint64_t>::max() / 4;
dist = max(dist, minDist);
uint64_t maxDist = numeric_limits<uint64_t>::max() / 4;
uint64_t maxCachedPrime = PrimeGenerator::maxCachedPrime();

if (dist < limit)
dist *= 4;
dist = max(dist, minDist);
dist = max(dist, maxCachedPrime);
dist *= 4;
dist = min(dist, maxDist);

return dist;
}

uint64_t getPrevDist(uint64_t n, uint64_t* dist)
uint64_t getPrevDist(uint64_t n, uint64_t dist)
{
double x = (double) n;
x = max(x, 10.0);
double logx = ceil(log(x));

double minDist = config::MIN_CACHE_ITERATOR;
double maxDist = config::MAX_CACHE_ITERATOR;
double logx = log(x);

minDist *= logx;
maxDist *= logx;

uint64_t minDist = config::MIN_CACHE_ITERATOR;
uint64_t maxDist = config::MAX_CACHE_ITERATOR;
minDist /= sizeof(uint64_t);
maxDist /= sizeof(uint64_t);
minDist *= (uint64_t) logx;
maxDist *= (uint64_t) logx;

if (*dist < minDist)
{
minDist = (double) *dist;
*dist *= 4;
}
uint64_t maxCachedPrime = PrimeGenerator::maxCachedPrime();
dist = max(dist, maxCachedPrime);
dist *= 4;

double defaultDist = sqrt(x) * 2;
double newDist = max(minDist, defaultDist);
newDist = min(newDist, maxDist);
uint64_t defaultDist = (uint64_t) (sqrt(x) * 2);
dist = min(dist, minDist);
dist = max(dist, defaultDist);
dist = min(dist, maxDist);

return (uint64_t) newDist;
return dist;
}

bool useStopHint(uint64_t start,
Expand Down Expand Up @@ -120,8 +119,8 @@ void IteratorHelper::prev(uint64_t* start,
uint64_t* dist)
{
*stop = checkedSub(*start, 1);
uint64_t prevDist = getPrevDist(*stop, dist);
*start = checkedSub(*stop, prevDist);
*dist = getPrevDist(*stop, *dist);
*start = checkedSub(*stop, *dist);

if (useStopHint(*start, *stop, stopHint))
*start = checkedSub(stopHint, maxPrimeGap(stopHint));
Expand Down
6 changes: 3 additions & 3 deletions src/iterator-c.cpp
Expand Up @@ -2,7 +2,7 @@
/// @file iterator-c.cpp
/// @brief C port of primesieve::iterator.
///
/// Copyright (C) 2018 Kim Walisch, <kim.walisch@gmail.com>
/// Copyright (C) 2019 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
Expand Down Expand Up @@ -52,7 +52,7 @@ void primesieve_init(primesieve_iterator* it)
it->stop_hint = get_max_stop();
it->i = 0;
it->last_idx = 0;
it->dist = PrimeGenerator::maxCachedPrime();
it->dist = 0;
it->vector = new vector<uint64_t>;
it->primeGenerator = nullptr;
it->is_error = false;
Expand All @@ -67,7 +67,7 @@ void primesieve_skipto(primesieve_iterator* it,
it->stop_hint = stop_hint;
it->i = 0;
it->last_idx = 0;
it->dist = PrimeGenerator::maxCachedPrime();
it->dist = 0;
auto& primes = getPrimes(it);
primes.clear();
clearPrimeGenerator(it);
Expand Down
4 changes: 2 additions & 2 deletions src/iterator.cpp
@@ -1,7 +1,7 @@
///
/// @file iterator.cpp
///
/// Copyright (C) 2018 Kim Walisch, <kim.walisch@gmail.com>
/// Copyright (C) 2019 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
Expand Down Expand Up @@ -47,7 +47,7 @@ void iterator::skipto(uint64_t start,
stop_hint_ = stop_hint;
i_ = 0;
last_idx_ = 0;
dist_ = PrimeGenerator::maxCachedPrime();
dist_ = 0;
clear(primeGenerator_);
primes_.clear();
}
Expand Down

0 comments on commit 944995e

Please sign in to comment.