From 2e63c4a31f946a8bafa92c49c1a0a116c0eb6dae Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 9 Nov 2026 22:32:31 +0100 Subject: Fixing rocm up --- modules/core/c++/abstract/args.hpp | 354 ++++++++++++++++++++++++++++++++ modules/core/c++/common.hpp | 8 + modules/core/c++/environment.hpp | 2 +- modules/core/c++/fplbm.hpp | 5 +- modules/core/c++/hlbm.hpp | 10 +- modules/core/c++/lbm.hpp | 4 +- modules/core/c++/particle.hpp | 14 ++ modules/core/c++/particle/aabb.hpp | 35 +++- modules/core/c++/particle/cell_list.hpp | 1 - modules/core/c++/particle/common.hpp | 7 + modules/core/c++/particle/contact.hpp | 1 - modules/core/c++/particle/integrate.hpp | 1 - modules/core/c++/particle/mask.hpp | 2 - modules/core/c++/particle/particle.hpp | 10 +- modules/core/c++/particle/porosity.hpp | 1 - modules/core/c++/particle/schema.hpp | 1 - modules/core/c++/psm.hpp | 14 +- modules/core/c++/util.hpp | 2 +- modules/core/tests/args.cpp | 262 +++++++++++++++++++++++ 19 files changed, 690 insertions(+), 44 deletions(-) create mode 100644 modules/core/c++/abstract/args.hpp create mode 100644 modules/core/tests/args.cpp (limited to 'modules/core') diff --git a/modules/core/c++/abstract/args.hpp b/modules/core/c++/abstract/args.hpp new file mode 100644 index 0000000..65be782 --- /dev/null +++ b/modules/core/c++/abstract/args.hpp @@ -0,0 +1,354 @@ +#pragma once + +#include +#include +#include +#include + +#include "data.hpp" +#include "data_math.hpp" +#include "encode.hpp" +#include "error.hpp" +#include "schema.hpp" +#include "unit.hpp" + +namespace kel { +namespace lbm { + +namespace enc { +/** + * Command line arguments as an encoding. + * + * Keeping this schema driven rather than hand parsed is what lets a program's + * argument list carry the same type information as the rest of the data model + * -- so a united quantity stays united and a mixed precision value keeps its + * storage type, instead of every call site reinventing a double. + */ +struct Args { + static constexpr string_literal name = "enc::Args"; +}; +} + +namespace sch { +/** + * `program` is argv[0], `args` the named `--name value` pairs, `positionals` + * the bare words in order. + */ +template +using Args = Struct< + Member, + Member, + Member +>; +} + +template +class data { + static_assert(always_false, + "Not supported. Use the sch::Args alias or check how it's defined."); +}; + +template +class data...>, sch::Tuple>, enc::Args> final { +private: + int argc_; + char** argv_; + +public: + data(int argc, char** argv): + argc_{argc}, + argv_{argv} + {} + + uint64_t size() const { + if(argc_ < 0){ + return 0u; + } + static_assert(sizeof(int) <= sizeof(uint64_t), "int is wider than uint64_t"); + return static_cast(argc_); + } + + std::string_view arg_view(uint64_t i) const { + if(i < size()){ + return std::string_view{argv_[i]}; + } + return ""; + } +}; + +namespace impl { + +/** + * Decodes one argument value into one leaf of the schema. + * + * Specialize this to teach the argument parser a new leaf type; everything + * above it -- name matching, positional ordering -- is shared. + */ +template +struct args_decode { + static_assert(always_false, + "No argument decoding for this schema. Add an args_decode specialization."); +}; + +template +struct args_decode, ToDec> { + using Schema = sch::Primitive; + using Native = typename native_data_type::type; + + static error_or decode(data& to, std::string_view view){ + if(view.empty()){ + return make_error("Can't decode empty data."); + } + + if constexpr ( std::is_floating_point_v ){ + /** + * strtod rather than from_chars: the floating point overload of + * from_chars is not available on every toolchain this builds with. + */ + std::string buf{view}; + char* end = nullptr; + auto val = std::strtod(buf.c_str(), &end); + if(end == buf.c_str() || *end != '\0'){ + return make_error( + std::string{"Expected a number, got '"} + std::string{view} + "'"); + } + to.set(static_cast(val)); + }else{ + Native val{}; + auto first = view.data(); + auto last = view.data() + view.size(); + auto res = std::from_chars(first, last, val); + if(res.ec != std::errc{} || res.ptr != last){ + return make_error( + std::string{"Expected an integer, got '"} + std::string{view} + "'"); + } + to.set(val); + } + + return make_void(); + } +}; + +template +struct args_decode { + static error_or decode(data& to, std::string_view view){ + if(view == "true" || view == "1" || view == "yes" || view == "on"){ + to.set(true); + return make_void(); + } + if(view == "false" || view == "0" || view == "no" || view == "off"){ + to.set(false); + return make_void(); + } + return make_error( + std::string{"Expected a boolean, got '"} + std::string{view} + "'"); + } +}; + +template +struct args_decode { + static error_or decode(data& to, std::string_view view){ + try { + to.set(std::string{view}); + } catch(const std::exception&){ + return make_error(); + } + return make_void(); + } +}; + +/** + * A mixed precision value is written and read as its interface type, so that + * is what the command line parses into; the narrowing to storage happens in + * the data class exactly as it does everywhere else. + */ +template +struct args_decode, ToDec> { + using Schema = sch::MixedPrecision; + + static error_or decode(data& to, std::string_view view){ + data interface_value; + auto eov = args_decode::decode(interface_value, view); + if(eov.is_error()){ + return eov; + } + to.set(interface_value.get()); + return make_void(); + } +}; + +/** + * A united quantity parses as its underlying value and keeps its unit. + * + * The unit is a property of the schema, not of the text, so there is nothing + * to read for it -- but the result is still typed, which is the whole point: + * a length argument cannot be handed to something expecting a time. + */ +template +struct args_decode, ToDec> { + using Schema = sch::Unit; + + static error_or decode(data& to, std::string_view view){ + data base; + auto eov = args_decode::decode(base, view); + if(eov.is_error()){ + return eov; + } + to = data{base}; + return make_void(); + } +}; + +/** + * A Tensor of rank zero is a single value, so it decodes like one. This is + * what makes sch::Scalar usable as an argument. + */ +template +struct args_decode, ToDec> { + using Schema = sch::Tensor; + + static error_or decode(data& to, std::string_view view){ + data inner; + auto eov = args_decode::decode(inner, view); + if(eov.is_error()){ + return eov; + } + to.flat_at(0u) = inner; + return make_void(); + } +}; + +template +struct args_decode...>, sch::Tuple>, ToDec> { + using Schema = sch::Args< + sch::Struct...>, + sch::Tuple + >; + + using SchemaStruct = sch::Struct...>; + using SchemaTuple = sch::Tuple; + + template + static error_or decode_struct_member( + data& to, + std::string_view name, + std::string_view value + ){ + if constexpr ( i < sizeof...(Str) ){ + static constexpr string_literal Literal = parameter_key_pack_type::literal; + using StrT = typename parameter_pack_type::type; + + if(Literal.view() == name){ + return args_decode::decode(to.template get(), value); + } + + return decode_struct_member(to, name, value); + }else{ + (void) to; + (void) value; + /** + * Named rather than generic: an unrecognised flag is almost always + * a typo, and saying which one turns a puzzled re-run into a fix. + */ + return make_error( + std::string{"Unknown argument --"} + std::string{name}); + } + } + + template + static error_or decode_tuple_member( + data& to, + uint64_t ptr_i, + std::string_view value + ){ + if constexpr ( i < sizeof...(Tup) ){ + if( i == ptr_i ){ + using TupT = typename parameter_pack_type::type; + return args_decode::decode(to.template get(), value); + } + return decode_tuple_member(to, ptr_i, value); + }else{ + (void) to; + (void) ptr_i; + return make_error( + std::string{"Too many positional arguments, did not expect '"} + + std::string{value} + "'"); + } + } + + static error_or decode( + const data& from, + data& to + ){ + if(from.size() == 0u){ + return make_error("No arguments at all, not even argv[0]"); + } + + to.template get<"program">().set(std::string{from.arg_view(0u)}); + uint64_t tuple_pos = 0u; + + for(uint64_t i = 1u; i < from.size(); ++i){ + auto view = from.arg_view(i); + + if(view.starts_with("--")){ + view.remove_prefix(2u); + ++i; + + if(i >= from.size()){ + return make_error( + std::string{"Missing value for --"} + std::string{view}); + } + + auto eov = decode_struct_member<0u>( + to.template get<"args">(), view, from.arg_view(i)); + if(eov.is_error()){ + return eov; + } + continue; + } + + auto eov = decode_tuple_member<0u>( + to.template get<"positionals">(), tuple_pos, view); + if(eov.is_error()){ + return eov; + } + ++tuple_pos; + } + + if(tuple_pos != sizeof...(Tup)){ + return make_error( + std::string{"Expected "} + std::to_string(sizeof...(Tup)) + + " positional arguments, got " + std::to_string(tuple_pos)); + } + + return make_void(); + } +}; + +} // namespace impl + +template class codec; + +template +class codec final { +public: + codec() = default; + + template + error_or decode(const data& from, data& to){ + return impl::args_decode::decode(from,to); + } +}; + +/** + * Parses argv straight into a schema instance. + */ +template +error_or parse_args(data& dec_dat, int argc, char** argv){ + data arg_dat{argc, argv}; + codec arg_cod; + return arg_cod.decode(arg_dat, dec_dat); +} + +} // namespace lbm +} // namespace kel diff --git a/modules/core/c++/common.hpp b/modules/core/c++/common.hpp index 52265e2..7fab2a1 100644 --- a/modules/core/c++/common.hpp +++ b/modules/core/c++/common.hpp @@ -1,6 +1,14 @@ #pragma once +/** + * Base include for this layer. + * + * Headers in c++/ take this rather than reaching into abstract/ themselves, so + * the data model arrives from one place and the layering stays visible. + */ #include "abstract/data.hpp" +#include "abstract/data_math.hpp" +#include "abstract/data_ptr.hpp" #include "abstract/math.hpp" namespace kel { diff --git a/modules/core/c++/environment.hpp b/modules/core/c++/environment.hpp index 0e2f6b9..7778d6a 100644 --- a/modules/core/c++/environment.hpp +++ b/modules/core/c++/environment.hpp @@ -62,7 +62,7 @@ kel::lbm::error_or init_lbm_env(const kel::lbm::data& args) * Located outside the project dir because dispatching build jobs with output data in the git directory * also copies simulated data which takes a long time. */ -kel::lbm::error_or output_directory(){ +inline kel::lbm::error_or output_directory(){ namespace fs = std::filesystem; const char* home_dir = std::getenv("HOME"); diff --git a/modules/core/c++/fplbm.hpp b/modules/core/c++/fplbm.hpp index 4f9c81d..f961bc9 100644 --- a/modules/core/c++/fplbm.hpp +++ b/modules/core/c++/fplbm.hpp @@ -183,11 +183,10 @@ public: - auto eo_aabb = particle_aabb::calculate(pg,index,mom_f.meta()); - if(eo_aabb.is_error()){ + kel::lbm::data::AABB> aabb; + if(not particle_aabb::try_calculate(pg,index,mom_f.meta(),aabb)){ return; } - auto& aabb = eo_aabb.get_value(); kel::lbm::data> three; three.at({}).set(3.0f); diff --git a/modules/core/c++/hlbm.hpp b/modules/core/c++/hlbm.hpp index b7bf003..acb2121 100644 --- a/modules/core/c++/hlbm.hpp +++ b/modules/core/c++/hlbm.hpp @@ -151,11 +151,10 @@ public: / static_cast::type>(steps) ); - auto eo_aabb = particle_aabb::calculate(part_spheroid_group,index,mvel_f.meta()); - if(eo_aabb.is_error()){ + kel::lbm::data::AABB> aabb; + if(not particle_aabb::try_calculate(part_spheroid_group,index,mvel_f.meta(),aabb)){ return; } - auto& aabb = eo_aabb.get_value(); auto start = aabb.template get<"a">(); auto stop = aabb.template get<"b">(); @@ -258,11 +257,10 @@ public: / static_cast::type>(steps) ); - auto eo_aabb = particle_aabb::calculate(part_group,index,mvel_f.meta()); - if(eo_aabb.is_error()){ + kel::lbm::data::AABB> aabb; + if(not particle_aabb::try_calculate(part_group,index,mvel_f.meta(),aabb)){ return; } - auto& aabb = eo_aabb.get_value(); auto vel_p = (pirb_pos-pirb_pos_old) / ts; diff --git a/modules/core/c++/lbm.hpp b/modules/core/c++/lbm.hpp index 042bad4..0631c79 100644 --- a/modules/core/c++/lbm.hpp +++ b/modules/core/c++/lbm.hpp @@ -17,8 +17,8 @@ #include "macroscopic.hpp" #include "momentum_gather.hpp" #include "memory.hpp" -#include "particle/contact.hpp" -#include "particle/integrate.hpp" +#include "particle.hpp" +#include "abstract/args.hpp" #include "psm.hpp" #include "component/psm.hpp" #include "stream.hpp" diff --git a/modules/core/c++/particle.hpp b/modules/core/c++/particle.hpp index 30c5005..6546f8d 100644 --- a/modules/core/c++/particle.hpp +++ b/modules/core/c++/particle.hpp @@ -1,7 +1,21 @@ #pragma once #include "component.hpp" + +/** + * Folder aggregator for particle/. Anything that wants the particle layer + * takes this, rather than reaching for individual headers. + */ #include "particle/particle.hpp" +#include "particle/aabb.hpp" +#include "particle/porosity.hpp" +#include "particle/mask.hpp" +#include "particle/particle_opa.hpp" +#include "particle/integrate.hpp" +#include "particle/contact.hpp" +#include "particle/cell_list.hpp" +#include "particle/couple.hpp" +#include "particle/voxel_narrow.hpp" namespace kel { namespace lbm { diff --git a/modules/core/c++/particle/aabb.hpp b/modules/core/c++/particle/aabb.hpp index caf3210..e207ad8 100644 --- a/modules/core/c++/particle/aabb.hpp +++ b/modules/core/c++/particle/aabb.hpp @@ -22,14 +22,30 @@ public: sch::Member,"b"> >; public: + /** + * Device-side entry point: reports success as a bool rather than an + * error_or. + * + * This matters more than it looks. error_or carries an `error`, and + * building one reaches get_template_error_id -> a function-local static + * (__cxa_guard_acquire) -> the error registry -> heap<> (operator new) -> + * std::vector (__throw_length_error) -> std::string (strlen). Returning + * error_or from a function called inside a kernel therefore pulls the + * whole host allocator and exception machinery into device code, which + * AdaptiveCpp's LoopSplitterInlining pass then has to walk -- it reports + * each of those symbols as undefined and chokes on the call graph. + * + * Nothing is lost: the only failure here is an out of range particle + * index, which a caller can act on just as well from a bool. + */ template - static constexpr kel::lbm::error_or> calculate(const kel::lbm::data& p_grp, const kel::lbm::data>& index, const kel::lbm::data>& meta){ + static constexpr bool try_calculate(const kel::lbm::data& p_grp, const kel::lbm::data>& index, const kel::lbm::data>& meta, kel::lbm::data& out){ static_assert(PC > 0u, "Can't calculate from no particles"); if(not (index.at({{0u}}).get() < PC) ){ - return kel::lbm::make_error("Too large i in particle_aabb"); + return false; } - kel::lbm::data aabb; + kel::lbm::data& aabb = out; auto& parts = p_grp.template get<"particles">(); auto& pi = parts.at(index); @@ -64,6 +80,19 @@ public: b.at({i}) = (b_i < meta.at({i})) ? b_i : meta.at({i}); } + return true; + } + + /** + * Host-side convenience wrapper. Keeps the error_or interface for callers + * that are not inside a kernel. + */ + template + static kel::lbm::error_or> calculate(const kel::lbm::data& p_grp, const kel::lbm::data>& index, const kel::lbm::data>& meta){ + kel::lbm::data aabb; + if(not try_calculate(p_grp,index,meta,aabb)){ + return kel::lbm::make_error("Too large i in particle_aabb"); + } return aabb; } }; diff --git a/modules/core/c++/particle/cell_list.hpp b/modules/core/c++/particle/cell_list.hpp index d1d1537..a43e1df 100644 --- a/modules/core/c++/particle/cell_list.hpp +++ b/modules/core/c++/particle/cell_list.hpp @@ -1,7 +1,6 @@ #pragma once #include "../component.hpp" -#include "../iterator.hpp" #include "common.hpp" #include "schema.hpp" #include "particle.hpp" diff --git a/modules/core/c++/particle/common.hpp b/modules/core/c++/particle/common.hpp index 9e673c2..019c29f 100644 --- a/modules/core/c++/particle/common.hpp +++ b/modules/core/c++/particle/common.hpp @@ -1,3 +1,10 @@ #pragma once +/** + * Base include for particle/. + * + * Chains to the layer above, and adds what every header in this folder needs, + * so the headers themselves only have to name their siblings. + */ #include "../common.hpp" +#include "../iterator.hpp" diff --git a/modules/core/c++/particle/contact.hpp b/modules/core/c++/particle/contact.hpp index 2e7973a..857e116 100644 --- a/modules/core/c++/particle/contact.hpp +++ b/modules/core/c++/particle/contact.hpp @@ -1,7 +1,6 @@ #pragma once #include "../component.hpp" -#include "../abstract/math.hpp" #include "common.hpp" #include "schema.hpp" #include "particle.hpp" diff --git a/modules/core/c++/particle/integrate.hpp b/modules/core/c++/particle/integrate.hpp index f83465a..a84cd37 100644 --- a/modules/core/c++/particle/integrate.hpp +++ b/modules/core/c++/particle/integrate.hpp @@ -35,7 +35,6 @@ inline uint64_t particle_sub_steps(kel::lbm::data sub_steps){ template constexpr bool particle_angular_supported = (D == 2u); - /** * Turns the accumulated force/torque into acceleration and advances the body. * diff --git a/modules/core/c++/particle/mask.hpp b/modules/core/c++/particle/mask.hpp index 246c31f..44dd74b 100644 --- a/modules/core/c++/particle/mask.hpp +++ b/modules/core/c++/particle/mask.hpp @@ -2,8 +2,6 @@ #include -#include "../abstract/math.hpp" -#include "../iterator.hpp" #include "common.hpp" #include "schema.hpp" diff --git a/modules/core/c++/particle/particle.hpp b/modules/core/c++/particle/particle.hpp index d9b5444..00e401a 100644 --- a/modules/core/c++/particle/particle.hpp +++ b/modules/core/c++/particle/particle.hpp @@ -1,15 +1,10 @@ #pragma once -#include "../abstract/math.hpp" -#include "../abstract/data_math.hpp" -#include "../abstract/data.hpp" - -#include "../iterator.hpp" +#include "common.hpp" #include "schema.hpp" #include "aabb.hpp" #include "mask.hpp" -#include "../abstract/data_ptr.hpp" #include "particle_opa.hpp" namespace kel { @@ -232,7 +227,6 @@ constexpr auto handle_collision = []( } }; - /** * Bounding-sphere overlap test. * @@ -281,7 +275,5 @@ constexpr auto particle_dem_lambda = [](kel::lbm::data class particle_porosity> final { public: diff --git a/modules/core/c++/particle/schema.hpp b/modules/core/c++/particle/schema.hpp index fe7a33c..39e1a36 100644 --- a/modules/core/c++/particle/schema.hpp +++ b/modules/core/c++/particle/schema.hpp @@ -75,7 +75,6 @@ using ParticleRigidBody = Struct< Member, "density"> >; - /** * CollisionType is defaulted so the plain `Particle` spelling used by the * integrator and the tests keeps working. diff --git a/modules/core/c++/psm.hpp b/modules/core/c++/psm.hpp index 19c3524..b540f38 100644 --- a/modules/core/c++/psm.hpp +++ b/modules/core/c++/psm.hpp @@ -186,21 +186,11 @@ void apply( * -------------------------------------------------------- */ - auto eo_aabb = - particle_aabb< - typename ParticleSchema::ValueType - >::calculate( - pg, - index, - vel_f.meta() - ); - - if(eo_aabb.is_error()){ + kel::lbm::data::AABB> aabb; + if(not particle_aabb::try_calculate(pg,index,vel_f.meta(),aabb)){ return; } - auto& aabb = eo_aabb.get_value(); - kel::lbm::data> start; kel::lbm::data> stop; diff --git a/modules/core/c++/util.hpp b/modules/core/c++/util.hpp index 8bafc16..b3c9217 100644 --- a/modules/core/c++/util.hpp +++ b/modules/core/c++/util.hpp @@ -48,7 +48,7 @@ public: }; */ -void print_progress_bar(const uint32_t progress, const uint32_t progress_target){ +inline void print_progress_bar(const uint32_t progress, const uint32_t progress_target){ std::cout<<"\r"; // <<"Progress: " // <<((100 * progress) / progress_target) diff --git a/modules/core/tests/args.cpp b/modules/core/tests/args.cpp new file mode 100644 index 0000000..e40fc87 --- /dev/null +++ b/modules/core/tests/args.cpp @@ -0,0 +1,262 @@ +#include + +#include +#include +#include + +#include "../c++/abstract/args.hpp" +#include "../c++/lbm_unit.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +/** + * argv is char**, so the fixture has to own the strings and hand out pointers. + */ +struct Argv { + std::vector store; + std::vector ptrs; + + explicit Argv(std::vector words): store{std::move(words)} { + ptrs.reserve(store.size()); + for(auto& s : store){ + ptrs.push_back(s.data()); + } + } + + int argc() const { return static_cast(ptrs.size()); } + char** argv() { return ptrs.data(); } +}; + +SAW_TEST("Args / named and positional round trip"){ + using namespace kel; + + using ArgStruct = sch::Struct< + sch::Member, + sch::Member + >; + using Sch = sch::Args>; + + Argv a{{"prog","--dx","0.25","2","--dt","0.5","9"}}; + + kel::lbm::data parsed; + auto eov = lbm::parse_args(parsed, a.argc(), a.argv()); + SAW_EXPECT(eov.is_value(), "parse should succeed"); + + SAW_EXPECT(parsed.template get<"program">().stl_view() == "prog", "argv[0] is the program"); + + auto& args = parsed.template get<"args">(); + SAW_EXPECT(args.template get<"dx">().get() == 0.25, "dx"); + SAW_EXPECT(args.template get<"dt">().get() == 0.5, "dt"); + + // Positionals keep their order even when interleaved with named arguments. + auto& pos = parsed.template get<"positionals">(); + SAW_EXPECT(pos.template get<0u>().get() == 2u, "first positional"); + SAW_EXPECT(pos.template get<1u>().get() == 9u, "second positional"); +} + +SAW_TEST("Args / reports the unknown flag by name"){ + using namespace kel; + + using Sch = sch::Args< + sch::Struct>, + sch::Tuple<> + >; + + Argv a{{"prog","--nope","1"}}; + + kel::lbm::data parsed; + auto eov = lbm::parse_args(parsed, a.argc(), a.argv()); + + SAW_EXPECT(eov.is_error(), "an unknown flag must be an error"); + // Naming it is the difference between a puzzled re-run and a fix. + SAW_EXPECT(eov.get_error().get_message().find("nope") != std::string_view::npos, + std::string{"error should name the flag: "} + + std::string{eov.get_error().get_message()}); +} + +SAW_TEST("Args / rejects malformed values and arity mistakes"){ + using namespace kel; + + using Sch = sch::Args< + sch::Struct>, + sch::Tuple + >; + + { + Argv a{{"prog","--dx","abc","1"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_error(), + "a non numeric value must fail"); + } + { + Argv a{{"prog","--dx"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_error(), + "a flag with no value must fail"); + } + { + Argv a{{"prog","--dx","1.0"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_error(), + "a missing positional must fail"); + } + { + Argv a{{"prog","--dx","1.0","1","2"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_error(), + "a surplus positional must fail"); + } + { + // Trailing text after a number is a mistake, not a prefix to accept. + Argv a{{"prog","--dx","1.0x","1"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_error(), + "trailing junk must fail rather than parse the prefix"); + } +} + +SAW_TEST("Args / integers reject fractional input"){ + using namespace kel; + + using Sch = sch::Args< + sch::Struct>, + sch::Tuple<> + >; + + Argv good{{"prog","--count","42"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, good.argc(), good.argv()).is_value(), "42 parses"); + SAW_EXPECT(p.template get<"args">().template get<"count">().get() == 42u, "count"); + + // 1.5 is not an integer; truncating silently would lose data. + Argv bad{{"prog","--count","1.5"}}; + kel::lbm::data q; + SAW_EXPECT(lbm::parse_args(q, bad.argc(), bad.argv()).is_error(), + "a fractional value is not an integer"); +} + +SAW_TEST("Args / strings and bools"){ + using namespace kel; + + using Sch = sch::Args< + sch::Struct< + sch::Member, + sch::Member + >, + sch::Tuple<> + >; + + Argv a{{"prog","--coupling","fplbm","--verbose","true"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_value(), "parse should succeed"); + + auto& args = p.template get<"args">(); + SAW_EXPECT(args.template get<"coupling">().stl_view() == "fplbm", "string member"); + SAW_EXPECT(args.template get<"verbose">().get() == true, "bool member"); + + for(const char* word : {"false","0","no","off"}){ + Argv b{{"prog","--coupling","x","--verbose",word}}; + kel::lbm::data q; + SAW_EXPECT(lbm::parse_args(q, b.argc(), b.argv()).is_value(), + std::string{"'"} + word + "' should parse"); + SAW_EXPECT(q.template get<"args">().template get<"verbose">().get() == false, + std::string{"'"} + word + "' should be false"); + } + + Argv bad{{"prog","--coupling","x","--verbose","maybe"}}; + kel::lbm::data r; + SAW_EXPECT(lbm::parse_args(r, bad.argc(), bad.argv()).is_error(), + "a non boolean must fail rather than default"); +} + +SAW_TEST("Args / mixed precision keeps its interface type"){ + using namespace kel; + + /** + * Stored narrow, read wide. The command line value goes in at interface + * precision, which is the whole reason for parsing through the schema + * instead of into a bare double. + */ + using Mixed = sch::MixedPrecision; + using Sch = sch::Args< + sch::Struct>, + sch::Tuple<> + >; + + Argv a{{"prog","--visc","0.5"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_value(), "parse should succeed"); + + auto& v = p.template get<"args">().template get<"visc">(); + static_assert(std::is_same_v, + "a mixed precision read must come back at interface precision"); + SAW_EXPECT(v.get() == 0.5, "value exactly representable in float survives"); +} + +SAW_TEST("Args / united quantities stay united"){ + using namespace kel; + + /** + * The unit lives in the schema, not the text, so nothing extra is read -- + * but the parsed result is typed, so a length cannot be passed where a + * time is wanted. + */ + using Sch = sch::Args< + sch::Struct< + sch::Member, "length">, + sch::Member, "duration"> + >, + sch::Tuple<> + >; + + Argv a{{"prog","--length","2.5","--duration","0.1"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_value(), "parse should succeed"); + + auto& args = p.template get<"args">(); + SAW_EXPECT(args.template get<"length">().handle().get() == 2.5, "length value"); + SAW_EXPECT(args.template get<"duration">().handle().get() == 0.1, "duration value"); + + // The two are different types, so the unit survived parsing. + static_assert(!std::is_same_v< + std::decay_t())>, + std::decay_t())>>, + "a length and a time must not share a type after parsing"); + + // And the algebra still works on what came off the command line. + auto velocity = args.template get<"length">() / args.template get<"duration">(); + SAW_EXPECT(std::abs(velocity.handle().get() - 25.0) < 1e-9, + std::string{"2.5 m / 0.1 s should be 25: "} + + std::to_string(velocity.handle().get())); +} + +SAW_TEST("Args / scalars parse as single values"){ + using namespace kel; + + // Scalar is a rank zero tensor; it should take one number, not a list. + using Sch = sch::Args< + sch::Struct, "radius">>, + sch::Tuple<> + >; + + Argv a{{"prog","--radius","3.75"}}; + kel::lbm::data p; + SAW_EXPECT(lbm::parse_args(p, a.argc(), a.argv()).is_value(), "parse should succeed"); + SAW_EXPECT(p.template get<"args">().template get<"radius">().at({}).get() == 3.75, + "scalar value"); +} + +SAW_TEST("Args / empty argv is an error not a default"){ + using namespace kel; + + using Sch = sch::Args, sch::Tuple<>>; + + kel::lbm::data p; + auto eov = lbm::parse_args(p, 0, nullptr); + SAW_EXPECT(eov.is_error(), "no argv at all must be reported"); +} +} -- cgit v1.3.1