forpy  2
desk.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_UTIL_DESK_H_
4 #define FORPY_UTIL_DESK_H_
5 
6 #include "../global.h"
7 #include "../types.h"
8 
9 namespace forpy {
10 
19 struct TreeDesk {
22  std::vector<TodoMark> marks;
25  std::atomic<size_t> *stored_in_leafs;
28  std::atomic<id_t> *next_id_p;
31  std::vector<std::pair<id_t, id_t>> *tree_p;
32 
36  inline void setup(std::atomic<size_t> *silp, std::atomic<id_t> *np,
37  std::vector<std::pair<id_t, id_t>> *tsp) {
38  stored_in_leafs = silp;
39  next_id_p = np;
40  tree_p = tsp;
41  }
45  inline void reset() {
46  marks.clear();
47  stored_in_leafs = nullptr;
48  next_id_p = nullptr;
49  tree_p = nullptr;
50  }
51 };
52 
61 struct DeciderDesk {
63  size_t n_samples, input_dim, annot_dim;
72 
78  std::vector<id_t> feature_indices;
80 
82  std::vector<float> full_sum;
84  float *full_sum_p;
86  const float *annot_p;
88  size_t annot_os;
89  const float *weights_p;
90  float full_w;
91  std::vector<id_t> sort_perm;
93  std::vector<id_t> elem_ids_sorted;
95  std::vector<float> feat_values;
96  float *feat_p;
98  std::vector<float> left_sum_vec;
99  float *left_sum_p;
101 
106  mu::variant<const float *, const double *, const uint *, const uint8_t *>
108 
110  bool make_to_leaf;
112  // These only contain valid values if `make_to_leaf` is false.
116 
122  std::vector<size_t> invalid_counts;
123 
127  std::vector<size_t> *node_to_featsel_p;
131  mu::variant<std::vector<float>, std::vector<double>, std::vector<uint32_t>,
132  std::vector<uint8_t>> *node_to_thresh_v_p;
133 
134  inline void setup(
135  std::vector<size_t> *ntfp,
136  mu::variant<std::vector<float>, std::vector<double>,
137  std::vector<uint32_t>, std::vector<uint8_t>> *nttp) {
138  node_to_featsel_p = ntfp;
139  node_to_thresh_v_p = nttp;
140  if (ntfp != nullptr) invalid_counts.resize(ntfp->size());
141  }
142  inline void reset() {
143  n_samples = input_dim = annot_dim = 0;
145  elem_id_p = nullptr;
146  weights_p = nullptr;
147  full_w = 0.f;
148  start_id = end_id = node_id = 0;
149  node_to_featsel_p = nullptr;
150  node_to_thresh_v_p = nullptr;
151  invalid_counts.clear();
152  }
153 };
154 
162 struct LeafDesk {
166  std::vector<Mat<float>> *leaf_regression_map_p;
167  inline void setup(std::vector<Mat<float>> *lrmp) {
168  leaf_regression_map_p = lrmp;
169  };
170  inline void reset() { leaf_regression_map_p = nullptr; }
171 };
172 
181 struct RandomDesk {
182  std::mt19937 random_engine;
184  inline void setup(const uint &seed) {
185  this->seed = seed;
186  random_engine.seed(seed);
187  };
188  inline void reset() {
189  this->seed = 0;
190  random_engine.seed(1);
191  };
192 };
193 
201 struct Desk {
202  inline Desk(int i) : thread_id(i){};
203  TreeDesk t;
207 
208  const int thread_id;
209  inline void setup(
210  std::atomic<size_t> *stored_in_leaf_p, std::atomic<id_t> *next_id_p,
211  std::vector<std::pair<id_t, id_t>> *tree_p,
212  std::vector<size_t> *ntfp = nullptr,
213  mu::variant<std::vector<float>, std::vector<double>,
214  std::vector<uint32_t>, std::vector<uint8_t>> *nttp = nullptr,
215  std::vector<Mat<float>> *lrmp = nullptr, const uint &random_seed = 0) {
217  t.setup(stored_in_leaf_p, next_id_p, tree_p);
218  d.setup(ntfp, nttp);
219  l.setup(lrmp);
220  r.setup(random_seed);
221  };
222  inline void reset() {
223  t.reset();
224  d.reset();
225  l.reset();
226  r.reset();
227  }
228 };
229 
230 }; // namespace forpy
231 #endif // FORPY_UTIL_DESK_H_
size_t annot_os
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:88
std::atomic< id_t > * next_id_p
Definition: desk.h:28
Desk(int i)
Definition: desk.h:202
Desk for decider training.
Definition: desk.h:61
size_t annot_dim
These variables are populated in IDecider::make_node.
Definition: desk.h:64
bool presorted
Variables used during the threshold optimization.
Definition: desk.h:77
void reset()
Definition: desk.h:188
mu::variant< const float *, const double *, const uint *, const uint8_t * > full_feat_p_v
Definition: desk.h:107
id_t left_id
Return values from IThreshOpt::optimize.
Definition: desk.h:114
DeciderDesk d
Definition: desk.h:204
std::vector< std::pair< id_t, id_t > > * tree_p
Definition: desk.h:31
void setup(std::vector< Mat< float >> *lrmp)
Definition: desk.h:167
size_t id_t
Element id type.
Definition: types.h:106
std::vector< size_t > * node_to_featsel_p
Definition: desk.h:127
Desk for tree training.
Definition: desk.h:19
uint min_samples_at_leaf
These variables are populated in IDecider::make_node.
Definition: desk.h:65
void reset()
Definition: desk.h:142
std::vector< size_t > invalid_counts
Definition: desk.h:122
id_t * sort_perm_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:92
std::vector< TodoMark > marks
Definition: desk.h:22
DataV class_feat_values
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:97
const int thread_id
Definition: desk.h:208
std::pair< id_t, id_t > interv_t
Definition: types.h:141
std::atomic< size_t > * stored_in_leafs
Definition: desk.h:25
void setup(std::atomic< size_t > *silp, std::atomic< id_t > *np, std::vector< std::pair< id_t, id_t >> *tsp)
Set up all the internal pointers.
Definition: desk.h:36
float * feat_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:96
std::mt19937 random_engine
Definition: desk.h:182
void setup(const uint &seed)
Definition: desk.h:184
const uint * class_annot_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:87
typename mu::variant< std::vector< float >, std::vector< double >, std::vector< uint >, std::vector< uint8_t > > DataV
Definition: storage.h:129
id_t start_id
These variables are populated in IDecider::make_node.
Definition: desk.h:70
std::vector< float > full_sum
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:83
std::vector< id_t > sort_perm
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:91
id_t * elem_ids_sorted_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:94
float maxproxy
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:85
void reset()
Definition: desk.h:170
const float * weights_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:89
size_t input_dim
These variables are populated in IDecider::make_node.
Definition: desk.h:64
mu::variant< SplitOptRes< float >, SplitOptRes< double >, SplitOptRes< uint >, SplitOptRes< uint8_t > > OptSplitV
Definition: types.h:139
mu::variant< std::vector< float >, std::vector< double >, std::vector< uint32_t >, std::vector< uint8_t > > * node_to_thresh_v_p
Definition: desk.h:132
std::vector< id_t > feature_indices
Variables used during the threshold optimization.
Definition: desk.h:78
size_t n_samples
These variables are populated in IDecider::make_node.
Definition: desk.h:64
id_t end_id
These variables are populated in IDecider::make_node.
Definition: desk.h:70
LeafDesk l
Definition: desk.h:205
float full_w
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:90
id_t node_id
These variables are populated in IDecider::make_node.
Definition: desk.h:70
std::vector< float > feat_values
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:95
interv_t left_int
Return values from IThreshOpt::optimize.
Definition: desk.h:113
TreeDesk t
Definition: desk.h:202
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Mat
Parameterized Matrix type (row major).
Definition: types.h:52
std::vector< id_t > elem_ids_sorted
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:93
float * left_sum_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:99
Main thread desk object.
Definition: desk.h:201
float fullentropy
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:85
void setup(std::vector< size_t > *ntfp, mu::variant< std::vector< float >, std::vector< double >, std::vector< uint32_t >, std::vector< uint8_t >> *nttp)
Definition: desk.h:134
OptSplitV best_res_v
Variables used during the threshold optimization.
Definition: desk.h:75
void setup(std::atomic< size_t > *stored_in_leaf_p, std::atomic< id_t > *next_id_p, std::vector< std::pair< id_t, id_t >> *tree_p, std::vector< size_t > *ntfp=nullptr, mu::variant< std::vector< float >, std::vector< double >, std::vector< uint32_t >, std::vector< uint8_t >> *nttp=nullptr, std::vector< Mat< float >> *lrmp=nullptr, const uint &random_seed=0)
Definition: desk.h:209
Desk for coordinating the random engines.
Definition: desk.h:181
id_t best_feat_idx
Variables used during the threshold optimization.
Definition: desk.h:76
id_t * elem_id_p
Definition: desk.h:69
void reset()
Definition: desk.h:222
std::vector< Mat< float > > * leaf_regression_map_p
Definition: desk.h:166
OptSplitV opt_res_v
Variables used during the threshold optimization.
Definition: desk.h:75
bool need_sort
Variables used during the threshold optimization.
Definition: desk.h:77
void reset()
Clear the marks and reset all pointers.
Definition: desk.h:45
float * full_sum_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:84
RandomDesk r
Definition: desk.h:206
bool make_to_leaf
Return values from IThreshOpt::optimize.
Definition: desk.h:111
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113
std::vector< float > left_sum_vec
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:98
const float * annot_p
Variables initialized in IThreshOpt::full_entropy.
Definition: desk.h:86
interv_t right_int
Return values from IThreshOpt::optimize.
Definition: desk.h:113
id_t right_id
Return values from IThreshOpt::optimize.
Definition: desk.h:114
Desk for leaf manager training.
Definition: desk.h:162