-
Notifications
You must be signed in to change notification settings - Fork 76
Fix scalar-broadcast fill after a non-inclusive axis (scikit-hep/boost-histogram#960) #431
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
Open
NJManganelli
wants to merge
2
commits into
boostorg:develop
Choose a base branch
from
NJManganelli:address_960
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,19 +103,37 @@ struct index_visitor { | |
| void call_1(std::true_type, const T& value) const { | ||
| // T is compatible value; fill single value N times | ||
|
|
||
| // Optimization: We call call_2 only once and then add the index shift onto the | ||
| // whole array of indices, because it is always the same. This also works if the | ||
| // axis grows during this operation. There are no shifts to apply if the zero-point | ||
| // changes. | ||
| const auto before = *begin_; | ||
| call_2(IsGrowing{}, begin_, value); | ||
| if (is_valid(*begin_)) { | ||
| // since index can be std::size_t or optional_index, must do conversion here | ||
| const auto delta = | ||
| static_cast<std::intptr_t>(*begin_) - static_cast<std::intptr_t>(before); | ||
| for (auto it = begin_ + 1; it != begin_ + size_; ++it) *it += delta; | ||
| } else | ||
| // Compute the contribution of this axis to the linear index once on a | ||
| // fresh index and add it to the whole array of indices. The contribution | ||
| // must not be derived from the change of *begin_, because a previous axis | ||
| // may have already invalidated some entries, including the first one | ||
| // (scikit-hep/boost-histogram#960). Entries that are already invalid stay | ||
| // invalid, all other entries receive the same contribution. | ||
| // | ||
| // The fresh index is biased by stride_, because the contribution of an | ||
| // underflow bin is -stride_ and the index type is unsigned. The bias is | ||
| // subtracted again when the delta is computed. | ||
| index_type idx{stride_}; | ||
| if (IsGrowing::value) { | ||
| // IsGrowing::value is a compile-time constant, the dead branch is | ||
| // eliminated; linearize_growth also handles non-growing axes. | ||
| axis::index_type shift; | ||
| linearize_growth(idx, shift, stride_, axis_, | ||
| try_cast<value_type, std::invalid_argument>(value)); | ||
| // No index shifts to apply if the zero-point changes, since all entries | ||
| // receive the same contribution which is computed after the growth, but | ||
| // the shift must be recorded so that the storage is resized correctly. | ||
| if (shift > 0) *shift_ += shift; | ||
| } else { | ||
| linearize(idx, stride_, axis_, try_cast<value_type, std::invalid_argument>(value)); | ||
| } | ||
| if (is_valid(idx)) { | ||
| const auto delta = static_cast<std::intptr_t>(static_cast<std::size_t>(idx)) - | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this relying on two's complement of the signed -> unsigned -> signed cast or just a superfluous cast? I need another comment here that explains that this double cast is not superfluous but intentional. |
||
| static_cast<std::intptr_t>(stride_); | ||
| for (auto it = begin_; it != begin_ + size_; ++it) *it += delta; | ||
| } else { | ||
| std::fill(begin_, begin_ + size_, invalid_index); | ||
| } | ||
| } | ||
|
|
||
| template <class T> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
looks like this replaces call_2 completely, in that case, those call_2 functions should be removed.
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.
Likewise, call_1 then doesn't need to be called call_1 anymore, just call_impl.