Skip to content

Commit 9c256b1

Browse files
atmyersWeiqunZhang
andauthored
Allow users to change the default vector growth stategy (#3389)
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 <[email protected]>
1 parent 4a4e8b3 commit 9c256b1

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

Src/Base/AMReX.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ amrex::Initialize (int& argc, char**& argv, bool build_parm_parse,
604604
iMultiFab::Initialize();
605605
VisMF::Initialize();
606606
AsyncOut::Initialize();
607+
VectorGrowthStrategy::Initialize();
607608

608609
#ifdef AMREX_USE_EB
609610
EB2::Initialize();

Src/Base/AMReX_PODVector.H

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ namespace amrex
218218
}
219219
}
220220

221+
namespace VectorGrowthStrategy
222+
{
223+
extern AMREX_EXPORT Real growth_factor;
224+
inline Real GetGrowthFactor () { return growth_factor; }
225+
226+
void Initialize ();
227+
}
228+
221229
template <class T, class Allocator = std::allocator<T> >
222230
class PODVector : public Allocator
223231
{
@@ -612,7 +620,8 @@ namespace amrex
612620

613621
while (new_capacity < (capacity() + a_num_to_be_added))
614622
{
615-
new_capacity = (3 * new_capacity + 1)/2;
623+
new_capacity = static_cast<size_type>(
624+
VectorGrowthStrategy::GetGrowthFactor() * static_cast<Real>(new_capacity + 1));
616625
}
617626

618627
return new_capacity;

Src/Base/AMReX_PODVector.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <AMReX_PODVector.H>
2+
#include <AMReX_ParmParse.H>
3+
#include <AMReX_REAL.H>
4+
5+
namespace amrex::VectorGrowthStrategy
6+
{
7+
Real growth_factor = 1.5_rt;
8+
9+
void Initialize () {
10+
ParmParse pp("amrex");
11+
pp.queryAdd("vector_growth_factor", growth_factor);
12+
13+
// clamp user input to reasonable values
14+
constexpr Real min_factor = 1.05_rt;
15+
constexpr Real max_factor = 4._rt;
16+
17+
if (growth_factor < min_factor) {
18+
if (Verbose()) {
19+
amrex::Print() << "Warning: user-provided vector growth factor is too small."
20+
<< " Clamping to " << min_factor << ". \n";
21+
}
22+
growth_factor = min_factor;
23+
}
24+
25+
if (growth_factor > max_factor) {
26+
if (Verbose()) {
27+
amrex::Print() << "Warning: user-provided vector growth factor is too large."
28+
<< " Clamping to " << max_factor << ". \n";
29+
}
30+
growth_factor = max_factor;
31+
}
32+
}
33+
}

Src/Base/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ foreach(D IN LISTS AMReX_SPACEDIM)
2525
AMReX_Exception.H
2626
AMReX_Extension.H
2727
AMReX_PODVector.H
28+
AMReX_PODVector.cpp
2829
AMReX_ParmParse.cpp
2930
AMReX_parmparse_fi.cpp
3031
AMReX_ParmParse.H

Src/Base/Make.package

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ C$(AMREX_BASE)_headers += AMReX_Demangle.H AMReX_Extension.H
1717
C$(AMREX_BASE)_headers += AMReX_GpuComplex.H
1818

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

2122
C$(AMREX_BASE)_headers += AMReX_BlockMutex.H
2223
C$(AMREX_BASE)_sources += AMReX_BlockMutex.cpp

0 commit comments

Comments
 (0)