Skip to content

PODVector: Add extra capacity#4734

Merged
WeiqunZhang merged 5 commits intoAMReX-Codes:developmentfrom
WeiqunZhang:pc_size_sqrt
Nov 6, 2025
Merged

PODVector: Add extra capacity#4734
WeiqunZhang merged 5 commits intoAMReX-Codes:developmentfrom
WeiqunZhang:pc_size_sqrt

Conversation

@WeiqunZhang
Copy link
Copy Markdown
Member

Add some extra room when we call PODVector::resize and reserve. The extra capacity is computed as 3*sqrt(capacity) suggested by @AlexanderSinn, and is capped at 10%. This might help particle codes avoid memory re-allocation.

Add some extra room when we call `PODVector::resize` and `reserve`. The
extra capacity is computed as 3*sqrt(capacity) suggested by @AlexanderSinn,
and is capped at 10%. This might help particle codes avoid memory
re-allocation.
@WeiqunZhang WeiqunZhang marked this pull request as draft October 26, 2025 23:09
@WeiqunZhang WeiqunZhang marked this pull request as ready for review October 27, 2025 21:51
@WeiqunZhang WeiqunZhang requested a review from atmyers October 27, 2025 21:51
@AlexanderSinn
Copy link
Copy Markdown
Member

I feel like there should still be a way to resize a vector to the exact capacity that is asked for. Only if the vector is used in a particle container with multiple tiles where particles are exchanged using a thermal process is this a good strategy. If there is only one tile or if the vector is used to store a grid or metadata, the extra capacity is not needed.

@WeiqunZhang
Copy link
Copy Markdown
Member Author

WeiqunZhang commented Oct 31, 2025

What's the downside?

@AlexanderSinn
Copy link
Copy Markdown
Member

Just a waste of (a little bit of) memory. It also could get very confusing when trying to implement custom allocation size strategies on top of PODVector.

@WeiqunZhang
Copy link
Copy Markdown
Member Author

Because it scales as sqrt(n), the extra memory cost is not an issue. As for implementing other growth strategies such as what are in #4735 , here is what I think. push_back and insert are rarely used and should not be used for GPU codes anyway. We probably do not care about constructors either. So our focus should be on just resize and reserve. In this PR, the growth strategy is implemented in function grow_podvector_capacity. It should not be hard at all to allow other strategies. In the current PR, grow_podvector_capacity is being used in 4 places. All we need is to have an extra parameter to the function, and the parameter can be obtained from the resize and reserve function as it's already being done in #4735. Re: ParmParse parameters in #4735, I think it's better to have them in particle containers, not in the vector class. I think that would be much more flexible and less confusing by having a fixed default in the vector and the change needs to be explicit. I think the default in vector should be that we have a little bit extra that scales as sqrt(n), because that cost is really not that much.

@AlexanderSinn
Copy link
Copy Markdown
Member

Sounds good. I am fine with merging this PR as is, and after that I will make a follow-up to add exact and geometric strategies to resize and reserve only, using grow_podvector_capacity. I also agree that those are the most important functions, and insert/push_back shouldn't really be used. Should the interface for resize and reserve include an optional argument or add new funcitons?

Option 1:

vec.reserve(n, amrex::GrowthStrategy::Geometric);
vec.resize(n, amrex::GrowthStrategy::Exact);
vec.resize(n /*, amrex::GrowthStrategy::Poisson*/); // Poisson is the default

Option 2:

vec.reserve_geometric_grow(n);
vec.resize_exact_grow(n);
vec.resize(n); // uses Poisson grow

Option 3:

vec.reserve_geometric(n);
vec.resize_exact(n);
vec.resize(n); // uses Poisson grow

@WeiqunZhang
Copy link
Copy Markdown
Member Author

I prefer Option 1. It's easier for the users.

Copy link
Copy Markdown
Member

@ax3l ax3l left a comment

Choose a reason for hiding this comment

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

Thank you! 🚀 ✨

@WeiqunZhang WeiqunZhang merged commit f54bcfa into AMReX-Codes:development Nov 6, 2025
73 checks passed
@WeiqunZhang WeiqunZhang deleted the pc_size_sqrt branch November 6, 2025 00:36
atmyers pushed a commit to atmyers/amrex that referenced this pull request Nov 6, 2025
Add some extra room when we call `PODVector::resize` and `reserve`. The
extra capacity is computed as 3*sqrt(capacity) suggested by
@AlexanderSinn, and is capped at 10%. This might help particle codes
avoid memory re-allocation.
WeiqunZhang pushed a commit that referenced this pull request Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants