From 3b2b84cb5f856e7f94013f338db710daaa22e331 Mon Sep 17 00:00:00 2001 From: Evgueni Ovtchinnikov Date: Mon, 9 Dec 2024 19:28:26 +0000 Subject: [PATCH 1/6] employed initialise_with_0 flag to reduce unnecessary 0-fill --- src/buildblock/ProjDataInMemory.cxx | 6 ++++++ src/include/stir/Array.h | 11 +++++++++++ src/include/stir/Array.inl | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/src/buildblock/ProjDataInMemory.cxx b/src/buildblock/ProjDataInMemory.cxx index 8ec0cfa3c..cd031159f 100644 --- a/src/buildblock/ProjDataInMemory.cxx +++ b/src/buildblock/ProjDataInMemory.cxx @@ -73,12 +73,18 @@ ProjDataInMemory::ProjDataInMemory(shared_ptr const& exam_info_s void ProjDataInMemory::create_buffer(const bool initialise_with_0) { + if (!initialise_with_0) { + this->buffer.set_initialise_with_zeros(false); + this->buffer.grow(0, this->size_all()); + return; + } #if 0 float *b = new float[this->size_all()]; if (initialise_with_0) memset(b, 0, this->size_all()*sizeof(float)); return b; #else + this->buffer.set_initialise_with_zeros(true); this->buffer = Array<1, float>(static_cast(this->size_all())); #endif } diff --git a/src/include/stir/Array.h b/src/include/stir/Array.h index f0c904438..59b32428b 100644 --- a/src/include/stir/Array.h +++ b/src/include/stir/Array.h @@ -569,6 +569,15 @@ class Array<1, elemT> : public NumericVectorWithOffset inline const elemT& at(const BasicCoordinate<1, int>& c) const; //@} + void set_initialise_with_zeros(bool iwz) + { + init_with_zeros_ = iwz; + } + bool get_initialise_with_zeros() const + { + return init_with_zeros_; + } + private: // Make sure we can call init() recursively. template @@ -579,6 +588,8 @@ class Array<1, elemT> : public NumericVectorWithOffset \arg data_ptr should start to a contiguous block of correct size */ inline void init(const IndexRange<1>& range, elemT* const data_ptr, bool copy_data); + + bool init_with_zeros_= 0; }; END_NAMESPACE_STIR diff --git a/src/include/stir/Array.inl b/src/include/stir/Array.inl index 724f91f22..6a503a154 100644 --- a/src/include/stir/Array.inl +++ b/src/include/stir/Array.inl @@ -583,6 +583,12 @@ Array<1, elemT>::resize(const int min_index, const int max_index) const size_type oldlength = this->size(); base_type::resize(min_index, max_index); + + if (!get_initialise_with_zeros()) { + this->check_state(); + return; + } + if (oldlength == 0) { for (int i = this->get_min_index(); i <= this->get_max_index(); i++) From 377020b5154542be68c7fec4b470899d3843f2fd Mon Sep 17 00:00:00 2001 From: Evgueni Ovtchinnikov Date: Tue, 10 Dec 2024 14:33:30 +0000 Subject: [PATCH 2/6] corrected the default value of init_with_zeros_ in Array.h --- src/include/stir/Array.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/stir/Array.h b/src/include/stir/Array.h index 59b32428b..17a42ffe1 100644 --- a/src/include/stir/Array.h +++ b/src/include/stir/Array.h @@ -589,7 +589,7 @@ class Array<1, elemT> : public NumericVectorWithOffset */ inline void init(const IndexRange<1>& range, elemT* const data_ptr, bool copy_data); - bool init_with_zeros_= 0; + bool init_with_zeros_= 1; }; END_NAMESPACE_STIR From 7b416afdb53cf54c28c078686a328e00596c02eb Mon Sep 17 00:00:00 2001 From: Evgueni Ovtchinnikov Date: Wed, 11 Dec 2024 12:07:50 +0000 Subject: [PATCH 3/6] corrected ProjDataInMemory::create_buffer (size->max_index) --- src/buildblock/ProjDataInMemory.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buildblock/ProjDataInMemory.cxx b/src/buildblock/ProjDataInMemory.cxx index cd031159f..7f51eb997 100644 --- a/src/buildblock/ProjDataInMemory.cxx +++ b/src/buildblock/ProjDataInMemory.cxx @@ -75,7 +75,7 @@ ProjDataInMemory::create_buffer(const bool initialise_with_0) { if (!initialise_with_0) { this->buffer.set_initialise_with_zeros(false); - this->buffer.grow(0, this->size_all()); + this->buffer.grow(0, this->size_all() - 1); return; } #if 0 From c3b25d61886b5308f65d92593e9fdbaf00321bd5 Mon Sep 17 00:00:00 2001 From: Evgueni Ovtchinnikov Date: Thu, 12 Dec 2024 12:40:10 +0000 Subject: [PATCH 4/6] followed pre-commit suggestions --- src/buildblock/ProjDataInMemory.cxx | 11 ++++++----- src/include/stir/Array.h | 2 +- src/include/stir/Array.inl | 9 +++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/buildblock/ProjDataInMemory.cxx b/src/buildblock/ProjDataInMemory.cxx index 4571e2a8a..033268e16 100644 --- a/src/buildblock/ProjDataInMemory.cxx +++ b/src/buildblock/ProjDataInMemory.cxx @@ -73,11 +73,12 @@ ProjDataInMemory::ProjDataInMemory(shared_ptr const& exam_info_s void ProjDataInMemory::create_buffer(const bool initialise_with_0) { - if (!initialise_with_0) { - this->buffer.set_initialise_with_zeros(false); - this->buffer.grow(0, this->size_all() - 1); - return; - } + if (!initialise_with_0) + { + this->buffer.set_initialise_with_zeros(false); + this->buffer.grow(0, this->size_all() - 1); + return; + } #if 0 float *b = new float[this->size_all()]; if (initialise_with_0) diff --git a/src/include/stir/Array.h b/src/include/stir/Array.h index 17a42ffe1..6aa4098d1 100644 --- a/src/include/stir/Array.h +++ b/src/include/stir/Array.h @@ -589,7 +589,7 @@ class Array<1, elemT> : public NumericVectorWithOffset */ inline void init(const IndexRange<1>& range, elemT* const data_ptr, bool copy_data); - bool init_with_zeros_= 1; + bool init_with_zeros_ = 1; }; END_NAMESPACE_STIR diff --git a/src/include/stir/Array.inl b/src/include/stir/Array.inl index 6a503a154..a66113167 100644 --- a/src/include/stir/Array.inl +++ b/src/include/stir/Array.inl @@ -584,10 +584,11 @@ Array<1, elemT>::resize(const int min_index, const int max_index) base_type::resize(min_index, max_index); - if (!get_initialise_with_zeros()) { - this->check_state(); - return; - } + if (!get_initialise_with_zeros()) + { + this->check_state(); + return; + } if (oldlength == 0) { From 3ff116d468649a68365bf35d1b1922b19ea86799 Mon Sep 17 00:00:00 2001 From: Evgueni Ovtchinnikov Date: Thu, 12 Dec 2024 16:47:36 +0000 Subject: [PATCH 5/6] adopted the reviewer's suggestions --- src/buildblock/ProjDataInMemory.cxx | 17 ++--------------- src/include/stir/Array.h | 2 +- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/buildblock/ProjDataInMemory.cxx b/src/buildblock/ProjDataInMemory.cxx index 033268e16..bebb7108e 100644 --- a/src/buildblock/ProjDataInMemory.cxx +++ b/src/buildblock/ProjDataInMemory.cxx @@ -73,21 +73,8 @@ ProjDataInMemory::ProjDataInMemory(shared_ptr const& exam_info_s void ProjDataInMemory::create_buffer(const bool initialise_with_0) { - if (!initialise_with_0) - { - this->buffer.set_initialise_with_zeros(false); - this->buffer.grow(0, this->size_all() - 1); - return; - } -#if 0 - float *b = new float[this->size_all()]; - if (initialise_with_0) - memset(b, 0, this->size_all()*sizeof(float)); - return b; -#else - this->buffer.set_initialise_with_zeros(true); - this->buffer = Array<1, float>(static_cast(this->size_all())); -#endif + this->buffer.set_initialise_with_zeros(initialise_with_0); + this->buffer.grow(0, this->size_all() - 1); } ///////////////// /set functions diff --git a/src/include/stir/Array.h b/src/include/stir/Array.h index 6aa4098d1..ccb438a2f 100644 --- a/src/include/stir/Array.h +++ b/src/include/stir/Array.h @@ -589,7 +589,7 @@ class Array<1, elemT> : public NumericVectorWithOffset */ inline void init(const IndexRange<1>& range, elemT* const data_ptr, bool copy_data); - bool init_with_zeros_ = 1; + bool init_with_zeros_ = true; }; END_NAMESPACE_STIR From b05ba0cb93eefdb087136b4b3f7cf9ad13bd4e1d Mon Sep 17 00:00:00 2001 From: Evgueni Ovtchinnikov Date: Tue, 7 Jan 2025 13:02:24 +0000 Subject: [PATCH 6/6] adopted reviewer suggestion on bool arg for grow and resize --- src/buildblock/ProjDataInMemory.cxx | 4 ++-- src/include/stir/Array.h | 4 ++-- src/include/stir/Array.inl | 11 ++++++----- src/include/stir/VectorWithOffset.h | 4 ++-- src/include/stir/VectorWithOffset.inl | 6 +++--- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/buildblock/ProjDataInMemory.cxx b/src/buildblock/ProjDataInMemory.cxx index bebb7108e..0e30cf359 100644 --- a/src/buildblock/ProjDataInMemory.cxx +++ b/src/buildblock/ProjDataInMemory.cxx @@ -73,8 +73,8 @@ ProjDataInMemory::ProjDataInMemory(shared_ptr const& exam_info_s void ProjDataInMemory::create_buffer(const bool initialise_with_0) { - this->buffer.set_initialise_with_zeros(initialise_with_0); - this->buffer.grow(0, this->size_all() - 1); + // this->buffer.set_initialise_with_zeros(initialise_with_0); + this->buffer.grow(0, this->size_all() - 1, initialise_with_0); } ///////////////// /set functions diff --git a/src/include/stir/Array.h b/src/include/stir/Array.h index ccb438a2f..adbae55f0 100644 --- a/src/include/stir/Array.h +++ b/src/include/stir/Array.h @@ -451,13 +451,13 @@ class Array<1, elemT> : public NumericVectorWithOffset inline virtual void grow(const IndexRange<1>& range); // Array::grow initialises new elements to 0 - inline void grow(const int min_index, const int max_index) override; + inline virtual void grow(const int min_index, const int max_index, bool initialise_with_0 = true); //! Array::resize initialises new elements to 0 inline virtual void resize(const IndexRange<1>& range); // Array::resize initialises new elements to 0 - inline void resize(const int min_index, const int max_index) override; + inline virtual void resize(const int min_index, const int max_index, bool initialise_with_0 = true); //! \name access to the data via a pointer //@{ diff --git a/src/include/stir/Array.inl b/src/include/stir/Array.inl index a66113167..dbb7e3f71 100644 --- a/src/include/stir/Array.inl +++ b/src/include/stir/Array.inl @@ -576,15 +576,16 @@ Array<1, elemT>::init(const IndexRange<1>& range, elemT* const data_ptr, bool co template void -Array<1, elemT>::resize(const int min_index, const int max_index) +Array<1, elemT>::resize(const int min_index, const int max_index, bool initialise_with_0) { this->check_state(); const int oldstart = this->get_min_index(); const size_type oldlength = this->size(); - base_type::resize(min_index, max_index); + base_type::resize(min_index, max_index, initialise_with_0); - if (!get_initialise_with_zeros()) + // if (!get_initialise_with_zeros()) + if (!initialise_with_0) { this->check_state(); return; @@ -614,9 +615,9 @@ Array<1, elemT>::resize(const IndexRange<1>& range) template void -Array<1, elemT>::grow(const int min_index, const int max_index) +Array<1, elemT>::grow(const int min_index, const int max_index, bool initialise_with_0) { - resize(min_index, max_index); + resize(min_index, max_index, initialise_with_0); } template diff --git a/src/include/stir/VectorWithOffset.h b/src/include/stir/VectorWithOffset.h index a08c9d099..f7119c63a 100644 --- a/src/include/stir/VectorWithOffset.h +++ b/src/include/stir/VectorWithOffset.h @@ -194,7 +194,7 @@ class VectorWithOffset resize() in a derived class, it is probably safest to overload grow() as well. */ - inline virtual void grow(const int min_index, const int max_index); + inline virtual void grow(const int min_index, const int max_index, bool initialise_with_0 = true); //! grow the range of the vector from 0 to new_size-1, new elements are set to \c T() inline void grow(const unsigned int new_size); @@ -207,7 +207,7 @@ class VectorWithOffset \todo in principle reallocation could be avoided when the new range would fit in the old one by shifting. */ - inline virtual void resize(const int min_index, const int max_index); + inline virtual void resize(const int min_index, const int max_index, bool initialise_with_0 = true); //! change the range of the vector from 0 to new_size-1, new elements are set to \c T() inline void resize(const unsigned int new_size); diff --git a/src/include/stir/VectorWithOffset.inl b/src/include/stir/VectorWithOffset.inl index 371936f3c..fd34a2312 100644 --- a/src/include/stir/VectorWithOffset.inl +++ b/src/include/stir/VectorWithOffset.inl @@ -416,7 +416,7 @@ VectorWithOffset::reserve(const unsigned int new_size) // the new members will be initialised with the default constructor for T template void -VectorWithOffset::resize(const int min_index, const int max_index) +VectorWithOffset::resize(const int min_index, const int max_index, bool initialise_with_0) { this->check_state(); if (min_index > max_index) @@ -482,11 +482,11 @@ VectorWithOffset::resize(const unsigned new_size) // the new members will be initialised with the default constructor for T template void -VectorWithOffset::grow(const int min_index, const int max_index) +VectorWithOffset::grow(const int min_index, const int max_index, bool initialise_with_0) { // allow grow arbitrary when it's zero length assert(length == 0 || (min_index <= this->get_min_index() && max_index >= this->get_max_index())); - this->resize(min_index, max_index); + this->resize(min_index, max_index, initialise_with_0); } template