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

Update operator+= logic for composite NodeCollection #2278

Merged
merged 12 commits into from
Feb 16, 2022

Conversation

mlober
Copy link
Contributor

@mlober mlober commented Jan 26, 2022

The operator+= of a node collection composite was changed in order to avoid the usage of the very inefficient operator++ which was previously called by operator+=. Instead of using the operator++ to loop through a NCO composite until the desired index is reached, one can now directly "jump" to this index in the new operator+=.
This change is necessary to avoid extremely large nest.connect-times when the source is a node collection composite object.

@heplesser heplesser added I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) S: Normal Handle this with default priority T: Enhancement New functionality, model or documentation labels Jan 27, 2022
Copy link
Contributor

@hakonsbm hakonsbm left a comment

Choose a reason for hiding this comment

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

Thanks for this improvement!

Comment on lines 721 to 748
element_idx_ += n * step_;
// If we went past the size of the primitive, we need to adjust the element
// and primitive part indices.
size_t primitive_size = composite_collection_->parts_[ part_idx_ ].size();
while ( element_idx_ >= primitive_size )
{
operator++();
element_idx_ = element_idx_ - primitive_size;
++part_idx_;
if ( part_idx_ < composite_collection_->parts_.size() )
{
primitive_size = composite_collection_->parts_[ part_idx_ ].size();
}
}
// If we went past the end of the composite, we need to adjust the
// position of the iterator.
if ( composite_collection_->end_offset_ != 0 or composite_collection_->end_part_ != 0 )
{
if ( part_idx_ >= composite_collection_->end_part_ and element_idx_ >= composite_collection_->end_offset_ )
{
part_idx_ = composite_collection_->end_part_;
element_idx_ = composite_collection_->end_offset_;
}
}
else if ( part_idx_ >= composite_collection_->parts_.size() )
{
auto end_of_composite = composite_collection_->end();
part_idx_ = end_of_composite.part_idx_;
element_idx_ = end_of_composite.element_idx_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Except for the different steplength in line 721, this is a duplication of the code from nc_const_iterator::operator++(). I think it would therefore be better to put this code in a separate helper function (a private function within nc_const_iterator), perhaps with the steplength as argument, and call that function from both operator++() and operator+=().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, it would make the code much nicer. Thanks! I will get to it in the next days.

@mlober
Copy link
Contributor Author

mlober commented Feb 8, 2022

I now put the duplicated code into a separate function "cond_update_idx".
The static checks fail due to formatting issues in functions that I have not touched. I reported this issue here: #2276

@mlober mlober requested a review from hakonsbm February 9, 2022 13:02
Copy link
Contributor

@hakonsbm hakonsbm left a comment

Choose a reason for hiding this comment

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

Thanks for the update! I have a couple more suggestions.

@@ -137,6 +137,8 @@ class nc_const_iterator
size_t offset,
size_t step = 1 );

void cond_update_idx(); //!< conditionally updates element_idx and part_idx
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer this to have a less cryptic name, for example composite_update_indices_() or something similar. For private class members, we end the name with a _ as a reminder that it is private. Also, for consistency, put the documentation above the declaration in a C-style comment.

{
operator++();
}
element_idx_ += n * step_;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the same as in the primitive_collection_ case, so it can be moved out before the if/else. Then the

if (  primitive_collection_ )
{...} 
else 
{...}

can become just

if ( composite_collection_ )
{...}

part_idx_ = end_of_composite.part_idx_;
element_idx_ = end_of_composite.element_idx_;
}
cond_update_idx();
Copy link
Contributor

@hakonsbm hakonsbm Feb 10, 2022

Choose a reason for hiding this comment

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

The element_idx_ += step_ above this line is the same in both cases here, so it can be moved out before the if/else.

@mlober mlober requested a review from hakonsbm February 10, 2022 16:32
@mlober
Copy link
Contributor Author

mlober commented Feb 10, 2022

Thanks for your feedback. I included your suggestions in this new version.
Again: The static checks fail due to formatting issues in functions that I have not touched. I reported this issue here: #2276

@hakonsbm
Copy link
Contributor

@mlober There are some differences between different clang-format versions. We are enforcing formatting with clang-format 9 now, so you can try formatting node_collection.h with that and see if that fixes the problem.

Copy link
Contributor

@hakonsbm hakonsbm left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@stinebuu stinebuu left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

@stinebuu stinebuu changed the title NCO composite - new operator+= Update operator+= logic for composite NodeCollection Feb 16, 2022
@stinebuu stinebuu merged commit 4ea78ae into nest:master Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) S: Normal Handle this with default priority T: Enhancement New functionality, model or documentation
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants