nodes_value_containers.hpp 14.3 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
33
    extern std::vector<double> PARAM_STORAGE_ARR; //!< Array to store parameterized feature values
    extern std::vector<double> PARAM_STORAGE_TEST_ARR; //!< Array to store parameterized feature values for the test set

34
35
    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
36
37
38
    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

39
    extern int N_SELECTED; //!< Number of features selected
40
41
    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
42
43
    extern int N_STORE_FEATURES; //!< Number of features with stored values
    extern int N_RUNGS_STORED; //!< Number of rungs with values stored
44
    extern int MAX_N_THREADS; //!< Get the maximum number of threads possible
45
    extern int N_OP_SLOTS; //!< The number of possible operator slots
46
    extern int N_PARAM_OP_SLOTS; //!< The maximum number of possible operator slots
47
    extern int MAX_RUNG; //!< The maximum rung for all features
Thomas Purcell's avatar
Thomas Purcell committed
48

Thomas Purcell's avatar
Thomas Purcell committed
49
    /**
50
51
     * @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
52
     *
53
54
55
     * @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
56
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
57
     * @param set_test_task_sz If True reset the task_sz vectors
58
     * @param use_params If True set up parameter storage
Thomas Purcell's avatar
Thomas Purcell committed
59
     */
60
61
62
63
64
65
66
67
    void initialize_values_arr(
        const int n_samples,
        const int n_samples_test,
        const int n_primary_feat,
        const int max_rung,
        const bool set_task_sz,
        const bool use_params
    );
Thomas Purcell's avatar
Thomas Purcell committed
68
69
70
71
72
73
74
75

    /**
     * @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
76
     * @param max_rung Largest rung of a feature
77
     * @param use_params If True set up parameter storage
Thomas Purcell's avatar
Thomas Purcell committed
78
     */
79
80
81
82
83
84
    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
85
    {
86
        initialize_values_arr(n_samples, n_samples_test, n_primary_feat, max_rung, true, false);
Thomas Purcell's avatar
Thomas Purcell committed
87
88
89
90
91
92
93
94
95
    }

    /**
     * @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
96
     * @param max_rung Largest rung of a feature
97
     * @param use_params If True set up parameter storage
Thomas Purcell's avatar
Thomas Purcell committed
98
     */
99
100
101
102
103
104
105
    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,
        const bool use_params
    );
Thomas Purcell's avatar
Thomas Purcell committed
106
107

    /**
108
109
     * @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
110
111
112
113
114
     *
     * @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
     */
115
    void resize_values_arr(const int n_dims, const int n_feat, const bool use_temp);
Thomas Purcell's avatar
Thomas Purcell committed
116

117
118
119
120
121
    /**
     * @brief Initialize the parameter storage array
     */
    void initialize_param_storage();

122
    /**
123
124
     * @brief Initialize the descriptor matrix
     * @details Initialize an empty descriptor matrix
125
126
127
128
129
     *
     */
    void initialize_d_matrix_arr();

    /**
130
131
     * @brief Resize the descriptor matrix
     * @details Using the number of newly selected features increase the size of the descriptor matrix
132
133
134
     *
     * @param n_select Number of features to select
     */
135
    void resize_d_matrix_arr(const int n_select);
136

Thomas Purcell's avatar
Thomas Purcell committed
137
138
139
140
141
    /**
     * @brief Reset the global TASK_SZ_TRAIN vector
     *
     * @param task_sz_train the new task_sz train
     */
142
    void set_task_sz_train(const std::vector<int> task_sz_train);
Thomas Purcell's avatar
Thomas Purcell committed
143
144
145
146
147
148

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

151
152
153
154
155
    /**
     * @brief Get the operator slot associated with a given rung/offset
     *
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
156
     * @param for_comp If true get a slot dedicated to comparing features
157
158
159
     *
     * @return The operator slot to use
     */
160
    inline int get_op_slot(const int rung, const int offset, const bool for_comp)
