diff options
| author | Charlie Blake | 2018-06-13 15:38:20 +0100 |
|---|---|---|
| committer | GitHub | 2018-06-13 15:38:20 +0100 |
| commit | ccb1232a48deef855d0e0f3ea0f69382494975cc (patch) | |
| tree | bcc8fe29ea84abbd6682c1a880064a90dd65f959 /src/main/game | |
| parent | Merge pull request #59 from thecharlesblake/paper-tidy-up (diff) | |
| parent | Add default cache capacity of 100,000,000 (diff) | |
| download | metasol-ccb1232a48deef855d0e0f3ea0f69382494975cc.tar.gz metasol-ccb1232a48deef855d0e0f3ea0f69382494975cc.zip | |
Merge pull request #60 from thecharlesblake/cache-mem-limit
Cache mem limit
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/global_cache.cpp | 117 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 36 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 4 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.legal_moves.cpp | 2 |
4 files changed, 140 insertions, 19 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 3620ddb..db0c6de 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -7,6 +7,7 @@ #include <boost/functional/hash.hpp> #include "global_cache.h" +#include "../input-output/output/log_helper.h" using namespace std; using namespace boost; @@ -14,12 +15,23 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; +typedef boost::multi_index::multi_index_container< + cached_game_state, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::hashed_unique< + boost::multi_index::identity<cached_game_state>, + hasher + > + > +> item_list; + /////////////////////// // CACHED GAME STATE // /////////////////////// -cached_game_state::cached_game_state(const game_state& gs) { +cached_game_state::cached_game_state(const game_state& gs) : live(true) { data.reserve(52+18); // Enough for each card and up to 18 piles if (gs.rules.hole) { @@ -159,22 +171,107 @@ size_t hasher::hash_value(card const& c) const { -////////////////// +/*////////////////// // GLOBAL CACHE // ////////////////// -bool global_cache::insert(const game_state& gs) { - return u_set.emplace(gs).second; +unlimited_cache::unlimited_cache(const game_state& gs) + : cache(0, hasher(gs)) { +} + +bool unlimited_cache::insert(const game_state& gs) { + return cache.emplace(gs).second; } -bool global_cache::contains(const game_state& gs) const { - return u_set.count(cached_game_state(gs)) > 0; +bool unlimited_cache::contains(const game_state& gs) const { + return cache.count(cached_game_state(gs)) > 0; } -void global_cache::clear() { - u_set.clear(); +void unlimited_cache::clear() { + cache.clear(); +}*/ + + +/////////////// +// LRU CACHE // +/////////////// + +// Based on https://www.boost.org/doc/libs/1_67_0/libs/multi_index/example/serialization.cpp +/* Boost.MultiIndex example of serialization of a MRU list. + * + * Copyright 2003-2008 Joaquin M Lopez Munoz. + * 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) + * + * See http://www.boost.org/libs/multi_index for library home page. + */ + +item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) { + return boost::make_tuple( + item_list::nth_index<0>::type::ctor_args(), + boost::make_tuple( + size_t(0), + multi_index::identity<cached_game_state>(), + hasher(gs), + equal_to<cached_game_state>() + ) + ); +} + +lru_cache::lru_cache(const game_state& gs, uint64_t max_num_items_) + : max_num_items(max_num_items_), cache(get_init_tuple(gs)), states_removed_from_cache(0) { +} + +pair<item_list::iterator, bool> lru_cache::insert(const game_state& gs) { + pair<item_list::iterator, bool> p = cache.push_front(cached_game_state(gs)); + + if(!p.second){ /* duplicate item */ + cache.relocate(cache.begin(), p.first); /* put in front */ + } else if(cache.size() > max_num_items){ /* keep the length <= max_num_items */ + + // If the least recently used node is 'live' (i.e. a parent), relocates + // it to the head of the list until this is no longer the case + for (uint64_t i = 0; prev(cache.end())->live; i++) { + cache.relocate(cache.begin(), prev(cache.end())); + + if (i == max_num_items) { + LOG_ERROR("All items in cache are live and cache is full"); + throw runtime_error("All items in cache are live and cache is full"); + } + } + cache.pop_back(); + states_removed_from_cache++; + } + return p; +} + +bool lru_cache::contains(const game_state& gs) const { + return cache.get<1>().count(cached_game_state(gs)) > 0; +} + +void lru_cache::clear() { + cache.clear(); +} + +item_list::size_type lru_cache::size() const { + return cache.size(); +} + +item_list::size_type lru_cache::bucket_count() const { + return cache.get<1>().bucket_count(); +} + +void lru_cache::set_non_live(item_list::iterator state_iter) { +#ifndef NDEBUG + bool succ = +#endif + cache.modify(state_iter, [](auto& v){ v.live = false; }); +#ifndef NDEBUG + assert(succ); +#endif } -global_cache::global_cache(const game_state& gs) - : u_set(0, hasher(gs)) { +uint64_t lru_cache::get_states_removed_from_cache() const { + return states_removed_from_cache; } diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 9541100..dd4670e 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -9,6 +9,10 @@ #include <unordered_set> #include <boost/pool/pool.hpp> #include <boost/pool/pool_alloc.hpp> +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/sequenced_index.hpp> +#include <boost/multi_index/identity.hpp> +#include <boost/multi_index/hashed_index.hpp> #include "sol_rules.h" #include "search-state/game_state.h" @@ -23,7 +27,9 @@ struct cached_game_state { void add_card_divider(); state_data data; + bool live; // Is a parent in the current search tree }; + bool operator==(const cached_game_state&, const cached_game_state&); struct hasher { @@ -36,18 +42,34 @@ struct hasher { const game_state& init_gs; }; -class global_cache { +class lru_cache { public: - explicit global_cache(const game_state&); - bool insert(const game_state&); + typedef boost::multi_index::multi_index_container< + cached_game_state, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::hashed_unique< + boost::multi_index::identity<cached_game_state>, + hasher + > + > + > item_list; + + explicit lru_cache(const game_state&, uint64_t); + std::pair<item_list::iterator, bool> insert(const game_state&); bool contains(const game_state&) const; void clear(); + item_list::size_type size() const; + item_list::size_type bucket_count() const; + void set_non_live(item_list::iterator); + uint64_t get_states_removed_from_cache() const; private: - std::unordered_set< - cached_game_state, - hasher - > u_set; + static item_list::ctor_args_list get_init_tuple(const game_state&); + + uint64_t max_num_items; + item_list cache; + uint64_t states_removed_from_cache; }; #endif //SOLVITAIRE_GLOBAL_CACHE_H diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 3136681..23a02af 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -35,7 +35,9 @@ card& pile::operator[] (size_type i) { } card pile::operator[] (size_type i) const { - return pile_vec[pile_vec.size() - 1 - i]; + vector<card>::size_type s = pile_vec.size() - 1 - i; + assert(s < pile_vec.size()); + return pile_vec[s]; } void pile::place(const card c) { diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index 2acfba6..f9fc941 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -242,7 +242,7 @@ void game_state::add_built_group_moves(vector<move> &moves) const { // Finds the size of the built group at the top of a pile pile::size_type game_state::get_built_group_height(pile::ref ref) const { pile::size_type i = 1; - while (is_next_built_group_card(piles[ref][i], piles[ref][i-1]) && i < piles[ref].size()) + while (i < piles[ref].size() && is_next_built_group_card(piles[ref][i], piles[ref][i-1])) i++; return i; } |
