4 #ifndef XGBOOST_CACHE_H_
5 #define XGBOOST_CACHE_H_
7 #include <xgboost/logging.h>
14 #include <unordered_map>
25 template <
typename CacheT>
30 std::weak_ptr<DMatrix>
ref;
37 Item(std::shared_ptr<DMatrix> m, std::shared_ptr<CacheT> v) :
ref{m},
value{
std::move(v)} {}
43 mutable std::mutex lock_;
56 std::size_t f = std::hash<DMatrix const*>()(key.ptr);
57 std::size_t s = std::hash<std::thread::id>()(key.thread_id);
74 std::vector<Key> expired;
75 std::queue<Key> remained;
78 auto p_fmat =
queue_.front();
81 if (it->second.ref.expired()) {
82 expired.push_back(it->first);
84 remained.push(it->first);
89 CHECK_EQ(remained.size() + expired.size(),
container_.size());
91 for (
auto const& key : expired) {
94 while (!remained.empty()) {
95 auto p_fmat = remained.front();
107 auto p_fmat =
queue_.front();
121 CHECK(lock_.try_lock());
123 CHECK(that.lock_.try_lock());
125 std::swap(this->container_, that.container_);
127 std::swap(this->max_size_, that.max_size_);
144 template <
typename...
Args>
145 std::shared_ptr<CacheT>
CacheItem(std::shared_ptr<DMatrix> m,
Args const&... args) {
147 std::lock_guard<std::mutex> guard{lock_};
155 auto key =
Key{m.get(), std::this_thread::get_id()};
159 container_.emplace(key,
Item{m, std::make_shared<CacheT>(args...)});
173 template <
typename...
Args>
174 std::shared_ptr<CacheT>
ResetItem(std::shared_ptr<DMatrix> m,
Args const&... args) {
175 std::lock_guard<std::mutex> guard{lock_};
177 auto key =
Key{m.get(), std::this_thread::get_id()};
180 it->second = {m, std::make_shared<CacheT>(args...)};
182 return it->second.value;
189 std::lock_guard<std::mutex> guard{lock_};
196 std::lock_guard<std::mutex> guard{lock_};
197 auto key =
Key{m, std::this_thread::get_id()};
Thread-aware FIFO cache for DMatrix related data.
Definition: cache.h:26
decltype(container_) const & Container()
Get a const reference to the underlying hash map. Clear expired caches before returning.
Definition: cache.h:188
void ClearExpired()
Definition: cache.h:71
std::unordered_map< Key, Item, Hash > container_
Definition: cache.h:65
DMatrixCache(std::size_t cache_size)
Definition: cache.h:118
DMatrixCache & operator=(DMatrixCache &&that)
Definition: cache.h:120
std::queue< Key > queue_
Definition: cache.h:66
std::shared_ptr< CacheT > ResetItem(std::shared_ptr< DMatrix > m, Args const &... args)
Re-initialize the item in cache.
Definition: cache.h:174
void CheckConsistent() const
Definition: cache.h:69
std::shared_ptr< CacheT > Entry(DMatrix const *m) const
Definition: cache.h:195
std::size_t max_size_
Definition: cache.h:67
static constexpr std::size_t DefaultSize()
Definition: cache.h:40
void ClearExcess()
Definition: cache.h:102
std::shared_ptr< CacheT > CacheItem(std::shared_ptr< DMatrix > m, Args const &... args)
Cache a new DMatrix if it's not in the cache already.
Definition: cache.h:145
Internal data structured used by XGBoost during training.
Definition: data.h:509
Definition: intrusive_ptr.h:207
void swap(xgboost::IntrusivePtr< T > &x, xgboost::IntrusivePtr< T > &y) noexcept
Definition: intrusive_ptr.h:209
namespace of xgboost
Definition: base.h:90
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
std::size_t operator()(Key const &key) const noexcept
Definition: cache.h:55
Item(std::shared_ptr< DMatrix > m, std::shared_ptr< CacheT > v)
Definition: cache.h:37
std::shared_ptr< CacheT > value
Definition: cache.h:32
CacheT const & Value() const
Definition: cache.h:34
CacheT & Value()
Definition: cache.h:35
std::weak_ptr< DMatrix > ref
Definition: cache.h:30
std::thread::id const thread_id
Definition: cache.h:48
bool operator==(Key const &that) const
Definition: cache.h:50
DMatrix const * ptr
Definition: cache.h:47