Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to speed up STIR acquisition data algebra by avoiding unnecessary 0-fill of newly created data. #1549

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
10 changes: 2 additions & 8 deletions src/buildblock/ProjDataInMemory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,8 @@ ProjDataInMemory::ProjDataInMemory(shared_ptr<const ExamInfo> const& exam_info_s
void
ProjDataInMemory::create_buffer(const bool initialise_with_0)
{
#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 = Array<1, float>(static_cast<int>(this->size_all()));
#endif
// this->buffer.set_initialise_with_zeros(initialise_with_0);
this->buffer.grow(0, this->size_all() - 1, initialise_with_0);
}

///////////////// /set functions
Expand Down
15 changes: 13 additions & 2 deletions src/include/stir/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,13 @@ class Array<1, elemT> : public NumericVectorWithOffset<elemT, elemT>
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inline virtual void grow(const int min_index, const int max_index, bool initialise_with_0 = true);
inline void grow(const int min_index, const int max_index, bool initialise_with_0 = true) override;


//! 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inline virtual void resize(const int min_index, const int max_index, bool initialise_with_0 = true);
inline void resize(const int min_index, const int max_index, bool initialise_with_0 = true) override;


//! \name access to the data via a pointer
//@{
Expand Down Expand Up @@ -569,6 +569,15 @@ class Array<1, elemT> : public NumericVectorWithOffset<elemT, elemT>
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_;
}
Comment on lines +572 to +579
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will need to be removed


private:
// Make sure we can call init() recursively.
template <int num_dimensions2, class elemT2>
Expand All @@ -579,6 +588,8 @@ class Array<1, elemT> : public NumericVectorWithOffset<elemT, elemT>
\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_ = true;
Comment on lines +591 to +592
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will need to be removed

};

END_NAMESPACE_STIR
Expand Down
16 changes: 12 additions & 4 deletions src/include/stir/Array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,21 @@ Array<1, elemT>::init(const IndexRange<1>& range, elemT* const data_ptr, bool co

template <class elemT>
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 (!initialise_with_0)
{
this->check_state();
return;
}

if (oldlength == 0)
{
for (int i = this->get_min_index(); i <= this->get_max_index(); i++)
Expand All @@ -607,9 +615,9 @@ Array<1, elemT>::resize(const IndexRange<1>& range)

template <class elemT>
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 <class elemT>
Expand Down
4 changes: 2 additions & 2 deletions src/include/stir/VectorWithOffset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/include/stir/VectorWithOffset.inl
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ VectorWithOffset<T>::reserve(const unsigned int new_size)
// the new members will be initialised with the default constructor for T
template <class T>
void
VectorWithOffset<T>::resize(const int min_index, const int max_index)
VectorWithOffset<T>::resize(const int min_index, const int max_index, bool initialise_with_0)
{
this->check_state();
if (min_index > max_index)
Expand Down Expand Up @@ -482,11 +482,11 @@ VectorWithOffset<T>::resize(const unsigned new_size)
// the new members will be initialised with the default constructor for T
template <class T>
void
VectorWithOffset<T>::grow(const int min_index, const int max_index)
VectorWithOffset<T>::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 <class T>
Expand Down
Loading