Commit 83408785 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

allow cache disabling, tweak LRU counter

parent eb9c915c
......@@ -41,7 +41,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#error This file requires at least C++11 support.
#endif
#include <array>
#ifndef POCKETFFT_CACHE_SIZE
#define POCKETFFT_CACHE_SIZE 16
#endif
#include <cmath>
#include <cstring>
#include <cstdlib>
......@@ -49,7 +52,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
#include <vector>
#include <complex>
#if POCKETFFT_CACHE_SIZE!=0
#include <array>
#include <mutex>
#endif
#ifdef POCKETFFT_OPENMP
#include <omp.h>
#endif
......@@ -2266,7 +2272,10 @@ template<typename T0> class pocketfft_r
template<typename T> shared_ptr<T> get_plan(size_t length)
{
constexpr size_t nmax=10;
#if POCKETFFT_CACHE_SIZE==0
return make_shared<T>(length);
#else
constexpr size_t nmax=POCKETFFT_CACHE_SIZE;
static array<shared_ptr<T>, nmax> cache;
static array<size_t, nmax> last_access{{0}};
static size_t access_counter = 0;
......@@ -2274,17 +2283,21 @@ template<typename T> shared_ptr<T> get_plan(size_t length)
auto find_in_cache = [&]() -> shared_ptr<T>
{
for (size_t i=0; i<nmax; ++i)
if (cache[i] && (cache[i]->length()==length))
for (size_t i=0; i<nmax; ++i)
if (cache[i] && (cache[i]->length()==length))
{
// no need to update if this is already the most recent entry
if (last_access[i]!=access_counter)
{
last_access[i] = access_counter++;
last_access[i] = ++access_counter;
// Guard against overflow
if (access_counter == 0)
last_access.fill(0);
return cache[i];
}
return cache[i];
}
return nullptr;
return nullptr;
};
{
......@@ -2304,9 +2317,10 @@ template<typename T> shared_ptr<T> get_plan(size_t length)
lru = i;
cache[lru] = plan;
last_access[lru] = access_counter++;
last_access[lru] = ++access_counter;
}
return plan;
#endif
}
class arr_info
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment