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 +++++++++++++++++++++++ modules/sycl/.nix/derivation.nix | 9 +- modules/sycl/tests/basic_ops.cpp | 349 +++++++++++++++++++++++++++++++ modules/sycl/tests/skip.hpp | 89 ++++++++ 22 files changed, 1136 insertions(+), 45 deletions(-) create mode 100644 modules/core/c++/abstract/args.hpp create mode 100644 modules/core/tests/args.cpp create mode 100644 modules/sycl/tests/basic_ops.cpp create mode 100644 modules/sycl/tests/skip.hpp (limited to 'modules') 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"); +} +} diff --git a/modules/sycl/.nix/derivation.nix b/modules/sycl/.nix/derivation.nix index 4422739..bfcb541 100644 --- a/modules/sycl/.nix/derivation.nix +++ b/modules/sycl/.nix/derivation.nix @@ -7,10 +7,12 @@ , forstio , kel , adaptive-cpp +# Names the build so the ROCm and non-ROCm variants land in distinct outputs. +, variant ? "" }: stdenv.mkDerivation { - pname = "${pname}-sycl"; + pname = "${pname}-sycl${variant}"; inherit version; src = ./..; @@ -33,6 +35,11 @@ stdenv.mkDerivation { doCheck = true; checkPhase = '' + # AdaptiveCpp caches JIT output under $HOME, which is /homeless-shelter + # in a nix build. Point it somewhere writable or the tests die before + # running a single case. + export HOME="$TMPDIR" + scons test ./bin/tests ''; diff --git a/modules/sycl/tests/basic_ops.cpp b/modules/sycl/tests/basic_ops.cpp new file mode 100644 index 0000000..4fdd4f6 --- /dev/null +++ b/modules/sycl/tests/basic_ops.cpp @@ -0,0 +1,349 @@ +#include + +#include +#include + +#include "../c++/lbm.hpp" +#include "skip.hpp" + +/** + * Minimal confirmation that the device layer actually works, rather than + * merely compiles. + * + * Everything else in the suite is host side, so a device allocation that never + * happened, a copy that moved nothing, or a kernel that silently did not run + * would all look exactly like success. Each test here therefore round trips a + * value: write it somewhere, read it back from the other side, compare. + */ + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +using kel::lbm::data; +using Dev = kel::lbm::encode::Sycl; + +SAW_TEST("Sycl / device allocation and FixedArray round trip"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + acpp::sycl::queue q; + lbm::device dev; + + using Sch = sch::FixedArray; + + data host; + for(uint64_t i = 0u; i < 4u; ++i){ + host.at({i}).set(i * 10u + 1u); + } + + data device_data{dev.get_handle()}; + + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + + // Clear the host copy so a no-op read back cannot pass by accident. + data back; + for(uint64_t i = 0u; i < 4u; ++i){ + back.at({i}).set(0u); + } + + auto eov2 = dev.copy_to_host(device_data, back); + SAW_EXPECT(eov2.is_value(), "copy to host failed"); + dev.get_handle().wait(); + + for(uint64_t i = 0u; i < 4u; ++i){ + SAW_EXPECT(back.at({i}).get() == i * 10u + 1u, + std::string{"round trip lost element "} + std::to_string(i) + + ": " + std::to_string(back.at({i}).get())); + } +} + +SAW_TEST("Sycl / a kernel actually runs and writes"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + /** + * The one that catches a kernel which never launched: the host never + * writes these values, so they can only come from the device. + */ + lbm::device dev; + auto& q = dev.get_handle(); + + using Sch = sch::FixedArray; + + data host; + for(uint64_t i = 0u; i < 8u; ++i){ + host.at({i}).set(0u); + } + + data device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data, Dev> view{device_data}; + + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<1u>{8u}, [=](acpp::sycl::id<1u> idx){ + data> index; + index.at({{0u}}).set(idx[0]); + view.at(index).set(idx[0] * 7u + 3u); + }); + }).wait(); + + auto eov2 = dev.copy_to_host(device_data, host); + SAW_EXPECT(eov2.is_value(), "copy to host failed"); + q.wait(); + + for(uint64_t i = 0u; i < 8u; ++i){ + SAW_EXPECT(host.at({i}).get() == i * 7u + 3u, + std::string{"kernel did not write element "} + std::to_string(i) + + ": got " + std::to_string(host.at({i}).get())); + } +} + +SAW_TEST("Sycl / dynamic Array round trips with its extent"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + // Array carries a runtime extent, so the device side has to pick it up + // rather than assume one. + lbm::device dev; + auto& q = dev.get_handle(); + + using Sch = sch::Array; + + data host{data>{{6u}}}; + SAW_EXPECT(host.size().get() == 6u, "host array should be sized from its meta"); + for(uint64_t i = 0u; i < 6u; ++i){ + host.at({i}).set(static_cast(i) * 0.5); + } + + data device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data back{data>{{6u}}}; + for(uint64_t i = 0u; i < 6u; ++i){ + back.at({i}).set(-1.0); + } + + auto eov2 = dev.copy_to_host(device_data, back); + SAW_EXPECT(eov2.is_value(), "copy to host failed"); + q.wait(); + + for(uint64_t i = 0u; i < 6u; ++i){ + SAW_EXPECT(back.at({i}).get() == static_cast(i) * 0.5, + std::string{"array round trip lost element "} + std::to_string(i)); + } +} + +SAW_TEST("Sycl / Struct members round trip independently"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + /** + * A struct copy walks member by member. If it reused one member's pointer + * or skipped one, the others would still look right -- so check each. + */ + lbm::device dev; + auto& q = dev.get_handle(); + + using Sch = sch::Struct< + sch::Member, "counts">, + sch::Member, "weights"> + >; + + data host; + for(uint64_t i = 0u; i < 3u; ++i){ + host.template get<"counts">().at({i}).set(i + 100u); + host.template get<"weights">().at({i}).set(static_cast(i) + 0.25); + } + + data device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data back; + for(uint64_t i = 0u; i < 3u; ++i){ + back.template get<"counts">().at({i}).set(0u); + back.template get<"weights">().at({i}).set(0.0); + } + + auto eov2 = dev.copy_to_host(device_data, back); + SAW_EXPECT(eov2.is_value(), "copy to host failed"); + q.wait(); + + for(uint64_t i = 0u; i < 3u; ++i){ + SAW_EXPECT(back.template get<"counts">().at({i}).get() == i + 100u, + std::string{"counts lost element "} + std::to_string(i)); + SAW_EXPECT(back.template get<"weights">().at({i}).get() + == static_cast(i) + 0.25, + std::string{"weights lost element "} + std::to_string(i)); + } +} + +SAW_TEST("Sycl / Chunk round trips and is addressable in a kernel"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + // Chunk is the type the whole fluid solver runs on, so it is the one that + // matters most. + lbm::device dev; + auto& q = dev.get_handle(); + + constexpr uint64_t nx = 4u; + constexpr uint64_t ny = 3u; + using Sch = sch::Chunk; + + data host; + lbm::iterator<2u>::apply([&](const auto& index){ + host.at(index).set(0u); + },{},host.get_dims()); + + data device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data, Dev> view{device_data}; + + // Write a position dependent value so a transposed or flattened index + // shows up as a mismatch rather than a plausible constant. + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<2u>{nx,ny}, [=](acpp::sycl::id<2u> idx){ + data> index; + index.at({0u}).set(idx[0]); + index.at({1u}).set(idx[1]); + view.at(index).set(idx[0] * 1000u + idx[1]); + }); + }).wait(); + + auto eov2 = dev.copy_to_host(device_data, host); + SAW_EXPECT(eov2.is_value(), "copy to host failed"); + q.wait(); + + uint64_t checked = 0u; + lbm::iterator<2u>::apply([&](const auto& index){ + auto x = index.at({0u}).get(); + auto y = index.at({1u}).get(); + SAW_EXPECT(host.at(index).get() == x * 1000u + y, + std::string{"chunk cell ("} + std::to_string(x) + "," + std::to_string(y) + + ") = " + std::to_string(host.at(index).get())); + ++checked; + },{},host.get_dims()); + + SAW_EXPECT(checked == nx * ny, "expected to visit every cell"); +} + +SAW_TEST("Sycl / make_view reaches struct members on device"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + /** + * make_view recurses into a struct, building a borrowed view per member. + * Confirm a kernel reached through it writes the member it was aimed at. + */ + lbm::device dev; + auto& q = dev.get_handle(); + + using Sch = sch::Struct< + sch::Member, "a">, + sch::Member, "b"> + >; + + data host; + for(uint64_t i = 0u; i < 4u; ++i){ + host.template get<"a">().at({i}).set(0u); + host.template get<"b">().at({i}).set(9u); + } + + data device_data{q}; + SAW_EXPECT(dev.copy_to_device(host, device_data).is_value(), "copy to device failed"); + q.wait(); + + auto view = lbm::make_view(device_data); + + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<1u>{4u}, [=](acpp::sycl::id<1u> idx){ + data> index; + index.at({{0u}}).set(idx[0]); + view.template get<"a">().at(index).set(idx[0] + 50u); + }); + }).wait(); + + SAW_EXPECT(dev.copy_to_host(device_data, host).is_value(), "copy to host failed"); + q.wait(); + + for(uint64_t i = 0u; i < 4u; ++i){ + SAW_EXPECT(host.template get<"a">().at({i}).get() == i + 50u, + std::string{"member a not written at "} + std::to_string(i)); + // The untouched member must be exactly as it was. + SAW_EXPECT(host.template get<"b">().at({i}).get() == 9u, + std::string{"member b was clobbered at "} + std::to_string(i)); + } +} + +SAW_TEST("Sycl / arithmetic on data inside a kernel"){ + using namespace kel; + + KEL_REQUIRE_SYCL(); + + /** + * The data class carries operators; confirm they behave on device rather + * than only in host tests. + */ + lbm::device dev; + auto& q = dev.get_handle(); + + using Sch = sch::FixedArray; + + data host; + for(uint64_t i = 0u; i < 5u; ++i){ + host.at({i}).set(static_cast(i)); + } + + data device_data{q}; + SAW_EXPECT(dev.copy_to_device(host, device_data).is_value(), "copy to device failed"); + q.wait(); + + data, Dev> view{device_data}; + + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<1u>{5u}, [=](acpp::sycl::id<1u> idx){ + data> index; + index.at({{0u}}).set(idx[0]); + + auto& v = view.at(index); + data two; + two.set(2.0); + data one; + one.set(1.0); + + v = v * two + one; + }); + }).wait(); + + SAW_EXPECT(dev.copy_to_host(device_data, host).is_value(), "copy to host failed"); + q.wait(); + + for(uint64_t i = 0u; i < 5u; ++i){ + auto expect = static_cast(i) * 2.0 + 1.0; + SAW_EXPECT(host.at({i}).get() == expect, + std::string{"arithmetic wrong at "} + std::to_string(i) + + ": " + std::to_string(host.at({i}).get()) + + " expected " + std::to_string(expect)); + } +} +} diff --git a/modules/sycl/tests/skip.hpp b/modules/sycl/tests/skip.hpp new file mode 100644 index 0000000..c653591 --- /dev/null +++ b/modules/sycl/tests/skip.hpp @@ -0,0 +1,89 @@ +#pragma once + +#include +#include +#include + +/** + * Skip support for device tests. + * + * forstio's suite only has pass and fail: SAW_EXPECT throws and the case is + * reported as a failure. That is the wrong verdict for a device test on a + * machine with no such device -- "this backend is absent" is not the same + * finding as "this backend is broken", and conflating them means a red suite + * that everyone learns to ignore. + * + * These let a case attempt the work and bow out cleanly instead. A skip is + * printed so it stays visible rather than looking like a silent pass. + */ + +#define KEL_SKIP(reason) \ + do { \ + std::cout << "[ SKIP ] " << __FILE__ << ":" << __LINE__ << ": " \ + << (reason) << std::endl; \ + return; \ + } while(false) + +#define KEL_SKIP_IF(cond, reason) \ + do { \ + if((cond)){ \ + KEL_SKIP(reason); \ + } \ + } while(false) + +/** + * Runs `expr`, skipping the case if it throws. + * + * Device construction is the usual culprit: a missing runtime or an + * unavailable backend surfaces as an exception out of the queue constructor, + * long before any of the behaviour under test could be wrong. + */ +#define KEL_SKIP_ON_THROW(expr, reason) \ + do { \ + try { \ + (void)(expr); \ + } catch(const std::exception& e){ \ + std::cout << "[ SKIP ] " << __FILE__ << ":" << __LINE__ << ": " \ + << (reason) << " (" << e.what() << ")" << std::endl; \ + return; \ + } catch(...){ \ + std::cout << "[ SKIP ] " << __FILE__ << ":" << __LINE__ << ": " \ + << (reason) << std::endl; \ + return; \ + } \ + } while(false) + +namespace kel { +namespace lbm { +namespace test { + +/** + * True when a default queue can be constructed, i.e. there is some backend to + * run on at all. Cached, because probing is not free and the answer cannot + * change within a run. + */ +inline bool sycl_available(){ + static const bool ok = [](){ + try { + acpp::sycl::queue q; + // Touching the device is what actually proves a backend resolved; + // constructing the queue alone can succeed lazily. + (void) q.get_device(); + return true; + } catch(...){ + return false; + } + }(); + return ok; +} + +} +} +} + +/** + * Guard for a whole device test case. + */ +#define KEL_REQUIRE_SYCL() \ + KEL_SKIP_IF(!::kel::lbm::test::sycl_available(), \ + "no usable SYCL device on this machine") -- cgit v1.3.1