nodes_value_containers.hpp 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
/** @file feature_creation/node/value_storage/node_value_containers.hpp
 *  @brief Centeral storage area for all descriptor matrix data
 *
 *  This file contains definitions where all descriptor/feature data is stored permanently or in temporary storage arrays
 *  All Nodes's (except ModelNodes) value_ptr/test_value_ptrs point to data here.
 *
 *  @author Thomas A. R. Purcell (tpurcell)
 *  @bug No known bugs.
 */
10
11
#ifndef NODE_VALUE_ARR
#define NODE_VALUE_ARR
Thomas Purcell's avatar
Thomas Purcell committed
12

Thomas Purcell's avatar
Thomas Purcell committed
13
#include <algorithm>
14
#include <cmath>
Thomas Purcell's avatar
Thomas Purcell committed
15
#include <memory>
Thomas Purcell's avatar
Thomas Purcell committed
16
#include <numeric>
Thomas Purcell's avatar
Thomas Purcell committed
17
18
#include <vector>

19
20
#include <omp.h>

Thomas Purcell's avatar
Thomas Purcell committed
21
22
23
24
#include<utils/enum.hpp>

namespace node_value_arrs
{
25
26
27
28
    extern std::vector<double> TEMP_STORAGE_ARR; //!< Array to temporarily store feature training data
    extern std::vector<double> VALUES_ARR; //!< Training data of the stored features
    extern std::vector<double> TEMP_STORAGE_TEST_ARR; //!< Array to temporarily store feature test data
    extern std::vector<double> TEST_VALUES_ARR; //!< Value of the stored features test data
Thomas Purcell's avatar
Thomas Purcell committed
29
    extern std::vector<double> D_MATRIX; //!< The descriptor matrix
30
31
    extern std::vector<int> TEMP_STORAGE_REG; //!< Register to see which feature is stored in each slot for the training data
    extern std::vector<int> TEMP_STORAGE_TEST_REG; //!< Register to see which feature is stored in each slot for the test data
Thomas Purcell's avatar
Thomas Purcell committed
32

Thomas Purcell's avatar
Thomas Purcell committed
33
34
35
    extern std::vector<int> TASK_SZ_TRAIN; //!< Number of training samples per task
    extern std::vector<int> TASK_SZ_TEST; //!< Number of test sample per task

36
    extern int N_SELECTED; //!< Number of features selected
37
38
    extern int N_SAMPLES; //!< Number of training samples for each feature
    extern int N_SAMPLES_TEST; //!< Number of test samples for each feature
39
    extern int N_STORE_FEATURES; //!< Number of features with stored values
Thomas Purcell's avatar
Thomas Purcell committed
40
    extern int N_RUNGS_STORED; //!< Number of rungs with values stored
41
    extern int MAX_N_THREADS; //!< Get the maximum number of threads possible
42
43
    extern int N_OP_SLOTS; //!< The number of possible operator slots
    extern int MAX_RUNG; //!< The maximum rung for all features
Thomas Purcell's avatar
Thomas Purcell committed
44

Thomas Purcell's avatar
Thomas Purcell committed
45
    /**
46
47
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
Thomas Purcell's avatar
Thomas Purcell committed
48
     *
49
50
51
     * @param n_samples Number of training samples for each feature
     * @param n_samples_test Number of test samples for each feature
     * @param n_primary_feat Number of primary features
52
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
53
54
     * @param set_test_task_sz If True reset the task_sz vectors
     */
55
    void initialize_values_arr(int n_samples, int n_samples_test, int n_primary_feat, int max_rung, bool et_task_sz);
Thomas Purcell's avatar
Thomas Purcell committed
56

57
58
59
60
61
62
63
64
65
66
67
68
    /**
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
     *
     * @param n_samples Number of training samples for each feature
     * @param n_samples_test Number of test samples for each feature
     * @param n_primary_feat Number of primary features
     * @param max_rung Largest rung of a feature
     */
    inline void initialize_values_arr(int n_samples, int n_samples_test, int n_primary_feat, int max_rung)
    {
        initialize_values_arr(n_samples, n_samples_test, n_primary_feat, max_rung, true);
Thomas Purcell's avatar
Thomas Purcell committed
69
70
71
72
73
74
75
76
77
    }

    /**
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
     *
     * @param task_sz_train Number of training samples per task
     * @param task_sz_test Number of test sample per task
     * @param n_primary_feat Number of primary features
78
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
79
     */
80
    void initialize_values_arr(std::vector<int> task_sz_train, std::vector<int> task_sz_test, int n_primary_feat, int max_rung);
Thomas Purcell's avatar
Thomas Purcell committed
81
82

