From 4d523f70e42cac37796e9dcd3dea1d84a752f652 Mon Sep 17 00:00:00 2001
From: Cristian C Lalescu <Cristian.Lalescu@ds.mpg.de>
Date: Fri, 17 Apr 2020 16:03:00 +0200
Subject: [PATCH] tweaks xincrement stats

add possibility to use outside field for temporary data, such that we
don't allocate and delete all the time
---
 cpp/field.cpp | 19 ++++++++++++-------
 cpp/field.hpp |  3 ++-
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/cpp/field.cpp b/cpp/field.cpp
index 3ee9f299..7d3f0afe 100644
--- a/cpp/field.cpp
+++ b/cpp/field.cpp
@@ -861,16 +861,20 @@ void field<rnumber, be, fc>::compute_rspace_xincrement_stats(
                 const hid_t group,
                 const std::string dset_name,
                 const hsize_t toffset,
-                const std::vector<double> max_estimate)
+                const std::vector<double> max_estimate,
+                field<rnumber, be, fc> *tmp_field)
 {
     TIMEZONE("field::compute_rspace_xincrement_stats");
     assert(this->real_space_representation);
     assert(fc == ONE || fc == THREE);
-    field<rnumber, be, fc> *tmp_field = new field<rnumber, be, fc>(
-            this->rlayout->sizes[2],
-            this->rlayout->sizes[1],
-            this->rlayout->sizes[0],
-            this->rlayout->comm);
+    bool own_field = false;
+    own_field = (tmp_field == NULL);
+    if (own_field)
+        tmp_field = new field<rnumber, be, fc>(
+                this->rlayout->sizes[2],
+                this->rlayout->sizes[1],
+                this->rlayout->sizes[0],
+                this->rlayout->comm);
     tmp_field->real_space_representation = true;
     this->RLOOP_simd(
                 [&](const ptrdiff_t rindex,
@@ -890,7 +894,8 @@ void field<rnumber, be, fc>::compute_rspace_xincrement_stats(
             dset_name,
             toffset,
             max_estimate);
-    delete tmp_field;
+    if (own_field)
+        delete tmp_field;
 }
 
 
diff --git a/cpp/field.hpp b/cpp/field.hpp
index 0364e8d5..f352be51 100644
--- a/cpp/field.hpp
+++ b/cpp/field.hpp
@@ -129,7 +129,8 @@ class field
                 const hid_t group,
                 const std::string dset_name,
                 const hsize_t toffset,
-                const std::vector<double> max_estimate);
+                const std::vector<double> max_estimate,
+                field<rnumber, be, fc> *tmp_field = NULL);
 
         void compute_rspace_stats(
                 const hid_t group,
-- 
GitLab