Medial Code Documentation
Loading...
Searching...
No Matches
hist_cache.h
1
4#ifndef XGBOOST_TREE_HIST_HIST_CACHE_H_
5#define XGBOOST_TREE_HIST_HIST_CACHE_H_
6#include <cstddef> // for size_t
7#include <map> // for map
8#include <memory> // for unique_ptr
9#include <vector> // for vector
10
11#include "../../common/hist_util.h" // for GHistRow, ConstGHistRow
12#include "../../common/ref_resource_view.h" // for ReallocVector
13#include "xgboost/base.h" // for bst_node_t, bst_bin_t
14#include "xgboost/logging.h" // for CHECK_GT
15#include "xgboost/span.h" // for Span
16
17namespace xgboost::tree {
31 // maps node index to offset in `data_`.
32 std::map<bst_node_t, std::size_t> node_map_;
33 // currently allocated bins, used for tracking consistentcy.
34 std::size_t current_size_{0};
35
36 // stores the histograms in a contiguous buffer
38 std::unique_ptr<Vec> data_{new Vec{}}; // nvcc 12.1 trips over std::make_unique
39
40 // number of histogram bins across all features
41 bst_bin_t n_total_bins_{0};
42 // limits the number of nodes that can be in the cache for each tree
43 std::size_t n_cached_nodes_{0};
44 // whether the tree has grown beyond the cache limit
45 bool has_exceeded_{false};
46
47 public:
48 BoundedHistCollection() = default;
49 common::GHistRow operator[](std::size_t idx) {
50 auto offset = node_map_.at(idx);
51 return common::Span{data_->data(), data_->size()}.subspan(offset, n_total_bins_);
52 }
53 common::ConstGHistRow operator[](std::size_t idx) const {
54 auto offset = node_map_.at(idx);
55 return common::Span{data_->data(), data_->size()}.subspan(offset, n_total_bins_);
56 }
57 void Reset(bst_bin_t n_total_bins, std::size_t n_cached_nodes) {
58 n_total_bins_ = n_total_bins;
59 n_cached_nodes_ = n_cached_nodes;
60 this->Clear(false);
61 }
65 void Clear(bool exceeded) {
66 node_map_.clear();
67 current_size_ = 0;
68 has_exceeded_ = exceeded;
69 }
70
71 [[nodiscard]] bool CanHost(common::Span<bst_node_t const> nodes_to_build,
72 common::Span<bst_node_t const> nodes_to_sub) const {
73 auto n_new_nodes = nodes_to_build.size() + nodes_to_sub.size();
74 return n_new_nodes + node_map_.size() <= n_cached_nodes_;
75 }
76
84 common::Span<bst_node_t const> nodes_to_sub) {
85 auto n_new_nodes = nodes_to_build.size() + nodes_to_sub.size();
86 auto alloc_size = n_new_nodes * n_total_bins_;
87 auto new_size = alloc_size + current_size_;
88 if (new_size > data_->size()) {
89 data_->Resize(new_size);
90 }
91 for (auto nidx : nodes_to_build) {
92 node_map_[nidx] = current_size_;
93 current_size_ += n_total_bins_;
94 }
95 for (auto nidx : nodes_to_sub) {
96 node_map_[nidx] = current_size_;
97 current_size_ += n_total_bins_;
98 }
99 CHECK_EQ(current_size_, new_size);
100 }
101 void AllocateHistograms(std::vector<bst_node_t> const& nodes) {
104 }
105
106 [[nodiscard]] bool HasExceeded() const { return has_exceeded_; }
107 [[nodiscard]] bool HistogramExists(bst_node_t nidx) const {
108 return node_map_.find(nidx) != node_map_.cend();
109 }
110 [[nodiscard]] std::size_t Size() const { return current_size_; }
111};
112} // namespace xgboost::tree
113#endif // XGBOOST_TREE_HIST_HIST_CACHE_H_
Definition ref_resource_view.h:166
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition span.h:424
A persistent cache for CPU histogram.
Definition hist_cache.h:30
void AllocateHistograms(common::Span< bst_node_t const > nodes_to_build, common::Span< bst_node_t const > nodes_to_sub)
Allocate histogram buffers for all nodes.
Definition hist_cache.h:83
void Clear(bool exceeded)
Clear the cache, mark whether the cache is exceeded the limit.
Definition hist_cache.h:65
Copyright 2015-2023 by XGBoost Contributors.
defines console logging options for xgboost. Use to enforce unified print behavior.
Copyright 2021-2023 by XGBoost Contributors.
Definition tree_updater.h:25
std::int32_t bst_node_t
Type for tree node index.
Definition base.h:112
int32_t bst_bin_t
Type for histogram bin index.
Definition base.h:103