nodes_value_containers.hpp 11.9 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
#include <vector>
18
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
19

20
21
#include <omp.h>

Thomas Purcell's avatar
Thomas Purcell committed
22
#include"utils/enum.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
23
24
25

namespace node_value_arrs
{
26
27
28
29
    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
30
    extern std::vector<double> D_MATRIX; //!< The descriptor matrix
31
32
    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
33

Thomas Purcell's avatar
Thomas Purcell committed
34
35
36
    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

37
    extern int N_SELECTED; //!< Number of features selected
38
39
    extern int N_SAMPLES; //!< Number of training samples for each feature
    extern int N_SAMPLES_TEST; //!< Number of test samples for each feature
Thomas Purcell's avatar
Thomas Purcell committed
40
41
    extern int N_STORE_FEATURES; //!< Number of features with stored values
    extern int N_RUNGS_STORED; //!< Number of rungs with values stored
42
    extern int MAX_N_THREADS; //!< Get the maximum number of threads possible
43
44
    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
45

Thomas Purcell's avatar
Thomas Purcell committed
46
    /**
47
48
     * @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
49
     *
50
51
52
     * @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
53
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
54
55
     * @param set_test_task_sz If True reset the task_sz vectors
     */
56
    void initialize_values_arr(const int n_samples, const int n_samples_test, const int n_primary_feat, const int max_rung, const bool et_task_sz);
Thomas Purcell's avatar
Thomas Purcell committed
57
58
59
60
61
62
63
64

    /**
     * @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
65
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
66
     */
67
    inline void initialize_values_arr(const int n_samples, const int n_samples_test, const int n_primary_feat, const int max_rung)
Thomas Purcell's avatar
Thomas Purcell committed
68
    {
69
        initialize_values_arr(n_samples, n_samples_test, n_primary_feat, max_rung, true);
Thomas Purcell's avatar
Thomas Purcell committed
70
71
72
73
74
75
76
77
78
    }

    /**
     * @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
79
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
80
     */
81
    void initialize_values_arr(const std::vector<int> task_sz_train, const std::vector<int> task_sz_test, const int n_primary_feat, const int max_rung);
Thomas Purcell's avatar
Thomas Purcell committed
82
83

    /**
84
85
     * @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
86
87
88
89
90
     *
     * @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
     */
91
    void resize_values_arr(const int n_dims, const int n_feat, const bool use_temp);
Thomas Purcell's avatar
Thomas Purcell committed
92

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

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

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

    /**
     * @brief Reset the global TASK_SZ_TEST vector
     *
     * @param task_sz_train the new test_sz train
     */
120
    void set_task_sz_test(const std::vector<int> task_sz_test);
Thomas Purcell's avatar
Thomas Purcell committed
121

122
123
124
125
126
    /**
     * @brief Get the operator slot associated with a given rung/offset
     *
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
127
     * @param for_comp If true get a slot dedicated to comparing features
128
129
130
     *
     * @return The operator slot to use
     */
131
    inline int get_op_slot(const int rung, const int offset, const bool for_comp)
132
133
134
    {
        return std::abs(N_OP_SLOTS / (1 + !for_comp) - static_cast<int>(std::pow(2, MAX_RUNG - rung)) - offset);
    }
135

Thomas Purcell's avatar
Thomas Purcell committed
136
    /**
137
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
138
     *
139
     * @param ind The Node's arr_ind
140
141
142
143
     * @param op_slot(int) Offset integer for TEMP_STORE_ARRAY
     *
     * @return The register element for a given feature index and op_slot
     */
144
    inline int& temp_storage_reg(const unsigned long int ind, const int op_slot=0)
145
146
147
148
149
    {
        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)
        ];
    }
150
151
152
153
154
155
156
157
158

    /**
     * @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
     */
159
    inline int& temp_storage_test_reg(const unsigned long int ind, const int op_slot=0)
160
161
162
163
164
    {
        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)
        ];
    }
165

Thomas Purcell's avatar
Thomas Purcell committed
166
    /**
167
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
168
     *
169
     * @param ind The Node's arr_ind
170
171
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
172
     * @param for_comp If true get a slot dedicated to comparing features
Thomas Purcell's avatar
Thomas Purcell committed
173
174
175
     *
     * @return The register element for a given feature index and offset
     */
176
    inline int& temp_storage_reg(const unsigned long int ind, const int rung, const int offset, const bool for_comp)
177
178
179
180
181
182
183
    {
        return TEMP_STORAGE_REG[
            (ind % N_STORE_FEATURES) +
            (get_op_slot(rung, offset, for_comp) % N_OP_SLOTS) * N_STORE_FEATURES +
            omp_get_thread_num() * (N_STORE_FEATURES * N_OP_SLOTS + 1)
        ];
    }
Thomas Purcell's avatar
Thomas Purcell committed
184

185
    /**
186
     * @brief Get a reference slot/feature register of the test data
187
     *
188
     * @param ind The Node's arr_ind
189
190
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
191
     * @param for_comp If true get a slot dedicated to comparing features
192
193
194
     *
     * @return The register element for a given feature index and offset
     */
195
    inline int& temp_storage_test_reg(const unsigned long int ind, const int rung, const int offset, const bool for_comp)
