-
Notifications
You must be signed in to change notification settings - Fork 95
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
base: master
Are you sure you want to change the base?
Conversation
Ubuntu jobs fail, see #1550 MacOS is unaffected by that, but has
(note that this will be seen in Debug mode only. Otherwise it will likely lead to a segfault) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this would work, it could be a bit surprising to the user, as the effect of set_initialise_with_zeros()
would be permanent. In the use-case that you have in mind, that'd be fine, but it'd have to come with serious warnings in the doxygen.
I'm rather in favour of having an explicit bool
argument to grow
and resize
(defaulting to true
). We could then have that for the constructor as well.
Please also use |
No point in pushing until I've sorted out #1552, which you'll have to merge here. |
#1552 is now merged, so merge |
I still think this would be a better option. @danieldeidda @markus-jehl what do you think? |
I tend to agree with @KrisThielemans, that explicitly stating it may be cleaner rather than relying on an internal setting on individual instances of the Array class. Just because I can imagine running into weird issues and struggling for a while before noticing what is going on 😅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some small comments, but realise now a conceptual difficulty: you had to add an arg to the virtual VectorWithOffset::grow
. However, there it is unused (and would necessarily make sense, as "0" might not work for an arbitrary type). This is therefore still surprising to the user.
All this is more complicated due to the fact that adding default values to virtual functions is not possible and changing default values for virtual functions isn't recommended, see e.g. https://softwareengineering.stackexchange.com/questions/355074/is-overriding-a-pure-virtual-function-with-default-arguments-good-or-bad.
Could we do something like this
// make a "no_op" function object
virtual VectorWithOffset::grow(min_idx, max_idx, func& f); // apply `f` on new elements
virtual VectorWithOffset::grow(min_idx, max_idx); // no `f`, i.e. current version
virtual Array::grow(min_idx, max_idx); // assign with 0 (i.e. current version)
using virtual Array::grow(min_idx, max_idx, f); // use base-class version
if we don't want to initialise, then we could pass the no_op function.
Difficulties:
- it's a bit ugly to have to pass
no_op
(caused by the current Array::grow default being the wrong thing) - to avoid overhead, we'd have to detect if
f == no_op
and avoids loops.
Not sure how to resolve this, unfortunately. Any ideas?
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
void set_initialise_with_zeros(bool iwz) | ||
{ | ||
init_with_zeros_ = iwz; | ||
} | ||
bool get_initialise_with_zeros() const | ||
{ | ||
return init_with_zeros_; | ||
} |
There was a problem hiding this comment.
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
|
||
bool init_with_zeros_ = true; |
There was a problem hiding this comment.
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
Changes in this pull request
Unnecessary 0-fill of newly created acquisition data reduced.
Testing performed
The following SIRF Python script (employing STIR via SIRF Python interface) was used for comparing STIR's
ProjDataInMemory
algebra performance withnumpy
:With a recent STIR, the output looks like
demonstrating that STIR's
ProjDataInMemory
algebra is much less efficient thannumpy
algebra applied toProjDataInMemory
data copied intonumpy
array. The investigation of this issue revealed that a considerable chunk of the computation time was wasted on zero-filling the newly createdProjDataInMemory
object wherex + y
data is to end up.This PR succeeded in reducing such waste, so that the timings have become
Related issues
Checklist before requesting a review
documentation/release_XXX.md
has been updated with any functionality change (if applicable)