From 02f266ab66e66160fd3e8186f099319f85ddf089 Mon Sep 17 00:00:00 2001
From: Thomas Purcell <purcell@fhi-berlin.mpg.de>
Date: Tue, 2 Jun 2020 18:28:24 +0200
Subject: [PATCH] Resizing ValueArrays now works properly

Possible to resize based off of rung
---
 .../feature_space/FeatureSpace.cpp            | 34 ++++++++++---------
 src/feature_creation/node/FeatureNode.hpp     |  3 +-
 src/feature_creation/node/Node.hpp            |  8 +++++
 .../value_storage/nodes_value_containers.cpp  |  7 +++-
 .../value_storage/nodes_value_containers.hpp  |  6 ++--
 src/inputs/InputParser.cpp                    |  2 +-
 6 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/src/feature_creation/feature_space/FeatureSpace.cpp b/src/feature_creation/feature_space/FeatureSpace.cpp
index 1434b91b..ac8847da 100644
--- a/src/feature_creation/feature_space/FeatureSpace.cpp
+++ b/src/feature_creation/feature_space/FeatureSpace.cpp
@@ -64,9 +64,7 @@ void FeatureSpace::generate_feature_space()
         _n_feat = _phi.size();
         std::array<int, 2> start_end = _mpi_comm->get_start_end_for_iterator(_phi.size() - _start_gen[_start_gen.size()-1], _start_gen[_start_gen.size()-1]);
 
-        int feat_ind = _n_feat + node_value_arrs::get_max_number_features(_allowed_ops, 1, start_end[0]);
-
-        next_phi.reserve(node_value_arrs::get_max_number_features(_allowed_ops, 1, start_end[1] - start_end[0]));
+        int feat_ind = 0;
 
         for(auto feat_1 = _phi.begin() + start_end[0]; feat_1 != _phi.begin() + start_end[1]; ++feat_1)
         {
@@ -130,26 +128,30 @@ void FeatureSpace::generate_feature_space()
         std::vector<std::vector<node_ptr>> next_phi_gathered;
         mpi::all_gather(*_mpi_comm, next_phi, next_phi_gathered);
 
+        feat_ind = _phi.size();
         for(auto& next_phi_vec : next_phi_gathered)
         {
             _phi.reserve(_phi.size() + next_phi_vec.size());
             for(auto& feat : next_phi_vec)
+            {
+                feat->reindex(feat_ind);
                 _phi.push_back(feat);
+                ++feat_ind;
+            }
         }
     }
-
-    // if(0 < _n_rung_store < _max_phi)
-    // {
-    //     node_value_arrs::resize_values_arr(_n_rung_store, _start_gen[_n_rung_store+1], true);
-    //     for(auto feat = _phi.begin(); feat != _phi.begin() + _start_gen[_n_rung_store+1]; ++feat)
-    //         (*feat)->set_value();
-    // }
-    // else if(_n_rung_store > 0)
-    // {
-    //     node_value_arrs::resize_values_arr(_max_phi, _phi.size(), false);
-    //     for(auto feat = _phi.begin(); feat != _phi.end(); ++feat)
-    //         (*feat)->set_value();
-    // }
+    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();
+    }
     _n_feat = _phi.size();
 }
 
diff --git a/src/feature_creation/node/FeatureNode.hpp b/src/feature_creation/node/FeatureNode.hpp
index 776b7306..e31596b2 100644
--- a/src/feature_creation/node/FeatureNode.hpp
+++ b/src/feature_creation/node/FeatureNode.hpp
@@ -57,8 +57,7 @@ public:
     /**
      * @brief Set the value for the feature
      */
-    inline void set_value(int offset = -1){std::copy_n(node_value_arrs::PRIMARY_FEAT_ARR.get() + _feat_ind * _n_samp, _n_samp, value_ptr());}
-
+    inline void set_value(int offset = -1){std::copy_n(node_value_arrs::get_primary_feat_ptr(_feat_ind), _n_samp, value_ptr());}
     /**
      * @brief Check if the feature contains NaN
      */
diff --git a/src/feature_creation/node/Node.hpp b/src/feature_creation/node/Node.hpp
index 89ec7456..d03d8ac9 100644
--- a/src/feature_creation/node/Node.hpp
+++ b/src/feature_creation/node/Node.hpp
@@ -47,6 +47,14 @@ public:
      */
     Node(const Node &o);
 
+    /**
+     * @brief Reindex the feature
+     * @details re-index the feature to be continuous
+     *
+     * @param ind the new feature index
+     */
+    inline void reindex(int ind){_feat_ind = ind;}
+
     /**
      * @brief Acesssor function to get the number of samples
      */
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 5e046464..8bf1d4dc 100644
--- a/src/feature_creation/node/value_storage/nodes_value_containers.cpp
+++ b/src/feature_creation/node/value_storage/nodes_value_containers.cpp
@@ -77,4 +77,9 @@ void node_value_arrs::resize_values_arr(int n_dims, int n_feat, bool use_temp)
         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());
     }
-}
\ No newline at end of file
+    else
+    {
+        TEMP_STORAGE_ARR = nullptr;
+        TEMP_STORAGE_REG = nullptr;
+    }
+}
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 119e4e0a..15527457 100644
--- a/src/feature_creation/node/value_storage/nodes_value_containers.hpp
+++ b/src/feature_creation/node/value_storage/nodes_value_containers.hpp
@@ -1,5 +1,5 @@
-#ifndef NODE_VALEU_ARR
-#define NODE_VALEU_ARR
+#ifndef NODE_VALUE_ARR
+#define NODE_VALUE_ARR
 
 #include <algorithm>
 #include <memory>
@@ -107,4 +107,4 @@ namespace node_value_arrs
     }
 }
 
-#endif
\ No newline at end of file
+#endif
diff --git a/src/inputs/InputParser.cpp b/src/inputs/InputParser.cpp
index 18d4ca46..f7984abb 100644
--- a/src/inputs/InputParser.cpp
+++ b/src/inputs/InputParser.cpp
@@ -137,7 +137,7 @@ void InputParser::generate_feature_space(std::shared_ptr<MPI_Interface> comm)
     for(int ff = 0; ff < headers.size(); ++ff)
     {
         phi_0.push_back(std::make_shared<FeatureNode>(ff, headers[ff], data[ff], units[ff]));
-        std::copy_n(data[ff].begin(), data[ff].size(), node_value_arrs::get_primary_feat_ptr(ff));
+        std::copy_n(data[ff].data(), data[ff].size(), node_value_arrs::get_primary_feat_ptr(ff));
     }
 
     _feat_space = std::make_shared<FeatureSpace>(comm, phi_0, _opset, _max_rung, _n_sis_select, _max_store_rung);
-- 
GitLab