Skip to content

Commit 21eca10

Browse files
committed
ParmParse: Print parsed values as comments in prettyPrint
Below is an example of the difference of the prettyPrint output. ``` < electrons.charge = -q_e # -1.6021766339999999e-19 < electrons.density = n0 # 9.9999999999999992e+22 --- > electrons.charge = -q_e > electrons.density = n0 ```
1 parent c4fcda1 commit 21eca10

File tree

2 files changed

+144
-34
lines changed

2 files changed

+144
-34
lines changed

Src/Base/AMReX_ParmParse.H

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ public:
17101710
// vector<vector<std::string>>.
17111711
std::vector<std::vector<std::string>> m_vals;
17121712
mutable Long m_count = 0;
1713-
std::variant<
1713+
mutable std::variant<
17141714
std::string*,
17151715
bool*,
17161716
int*,
@@ -1721,6 +1721,8 @@ public:
17211721
float*,
17221722
double*
17231723
> m_typehint = static_cast<std::string*>(nullptr);
1724+
mutable std::vector<std::variant<bool, int, long, long long, float, double>> m_last_vals;
1725+
mutable bool m_parsed = false;
17241726
};
17251727
using Table = std::unordered_map<std::string, PP_entry>;
17261728

Src/Base/AMReX_ParmParse.cpp

Lines changed: 141 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,29 @@ namespace
4848
{
4949

5050
std::string pp_to_pretty_string (std::string const& name,
51-
std::vector<std::string> const& vals)
51+
std::vector<std::string> const& vals,
52+
ParmParse::PP_entry const* entry)
5253
{
5354
std::stringstream ss;
5455
ss << name << " =";
5556
for (auto const& v : vals) {
5657
ss << " " << v;
5758
}
59+
if (entry && entry->m_parsed && ! entry->m_last_vals.empty()) {
60+
int min_col = 36;
61+
int pad = min_col - static_cast<int>(ss.str().size());
62+
if (pad > 0) {
63+
ss << std::string(pad, ' ');
64+
}
65+
ss.precision(17);
66+
ss << " #";
67+
for (auto const& x : entry->m_last_vals) {
68+
std::visit([&] (auto&& arg)
69+
{
70+
ss << " " << arg;
71+
}, x);
72+
}
73+
}
5874
return ss.str();
5975
}
6076

@@ -667,6 +683,21 @@ bool pp_parser (const ParmParse::Table& table, const std::string& parser_prefix,
667683
const std::string& name, const std::string& val, T& ref,
668684
bool use_querywithparser);
669685

