diff options
25 files changed, 1195 insertions, 71 deletions
diff --git a/.nix/adaptive-cpp.nix b/.nix/adaptive-cpp.nix index b8a7688..2e631e1 100644 --- a/.nix/adaptive-cpp.nix +++ b/.nix/adaptive-cpp.nix @@ -20,7 +20,7 @@ let version = "25.02.0"; -in stdenv.mkDerivation { +in stdenv.mkDerivation ({ pname = "adaptive-cpp-kel-custom"; inherit version; @@ -54,8 +54,26 @@ in stdenv.mkDerivation { cmakeFlags = [ "-DCLANG_INCLUDE_PATH=${llvmPackages.libclang.dev}/include" + ] ++ lib.optionals config.rocm [ + "-DWITH_ROCM_BACKEND=ON" + + # Keep OpenCL and Level Zero off. src/CMakeLists.txt turns WITH_LLVM_TO_SPIRV + # on for either of them, and that target FetchContent-clones + # SPIRV-LLVM-Translator at configure time, which a nix build cannot do -- + # it has neither git nor network. ROCm is the backend we want here and it + # does not go through SPIR-V. + # + # This only bites when rocm = true: rocmPackages.clr brings OpenCL in, so + # the backend autodetects on, where in the base build it stays off. + "-DWITH_OPENCL_BACKEND=OFF" + "-DWITH_LEVEL_ZERO_BACKEND=OFF" ]; + # nixpkgs 26.05 turns the zerocallusedregs hardening flag on by default, and + # clang rejects -fzero-call-used-regs for the amdgcn-amd-amdhsa target, so + # every device bitcode object under src/libkernel/sscp/amdgpu fails to build. + # Not an AdaptiveCpp problem -- the same source built fine on 25.11. + postInstall = '' if [ -d "$out/include/AdaptiveCpp" ]; then mv $out/include/AdaptiveCpp .hidden-fake @@ -76,4 +94,13 @@ in stdenv.mkDerivation { postPatch = '' patchShebangs . ''; -} +} // lib.optionalAttrs config.rocm { + # nixpkgs 26.05 turns the zerocallusedregs hardening flag on by default, and + # clang rejects -fzero-call-used-regs for the amdgcn-amd-amdhsa target, so + # every device bitcode object under src/libkernel/sscp/amdgpu fails to build. + # Not an AdaptiveCpp problem -- the same source built fine on 25.11. + # + # Set via optionalAttrs rather than lib.optionals so the attribute is absent + # (not just empty) without rocm, which keeps the base derivation hash stable. + hardeningDisable = [ "zerocallusedregs" ]; +}) diff --git a/default.nix b/default.nix index cc36692..e577796 100644 --- a/default.nix +++ b/default.nix @@ -36,14 +36,22 @@ let ''; }; - adaptive-cpp = pkgs.callPackage .nix/adaptive-cpp.nix { + # One builder, two instances, so the ROCm and non-ROCm toolchains differ in + # exactly one input and nothing else. + mkAdaptiveCpp = enableRocm: pkgs.callPackage .nix/adaptive-cpp.nix { inherit stdenv llvmPackages lld; config = { - inherit rocm; + rocm = enableRocm; }; }; + adaptive-cpp-base = mkAdaptiveCpp false; + adaptive-cpp-rocm = mkAdaptiveCpp true; + + # What the examples and the default build use. Flip with --arg rocm true. + adaptive-cpp = if rocm then adaptive-cpp-rocm else adaptive-cpp-base; + forstio = let forstioSrc = stdenv.mkDerivation { name = "forstio-src"; @@ -77,8 +85,16 @@ in rec { core = pkgs.callPackage ./modules/core/.nix/derivation.nix { inherit forstio stdenv clang-tools pname version; }; + # Both toolchains, same sources, same everything else -- so a failure + # in one and not the other points at the backend rather than the code. sycl = pkgs.callPackage ./modules/sycl/.nix/derivation.nix { - inherit forstio stdenv clang-tools pname version kel adaptive-cpp; + inherit forstio stdenv clang-tools pname version kel; + adaptive-cpp = adaptive-cpp-base; + }; + sycl-rocm = pkgs.callPackage ./modules/sycl/.nix/derivation.nix { + inherit forstio stdenv clang-tools pname version kel; + adaptive-cpp = adaptive-cpp-rocm; + variant = "-rocm"; }; }; }; diff --git a/examples/meta/meta.cpp b/examples/meta/meta.cpp index 2e2f28c..6e0649f 100644 --- a/examples/meta/meta.cpp +++ b/examples/meta/meta.cpp @@ -1,7 +1,5 @@ #include <kel/lbm/lbm.hpp> -#include <forstio/codec/args.hpp> - #include <iostream> namespace kel { @@ -34,7 +32,7 @@ kel::lbm::error_or<void> meta_lbm_info(const kel::lbm::data<kel::lbm::sch::MetaA {args_str.template get<"dt">()} }; print_lbm_meta<T,Desc>(conv, {args_str.template get<"kin_vis">()},{args_str.template get<"char_vel">()},{args_str.template get<"char_len">()}); - + return kel::lbm::make_void(); } @@ -42,22 +40,15 @@ kel::lbm::error_or<void> meta_lbm_info(const kel::lbm::data<kel::lbm::sch::MetaA kel::lbm::error_or<void> meta_main(int argc, char** argv){ using namespace kel::lbm; - std::string_view cfg_file_name = "config.json"; - if(argc > 1){ - cfg_file_name = argv[1]; - } - kel::lbm::data<sch::MetaArgs> args_dat; auto& args_str = args_dat.template get<"args">(); - { - { - args_str.template get<"dx">().set(1); - args_str.template get<"dt">().set(1); - } - kel::lbm::data<sch::MetaArgs, saw::encode::Args> args_enc{argc,argv}; - kel::lbm::codec<sch::MetaArgs, saw::encode::Args> args_codec; - auto eov = args_codec.decode(args_enc,args_dat); + // Defaults for anything the caller leaves out. + args_str.template get<"dx">().set(1); + args_str.template get<"dt">().set(1); + + { + auto eov = parse_args(args_dat, argc, argv); if(eov.is_error()){ return eov; } @@ -65,11 +56,10 @@ kel::lbm::error_or<void> meta_main(int argc, char** argv){ auto& args_pos = args_dat.template get<"positionals">(); - - switch(args_pos.get<0u>().get()){ + switch(args_pos.template get<0u>().get()){ case 1u: { - switch(args_pos.get<1u>().get()){ + switch(args_pos.template get<1u>().get()){ case 3u: meta_lbm_info<sch::Prec,sch::Descriptor<1u,3u>>(args_str); break; @@ -80,7 +70,7 @@ kel::lbm::error_or<void> meta_main(int argc, char** argv){ break; case 2u: { - switch(args_pos.get<1u>().get()){ + switch(args_pos.template get<1u>().get()){ case 5u: meta_lbm_info<sch::Prec,sch::Descriptor<2u,5u>>(args_str); break; @@ -94,7 +84,7 @@ kel::lbm::error_or<void> meta_main(int argc, char** argv){ break; case 3u: { - switch(args_pos.get<1u>().get()){ + switch(args_pos.template get<1u>().get()){ case 7u: return kel::lbm::make_error<kel::lbm::err::not_implemented>("D3Q7 Velocity Set is not implemented"); // meta_lbm_info<sch::Prec,sch::Descriptor<3u,19u>>(args_str); 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 <charconv> +#include <cstdlib> +#include <string> +#include <string_view> + +#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<typename InnerSchemaStruct, typename InnerSchemaTuple> +using Args = Struct< + Member<String, "program">, + Member<InnerSchemaStruct, "args">, + Member<InnerSchemaTuple, "positionals"> +>; +} + +template<typename T> +class data<T, enc::Args> { + static_assert(always_false<T>, + "Not supported. Use the sch::Args alias or check how it's defined."); +}; + +template<typename... Str, string_literal... Lits, typename... Tup> +class data<sch::Args<sch::Struct<sch::Member<Str,Lits>...>, sch::Tuple<Tup...>>, 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<uint64_t>(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<typename T, typename ToDec> +struct args_decode { + static_assert(always_false<T,ToDec>, + "No argument decoding for this schema. Add an args_decode specialization."); +}; + +template<typename K, uint64_t N, typename ToDec> +struct args_decode<sch::Primitive<K,N>, ToDec> { + using Schema = sch::Primitive<K,N>; + using Native = typename native_data_type<Schema>::type; + + static error_or<void> decode(data<Schema, ToDec>& to, std::string_view view){ + if(view.empty()){ + return make_error<err::invalid_state>("Can't decode empty data."); + } + + if constexpr ( std::is_floating_point_v<Native> ){ + /** + * 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<err::invalid_state>( + std::string{"Expected a number, got '"} + std::string{view} + "'"); + } + to.set(static_cast<Native>(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<err::invalid_state>( + std::string{"Expected an integer, got '"} + std::string{view} + "'"); + } + to.set(val); + } + + return make_void(); + } +}; + +template<typename ToDec> +struct args_decode<sch::Bool, ToDec> { + static error_or<void> decode(data<sch::Bool, ToDec>& 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<err::invalid_state>( + std::string{"Expected a boolean, got '"} + std::string{view} + "'"); + } +}; + +template<typename ToDec> +struct args_decode<sch::String, ToDec> { + static error_or<void> decode(data<sch::String, ToDec>& to, std::string_view view){ + try { + to.set(std::string{view}); + } catch(const std::exception&){ + return make_error<err::out_of_memory>(); + } + 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<typename S, typename I, typename ToDec> +struct args_decode<sch::MixedPrecision<S,I>, ToDec> { + using Schema = sch::MixedPrecision<S,I>; + + static error_or<void> decode(data<Schema, ToDec>& to, std::string_view view){ + data<I, ToDec> interface_value; + auto eov = args_decode<I, ToDec>::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<typename BaseSchema, typename... Elements, typename ToDec> +struct args_decode<sch::Unit<BaseSchema, Elements...>, ToDec> { + using Schema = sch::Unit<BaseSchema, Elements...>; + + static error_or<void> decode(data<Schema, ToDec>& to, std::string_view view){ + data<BaseSchema, ToDec> base; + auto eov = args_decode<BaseSchema, ToDec>::decode(base, view); + if(eov.is_error()){ + return eov; + } + to = data<Schema, ToDec>{base}; + return make_void(); + } +}; + +/** + * A Tensor of rank zero is a single value, so it decodes like one. This is + * what makes sch::Scalar<T> usable as an argument. + */ +template<typename Inner, typename ToDec> +struct args_decode<sch::Tensor<Inner>, ToDec> { + using Schema = sch::Tensor<Inner>; + + static error_or<void> decode(data<Schema, ToDec>& to, std::string_view view){ + data<Inner, ToDec> inner; + auto eov = args_decode<Inner, ToDec>::decode(inner, view); + if(eov.is_error()){ + return eov; + } + to.flat_at(0u) = inner; + return make_void(); + } +}; + +template<typename... Str, string_literal... Lits, typename... Tup, typename ToDec> +struct args_decode<sch::Args<sch::Struct<sch::Member<Str,Lits>...>, sch::Tuple<Tup...>>, ToDec> { + using Schema = sch::Args< + sch::Struct<sch::Member<Str,Lits>...>, + sch::Tuple<Tup...> + >; + + using SchemaStruct = sch::Struct<sch::Member<Str,Lits>...>; + using SchemaTuple = sch::Tuple<Tup...>; + + template<uint64_t i> + static error_or<void> decode_struct_member( + data<SchemaStruct, ToDec>& to, + std::string_view name, + std::string_view value + ){ + if constexpr ( i < sizeof...(Str) ){ + static constexpr string_literal Literal = parameter_key_pack_type<i,Lits...>::literal; + using StrT = typename parameter_pack_type<i,Str...>::type; + + if(Literal.view() == name){ + return args_decode<StrT, ToDec>::decode(to.template get<Literal>(), value); + } + + return decode_struct_member<i+1u>(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<err::not_found>( + std::string{"Unknown argument --"} + std::string{name}); + } + } + + template<uint64_t i> + static error_or<void> decode_tuple_member( + data<SchemaTuple, ToDec>& to, + uint64_t ptr_i, + std::string_view value + ){ + if constexpr ( i < sizeof...(Tup) ){ + if( i == ptr_i ){ + using TupT = typename parameter_pack_type<i,Tup...>::type; + return args_decode<TupT, ToDec>::decode(to.template get<i>(), value); + } + return decode_tuple_member<i+1u>(to, ptr_i, value); + }else{ + (void) to; + (void) ptr_i; + return make_error<err::invalid_state>( + std::string{"Too many positional arguments, did not expect '"} + + std::string{value} + "'"); + } + } + + static error_or<void> decode( + const data<Schema, enc::Args>& from, + data<Schema, ToDec>& to + ){ + if(from.size() == 0u){ + return make_error<err::invalid_state>("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<err::invalid_state>( + 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<err::invalid_state>( + std::string{"Expected "} + std::to_string(sizeof...(Tup)) + + " positional arguments, got " + std::to_string(tuple_pos)); + } + + return make_void(); + } +}; + +} // namespace impl + +template<typename Schema, typename Encoding> class codec; + +template<typename Schema> +class codec<Schema, enc::Args> final { +public: + codec() = default; + + template<typename ToDec> + error_or<void> decode(const data<Schema, enc::Args>& from, data<Schema, ToDec>& to){ + return impl::args_decode<Schema,ToDec>::decode(from,to); + } +}; + +/** + * Parses argv straight into a schema instance. + */ +template<typename ArgSch> +error_or<void> parse_args(data<ArgSch>& dec_dat, int argc, char** argv){ + data<ArgSch, enc::Args> arg_dat{argc, argv}; + codec<ArgSch, enc::Args> 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<environment> init_lbm_env(const kel::lbm::data<ArgSch>& 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<std::filesystem::path> output_directory(){ +inline kel::lbm::error_or<std::filesystem::path> 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<typename ParticleSchema::ValueType>::calculate(pg,index,mom_f.meta()); - if(eo_aabb.is_error()){ + kel::lbm::data<typename particle_aabb<typename ParticleSchema::ValueType>::AABB> aabb; + if(not particle_aabb<typename ParticleSchema::ValueType>::try_calculate(pg,index,mom_f.meta(),aabb)){ return; } - auto& aabb = eo_aabb.get_value(); kel::lbm::data<sch::Scalar<T>> 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<typename kel::lbm::native_data_type<T>::type>(steps) ); - auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(part_spheroid_group,index,mvel_f.meta()); - if(eo_aabb.is_error()){ + kel::lbm::data<typename particle_aabb<typename ParticleSchema::ValueType>::AABB> aabb; + if(not particle_aabb<typename ParticleSchema::ValueType>::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<typename kel::lbm::native_data_type<T>::type>(steps) ); - auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(part_group,index,mvel_f.meta()); - if(eo_aabb.is_error()){ + kel::lbm::data<typename particle_aabb<typename ParticleSchema::ValueType>::AABB> aabb; + if(not particle_aabb<typename ParticleSchema::ValueType>::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<sch::FixedArray<sch::UInt64,D>,"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<typename Sch, typename Encode> - static constexpr kel::lbm::error_or<kel::lbm::data<AABB>> calculate(const kel::lbm::data<Sch,Encode>& p_grp, const kel::lbm::data<sch::FixedArray<sch::UInt64,1u>>& index, const kel::lbm::data<sch::FixedArray<sch::UInt64,D>>& meta){ + static constexpr bool try_calculate(const kel::lbm::data<Sch,Encode>& p_grp, const kel::lbm::data<sch::FixedArray<sch::UInt64,1u>>& index, const kel::lbm::data<sch::FixedArray<sch::UInt64,D>>& meta, kel::lbm::data<AABB>& out){ static_assert(PC > 0u, "Can't calculate from no particles"); if(not (index.at({{0u}}).get() < PC) ){ - return kel::lbm::make_error<kel::lbm::err::critical>("Too large i in particle_aabb"); + return false; } - kel::lbm::data<AABB> aabb; + kel::lbm::data<AABB>& 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<typename Sch, typename Encode> + static kel::lbm::error_or<kel::lbm::data<AABB>> calculate(const kel::lbm::data<Sch,Encode>& p_grp, const kel::lbm::data<sch::FixedArray<sch::UInt64,1u>>& index, const kel::lbm::data<sch::FixedArray<sch::UInt64,D>>& meta){ + kel::lbm::data<AABB> aabb; + if(not try_calculate(p_grp,index,meta,aabb)){ + return kel::lbm::make_error<kel::lbm::err::critical>("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<sch::UInt64> sub_steps){ template<uint64_t D> 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 <cmath> -#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<sch::ParticleGroup<T,D,Pa } }; - - } } diff --git a/modules/core/c++/particle/porosity.hpp b/modules/core/c++/particle/porosity.hpp index 593b15e..cd4e804 100644 --- a/modules/core/c++/particle/porosity.hpp +++ b/modules/core/c++/particle/porosity.hpp @@ -88,7 +88,6 @@ public: } }; - template<typename T, uint64_t D, uint64_t PC> class particle_porosity<T, D, PC, coll::Spheroid<T>> 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<Scalar<T>, "density"> >; - /** * CollisionType is defaulted so the plain `Particle<T,D>` 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<typename particle_aabb<typename ParticleSchema::ValueType>::AABB> aabb; + if(not particle_aabb<typename ParticleSchema::ValueType>::try_calculate(pg,index,vel_f.meta(),aabb)){ return; } - auto& aabb = eo_aabb.get_value(); - kel::lbm::data<sch::FixedArray<sch::UInt64,Descriptor::D>> start; kel::lbm::data<sch::FixedArray<sch::UInt64,Descriptor::D>> 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 <forstio/test/suite.hpp> + +#include <cmath> +#include <string> +#include <vector> + +#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<std::string> store; + std::vector<char*> ptrs; + + explicit Argv(std::vector<std::string> words): store{std::move(words)} { + ptrs.reserve(store.size()); + for(auto& s : store){ + ptrs.push_back(s.data()); + } + } + + int argc() const { return static_cast<int>(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::Float64, "dx">, + sch::Member<sch::Float64, "dt"> + >; + using Sch = sch::Args<ArgStruct, sch::Tuple<sch::UInt64, sch::UInt64>>; + + Argv a{{"prog","--dx","0.25","2","--dt","0.5","9"}}; + + kel::lbm::data<Sch> 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::Member<sch::Float64, "dx">>, + sch::Tuple<> + >; + + Argv a{{"prog","--nope","1"}}; + + kel::lbm::data<Sch> 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::Member<sch::Float64, "dx">>, + sch::Tuple<sch::UInt64> + >; + + { + Argv a{{"prog","--dx","abc","1"}}; + kel::lbm::data<Sch> 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<Sch> 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<Sch> 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<Sch> 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<Sch> 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::Member<sch::UInt64, "count">>, + sch::Tuple<> + >; + + Argv good{{"prog","--count","42"}}; + kel::lbm::data<Sch> 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<Sch> 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::String, "coupling">, + sch::Member<sch::Bool, "verbose"> + >, + sch::Tuple<> + >; + + Argv a{{"prog","--coupling","fplbm","--verbose","true"}}; + kel::lbm::data<Sch> 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<Sch> 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<Sch> 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<sch::Float32, sch::Float64>; + using Sch = sch::Args< + sch::Struct<sch::Member<Mixed, "visc">>, + sch::Tuple<> + >; + + Argv a{{"prog","--visc","0.5"}}; + kel::lbm::data<Sch> 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<decltype(v.get()), double>, + "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<sch::SiMeter<sch::Float64>, "length">, + sch::Member<sch::SiSecond<sch::Float64>, "duration"> + >, + sch::Tuple<> + >; + + Argv a{{"prog","--length","2.5","--duration","0.1"}}; + kel::lbm::data<Sch> 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<decltype(args.template get<"length">())>, + std::decay_t<decltype(args.template get<"duration">())>>, + "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<T> is a rank zero tensor; it should take one number, not a list. + using Sch = sch::Args< + sch::Struct<sch::Member<sch::Scalar<sch::Float64>, "radius">>, + sch::Tuple<> + >; + + Argv a{{"prog","--radius","3.75"}}; + kel::lbm::data<Sch> 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::Struct<>, sch::Tuple<>>; + + kel::lbm::data<Sch> 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 <forstio/test/suite.hpp> + +#include <cmath> +#include <string> + +#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<kel::lbm::enc::Native>; + +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<sch::UInt64, 4u>; + + data<Sch> host; + for(uint64_t i = 0u; i < 4u; ++i){ + host.at({i}).set(i * 10u + 1u); + } + + data<Sch, Dev> 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<Sch> 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<sch::UInt64, 8u>; + + data<Sch> host; + for(uint64_t i = 0u; i < 8u; ++i){ + host.at({i}).set(0u); + } + + data<Sch, Dev> device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data<sch::Ptr<Sch>, Dev> view{device_data}; + + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<1u>{8u}, [=](acpp::sycl::id<1u> idx){ + data<sch::FixedArray<sch::UInt64,1u>> 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<sch::Float64, 1u>; + + data<Sch> host{data<sch::FixedArray<sch::UInt64,1u>>{{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<double>(i) * 0.5); + } + + data<Sch, Dev> device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data<Sch> back{data<sch::FixedArray<sch::UInt64,1u>>{{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<double>(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<sch::FixedArray<sch::UInt64,3u>, "counts">, + sch::Member<sch::FixedArray<sch::Float64,3u>, "weights"> + >; + + data<Sch> 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<double>(i) + 0.25); + } + + data<Sch, Dev> device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data<Sch> 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<double>(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<sch::UInt64, 0u, nx, ny>; + + data<Sch> host; + lbm::iterator<2u>::apply([&](const auto& index){ + host.at(index).set(0u); + },{},host.get_dims()); + + data<Sch, Dev> device_data{q}; + auto eov = dev.copy_to_device(host, device_data); + SAW_EXPECT(eov.is_value(), "copy to device failed"); + q.wait(); + + data<sch::Ptr<Sch>, 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<sch::FixedArray<sch::UInt64,2u>> 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<sch::FixedArray<sch::UInt64,4u>, "a">, + sch::Member<sch::FixedArray<sch::UInt64,4u>, "b"> + >; + + data<Sch> 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<Sch, Dev> 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<sch::FixedArray<sch::UInt64,1u>> 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<sch::Float64, 5u>; + + data<Sch> host; + for(uint64_t i = 0u; i < 5u; ++i){ + host.at({i}).set(static_cast<double>(i)); + } + + data<Sch, Dev> device_data{q}; + SAW_EXPECT(dev.copy_to_device(host, device_data).is_value(), "copy to device failed"); + q.wait(); + + data<sch::Ptr<Sch>, Dev> view{device_data}; + + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<1u>{5u}, [=](acpp::sycl::id<1u> idx){ + data<sch::FixedArray<sch::UInt64,1u>> index; + index.at({{0u}}).set(idx[0]); + + auto& v = view.at(index); + data<sch::Float64> two; + two.set(2.0); + data<sch::Float64> 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<double>(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 <exception> +#include <iostream> +#include <string> + +/** + * 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") |
