From d9e086e637288db8ab6b50ec2d1a3bae12bb32ad Mon Sep 17 00:00:00 2001 From: Thomas Purcell <purcell@fhi-berlin.mpg.de> Date: Tue, 2 Jun 2020 19:34:40 +0200 Subject: [PATCH] Resize value arrays each step to save time in feature creation --- .../feature_space/FeatureSpace.cpp | 21 +++++------- .../value_storage/nodes_value_containers.cpp | 34 +++++++++++-------- .../value_storage/nodes_value_containers.hpp | 20 +++++++---- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/feature_creation/feature_space/FeatureSpace.cpp b/src/feature_creation/feature_space/FeatureSpace.cpp index cd3a6349..23b16ee9 100644 --- a/src/feature_creation/feature_space/FeatureSpace.cpp +++ b/src/feature_creation/feature_space/FeatureSpace.cpp @@ -66,7 +66,7 @@ void FeatureSpace::generate_feature_space() // next_phi.reserve(node_value_arrs::get_max_number_features(_allowed_ops, 1, start_end[1] - start_end[0])); - int feat_ind = 0; + int feat_ind = _phi.size(); for(auto feat_1 = _phi.begin() + start_end[0]; feat_1 != _phi.begin() + start_end[1]; ++feat_1) { @@ -141,18 +141,13 @@ void FeatureSpace::generate_feature_space() ++feat_ind; } } - } - if((_n_rung_store > 0) && (_n_rung_store < _max_phi)) - { - node_value_arrs::resize_values_arr(_n_rung_store, _start_gen[_n_rung_store+1], true); - for(int ff = 0; ff < _start_gen[_n_rung_store + 1]; ++ff) - _phi[ff]->set_value(); - } - else if(_n_rung_store > 0) - { - node_value_arrs::resize_values_arr(_max_phi, _phi.size(), false); - for(int ff = 0; ff < _phi.size(); ++ff) - _phi[ff]->set_value(); + if(nn <= _n_rung_store) + { + bool use_temp = (nn != _max_phi) || (_max_phi > _n_rung_store); + node_value_arrs::resize_values_arr(_n_rung_store, _phi.size(), use_temp); + for(int ff = _start_gen[0]; ff < _phi.size(); ++ff) + _phi[ff]->set_value(); + } } _n_feat = _phi.size(); } diff --git a/src/feature_creation/node/value_storage/nodes_value_containers.cpp b/src/feature_creation/node/value_storage/nodes_value_containers.cpp index 8bf1d4dc..5a7d133f 100644 --- a/src/feature_creation/node/value_storage/nodes_value_containers.cpp +++ b/src/feature_creation/node/value_storage/nodes_value_containers.cpp @@ -4,11 +4,11 @@ int node_value_arrs::N_SAMPLES; int node_value_arrs::N_STORE_FEATURES; int node_value_arrs::N_RUNGS_STORED; -std::unique_ptr<int[]> node_value_arrs::TEMP_STORAGE_REG = nullptr; +std::vector<int> node_value_arrs::TEMP_STORAGE_REG; -std::unique_ptr<double[]> node_value_arrs::VALUES_ARR = nullptr; -std::unique_ptr<double[]> node_value_arrs::PRIMARY_FEAT_ARR = nullptr; -std::unique_ptr<double[]> node_value_arrs::TEMP_STORAGE_ARR = nullptr; +std::vector<double> node_value_arrs::PRIMARY_FEAT_ARR; +std::vector<double> node_value_arrs::VALUES_ARR; +std::vector<double> node_value_arrs::TEMP_STORAGE_ARR; int node_value_arrs::get_number_new_features(std::string new_op, int n_current_features) { @@ -56,12 +56,13 @@ void node_value_arrs::initialize_values_arr(int n_samples, int n_primary_feat) N_RUNGS_STORED = 0; N_STORE_FEATURES = n_primary_feat; - PRIMARY_FEAT_ARR = std::unique_ptr<double[]>(new double[N_STORE_FEATURES * N_SAMPLES]); - VALUES_ARR = std::unique_ptr<double[]>(new double[N_STORE_FEATURES * N_SAMPLES]); + PRIMARY_FEAT_ARR = std::vector<double>(N_STORE_FEATURES * N_SAMPLES); + PRIMARY_FEAT_ARR.shrink_to_fit(); - TEMP_STORAGE_ARR = std::unique_ptr<double[]>(new double[3 * N_STORE_FEATURES * N_SAMPLES]); - TEMP_STORAGE_REG = std::unique_ptr<int[]>(new int[3 * N_STORE_FEATURES]); - std::copy_n(std::vector<int>(3*N_STORE_FEATURES, -1).data(), 3*N_STORE_FEATURES, TEMP_STORAGE_REG.get()); + VALUES_ARR = std::vector<double>(N_STORE_FEATURES * N_SAMPLES); + + TEMP_STORAGE_ARR = std::vector<double>(3 * N_STORE_FEATURES * N_SAMPLES); + TEMP_STORAGE_REG = std::vector<int>(3 * N_STORE_FEATURES, -1); } void node_value_arrs::resize_values_arr(int n_dims, int n_feat, bool use_temp) @@ -69,17 +70,20 @@ void node_value_arrs::resize_values_arr(int n_dims, int n_feat, bool use_temp) N_RUNGS_STORED = n_dims; N_STORE_FEATURES = n_feat; - VALUES_ARR = std::unique_ptr<double[]>(new double[N_STORE_FEATURES * N_SAMPLES]); + VALUES_ARR.resize(N_STORE_FEATURES * N_SAMPLES); + VALUES_ARR.shrink_to_fit(); if(use_temp) { - TEMP_STORAGE_ARR = std::unique_ptr<double[]>(new double[3 * N_STORE_FEATURES * N_SAMPLES]); - TEMP_STORAGE_REG = std::unique_ptr<int[]>(new int[3 * N_STORE_FEATURES]); - std::copy_n(std::vector<int>(3*N_STORE_FEATURES, -1).data(), 3*N_STORE_FEATURES, TEMP_STORAGE_REG.get()); + TEMP_STORAGE_ARR.resize(3 * N_STORE_FEATURES * N_SAMPLES); + TEMP_STORAGE_ARR.shrink_to_fit(); + + TEMP_STORAGE_REG.resize(3 * N_STORE_FEATURES * N_SAMPLES, - 1); + TEMP_STORAGE_REG.shrink_to_fit(); } else { - TEMP_STORAGE_ARR = nullptr; - TEMP_STORAGE_REG = nullptr; + TEMP_STORAGE_ARR = {}; + TEMP_STORAGE_REG = {}; } } diff --git a/src/feature_creation/node/value_storage/nodes_value_containers.hpp b/src/feature_creation/node/value_storage/nodes_value_containers.hpp index 15527457..91f0c6ee 100644 --- a/src/feature_creation/node/value_storage/nodes_value_containers.hpp +++ b/src/feature_creation/node/value_storage/nodes_value_containers.hpp @@ -13,10 +13,10 @@ namespace node_value_arrs extern int N_STORE_FEATURES; //!< Number of features with stored values extern int N_RUNGS_STORED; //!< Number of rungs with values stored - extern std::unique_ptr<int[]> TEMP_STORAGE_REG; //!< Register to see which feature is stored in each slot - extern std::unique_ptr<double[]> PRIMARY_FEAT_ARR; //!< Value of the stored features - extern std::unique_ptr<double[]> VALUES_ARR; //!< Value of the stored features - extern std::unique_ptr<double[]> TEMP_STORAGE_ARR; //!< Array to temporarily store feature values + extern std::vector<int> TEMP_STORAGE_REG; //!< Register to see which feature is stored in each slot + extern std::vector<double> PRIMARY_FEAT_ARR; //!< Value of the stored features + extern std::vector<double> VALUES_ARR; //!< Value of the stored features + extern std::vector<double> TEMP_STORAGE_ARR; //!< Array to temporarily store feature values /** * @brief Get the maximum number of new features for each rung @@ -74,7 +74,7 @@ namespace node_value_arrs * * @return pointer to the feature;s data array */ - inline double* access_value_arr(int feature_ind){return VALUES_ARR.get() + feature_ind*N_SAMPLES;} + inline double* access_value_arr(int feature_ind){return VALUES_ARR.data() + feature_ind*N_SAMPLES;} /** * @brief Access element of temporary storage array @@ -83,7 +83,7 @@ namespace node_value_arrs * * @return pointer to the feature's temporary storage */ - inline double* access_temp_storage(int slot){return TEMP_STORAGE_ARR.get() + slot*N_SAMPLES;} + inline double* access_temp_storage(int slot){return TEMP_STORAGE_ARR.data() + slot*N_SAMPLES;} /** * @brief Access the value_ptr to a feature @@ -101,9 +101,15 @@ namespace node_value_arrs return access_temp_storage((ind % N_STORE_FEATURES) + (offset % 3) * N_STORE_FEATURES); } + /** + * @brief Get the pointer to a primary feature + * + * @param ind Index of the primary feature + * @return The pointer to the primary feature's data + */ inline double* get_primary_feat_ptr(int ind) { - return PRIMARY_FEAT_ARR.get() + ind * N_SAMPLES; + return PRIMARY_FEAT_ARR.data() + ind * N_SAMPLES; } } -- GitLab