686+
template <typename T>
687+
void pp_entry_set_last_val (ParmParse::PP_entry const& entry, int ival, T ref, bool parsed)
688+
{
689+
#ifdef AMREX_USE_OMP
690+
#pragma omp single nowait
691+
#endif
692+
{
693+
if (ival >= entry.m_last_vals.size()) {
694+
entry.m_last_vals.resize(ival+1);
695+
}
696+
entry.m_last_vals[ival] = ref;
697+
if (parsed) { entry.m_parsed = true; }
698+
}
699+
}
700+
670701
template <class T>
671702
bool
672703
squeryval (const ParmParse::Table& table,
@@ -684,6 +715,17 @@ squeryval (const ParmParse::Table& table,
684715
{
685716
return false;
686717
}
718+
719+
auto const& entry = table.at(name);
720+
721+
#ifdef AMREX_USE_OMP
722+
#pragma omp single nowait
723+
#endif
724+
{
725+
using T_ptr = std::decay_t<T>*;
726+
entry.m_typehint = static_cast<T_ptr>(nullptr);
727+
}
728+
687729
//
688730
// Does it have ival values?
689731
//
@@ -705,17 +747,21 @@ squeryval (const ParmParse::Table& table,
705747

706748
const std::string& valname = (*def)[ival];
707749

708-
bool ok = is(valname, ref);
709-
if ( !ok )
710-
{
711-
if constexpr (std::is_same_v<T,bool> ||
712-
std::is_same_v<T,int> ||
713-
std::is_same_v<T,long> ||
714-
std::is_same_v<T,long long> ||
715-
std::is_same_v<T,float> ||
716-
std::is_same_v<T,double>)
717-
{
750+
constexpr bool is_integral_floating = (std::is_same_v<T,bool> ||
751+
std::is_same_v<T,int> ||
752+
std::is_same_v<T,long> ||
753+
std::is_same_v<T,long long> ||
754+
std::is_same_v<T,float> ||
755+
std::is_same_v<T,double>);
756+
757+
if (is(valname, ref)) {
758+
if constexpr (is_integral_floating) {
759+
pp_entry_set_last_val(entry, ival, ref, false);
760+
}
761+
} else {
762+
if constexpr (is_integral_floating) {
718763
if (pp_parser(table, parser_prefix, name, valname, ref, false)) {
764+
pp_entry_set_last_val(entry, ival, ref, true);
719765
return true;
720766
}
721767
} else {
@@ -740,6 +786,7 @@ squeryval (const ParmParse::Table& table,
740786
<< pp_to_string(name,*def) << '\n';
741787
amrex::Abort();
742788
}
789+
743790
return true;
744791
}
745792

@@ -789,6 +836,17 @@ squeryarr (const ParmParse::Table& table,
789836
{
790837
return false;
791838
}
839+
840+
auto const& entry = table.at(name);
841+
842+
#ifdef AMREX_USE_OMP
843+
#pragma omp single nowait
844+
#endif
845+
{
846+
using T_ptr = std::decay_t<T>*;
847+
entry.m_typehint = static_cast<T_ptr>(nullptr);
848+
}
849+
792850
//
793851
// Does it have sufficient number of values and are they all
794852
// the same type?
@@ -800,6 +858,13 @@ squeryarr (const ParmParse::Table& table,
800858

801859
if ( num_val == 0 ) { return true; }
802860

861+
constexpr bool is_integral_floating = (std::is_same_v<T,bool> ||
862+
std::is_same_v<T,int> ||
863+
std::is_same_v<T,long> ||
864+
std::is_same_v<T,long long> ||
865+
std::is_same_v<T,float> ||
866+
std::is_same_v<T,double>);
867+
803868
int stop_ix = start_ix + num_val - 1;
804869
if ( static_cast<int>(ref.size()) <= stop_ix )
805870
{
@@ -822,16 +887,14 @@ squeryarr (const ParmParse::Table& table,
822887
for ( int n = start_ix; n <= stop_ix; n++ )
823888
{
824889
const std::string& valname = (*def)[n];
825-
bool ok = is(valname, ref[n]);
826-
if ( !ok )
827-
{
828-
if constexpr (std::is_same_v<T,int> ||
829-
std::is_same_v<T,long> ||
830-
std::is_same_v<T,long long> ||
831-
std::is_same_v<T,float> ||
832-
std::is_same_v<T,double>)
833-
{
890+
if (is(valname, ref[n])) {
891+
if constexpr (is_integral_floating) {
892+
pp_entry_set_last_val(entry, n, ref[n], false);
893+
}
894+
} else {
895+
if constexpr (is_integral_floating) {
834896
if (pp_parser(table, parser_prefix, name, valname, ref[n], false)) {
897+
pp_entry_set_last_val(entry, n, ref[n], true);
835898
continue;
836899
}
837900
} else {
@@ -1283,7 +1346,7 @@ ParmParse::dumpTable (std::ostream& os, bool prettyPrint)
12831346
auto const& entry = g_table[name];
12841347
if (prettyPrint && entry.m_count > 0) {
12851348
for (auto const& vals : entry.m_vals) {
1286-
os << pp_to_pretty_string(name, vals) << '\n';
1349+
os << pp_to_pretty_string(name, vals, nullptr) << '\n';
12871350
}
12881351
}
12891352
else {
@@ -1317,16 +1380,9 @@ void pretty_print_table (std::ostream& os, PPFlag pp_flag)
13171380

13181381
for (auto const& name : sorted_names) {
13191382
auto const& entry = g_table[name];
1320-
std::vector<std::string> value_string;
1321-
std::unordered_map<std::string,int> count;
1322-
for (auto const& vals : entry.m_vals) {
1323-
value_string.emplace_back(pp_to_pretty_string(name, vals));
1324-
++count[value_string.back()];
1325-
}
1326-
for (auto const& s : value_string) {
1327-
if (--count[s] == 0) {
1328-
os << s << '\n';
1329-
}
1383+
if (! entry.m_vals.empty()) {
1384+
auto const& val = entry.m_vals.back();
1385+
os << pp_to_pretty_string(name, val, &entry) << '\n';
13301386
}
13311387
}
13321388
}
@@ -2229,7 +2285,34 @@ bool squeryWithParser (const ParmParse::Table& table,
22292285
for (auto const& v : vals) {
22302286
combined_string.append(v);
22312287
}
2232-
return pp_parser(table, parser_prefix, name, combined_string, ref, true);
2288+
2289+
constexpr bool is_integral_floating = (std::is_same_v<T,bool> ||
2290+
std::is_same_v<T,int> ||
2291+
std::is_same_v<T,long> ||
2292+
std::is_same_v<T,long long> ||
2293+
std::is_same_v<T,float> ||
2294+
std::is_same_v<T,double>);
2295+
2296+
auto const& entry = table.at(name);
2297+
2298+
if (pp_parser(table, parser_prefix, name, combined_string, ref, true))
2299+
{
2300+
#ifdef AMREX_USE_OMP
2301+
#pragma omp single nowait
2302+
#endif
2303+
{
2304+
using T_ptr = std::decay_t<T>*;
2305+
entry.m_typehint = static_cast<T_ptr>(nullptr);
2306+
}
2307+
2308+
if constexpr (is_integral_floating) {
2309+
pp_entry_set_last_val(entry, 0, ref, true);
2310+
}
2311+
2312+
return true;
2313+
} else {
2314+
return false;
2315+
}
22332316
}
22342317

22352318
template <class T>
@@ -2244,11 +2327,36 @@ bool squeryarrWithParser (const ParmParse::Table& table,
22442327
ParmParse::FIRST, ParmParse::ALL, ParmParse::LAST);
22452328
if (!exist) { return false; }
22462329

2330+
constexpr bool is_integral_floating = (std::is_same_v<T,bool> ||
2331+
std::is_same_v<T,int> ||
2332+
std::is_same_v<T,long> ||
2333+
std::is_same_v<T,long long> ||
2334+
std::is_same_v<T,float> ||
2335+
std::is_same_v<T,double>);
2336+
2337+
auto const& entry = table.at(name);
2338+
22472339
AMREX_ALWAYS_ASSERT(int(vals.size()) == nvals);
22482340
for (int ival = 0; ival < nvals; ++ival) {
22492341
bool r = pp_parser(table, parser_prefix, name, vals[ival], ptr[ival], true);
2250-
if (!r) { return false; }
2342+
if (r) {
2343+
if constexpr (is_integral_floating) {
2344+
pp_entry_set_last_val(entry, ival, ptr[ival], true);
2345+
}
2346+
} else {
2347+
return false;
2348+
}
2349+
}
2350+
2351+
#ifdef AMREX_USE_OMP
2352+
#pragma omp single nowait
2353+
#endif
2354+
{
2355+
auto const& entry = table.at(name);
2356+
using T_ptr = std::decay_t<T>*;
2357+
entry.m_typehint = static_cast<T_ptr>(nullptr);
22512358
}
2359+
22522360
return true;
22532361
}
22542362
}

0 commit comments

Comments
 (0)