161
162
163
    {
        return std::abs(N_OP_SLOTS / (1 + !for_comp) - static_cast<int>(std::pow(2, MAX_RUNG - rung)) - offset);
    }
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
    /**
     * @brief Get the parameter operator slot associated with a given rung/offset
     *
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
     * @param for_comp If true get a slot dedicated to comparing features
     *
     * @return The operator slot to use
     */
    inline int get_param_op_slot(const int rung, const int offset, const bool for_comp)
    {
        return std::abs(N_PARAM_OP_SLOTS / (1 + !for_comp) - static_cast<int>(std::pow(2, MAX_RUNG - rung)) - offset);
    }

Thomas Purcell's avatar
Thomas Purcell committed
179
    /**
180
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
181
     *
182
     * @param ind The Node's arr_ind
183
184
185
186
     * @param op_slot(int) Offset integer for TEMP_STORE_ARRAY
     *
     * @return The register element for a given feature index and op_slot
     */
187
    inline int& temp_storage_reg(const unsigned long int ind, const int op_slot=0)
188
189
190
191
192
    {
        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)
        ];
    }
193
194
195
196
197
198
199
200
201

    /**
     * @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
     */
202
    inline int& temp_storage_test_reg(const unsigned long int ind, const int op_slot=0)
203
204
205
206
207
    {
        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)
        ];
    }
208

Thomas Purcell's avatar
Thomas Purcell committed
209
    /**
210
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
211
     *
212
     * @param ind The Node's arr_ind
213
214
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
215
     * @param for_comp If true get a slot dedicated to comparing features
Thomas Purcell's avatar
Thomas Purcell committed
216
217
218
     *
     * @return The register element for a given feature index and offset
     */
219
    inline int& temp_storage_reg(const unsigned long int ind, const int rung, const int offset, const bool for_comp)
220
221
222
223
224
225
226
    {
        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
227

228
    /**
229
     * @brief Get a reference slot/feature register of the test data
230
     *
231
     * @param ind The Node's arr_ind
232
233
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
234
     * @param for_comp If true get a slot dedicated to comparing features
235
236
237
     *
     * @return The register element for a given feature index and offset
     */
238
    inline int& temp_storage_test_reg(const unsigned long int ind, const int rung, const int offset, const bool for_comp)
239
240
241
242
243
244
245
    {
        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)
        ];
    }
246

Thomas Purcell's avatar
Thomas Purcell committed
247
    /**
248
     * @brief Access element of the permanent training data storage array
Thomas Purcell's avatar
Thomas Purcell committed
249
     *
250
     * @param feature_ind The _arr_ind of Node to get the training data of
Thomas Purcell's avatar
Thomas Purcell committed
251
     *
252
     * @return pointer to the Node's training data
Thomas Purcell's avatar
Thomas Purcell committed
253
     */
254
    inline double* access_value_arr(const unsigned long int feature_ind){return &VALUES_ARR[feature_ind*N_SAMPLES];}
255

256
    /**
257
     * @brief Access element of the permanent test data storage array
258
     *
259
     * @param feature_ind The _arr_ind of Node to get the test data of
260
     *
261
     * @return pointer to the Node's test data
262
     */
263
    inline double* access_test_value_arr(const unsigned long int feature_ind){return &TEST_VALUES_ARR[feature_ind*N_SAMPLES_TEST];}
264

Thomas Purcell's avatar
Thomas Purcell committed
265
    /**
266
     * @brief Access element of temporary storage array for the training data
Thomas Purcell's avatar
Thomas Purcell committed
267
     *
268
     * @param slot The slot of the temporary storage array
Thomas Purcell's avatar
Thomas Purcell committed
269
     *
270
     * @return pointer to the data stored in the specified slot
Thomas Purcell's avatar
Thomas Purcell committed
271
     */
272
    inline double* access_temp_storage(const unsigned long int slot){return &TEMP_STORAGE_ARR[slot*N_SAMPLES];}
273

