Skip to content

Commit

Permalink
Allow users to change the default vector growth stategy (#3389)
Browse files Browse the repository at this point in the history
This allows users to set at runtime an alternate to the default vector
growth factor of 1.5. This could help save memory in simulations that
use a lot of particles and are close to the capacity of the GPU device.

The proposed changes:
- [ ] fix a bug or incorrect behavior in AMReX
- [x] add new capabilities to AMReX
- [ ] changes answers in the test suite to more than roundoff level
- [ ] are likely to significantly affect the results of downstream AMReX
users
- [ ] include documentation in the code and/or rst files, if appropriate

---------

Co-authored-by: Weiqun Zhang <WeiqunZhang@lbl.gov>
  • Loading branch information
atmyers and WeiqunZhang committed Jun 29, 2023
1 parent 4a4e8b3 commit 9c256b1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions Src/Base/AMReX.cpp
Expand Up @@ -604,6 +604,7 @@ amrex::Initialize (int& argc, char**& argv, bool build_parm_parse,
iMultiFab::Initialize();
VisMF::Initialize();
AsyncOut::Initialize();
VectorGrowthStrategy::Initialize();

#ifdef AMREX_USE_EB
EB2::Initialize();
Expand Down
11 changes: 10 additions & 1 deletion Src/Base/AMReX_PODVector.H
Expand Up @@ -218,6 +218,14 @@ namespace amrex
}
}

namespace VectorGrowthStrategy
{
extern AMREX_EXPORT Real growth_factor;
inline Real GetGrowthFactor () { return growth_factor; }

void Initialize ();
}

template <class T, class Allocator = std::allocator<T> >
class PODVector : public Allocator
{
Expand Down Expand Up @@ -612,7 +620,8 @@ namespace amrex

while (new_capacity < (capacity() + a_num_to_be_added))
{
new_capacity = (3 * new_capacity + 1)/2;
new_capacity = static_cast<size_type>(
VectorGrowthStrategy::GetGrowthFactor() * static_cast<Real>(new_capacity + 1));
}

return new_capacity;
Expand Down
33 changes: 33 additions & 0 deletions Src/Base/AMReX_PODVector.cpp
@@ -0,0 +1,33 @@
#include <AMReX_PODVector.H>
#include <AMReX_ParmParse.H>
#include <AMReX_REAL.H>

namespace amrex::VectorGrowthStrategy
{
Real growth_factor = 1.5_rt;

void Initialize () {
ParmParse pp("amrex");
pp.queryAdd("vector_growth_factor", growth_factor);

// clamp user input to reasonable values
constexpr Real min_factor = 1.05_rt;
constexpr Real max_factor = 4._rt;

if (growth_factor < min_factor) {
if (Verbose()) {
amrex::Print() << "Warning: user-provided vector growth factor is too small."
<< " Clamping to " << min_factor << ". \n";
}
growth_factor = min_factor;
}

if (growth_factor > max_factor) {
if (Verbose()) {
amrex::Print() << "Warning: user-provided vector growth factor is too large."
<< " Clamping to " << max_factor << ". \n";
}
growth_factor = max_factor;
}
}
}
1 change: 1 addition & 0 deletions Src/Base/CMakeLists.txt
Expand Up @@ -25,6 +25,7 @@ foreach(D IN LISTS AMReX_SPACEDIM)
AMReX_Exception.H
AMReX_Extension.H
AMReX_PODVector.H
AMReX_PODVector.cpp
AMReX_ParmParse.cpp
AMReX_parmparse_fi.cpp
AMReX_ParmParse.H
Expand Down
1 change: 1 addition & 0 deletions Src/Base/Make.package
Expand Up @@ -17,6 +17,7 @@ C$(AMREX_BASE)_headers += AMReX_Demangle.H AMReX_Extension.H
C$(AMREX_BASE)_headers += AMReX_GpuComplex.H

C$(AMREX_BASE)_headers += AMReX_PODVector.H
C$(AMREX_BASE)_sources += AMReX_PODVector.cpp

C$(AMREX_BASE)_headers += AMReX_BlockMutex.H
C$(AMREX_BASE)_sources += AMReX_BlockMutex.cpp
Expand Down

0 comments on commit 9c256b1

Please sign in to comment.