196
197
198
199
200
201
202
    {
        return TEMP_STORAGE_TEST_REG[
            (ind % N_STORE_FEATURES) +
            (get_op_slot(rung, offset, for_comp) % N_OP_SLOTS) * N_STORE_FEATURES +
            omp_get_thread_num() * (N_STORE_FEATURES * N_OP_SLOTS + 1)
        ];
    }
203

Thomas Purcell's avatar
Thomas Purcell committed
204
    /**
205
     * @brief Access element of the permanent training data storage array
Thomas Purcell's avatar
Thomas Purcell committed
206
     *
207
     * @param feature_ind The _arr_ind of Node to get the training data of
Thomas Purcell's avatar
Thomas Purcell committed
208
     *
209
     * @return pointer to the Node's training data
Thomas Purcell's avatar
Thomas Purcell committed
210
     */
211
    inline double* access_value_arr(const unsigned long int feature_ind){return &VALUES_ARR[feature_ind*N_SAMPLES];}
212

213
    /**
214
     * @brief Access element of the permanent test data storage array
215
     *
216
     * @param feature_ind The _arr_ind of Node to get the test data of
217
     *
218
     * @return pointer to the Node's test data
219
     */
220
    inline double* access_test_value_arr(const unsigned long int feature_ind){return &TEST_VALUES_ARR[feature_ind*N_SAMPLES_TEST];}
221

Thomas Purcell's avatar
Thomas Purcell committed
222
    /**
223
     * @brief Access element of temporary storage array for the training data
Thomas Purcell's avatar
Thomas Purcell committed
224
     *
225
     * @param slot The slot of the temporary storage array
Thomas Purcell's avatar
Thomas Purcell committed
226
     *
227
     * @return pointer to the data stored in the specified slot
Thomas Purcell's avatar
Thomas Purcell committed
228
     */
229
    inline double* access_temp_storage(const unsigned long int slot){return &TEMP_STORAGE_ARR[slot*N_SAMPLES];}
230

231
    /**
232
     * @brief Access element of temporary storage array for the test data
233
     *
234
     * @param slot The slot of the temporary storage array
235
     *
236
     * @return pointer to the data stored in the specified slot
237
     */
238
    inline double* access_temp_storage_test(const unsigned long int slot){return &TEMP_STORAGE_TEST_ARR[slot*N_SAMPLES_TEST];}
239

Thomas Purcell's avatar
Thomas Purcell committed
240
    /**
241
     * @brief Get a Node's value_ptr
Thomas Purcell's avatar
Thomas Purcell committed
242
     *
243
244
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
245
     * @param rung Rung of the feature
246
     * @param offset(int) the offset for temporary storage access
247
248
     * @param for_comp If true get a slot dedicated to comparing features
     * @param modify_reg If true modify the register of the array (if false set the register to -1)
Thomas Purcell's avatar
Thomas Purcell committed
249
250
251
     *
     * @return The value pointer
     */
252
253
254
255
256
257
258
259
    double* get_value_ptr(
        const unsigned long int arr_ind,
        const unsigned long int feat_ind,
        const int rung=0,
        const int offset=0,
        const bool for_comp=false,
        const bool modify_reg=true
    );
Thomas Purcell's avatar
Thomas Purcell committed
260

261
    /**
262
     * @brief Get a Node's test_value_ptr
263
     *
264
265
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
266
     * @param rung Rung of the feature
267
     * @param offset(int) the offset for temporary storage access
268
269
     * @param for_comp If true get a slot dedicated to comparing features
     * @param modify_reg If true modify the register of the array (if false set the register to -1)
270
271
272
     *
     * @return The value pointer
     */
273
274
275
276
277
278
279
280
    double* get_test_value_ptr(
        const unsigned long int arr_ind,
        const unsigned long int feat_ind,
        const int rung=0,
        const int offset=0,
        const bool for_comp=false,
        const bool modify_reg=true
    );
281

282
    /**
283
     * @brief Get the pointer to a particular selected Node from sis
284
     *
285
     * @param ind Index of the data in the descriptor matrix
286
     * @return The pointer to the descriptor matrix's data
287
     */
288
    inline double* get_d_matrix_ptr(const int ind){return &D_MATRIX[ind * N_SAMPLES];}
Thomas Purcell's avatar
Bug Fix    
Thomas Purcell committed
289

290
291
292
293
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
294
    inline void clear_temp_reg(){std::fill_n(TEMP_STORAGE_REG.begin(), TEMP_STORAGE_REG.size(), -1);}
295

296
297
298
299
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
300
301
302
303
    inline void clear_temp_reg_thread()
    {
        std::fill_n(TEMP_STORAGE_REG.begin() + (N_STORE_FEATURES * N_OP_SLOTS + 1) * omp_get_thread_num(), N_STORE_FEATURES * N_OP_SLOTS + 1, -1);
    }
304

305
306
307
308
    /**
     * @brief Flush the temporary storage register (test data)
     * @details Reset all slots in the register to -1
     */
309
    inline void clear_temp_test_reg(){std::fill_n(TEMP_STORAGE_TEST_REG.begin(), TEMP_STORAGE_TEST_REG.size(), -1);}
310

Thomas Purcell's avatar
Thomas Purcell committed
311
312
}

313
#endif