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/sycl/.nix/derivation.nix | 9 +- modules/sycl/tests/basic_ops.cpp | 349 +++++++++++++++++++++++++++++++++++++++ modules/sycl/tests/skip.hpp | 89 ++++++++++ 3 files changed, 446 insertions(+), 1 deletion(-) create mode 100644 modules/sycl/tests/basic_ops.cpp create mode 100644 modules/sycl/tests/skip.hpp (limited to 'modules/sycl') 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