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()};
 
DMatrix 相关数据的线程感知 FIFO 缓存。
定义: cache.h:26
decltype(container_) const & Container()
获取底层哈希映射的常量引用。返回前清除过期的缓存。
定义: cache.h:188
void ClearExpired()
定义: cache.h:71
std::unordered_map< Key, Item, Hash > container_
定义: cache.h:65
DMatrixCache(std::size_t cache_size)
定义: cache.h:118
DMatrixCache & operator=(DMatrixCache &&that)
定义: cache.h:120
std::queue< Key > queue_
定义: cache.h:66
std::shared_ptr< CacheT > ResetItem(std::shared_ptr< DMatrix > m, Args const &... args)
重新初始化缓存中的项。
定义: cache.h:174
void CheckConsistent() const
定义: cache.h:69
std::shared_ptr< CacheT > Entry(DMatrix const *m) const
定义: cache.h:195
std::size_t max_size_
定义: cache.h:67
static constexpr std::size_t DefaultSize()
定义: cache.h:40
void ClearExcess()
定义: cache.h:102
std::shared_ptr< CacheT > CacheItem(std::shared_ptr< DMatrix > m, Args const &... args)
如果 DMatrix 尚未在缓存中,则缓存新的 DMatrix。
定义: cache.h:145
内部数据结构,由XGBoost用于保存所有外部数据。
Definition: data.h:573
Definition: intrusive_ptr.h:207
void swap(xgboost::IntrusivePtr< T > &x, xgboost::IntrusivePtr< T > &y) noexcept
定义: intrusive_ptr.h:209
集成目标、gbm和评估的学习器接口。这是用户面临的XGB...
Definition: base.h:97
std::vector< std::pair< std::string, std::string > > Args
定义: base.h:324
std::size_t operator()(Key const &key) const noexcept
定义: cache.h:55
Item(std::shared_ptr< DMatrix > m, std::shared_ptr< CacheT > v)
定义: cache.h:37
std::shared_ptr< CacheT > value
定义: cache.h:32
CacheT const & Value() const
定义: cache.h:34
CacheT & Value()
定义: cache.h:35
std::weak_ptr< DMatrix > ref
定义: cache.h:30
std::thread::id const thread_id
定义: cache.h:48
bool operator==(Key const &that) const
定义: cache.h:50
DMatrix const * ptr
定义: cache.h:47