274
    /**
275
     * @brief Access element of temporary storage array for the test data
276
     *
277
     * @param slot The slot of the temporary storage array
278
     *
279
     * @return pointer to the data stored in the specified slot
280
     */
281
    inline double* access_temp_storage_test(const unsigned long int slot){return &TEMP_STORAGE_TEST_ARR[slot*N_SAMPLES_TEST];}
282

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    /**
     * @brief Access the param storage array
     *
     * @param rung Rung of the feature
     * @param offset(int) the offset for temporary storage access
     * @param for_comp If true get a slot dedicated to comparing features
     * @return pointer to the correct element of the PARAM_STORAGE_ARR
     */
    inline double* access_param_storage(const int rung=0, const int offset=0, const bool for_comp=false)
    {
        return &PARAM_STORAGE_ARR[
            N_SAMPLES *
            (
                (get_param_op_slot(rung, offset, for_comp) % N_PARAM_OP_SLOTS) +
                omp_get_thread_num() * (N_PARAM_OP_SLOTS + 1)
            )
        ];
    }

    /**
     * @brief Access the param storage array for the test set
     *
     * @param rung Rung of the feature
     * @param offset(int) the offset for temporary storage access
     * @param for_comp If true get a slot dedicated to comparing features
     * @return pointer to the correct element of the PARAM_STORAGE_ARR
     */
    inline double* access_param_storage_test(const int rung=0, const int offset=0, const bool for_comp=false)
    {
        return &PARAM_STORAGE_TEST_ARR[
            N_SAMPLES_TEST *
            (
                (get_param_op_slot(rung, offset, for_comp) % N_PARAM_OP_SLOTS) +
                omp_get_thread_num() * (N_PARAM_OP_SLOTS + 1)
            )
        ];
    }

Thomas Purcell's avatar
Thomas Purcell committed
321
    /**
322
     * @brief Get a Node's value_ptr
Thomas Purcell's avatar
Thomas Purcell committed
323
     *
324
325
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
326
     * @param rung Rung of the feature
327
     * @param offset(int) the offset for temporary storage access
328
     * @param for_comp If true get a slot dedicated to comparing features
Thomas Purcell's avatar
Thomas Purcell committed
329
330
331
     *
     * @return The value pointer
     */
332
333
334
335
336
    double* get_value_ptr(
        const unsigned long int arr_ind,
        const unsigned long int feat_ind,
        const int rung=0,
        const int offset=0,
337
        const bool for_comp=false
338
    );
Thomas Purcell's avatar
Thomas Purcell committed
339

340
    /**
341
     * @brief Get a Node's test_value_ptr
342
     *
343
344
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
345
     * @param rung Rung of the feature
346
     * @param offset(int) the offset for temporary storage access
347
     * @param for_comp If true get a slot dedicated to comparing features
348
349
350
     *
     * @return The value pointer
     */
351
352
353
354
355
    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,
356
        const bool for_comp=false
357
    );
358

359
    /**
360
     * @brief Get the pointer to a particular selected Node from sis
361
     *
362
     * @param ind Index of the data in the descriptor matrix
363
     * @return The pointer to the descriptor matrix's data
364
     */
365
    inline double* get_d_matrix_ptr(const int ind){return &D_MATRIX[ind * N_SAMPLES];}
Thomas Purcell's avatar
Bug Fix    
Thomas Purcell committed
366

367
368
369
370
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
371
    inline void clear_temp_reg(){std::fill_n(TEMP_STORAGE_REG.begin(), TEMP_STORAGE_REG.size(), -1);}
372

373
374
375
376
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
377
378
379
380
    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);
    }
381

382
383
384
385
    /**
     * @brief Flush the temporary storage register (test data)
     * @details Reset all slots in the register to -1
     */
386
    inline void clear_temp_test_reg(){std::fill_n(TEMP_STORAGE_TEST_REG.begin(), TEMP_STORAGE_TEST_REG.size(), -1);}
387

Thomas Purcell's avatar
Thomas Purcell committed
388
389
}

390
#endif