Skip to content

Commit ec1881d

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 ec1881d

File tree

2 files changed

+143
-34
lines changed

2 files changed

+143
-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: 140 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 {
@@ -789,6 +835,17 @@ squeryarr (const ParmParse::Table& table,
789835
{
790836
return false;
791837
}
838+
839+
auto const& entry = table.at(name);
840+
841+
#ifdef AMREX_USE_OMP
842+
#pragma omp single nowait
843+
#endif
844+
{
845+
using T_ptr = std::decay_t<T>*;
846+
entry.m_typehint = static_cast<T_ptr>(nullptr);
847+
}
848+
792849
//
793850
// Does it have sufficient number of values and are they all
794851
// the same type?
@@ -800,6 +857,13 @@ squeryarr (const ParmParse::Table& table,
800857

801858
if ( num_val == 0 ) { return true; }
802859

860+
constexpr bool is_integral_floating = (std::is_same_v<T,bool> ||
861+
std::is_same_v<T,int> ||
862+
std::is_same_v<T,long> ||
863+
std::is_same_v<T,long long> ||
864+
std::is_same_v<T,float> ||
865+
std::is_same_v<T,double>);
866+
803867
int stop_ix = start_ix + num_val - 1;
804868
if ( static_cast<int>(ref.size()) <= stop_ix )
805869
{
@@ -822,16 +886,14 @@ squeryarr (const ParmParse::Table& table,
822886
for ( int n = start_ix; n <= stop_ix; n++ )
823887
{
824888
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-
{
889+
if (is(valname, ref[n])) {
890+
if constexpr (is_integral_floating) {
891+
pp_entry_set_last_val(entry, n, ref[n], false);
892+
}
893+
} else {
894+
if constexpr (is_integral_floating) {
834895
if (pp_parser(table, parser_prefix, name, valname, ref[n], false)) {
896+
pp_entry_set_last_val(entry, n, ref[n], true);
835897
continue;
836898
}
837899
} else {
@@ -1283,7 +1345,7 @@ ParmParse::dumpTable (std::ostream& os, bool prettyPrint)
12831345
auto const& entry = g_table[name];
12841346
if (prettyPrint && entry.m_count > 0) {
12851347
for (auto const& vals : entry.m_vals) {
1286-
os << pp_to_pretty_string(name, vals) << '\n';
1348+
os << pp_to_pretty_string(name, vals, nullptr) << '\n';
12871349
}
12881350
}
12891351
else {
@@ -1317,16 +1379,9 @@ void pretty_print_table (std::ostream& os, PPFlag pp_flag)
13171379

13181380
for (auto const& name : sorted_names) {
13191381
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-
}
1382+
if (! entry.m_vals.empty()) {
1383+
auto const& val = entry.m_vals.back();
1384+
os << pp_to_pretty_string(name, val, &entry) << '\n';
13301385
}
13311386
}
13321387
}
@@ -2229,7 +2284,34 @@ bool squeryWithParser (const ParmParse::Table& table,
22292284
for (auto const& v : vals) {
22302285
combined_string.append(v);
22312286
}
2232-
return pp_parser(table, parser_prefix, name, combined_string, ref, true);
2287+
2288+
constexpr bool is_integral_floating = (std::is_same_v<T,bool> ||
2289+
std::is_same_v<T,int> ||
2290+
std::is_same_v<T,long> ||
2291+
std::is_same_v<T,long long> ||
2292+
std::is_same_v<T,float> ||
2293+
std::is_same_v<T,double>);
2294+
2295+
auto const& entry = table.at(name);
2296+
2297+
if (pp_parser(table, parser_prefix, name, combined_string, ref, true))
2298+
{
2299+
#ifdef AMREX_USE_OMP
2300+
#pragma omp single nowait
2301+
#endif
2302+
{
2303+
using T_ptr = std::decay_t<T>*;
2304+
entry.m_typehint = static_cast<T_ptr>(nullptr);
2305+
}
2306+
2307+
if constexpr (is_integral_floating) {
2308+
pp_entry_set_last_val(entry, 0, ref, true);
2309+
}
2310+
2311+
return true;
2312+
} else {
2313+
return false;
2314+
}
22332315
}
22342316

22352317
template <class T>
@@ -2244,11 +2326,36 @@ bool squeryarrWithParser (const ParmParse::Table& table,
22442326
ParmParse::FIRST, ParmParse::ALL, ParmParse::LAST);
22452327
if (!exist) { return false; }
22462328

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

0 commit comments

Comments
 (0)