From 0684148d54e8839ced859e63906449368188d46e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 11 Jun 2018 17:23:05 +0100 Subject: Speedup build and add lru cache --- src/main/game/global_cache.cpp | 78 +++++++++++++++++++--- src/main/game/global_cache.h | 47 +++++++++++-- src/main/game/pile.cpp | 4 +- .../game/search-state/game_state.legal_moves.cpp | 2 +- 4 files changed, 116 insertions(+), 15 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 3620ddb..3732789 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -14,6 +14,17 @@ 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, + hasher + > + > +> item_list; + /////////////////////// // CACHED GAME STATE // @@ -163,18 +174,69 @@ 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 unlimited_cache::contains(const game_state& gs) const { + return cache.count(cached_game_state(gs)) > 0; +} + +void unlimited_cache::clear() { + cache.clear(); } -bool global_cache::contains(const game_state& gs) const { - return u_set.count(cached_game_state(gs)) > 0; + +/////////////// +// 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(), + hasher(gs), + equal_to() + ) + ); +} + +lru_cache::lru_cache(const game_state& gs, size_t max_num_items_) + : max_num_items(max_num_items_), cache(get_init_tuple(gs)) { +} + +bool lru_cache::insert(const game_state& gs) { + pair 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 */ + cache.pop_back(); + } + return p.second; } -void global_cache::clear() { - u_set.clear(); +bool lru_cache::contains(const game_state& gs) const { + return cache.get<1>().count(cached_game_state(gs)) > 0; } -global_cache::global_cache(const game_state& gs) - : u_set(0, hasher(gs)) { +void lru_cache::clear() { + cache.clear(); } diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 9541100..e420b57 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -9,6 +9,10 @@ #include #include #include +#include +#include +#include +#include #include "sol_rules.h" #include "search-state/game_state.h" @@ -24,6 +28,7 @@ struct cached_game_state { state_data data; }; + bool operator==(const cached_game_state&, const cached_game_state&); struct hasher { @@ -38,16 +43,48 @@ struct hasher { class global_cache { public: - explicit global_cache(const game_state&); - bool insert(const game_state&); - bool contains(const game_state&) const; - void clear(); + virtual ~global_cache() = default; + virtual bool insert(const game_state&) = 0; + virtual bool contains(const game_state&) const = 0; + virtual void clear() = 0; +}; + +class unlimited_cache : public global_cache { +public: + explicit unlimited_cache(const game_state&); + bool insert(const game_state&) override; + bool contains(const game_state&) const override; + void clear() override; private: std::unordered_set< cached_game_state, hasher - > u_set; + > cache; +}; + +class lru_cache : public global_cache { + 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, + hasher + > + > + > item_list; +public: + explicit lru_cache(const game_state&, std::size_t); + bool insert(const game_state&) override; + bool contains(const game_state&) const override; + void clear() override; + +private: + static item_list::ctor_args_list get_init_tuple(const game_state&); + + std::size_t max_num_items; + item_list 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::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 &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; } -- cgit v1.2.1 From 43a11ee2ee7827290d631698ea25b6724c8a1c29 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 12 Jun 2018 16:57:32 +0100 Subject: Add lru cache with elems limit --- src/main/game/global_cache.cpp | 34 ++++++++++++++++++++++++++++------ src/main/game/global_cache.h | 35 +++++++++-------------------------- 2 files changed, 37 insertions(+), 32 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 3732789..2e271cc 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -7,6 +7,7 @@ #include #include "global_cache.h" +#include "../input-output/output/log_helper.h" using namespace std; using namespace boost; @@ -30,7 +31,7 @@ typedef boost::multi_index::multi_index_container< // 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) { @@ -170,7 +171,7 @@ size_t hasher::hash_value(card const& c) const { -////////////////// +/*////////////////// // GLOBAL CACHE // ////////////////// @@ -188,7 +189,7 @@ bool unlimited_cache::contains(const game_state& gs) const { void unlimited_cache::clear() { cache.clear(); -} +}*/ /////////////// @@ -219,18 +220,30 @@ item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) { } lru_cache::lru_cache(const game_state& gs, size_t max_num_items_) - : max_num_items(max_num_items_), cache(get_init_tuple(gs)) { + : max_num_items(max_num_items_), cache(get_init_tuple(gs)), states_removed_from_cache(0) { } -bool lru_cache::insert(const game_state& gs) { +pair lru_cache::insert(const game_state& gs) { pair 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 (size_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.second; + return p; } bool lru_cache::contains(const game_state& gs) const { @@ -240,3 +253,12 @@ bool lru_cache::contains(const game_state& gs) const { void lru_cache::clear() { cache.clear(); } + +void lru_cache::set_non_live(item_list::iterator state_iter) { + cache.modify(state_iter, [](auto& v){ v.live = false; }); + // TODO assert +} + +int 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 e420b57..7798f78 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -27,6 +27,7 @@ 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&); @@ -41,29 +42,8 @@ struct hasher { const game_state& init_gs; }; -class global_cache { +class lru_cache { public: - virtual ~global_cache() = default; - virtual bool insert(const game_state&) = 0; - virtual bool contains(const game_state&) const = 0; - virtual void clear() = 0; -}; - -class unlimited_cache : public global_cache { -public: - explicit unlimited_cache(const game_state&); - bool insert(const game_state&) override; - bool contains(const game_state&) const override; - void clear() override; - -private: - std::unordered_set< - cached_game_state, - hasher - > cache; -}; - -class lru_cache : public global_cache { typedef boost::multi_index::multi_index_container< cached_game_state, boost::multi_index::indexed_by< @@ -74,17 +54,20 @@ class lru_cache : public global_cache { > > > item_list; -public: + explicit lru_cache(const game_state&, std::size_t); - bool insert(const game_state&) override; - bool contains(const game_state&) const override; - void clear() override; + std::pair insert(const game_state&); + bool contains(const game_state&) const; + void clear(); + void set_non_live(item_list::iterator); + int get_states_removed_from_cache() const; private: static item_list::ctor_args_list get_init_tuple(const game_state&); std::size_t max_num_items; item_list cache; + int states_removed_from_cache; }; #endif //SOLVITAIRE_GLOBAL_CACHE_H -- cgit v1.2.1 From fb3e96a333cc9b27529f5d2c7866a2f7c4d43ec2 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 13 Jun 2018 15:08:11 +0100 Subject: Add cache capacity command line option --- src/main/game/global_cache.cpp | 21 +++++++++++++++++---- src/main/game/global_cache.h | 10 ++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 2e271cc..db0c6de 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -219,7 +219,7 @@ item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) { ); } -lru_cache::lru_cache(const game_state& gs, size_t max_num_items_) +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) { } @@ -232,7 +232,7 @@ pair lru_cache::insert(const game_state& gs) { // 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 (size_t i = 0; prev(cache.end())->live; i++) { + for (uint64_t i = 0; prev(cache.end())->live; i++) { cache.relocate(cache.begin(), prev(cache.end())); if (i == max_num_items) { @@ -254,11 +254,24 @@ 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; }); - // TODO assert +#ifndef NDEBUG + assert(succ); +#endif } -int lru_cache::get_states_removed_from_cache() const { +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 7798f78..dd4670e 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -55,19 +55,21 @@ public: > > item_list; - explicit lru_cache(const game_state&, std::size_t); + explicit lru_cache(const game_state&, uint64_t); std::pair 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); - int get_states_removed_from_cache() const; + uint64_t get_states_removed_from_cache() const; private: static item_list::ctor_args_list get_init_tuple(const game_state&); - std::size_t max_num_items; + uint64_t max_num_items; item_list cache; - int states_removed_from_cache; + uint64_t states_removed_from_cache; }; #endif //SOLVITAIRE_GLOBAL_CACHE_H -- cgit v1.2.1