    /**
83
84
     * @brief Resize the node value arrays
     * @details Using the updated number of features increase the size of the storage arrays
Thomas Purcell's avatar
Thomas Purcell committed
85
86
87
88
89
     *
     * @param n_dims Number of dimensions to store
     * @param n_feat number of features to store
     * @param use_temp If true keep the temporary_storage
     */
Thomas Purcell's avatar
Thomas Purcell committed
90
    void resize_values_arr(int n_dims, unsigned long int n_feat, bool use_temp);
Thomas Purcell's avatar
Thomas Purcell committed
91

92
    /**
93
94
     * @brief Initialize the descriptor matrix
     * @details Initialize an empty descriptor matrix
95
96
97
98
99
     *
     */
    void initialize_d_matrix_arr();

    /**
100
101
     * @brief Resize the descriptor matrix
     * @details Using the number of newly selected features increase the size of the descriptor matrix
102
103
104
105
106
     *
     * @param n_select Number of features to select
     */
    void resize_d_matrix_arr(int n_select);

Thomas Purcell's avatar
Thomas Purcell committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    /**
     * @brief Reset the global TASK_SZ_TRAIN vector
     *
     * @param task_sz_train the new task_sz train
     */
    void set_task_sz_train(std::vector<int> task_sz_train);

    /**
     * @brief Reset the global TASK_SZ_TEST vector
     *
     * @param task_sz_train the new test_sz train
     */
    void set_task_sz_test(std::vector<int> task_sz_test);

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    /**
     * @brief Get the operator slot associated with a given rung/offset
     *
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
     *
     * @return The operator slot to use
     */
    inline int get_op_slot(int rung, int offset){return std::abs(N_OP_SLOTS - static_cast<int>(std::pow(2, MAX_RUNG - rung)) - offset);}

    /**
     * @brief Get a reference slot/feature register of the training data
     *
     * @param ind The Node's arr_ind
     * @param op_slot(int) Offset integer for TEMP_STORE_ARRAY
     *
     * @return The register element for a given feature index and op_slot
     */
    inline int& temp_storage_reg(unsigned long int ind, int op_slot = 0){return TEMP_STORAGE_REG[(ind % N_STORE_FEATURES) + (op_slot % N_OP_SLOTS) * N_STORE_FEATURES + omp_get_thread_num() * (N_STORE_FEATURES * N_OP_SLOTS + 1)];}

