Skip to content

Commit

Permalink
Add ParmParse features for WarpX (#4149)
Browse files Browse the repository at this point in the history
* Add `queryIntAsDouble` and `queryarrIntAsDouble`.

* Make recursion detection more robust for WarpX useage.

* Add a test.
  • Loading branch information
WeiqunZhang authored Sep 15, 2024
1 parent 0286385 commit dfe1747
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=40M
export CCACHE_MAXSIZE=100M
export CCACHE_EXTRAFILES=${{ github.workspace }}/.clang-tidy
export CCACHE_LOGFILE=${{ github.workspace }}/ccache.log.txt
ccache -z
Expand Down Expand Up @@ -360,7 +360,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=315M
export CCACHE_MAXSIZE=400M
export CCACHE_EXTRAFILES=${{ github.workspace }}/.clang-tidy
export CCACHE_LOGFILE=${{ github.workspace }}/ccache.log.txt
ccache -z
Expand Down Expand Up @@ -616,7 +616,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=30M
export CCACHE_MAXSIZE=100M
export CCACHE_EXTRAFILES=${{ github.workspace }}/.clang-tidy
export CCACHE_LOGFILE=${{ github.workspace }}/ccache.log.txt
ccache -z
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/hip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=100M
export CCACHE_MAXSIZE=150M
ccache -z
source /etc/profile.d/rocm.sh
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=40M
export CCACHE_MAXSIZE=100M
ccache -z
source /etc/profile.d/rocm.sh
Expand Down Expand Up @@ -142,7 +142,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=60M
export CCACHE_MAXSIZE=100M
ccache -z
./configure --dim 2 --with-hip yes --enable-eb yes --enable-xsdk-defaults yes --with-mpi no --with-omp no --single-precision yes --single-precision-particles yes
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/intel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=45M
export CCACHE_MAXSIZE=100M
export CCACHE_DEPEND=1
ccache -z
Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=55M
export CCACHE_MAXSIZE=150M
export CCACHE_DEPEND=1
ccache -z
Expand Down Expand Up @@ -126,7 +126,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=55M
export CCACHE_MAXSIZE=100M
export CCACHE_DEPEND=1
ccache -z
Expand Down Expand Up @@ -175,7 +175,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=55M
export CCACHE_MAXSIZE=100M
export CCACHE_DEPEND=1
ccache -z
Expand Down Expand Up @@ -221,7 +221,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=40M
export CCACHE_MAXSIZE=100M
ccache -z
set +e
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: |
export CCACHE_COMPRESS=1
export CCACHE_COMPRESSLEVEL=10
export CCACHE_MAXSIZE=40M
export CCACHE_MAXSIZE=100M
export CCACHE_EXTRAFILES=${{ github.workspace }}/.clang-tidy
export CCACHE_LOGFILE=${{ github.workspace }}/ccache.log.txt
ccache -z
Expand Down
64 changes: 64 additions & 0 deletions Src/Base/AMReX_ParmParse.H
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,70 @@ public:
}
}

//! \brief Query integer with Parser, but treat the number as double
//! precision during parsing. The final result is cast to integer. It
//! may result in a runtime error if the conversion is not safe.
template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
int queryAsDouble (const char* name, T& ref) const
{
double dref;
int exist = queryWithParser(name, dref);
if (exist) {
dref = std::round(dref);
ref = static_cast<T>(dref);
if (static_cast<double>(ref) != dref) {
amrex::Abort("ParmParse:: queryAsDouble is not safe");
}
}
return exist;
}

//! \brief Query integer array with Parser, but treat the numbers as
//! double precision uring parsing. The final results are cast to
//! integers. It may result in a runtime error if the conversion is not
//! safe.
template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
int queryarrAsDouble (const char* name, int nvals, T* ref) const
{
std::vector<double> dref(nvals);
int exist = queryarrWithParser(name, nvals, dref.data());
if (exist) {
for (int i = 0; i < nvals; ++i) {
dref[i] = std::round(dref[i]);
ref[i] = static_cast<T>(dref[i]);
if (static_cast<double>(ref[i]) != dref[i]) {
amrex::Abort("ParmParse:: queryarrAsDouble is not safe");
}
}
}
return exist;
}

//! \brief Get integer with Parser, but treat the number as double
//! precision during parsing. The final result is cast to integer. It
//! may result in a runtime error if the conversion is not safe.
template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
void getAsDouble (const char* name, T& ref) const
{
int exist = this->queryAsDouble(name, ref);
if (!exist) {
amrex::Error(std::string("ParmParse::getAsDouble: failed to get ")+name);
}
}

//! \brief Get integer array with Parser, but treat the numbers as
//! double precision uring parsing. The final results are cast to
//! integers. It may result in a runtime error if the conversion is not
//! safe.
template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
void getarrAsDouble (const char* name, int nvals, T* ref) const
{
int exist = this->queryarrAsDouble(name, nvals, ref);
if (!exist) {
amrex::Error(std::string("ParmParse::getarrAsDouble: failed to get ")+name);
}
}