    /**
     * @brief Get a reference slot/feature register of the test data
     *
     * @param ind The Node's arr_ind
     * @param op_slot(int) Offset integer for TEMP_STORE_TEST_ARRAY
     *
     * @return The register element for a given feature index and op_slot
     */
    inline int& temp_storage_test_reg(unsigned long int ind, int op_slot = 0){return TEMP_STORAGE_TEST_REG[(ind % N_STORE_FEATURES) + (op_slot % N_OP_SLOTS) * N_STORE_FEATURES + omp_get_thread_num() * (N_STORE_FEATURES * N_OP_SLOTS + 1)];}

Thomas Purcell's avatar
Thomas Purcell committed
151
    /**
152
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
153
     *
154
     * @param ind The Node's arr_ind
155
156
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
Thomas Purcell's avatar
Thomas Purcell committed
157
158
159
     *
     * @return The register element for a given feature index and offset
     */
160
    inline int& temp_storage_reg(unsigned long int ind, int rung, int offset){return TEMP_STORAGE_REG[(ind % N_STORE_FEATURES) + (get_op_slot(rung, offset) % N_OP_SLOTS) * N_STORE_FEATURES + omp_get_thread_num() * (N_STORE_FEATURES * N_OP_SLOTS + 1)];}
Thomas Purcell's avatar
Thomas Purcell committed
161

162
    /**
163
     * @brief Get a reference slot/feature register of the test data
164
     *
165
     * @param ind The Node's arr_ind
166
167
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
168
169
170
     *
     * @return The register element for a given feature index and offset
     */
171
    inline int& temp_storage_test_reg(unsigned long int ind, int rung, int offset){return TEMP_STORAGE_TEST_REG[(ind % N_STORE_FEATURES) + (get_op_slot(rung, offset) % N_OP_SLOTS) * N_STORE_FEATURES + omp_get_thread_num() * (N_STORE_FEATURES * N_OP_SLOTS + 1)];}
172

Thomas Purcell's avatar
Thomas Purcell committed
173
    /**
174
     * @brief Access element of the permanent training data storage array
Thomas Purcell's avatar
Thomas Purcell committed
175
     *
176
     * @param feature_ind The _arr_ind of Node to get the training data of
Thomas Purcell's avatar
Thomas Purcell committed
177
     *
178
     * @return pointer to the Node's training data
Thomas Purcell's avatar
Thomas Purcell committed
179
     */
180
    inline double* access_value_arr(unsigned long int feature_ind){return VALUES_ARR.data() + feature_ind*N_SAMPLES;}
181

182
    /**
183
     * @brief Access element of the permanent test data storage array
184
     *
185
     * @param feature_ind The _arr_ind of Node to get the test data of
186
     *
187
     * @return pointer to the Node's test data
188
     */
189
    inline double* access_test_value_arr(unsigned long int feature_ind){return TEST_VALUES_ARR.data() + feature_ind*N_SAMPLES_TEST;}
190

Thomas Purcell's avatar
Thomas Purcell committed
191
    /**
192
     * @brief Access element of temporary storage array for the training data
Thomas Purcell's avatar
Thomas Purcell committed
193
     *
194
     * @param slot The slot of the temporary storage array
Thomas Purcell's avatar
Thomas Purcell committed
195
     *
196
     * @return pointer to the data stored in the specified slot
Thomas Purcell's avatar
Thomas Purcell committed
197
     */
198
    inline double* access_temp_storage(unsigned long int slot){return TEMP_STORAGE_ARR.data() + slot*N_SAMPLES;}
199

200
    /**
201
     * @brief Access element of temporary storage array for the test data
202
     *
203
     * @param slot The slot of the temporary storage array
204
     *
205
     * @return pointer to the data stored in the specified slot
206
     */
207
    inline double* access_temp_storage_test(unsigned long int slot){return TEMP_STORAGE_TEST_ARR.data() + slot*N_SAMPLES_TEST;}
208

Thomas Purcell's avatar
Thomas Purcell committed
209
    /**
210
     * @brief Get a Node's value_ptr
Thomas Purcell's avatar
Thomas Purcell committed
211
     *
212
213
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
214
     * @param offset(int) the offset for temporary storage access
Thomas Purcell's avatar
Thomas Purcell committed
215
216
217
     *
     * @return The value pointer
     */
218
    double* get_value_ptr(unsigned long int arr_ind, unsigned long int feat_ind, int rung = 0, int offset = 0);
Thomas Purcell's avatar
Thomas Purcell committed
219

220
    /**
221
     * @brief Get a Node's test_value_ptr
222
     *
223
224
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
225
     * @param offset(int) the offset for temporary storage access
226
227
228
     *
     * @return The value pointer
     */
229
    double* get_test_value_ptr(unsigned long int arr_ind, unsigned long int feat_ind, int rung = 0, int offset = 0);
230

231
    /**
232
     * @brief Get the pointer to a particular selected Node from sis
233
     *
234
     * @param ind Index of the data in the descriptor matrix
235
     * @return The pointer to the descriptor matrix's data
236
     */
237
    inline double* get_d_matrix_ptr(int ind)
Thomas Purcell's avatar
Thomas Purcell committed
238
    {
239
        return D_MATRIX.data() + ind * N_SAMPLES;
Thomas Purcell's avatar
Thomas Purcell committed
240
    }
Thomas Purcell's avatar
Bug Fix    
Thomas Purcell committed
241

242
243
244
245
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
Thomas Purcell's avatar
Bug Fix    
Thomas Purcell committed
246
    inline void clear_temp_reg(){std::fill_n(TEMP_STORAGE_REG.data(), TEMP_STORAGE_REG.size(), -1);}
247

248
249
250
251
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
252
    inline void clear_temp_reg_thread(){std::fill_n(TEMP_STORAGE_REG.data() + N_STORE_FEATURES * N_OP_SLOTS * omp_get_thread_num(), N_STORE_FEATURES * N_OP_SLOTS, -1);}
253

254
255
256
257
    /**
     * @brief Flush the temporary storage register (test data)
     * @details Reset all slots in the register to -1
     */
Thomas Purcell's avatar
Bug Fix    
Thomas Purcell committed
258
    inline void clear_temp_test_reg(){std::fill_n(TEMP_STORAGE_TEST_REG.data(), TEMP_STORAGE_TEST_REG.size(), -1);}
259

Thomas Purcell's avatar
Thomas Purcell committed
260
261
}

262
#endif