//! Remove given name from the table.
int remove (const char* name);

Expand Down
20 changes: 17 additions & 3 deletions Src/Base/AMReX_ParmParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,20 +991,34 @@ pp_make_parser (std::string const& func, Vector<std::string> const& vars,
symbols.erase(var);
}

bool recursive = false;
auto& recursive_symbols = g_parser_recursive_symbols[OpenMP::get_thread_num()];

for (auto const& s : symbols) {
value_t v = 0;
bool r = false;
for (auto const& pf : prefixes) {
std::string pfs = pf + s;
if (auto found = recursive_symbols.find(pfs); found != recursive_symbols.end()) {
recursive = true;
continue;
}
if (use_querywithparser) {
r = squeryWithParser(table, parser_prefix, pf+s, v);
r = squeryWithParser(table, parser_prefix, pfs, v);
} else {
r = squeryval(table, parser_prefix, pf+s, v,
r = squeryval(table, parser_prefix, pfs, v,
ParmParse::FIRST, ParmParse::LAST);
}
if (r) { break; }
}
if (r == false) {
amrex::Error("ParmParse: failed to parse " + func);
std::string msg("ParmParse: failed to parse "+func);
if (recursive) {
msg.append(" due to recursive symbol ").append(s);
} else {
msg.append(" due to unknown symbol ").append(s);
}
amrex::Error(msg);
}
parser.setConstant(s, v);
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ else()
# List of subdirectories to search for CMakeLists.
#
set( AMREX_TESTS_SUBDIRS Amr AsyncOut CLZ CTOParFor DeviceGlobal Enum
MultiBlock MultiPeriod Parser Parser2 Reinit
MultiBlock MultiPeriod ParmParse Parser Parser2 Reinit
RoundoffDomain)

if (AMReX_PARTICLES)
Expand Down
9 changes: 9 additions & 0 deletions Tests/ParmParse/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
foreach(D IN LISTS AMReX_SPACEDIM)
set(_sources main.cpp)
set(_input_files inputs)

setup_test(${D} _sources _input_files)

unset(_sources)
unset(_input_files)
endforeach()
24 changes: 24 additions & 0 deletions Tests/ParmParse/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
AMREX_HOME ?= ../../amrex

DEBUG = TRUE

DIM = 3

COMP = gcc

USE_MPI = FALSE
USE_OMP = FALSE
USE_CUDA = FALSE
USE_HIP = FALSE
USE_SYCL = FALSE

BL_NO_FORT = TRUE

TINY_PROFILE = FALSE

include $(AMREX_HOME)/Tools/GNUMake/Make.defs

include ./Make.package
include $(AMREX_HOME)/Src/Base/Make.package

include $(AMREX_HOME)/Tools/GNUMake/Make.rules
1 change: 1 addition & 0 deletions Tests/ParmParse/Make.package
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CEXE_sources += main.cpp
62 changes: 62 additions & 0 deletions Tests/ParmParse/inputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

amrex.signal_handling = 0
amrex.throw_exception = 1
amrex.v = 0

name = "I am w" \
"line 2"

b = ((1, 2, 3) (7, 8,9) (1,0, 1))

# three numbers. whitespaces inside `""` are okay.
f = 3+4 99 "5 + 6"

# two numbers. `\` is for continuation
g = 3.1+4.1 \
5.0+6.6

# two numbers unless using [query|get]WithParser
w = 1 -2

my_constants.alpha = 5.
amrex.c = c

# must use [query|get]WithParser
amrex.foo = sin( pi/2 ) + alpha + -amrex.c**2.5/c^2

# either [query|get] or [query|get]WithParser is okay
amrex.bar = sin(pi/2)+alpha+-amrex.c**2.5/c^2

# one string across multiple lines
amrex.bar2 = "sin(pi/2)+alpha+
-amrex.c**2.5/c^2"

geom.prob_lo = -2*sin(pi/4)/sqrt(2) -sin(pi/2)-cos(pi/2) (sin(pi*3/2)+cos(pi*3/2))

# three numbers. `\` is for continuation
geom.prob_hi = "2*sin(pi/4)/sqrt(2)" \
"sin(pi/2) + cos(pi/2)" \
-(sin(pi*3/2)+cos(pi*3/2))

long_int_1 = 123456789012345
long_int_2 = 123'456'789'012'345
long_int_3 = 1.23456789012345e14

# recursion like this is not allowed
code.a = code.b
code.b = code.c
code.c = code.d
code.d = code.a

# Recursion like this is allowed, if my_constants is added as parser prefix.
# It's same as max_steps = my_constants.max_steps
my_constants.max_steps = 40
max_steps = max_steps
warpx.max_steps = max_steps

# query int as double
my_constants.lx = 40.e-6
my_constants.dx = 6.25e-7
my_constants.nx = lx/dx
n_cell = nx nx nx
ny = nx
Loading

0 comments on commit dfe1747

Please sign in to comment.