From b965ed922f70fe9ae465cfa338a74aa0ebf2c9ae Mon Sep 17 00:00:00 2001 From: Lukas Burgholzer Date: Fri, 9 Oct 2026 23:22:30 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Unify=20QDMI=20Slurm?= =?UTF-8?q?=20integration=20and=20the=20reusable=20cluster?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use Slurm environment export for job configuration and an optional standalone SPANK module for license-specific site defaults. Probe device availability explicitly after runtime setup with the shared native command launcher. Provide a scalable Docker Compose cluster for demonstrations and integration checks. Keep device compilation independent of Core rebuilds and run shared transport coverage once. Shorten the guides and remove automatic validation, custom job options, duplicated launcher code, and redundant fixture machinery. Co-authored-by: Patrick Hopf <81010725+flowerthrower@users.noreply.github.com> Assisted-by: GPT-6 via Codex --- .agent/plans/qdmi-slurm.md | 42 ++ .github/workflows/ci.yml | 6 +- .github/workflows/slurm.yml | 19 +- .license-tools-config.json | 2 + CMakeLists.txt | 3 + bindings/qdmi/slurm.cpp | 8 +- docker/slurm/Dockerfile | 106 +++ docker/slurm/README.md | 63 ++ {test => docker}/slurm/cgroup.conf | 0 docker/slurm/compose.yml | 58 ++ docker/slurm/node.conf | 11 + docker/slurm/prepare.sh | 20 + {test => docker}/slurm/slurm.conf | 10 +- .../slurm/start.sh | 8 + docs/glossary.md | 6 + docs/qdmi/configuration.md | 8 +- docs/qdmi/driver.md | 25 +- docs/qdmi/slurm.md | 338 ++++------ include/mqt-core/qdmi/Slurm.hpp | 12 +- noxfile.py | 2 +- pyproject.toml | 3 + python/mqt/core/_commands.py | 7 + python/mqt/core/qdmi/slurm.pyi | 8 +- spank/CMakeLists.txt | 38 ++ spank/LICENSE.md | 614 ++++++++++++++++++ spank/mqt-core-qdmi-spank.map | 33 + spank/spank.cpp | 296 +++++++++ src/qdmi/CMakeLists.txt | 11 + src/qdmi/Check.cpp | 198 ++++++ test/python/test_cli.py | 25 +- test/python/test_slurm_integration.py | 72 +- test/qdmi/CMakeLists.txt | 9 + test/qdmi/driver/session_device.cpp | 12 + test/qdmi/test_check_cli.cmake | 36 + test/slurm/.dockerignore | 1 - test/slurm/Dockerfile | 46 -- test/slurm/bell_job.py | 2 +- test/slurm/compose.yml | 99 +-- test/slurm/mqt-slurm-prepare.service | 20 - test/slurm/mqt-slurm-test-environment.conf | 11 + test/slurm/provider_probe.py | 38 ++ test/slurm/run_integration.py | 443 ++++++++++--- test/slurm/sc_job.py | 2 +- 43 files changed, 2248 insertions(+), 523 deletions(-) create mode 100644 .agent/plans/qdmi-slurm.md create mode 100644 docker/slurm/Dockerfile create mode 100644 docker/slurm/README.md rename {test => docker}/slurm/cgroup.conf (100%) create mode 100644 docker/slurm/compose.yml create mode 100644 docker/slurm/node.conf create mode 100644 docker/slurm/prepare.sh rename {test => docker}/slurm/slurm.conf (65%) rename test/slurm/mqt-slurm-prepare => docker/slurm/start.sh (69%) create mode 100644 spank/CMakeLists.txt create mode 100644 spank/LICENSE.md create mode 100644 spank/mqt-core-qdmi-spank.map create mode 100644 spank/spank.cpp create mode 100644 src/qdmi/Check.cpp create mode 100644 test/qdmi/test_check_cli.cmake delete mode 100644 test/slurm/.dockerignore delete mode 100644 test/slurm/Dockerfile delete mode 100644 test/slurm/mqt-slurm-prepare.service create mode 100644 test/slurm/mqt-slurm-test-environment.conf create mode 100644 test/slurm/provider_probe.py diff --git a/.agent/plans/qdmi-slurm.md b/.agent/plans/qdmi-slurm.md new file mode 100644 index 0000000000..ab287e7b8d --- /dev/null +++ b/.agent/plans/qdmi-slurm.md @@ -0,0 +1,42 @@ +# QDMI workloads on Slurm + +Status: implemented; publication and hosted checks pending. + +## Goal and scope + +Provide one Slurm integration above MQT Core's QDMI client interface. Device +implementations supply their runtime, credentials, and a small smoke test. The +Docker cluster in `docker/slurm/` supports tests and local demonstrations. + +## Decisions + +- Slurm license names identify QDMI devices; admission and device authorization + remain separate. The selector accepts one ID with an optional `:1` count. +- Use Slurm's environment export for job settings. The optional source-only + SPANK module supplies license-specific site defaults without loading devices. +- Run the availability command after selecting the workload environment. Keep + its bounded native worker; remove automatic SPANK validation and per-step + state. +- Use the shared native command launcher and runtime installation helpers. +- Use one scalable Compose compute service, with disposable rootful Linux + cgroup-v2 hosts as the supported deployment boundary. +- Consolidate the unreleased Core stack so these contracts are reviewed + together. + +## Progress + +- [x] Simplify the plugin, checker, documentation, and test coverage. +- [x] Separate reusable cluster setup from test assertions and validate scaling. +- [x] Refresh device integrations on upstream main, simplify smoke tests, and + make ordinary IQM CI work without live service credentials. +- [x] Validate focused native/Python checks, standalone SPANK, and cluster + scaling. +- [x] Pass IQM and Braket smoke tests in both native and wheel modes. +- [ ] Publish the revised PRs and inspect hosted checks. + +## Validation + +Run the focused checker tests, native command tests, standalone SPANK lint, +cluster admission/environment tests, and IQM/Braket native/wheel smoke tests. +Run each repository's required lint. Check final-head CI separately from local +results. Real quantum hardware is outside this task. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f39a775174..34e902dd0e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -137,9 +137,9 @@ jobs: setup-python: true install-pkgs: "nanobind==3.1.0" cpp-linter-extra-args: "-std=c++20 -Wunused-template" - # Vendored third-party headers are checked upstream and are not maintained - # according to MQT Core's clang-tidy configuration. - cpp-linter-ignore-extra: "vendor/**" + # Vendored headers are checked upstream; standalone SPANK is checked in + # the Slurm workflow against its own compilation database and headers. + cpp-linter-ignore-extra: "vendor/**|spank/**" setup-mlir: true llvm-version: 23.1.2 permissions: diff --git a/.github/workflows/slurm.yml b/.github/workflows/slurm.yml index ecd42b8372..83b96e3de9 100644 --- a/.github/workflows/slurm.yml +++ b/.github/workflows/slurm.yml @@ -13,6 +13,8 @@ on: - "pyproject.toml" - "python/mqt/core/qdmi/**" - "src/qdmi/**" + - "spank/**" + - "docker/slurm/**" - "test/slurm/**" - "test/qdmi/**" - "test/python/qdmi/**" @@ -28,6 +30,8 @@ on: - "pyproject.toml" - "python/mqt/core/qdmi/**" - "src/qdmi/**" + - "spank/**" + - "docker/slurm/**" - "test/slurm/**" - "test/qdmi/**" - "test/python/qdmi/**" @@ -44,7 +48,7 @@ permissions: jobs: slurm: - name: Slurm 25.11 and QDMI devices + name: Slurm 25.11, QDMI devices, and SPANK runs-on: ubuntu-26.04 timeout-minutes: 45 env: @@ -78,10 +82,19 @@ jobs: with: version: v0.16.0 + - name: Lint the standalone SPANK component + run: | + sudo apt-get update + curl -fsSL https://apt.llvm.org/llvm.sh -o "$RUNNER_TEMP/llvm-install.sh" + sudo bash "$RUNNER_TEMP/llvm-install.sh" 23 + sudo apt-get install --no-install-recommends -y clang-tidy-23 libslurm-dev + cmake -S spank -B build/spank -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + clang-tidy-23 -p build/spank spank/spank.cpp --header-filter='(^|/)spank/' --warnings-as-errors='*' + - name: Build the MQT Core wheel timeout-minutes: 15 - run: uv build --wheel --out-dir test/slurm/dist -Ccmake.define.DEPLOY=ON + run: uv build --wheel --out-dir dist -Ccmake.define.DEPLOY=ON - name: Test Slurm admission and QDMI execution timeout-minutes: 15 - run: uv run --no-project --python 3.14 test/slurm/run_integration.py + run: uv run --no-project --python 3.14 test/slurm/run_integration.py --nodes 3 diff --git a/.license-tools-config.json b/.license-tools-config.json index 5080d2adaa..01b2b6fd69 100644 --- a/.license-tools-config.json +++ b/.license-tools-config.json @@ -18,6 +18,8 @@ ".proto": "SLASH_STYLE" }, "exclude": [ + "^spank/", + "^docker/slurm/Dockerfile$", "^\\.[^/]+", "/\\.[^/]+", ".*\\.qasm", diff --git a/CMakeLists.txt b/CMakeLists.txt index 654c661958..82254173c7 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,6 +180,9 @@ endif() if(BUILD_MQT_CORE_BINDINGS) set(MQT_CORE_WHEEL_TARGETS mqt-core-bench-bindings mqt-core-dd-bindings mqt-core-qdmi-bindings) + if(TARGET mqt-core-qdmi-check) + list(APPEND MQT_CORE_WHEEL_TARGETS mqt-core-qdmi-check) + endif() if(BUILD_MQT_CORE_MLIR) list(APPEND MQT_CORE_WHEEL_TARGETS mqt-cc mqt-core-bench mqt-core-mlir-bindings) endif() diff --git a/bindings/qdmi/slurm.cpp b/bindings/qdmi/slurm.cpp index 7ca641f184..8ddac54f91 100644 --- a/bindings/qdmi/slurm.cpp +++ b/bindings/qdmi/slurm.cpp @@ -25,18 +25,18 @@ void registerSlurm(nb::module_& qdmiModule) { R"pb(Open the QDMI device named by the Slurm license environment. ``SLURM_JOB_LICENSES`` must contain one local license whose name equals a stable -ID visible to the selected QDMI Driver. The optional count must be one. The -function opens a fresh Client session and accepts device status ``IDLE`` or +ID visible to the selected QDMI driver. The optional count must be one. The +function opens a fresh driver session and accepts device status ``IDLE`` or ``BUSY``. It does not apply job-specific QDMI configuration or credentials. Warning: ``SLURM_JOB_LICENSES`` is process-mutable. This function uses it only for device selection. It does not verify a Slurm allocation, authenticate the - caller, or authorize device access. The provider or operating system must + caller, or authorize device access. The device implementation or operating system must enforce access independently. Returns: - mqt.core.qdmi.Device: The fresh device session. + mqt.core.qdmi.Device: The selected device. Raises: RuntimeError: If the license value or named device does not satisfy this diff --git a/docker/slurm/Dockerfile b/docker/slurm/Dockerfile new file mode 100644 index 0000000000..1c6953be18 --- /dev/null +++ b/docker/slurm/Dockerfile @@ -0,0 +1,106 @@ +# syntax=docker/dockerfile:1.19 + +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +FROM ghcr.io/astral-sh/uv:0.12.1 AS uv + +FROM ubuntu:26.04 AS spank + +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + cmake g++ make libslurm-dev +COPY --from=core spank /src/spank +RUN cmake -S /src/spank -B /build -DCMAKE_BUILD_TYPE=Release \ + && cmake --build /build --parallel 2 \ + && cmake --install /build --prefix /usr/local + +FROM ubuntu:26.04 AS base + +ENV container=docker + +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + ca-certificates \ + libcurl4t64 \ + dbus \ + munge \ + python3 \ + slurm-wlm \ + systemd \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=uv /uv /uvx /bin/ +COPY --from=spank /usr/local/lib/slurm/mqt-core-qdmi-spank.so /usr/local/lib/slurm/ +COPY --from=spank /usr/local/share/licenses/mqt-core-qdmi-spank /usr/local/share/licenses/mqt-core-qdmi-spank +COPY --chmod=755 start.sh /usr/local/sbin/mqt-slurm-start +COPY node.conf /etc/systemd/system/slurmd.service.d/mqt-node.conf + +RUN useradd --uid 10000 --user-group --create-home mqt-test \ + && systemctl disable slurmctld.service slurmd.service + +RUN --mount=type=bind,from=dist,target=/tmp/dist \ + --mount=type=cache,target=/root/.cache/uv \ + uv pip install --system --break-system-packages --link-mode=copy /tmp/dist/mqt_core-*.whl + +ARG SETUP_SCRIPT="" + +FROM scratch AS provider-source + +COPY --from=workload \ + --exclude=.git --exclude=.github --exclude=build --exclude=build-* --exclude=cmake-build-* \ + --exclude=.cache --exclude=.ccache --exclude=.venv* --exclude=.nox --exclude=CMakeUserPresets.json \ + --exclude=.ruff_cache --exclude=.rumdl_cache --exclude=.pytest_cache --exclude=.mypy_cache \ + --exclude=**/__pycache__ --exclude=dist --exclude=test/slurm/runtime --exclude=test/slurm/dist \ + . /workload + +FROM ubuntu:26.04 AS provider-build + +COPY --from=uv /uv /uvx /bin/ + +ARG SETUP_SCRIPT="" +RUN if [ -n "$SETUP_SCRIPT" ]; then \ + apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ + build-essential ca-certificates cmake git libcurl4-openssl-dev libssl-dev python3; \ + fi + +ARG PROVIDER_RUNTIME_COMPONENT="" +RUN --mount=type=bind,from=provider-source,source=/workload,target=/workload \ + --mount=type=cache,target=/root/.cache/uv \ + mkdir -p /opt/provider/wheels /opt/provider/native \ + && if [ -n "$SETUP_SCRIPT" ]; then \ + CMAKE_BUILD_PARALLEL_LEVEL=2 uv build /workload --wheel --out-dir /opt/provider/wheels \ + --config-setting=build-dir=/tmp/provider-build \ + && cmake --install /tmp/provider-build --prefix /opt/provider/native \ + --component "${PROVIDER_RUNTIME_COMPONENT:?Set the provider Runtime component}"; \ + fi + +FROM base AS common + +ARG PROVIDER_INSTALL_MODE="native" +RUN --mount=type=bind,from=dist,target=/tmp/dist \ + --mount=type=bind,from=provider-source,source=/workload,target=/workload \ + --mount=type=bind,from=provider-build,source=/opt/provider,target=/tmp/provider \ + --mount=type=cache,target=/root/.cache/uv \ + if [ -n "$SETUP_SCRIPT" ]; then \ + case "$PROVIDER_INSTALL_MODE" in native|wheel) ;; *) exit 1 ;; esac \ + && set -- /tmp/provider/wheels/*.whl \ + && test "$#" -eq 1 && test -f "$1" && provider_wheel="$1[qiskit]" \ + && set -- /tmp/dist/mqt_core-*.whl \ + && test "$#" -eq 1 && test -f "$1" \ + && printf 'mqt-core[qiskit,pennylane] @ file://%s\n' "$1" > /tmp/core-override.txt \ + && uv pip install --system --break-system-packages --link-mode=copy \ + --overrides /tmp/core-override.txt "$provider_wheel" "$1[qiskit,pennylane]" \ + && if [ "$PROVIDER_INSTALL_MODE" = native ]; then cp -a /tmp/provider/native /opt/provider-native; fi \ + && /bin/sh "/workload/$SETUP_SCRIPT"; \ + fi + +STOPSIGNAL SIGRTMIN+3 +CMD ["/usr/local/sbin/mqt-slurm-start", "node"] diff --git a/docker/slurm/README.md b/docker/slurm/README.md new file mode 100644 index 0000000000..77469a590c --- /dev/null +++ b/docker/slurm/README.md @@ -0,0 +1,63 @@ +# Local Slurm cluster + +Run a small Slurm cluster for QDMI demonstrations and integration tests. It uses +one controller and as many compute containers as requested. Each compute node +offers two CPUs and 512 MiB of scheduled memory. + +Use rootful Docker Compose on a disposable Linux host with cgroup v2. The +containers are privileged and share the host cgroup hierarchy so that Slurm can +enforce job CPU and memory allocations. This setup is for local use; it does not +configure a production cluster. + +From the MQT Core checkout, build exactly one wheel and prepare the shared +files: + +```console +uv build --wheel --out-dir dist -Ccmake.define.DEPLOY=ON +sh docker/slurm/prepare.sh +docker compose -f docker/slurm/compose.yml up --build -d --wait --scale node=2 +``` + +The image builds the optional GPL SPANK plugin from `spank/` separately from the +MIT MQT Core wheel. `build/slurm/plugstack.conf` is initially empty. Site +defaults can be enabled there using the configuration in the +[Slurm guide](../../docs/qdmi/slurm.md). + +Submit jobs as the unprivileged user shared by all nodes: + +```console +docker compose -f docker/slurm/compose.yml exec --user 10000:10000 controller \ + srun --licenses=mqt.sc.default python3 /workspace/test/slurm/sc_job.py +docker compose -f docker/slurm/compose.yml exec controller sinfo +docker compose -f docker/slurm/compose.yml up -d --wait --scale node=4 +``` + +Slurm registers each compute container dynamically using its unique hostname. +The default partition accepts up to 128 nodes. Scaling down stops containers; +Slurm retains their inactive node records until `scontrol delete NodeName=...` +or a fresh cluster is created. Drain nodes and wait for their jobs before +scaling down an active cluster. + +The shared `/jobs` directory is `build/slurm/jobs` on the host. Edit +`build/slurm/slurm.conf` to change the license counts or Slurm configuration, +then run `scontrol reconfigure` in the controller. Stop the cluster with: + +```console +docker compose -f docker/slurm/compose.yml down --volumes +rm -r build/slurm +``` + +The image and Docker build cache remain available. To run isolated clusters, use +a different Compose `--project-name`, set `MQT_CORE_SLURM_RUNTIME` to an +absolute directory, and pass that directory to `prepare.sh`. + +The integration runner in `test/slurm/run_integration.py` uses these same images +and services. Its test-only overlay adds daemon environment sentinels; the +reusable image contains no test configuration. Run it with `--nodes 3` to check +a different cluster size. `MQT_CORE_SLURM_DIST` selects an existing directory +containing one MQT Core wheel. Device implementation tests can extend the image +with `MQT_CORE_SLURM_WORKLOAD`, `MQT_CORE_SLURM_SETUP_SCRIPT`, and the build +arguments `PROVIDER_RUNTIME_COMPONENT` and `PROVIDER_INSTALL_MODE` (`native` or +`wheel`). Set build arguments on `controller`; `node` reuses that image. +Provider Compose overlays add their mock endpoint and its startup dependency to +both Slurm services. diff --git a/test/slurm/cgroup.conf b/docker/slurm/cgroup.conf similarity index 100% rename from test/slurm/cgroup.conf rename to docker/slurm/cgroup.conf diff --git a/docker/slurm/compose.yml b/docker/slurm/compose.yml new file mode 100644 index 0000000000..88bc3dc223 --- /dev/null +++ b/docker/slurm/compose.yml @@ -0,0 +1,58 @@ +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +x-cluster: &cluster + privileged: true + cgroup: host + tmpfs: + - /run + - /run/lock + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + - ${MQT_CORE_SLURM_CORE:-../..}:/workspace:ro + - ${MQT_CORE_SLURM_WORKLOAD:-../..}:/workload:ro + - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}:/runtime + - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}/jobs:/jobs + - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}/slurm.conf:/etc/slurm/slurm.conf:ro + - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}/plugstack.conf:/etc/slurm/plugstack.conf:ro + - ./cgroup.conf:/etc/slurm/cgroup.conf:ro + +x-healthcheck: &healthcheck + interval: 2s + timeout: 2s + retries: 30 + start_period: 5s + +services: + controller: + <<: *cluster + build: + context: . + additional_contexts: + core: ${MQT_CORE_SLURM_CORE:-../..} + workload: ${MQT_CORE_SLURM_WORKLOAD:-../..} + dist: ${MQT_CORE_SLURM_DIST:-../../dist} + args: + SETUP_SCRIPT: ${MQT_CORE_SLURM_SETUP_SCRIPT:-} + hostname: controller + command: [/usr/local/sbin/mqt-slurm-start, controller] + healthcheck: + <<: *healthcheck + test: + [CMD, systemctl, is-active, --quiet, munge.service, slurmctld.service] + + node: + <<: *cluster + image: ${COMPOSE_PROJECT_NAME:-slurm}-controller + pull_policy: never + depends_on: + controller: + condition: service_healthy + healthcheck: + <<: *healthcheck + test: [CMD, systemctl, is-active, --quiet, munge.service, slurmd.service] diff --git a/docker/slurm/node.conf b/docker/slurm/node.conf new file mode 100644 index 0000000000..0c086180e1 --- /dev/null +++ b/docker/slurm/node.conf @@ -0,0 +1,11 @@ +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +[Service] +ExecStart= +ExecStart=/usr/sbin/slurmd --systemd -Z --conf "CPUs=2 Boards=1 SocketsPerBoard=1 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=512" diff --git a/docker/slurm/prepare.sh b/docker/slurm/prepare.sh new file mode 100644 index 0000000000..0ea7c79d49 --- /dev/null +++ b/docker/slurm/prepare.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +set -eu +umask 077 +runtime=${1:-"$(dirname "$0")/../../build/slurm"} +mkdir -p "$(dirname "$runtime")" +mkdir "$runtime" +head -c 1024 /dev/urandom > "$runtime/munge.key" +mkdir "$runtime/jobs" +chmod 1777 "$runtime/jobs" +cp "$(dirname "$0")/slurm.conf" "$runtime/slurm.conf" +touch "$runtime/plugstack.conf" +chmod 644 "$runtime/slurm.conf" "$runtime/plugstack.conf" diff --git a/test/slurm/slurm.conf b/docker/slurm/slurm.conf similarity index 65% rename from test/slurm/slurm.conf rename to docker/slurm/slurm.conf index 8bb1182833..95985254de 100644 --- a/test/slurm/slurm.conf +++ b/docker/slurm/slurm.conf @@ -6,7 +6,7 @@ # # Licensed under the MIT License -ClusterName=mqt-core-test +ClusterName=mqt-core SlurmctldHost=controller SlurmUser=slurm AuthType=auth/munge @@ -19,7 +19,8 @@ ProctrackType=proctrack/cgroup TaskPlugin=task/cgroup,task/affinity JobAcctGatherType=jobacct_gather/cgroup SelectType=select/cons_tres -SelectTypeParameters=CR_CPU +SelectTypeParameters=CR_CPU_Memory +DefMemPerCPU=256 PrologFlags=Contain SchedulerType=sched/backfill @@ -29,6 +30,5 @@ SlurmctldParameters=reconfig_on_restart Licenses=mqt.ddsim.default:2,mqt.sc.default:1 -NodeName=node1 NodeAddr=node1 CPUs=2 Boards=1 SocketsPerBoard=1 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=512 State=UNKNOWN -NodeName=node2 NodeAddr=node2 CPUs=2 Boards=1 SocketsPerBoard=1 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=512 State=UNKNOWN -PartitionName=compute Nodes=node1,node2 Default=YES MaxTime=INFINITE State=UP +MaxNodeCount=128 +PartitionName=compute Nodes=ALL Default=YES MaxTime=INFINITE State=UP diff --git a/test/slurm/mqt-slurm-prepare b/docker/slurm/start.sh similarity index 69% rename from test/slurm/mqt-slurm-prepare rename to docker/slurm/start.sh index a106b55932..5593f4bf4e 100755 --- a/test/slurm/mqt-slurm-prepare +++ b/docker/slurm/start.sh @@ -9,9 +9,17 @@ set -eu +case "${1:-node}" in + controller) daemon=slurmctld ;; + node) daemon=slurmd ;; + *) echo "Expected controller or node" >&2; exit 1 ;; +esac + test -s /runtime/munge.key install -d -o munge -g munge -m 0755 /etc/munge /run/munge install -o munge -g munge -m 0400 /runtime/munge.key /etc/munge/munge.key install -d -o slurm -g slurm -m 0755 /var/spool/slurmctld /var/log/slurm install -d -o root -g root -m 0755 /var/spool/slurmd +systemctl enable munge.service "$daemon.service" +exec /usr/lib/systemd/systemd diff --git a/docs/glossary.md b/docs/glossary.md index fd49176b9f..2c3e71b4bb 100644 --- a/docs/glossary.md +++ b/docs/glossary.md @@ -152,6 +152,12 @@ C++ QDMI library the QDMI Client Interface. The driver manages device libraries and their configuration; the C++ library is independent of that implementation. +SPANK + **Preferred term:** SPANK. **Accepted expansion:** Slurm Plug-in Architecture + for Node and job Kontrol. Slurm's plugin interface for job launch hooks. + MQT Core's optional SPANK module supplies license-specific defaults for job + environments. + QIR Quantum Intermediate Representation **Preferred term:** Quantum Intermediate Representation. **Accepted diff --git a/docs/qdmi/configuration.md b/docs/qdmi/configuration.md index c881e7f645..2b7d5614d0 100644 --- a/docs/qdmi/configuration.md +++ b/docs/qdmi/configuration.md @@ -215,10 +215,10 @@ The equivalent C++ function is `qdmi::slurm::openDeviceFromLicense()` from `` or `:1`. They reject remote, compound, and non-unit license values. -The adapter opens a fresh device session from the persistent definition. It does -not replace configuration or inject credentials. Each device implementation -defines its own credential sources. The adapter accepts QDMI device status -`IDLE` and `BUSY`. It rejects all other device states. +The adapter creates a fresh driver session and selects the device with the +licensed ID. It does not replace configuration or inject credentials. Each +device implementation defines its own credential sources. The adapter accepts +QDMI device status `IDLE` and `BUSY`. It rejects all other device states. `SLURM_JOB_LICENSES` is process-mutable. The adapter uses this value only for device selection. It does not verify that Slurm allocated the license. It does diff --git a/docs/qdmi/driver.md b/docs/qdmi/driver.md index 708d9ffdb8..08462055a8 100644 --- a/docs/qdmi/driver.md +++ b/docs/qdmi/driver.md @@ -119,10 +119,33 @@ integration tests. The driver is a shared library. The C++ QDMI library follows the project’s static/shared build setting and is shared in Python wheels. Device-free builds can use another QDMI driver through `driver_path` or `MQT_CORE_QDMI_DRIVER`. The -The builtin MQT Core QDMI driver can load external device libraries through +builtin MQT Core QDMI driver can load external device libraries through [QDMI device configuration](configuration.md). C++ test builds require the bundled devices available in the selected build configuration. +## Probe device availability + +The availability command probes whether a configured QDMI device is operational. +It opens the device, accepts an `IDLE` or `BUSY` status, and exits without +submitting a quantum job: + +```console +mqt-core-qdmi-check --device mqt.sc.default --timeout 10 +``` + +Run it in the workload environment with the required credentials. For an +external device, set `MQT_CORE_QDMI_CONFIG_FILE` to its catalogue; the native +command uses [QDMI configuration](configuration.md), independently of Python's +entry-point discovery and in-process registrations. + +The timeout covers device initialization, the status query, and worker exit. It +defaults to 30 seconds and accepts whole seconds from 1 to 3600. Exit codes are +0 for availability, 1 for failure, 2 for invalid arguments, and 124 for a +timeout. Success is silent. Device output is suppressed to protect credentials. + +The command is included in Linux and macOS builds and wheels. Availability is a +snapshot; it does not authorize access or reserve device capacity. + ## Python Bindings The C++ QDMI library adds owning wrappers for driver sessions, devices, sites, diff --git a/docs/qdmi/slurm.md b/docs/qdmi/slurm.md index 0ab6e59132..ed61431746 100644 --- a/docs/qdmi/slurm.md +++ b/docs/qdmi/slurm.md @@ -1,269 +1,163 @@ # Use QDMI devices with Slurm -This example uses Slurm 25.11 or newer on Ubuntu 26.04. It has one controller, -two compute nodes, and two CPUs on each compute node. +Slurm can limit concurrent access to a quantum device through a cluster-wide +license. Use a QDMI device ID as the license name, then open that device in the +job with MQT Core. The job can compile and submit workloads through the same +QDMI interface it uses outside Slurm. -A Slurm license controls admission to a cluster-wide resource. In this setup, -the license name is a stable QDMI device ID. A license does not show provider -availability. It does not show the device queue. The QDMI provider supplies that -information when its interface supports it. +Slurm schedules jobs; the device implementation authenticates users and submits +quantum work. A license does not grant device access or reserve capacity at a +remote service. Device availability and queues can change after admission. -## Understand the control boundaries +## Run a job -This example is suitable for admission and accounting tests on a cooperative -cluster. It does not make a Slurm license an access-control credential. The -controls are independent: +Install MQT Core and the required QDMI device implementation in the workload +environment. Make its catalogue, libraries, and credentials available on the +compute nodes. See [device configuration](configuration.md) and the device +implementation's installation guide. -- Slurm admits jobs and accounts for the configured license count. -- The MQT Core adapter uses the license environment to select a client-visible - QDMI device. -- The QDMI provider reports device availability and queue data. -- The provider or the operating system authorizes access to the device. - -`SLURM_JOB_LICENSES` is process-mutable. A job can change it before it calls -`slurm.open_device_from_license()`. Thus, the function does not prove that Slurm -allocated the named license. It does not authenticate the user. It does not -authorize access. A lookup through another Slurm interface would not make MQT -Core an access-control boundary because a program can also call -`mqt.core.qdmi.open_device(device_id)` directly. - -## Install the software - -Install the same MQT Core package on each compute node. The package contains the -MQT Core QDMI interface and the bundled QDMI devices. You can use a shared -software environment or install the same wheel on each node. - -Install Slurm, Munge, and systemd. Start Munge before Slurm. Use the same Munge -key on all nodes. Keep this key outside the QDMI device configuration. - -Use the unified cgroup v2 hierarchy. Add these settings to `slurm.conf`: - -```ini -ProctrackType=proctrack/cgroup -TaskPlugin=task/cgroup,task/affinity -JobAcctGatherType=jobacct_gather/cgroup -SelectType=select/cons_tres -SelectTypeParameters=CR_CPU -``` - -Use the cgroup plugin to constrain processors and memory. For example, use this -`cgroup.conf`: +An administrator registers each device ID in `slurm.conf`, for example: ```ini -CgroupPlugin=autodetect -ConstrainCores=yes -ConstrainRAMSpace=yes -ConstrainSwapSpace=yes +Licenses=mqt.sc.default:1,amazon.braket.sv1:2,iqm.emerald:1 ``` -This fixture has no local device file. For strict isolation of a local device, -configure it as a Slurm GRES with a `File=` entry. Also set -`ConstrainDevices=yes` for the cgroup task plugin. Slurm can then restrict the -device files that a job can open. See the [Slurm GRES configuration] -documentation. A remote QPU has no local device file, so the provider must -enforce its authorization. +The counts limit simultaneous Slurm allocations. Choose counts appropriate for +the device and the site's access policy. -Run `slurmd -C` on each compute node and use its output for the `NodeName` -record. The MQT Core test uses two CPUs on `node1` and `node2`. +Request one device with `--licenses=ID` or `--licenses=ID:1`: -## Register the devices - -MQT Core installs persistent definitions for `mqt.ddsim.default`, -`mqt.sc.default`, `mqt.sc.iqm.garnet`, and `mqt.sc.iqm.emerald`, so these four -devices need no further registry file. Verify their stable IDs before you -configure Slurm: - -```console -python -c "from mqt.core.qdmi import builtin_driver; print(*builtin_driver.registered_device_ids(), sep='\n')" +```bash +#!/bin/bash +#SBATCH --licenses=amazon.braket.sv1 +#SBATCH --time=00:05:00 +set -eu + +source /shared/quantum/.venv/bin/activate +export MQT_CORE_QDMI_CONFIG_FILE=/shared/quantum/devices.json +export AWS_PROFILE=research +mqt-core-qdmi-check --device amazon.braket.sv1 --timeout 10 +srun python workload.py ``` -For an external provider, install its shared library and QDMI manifest. You can -also add one trusted system registry file at `/etc/mqt-core/qdmi.json`. The `id` -field in that file is the stable device ID. Use the same ID as the local Slurm -license name. Do not put short-lived access tokens in this file. Use the -authentication method that the provider documents for batch jobs. - -The Slurm adapter does not supply credentials. IQM can use its configured token -source. Amazon Braket uses the AWS credential provider chain, such as an -instance role, workload identity, or AWS profile. A job can also export provider -configuration. Use provider-scoped credentials with minimum permissions. Do not -store access tokens or AWS access keys in a persistent device definition. +In `workload.py`, select the allocated device: -Add the licenses to `slurm.conf` on the controller: +```python +from mqt.core.qdmi import slurm -```ini -Licenses=mqt.ddsim.default:2,mqt.sc.default:1 +device = slurm.open_device_from_license() ``` -The DDSIM count is two. Therefore, Slurm can admit two jobs that each request -one DDSIM license. These jobs can run on different nodes. The count is a local -cluster policy. It is not a DDSIM property and it is not a per-node count. +Pass `device` to the appropriate Qiskit or PennyLane adapter. MQT Core accepts +one local license with a unit count and requires the device to report `IDLE` or +`BUSY`. Compound license expressions and remote licenses are unsupported. -Restart `slurmctld` after you first add the licenses. Reconfigure the compute -nodes as required by your Slurm installation. Then inspect the configured -resources: +The optional [availability command](driver.md#probe-device-availability) gives a +quick indication that the device is operational. Run it after activating the +workload environment and setting credentials. It does not reserve the device. +The workload still opens the device and handles submission errors normally. -```console -scontrol show lic -``` +Slurm exports the submission environment by default. Use ordinary environment +variables or Slurm's `--export` option for job-specific settings. Variables set +inside a batch script are inherited by its subsequent `srun` steps. -The initial report must contain these values: +## Configure the cluster -```text -LicenseName=mqt.ddsim.default Total=2 Used=0 Free=2 Remote=no -LicenseName=mqt.sc.default Total=1 Used=0 Free=1 Remote=no -``` +Use matching Slurm versions across the cluster. This integration requires Slurm +25.11 or newer. -## Submit a DDSIM job +| Location | Software and configuration | +| ---------------------- | ---------------------------------------------------- | +| Login/submission nodes | Slurm clients and access to the workload environment | +| Controller | `slurmctld`, scheduling policy, and license counts | +| Compute nodes | `slurmd`, cgroup v2, and the workload environment | +| Accounting service | `slurmdbd` when persistent accounting is needed | -Save this program as `bell.py` in a location that all compute nodes can read: +Static local licenses do not require an accounting database. The controller does +not need device libraries or SDKs. Use consistent numeric user/group IDs and +readable catalogue/library paths across compute nodes. A shared versioned +environment and identical per-node installations are both suitable. -```python -from mqt.core.qdmi import ProgramFormat, slurm - -program = """OPENQASM 2.0; -include "qelib1.inc"; -qreg q[2]; -creg c[2]; -h q[0]; -cx q[0], q[1]; -measure q -> c; -""" +Keep scheduler authentication, such as Munge, separate from device credentials. +For CPU and allocated-memory constraints, use memory-consuming selection with +cgroup enforcement: -device = slurm.open_device_from_license() -job = device.submit_job(program, ProgramFormat.QASM2, num_shots=256) -if not job.wait(60): - raise RuntimeError("DDSIM did not finish within 60 seconds") - -counts = job.get_counts() -if sum(counts.values()) != 256 or not set(counts) <= {"00", "11"}: - raise RuntimeError(f"Invalid Bell results: {counts}") -print(counts) +```ini +# slurm.conf +ProctrackType=proctrack/cgroup +TaskPlugin=task/cgroup,task/affinity +JobAcctGatherType=jobacct_gather/cgroup +SelectType=select/cons_tres +SelectTypeParameters=CR_CPU_Memory ``` -Save this batch script as `bell.sbatch`: - -```bash -#!/bin/bash -#SBATCH --nodes=1 -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=1 -#SBATCH --licenses=mqt.ddsim.default:1 -#SBATCH --output=bell-%j.out - -set -euo pipefail -python bell.py +```ini +# cgroup.conf +CgroupPlugin=cgroup/v2 +ConstrainCores=yes +ConstrainRAMSpace=yes +ConstrainSwapSpace=yes ``` -The adapter reads `SLURM_JOB_LICENSES` and selects the persistent device -definition with the same ID. It requires one unambiguous QDMI device license. It -opens a fresh device session and checks the device status. The function accepts -`IDLE` and `BUSY`. This check is not authorization. The provider can still -reject a later submission or put the quantum task in its device queue. - -Submit the job with this command: - -```console -sbatch bell.sbatch -``` +Set node resources, memory defaults, partitions, accounts, and limits for the +site. See the +[Slurm administration guide](https://slurm.schedmd.com/quickstart_admin.html) +and [cgroup configuration](https://slurm.schedmd.com/cgroup.conf.html). -The same open handle works with application adapters. Pass it to -{py:class}`mqt.core.plugins.qiskit.backend.QDMIBackend` or to the PennyLane -{py:class}`mqt.core.plugins.pennylane.device.QDMIDevice`. See the -{doc}`pennylane_device` guide for the PennyLane constructor. +`SLURM_JOB_LICENSES` is mutable within a process. MQT Core uses it for device +selection, not as proof of allocation or authorization. Device services and +operating-system permissions must enforce access independently. -Open the selected device once per application process and reuse its handle for -subsequent quantum jobs. The adapter validates the license locally, opens only -the selected device, and queries its status once. Device selection uses the -ordinary QDMI driver lookup and needs no controller RPC. Provider initialization -and network requests can still dominate opening time; use provider-supported -timeout settings for network requests. +## Optional site defaults with SPANK -## Check concurrent jobs +Use MQT Core's SPANK module when a device license should select default +catalogue paths or credential references. Jobs can override these defaults +through their environment. No plugin is needed when jobs already supply their +configuration. -For a scheduling test, add a sufficiently long classical post-processing step -after the Python command. For example, add `sleep 120` to `bell.sbatch`. Then -submit two jobs: +Build the standalone module against the cluster's Slurm development headers: ```console -sbatch --nodelist=node1 bell.sbatch -sbatch --nodelist=node2 bell.sbatch +cmake -S spank -B build/spank -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local +cmake --build build/spank +cmake --install build/spank ``` -Both jobs can run because two DDSIM licenses exist. Each job uses one CPU. One -CPU remains free on each node. Submit a third DDSIM job. Slurm keeps it pending -until one DDSIM license becomes free. +The build requires Linux, CMake, and a C++20 compiler. It does not need LLVM or +device SDKs. Install the module and its `plugstack.conf` entry on compute nodes. +If login or submission hosts use the same plugstack configuration, install the +module there too. Rebuild it when changing Slurm major versions. +`MQT_CORE_SPANK_INSTALL_DIR` selects the module installation directory. -Use these commands to inspect the state: +Load the module once, with the permitted device IDs and non-secret defaults: -```console -squeue --format="%.18i %.9T %.20R %.12N %.20L" -scontrol show lic mqt.ddsim.default -scontrol show node node1 -scontrol show node node2 +```ini +required /usr/local/lib/slurm/mqt-core-qdmi-spank.so licenses=amazon.braket.sv1,iqm.emerald qdmi_config_file=/etc/mqt-core/qdmi.json reference=AWS_PROFILE:amazon.braket.sv1:quantum reference=IQM_TOKENS_FILE:iqm.emerald:/shared/iqm/tokens.json ``` -The third job must have state `PENDING` and reason `Licenses`. The license -report must show `Total=2 Used=2 Free=0`. The node records must still show one -free CPU on each node. A job that requests `mqt.sc.default:1` can use one of -these CPUs because it uses a different license. - -When one DDSIM job ends, Slurm returns its license. The pending job can then -start without a change to the device registry or `slurm.conf`. - -## Diagnose a failure +`qdmi_config_file=PATH` supplies `MQT_CORE_QDMI_CONFIG_FILE`. Each +`reference=ENV:ID,ID:DEFAULT` supplies an environment variable for the listed +device IDs. Use paths, profile names, and other non-secret references; keep +tokens and passwords out of `plugstack.conf`. -First, run `scontrol show job `. Check `JobState`, `Reason`, `Licenses`, -and `NodeList`. Use `scontrol show lic` to compare the total, used, and free -counts. Use `scontrol show node` to check CPU allocation. +Defaults apply only to an exact configured `ID` or `ID:1` license expression. +Other jobs pass through unchanged. Values already present in the job environment +take precedence. The module never reads credential files or loads a device +implementation, and it does not copy credentials from Slurm daemons. Invalid +settings fail the job without draining the compute node. -If a node is down, check `systemctl status munge slurmd` and the Slurm journal -on that node. Check that `/sys/fs/cgroup/cgroup.controllers` exists. Check that -all nodes use the same Munge key and the same `slurm.conf`. +The module is GPL-3.0-or-later and distributed in the source checkout, +separately from MQT Core's MIT-licensed runtime, wheels, and source packages. -If MQT Core cannot select a device, print `SLURM_JOB_LICENSES` inside the batch -job and list the IDs visible to `Session`. Use this value only to diagnose -selection. It is not proof of the Slurm allocation. The license name and stable -ID must match exactly. Do not add a generic device license. Do not use a Slurm -OR license expression for device selection because the environment does not -identify a single selected device in that case. +## Try the Docker cluster -[Slurm GRES configuration]: https://slurm.schedmd.com/gres.conf.html - -## Run the integration tests - -From a source checkout on a Linux Docker host with cgroup v2, build one wheel -and run the fixture: - -```console -uv build --wheel --out-dir test/slurm/dist -Ccmake.define.DEPLOY=ON -uv run --no-project --python 3.14 test/slurm/run_integration.py -``` - -Keep exactly one wheel in `test/slurm/dist`. The fixture installs that wheel in -one controller and two compute containers. It checks admission, license -contention, independent SC execution, Bell results, and terminal job states and -exit codes. It uses privileged containers to exercise real Slurm cgroups; it is -an isolated test cluster, not a production deployment template. - -Each invocation uses its own Docker project, Munge key, and directory below -`test/slurm/runtime`. Successful runs remove their containers, images, and -artifacts; failures retain artifacts and print the project and runtime path. -Docker's build cache remains available to later runs. Commands have 30-second -deadlines, image build/startup has a ten-minute deadline, and diagnostics and -cleanup have separate short deadlines. Batch jobs request a five-minute time -limit. An interrupted command terminates its process group, including a Docker -Compose child. - -The runner prints setup, execution, and total durations. Its inexpensive -failure-path tests can run before building a wheel: - -```console -uv run --no-project --with 'pytest>=9.0.1' --python 3.14 pytest -o addopts= -q test/python/test_slurm_integration.py -``` +The reusable +[Docker Slurm setup](https://github.com/munich-quantum-toolkit/core/tree/main/docker/slurm) +supports local demonstrations and integration tests with a configurable number +of compute containers. Follow its README to build the workload image, start the +cluster, submit jobs, and remove it. -CI enables the existing sccache compiler integration and reports cache counters. -Compare compiler requests, hits, and wall time before attributing a build-time -change to caching. The fixture's bounded polling targets its private controller; -do not copy these loops into production monitoring. See the -[Slurm RPC performance guidance](https://slurm.schedmd.com/squeue.html#SECTION_PERFORMANCE). +Use rootful Docker on a disposable Linux cgroup-v2 host. The containers run +systemd and require privileged access to the host cgroup hierarchy. Jobs run as +an unprivileged user. This setup is intended for development and demonstrations, +not a production security boundary. diff --git a/include/mqt-core/qdmi/Slurm.hpp b/include/mqt-core/qdmi/Slurm.hpp index 559eadd99d..cb72ce748b 100644 --- a/include/mqt-core/qdmi/Slurm.hpp +++ b/include/mqt-core/qdmi/Slurm.hpp @@ -19,16 +19,16 @@ namespace qdmi::slurm { /// Opens the QDMI device named by the Slurm license environment. -/// @return A fresh device session using the registered device definition. +/// @return The selected device. /// /// The @c SLURM_JOB_LICENSES value must contain exactly one local -/// license. Its name must equal a registered QDMI device ID. The optional -/// license count must be one. The device must report @c QDMI_DEVICE_STATUS_IDLE -/// or @c QDMI_DEVICE_STATUS_BUSY. +/// license. Its name must equal an ID visible to the selected QDMI driver. +/// The optional license count must be one. The device must report @c +/// QDMI_DEVICE_STATUS_IDLE or @c QDMI_DEVICE_STATUS_BUSY. /// @warning This function uses process-mutable environment data for device /// selection. It does not verify a Slurm allocation, authenticate the caller, -/// or authorize access to the device. The provider or operating system must -/// enforce access independently. +/// or authorize access to the device. The device implementation or operating +/// system must enforce access independently. /// @throws std::runtime_error If the license value is missing, malformed, /// compound, remote, has a non-unit count, names an unknown device, or names a /// device in another state. diff --git a/noxfile.py b/noxfile.py index e035694ee9..3006d929b1 100755 --- a/noxfile.py +++ b/noxfile.py @@ -111,7 +111,7 @@ def cpp_lint(session: nox.Session) -> None: "--style=", "--tidy-checks=", f"--version={llvm_bin}", - "--ignore=build|!build/mlir/**|**/include|include|vendor/**", + "--ignore=build|!build/mlir/**|**/include|include|vendor/**|spank/**", "--thread-comments=false", "--step-summary=false", "--database=build/cpp-lint", diff --git a/pyproject.toml b/pyproject.toml index f4ff5cb3d5..d3a769ab4c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,6 +114,7 @@ PyPI = "https://pypi.org/project/mqt-core/" mqt-cc = "mqt.core._commands:compiler" mqt-core-bench = "mqt.core._commands:benchmark" mqt-core-cli = "mqt.core.__main__:main" +mqt-core-qdmi-check = "mqt.core._commands:qdmi_check" [project.entry-points."pennylane.plugins"] "mqt.ddsim.default" = "mqt.core.plugins.pennylane:DDSIMDevice" @@ -202,6 +203,7 @@ version = ">=1.10" include = [ "python/mqt/core/_version.py" ] exclude = [ + "spank/**", "**/.github", "**/doc", "**/docs", @@ -376,6 +378,7 @@ PC170 = "We do not use rST files anymore" [tool.check-sdist] sdist-only = [ "python/mqt/core/_version.py" ] git-only = [ + "spank/*", "docs/*", "test/*", "mlir/*", diff --git a/python/mqt/core/_commands.py b/python/mqt/core/_commands.py index 98d9223098..d381e51437 100644 --- a/python/mqt/core/_commands.py +++ b/python/mqt/core/_commands.py @@ -27,6 +27,13 @@ def benchmark() -> NoReturn: run_tool("mqt-core-bench") +def qdmi_check() -> NoReturn: + """Probe whether a QDMI device is operational.""" + if sys.platform == "win32": + sys.exit("The QDMI availability check requires Linux or macOS.") + run_tool("mqt-core-qdmi-check") + + def run_tool(name: str) -> NoReturn: """Replace this process with a bundled native tool.""" suffix = ".exe" if sys.platform == "win32" else "" diff --git a/python/mqt/core/qdmi/slurm.pyi b/python/mqt/core/qdmi/slurm.pyi index 4c15545394..ba17718d89 100644 --- a/python/mqt/core/qdmi/slurm.pyi +++ b/python/mqt/core/qdmi/slurm.pyi @@ -14,18 +14,18 @@ def open_device_from_license() -> mqt.core.qdmi.Device: """Open the QDMI device named by the Slurm license environment. ``SLURM_JOB_LICENSES`` must contain one local license whose name equals a stable - ID visible to the selected QDMI Driver. The optional count must be one. The - function opens a fresh Client session and accepts device status ``IDLE`` or + ID visible to the selected QDMI driver. The optional count must be one. The + function opens a fresh driver session and accepts device status ``IDLE`` or ``BUSY``. It does not apply job-specific QDMI configuration or credentials. Warning: ``SLURM_JOB_LICENSES`` is process-mutable. This function uses it only for device selection. It does not verify a Slurm allocation, authenticate the - caller, or authorize device access. The provider or operating system must + caller, or authorize device access. The device implementation or operating system must enforce access independently. Returns: - mqt.core.qdmi.Device: The fresh device session. + mqt.core.qdmi.Device: The selected device. Raises: RuntimeError: If the license value or named device does not satisfy this diff --git a/spank/CMakeLists.txt b/spank/CMakeLists.txt new file mode 100644 index 0000000000..c54d80b1b3 --- /dev/null +++ b/spank/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of the optional MQT Core SPANK component. +# See LICENSE.md for the GNU General Public License, version 3 or later. + +cmake_minimum_required(VERSION 3.28...4.4) +project(mqt-core-qdmi-spank LANGUAGES CXX) + +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux") + message(FATAL_ERROR "The MQT Core SPANK component requires Linux and Slurm 25.11 or newer") +endif() + +include(GNUInstallDirs) +find_path(SLURM_SPANK_INCLUDE_DIR slurm/spank.h REQUIRED) + +add_library(mqt-core-qdmi-spank MODULE spank.cpp) +target_compile_features(mqt-core-qdmi-spank PRIVATE cxx_std_20) +target_include_directories(mqt-core-qdmi-spank SYSTEM PRIVATE ${SLURM_SPANK_INCLUDE_DIR}) +set_target_properties(mqt-core-qdmi-spank PROPERTIES PREFIX "") +target_link_options(mqt-core-qdmi-spank PRIVATE + "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/mqt-core-qdmi-spank.map") +set_property( + TARGET mqt-core-qdmi-spank + APPEND + PROPERTY LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/mqt-core-qdmi-spank.map") + +set(MQT_CORE_SPANK_INSTALL_DIR + "${CMAKE_INSTALL_LIBDIR}/slurm" + CACHE PATH "Install directory for the Slurm SPANK module") +install(TARGETS mqt-core-qdmi-spank LIBRARY DESTINATION ${MQT_CORE_SPANK_INSTALL_DIR} + COMPONENT mqt-core-qdmi-spank) +install( + FILES LICENSE.md + DESTINATION ${CMAKE_INSTALL_DATADIR}/licenses/mqt-core-qdmi-spank + COMPONENT mqt-core-qdmi-spank) diff --git a/spank/LICENSE.md b/spank/LICENSE.md new file mode 100644 index 0000000000..1cc5da492a --- /dev/null +++ b/spank/LICENSE.md @@ -0,0 +1,614 @@ +# GNU General Public License + +_Version 3, 29 June 2007_ +_Copyright © 2007 Free Software Foundation, Inc. <>_ + +Everyone is permitted to copy and distribute verbatim copies of this license +document, but changing it is not allowed. + +## Preamble + +The GNU General Public License is a free, copyleft license for software and +other kinds of works. + +The licenses for most software and other practical works are designed to take +away your freedom to share and change the works. By contrast, the GNU General +Public License is intended to guarantee your freedom to share and change all +versions of a program--to make sure it remains free software for all its users. +We, the Free Software Foundation, use the GNU General Public License for most of +our software; it applies also to any other work released this way by its +authors. You can apply it to your programs, too. + +When we speak of free software, we are referring to freedom, not price. Our +General Public Licenses are designed to make sure that you have the freedom to +distribute copies of free software (and charge for them if you wish), that you +receive source code or can get it if you want it, that you can change the +software or use pieces of it in new free programs, and that you know you can do +these things. + +To protect your rights, we need to prevent others from denying you these rights +or asking you to surrender the rights. Therefore, you have certain +responsibilities if you distribute copies of the software, or if you modify it: +responsibilities to respect the freedom of others. + +For example, if you distribute copies of such a program, whether gratis or for a +fee, you must pass on to the recipients the same freedoms that you received. You +must make sure that they, too, receive or can get the source code. And you must +show them these terms so they know their rights. + +Developers that use the GNU GPL protect your rights with two steps: **(1)** +assert copyright on the software, and **(2)** offer you this License giving you +legal permission to copy, distribute and/or modify it. + +For the developers' and authors' protection, the GPL clearly explains that there +is no warranty for this free software. For both users' and authors' sake, the +GPL requires that modified versions be marked as changed, so that their problems +will not be attributed erroneously to authors of previous versions. + +Some devices are designed to deny users access to install or run modified +versions of the software inside them, although the manufacturer can do so. This +is fundamentally incompatible with the aim of protecting users' freedom to +change the software. The systematic pattern of such abuse occurs in the area of +products for individuals to use, which is precisely where it is most +unacceptable. Therefore, we have designed this version of the GPL to prohibit +the practice for those products. If such problems arise substantially in other +domains, we stand ready to extend this provision to those domains in future +versions of the GPL, as needed to protect the freedom of users. + +Finally, every program is threatened constantly by software patents. States +should not allow patents to restrict development and use of software on +general-purpose computers, but in those that do, we wish to avoid the special +danger that patents applied to a free program could make it effectively +proprietary. To prevent this, the GPL assures that patents cannot be used to +render the program non-free. + +The precise terms and conditions for copying, distribution and modification +follow. + +## TERMS AND CONDITIONS + +### 0. Definitions + +"This License" refers to version 3 of the GNU General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds of works, +such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this License. Each +licensee is addressed as "you". "Licensees" and "recipients" may be individuals +or organizations. + +To "modify" a work means to copy from or adapt all or part of the work in a +fashion requiring copyright permission, other than the making of an exact copy. +The resulting work is called a "modified version" of the earlier work or a work +"based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based on the +Program. + +To "propagate" a work means to do anything with it that, without permission, +would make you directly or secondarily liable for infringement under applicable +copyright law, except executing it on a computer or modifying a private copy. +Propagation includes copying, distribution (with or without modification), +making available to the public, and in some countries other activities as well. + +To "convey" a work means any kind of propagation that enables other parties to +make or receive copies. Mere interaction with a user through a computer network, +with no transfer of a copy, is not conveying. + +An interactive user interface displays "Appropriate Legal Notices" to the extent +that it includes a convenient and prominently visible feature that **(1)** +displays an appropriate copyright notice, and **(2)** tells the user that there +is no warranty for the work (except to the extent that warranties are provided), +that licensees may convey the work under this License, and how to view a copy of +this License. If the interface presents a list of user commands or options, such +as a menu, a prominent item in the list meets this criterion. + +### 1. Source Code + +The "source code" for a work means the preferred form of the work for making +modifications to it. "Object code" means any non-source form of a work. + +A "Standard Interface" means an interface that either is an official standard +defined by a recognized standards body, or, in the case of interfaces specified +for a particular programming language, one that is widely used among developers +working in that language. + +The "System Libraries" of an executable work include anything, other than the +work as a whole, that **(a)** is included in the normal form of packaging a +Major Component, but which is not part of that Major Component, and **(b)** +serves only to enable use of the work with that Major Component, or to implement +a Standard Interface for which an implementation is available to the public in +source code form. A "Major Component", in this context, means a major essential +component (kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to produce the +work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all the source +code needed to generate, install, and (for an executable work) run the object +code and to modify the work, including scripts to control those activities. +However, it does not include the work's System Libraries, or general-purpose +tools or generally available free programs which are used unmodified in +performing those activities but which are not part of the work. For example, +Corresponding Source includes interface definition files associated with source +files for the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, such as by +intimate data communication or control flow between those subprograms and other +parts of the work. + +The Corresponding Source need not include anything that users can regenerate +automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. + +### 2. Basic Permissions + +All rights granted under this License are granted for the term of copyright on +the Program, and are irrevocable provided the stated conditions are met. This +License explicitly affirms your unlimited permission to run the unmodified +Program. The output from running a covered work is covered by this License only +if the output, given its content, constitutes a covered work. This License +acknowledges your rights of fair use or other equivalent, as provided by +copyright law. + +You may make, run and propagate covered works that you do not convey, without +conditions so long as your license otherwise remains in force. You may convey +covered works to others for the sole purpose of having them make modifications +exclusively for you, or provide you with facilities for running those works, +provided that you comply with the terms of this License in conveying all +material for which you do not control copyright. Those thus making or running +the covered works for you must do so exclusively on your behalf, under your +direction and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the conditions +stated below. Sublicensing is not allowed; section 10 makes it unnecessary. + +### 3. Protecting Users' Legal Rights From Anti-Circumvention Law + +No covered work shall be deemed part of an effective technological measure under +any applicable law fulfilling obligations under article 11 of the WIPO copyright +treaty adopted on 20 December 1996, or similar laws prohibiting or restricting +circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention is +effected by exercising rights under this License with respect to the covered +work, and you disclaim any intention to limit operation or modification of the +work as a means of enforcing, against the work's users, your or third parties' +legal rights to forbid circumvention of technological measures. + +### 4. Conveying Verbatim Copies + +You may convey verbatim copies of the Program's source code as you receive it, +in any medium, provided that you conspicuously and appropriately publish on each +copy an appropriate copyright notice; keep intact all notices stating that this +License and any non-permissive terms added in accord with section 7 apply to the +code; keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you may +offer support or warranty protection for a fee. + +### 5. Conveying Modified Source Versions + +You may convey a work based on the Program, or the modifications to produce it +from the Program, in the form of source code under the terms of section 4, +provided that you also meet all of these conditions: + +- **a)** The work must carry prominent notices stating that you modified it, and + giving a relevant date. +- **b)** The work must carry prominent notices stating that it is released under + this License and any conditions added under section 7. This requirement + modifies the requirement in section 4 to "keep intact all notices". +- **c)** You must license the entire work, as a whole, under this License to + anyone who comes into possession of a copy. This License will therefore apply, + along with any applicable section 7 additional terms, to the whole of the + work, and all its parts, regardless of how they are packaged. This License + gives no permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. +- **d)** If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive interfaces + that do not display Appropriate Legal Notices, your work need not make them do + so. + +A compilation of a covered work with other separate and independent works, which +are not by their nature extensions of the covered work, and which are not +combined with it such as to form a larger program, in or on a volume of a +storage or distribution medium, is called an "aggregate" if the compilation and +its resulting copyright are not used to limit the access or legal rights of the +compilation's users beyond what the individual works permit. Inclusion of a +covered work in an aggregate does not cause this License to apply to the other +parts of the aggregate. + +### 6. Conveying Non-Source Forms + +You may convey a covered work in object code form under the terms of sections 4 +and 5, provided that you also convey the machine-readable Corresponding Source +under the terms of this License, in one of these ways: + +- **a)** Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the Corresponding + Source fixed on a durable physical medium customarily used for software + interchange. +- **b)** Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a written offer, + valid for at least three years and valid for as long as you offer spare parts + or customer support for that product model, to give anyone who possesses the + object code either **(1)** a copy of the Corresponding Source for all the + software in the product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no more than + your reasonable cost of physically performing this conveying of source, or + **(2)** access to copy the Corresponding Source from a network server at no + charge. +- **c)** Convey individual copies of the object code with a copy of the written + offer to provide the Corresponding Source. This alternative is allowed only + occasionally and noncommercially, and only if you received the object code + with such an offer, in accord with subsection 6b. +- **d)** Convey the object code by offering access from a designated place + (gratis or for a charge), and offer equivalent access to the Corresponding + Source in the same way through the same place at no further charge. You need + not require recipients to copy the Corresponding Source along with the object + code. If the place to copy the object code is a network server, the + Corresponding Source may be on a different server (operated by you or a third + party) that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the Corresponding + Source, you remain obligated to ensure that it is available for as long as + needed to satisfy these requirements. +- **e)** Convey the object code using peer-to-peer transmission, provided you + inform other peers where the object code and Corresponding Source of the work + are being offered to the general public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded from the +Corresponding Source as a System Library, need not be included in conveying the +object code work. + +A "User Product" is either **(1)** a "consumer product", which means any +tangible personal property which is normally used for personal, family, or +household purposes, or **(2)** anything designed or sold for incorporation into +a dwelling. In determining whether a product is a consumer product, doubtful +cases shall be resolved in favor of coverage. For a particular product received +by a particular user, "normally used" refers to a typical or common use of that +class of product, regardless of the status of the particular user or of the way +in which the particular user actually uses, or expects or is expected to use, +the product. A product is a consumer product regardless of whether the product +has substantial commercial, industrial or non-consumer uses, unless such uses +represent the only significant mode of use of the product. + +"Installation Information" for a User Product means any methods, procedures, +authorization keys, or other information required to install and execute +modified versions of a covered work in that User Product from a modified version +of its Corresponding Source. The information must suffice to ensure that the +continued functioning of the modified object code is in no case prevented or +interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as part of a +transaction in which the right of possession and use of the User Product is +transferred to the recipient in perpetuity or for a fixed term (regardless of +how the transaction is characterized), the Corresponding Source conveyed under +this section must be accompanied by the Installation Information. But this +requirement does not apply if neither you nor any third party retains the +ability to install modified object code on the User Product (for example, the +work has been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates for a +work that has been modified or installed by the recipient, or for the User +Product in which it has been modified or installed. Access to a network may be +denied when the modification itself materially and adversely affects the +operation of the network or violates the rules and protocols for communication +across the network. + +Corresponding Source conveyed, and Installation Information provided, in accord +with this section must be in a format that is publicly documented (and with an +implementation available to the public in source code form), and must require no +special password or key for unpacking, reading or copying. + +### 7. Additional Terms + +"Additional permissions" are terms that supplement the terms of this License by +making exceptions from one or more of its conditions. Additional permissions +that are applicable to the entire Program shall be treated as though they were +included in this License, to the extent that they are valid under applicable +law. If additional permissions apply only to part of the Program, that part may +be used separately under those permissions, but the entire Program remains +governed by this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option remove any +additional permissions from that copy, or from any part of it. (Additional +permissions may be written to require their own removal in certain cases when +you modify the work.) You may place additional permissions on material, added by +you to a covered work, for which you have or can give appropriate copyright +permission. + +Notwithstanding any other provision of this License, for material you add to a +covered work, you may (if authorized by the copyright holders of that material) +supplement the terms of this License with terms: + +- **a)** Disclaiming warranty or limiting liability differently from the terms + of sections 15 and 16 of this License; or +- **b)** Requiring preservation of specified reasonable legal notices or author + attributions in that material or in the Appropriate Legal Notices displayed by + works containing it; or +- **c)** Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in reasonable ways + as different from the original version; or +- **d)** Limiting the use for publicity purposes of names of licensors or + authors of the material; or +- **e)** Declining to grant rights under trademark law for use of some trade + names, trademarks, or service marks; or +- **f)** Requiring indemnification of licensors and authors of that material by + anyone who conveys the material (or modified versions of it) with contractual + assumptions of liability to the recipient, for any liability that these + contractual assumptions directly impose on those licensors and authors. + +All other non-permissive additional terms are considered "further restrictions" +within the meaning of section 10. If the Program as you received it, or any part +of it, contains a notice stating that it is governed by this License along with +a term that is a further restriction, you may remove that term. If a license +document contains a further restriction but permits relicensing or conveying +under this License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does not survive +such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must place, +in the relevant source files, a statement of the additional terms that apply to +those files, or a notice indicating where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the form of a +separately written license, or stated as exceptions; the above requirements +apply either way. + +### 8. Termination + +You may not propagate or modify a covered work except as expressly provided +under this License. Any attempt otherwise to propagate or modify it is void, and +will automatically terminate your rights under this License (including any +patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a +particular copyright holder is reinstated **(a)** provisionally, unless and +until the copyright holder explicitly and finally terminates your license, and +**(b)** permanently, if the copyright holder fails to notify you of the +violation by some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated +permanently if the copyright holder notifies you of the violation by some +reasonable means, this is the first time you have received notice of violation +of this License (for any work) from that copyright holder, and you cure the +violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses of +parties who have received copies or rights from you under this License. If your +rights have been terminated and not permanently reinstated, you do not qualify +to receive new licenses for the same material under section 10. + +### 9. Acceptance Not Required for Having Copies + +You are not required to accept this License in order to receive or run a copy of +the Program. Ancillary propagation of a covered work occurring solely as a +consequence of using peer-to-peer transmission to receive a copy likewise does +not require acceptance. However, nothing other than this License grants you +permission to propagate or modify any covered work. These actions infringe +copyright if you do not accept this License. Therefore, by modifying or +propagating a covered work, you indicate your acceptance of this License to do +so. + +### 10. Automatic Licensing of Downstream Recipients + +Each time you convey a covered work, the recipient automatically receives a +license from the original licensors, to run, modify and propagate that work, +subject to this License. You are not responsible for enforcing compliance by +third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered work results +from an entity transaction, each party to that transaction who receives a copy +of the work also receives whatever licenses to the work the party's predecessor +in interest had or could give under the previous paragraph, plus a right to +possession of the Corresponding Source of the work from the predecessor in +interest, if the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights +granted or affirmed under this License. For example, you may not impose a +license fee, royalty, or other charge for exercise of rights granted under this +License, and you may not initiate litigation (including a cross-claim or +counterclaim in a lawsuit) alleging that any patent claim is infringed by +making, using, selling, offering for sale, or importing the Program or any +portion of it. + +### 11. Patents + +A "contributor" is a copyright holder who authorizes use under this License of +the Program or a work on which the Program is based. The work thus licensed is +called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned or +controlled by the contributor, whether already acquired or hereafter acquired, +that would be infringed by some manner, permitted by this License, of making, +using, or selling its contributor version, but do not include claims that would +be infringed only as a consequence of further modification of the contributor +version. For purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent +license under the contributor's essential patent claims, to make, use, sell, +offer for sale, import and otherwise run, modify and propagate the contents of +its contributor version. + +In the following three paragraphs, a "patent license" is any express agreement +or commitment, however denominated, not to enforce a patent (such as an express +permission to practice a patent or covenant not to sue for patent infringement). +To "grant" such a patent license to a party means to make such an agreement or +commitment not to enforce a patent against the party. + +If you convey a covered work, knowingly relying on a patent license, and the +Corresponding Source of the work is not available for anyone to copy, free of +charge and under the terms of this License, through a publicly available network +server or other readily accessible means, then you must either **(1)** cause the +Corresponding Source to be so available, or **(2)** arrange to deprive yourself +of the benefit of the patent license for this particular work, or **(3)** +arrange, in a manner consistent with the requirements of this License, to extend +the patent license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the covered +work in a country, or your recipient's use of the covered work in a country, +would infringe one or more identifiable patents in that country that you have +reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, you +convey, or propagate by procuring conveyance of, a covered work, and grant a +patent license to some of the parties receiving the covered work authorizing +them to use, propagate, modify or convey a specific copy of the covered work, +then the patent license you grant is automatically extended to all recipients of +the covered work and works based on it. + +A patent license is "discriminatory" if it does not include within the scope of +its coverage, prohibits the exercise of, or is conditioned on the non-exercise +of one or more of the rights that are specifically granted under this License. +You may not convey a covered work if you are a party to an arrangement with a +third party that is in the business of distributing software, under which you +make payment to the third party based on the extent of your activity of +conveying the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory patent +license **(a)** in connection with copies of the covered work conveyed by you +(or copies made from those copies), or **(b)** primarily for and in connection +with specific products or compilations that contain the covered work, unless you +entered into that arrangement, or that patent license was granted, prior to 28 +March 2007. + +Nothing in this License shall be construed as excluding or limiting any implied +license or other defenses to infringement that may otherwise be available to you +under applicable patent law. + +### 12. No Surrender of Others' Freedom + +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not excuse +you from the conditions of this License. If you cannot convey a covered work so +as to satisfy simultaneously your obligations under this License and any other +pertinent obligations, then as a consequence you may not convey it at all. For +example, if you agree to terms that obligate you to collect a royalty for +further conveying from those to whom you convey the Program, the only way you +could satisfy both those terms and this License would be to refrain entirely +from conveying the Program. + +### 13. Use with the GNU Affero General Public License + +Notwithstanding any other provision of this License, you have permission to link +or combine any covered work with a work licensed under version 3 of the GNU +Affero General Public License into a single combined work, and to convey the +resulting work. The terms of this License will continue to apply to the part +which is the covered work, but the special requirements of the GNU Affero +General Public License, section 13, concerning interaction through a network +will apply to the combination as such. + +### 14. Revised Versions of this License + +The Free Software Foundation may publish revised and/or new versions of the GNU +General Public License from time to time. Such new versions will be similar in +spirit to the present version, but may differ in detail to address new problems +or concerns. + +Each version is given a distinguishing version number. If the Program specifies +that a certain numbered version of the GNU General Public License "or any later +version" applies to it, you have the option of following the terms and +conditions either of that numbered version or of any later version published by +the Free Software Foundation. If the Program does not specify a version number +of the GNU General Public License, you may choose any version ever published by +the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the +GNU General Public License can be used, that proxy's public statement of +acceptance of a version permanently authorizes you to choose that version for +the Program. + +Later license versions may give you additional or different permissions. +However, no additional obligations are imposed on any author or copyright holder +as a result of your choosing to follow a later version. + +### 15. Disclaimer of Warranty + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER +PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER +EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE +QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE +DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + +### 16. Limitation of Liability + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY +COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS +PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, +INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE +THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED +INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE +PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY +HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +### 17. Interpretation of Sections 15 and 16 + +If the disclaimer of warranty and limitation of liability provided above cannot +be given local legal effect according to their terms, reviewing courts shall +apply local law that most closely approximates an absolute waiver of all civil +liability in connection with the Program, unless a warranty or assumption of +liability accompanies a copy of the Program in return for a fee. + +_END OF TERMS AND CONDITIONS_ + +## How to Apply These Terms to Your New Programs + +If you develop a new program, and you want it to be of the greatest possible use +to the public, the best way to achieve this is to make it free software which +everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach +them to the start of each source file to most effectively state the exclusion of +warranty; and each file should have at least the "copyright" line and a pointer +to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + +If the program does terminal interaction, make it output a short notice like +this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type 'show c' for details. + +The hypothetical commands `show w` and `show c` should show the appropriate +parts of the General Public License. Of course, your program's commands might be +different; for a GUI interface, you would use an "about box". + +You should also get your employer (if you work as a programmer) or school, if +any, to sign a "copyright disclaimer" for the program, if necessary. For more +information on this, and how to apply and follow the GNU GPL, see +<>. + +The GNU General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may consider +it more useful to permit linking proprietary applications with the library. If +this is what you want to do, use the GNU Lesser General Public License instead +of this License. But first, please read +<>. diff --git a/spank/mqt-core-qdmi-spank.map b/spank/mqt-core-qdmi-spank.map new file mode 100644 index 0000000000..b65e7bfe2d --- /dev/null +++ b/spank/mqt-core-qdmi-spank.map @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH + * All rights reserved. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +{ + global: + plugin_name; + plugin_type; + plugin_version; + spank_plugin_version; + slurm_spank_init_failure_mode; + slurm_spank_init; + slurm_spank_user_init; + slurm_spank_task_init; + local: + *; +}; diff --git a/spank/spank.cpp b/spank/spank.cpp new file mode 100644 index 0000000000..e24dd121f1 --- /dev/null +++ b/spank/spank.cpp @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH + * All rights reserved. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +/// @file spank.cpp +/// @brief Transport administrator-declared configuration references to QDMI +/// jobs. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +#include +#include +#include +} + +static_assert(SLURM_VERSION_NUMBER >= SLURM_VERSION_NUM(25, 11, 0), + "The MQT Core SPANK component requires Slurm 25.11 or newer"); + +// Names and signatures in this block are fixed by Slurm's plugin ABI. +// NOLINTBEGIN(readability-identifier-naming) +extern "C" { +extern const char plugin_name[] = "mqt_core_qdmi"; +extern const char plugin_type[] = "spank"; +extern const unsigned int plugin_version = SLURM_VERSION_NUMBER; +extern const unsigned int spank_plugin_version = 1; +int slurm_spank_init_failure_mode = + ESPANK_JOB_FAILURE; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +} +// NOLINTEND(readability-identifier-naming) + +namespace { +constexpr auto LICENSE_ENVIRONMENT = "SLURM_JOB_LICENSES"; +constexpr auto CATALOGUE_ENVIRONMENT = "MQT_CORE_QDMI_CONFIG_FILE"; +constexpr size_t MAX_VALUE_SIZE = 4095; +constexpr int TASK_REJECTED = -1; + +void fail(const char* message) { + // Slurm exposes logging through a variadic C ABI. + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + slurm_spank_log("mqt-core-qdmi: %s", message); +} + +bool validValue(const std::string_view value) { + return !value.empty() && value.size() <= MAX_VALUE_SIZE && + value.find_first_of("\r\n") == std::string_view::npos; +} + +auto jobEnvironment(spank_t spank, const char* name) + -> std::optional { + // S_JOB_ENV requires a mutable char*** output parameter. + char** values = nullptr; // NOLINT(misc-const-correctness) + // Slurm exposes item lookup through a variadic C ABI. + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + if (spank_get_item(spank, S_JOB_ENV, &values) != ESPANK_SUCCESS || + values == nullptr) { + throw std::runtime_error("could not read the job environment"); + } + const auto prefix = std::string{name} + '='; + // S_JOB_ENV is a borrowed, null-terminated array owned by Slurm. + // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) + for (size_t index = 0; values[index] != nullptr; ++index) { + const std::string_view entry{values[index]}; + if (entry.starts_with(prefix)) { + return std::string{entry.substr(prefix.size())}; + } + } + // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) + return std::nullopt; +} + +auto licenseIds(std::string_view value) -> std::vector { + std::vector ids; + do { + const auto separator = value.find(','); + const auto id = value.substr(0, separator); + if (!validValue(id) || + id.find_first_of(" \t:|@") != std::string_view::npos || + std::ranges::find(ids, id) != ids.end()) { + throw std::runtime_error("invalid or duplicate configured license ID"); + } + ids.emplace_back(id); + if (separator == std::string_view::npos) { + return ids; + } + value.remove_prefix(separator + 1); + } while (true); +} + +bool validEnvironmentName(const std::string_view name) { + constexpr std::string_view letters = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; + constexpr std::string_view characters = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"; + return !name.empty() && + letters.find(name.front()) != std::string_view::npos && + name.find_first_not_of(characters) == std::string_view::npos; +} + +struct Reference { + std::string environment; + std::vector licenses; + std::string defaultValue; +}; + +class Configuration final { +public: + void parse(const int count, char** arguments) { + for (const auto* rawArgument : + std::span{arguments, static_cast(count)}) { + if (rawArgument == nullptr || !validValue(rawArgument)) { + throw std::runtime_error("malformed plugstack argument"); + } + const std::string_view argument{rawArgument}; + const auto separator = argument.find('='); + if (separator == std::string_view::npos) { + throw std::runtime_error("plugstack arguments must use key=value"); + } + const auto key = argument.substr(0, separator); + const auto value = argument.substr(separator + 1); + if (key == "licenses" && licenses_.empty()) { + licenses_ = licenseIds(value); + } else if (key == "qdmi_config_file" && !catalogueDefault_) { + if (!validValue(value)) { + throw std::runtime_error("invalid catalogue reference"); + } + catalogueDefault_ = value; + } else if (key == "reference") { + const auto nameEnd = value.find(':'); + const auto idsEnd = + value.find(':', nameEnd == std::string_view::npos ? value.size() + : nameEnd + 1); + if (nameEnd == std::string_view::npos || + idsEnd == std::string_view::npos) { + throw std::runtime_error("reference must use ENV:LICENSES:DEFAULT"); + } + const auto name = value.substr(0, nameEnd); + if (!validEnvironmentName(name) || name == CATALOGUE_ENVIRONMENT || + name == LICENSE_ENVIRONMENT || + std::ranges::any_of(references_, [&](const auto& reference) { + return reference.environment == name; + })) { + throw std::runtime_error("invalid or duplicate reference name"); + } + const auto defaultValue = value.substr(idsEnd + 1); + if (!validValue(defaultValue)) { + throw std::runtime_error("invalid default reference value"); + } + references_.push_back({ + .environment = std::string{name}, + .licenses = + licenseIds(value.substr(nameEnd + 1, idsEnd - nameEnd - 1)), + .defaultValue = std::string{defaultValue}, + }); + } else { + throw std::runtime_error("unknown or repeated plugstack argument"); + } + } + if (licenses_.empty()) { + throw std::runtime_error("configure concrete license IDs with licenses="); + } + for (const auto& reference : references_) { + for (const auto& id : reference.licenses) { + if (std::ranges::find(licenses_, id) == licenses_.end()) { + throw std::runtime_error("reference names an unconfigured license"); + } + } + } + } + + void inject(spank_t spank) const { + const auto expression = jobEnvironment(spank, LICENSE_ENVIRONMENT); + const auto selected = std::ranges::find_if(licenses_, [&](const auto& id) { + return expression && (*expression == id || *expression == id + ":1"); + }); + if (selected == licenses_.end()) { + return; + } + + if (catalogueDefault_) { + apply(spank, CATALOGUE_ENVIRONMENT, *catalogueDefault_); + } + for (const auto& reference : references_) { + if (std::ranges::find(reference.licenses, *selected) != + reference.licenses.end()) { + apply(spank, reference.environment.c_str(), reference.defaultValue); + } + } + } + +private: + static void apply(spank_t spank, const char* environment, + const std::string& defaultValue) { + const auto value = jobEnvironment(spank, environment); + if (value && !validValue(*value)) { + throw std::runtime_error( + "job environment value is malformed or too long"); + } + if (value) { + return; + } + const auto result = + spank_setenv(spank, environment, defaultValue.c_str(), 0); + if (result != ESPANK_SUCCESS && result != ESPANK_ENV_EXISTS) { + throw std::runtime_error("failed to set a default QDMI reference"); + } + } + + std::vector licenses_; + std::vector references_; + std::optional catalogueDefault_; +}; + +Configuration + configuration; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +bool licenseEnvironmentReady = true; + +} // namespace + +// Names and signatures are fixed by Slurm's plugin ABI. +// NOLINTBEGIN(misc-use-internal-linkage, readability-identifier-naming, +// cppcoreguidelines-avoid-c-arrays) +extern "C" { +int slurm_spank_init(spank_t /*spank*/, const int count, char* arguments[]) { + try { + configuration = Configuration{}; + licenseEnvironmentReady = true; + configuration.parse(count, arguments); + return ESPANK_SUCCESS; + } catch (const std::exception& error) { + fail(error.what()); + } catch (...) { + fail("QDMI SPANK initialization failed"); + } + return TASK_REJECTED; +} + +int slurm_spank_user_init(spank_t spank, int /*count*/, char* /*arguments*/[]) { + if (spank_remote(spank) == 1) { + // Slurm restores allocated licenses before task-init. A submitted value + // must not make a job with no allocated license match a configured device. + const auto result = spank_unsetenv(spank, LICENSE_ENVIRONMENT); + if (result != ESPANK_SUCCESS && result != ESPANK_ENV_NOEXIST) { + // Fail in task-init: a user-init error can drain the node. + licenseEnvironmentReady = false; + fail("could not clear inherited license selection"); + } + } + return ESPANK_SUCCESS; +} + +int slurm_spank_task_init(spank_t spank, int /*count*/, char* /*arguments*/[]) { + if (spank_remote(spank) != 1) { + return ESPANK_SUCCESS; + } + try { + if (!licenseEnvironmentReady) { + throw std::runtime_error("could not clear inherited license selection"); + } + configuration.inject(spank); + return ESPANK_SUCCESS; + } catch (const std::exception& error) { + fail(error.what()); + } catch (...) { + fail("QDMI configuration injection failed"); + } + return TASK_REJECTED; +} +} +// NOLINTEND(misc-use-internal-linkage, readability-identifier-naming, +// cppcoreguidelines-avoid-c-arrays) diff --git a/src/qdmi/CMakeLists.txt b/src/qdmi/CMakeLists.txt index aacc1735b4..995976d166 100644 --- a/src/qdmi/CMakeLists.txt +++ b/src/qdmi/CMakeLists.txt @@ -40,6 +40,17 @@ if(NOT TARGET ${TARGET_NAME}) list(APPEND MQT_CORE_TARGETS ${TARGET_NAME}) endif() +if(UNIX) + add_executable(mqt-core-qdmi-check Check.cpp) + target_link_libraries(mqt-core-qdmi-check PRIVATE MQT::CoreQDMI MQT::ProjectWarnings + MQT::ProjectOptions) + mqt_copy_qdmi_runtime(mqt-core-qdmi-check) + if(MQT_CORE_INSTALL) + install(TARGETS mqt-core-qdmi-check RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + COMPONENT ${MQT_CORE_TARGET_NAME}_Runtime) + endif() +endif() + set(MQT_CORE_TARGETS ${MQT_CORE_TARGETS} PARENT_SCOPE) diff --git a/src/qdmi/Check.cpp b/src/qdmi/Check.cpp new file mode 100644 index 0000000000..d8238e0791 --- /dev/null +++ b/src/qdmi/Check.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM + * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH + * All rights reserved. + * + * SPDX-License-Identifier: MIT + * + * Licensed under the MIT License + */ + +#include "qdmi/QDMI.hpp" + +#include "qdmi/constants.h" + +#include +// POSIX signal handling is not part of the C++ interface. +#include // NOLINT(modernize-deprecated-headers) +#include +#include +#include +#include + +#ifdef __linux__ +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +// A signal handler may only communicate through sig_atomic_t. +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +volatile sig_atomic_t interrupted = 0; + +extern "C" void handleSignal(const int signal) { interrupted = signal; } + +constexpr std::string_view USAGE = + "Usage: mqt-core-qdmi-check --device ID [--timeout SECONDS]\n" + "Probe whether a registered QDMI device is operational (default timeout: " + "30 seconds).\n" + "Exit codes: 0 available, 1 failed, 2 invalid arguments, 124 timeout.\n"; + +[[nodiscard]] auto check(const std::string& id) -> int { + try { + const auto device = qdmi::Session::openDevice(id); + const auto status = device.getStatus(); + return status == QDMI_DEVICE_STATUS_IDLE || + status == QDMI_DEVICE_STATUS_BUSY + ? 0 + : 1; + } catch (...) { + return 1; + } +} + +[[nodiscard]] auto supervise(const std::string& id, + const std::chrono::seconds timeout) -> int { + struct sigaction action{}; + action.sa_handler = handleSignal; + sigemptyset(&action.sa_mask); + if (sigaction(SIGTERM, &action, nullptr) != 0 || + sigaction(SIGINT, &action, nullptr) != 0) { + return 1; + } + + const auto deadline = std::chrono::steady_clock::now() + timeout; +#ifdef __linux__ + const auto supervisor = getpid(); +#endif + const auto child = fork(); + if (child < 0) { + return 1; + } + if (child == 0) { +#ifdef __linux__ + // Stop the worker if its supervisor is killed. + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0 || getppid() != supervisor) { + _exit(1); + } +#endif + action.sa_handler = SIG_DFL; + const rlimit coreLimit{.rlim_cur = 0, .rlim_max = 0}; + if (setpgid(0, 0) != 0 || sigaction(SIGTERM, &action, nullptr) != 0 || + sigaction(SIGINT, &action, nullptr) != 0 || + setrlimit(RLIMIT_CORE, &coreLimit) != 0) { + _exit(1); + } + // Device diagnostics can contain credentials, URLs, or configuration. + // POSIX open takes no mode argument when it does not create a file. + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + const auto sink = open("/dev/null", O_RDWR); + if (sink < 0 || dup2(sink, STDIN_FILENO) < 0 || + dup2(sink, STDOUT_FILENO) < 0 || dup2(sink, STDERR_FILENO) < 0) { + _exit(1); + } + if (sink > STDERR_FILENO) { + close(sink); + } + // Run process-exit handlers within the worker deadline. + std::exit(check(id)); // NOLINT(concurrency-mt-unsafe) + } + + // Either side can establish the group before the child loads a device. + static_cast(setpgid(child, child)); + int result = 1; + while (interrupted == 0) { + // clang-tidy 23 does not map glibc's wait definitions to . + // NOLINTBEGIN(misc-include-cleaner) + siginfo_t status{}; + const auto waited = waitid(P_PID, static_cast(child), &status, + WEXITED | WNOHANG | WNOWAIT); + if (waited == 0 && status.si_pid == child) { + result = status.si_code == CLD_EXITED && status.si_status == 0 ? 0 : 1; + // NOLINTEND(misc-include-cleaner) + break; + } + if (waited < 0 && errno != EINTR) { + break; + } + if (std::chrono::steady_clock::now() >= deadline) { + result = 124; + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + // Keep the worker unreaped until group cleanup so its PID cannot be reused. + static_cast(kill(-child, SIGKILL)); + static_cast(kill(child, SIGKILL)); + while (waitpid(child, nullptr, 0) < 0 && errno == EINTR) { + } + return result; +} + +} // namespace + +int main(const int argc, char** argv) try { + const auto arguments = std::span(argv, static_cast(argc)); + if (argc == 2 && std::string_view(arguments[1]) == "--help") { + std::cout << USAGE; + return 0; + } + std::string id; + int seconds = 30; + bool hasTimeout = false; + for (size_t i = 1; i < arguments.size(); ++i) { + const std::string_view option(arguments[i]); + if (++i == arguments.size()) { + std::cerr << USAGE; + return 2; + } + const std::string_view value(arguments[i]); + if (option == "--device" && id.empty() && !value.empty()) { + id = value; + } else if (option == "--timeout" && !hasTimeout) { + // from_chars requires a pointer range within the string view. + // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) + const auto parsed = + std::from_chars(value.data(), value.data() + value.size(), seconds); + if (parsed.ec != std::errc{} || + parsed.ptr != value.data() + value.size() || seconds < 1 || + seconds > 3600) { + std::cerr << USAGE; + return 2; + } + // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) + hasTimeout = true; + } else { + std::cerr << USAGE; + return 2; + } + } + if (id.empty()) { + std::cerr << USAGE; + return 2; + } + const auto result = supervise(id, std::chrono::seconds(seconds)); + if (result == 124) { + std::cerr << "QDMI device check timed out\n"; + } else if (result != 0) { + std::cerr << "QDMI device check failed\n"; + } + return result; +} catch (...) { + std::cerr << "QDMI device check failed\n"; + return 1; +} diff --git a/test/python/test_cli.py b/test/python/test_cli.py index 3a1954803f..1804a507f1 100644 --- a/test/python/test_cli.py +++ b/test/python/test_cli.py @@ -20,6 +20,7 @@ import pytest from mqt.core import __version__ as mqt_core_version +from mqt.core import _commands # ruff: ignore[import-private-name] if TYPE_CHECKING: from pytest_console_scripts import ScriptRunner @@ -138,7 +139,14 @@ def test_benchmark_cli(script_runner: ScriptRunner) -> None: assert '"teleportation"' in ret.stdout -@pytest.mark.parametrize("tool", ["mqt-cc", "mqt-core-bench"]) +@pytest.mark.parametrize( + "tool", + [ + "mqt-cc", + "mqt-core-bench", + pytest.param("mqt-core-qdmi-check", marks=pytest.mark.skipif(sys.platform == "win32", reason="POSIX checker")), + ], +) def test_native_tool_entry_point(script_runner: ScriptRunner, tool: str) -> None: """Resolve the console entry point and forward arguments to its native tool.""" with patch("os.execv") as execute: @@ -147,3 +155,18 @@ def test_native_tool_entry_point(script_runner: ScriptRunner, tool: str) -> None executable, arguments = execute.call_args.args assert Path(executable).is_file() assert arguments == [str(executable), "--help"] + + +@pytest.mark.script_launch_mode("subprocess") +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX checker") +def test_qdmi_availability(script_runner: ScriptRunner) -> None: + """Probe a device through the installed command and catalogue.""" + result = script_runner.run(["mqt-core-qdmi-check", "--device", "mqt.sc.default"]) + assert result.success + assert not result.stdout + + +def test_qdmi_availability_windows() -> None: + """Explain the availability command's platform requirement.""" + with patch.object(_commands.sys, "platform", "win32"), pytest.raises(SystemExit, match="Linux or macOS"): + _commands.qdmi_check() diff --git a/test/python/test_slurm_integration.py b/test/python/test_slurm_integration.py index 4af0967d6c..96c6a8c3c0 100644 --- a/test/python/test_slurm_integration.py +++ b/test/python/test_slurm_integration.py @@ -44,6 +44,25 @@ def load_runner() -> ModuleType: runner = load_runner() +def test_runner_accepts_scaled_nodes() -> None: + """Run the same cluster with more compute nodes without a new Compose file.""" + assert runner.parse_arguments(("--nodes", "3")).nodes == 3 + + +def test_prepare_keeps_private_key_and_preserves_existing_cluster(tmp_path: Path) -> None: + """Create usable shared files without overwriting a cluster's authentication.""" + runtime = tmp_path / "cluster" + command = ("sh", str(runner.CLUSTER / "prepare.sh"), str(runtime)) + runner.run(command) + key = runtime / "munge.key" + assert len(key.read_bytes()) == 1024 + assert key.stat().st_mode & 0o777 == 0o600 + assert (runtime / "slurm.conf").stat().st_mode & 0o777 == 0o644 + original = key.read_bytes() + assert runner.run(command, check=False).returncode != 0 + assert key.read_bytes() == original + + def test_timeout_kills_children_holding_output_pipes(tmp_path: Path) -> None: """A Compose-like grandchild must not defeat the command deadline.""" marker = tmp_path / "started" @@ -111,7 +130,7 @@ def test_preflight_does_not_touch_runtime(tmp_path: Path, monkeypatch: pytest.Mo def test_diagnostic_failure_still_tears_down(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """An unavailable diagnostic service must not orphan a failed cluster.""" - (tmp_path / "core.whl").touch() + (tmp_path / "mqt_core-0.whl").touch() monkeypatch.setattr(runner, "DIST", tmp_path) monkeypatch.setattr(runner, "RUNTIME", tmp_path / "runtime") monkeypatch.setattr(runner, "run", lambda *args: subprocess.CompletedProcess(args, 0, "2", "")) @@ -150,3 +169,54 @@ def run(command: tuple[str, ...], **kwargs: object) -> subprocess.CompletedProce assert first.RUNTIME != second.RUNTIME assert calls[0][0] != calls[1][0] assert calls[0][1] != calls[1][1] + + +def test_provider_options_preserve_command_arguments(tmp_path: Path) -> None: + """Keep workload arguments separate from fixture and reference options.""" + setup = tmp_path / "setup.sh" + setup.touch() + options = runner.parse_arguments(( + "--workload", + str(tmp_path), + "--setup-script", + "setup.sh", + "--device-license", + "provider.device", + "--reference", + "PROVIDER_PROFILE=profile=value", + "--", + "python3", + "probe.py", + "--label", + "one argument", + )) + assert options.workload == tmp_path + assert options.reference == ["PROVIDER_PROFILE=profile=value"] + assert options.command == ["python3", "probe.py", "--label", "one argument"] + + +@pytest.mark.parametrize( + "arguments", + [ + ("--device-license", "provider.device"), + ("--", "python3", "probe.py"), + ("--device-license", "provider.device:2", "--", "/bin/true"), + ("--reference", "PROVIDER_PROFILE"), + ("--reference", "PROVIDER_PROFILE=two words"), + ], +) +def test_invalid_provider_inputs_fail_before_docker(arguments: tuple[str, ...]) -> None: + """Reject incomplete or unrepresentable fixture inputs at the CLI.""" + with pytest.raises(SystemExit) as error: + runner.parse_arguments(arguments) + assert error.value.code == 2 + + +def test_setup_script_must_stay_inside_workload(tmp_path: Path) -> None: + """Do not accept a setup script that the workload build context cannot supply.""" + workload = tmp_path / "workload" + workload.mkdir() + (tmp_path / "outside.sh").touch() + with pytest.raises(SystemExit) as error: + runner.parse_arguments(("--workload", str(workload), "--setup-script", "../outside.sh")) + assert error.value.code == 2 diff --git a/test/qdmi/CMakeLists.txt b/test/qdmi/CMakeLists.txt index cdb72a97b5..1cb6f61286 100644 --- a/test/qdmi/CMakeLists.txt +++ b/test/qdmi/CMakeLists.txt @@ -60,4 +60,13 @@ if(TARGET MQT::CoreQDMI) endif() add_dependencies(${TARGET_NAME} mqt-core-qdmi-session-device) mqt_copy_qdmi_runtime(${TARGET_NAME}) + if(TARGET mqt-core-qdmi-check) + add_test( + NAME mqt-core-qdmi-check-cli + COMMAND + ${CMAKE_COMMAND} "-DCHECKER=$" + "-DSESSION_DEVICE=$" + "-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/checker" -P + "${CMAKE_CURRENT_SOURCE_DIR}/test_check_cli.cmake") + endif() endif() diff --git a/test/qdmi/driver/session_device.cpp b/test/qdmi/driver/session_device.cpp index 131f4f93f8..72fbf833ab 100644 --- a/test/qdmi/driver/session_device.cpp +++ b/test/qdmi/driver/session_device.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -292,6 +294,16 @@ TEST_SESSION_QDMI_device_session_init(QDMI_Device_Session session) { if (session->initialized) { return QDMI_ERROR_BADSTATE; } + if (parameter(session, QDMI_DEVICE_SESSION_PARAMETER_CUSTOM4) == + "hang-exit") { + if (std::atexit( + [] { std::this_thread::sleep_for(std::chrono::hours(1)); }) != 0) { + return QDMI_ERROR_FATAL; + } + } + if (parameter(session, QDMI_DEVICE_SESSION_PARAMETER_CUSTOM4) == "hang") { + std::this_thread::sleep_for(std::chrono::hours(1)); + } session->initialized = true; return QDMI_SUCCESS; } diff --git a/test/qdmi/test_check_cli.cmake b/test/qdmi/test_check_cli.cmake new file mode 100644 index 0000000000..b6207699ff --- /dev/null +++ b/test/qdmi/test_check_cli.cmake @@ -0,0 +1,36 @@ +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +file(MAKE_DIRECTORY "${WORK_DIR}") +set(configuration "${WORK_DIR}/devices.json") +foreach(status idle busy offline hang hang-exit) + file( + WRITE "${configuration}" + "{\"schema-version\":1,\"qdmi\":{\"devices\":[{\"id\":\"test.check\",\"library\":\"${SESSION_DEVICE}\",\"prefix\":\"TEST_SESSION\",\"session\":{\"custom4\":\"${status}\"}}]}}" + ) + execute_process( + COMMAND + "${CMAKE_COMMAND}" -E env --unset=MQT_CORE_QDMI_DRIVER --unset=MQT_CORE_QDMI_CONFIG_JSON + "MQT_CORE_QDMI_CONFIG_FILE=${configuration}" "${CHECKER}" --device test.check --timeout 1 + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + TIMEOUT 5) + if(status MATCHES "^hang") + set(expected 124) + elseif(status STREQUAL "offline") + set(expected 1) + else() + set(expected 0) + endif() + if(NOT result STREQUAL "${expected}" OR NOT output STREQUAL "") + message( + FATAL_ERROR + "Availability check for ${status}: expected ${expected}, got ${result}: ${output}${error}") + endif() +endforeach() diff --git a/test/slurm/.dockerignore b/test/slurm/.dockerignore deleted file mode 100644 index 712f264ddd..0000000000 --- a/test/slurm/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -runtime/ diff --git a/test/slurm/Dockerfile b/test/slurm/Dockerfile deleted file mode 100644 index cf76b1d637..0000000000 --- a/test/slurm/Dockerfile +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM -# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH -# All rights reserved. -# -# SPDX-License-Identifier: MIT -# -# Licensed under the MIT License - -FROM ghcr.io/astral-sh/uv:0.12.1 AS uv - -FROM ubuntu:26.04 AS common - -ENV container=docker - -RUN apt-get update \ - && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ - ca-certificates \ - dbus \ - munge \ - python3 \ - slurm-wlm \ - systemd \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* - -COPY --from=uv /uv /uvx /bin/ -COPY mqt-slurm-prepare /usr/local/sbin/mqt-slurm-prepare -COPY mqt-slurm-prepare.service /etc/systemd/system/mqt-slurm-prepare.service - -RUN --mount=type=bind,source=dist,target=/tmp/dist \ - --mount=type=cache,target=/root/.cache/uv \ - uv pip install --system --break-system-packages --link-mode=copy /tmp/dist/*.whl \ - && chmod 0755 /usr/local/sbin/mqt-slurm-prepare \ - && systemctl enable mqt-slurm-prepare.service munge.service - -STOPSIGNAL SIGRTMIN+3 - -CMD ["/usr/lib/systemd/systemd"] - -FROM common AS controller - -RUN systemctl enable slurmctld.service - -FROM common AS compute - -RUN systemctl enable slurmd.service diff --git a/test/slurm/bell_job.py b/test/slurm/bell_job.py index ca94ed5b61..07d02adacf 100644 --- a/test/slurm/bell_job.py +++ b/test/slurm/bell_job.py @@ -58,7 +58,7 @@ def main() -> None: "node": os.environ["SLURM_JOB_NODELIST"], "shots": SHOTS, } - runtime = Path("/runtime") + runtime = Path("/jobs") result_path = runtime / f"ddsim-{job_id}.json" temporary = result_path.with_suffix(".tmp") temporary.write_text(json.dumps(result, sort_keys=True), encoding="utf-8") diff --git a/test/slurm/compose.yml b/test/slurm/compose.yml index b933903632..e17ff25ec0 100644 --- a/test/slurm/compose.yml +++ b/test/slurm/compose.yml @@ -7,101 +7,6 @@ # Licensed under the MIT License services: - controller: - build: - context: . - target: controller - hostname: controller - privileged: true - cgroup: host - tmpfs: - - /run - - /run/lock + node: volumes: - - /sys/fs/cgroup:/sys/fs/cgroup:rw - - ../..:/workspace:ro - - ${MQT_CORE_SLURM_RUNTIME:-./runtime}:/runtime - - ./slurm.conf:/etc/slurm/slurm.conf:ro - - ./cgroup.conf:/etc/slurm/cgroup.conf:ro - healthcheck: - test: - [ - "CMD", - "systemctl", - "is-active", - "--quiet", - "munge.service", - "slurmctld.service", - ] - interval: 2s - timeout: 2s - retries: 30 - start_period: 5s - - node1: - build: - context: . - target: compute - hostname: node1 - privileged: true - cgroup: host - depends_on: - controller: - condition: service_healthy - tmpfs: - - /run - - /run/lock - volumes: - - /sys/fs/cgroup:/sys/fs/cgroup:rw - - ../..:/workspace:ro - - ${MQT_CORE_SLURM_RUNTIME:-./runtime}:/runtime - - ./slurm.conf:/etc/slurm/slurm.conf:ro - - ./cgroup.conf:/etc/slurm/cgroup.conf:ro - healthcheck: - test: - [ - "CMD", - "systemctl", - "is-active", - "--quiet", - "munge.service", - "slurmd.service", - ] - interval: 2s - timeout: 2s - retries: 30 - start_period: 5s - - node2: - build: - context: . - target: compute - hostname: node2 - privileged: true - cgroup: host - depends_on: - controller: - condition: service_healthy - tmpfs: - - /run - - /run/lock - volumes: - - /sys/fs/cgroup:/sys/fs/cgroup:rw - - ../..:/workspace:ro - - ${MQT_CORE_SLURM_RUNTIME:-./runtime}:/runtime - - ./slurm.conf:/etc/slurm/slurm.conf:ro - - ./cgroup.conf:/etc/slurm/cgroup.conf:ro - healthcheck: - test: - [ - "CMD", - "systemctl", - "is-active", - "--quiet", - "munge.service", - "slurmd.service", - ] - interval: 2s - timeout: 2s - retries: 30 - start_period: 5s + - ../../test/slurm/mqt-slurm-test-environment.conf:/etc/systemd/system/slurmd.service.d/mqt-test.conf:ro diff --git a/test/slurm/mqt-slurm-prepare.service b/test/slurm/mqt-slurm-prepare.service deleted file mode 100644 index ea26c9d04e..0000000000 --- a/test/slurm/mqt-slurm-prepare.service +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM -# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH -# All rights reserved. -# -# SPDX-License-Identifier: MIT -# -# Licensed under the MIT License - -[Unit] -Description=Prepare the MQT Core Slurm test node -RequiresMountsFor=/runtime -Before=munge.service slurmctld.service slurmd.service - -[Service] -Type=oneshot -ExecStart=/usr/local/sbin/mqt-slurm-prepare -RemainAfterExit=yes - -[Install] -WantedBy=multi-user.target diff --git a/test/slurm/mqt-slurm-test-environment.conf b/test/slurm/mqt-slurm-test-environment.conf new file mode 100644 index 0000000000..90497850f6 --- /dev/null +++ b/test/slurm/mqt-slurm-test-environment.conf @@ -0,0 +1,11 @@ +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +[Service] +Environment=MQT_SLURM_TEST_REFERENCE=daemon-only +Environment=MQT_CORE_QDMI_CONFIG_FILE=/daemon-only/qdmi.json diff --git a/test/slurm/provider_probe.py b/test/slurm/provider_probe.py new file mode 100644 index 0000000000..42d7cb59db --- /dev/null +++ b/test/slurm/provider_probe.py @@ -0,0 +1,38 @@ +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +"""Common assertions for provider workloads in the shared Slurm fixture.""" + +from __future__ import annotations + +import json +import os +from pathlib import Path +from typing import TYPE_CHECKING + +from mqt.core.qdmi import slurm + +if TYPE_CHECKING: + from mqt.core.qdmi import Device + + +def open_device_from_license() -> Device: + """Open the licensed device and require the catalogue's native library.""" + device = slurm.open_device_from_license() + catalogue = Path(os.environ["MQT_CORE_QDMI_CONFIG_FILE"]) + definitions = json.loads(catalogue.read_text(encoding="utf-8"))["qdmi"]["devices"] + device_id = os.environ["SLURM_JOB_LICENSES"].split(":", maxsplit=1)[0] + definition = next(entry for entry in definitions if entry["id"] == device_id) + library = (catalogue.parent / definition["library"]).resolve() + native_prefix = Path("/opt/provider-native") + if not library.is_relative_to(native_prefix): + assert not native_prefix.exists() + mappings = (line.split(maxsplit=5) for line in Path("/proc/self/maps").read_text(encoding="utf-8").splitlines()) + loaded = {Path(fields[5]).resolve() for fields in mappings if len(fields) == 6 and fields[5].startswith("/")} + assert {path for path in loaded if path.name == library.name} == {library} + return device diff --git a/test/slurm/run_integration.py b/test/slurm/run_integration.py index e936b7b7d5..4905b14c82 100644 --- a/test/slurm/run_integration.py +++ b/test/slurm/run_integration.py @@ -10,16 +10,17 @@ from __future__ import annotations +import argparse import contextlib import json import logging import os import re -import secrets import shlex import shutil import signal import subprocess +import sys import time import uuid from pathlib import Path @@ -30,20 +31,26 @@ ROOT = Path(__file__).resolve().parents[2] FIXTURE = ROOT / "test" / "slurm" -DIST = FIXTURE / "dist" -RUNTIME = FIXTURE / "runtime" / uuid.uuid4().hex +CLUSTER = ROOT / "docker" / "slurm" +DIST = ROOT / "dist" +RUNTIME = ROOT / "build" / "slurm-tests" / uuid.uuid4().hex +NODES: list[str] = [] COMPOSE = ( "docker", "compose", "--project-name", f"mqt-core-slurm-{RUNTIME.name}", "--file", + str(CLUSTER / "compose.yml"), + "--file", str(FIXTURE / "compose.yml"), ) TIMEOUT = 120.0 COMMAND_TIMEOUT = 30.0 RESULT_VISIBILITY_GRACE_PERIOD = 5.0 LOGGER = logging.getLogger(__name__) +COMPOSE_FILES: list[str] = [] +COMPOSE_ENV: dict[str, str] = {} def _communicate(process: subprocess.Popen[str], timeout: float) -> tuple[str, str]: @@ -107,11 +114,11 @@ def compose( ) -> subprocess.CompletedProcess[str]: """Run Docker Compose for this invocation's isolated project and artifacts.""" return run( - (*COMPOSE, *arguments), + (*COMPOSE, *COMPOSE_FILES, *arguments), check=check, timeout=timeout, capture_output=capture_output, - env={**os.environ, "MQT_CORE_SLURM_RUNTIME": str(RUNTIME)}, + env={**os.environ, **COMPOSE_ENV, "MQT_CORE_SLURM_RUNTIME": str(RUNTIME)}, ) @@ -122,7 +129,23 @@ def controller(*command: str, check: bool = True, timeout: float = COMMAND_TIMEO def compute(node: str, *command: str, check: bool = True) -> subprocess.CompletedProcess[str]: """Run a diagnostic command in one compute container.""" - return compose("exec", "-T", node, *command, check=check) + return compose("exec", "-T", "--index", str(NODES.index(node) + 1), "node", *command, check=check) + + +def job(*command: str, check: bool = True, timeout: float = COMMAND_TIMEOUT) -> subprocess.CompletedProcess[str]: + """Submit workloads as the same unprivileged user on every node.""" + return compose( + "exec", + "-T", + "--user", + "10000:10000", + "controller", + "env", + "PYTHONPATH=/workspace/test/slurm", + *command, + check=check, + timeout=timeout, + ) def wait_for(description: str, predicate: Callable[[], bool], timeout: float = TIMEOUT) -> None: @@ -195,14 +218,14 @@ def submit(script: str, license_expression: str, *, node: str | None = None, hol "--time=5", f"--licenses={license_expression}", "--chdir=/workspace", - "--output=/runtime/slurm-%j.out", + "--output=/jobs/slurm-%j.out", ] if node is not None: command.append(f"--nodelist={node}") command.append(f"/workspace/test/slurm/{script}") if hold: command.append("--hold") - job_id = controller(*command).stdout.strip().split(";", maxsplit=1)[0] + job_id = job(*command).stdout.strip().split(";", maxsplit=1)[0] if not job_id.isdecimal(): msg = f"sbatch returned an invalid job ID: {job_id!r}" raise RuntimeError(msg) @@ -226,12 +249,12 @@ def assert_license(name: str, *, total: int, used: int, free: int) -> None: def load_result(kind: str, job_id: str) -> dict[str, Any]: """Load one batch-job result from the shared runtime directory.""" - return json.loads((RUNTIME / f"{kind}-{job_id}.json").read_text(encoding="utf-8")) + return json.loads((RUNTIME / "jobs" / f"{kind}-{job_id}.json").read_text(encoding="utf-8")) def wait_for_result(kind: str, job_id: str, description: str) -> None: """Wait for a result and allow bounded shared-file visibility delay.""" - result_path = RUNTIME / f"{kind}-{job_id}.json" + result_path = RUNTIME / "jobs" / f"{kind}-{job_id}.json" left_queue_at: float | None = None def result_exists_or_raise() -> bool: @@ -249,7 +272,7 @@ def result_exists_or_raise() -> bool: if now - left_queue_at < RESULT_VISIBILITY_GRACE_PERIOD: return False - output_path = RUNTIME / f"slurm-{job_id}.out" + output_path = RUNTIME / "jobs" / f"slurm-{job_id}.out" output = output_path.read_text(encoding="utf-8") if output_path.exists() else "" msg = f"Slurm job {job_id} exited before producing {result_path.name}:\n{output.rstrip()}" raise RuntimeError(msg) @@ -259,7 +282,7 @@ def result_exists_or_raise() -> bool: def wait_for_failed_adapter(job_id: str, diagnostic: str) -> None: """Require an adapter diagnostic and a failed batch job without a result.""" - output_path = RUNTIME / f"slurm-{job_id}.out" + output_path = RUNTIME / "jobs" / f"slurm-{job_id}.out" def failed_with_diagnostic() -> bool: if not job_finished(job_id, expected_state="FAILED") or not output_path.exists(): @@ -267,7 +290,7 @@ def failed_with_diagnostic() -> bool: return diagnostic in output_path.read_text(encoding="utf-8") wait_for(f"Slurm job {job_id} to fail with {diagnostic!r}", failed_with_diagnostic) - if (RUNTIME / f"ddsim-{job_id}.json").exists(): + if (RUNTIME / "jobs" / f"ddsim-{job_id}.json").exists(): msg = f"Rejected Slurm job {job_id} unexpectedly produced a DDSIM result" raise AssertionError(msg) @@ -291,10 +314,7 @@ def assert_bell_result(job_id: str, expected_node: str | None = None) -> None: def clean_runtime() -> None: """Create private artifacts and a Munge key for this invocation only.""" - RUNTIME.mkdir(mode=0o700, parents=True, exist_ok=False) - key = RUNTIME / "munge.key" - key.write_bytes(secrets.token_bytes(1024)) - key.chmod(0o600) + run(("sh", str(CLUSTER / "prepare.sh"), str(RUNTIME))) def print_diagnostics() -> None: @@ -310,22 +330,20 @@ def print_diagnostics() -> None: ) controller("scontrol", "show", "node", check=False, timeout=5) controller("scontrol", "show", "lic", check=False, timeout=5) - for output in sorted(RUNTIME.glob("slurm-*.out")): + for output in sorted((RUNTIME / "jobs").glob("slurm-*.out")): LOGGER.info("=== %s ===", output.name) try: LOGGER.info("%s", output.read_text(encoding="utf-8").rstrip()) except OSError as error: LOGGER.info("Could not read %s: %s", output, error) - for service, units in ( - ("controller", ("munge.service", "slurmctld.service")), - ("node1", ("munge.service", "slurmd.service")), - ("node2", ("munge.service", "slurmd.service")), - ): - compose("exec", "-T", service, "systemctl", "status", "--no-pager", *units, check=False, timeout=5) + for service, index, units in [ + ("controller", 1, ("munge.service", "slurmctld.service")), + *(("node", index, ("munge.service", "slurmd.service")) for index in range(1, len(NODES) + 1)), + ]: + prefix = ("exec", "-T", "--index", str(index), service) + compose(*prefix, "systemctl", "status", "--no-pager", *units, check=False, timeout=5) compose( - "exec", - "-T", - service, + *prefix, "journalctl", "--no-pager", "--lines=100", @@ -336,12 +354,260 @@ def print_diagnostics() -> None: compose("logs", "--no-color", check=False, timeout=5) -def main() -> None: - """Build the cluster and verify Slurm admission and DDSIM execution.""" - wheels = tuple(DIST.glob("*.whl")) +def test_core() -> None: + """Verify admission and execution with the bundled DDSIM and SC devices.""" + registry_check = ( + "from pathlib import Path; " + "import mqt.core; " + "from mqt.core.qdmi import device_ids; " + "module_path = Path(mqt.core.__file__).resolve(); " + "assert not any(module_path.is_relative_to(root) for root in ('/workspace', '/runtime')), module_path; " + "ids = set(device_ids()); " + "assert 'mqt.ddsim.default' in ids and 'mqt.sc.default' in ids, ids" + ) + controller("python3", "-c", registry_check) + assert_license("mqt.ddsim.default", total=2, used=0, free=2) + assert_license("mqt.sc.default", total=1, used=0, free=1) + + non_unit = submit("ddsim-job.sh", "mqt.ddsim.default:2") + wait_for_failed_adapter(non_unit, "must request exactly one Slurm license") + compound = submit("ddsim-job.sh", "mqt.ddsim.default:1,mqt.sc.default:1") + wait_for_failed_adapter(compound, "uses a compound AND expression") + alternative = submit("ddsim-job.sh", "mqt.ddsim.default:1|mqt.sc.default:1") + wait_for_failed_adapter(alternative, "uses a compound OR expression") + assert_license("mqt.ddsim.default", total=2, used=0, free=2) + assert_license("mqt.sc.default", total=1, used=0, free=1) + + first = submit("ddsim-job.sh", "mqt.ddsim.default:1", node=NODES[0], hold=True) + second = submit("ddsim-job.sh", "mqt.ddsim.default:1", node=NODES[1], hold=True) + wait_for_result("ddsim", first, "the first DDSIM Bell result") + wait_for_result("ddsim", second, "the second DDSIM Bell result") + wait_for("the first DDSIM job to hold on its node", lambda: job_matches(first, "RUNNING", node=NODES[0])) + wait_for("the second DDSIM job to hold on its node", lambda: job_matches(second, "RUNNING", node=NODES[1])) + if "CPUAlloc=1" not in node_record(NODES[0]) or "CPUAlloc=1" not in node_record(NODES[1]): + msg = "Each held DDSIM job must leave one processor free on its compute node" + raise AssertionError(msg) + + third = submit("ddsim-job.sh", "mqt.ddsim.default:1") + wait_for( + "the third DDSIM job to wait for its license", + lambda: job_matches(third, "PENDING", reason="Licenses"), + ) + assert_license("mqt.ddsim.default", total=2, used=2, free=0) + + sc_job = submit("sc-job.sh", "mqt.sc.default:1") + wait_for_result("sc", sc_job, "the SC job to execute on a free CPU") + wait_for("the SC job to complete", lambda: job_finished(sc_job)) + sc_result = load_result("sc", sc_job) + if sc_result["node"] not in NODES or sc_result["qubits"] <= 0: + msg = f"Unexpected SC job result: {sc_result}" + raise AssertionError(msg) + if not job_matches(first, "RUNNING", node=NODES[0]) or not job_matches(second, "RUNNING", node=NODES[1]): + msg = "The SC job did not complete while both DDSIM licenses remained held" + raise AssertionError(msg) + + (RUNTIME / "jobs" / f"release-{first}").touch() + wait_for("the released first DDSIM job to finish", lambda: job_finished(first)) + wait_for_result("ddsim", third, "the pending third DDSIM job to execute") + wait_for("the third DDSIM job to finish", lambda: job_finished(third)) + + assert_bell_result(first, NODES[0]) + assert_bell_result(second, NODES[1]) + assert_bell_result(third) + assert_license("mqt.ddsim.default", total=2, used=1, free=1) + + (RUNTIME / "jobs" / f"release-{second}").touch() + wait_for("the released second DDSIM job to finish", lambda: job_finished(second)) + assert_license("mqt.ddsim.default", total=2, used=0, free=2) + + LOGGER.info( + "Slurm 25.11+ admitted two held DDSIM jobs, blocked the third for Licenses, " + "ran the SC job on a free CPU, and executed the third Bell job after release." + ) + + +def parse_arguments(arguments: Sequence[str]) -> argparse.Namespace: + """Read the provider build inputs and the command to run in an allocation.""" + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--workload", type=Path, default=ROOT) + parser.add_argument("--dist", type=Path, default=DIST) + parser.add_argument("--nodes", type=int, default=2) + parser.add_argument("--setup-script", default="") + parser.add_argument("--compose-file", type=Path) + parser.add_argument("--device-license") + parser.add_argument("--qdmi-config-file") + parser.add_argument("--reference", action="append", default=[]) + parser.add_argument("command", nargs=argparse.REMAINDER) + options = parser.parse_args(arguments) + if options.nodes < 2: + parser.error("--nodes must be at least two for the admission tests") + if options.command[:1] == ["--"]: + options.command = options.command[1:] + if bool(options.command) != bool(options.device_license): + parser.error("--device-license and a command after -- must be supplied together") + if options.device_license and re.search(r"[\s,:|@]", options.device_license): + parser.error("--device-license must be one local device ID without a count") + if options.setup_script: + setup = (options.workload / options.setup_script).resolve() + if not setup.is_relative_to(options.workload.resolve()) or not setup.is_file(): + parser.error("--setup-script must name a file inside --workload") + for reference in options.reference: + name, separator, value = reference.partition("=") + if not separator or re.fullmatch(r"[A-Za-z_][A-Za-z_0-9]*", name) is None or any(c.isspace() for c in value): + parser.error("--reference must use ENV=value without whitespace") + if options.qdmi_config_file and any(c.isspace() for c in options.qdmi_config_file): + parser.error("--qdmi-config-file must not contain whitespace") + return options + + +def test_provider(options: argparse.Namespace) -> None: + """Run a provider workload with job configuration and site defaults.""" + environment = list(options.reference) + if options.qdmi_config_file: + environment.append(f"MQT_CORE_QDMI_CONFIG_FILE={options.qdmi_config_file}") + allocation = ("srun", "--immediate=5", "--time=5", "--ntasks=1", f"--licenses={options.device_license}:1") + job("env", *environment, *allocation, *options.command, timeout=300) + + configuration = [ + "required /usr/local/lib/slurm/mqt-core-qdmi-spank.so", + f"licenses={options.device_license}", + ] + if options.qdmi_config_file: + configuration.append(f"qdmi_config_file={options.qdmi_config_file}") + for reference in options.reference: + name, _, value = reference.partition("=") + configuration.append(f"reference={name}:{options.device_license}:{value}") + (RUNTIME / "plugstack.conf").write_text(" ".join(configuration) + "\n", encoding="utf-8") + job(*allocation, *options.command, timeout=300) + (RUNTIME / "plugstack.conf").write_text("", encoding="utf-8") + + +def test_spank_transport() -> None: + """Check license-aware defaults and native overrides through srun and sbatch.""" + selected = "mqt.ddsim.default" + other = "mqt.sc.default" + reference = "MQT_SLURM_TEST_REFERENCE" + catalogue = "/runtime/site.qdmi.json" + (RUNTIME / "plugstack.conf").write_text( + "required /usr/local/lib/slurm/mqt-core-qdmi-spank.so " + f"licenses={selected},{other} qdmi_config_file={catalogue} " + f"reference={reference}:{selected}:site-default\n", + encoding="utf-8", + ) + program = ( + "import json, os; assert os.geteuid() == 10000, os.geteuid(); " + f"print(json.dumps([os.environ.get('{reference}'), os.environ.get('MQT_CORE_QDMI_CONFIG_FILE')]), flush=True)" + ) + allocation = ("srun", "--immediate=5", "--time=1", "--ntasks=1") + + def values(*arguments: str) -> list[str | None]: + return json.loads(job(*arguments, "python3", "-c", program, timeout=60).stdout) + + assert values(*allocation, f"--licenses={selected}") == ["site-default", catalogue] + assert values(*allocation, f"--licenses={selected}:1") == ["site-default", catalogue] + assert values(*allocation, f"--licenses={selected}:2") == [None, None] + assert values(*allocation, f"--licenses={selected},{other}") == [None, None] + assert values("env", f"{reference}=job-value", *allocation, f"--licenses={selected}") == ["job-value", catalogue] + assert values("env", "MQT_CORE_QDMI_CONFIG_FILE=/runtime/job.qdmi.json", *allocation, f"--licenses={selected}") == [ + "site-default", + "/runtime/job.qdmi.json", + ] + assert values(*allocation, f"--licenses={other}") == [None, catalogue] + assert values(*allocation) == [None, None] + + # Slurm allocation metadata is not subject to the QDMI reference size limit. + unrelated = ",".join(f"unrelated-{index:03d}-{'x' * 49}:1" for index in range(65)) + slurm_config = RUNTIME / "slurm.conf" + original_config = slurm_config.read_text(encoding="utf-8") + slurm_config.write_text(original_config.replace("Licenses=", f"Licenses={unrelated},", 1), encoding="utf-8") + controller("scontrol", "reconfigure") + try: + assert values(*allocation, f"--licenses={unrelated}") == [None, None] + finally: + slurm_config.write_text(original_config, encoding="utf-8") + controller("scontrol", "reconfigure") + + for value in ("", "multiline\nvalue", "x" * 4096): + result = job( + "env", f"{reference}={value}", *allocation, f"--licenses={selected}", "/bin/true", check=False, timeout=60 + ) + assert result.returncode != 0, "Malformed reference unexpectedly reached the task" + for node in NODES: + wait_for(f"{node} to return to IDLE after rejected tasks", lambda node=node: node_is_idle(node)) + assert "DRAIN" not in node_record(node) + + # A submitted environment value must not turn an unlicensed allocation into + # a matching job in the remote SPANK hook. + assert values("env", f"SLURM_JOB_LICENSES={selected}", *allocation) == [None, None] + + output = RUNTIME / "jobs" / "spank-batch.out" + job( + "env", + f"{reference}=batch-value", + "sbatch", + "--wait", + "--time=1", + "--ntasks=1", + f"--nodelist={NODES[0]}", + f"--licenses={selected}", + "--output=/jobs/spank-batch.out", + "--wrap", + shlex.join(("python3", "-c", program)), + timeout=120, + ) + assert json.loads(output.read_text(encoding="utf-8")) == ["batch-value", catalogue] + (RUNTIME / "plugstack.conf").write_text( + f"required /usr/local/lib/slurm/mqt-core-qdmi-spank.so licenses={selected} reference={reference}:{selected}:\n", + encoding="utf-8", + ) + assert job(*allocation, f"--licenses={selected}", "/bin/true", check=False).returncode != 0 + (RUNTIME / "plugstack.conf").write_text("", encoding="utf-8") + for node in NODES: + wait_for(f"{node} to return to IDLE", lambda node=node: node_is_idle(node)) + + +def test_explicit_check() -> None: + """Run the payload only after the checker accepts the job's configuration.""" + body = RUNTIME / "jobs" / "checked-body" + (RUNTIME / "jobs" / "checker.qdmi.json").write_text( + json.dumps({"schema-version": 1, "qdmi": {"devices": [{"id": "mqt.sc.default", "enabled": False}]}}), + encoding="utf-8", + ) + for enabled in (True, False): + body.unlink(missing_ok=True) + setup = "unset MQT_CORE_QDMI_CONFIG_FILE MQT_CORE_QDMI_CONFIG_JSON\n" + if not enabled: + setup += "export MQT_CORE_QDMI_CONFIG_FILE=/jobs/checker.qdmi.json\n" + result = job( + "srun", + "--immediate=5", + "--time=1", + "--ntasks=1", + "--licenses=mqt.sc.default", + "sh", + "-ec", + setup + "mqt-core-qdmi-check --device mqt.sc.default --timeout 10\ntouch /jobs/checked-body", + check=False, + timeout=60, + ) + assert (result.returncode == 0) == enabled + assert body.exists() == enabled + + +def main(arguments: Sequence[str] = ()) -> None: + """Build the shared cluster and run its Core or provider workload.""" + options = parse_arguments(arguments) + wheels = tuple(options.dist.glob("mqt_core-*.whl")) if len(wheels) != 1: - msg = f"Build exactly one MQT Core wheel in {DIST}, found {len(wheels)}" + msg = f"Build exactly one MQT Core wheel in {options.dist}, found {len(wheels)}" raise RuntimeError(msg) + COMPOSE_ENV.update({ + "MQT_CORE_SLURM_CORE": str(ROOT), + "MQT_CORE_SLURM_WORKLOAD": str(options.workload.resolve()), + "MQT_CORE_SLURM_DIST": str(options.dist.resolve()), + "MQT_CORE_SLURM_SETUP_SCRIPT": options.setup_script, + }) + COMPOSE_FILES[:] = ("--file", str(options.compose_file.resolve())) if options.compose_file else () started_at = time.monotonic() @@ -354,9 +620,30 @@ def main() -> None: raise RuntimeError(msg) clean_runtime() + if options.device_license: + configuration = (RUNTIME / "slurm.conf").read_text(encoding="utf-8") + configuration = re.sub( + r"^Licenses=(.*)$", rf"Licenses=\1,{options.device_license}:2", configuration, flags=re.MULTILINE + ) + (RUNTIME / "slurm.conf").write_text(configuration, encoding="utf-8") LOGGER.info("Slurm runtime directory: %s", RUNTIME) started = True - compose("up", "--build", "--detach", "--wait", "--wait-timeout", "120", timeout=600, capture_output=False) + compose( + "up", + "--build", + "--detach", + "--scale", + f"node={options.nodes}", + "--wait", + "--wait-timeout", + "120", + timeout=1800, + capture_output=False, + ) + NODES[:] = [ + compose("exec", "-T", "--index", str(index), "node", "hostname").stdout.strip() + for index in range(1, options.nodes + 1) + ] LOGGER.info("Slurm image build and startup: %.2fs", time.monotonic() - started_at) testing_at = time.monotonic() @@ -366,7 +653,7 @@ def main() -> None: msg = f"The fixture requires Slurm 25.11 or newer, got {version_output!r}" raise RuntimeError(msg) - for node in ("node1", "node2"): + for node in NODES: compute(node, "test", "-r", "/sys/fs/cgroup/cgroup.controllers") delegate = compute( node, @@ -379,76 +666,28 @@ def main() -> None: if delegate != "yes": msg = f"The packaged slurmd.service on {node} must set Delegate=yes, got {delegate!r}" raise RuntimeError(msg) + compute( + node, + "python3", + "-c", + "from pathlib import Path; import subprocess; " + "pid = subprocess.check_output(['systemctl', 'show', 'slurmd.service', " + "'--property=MainPID', '--value'], text=True).strip(); " + "environment = Path('/proc/' + pid + '/environ').read_bytes().split(b'\\0'); " + "assert b'MQT_SLURM_TEST_REFERENCE=daemon-only' in environment; " + "assert b'MQT_CORE_QDMI_CONFIG_FILE=/daemon-only/qdmi.json' in environment", + ) wait_for(f"{node} to become IDLE with two processors", lambda node=node: node_is_idle(node)) - registry_check = ( - "from pathlib import Path; " - "import mqt.core; " - "from mqt.core.qdmi import device_ids; " - "module_path = Path(mqt.core.__file__).resolve(); " - "assert not any(module_path.is_relative_to(root) for root in ('/workspace', '/runtime')), module_path; " - "ids = set(device_ids()); " - "assert 'mqt.ddsim.default' in ids and 'mqt.sc.default' in ids, ids" - ) - controller("python3", "-c", registry_check) - assert_license("mqt.ddsim.default", total=2, used=0, free=2) - assert_license("mqt.sc.default", total=1, used=0, free=1) - - non_unit = submit("ddsim-job.sh", "mqt.ddsim.default:2") - wait_for_failed_adapter(non_unit, "must request exactly one Slurm license") - compound = submit("ddsim-job.sh", "mqt.ddsim.default:1,mqt.sc.default:1") - wait_for_failed_adapter(compound, "uses a compound AND expression") - alternative = submit("ddsim-job.sh", "mqt.ddsim.default:1|mqt.sc.default:1") - wait_for_failed_adapter(alternative, "uses a compound OR expression") - assert_license("mqt.ddsim.default", total=2, used=0, free=2) - assert_license("mqt.sc.default", total=1, used=0, free=1) - - first = submit("ddsim-job.sh", "mqt.ddsim.default:1", node="node1", hold=True) - second = submit("ddsim-job.sh", "mqt.ddsim.default:1", node="node2", hold=True) - wait_for_result("ddsim", first, "the first DDSIM Bell result") - wait_for_result("ddsim", second, "the second DDSIM Bell result") - wait_for("the first DDSIM job to hold on node1", lambda: job_matches(first, "RUNNING", node="node1")) - wait_for("the second DDSIM job to hold on node2", lambda: job_matches(second, "RUNNING", node="node2")) - if "CPUAlloc=1" not in node_record("node1") or "CPUAlloc=1" not in node_record("node2"): - msg = "Each held DDSIM job must leave one processor free on its compute node" - raise AssertionError(msg) - - third = submit("ddsim-job.sh", "mqt.ddsim.default:1") - wait_for( - "the third DDSIM job to wait for its license", - lambda: job_matches(third, "PENDING", reason="Licenses"), - ) - assert_license("mqt.ddsim.default", total=2, used=2, free=0) - - sc_job = submit("sc-job.sh", "mqt.sc.default:1") - wait_for_result("sc", sc_job, "the SC job to execute on a free CPU") - wait_for("the SC job to complete", lambda: job_finished(sc_job)) - sc_result = load_result("sc", sc_job) - if sc_result["node"] not in {"node1", "node2"} or sc_result["qubits"] <= 0: - msg = f"Unexpected SC job result: {sc_result}" - raise AssertionError(msg) - if not job_matches(first, "RUNNING", node="node1") or not job_matches(second, "RUNNING", node="node2"): - msg = "The SC job did not complete while both DDSIM licenses remained held" - raise AssertionError(msg) - - (RUNTIME / f"release-{first}").touch() - wait_for("the released first DDSIM job to finish", lambda: job_finished(first)) - wait_for_result("ddsim", third, "the pending third DDSIM job to execute") - wait_for("the third DDSIM job to finish", lambda: job_finished(third)) - - assert_bell_result(first, "node1") - assert_bell_result(second, "node2") - assert_bell_result(third) - assert_license("mqt.ddsim.default", total=2, used=1, free=1) - - (RUNTIME / f"release-{second}").touch() - wait_for("the released second DDSIM job to finish", lambda: job_finished(second)) - assert_license("mqt.ddsim.default", total=2, used=0, free=2) - - LOGGER.info( - "Slurm 25.11+ admitted two held DDSIM jobs, blocked the third for Licenses, " - "ran the SC job on a free CPU, and executed the third Bell job after release." - ) + registered = set(controller("sinfo", "--Node", "--noheader", "--format=%N").stdout.split()) + assert registered == set(NODES), registered + if options.command: + test_provider(options) + else: + test_core() + test_spank_transport() + test_explicit_check() + success = True LOGGER.info("Slurm admission and execution checks: %.2fs", time.monotonic() - testing_at) finally: @@ -471,4 +710,4 @@ def main() -> None: if __name__ == "__main__": logging.basicConfig(level=logging.INFO, format="%(message)s") - main() + main(sys.argv[1:]) diff --git a/test/slurm/sc_job.py b/test/slurm/sc_job.py index 5c5878b3b0..9e2349fc91 100644 --- a/test/slurm/sc_job.py +++ b/test/slurm/sc_job.py @@ -28,7 +28,7 @@ def main() -> None: "node": os.environ["SLURM_JOB_NODELIST"], "qubits": device.qubits_num(), } - result_path = Path(f"/runtime/sc-{job_id}.json") + result_path = Path(f"/jobs/sc-{job_id}.json") temporary = result_path.with_suffix(".tmp") temporary.write_text(json.dumps(result, sort_keys=True), encoding="utf-8") temporary.replace(result_path) From ecef8cea45edb806f11ff3bbf7d59e52259400c0 Mon Sep 17 00:00:00 2001 From: Lukas Burgholzer Date: Fri, 9 Oct 2026 23:43:12 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=A7=AA=20Cover=20availability=20comma?= =?UTF-8?q?nd=20usage=20and=20argument=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep a compact case table for help, missing arguments, invalid timeouts, unknown options, and unavailable IDs alongside the device-status checks. This restores public CLI coverage without the installed-test harness. Assisted-by: GPT-6 via Codex --- test/qdmi/test_check_cli.cmake | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/qdmi/test_check_cli.cmake b/test/qdmi/test_check_cli.cmake index b6207699ff..9698f4a617 100644 --- a/test/qdmi/test_check_cli.cmake +++ b/test/qdmi/test_check_cli.cmake @@ -34,3 +34,17 @@ foreach(status idle busy offline hang hang-exit) "Availability check for ${status}: expected ${expected}, got ${result}: ${output}${error}") endif() endforeach() + +foreach(arguments IN + ITEMS "0;--help" "2;--device" "2;--timeout;1" "2;--device;test.check;--timeout;0" + "2;--unknown;value" "1;--device;unknown") + list(POP_FRONT arguments expected) + execute_process( + COMMAND "${CHECKER}" ${arguments} + RESULT_VARIABLE result + OUTPUT_QUIET ERROR_QUIET + TIMEOUT 5) + if(NOT result STREQUAL "${expected}") + message(FATAL_ERROR "Availability command ${arguments}: expected ${expected}, got ${result}") + endif() +endforeach() From 443e40efda5f33f170d8fde3c1b7dd19e83a1381 Mon Sep 17 00:00:00 2001 From: Lukas Burgholzer Date: Fri, 9 Oct 2026 23:50:25 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=A7=AA=20Run=20Slurm=20checks=20when?= =?UTF-8?q?=20the=20shared=20command=20launcher=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The availability entry point uses _commands.py, so launcher changes must trigger the installed Slurm workload checks. Assisted-by: GPT-6 via Codex --- .github/workflows/slurm.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/slurm.yml b/.github/workflows/slurm.yml index 83b96e3de9..85631331af 100644 --- a/.github/workflows/slurm.yml +++ b/.github/workflows/slurm.yml @@ -11,6 +11,7 @@ on: - "cmake/**" - "include/mqt-core/qdmi/**" - "pyproject.toml" + - "python/mqt/core/_commands.py" - "python/mqt/core/qdmi/**" - "src/qdmi/**" - "spank/**" @@ -28,6 +29,7 @@ on: - "cmake/**" - "include/mqt-core/qdmi/**" - "pyproject.toml" + - "python/mqt/core/_commands.py" - "python/mqt/core/qdmi/**" - "src/qdmi/**" - "spank/**" From 224e97eb0b2fc1d60efaa2be6a9c399302229334 Mon Sep 17 00:00:00 2001 From: Lukas Burgholzer Date: Sat, 10 Oct 2026 01:48:54 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Gate=20QDMI=20availabi?= =?UTF-8?q?lity=20with=20Slurm=20licenses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use license-only reservations for site health checks before scheduling. Ordinary job environments carry credentials, so remove the SPANK module and its build, licensing, configuration, and transport-test machinery. Discover installed manifests through the Python command launcher and support bounded Windows checks with Job Objects. Keep real timeout/descendant tests and exercise outage recovery in the shared Slurm cluster. Drop the Cache.cmake workaround and mirror the portable CI condition from #2726 while that PR remains open. Keep native developer CPU tuning. Assisted-by: GPT-6 via Codex --- .agent/plans/qdmi-slurm.md | 67 ++- .github/workflows/ci.yml | 6 +- .github/workflows/slurm.yml | 17 +- .license-tools-config.json | 1 - cmake/CompilerOptions.cmake | 4 +- docker/slurm/Dockerfile | 12 - docker/slurm/README.md | 12 +- docker/slurm/compose.yml | 6 +- docker/slurm/prepare.sh | 3 +- docs/glossary.md | 10 +- docs/qdmi/driver.md | 18 +- docs/qdmi/slurm.md | 75 ++- examples/slurm/README.md | 99 ++++ examples/slurm/update_availability.py | 111 +++++ noxfile.py | 2 +- pyproject.toml | 2 - python/mqt/core/_commands.py | 13 +- spank/CMakeLists.txt | 38 -- spank/LICENSE.md | 614 ------------------------- spank/mqt-core-qdmi-spank.map | 33 -- spank/spank.cpp | 296 ------------ src/qdmi/CMakeLists.txt | 8 +- src/qdmi/Check.cpp | 168 ++++++- test/python/qdmi/test_discovery.py | 38 ++ test/python/test_cli.py | 10 +- test/python/test_slurm_availability.py | 84 ++++ test/python/test_slurm_integration.py | 7 +- test/qdmi/driver/session_device.cpp | 7 + test/qdmi/test_check_cli.cmake | 29 ++ test/slurm/provider_probe.py | 2 +- test/slurm/run_integration.py | 166 ++----- 31 files changed, 688 insertions(+), 1270 deletions(-) create mode 100644 examples/slurm/README.md create mode 100755 examples/slurm/update_availability.py delete mode 100644 spank/CMakeLists.txt delete mode 100644 spank/LICENSE.md delete mode 100644 spank/mqt-core-qdmi-spank.map delete mode 100644 spank/spank.cpp create mode 100644 test/python/test_slurm_availability.py diff --git a/.agent/plans/qdmi-slurm.md b/.agent/plans/qdmi-slurm.md index ab287e7b8d..47ca5aefb1 100644 --- a/.agent/plans/qdmi-slurm.md +++ b/.agent/plans/qdmi-slurm.md @@ -1,42 +1,35 @@ # QDMI workloads on Slurm -Status: implemented; publication and hosted checks pending. - -## Goal and scope - -Provide one Slurm integration above MQT Core's QDMI client interface. Device -implementations supply their runtime, credentials, and a small smoke test. The -Docker cluster in `docker/slurm/` supports tests and local demonstrations. - -## Decisions - -- Slurm license names identify QDMI devices; admission and device authorization - remain separate. The selector accepts one ID with an optional `:1` count. -- Use Slurm's environment export for job settings. The optional source-only - SPANK module supplies license-specific site defaults without loading devices. -- Run the availability command after selecting the workload environment. Keep - its bounded native worker; remove automatic SPANK validation and per-step - state. -- Use the shared native command launcher and runtime installation helpers. -- Use one scalable Compose compute service, with disposable rootful Linux - cgroup-v2 hosts as the supported deployment boundary. -- Consolidate the unreleased Core stack so these contracts are reviewed - together. - -## Progress - -- [x] Simplify the plugin, checker, documentation, and test coverage. -- [x] Separate reusable cluster setup from test assertions and validate scaling. -- [x] Refresh device integrations on upstream main, simplify smoke tests, and - make ordinary IQM CI work without live service credentials. -- [x] Validate focused native/Python checks, standalone SPANK, and cluster - scaling. -- [x] Pass IQM and Braket smoke tests in both native and wheel modes. -- [ ] Publish the revised PRs and inspect hosted checks. +Status: implemented and published; hosted checks pending. + +## Scope and decisions + +- Slurm licenses identify devices and limit allocations. Use ordinary job + environments for catalogue and credential configuration; no SPANK module is + needed by MQT Core, IQM, or Braket. +- A privileged site monitor reserves all licenses for an unavailable device. + Block before probing; only a successful bounded check reopens scheduling. + Running jobs continue. Polling needs supervision and is not a reservation at + the remote device service. +- The checker uses installed Python catalogue metadata when launched through + Python and bounded process-tree cleanup on POSIX and Windows. +- The scalable Docker cluster is shared by tests and demonstrations. Credentials + enter at runtime. IQM Emerald mock and Braket SV1 execute small workloads in + native and wheel modes; no quantum hardware is in scope. +- Keep the unreleased Core work consolidated in #2599 and update the two device + PRs against upstream main. Preserve credentialed ordinary CI lanes. ## Validation -Run the focused checker tests, native command tests, standalone SPANK lint, -cluster admission/environment tests, and IQM/Braket native/wheel smoke tests. -Run each repository's required lint. Check final-head CI separately from local -results. Real quantum hardware is outside this task. +Run checker CLI/discovery and descendant-cleanup tests, the focused runner +suite, and a real Slurm availability block/recovery scenario. Exercise both +credentialed device workloads in native and wheel modes. Run repository lint and +full-file C++ lint. Record local and hosted results separately; publishing is +not a request to monitor CI. + +Local validation: 48 focused Python cases and seven native tests pass. The +three-node cluster proves license capacity, outage blocking, and recovery. +Credentialed Emerald mock and SV1 tests pass in both installation modes. +Executable documentation and lint pass. Windows execution awaits hosted CI. The +upstream #2726 portable-CI condition is mirrored until it merges; the +Cache.cmake workaround is removed. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 34e902dd0e..f39a775174 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -137,9 +137,9 @@ jobs: setup-python: true install-pkgs: "nanobind==3.1.0" cpp-linter-extra-args: "-std=c++20 -Wunused-template" - # Vendored headers are checked upstream; standalone SPANK is checked in - # the Slurm workflow against its own compilation database and headers. - cpp-linter-ignore-extra: "vendor/**|spank/**" + # Vendored third-party headers are checked upstream and are not maintained + # according to MQT Core's clang-tidy configuration. + cpp-linter-ignore-extra: "vendor/**" setup-mlir: true llvm-version: 23.1.2 permissions: diff --git a/.github/workflows/slurm.yml b/.github/workflows/slurm.yml index 85631331af..b995a9e9fb 100644 --- a/.github/workflows/slurm.yml +++ b/.github/workflows/slurm.yml @@ -14,8 +14,8 @@ on: - "python/mqt/core/_commands.py" - "python/mqt/core/qdmi/**" - "src/qdmi/**" - - "spank/**" - "docker/slurm/**" + - "examples/slurm/**" - "test/slurm/**" - "test/qdmi/**" - "test/python/qdmi/**" @@ -32,8 +32,8 @@ on: - "python/mqt/core/_commands.py" - "python/mqt/core/qdmi/**" - "src/qdmi/**" - - "spank/**" - "docker/slurm/**" + - "examples/slurm/**" - "test/slurm/**" - "test/qdmi/**" - "test/python/qdmi/**" @@ -50,7 +50,7 @@ permissions: jobs: slurm: - name: Slurm 25.11, QDMI devices, and SPANK + name: Slurm scheduling and QDMI execution runs-on: ubuntu-26.04 timeout-minutes: 45 env: @@ -72,7 +72,7 @@ jobs: - name: Check the Slurm test runner run: >- uv run --no-project --with 'pytest>=9.0.1' --python 3.14 - pytest -o addopts= -q test/python/test_slurm_integration.py + pytest -o addopts= -q test/python/test_slurm_integration.py test/python/test_slurm_availability.py - name: Set up MLIR uses: munich-quantum-software/setup-mlir@8d3eae73d0f0196fd30c787d91ebd2018cb6c709 # v1.5.0 @@ -84,15 +84,6 @@ jobs: with: version: v0.16.0 - - name: Lint the standalone SPANK component - run: | - sudo apt-get update - curl -fsSL https://apt.llvm.org/llvm.sh -o "$RUNNER_TEMP/llvm-install.sh" - sudo bash "$RUNNER_TEMP/llvm-install.sh" 23 - sudo apt-get install --no-install-recommends -y clang-tidy-23 libslurm-dev - cmake -S spank -B build/spank -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - clang-tidy-23 -p build/spank spank/spank.cpp --header-filter='(^|/)spank/' --warnings-as-errors='*' - - name: Build the MQT Core wheel timeout-minutes: 15 run: uv build --wheel --out-dir dist -Ccmake.define.DEPLOY=ON diff --git a/.license-tools-config.json b/.license-tools-config.json index 01b2b6fd69..31bd13dbb0 100644 --- a/.license-tools-config.json +++ b/.license-tools-config.json @@ -18,7 +18,6 @@ ".proto": "SLASH_STYLE" }, "exclude": [ - "^spank/", "^docker/slurm/Dockerfile$", "^\\.[^/]+", "/\\.[^/]+", diff --git a/cmake/CompilerOptions.cmake b/cmake/CompilerOptions.cmake index 08df146a25..e258e16f9c 100644 --- a/cmake/CompilerOptions.cmake +++ b/cmake/CompilerOptions.cmake @@ -33,8 +33,8 @@ function(enable_project_options target_name) target_link_libraries(${target_name} INTERFACE --coverage) endif() - if(NOT DEPLOY) - # only include machine-specific optimizations when building for the host machine + if(NOT DEPLOY AND NOT DEFINED ENV{CI}) + # CI caches can reuse object files on runners with different CPUs. check_cxx_compiler_flag(-mtune=native HAS_MTUNE_NATIVE) if(HAS_MTUNE_NATIVE) target_compile_options(${target_name} INTERFACE -mtune=native) diff --git a/docker/slurm/Dockerfile b/docker/slurm/Dockerfile index 1c6953be18..ae66868a70 100644 --- a/docker/slurm/Dockerfile +++ b/docker/slurm/Dockerfile @@ -10,16 +10,6 @@ FROM ghcr.io/astral-sh/uv:0.12.1 AS uv -FROM ubuntu:26.04 AS spank - -RUN apt-get update \ - && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ - cmake g++ make libslurm-dev -COPY --from=core spank /src/spank -RUN cmake -S /src/spank -B /build -DCMAKE_BUILD_TYPE=Release \ - && cmake --build /build --parallel 2 \ - && cmake --install /build --prefix /usr/local - FROM ubuntu:26.04 AS base ENV container=docker @@ -37,8 +27,6 @@ RUN apt-get update \ && rm -rf /var/lib/apt/lists/* COPY --from=uv /uv /uvx /bin/ -COPY --from=spank /usr/local/lib/slurm/mqt-core-qdmi-spank.so /usr/local/lib/slurm/ -COPY --from=spank /usr/local/share/licenses/mqt-core-qdmi-spank /usr/local/share/licenses/mqt-core-qdmi-spank COPY --chmod=755 start.sh /usr/local/sbin/mqt-slurm-start COPY node.conf /etc/systemd/system/slurmd.service.d/mqt-node.conf diff --git a/docker/slurm/README.md b/docker/slurm/README.md index 77469a590c..a854ff06c5 100644 --- a/docker/slurm/README.md +++ b/docker/slurm/README.md @@ -18,11 +18,6 @@ sh docker/slurm/prepare.sh docker compose -f docker/slurm/compose.yml up --build -d --wait --scale node=2 ``` -The image builds the optional GPL SPANK plugin from `spank/` separately from the -MIT MQT Core wheel. `build/slurm/plugstack.conf` is initially empty. Site -defaults can be enabled there using the configuration in the -[Slurm guide](../../docs/qdmi/slurm.md). - Submit jobs as the unprivileged user shared by all nodes: ```console @@ -58,6 +53,7 @@ a different cluster size. `MQT_CORE_SLURM_DIST` selects an existing directory containing one MQT Core wheel. Device implementation tests can extend the image with `MQT_CORE_SLURM_WORKLOAD`, `MQT_CORE_SLURM_SETUP_SCRIPT`, and the build arguments `PROVIDER_RUNTIME_COMPONENT` and `PROVIDER_INSTALL_MODE` (`native` or -`wheel`). Set build arguments on `controller`; `node` reuses that image. -Provider Compose overlays add their mock endpoint and its startup dependency to -both Slurm services. +`wheel`). Set build arguments on `controller`; `node` reuses that image. Device +implementation overlays supply credentials to the submission container at +runtime. Slurm exports these settings to the workload; credentials are never +part of the image build. diff --git a/docker/slurm/compose.yml b/docker/slurm/compose.yml index 88bc3dc223..a620cdddb3 100644 --- a/docker/slurm/compose.yml +++ b/docker/slurm/compose.yml @@ -15,11 +15,10 @@ x-cluster: &cluster volumes: - /sys/fs/cgroup:/sys/fs/cgroup:rw - ${MQT_CORE_SLURM_CORE:-../..}:/workspace:ro - - ${MQT_CORE_SLURM_WORKLOAD:-../..}:/workload:ro + - ${MQT_CORE_SLURM_WORKLOAD:-.}:/workload:ro - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}:/runtime - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}/jobs:/jobs - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}/slurm.conf:/etc/slurm/slurm.conf:ro - - ${MQT_CORE_SLURM_RUNTIME:-../../build/slurm}/plugstack.conf:/etc/slurm/plugstack.conf:ro - ./cgroup.conf:/etc/slurm/cgroup.conf:ro x-healthcheck: &healthcheck @@ -34,8 +33,7 @@ services: build: context: . additional_contexts: - core: ${MQT_CORE_SLURM_CORE:-../..} - workload: ${MQT_CORE_SLURM_WORKLOAD:-../..} + workload: ${MQT_CORE_SLURM_WORKLOAD:-.} dist: ${MQT_CORE_SLURM_DIST:-../../dist} args: SETUP_SCRIPT: ${MQT_CORE_SLURM_SETUP_SCRIPT:-} diff --git a/docker/slurm/prepare.sh b/docker/slurm/prepare.sh index 0ea7c79d49..f369f203c8 100644 --- a/docker/slurm/prepare.sh +++ b/docker/slurm/prepare.sh @@ -16,5 +16,4 @@ head -c 1024 /dev/urandom > "$runtime/munge.key" mkdir "$runtime/jobs" chmod 1777 "$runtime/jobs" cp "$(dirname "$0")/slurm.conf" "$runtime/slurm.conf" -touch "$runtime/plugstack.conf" -chmod 644 "$runtime/slurm.conf" "$runtime/plugstack.conf" +chmod 644 "$runtime/slurm.conf" diff --git a/docs/glossary.md b/docs/glossary.md index 2c3e71b4bb..0492cd0052 100644 --- a/docs/glossary.md +++ b/docs/glossary.md @@ -133,14 +133,14 @@ Quantum Device Management Interface QDMI Client interface **Preferred term:** QDMI Client interface. The standard C interface consumed by - applications to open sessions, query devices, and manage jobs. Core's C++ and + applications to open sessions, query devices, and manage jobs. MQT Core's C++ and Python Client wrappers consume this interface; they are not a driver. QDMI driver **Preferred term:** QDMI driver. An implementation of the QDMI Client interface. The builtin MQT Core QDMI driver loads QDMI device libraries. A replacement driver owns its own discovery, configuration, and device access; applications must not - assume it provides Core's private driver extension. + assume it provides MQT Core's private driver extension. QDMI session **Preferred term:** QDMI session. A connection to a QDMI driver, represented @@ -152,12 +152,6 @@ C++ QDMI library the QDMI Client Interface. The driver manages device libraries and their configuration; the C++ library is independent of that implementation. -SPANK - **Preferred term:** SPANK. **Accepted expansion:** Slurm Plug-in Architecture - for Node and job Kontrol. Slurm's plugin interface for job launch hooks. - MQT Core's optional SPANK module supplies license-specific defaults for job - environments. - QIR Quantum Intermediate Representation **Preferred term:** Quantum Intermediate Representation. **Accepted diff --git a/docs/qdmi/driver.md b/docs/qdmi/driver.md index 08462055a8..a5235bf9f2 100644 --- a/docs/qdmi/driver.md +++ b/docs/qdmi/driver.md @@ -133,18 +133,22 @@ submitting a quantum job: mqt-core-qdmi-check --device mqt.sc.default --timeout 10 ``` -Run it in the workload environment with the required credentials. For an -external device, set `MQT_CORE_QDMI_CONFIG_FILE` to its catalogue; the native -command uses [QDMI configuration](configuration.md), independently of Python's -entry-point discovery and in-process registrations. +Run it in the workload environment with the required credentials. The Python +console script discovers +[installed device manifests](configuration.md#installed-device-manifests). The +native executable accepts additional manifests through repeated +`--manifest PATH` arguments. Invalid manifests are skipped. These manifests +retain the lowest precedence in [QDMI configuration](configuration.md); +`MQT_CORE_QDMI_CONFIG_FILE` selects an explicit catalogue. The timeout covers device initialization, the status query, and worker exit. It defaults to 30 seconds and accepts whole seconds from 1 to 3600. Exit codes are 0 for availability, 1 for failure, 2 for invalid arguments, and 124 for a -timeout. Success is silent. Device output is suppressed to protect credentials. +timeout. Device output is suppressed to protect credentials. -The command is included in Linux and macOS builds and wheels. Availability is a -snapshot; it does not authorize access or reserve device capacity. +The command is included in Linux, macOS, and Windows builds and wheels (Windows +10 or Windows Server 2016 and newer). Availability is a snapshot; it does not +authorize access or reserve device capacity. ## Python Bindings diff --git a/docs/qdmi/slurm.md b/docs/qdmi/slurm.md index ed61431746..7eb917655e 100644 --- a/docs/qdmi/slurm.md +++ b/docs/qdmi/slurm.md @@ -61,6 +61,40 @@ Slurm exports the submission environment by default. Use ordinary environment variables or Slurm's `--export` option for job-specific settings. Variables set inside a batch script are inherited by its subsequent `srun` steps. +## Hold jobs while a device is unavailable + +A check inside a job runs after Slurm has allocated resources. To leave jobs +pending while a device is unavailable, an administrator must update scheduler +state independently of the jobs. + +Slurm supports +[license-only reservations](https://slurm.schedmd.com/reservations.html) for +unavailable shared resources. Reserve the device's entire configured license +count for an administrative account. New jobs requiring it remain pending with +reason `Licenses`; running jobs continue, and other devices remain usable. +Remove the reservation after a successful health check. + +The +[availability monitor example](https://github.com/munich-quantum-toolkit/core/tree/main/examples/slurm) +performs one such update. It blocks the licenses before invoking the bounded +QDMI checker and removes the block only on success. Run it periodically from a +trusted administrative host with Slurm clients, MQT Core, the device +implementation, and site-owned credentials. Initialize the blocks before +admitting workloads. The controller does not need device libraries. + +Use a site account that can assess the shared device's operational state. One +user's expired credentials must not determine availability for every user. +Conversely, a successful site check does not verify each user's authorization. +The monitor is a snapshot: device status can change between checks, and a +stopped monitor leaves its last scheduler state in place. Supervise it and alert +on stale updates. Keep the optional job check for the user's environment. + +This integration uses licenses and Slurm's environment export. Device libraries +run in application or checker processes; no Slurm plugin is needed. The +[QRMI integration paper](https://arxiv.org/abs/2607.19591) describes a separate +acquire/execute/release lifecycle for services that issue allocation tokens. The +QDMI device implementations used here do not require that lifecycle. + ## Configure the cluster Use matching Slurm versions across the cluster. This integration requires Slurm @@ -108,47 +142,6 @@ and [cgroup configuration](https://slurm.schedmd.com/cgroup.conf.html). selection, not as proof of allocation or authorization. Device services and operating-system permissions must enforce access independently. -## Optional site defaults with SPANK - -Use MQT Core's SPANK module when a device license should select default -catalogue paths or credential references. Jobs can override these defaults -through their environment. No plugin is needed when jobs already supply their -configuration. - -Build the standalone module against the cluster's Slurm development headers: - -```console -cmake -S spank -B build/spank -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -cmake --build build/spank -cmake --install build/spank -``` - -The build requires Linux, CMake, and a C++20 compiler. It does not need LLVM or -device SDKs. Install the module and its `plugstack.conf` entry on compute nodes. -If login or submission hosts use the same plugstack configuration, install the -module there too. Rebuild it when changing Slurm major versions. -`MQT_CORE_SPANK_INSTALL_DIR` selects the module installation directory. - -Load the module once, with the permitted device IDs and non-secret defaults: - -```ini -required /usr/local/lib/slurm/mqt-core-qdmi-spank.so licenses=amazon.braket.sv1,iqm.emerald qdmi_config_file=/etc/mqt-core/qdmi.json reference=AWS_PROFILE:amazon.braket.sv1:quantum reference=IQM_TOKENS_FILE:iqm.emerald:/shared/iqm/tokens.json -``` - -`qdmi_config_file=PATH` supplies `MQT_CORE_QDMI_CONFIG_FILE`. Each -`reference=ENV:ID,ID:DEFAULT` supplies an environment variable for the listed -device IDs. Use paths, profile names, and other non-secret references; keep -tokens and passwords out of `plugstack.conf`. - -Defaults apply only to an exact configured `ID` or `ID:1` license expression. -Other jobs pass through unchanged. Values already present in the job environment -take precedence. The module never reads credential files or loads a device -implementation, and it does not copy credentials from Slurm daemons. Invalid -settings fail the job without draining the compute node. - -The module is GPL-3.0-or-later and distributed in the source checkout, -separately from MQT Core's MIT-licensed runtime, wheels, and source packages. - ## Try the Docker cluster The reusable diff --git a/examples/slurm/README.md b/examples/slurm/README.md new file mode 100644 index 0000000000..f2ec65a5fa --- /dev/null +++ b/examples/slurm/README.md @@ -0,0 +1,99 @@ +# Update Slurm device availability + +`update_availability.py` uses Slurm 25.11+ license-only reservations to keep +jobs pending while a QDMI device is unavailable. It reserves the device's full +local license count before each check and removes that reservation only after a +successful check. Running jobs continue, and unrelated licenses remain usable. +No SPANK plugin or SlurmDBD is required. + +Run one monitor per device, as root or the configured `SlurmUser`, on an +administrative host with Slurm clients and the site's QDMI checker, catalogue, +device implementation, and credentials. The controller itself needs no device +implementation. The monitor's credentials must represent site access; a user's +failed authentication must not change cluster-wide availability. Jobs retain +their own credentials and must handle failures after allocation. + +For `Licenses=mqt.ddsim.default:2`, run: + +```console +python3 update_availability.py --license mqt.ddsim.default:2 --checker /opt/qdmi/bin/mqt-core-qdmi-check +``` + +The count must equal the full configured capacity. The monitor owns +`qdmi-unavailable-mqt.ddsim.default`; reserve this name for it, and do not +create overlapping license reservations. Each invocation renews the block for +one year. Use `--block-only` to close admission without probing. Stop both the +timer and its service before using this option for a manual outage, so an +in-flight or later healthy check cannot reopen it. + +Exit status is `0` for a successful readiness check or `--block-only`, `1` for a +failed or timed-out probe with admission blocked, and `2` for invalid arguments +or a controller operation failure. Alert on controller failures: an unreachable +controller cannot be updated. Checker diagnostics are suppressed because they +can contain credentials. Inspect device failures separately under the site's +logging policy. + +The checker accepts `IDLE` and `BUSY` as operational states. The license count +is the site's concurrency policy; this check does not measure the remote queue +or reserve capacity at the service. Probe failures, including expired monitor +credentials, leave admission closed until a later successful probe. + +## Poll with systemd + +Copy the example to `/opt/mqt-core/examples/slurm/`. Adapt this service for each +device as `/etc/systemd/system/qdmi-availability.service`: + +```ini +[Unit] +Description=Update QDMI device admission in Slurm +Wants=network-online.target +After=network-online.target munge.service + +[Service] +Type=oneshot +User=slurm +Environment=MQT_CORE_QDMI_CONFIG_FILE=/etc/mqt-core/site.qdmi.json +ExecStart=/usr/bin/python3 /opt/mqt-core/examples/slurm/update_availability.py --license mqt.ddsim.default:2 --checker /opt/qdmi/bin/mqt-core-qdmi-check +TimeoutStartSec=45 +``` + +Provide any device-specific credential-file reference through a root-owned +service configuration; do not put tokens or keys in command-line arguments. +Install `/etc/systemd/system/qdmi-availability.timer`: + +```ini +[Unit] +Description=Poll QDMI device readiness + +[Timer] +OnBootSec=1s +OnUnitInactiveSec=30s +AccuracySec=1s + +[Install] +WantedBy=timers.target +``` + +Before opening the queue, establish blocks for every monitored license. For an +existing cluster, this sequence pauses new allocations while retaining running +jobs. Apply it to every partition that can request these licenses: + +```console +scontrol update PartitionName=compute State=DOWN +python3 /opt/mqt-core/examples/slurm/update_availability.py --license mqt.ddsim.default:2 --block-only +systemctl daemon-reload +systemctl enable --now qdmi-availability.timer +scontrol update PartitionName=compute State=UP +``` + +For a new controller, start the affected partitions with `State=DOWN` in +`slurm.conf`, then establish the blocks before setting them `UP`. Incorporate +this ordering into site startup procedures. + +Polling gives a readiness snapshot. A device can fail after a successful probe, +and a stopped timer after success leaves the license open. Supervise the timer +and service; if the site requires a maximum observation age, an independent +watchdog must run `--block-only` when that age is exceeded. A service +`OnFailure` handler can alert or close admission, but cannot detect a stopped +timer by itself. Inspect both `Free` and `Reserved` in `scontrol show licenses`: +reserved tokens can still appear in `Free`. diff --git a/examples/slurm/update_availability.py b/examples/slurm/update_availability.py new file mode 100755 index 0000000000..cee4b29053 --- /dev/null +++ b/examples/slurm/update_availability.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +"""Block a local Slurm device license until a site readiness probe succeeds.""" + +from __future__ import annotations + +import argparse +import json +import logging +import re +import subprocess +import sys +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from collections.abc import Sequence + +LOGGER = logging.getLogger("qdmi-slurm-availability") + + +def scontrol(*arguments: str) -> str: + """Run a bounded controller operation. + + Returns: + The command's standard output. + """ + # Slurm clients come from the administrator's PATH; no shell is involved. + return subprocess.run( # ruff: ignore[subprocess-without-shell-equals-true] + ("scontrol", *arguments), # ruff: ignore[start-process-with-partial-path] + check=True, + stdout=subprocess.PIPE, + text=True, + timeout=10, + ).stdout + + +def main(arguments: Sequence[str] | None = None) -> int: + """Close admission before probing. + + Returns: + Zero for readiness or block-only success, one for a failed probe, or two + for a controller error. + """ + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--license", required=True, metavar="ID:COUNT", help="local device ID and full configured capacity" + ) + parser.add_argument("--checker", default="mqt-core-qdmi-check", help="site checker executable") + parser.add_argument("--timeout", type=int, default=10, help="checker deadline in seconds (default: 10)") + parser.add_argument("--block-only", action="store_true", help="close admission without running the checker") + options = parser.parse_args(arguments) + if re.fullmatch(r"[A-Za-z0-9_.-]+:[1-9][0-9]*", options.license) is None: + parser.error("--license must be a local ID:COUNT with a positive count") + if not 1 <= options.timeout <= 3600: + parser.error("--timeout must be between 1 and 3600 seconds") + + device = options.license.rsplit(":", maxsplit=1)[0] + name = f"qdmi-unavailable-{device}" + try: + reservations = json.loads(scontrol("--json", "show", "reservations"))["reservations"] + exists = any(reservation["name"] == name for reservation in reservations) + # Active reservations cannot change their start time. Renew the end time + # because Slurm implements Duration=infinite as one year. + scontrol( + "update" if exists else "create", + f"ReservationName={name}", + *(("StartTime=now",) if not exists else ()), + "EndTime=now+365days", + "Users=root", + "Flags=LICENSE_ONLY,IGNORE_JOBS", + f"Licenses={options.license}", + ) + except (OSError, subprocess.SubprocessError, ValueError, KeyError): + LOGGER.exception("Could not block %s", device) + return 2 + + if options.block_only: + return 0 + try: + # The administrator selects the site executable; arguments use no shell. + result = subprocess.run( # ruff: ignore[subprocess-without-shell-equals-true] + (options.checker, "--device", device, "--timeout", str(options.timeout)), + check=False, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + timeout=options.timeout + 2, + ) + except (OSError, subprocess.SubprocessError): + LOGGER.exception("Readiness probe failed; %s remains blocked", device) + return 1 + if result.returncode != 0: + LOGGER.error("Readiness probe failed; %s remains blocked", device) + return 1 + + try: + scontrol("delete", f"ReservationName={name}") + except (OSError, subprocess.SubprocessError): + LOGGER.exception("Could not reopen %s", device) + return 2 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/noxfile.py b/noxfile.py index 3006d929b1..e035694ee9 100755 --- a/noxfile.py +++ b/noxfile.py @@ -111,7 +111,7 @@ def cpp_lint(session: nox.Session) -> None: "--style=", "--tidy-checks=", f"--version={llvm_bin}", - "--ignore=build|!build/mlir/**|**/include|include|vendor/**|spank/**", + "--ignore=build|!build/mlir/**|**/include|include|vendor/**", "--thread-comments=false", "--step-summary=false", "--database=build/cpp-lint", diff --git a/pyproject.toml b/pyproject.toml index d3a769ab4c..2a6f439765 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -203,7 +203,6 @@ version = ">=1.10" include = [ "python/mqt/core/_version.py" ] exclude = [ - "spank/**", "**/.github", "**/doc", "**/docs", @@ -378,7 +377,6 @@ PC170 = "We do not use rST files anymore" [tool.check-sdist] sdist-only = [ "python/mqt/core/_version.py" ] git-only = [ - "spank/*", "docs/*", "test/*", "mlir/*", diff --git a/python/mqt/core/_commands.py b/python/mqt/core/_commands.py index d381e51437..c304c376f0 100644 --- a/python/mqt/core/_commands.py +++ b/python/mqt/core/_commands.py @@ -16,6 +16,8 @@ from pathlib import Path from typing import NoReturn +from ._qdmi_discovery import discover_qdmi_manifests + def compiler() -> NoReturn: """Launch the bundled MQT compiler.""" @@ -29,16 +31,17 @@ def benchmark() -> NoReturn: def qdmi_check() -> NoReturn: """Probe whether a QDMI device is operational.""" - if sys.platform == "win32": - sys.exit("The QDMI availability check requires Linux or macOS.") - run_tool("mqt-core-qdmi-check") + arguments: list[str] = [] + if sys.argv[1:] != ["--help"]: + discover_qdmi_manifests(lambda path: arguments.extend(("--manifest", str(path)))) + run_tool("mqt-core-qdmi-check", *arguments) -def run_tool(name: str) -> NoReturn: +def run_tool(name: str, *extra_arguments: str) -> NoReturn: """Replace this process with a bundled native tool.""" suffix = ".exe" if sys.platform == "win32" else "" executable = Path(str(distribution("mqt-core").locate_file(f"mqt/core/bin/{name}{suffix}"))) - os.execv(executable, [str(executable), *sys.argv[1:]]) # ruff: ignore[start-process-with-no-shell] + os.execv(executable, [str(executable), *sys.argv[1:], *extra_arguments]) # ruff: ignore[start-process-with-no-shell] def include_dir() -> Path: diff --git a/spank/CMakeLists.txt b/spank/CMakeLists.txt deleted file mode 100644 index c54d80b1b3..0000000000 --- a/spank/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH -# All rights reserved. -# -# SPDX-License-Identifier: GPL-3.0-or-later -# -# This file is part of the optional MQT Core SPANK component. -# See LICENSE.md for the GNU General Public License, version 3 or later. - -cmake_minimum_required(VERSION 3.28...4.4) -project(mqt-core-qdmi-spank LANGUAGES CXX) - -if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux") - message(FATAL_ERROR "The MQT Core SPANK component requires Linux and Slurm 25.11 or newer") -endif() - -include(GNUInstallDirs) -find_path(SLURM_SPANK_INCLUDE_DIR slurm/spank.h REQUIRED) - -add_library(mqt-core-qdmi-spank MODULE spank.cpp) -target_compile_features(mqt-core-qdmi-spank PRIVATE cxx_std_20) -target_include_directories(mqt-core-qdmi-spank SYSTEM PRIVATE ${SLURM_SPANK_INCLUDE_DIR}) -set_target_properties(mqt-core-qdmi-spank PROPERTIES PREFIX "") -target_link_options(mqt-core-qdmi-spank PRIVATE - "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/mqt-core-qdmi-spank.map") -set_property( - TARGET mqt-core-qdmi-spank - APPEND - PROPERTY LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/mqt-core-qdmi-spank.map") - -set(MQT_CORE_SPANK_INSTALL_DIR - "${CMAKE_INSTALL_LIBDIR}/slurm" - CACHE PATH "Install directory for the Slurm SPANK module") -install(TARGETS mqt-core-qdmi-spank LIBRARY DESTINATION ${MQT_CORE_SPANK_INSTALL_DIR} - COMPONENT mqt-core-qdmi-spank) -install( - FILES LICENSE.md - DESTINATION ${CMAKE_INSTALL_DATADIR}/licenses/mqt-core-qdmi-spank - COMPONENT mqt-core-qdmi-spank) diff --git a/spank/LICENSE.md b/spank/LICENSE.md deleted file mode 100644 index 1cc5da492a..0000000000 --- a/spank/LICENSE.md +++ /dev/null @@ -1,614 +0,0 @@ -# GNU General Public License - -_Version 3, 29 June 2007_ -_Copyright © 2007 Free Software Foundation, Inc. <>_ - -Everyone is permitted to copy and distribute verbatim copies of this license -document, but changing it is not allowed. - -## Preamble - -The GNU General Public License is a free, copyleft license for software and -other kinds of works. - -The licenses for most software and other practical works are designed to take -away your freedom to share and change the works. By contrast, the GNU General -Public License is intended to guarantee your freedom to share and change all -versions of a program--to make sure it remains free software for all its users. -We, the Free Software Foundation, use the GNU General Public License for most of -our software; it applies also to any other work released this way by its -authors. You can apply it to your programs, too. - -When we speak of free software, we are referring to freedom, not price. Our -General Public Licenses are designed to make sure that you have the freedom to -distribute copies of free software (and charge for them if you wish), that you -receive source code or can get it if you want it, that you can change the -software or use pieces of it in new free programs, and that you know you can do -these things. - -To protect your rights, we need to prevent others from denying you these rights -or asking you to surrender the rights. Therefore, you have certain -responsibilities if you distribute copies of the software, or if you modify it: -responsibilities to respect the freedom of others. - -For example, if you distribute copies of such a program, whether gratis or for a -fee, you must pass on to the recipients the same freedoms that you received. You -must make sure that they, too, receive or can get the source code. And you must -show them these terms so they know their rights. - -Developers that use the GNU GPL protect your rights with two steps: **(1)** -assert copyright on the software, and **(2)** offer you this License giving you -legal permission to copy, distribute and/or modify it. - -For the developers' and authors' protection, the GPL clearly explains that there -is no warranty for this free software. For both users' and authors' sake, the -GPL requires that modified versions be marked as changed, so that their problems -will not be attributed erroneously to authors of previous versions. - -Some devices are designed to deny users access to install or run modified -versions of the software inside them, although the manufacturer can do so. This -is fundamentally incompatible with the aim of protecting users' freedom to -change the software. The systematic pattern of such abuse occurs in the area of -products for individuals to use, which is precisely where it is most -unacceptable. Therefore, we have designed this version of the GPL to prohibit -the practice for those products. If such problems arise substantially in other -domains, we stand ready to extend this provision to those domains in future -versions of the GPL, as needed to protect the freedom of users. - -Finally, every program is threatened constantly by software patents. States -should not allow patents to restrict development and use of software on -general-purpose computers, but in those that do, we wish to avoid the special -danger that patents applied to a free program could make it effectively -proprietary. To prevent this, the GPL assures that patents cannot be used to -render the program non-free. - -The precise terms and conditions for copying, distribution and modification -follow. - -## TERMS AND CONDITIONS - -### 0. Definitions - -"This License" refers to version 3 of the GNU General Public License. - -"Copyright" also means copyright-like laws that apply to other kinds of works, -such as semiconductor masks. - -"The Program" refers to any copyrightable work licensed under this License. Each -licensee is addressed as "you". "Licensees" and "recipients" may be individuals -or organizations. - -To "modify" a work means to copy from or adapt all or part of the work in a -fashion requiring copyright permission, other than the making of an exact copy. -The resulting work is called a "modified version" of the earlier work or a work -"based on" the earlier work. - -A "covered work" means either the unmodified Program or a work based on the -Program. - -To "propagate" a work means to do anything with it that, without permission, -would make you directly or secondarily liable for infringement under applicable -copyright law, except executing it on a computer or modifying a private copy. -Propagation includes copying, distribution (with or without modification), -making available to the public, and in some countries other activities as well. - -To "convey" a work means any kind of propagation that enables other parties to -make or receive copies. Mere interaction with a user through a computer network, -with no transfer of a copy, is not conveying. - -An interactive user interface displays "Appropriate Legal Notices" to the extent -that it includes a convenient and prominently visible feature that **(1)** -displays an appropriate copyright notice, and **(2)** tells the user that there -is no warranty for the work (except to the extent that warranties are provided), -that licensees may convey the work under this License, and how to view a copy of -this License. If the interface presents a list of user commands or options, such -as a menu, a prominent item in the list meets this criterion. - -### 1. Source Code - -The "source code" for a work means the preferred form of the work for making -modifications to it. "Object code" means any non-source form of a work. - -A "Standard Interface" means an interface that either is an official standard -defined by a recognized standards body, or, in the case of interfaces specified -for a particular programming language, one that is widely used among developers -working in that language. - -The "System Libraries" of an executable work include anything, other than the -work as a whole, that **(a)** is included in the normal form of packaging a -Major Component, but which is not part of that Major Component, and **(b)** -serves only to enable use of the work with that Major Component, or to implement -a Standard Interface for which an implementation is available to the public in -source code form. A "Major Component", in this context, means a major essential -component (kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to produce the -work, or an object code interpreter used to run it. - -The "Corresponding Source" for a work in object code form means all the source -code needed to generate, install, and (for an executable work) run the object -code and to modify the work, including scripts to control those activities. -However, it does not include the work's System Libraries, or general-purpose -tools or generally available free programs which are used unmodified in -performing those activities but which are not part of the work. For example, -Corresponding Source includes interface definition files associated with source -files for the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, such as by -intimate data communication or control flow between those subprograms and other -parts of the work. - -The Corresponding Source need not include anything that users can regenerate -automatically from other parts of the Corresponding Source. - -The Corresponding Source for a work in source code form is that same work. - -### 2. Basic Permissions - -All rights granted under this License are granted for the term of copyright on -the Program, and are irrevocable provided the stated conditions are met. This -License explicitly affirms your unlimited permission to run the unmodified -Program. The output from running a covered work is covered by this License only -if the output, given its content, constitutes a covered work. This License -acknowledges your rights of fair use or other equivalent, as provided by -copyright law. - -You may make, run and propagate covered works that you do not convey, without -conditions so long as your license otherwise remains in force. You may convey -covered works to others for the sole purpose of having them make modifications -exclusively for you, or provide you with facilities for running those works, -provided that you comply with the terms of this License in conveying all -material for which you do not control copyright. Those thus making or running -the covered works for you must do so exclusively on your behalf, under your -direction and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - -Conveying under any other circumstances is permitted solely under the conditions -stated below. Sublicensing is not allowed; section 10 makes it unnecessary. - -### 3. Protecting Users' Legal Rights From Anti-Circumvention Law - -No covered work shall be deemed part of an effective technological measure under -any applicable law fulfilling obligations under article 11 of the WIPO copyright -treaty adopted on 20 December 1996, or similar laws prohibiting or restricting -circumvention of such measures. - -When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention is -effected by exercising rights under this License with respect to the covered -work, and you disclaim any intention to limit operation or modification of the -work as a means of enforcing, against the work's users, your or third parties' -legal rights to forbid circumvention of technological measures. - -### 4. Conveying Verbatim Copies - -You may convey verbatim copies of the Program's source code as you receive it, -in any medium, provided that you conspicuously and appropriately publish on each -copy an appropriate copyright notice; keep intact all notices stating that this -License and any non-permissive terms added in accord with section 7 apply to the -code; keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - -You may charge any price or no price for each copy that you convey, and you may -offer support or warranty protection for a fee. - -### 5. Conveying Modified Source Versions - -You may convey a work based on the Program, or the modifications to produce it -from the Program, in the form of source code under the terms of section 4, -provided that you also meet all of these conditions: - -- **a)** The work must carry prominent notices stating that you modified it, and - giving a relevant date. -- **b)** The work must carry prominent notices stating that it is released under - this License and any conditions added under section 7. This requirement - modifies the requirement in section 4 to "keep intact all notices". -- **c)** You must license the entire work, as a whole, under this License to - anyone who comes into possession of a copy. This License will therefore apply, - along with any applicable section 7 additional terms, to the whole of the - work, and all its parts, regardless of how they are packaged. This License - gives no permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. -- **d)** If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive interfaces - that do not display Appropriate Legal Notices, your work need not make them do - so. - -A compilation of a covered work with other separate and independent works, which -are not by their nature extensions of the covered work, and which are not -combined with it such as to form a larger program, in or on a volume of a -storage or distribution medium, is called an "aggregate" if the compilation and -its resulting copyright are not used to limit the access or legal rights of the -compilation's users beyond what the individual works permit. Inclusion of a -covered work in an aggregate does not cause this License to apply to the other -parts of the aggregate. - -### 6. Conveying Non-Source Forms - -You may convey a covered work in object code form under the terms of sections 4 -and 5, provided that you also convey the machine-readable Corresponding Source -under the terms of this License, in one of these ways: - -- **a)** Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the Corresponding - Source fixed on a durable physical medium customarily used for software - interchange. -- **b)** Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a written offer, - valid for at least three years and valid for as long as you offer spare parts - or customer support for that product model, to give anyone who possesses the - object code either **(1)** a copy of the Corresponding Source for all the - software in the product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no more than - your reasonable cost of physically performing this conveying of source, or - **(2)** access to copy the Corresponding Source from a network server at no - charge. -- **c)** Convey individual copies of the object code with a copy of the written - offer to provide the Corresponding Source. This alternative is allowed only - occasionally and noncommercially, and only if you received the object code - with such an offer, in accord with subsection 6b. -- **d)** Convey the object code by offering access from a designated place - (gratis or for a charge), and offer equivalent access to the Corresponding - Source in the same way through the same place at no further charge. You need - not require recipients to copy the Corresponding Source along with the object - code. If the place to copy the object code is a network server, the - Corresponding Source may be on a different server (operated by you or a third - party) that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the Corresponding - Source, you remain obligated to ensure that it is available for as long as - needed to satisfy these requirements. -- **e)** Convey the object code using peer-to-peer transmission, provided you - inform other peers where the object code and Corresponding Source of the work - are being offered to the general public at no charge under subsection 6d. - -A separable portion of the object code, whose source code is excluded from the -Corresponding Source as a System Library, need not be included in conveying the -object code work. - -A "User Product" is either **(1)** a "consumer product", which means any -tangible personal property which is normally used for personal, family, or -household purposes, or **(2)** anything designed or sold for incorporation into -a dwelling. In determining whether a product is a consumer product, doubtful -cases shall be resolved in favor of coverage. For a particular product received -by a particular user, "normally used" refers to a typical or common use of that -class of product, regardless of the status of the particular user or of the way -in which the particular user actually uses, or expects or is expected to use, -the product. A product is a consumer product regardless of whether the product -has substantial commercial, industrial or non-consumer uses, unless such uses -represent the only significant mode of use of the product. - -"Installation Information" for a User Product means any methods, procedures, -authorization keys, or other information required to install and execute -modified versions of a covered work in that User Product from a modified version -of its Corresponding Source. The information must suffice to ensure that the -continued functioning of the modified object code is in no case prevented or -interfered with solely because modification has been made. - -If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as part of a -transaction in which the right of possession and use of the User Product is -transferred to the recipient in perpetuity or for a fixed term (regardless of -how the transaction is characterized), the Corresponding Source conveyed under -this section must be accompanied by the Installation Information. But this -requirement does not apply if neither you nor any third party retains the -ability to install modified object code on the User Product (for example, the -work has been installed in ROM). - -The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates for a -work that has been modified or installed by the recipient, or for the User -Product in which it has been modified or installed. Access to a network may be -denied when the modification itself materially and adversely affects the -operation of the network or violates the rules and protocols for communication -across the network. - -Corresponding Source conveyed, and Installation Information provided, in accord -with this section must be in a format that is publicly documented (and with an -implementation available to the public in source code form), and must require no -special password or key for unpacking, reading or copying. - -### 7. Additional Terms - -"Additional permissions" are terms that supplement the terms of this License by -making exceptions from one or more of its conditions. Additional permissions -that are applicable to the entire Program shall be treated as though they were -included in this License, to the extent that they are valid under applicable -law. If additional permissions apply only to part of the Program, that part may -be used separately under those permissions, but the entire Program remains -governed by this License without regard to the additional permissions. - -When you convey a copy of a covered work, you may at your option remove any -additional permissions from that copy, or from any part of it. (Additional -permissions may be written to require their own removal in certain cases when -you modify the work.) You may place additional permissions on material, added by -you to a covered work, for which you have or can give appropriate copyright -permission. - -Notwithstanding any other provision of this License, for material you add to a -covered work, you may (if authorized by the copyright holders of that material) -supplement the terms of this License with terms: - -- **a)** Disclaiming warranty or limiting liability differently from the terms - of sections 15 and 16 of this License; or -- **b)** Requiring preservation of specified reasonable legal notices or author - attributions in that material or in the Appropriate Legal Notices displayed by - works containing it; or -- **c)** Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in reasonable ways - as different from the original version; or -- **d)** Limiting the use for publicity purposes of names of licensors or - authors of the material; or -- **e)** Declining to grant rights under trademark law for use of some trade - names, trademarks, or service marks; or -- **f)** Requiring indemnification of licensors and authors of that material by - anyone who conveys the material (or modified versions of it) with contractual - assumptions of liability to the recipient, for any liability that these - contractual assumptions directly impose on those licensors and authors. - -All other non-permissive additional terms are considered "further restrictions" -within the meaning of section 10. If the Program as you received it, or any part -of it, contains a notice stating that it is governed by this License along with -a term that is a further restriction, you may remove that term. If a license -document contains a further restriction but permits relicensing or conveying -under this License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does not survive -such relicensing or conveying. - -If you add terms to a covered work in accord with this section, you must place, -in the relevant source files, a statement of the additional terms that apply to -those files, or a notice indicating where to find the applicable terms. - -Additional terms, permissive or non-permissive, may be stated in the form of a -separately written license, or stated as exceptions; the above requirements -apply either way. - -### 8. Termination - -You may not propagate or modify a covered work except as expressly provided -under this License. Any attempt otherwise to propagate or modify it is void, and -will automatically terminate your rights under this License (including any -patent licenses granted under the third paragraph of section 11). - -However, if you cease all violation of this License, then your license from a -particular copyright holder is reinstated **(a)** provisionally, unless and -until the copyright holder explicitly and finally terminates your license, and -**(b)** permanently, if the copyright holder fails to notify you of the -violation by some reasonable means prior to 60 days after the cessation. - -Moreover, your license from a particular copyright holder is reinstated -permanently if the copyright holder notifies you of the violation by some -reasonable means, this is the first time you have received notice of violation -of this License (for any work) from that copyright holder, and you cure the -violation prior to 30 days after your receipt of the notice. - -Termination of your rights under this section does not terminate the licenses of -parties who have received copies or rights from you under this License. If your -rights have been terminated and not permanently reinstated, you do not qualify -to receive new licenses for the same material under section 10. - -### 9. Acceptance Not Required for Having Copies - -You are not required to accept this License in order to receive or run a copy of -the Program. Ancillary propagation of a covered work occurring solely as a -consequence of using peer-to-peer transmission to receive a copy likewise does -not require acceptance. However, nothing other than this License grants you -permission to propagate or modify any covered work. These actions infringe -copyright if you do not accept this License. Therefore, by modifying or -propagating a covered work, you indicate your acceptance of this License to do -so. - -### 10. Automatic Licensing of Downstream Recipients - -Each time you convey a covered work, the recipient automatically receives a -license from the original licensors, to run, modify and propagate that work, -subject to this License. You are not responsible for enforcing compliance by -third parties with this License. - -An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered work results -from an entity transaction, each party to that transaction who receives a copy -of the work also receives whatever licenses to the work the party's predecessor -in interest had or could give under the previous paragraph, plus a right to -possession of the Corresponding Source of the work from the predecessor in -interest, if the predecessor has it or can get it with reasonable efforts. - -You may not impose any further restrictions on the exercise of the rights -granted or affirmed under this License. For example, you may not impose a -license fee, royalty, or other charge for exercise of rights granted under this -License, and you may not initiate litigation (including a cross-claim or -counterclaim in a lawsuit) alleging that any patent claim is infringed by -making, using, selling, offering for sale, or importing the Program or any -portion of it. - -### 11. Patents - -A "contributor" is a copyright holder who authorizes use under this License of -the Program or a work on which the Program is based. The work thus licensed is -called the contributor's "contributor version". - -A contributor's "essential patent claims" are all patent claims owned or -controlled by the contributor, whether already acquired or hereafter acquired, -that would be infringed by some manner, permitted by this License, of making, -using, or selling its contributor version, but do not include claims that would -be infringed only as a consequence of further modification of the contributor -version. For purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of this License. - -Each contributor grants you a non-exclusive, worldwide, royalty-free patent -license under the contributor's essential patent claims, to make, use, sell, -offer for sale, import and otherwise run, modify and propagate the contents of -its contributor version. - -In the following three paragraphs, a "patent license" is any express agreement -or commitment, however denominated, not to enforce a patent (such as an express -permission to practice a patent or covenant not to sue for patent infringement). -To "grant" such a patent license to a party means to make such an agreement or -commitment not to enforce a patent against the party. - -If you convey a covered work, knowingly relying on a patent license, and the -Corresponding Source of the work is not available for anyone to copy, free of -charge and under the terms of this License, through a publicly available network -server or other readily accessible means, then you must either **(1)** cause the -Corresponding Source to be so available, or **(2)** arrange to deprive yourself -of the benefit of the patent license for this particular work, or **(3)** -arrange, in a manner consistent with the requirements of this License, to extend -the patent license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the covered -work in a country, or your recipient's use of the covered work in a country, -would infringe one or more identifiable patents in that country that you have -reason to believe are valid. - -If, pursuant to or in connection with a single transaction or arrangement, you -convey, or propagate by procuring conveyance of, a covered work, and grant a -patent license to some of the parties receiving the covered work authorizing -them to use, propagate, modify or convey a specific copy of the covered work, -then the patent license you grant is automatically extended to all recipients of -the covered work and works based on it. - -A patent license is "discriminatory" if it does not include within the scope of -its coverage, prohibits the exercise of, or is conditioned on the non-exercise -of one or more of the rights that are specifically granted under this License. -You may not convey a covered work if you are a party to an arrangement with a -third party that is in the business of distributing software, under which you -make payment to the third party based on the extent of your activity of -conveying the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory patent -license **(a)** in connection with copies of the covered work conveyed by you -(or copies made from those copies), or **(b)** primarily for and in connection -with specific products or compilations that contain the covered work, unless you -entered into that arrangement, or that patent license was granted, prior to 28 -March 2007. - -Nothing in this License shall be construed as excluding or limiting any implied -license or other defenses to infringement that may otherwise be available to you -under applicable patent law. - -### 12. No Surrender of Others' Freedom - -If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not excuse -you from the conditions of this License. If you cannot convey a covered work so -as to satisfy simultaneously your obligations under this License and any other -pertinent obligations, then as a consequence you may not convey it at all. For -example, if you agree to terms that obligate you to collect a royalty for -further conveying from those to whom you convey the Program, the only way you -could satisfy both those terms and this License would be to refrain entirely -from conveying the Program. - -### 13. Use with the GNU Affero General Public License - -Notwithstanding any other provision of this License, you have permission to link -or combine any covered work with a work licensed under version 3 of the GNU -Affero General Public License into a single combined work, and to convey the -resulting work. The terms of this License will continue to apply to the part -which is the covered work, but the special requirements of the GNU Affero -General Public License, section 13, concerning interaction through a network -will apply to the combination as such. - -### 14. Revised Versions of this License - -The Free Software Foundation may publish revised and/or new versions of the GNU -General Public License from time to time. Such new versions will be similar in -spirit to the present version, but may differ in detail to address new problems -or concerns. - -Each version is given a distinguishing version number. If the Program specifies -that a certain numbered version of the GNU General Public License "or any later -version" applies to it, you have the option of following the terms and -conditions either of that numbered version or of any later version published by -the Free Software Foundation. If the Program does not specify a version number -of the GNU General Public License, you may choose any version ever published by -the Free Software Foundation. - -If the Program specifies that a proxy can decide which future versions of the -GNU General Public License can be used, that proxy's public statement of -acceptance of a version permanently authorizes you to choose that version for -the Program. - -Later license versions may give you additional or different permissions. -However, no additional obligations are imposed on any author or copyright holder -as a result of your choosing to follow a later version. - -### 15. Disclaimer of Warranty - -THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER -PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER -EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE -QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE -DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - -### 16. Limitation of Liability - -IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY -COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS -PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, -INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE -THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED -INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE -PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY -HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - -### 17. Interpretation of Sections 15 and 16 - -If the disclaimer of warranty and limitation of liability provided above cannot -be given local legal effect according to their terms, reviewing courts shall -apply local law that most closely approximates an absolute waiver of all civil -liability in connection with the Program, unless a warranty or assumption of -liability accompanies a copy of the Program in return for a fee. - -_END OF TERMS AND CONDITIONS_ - -## How to Apply These Terms to Your New Programs - -If you develop a new program, and you want it to be of the greatest possible use -to the public, the best way to achieve this is to make it free software which -everyone can redistribute and change under these terms. - -To do so, attach the following notices to the program. It is safest to attach -them to the start of each source file to most effectively state the exclusion of -warranty; and each file should have at least the "copyright" line and a pointer -to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - -If the program does terminal interaction, make it output a short notice like -this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type 'show c' for details. - -The hypothetical commands `show w` and `show c` should show the appropriate -parts of the General Public License. Of course, your program's commands might be -different; for a GUI interface, you would use an "about box". - -You should also get your employer (if you work as a programmer) or school, if -any, to sign a "copyright disclaimer" for the program, if necessary. For more -information on this, and how to apply and follow the GNU GPL, see -<>. - -The GNU General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may consider -it more useful to permit linking proprietary applications with the library. If -this is what you want to do, use the GNU Lesser General Public License instead -of this License. But first, please read -<>. diff --git a/spank/mqt-core-qdmi-spank.map b/spank/mqt-core-qdmi-spank.map deleted file mode 100644 index b65e7bfe2d..0000000000 --- a/spank/mqt-core-qdmi-spank.map +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH - * All rights reserved. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -{ - global: - plugin_name; - plugin_type; - plugin_version; - spank_plugin_version; - slurm_spank_init_failure_mode; - slurm_spank_init; - slurm_spank_user_init; - slurm_spank_task_init; - local: - *; -}; diff --git a/spank/spank.cpp b/spank/spank.cpp deleted file mode 100644 index e24dd121f1..0000000000 --- a/spank/spank.cpp +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH - * All rights reserved. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -/// @file spank.cpp -/// @brief Transport administrator-declared configuration references to QDMI -/// jobs. - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -extern "C" { -#include -#include -#include -} - -static_assert(SLURM_VERSION_NUMBER >= SLURM_VERSION_NUM(25, 11, 0), - "The MQT Core SPANK component requires Slurm 25.11 or newer"); - -// Names and signatures in this block are fixed by Slurm's plugin ABI. -// NOLINTBEGIN(readability-identifier-naming) -extern "C" { -extern const char plugin_name[] = "mqt_core_qdmi"; -extern const char plugin_type[] = "spank"; -extern const unsigned int plugin_version = SLURM_VERSION_NUMBER; -extern const unsigned int spank_plugin_version = 1; -int slurm_spank_init_failure_mode = - ESPANK_JOB_FAILURE; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -} -// NOLINTEND(readability-identifier-naming) - -namespace { -constexpr auto LICENSE_ENVIRONMENT = "SLURM_JOB_LICENSES"; -constexpr auto CATALOGUE_ENVIRONMENT = "MQT_CORE_QDMI_CONFIG_FILE"; -constexpr size_t MAX_VALUE_SIZE = 4095; -constexpr int TASK_REJECTED = -1; - -void fail(const char* message) { - // Slurm exposes logging through a variadic C ABI. - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - slurm_spank_log("mqt-core-qdmi: %s", message); -} - -bool validValue(const std::string_view value) { - return !value.empty() && value.size() <= MAX_VALUE_SIZE && - value.find_first_of("\r\n") == std::string_view::npos; -} - -auto jobEnvironment(spank_t spank, const char* name) - -> std::optional { - // S_JOB_ENV requires a mutable char*** output parameter. - char** values = nullptr; // NOLINT(misc-const-correctness) - // Slurm exposes item lookup through a variadic C ABI. - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - if (spank_get_item(spank, S_JOB_ENV, &values) != ESPANK_SUCCESS || - values == nullptr) { - throw std::runtime_error("could not read the job environment"); - } - const auto prefix = std::string{name} + '='; - // S_JOB_ENV is a borrowed, null-terminated array owned by Slurm. - // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) - for (size_t index = 0; values[index] != nullptr; ++index) { - const std::string_view entry{values[index]}; - if (entry.starts_with(prefix)) { - return std::string{entry.substr(prefix.size())}; - } - } - // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) - return std::nullopt; -} - -auto licenseIds(std::string_view value) -> std::vector { - std::vector ids; - do { - const auto separator = value.find(','); - const auto id = value.substr(0, separator); - if (!validValue(id) || - id.find_first_of(" \t:|@") != std::string_view::npos || - std::ranges::find(ids, id) != ids.end()) { - throw std::runtime_error("invalid or duplicate configured license ID"); - } - ids.emplace_back(id); - if (separator == std::string_view::npos) { - return ids; - } - value.remove_prefix(separator + 1); - } while (true); -} - -bool validEnvironmentName(const std::string_view name) { - constexpr std::string_view letters = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; - constexpr std::string_view characters = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"; - return !name.empty() && - letters.find(name.front()) != std::string_view::npos && - name.find_first_not_of(characters) == std::string_view::npos; -} - -struct Reference { - std::string environment; - std::vector licenses; - std::string defaultValue; -}; - -class Configuration final { -public: - void parse(const int count, char** arguments) { - for (const auto* rawArgument : - std::span{arguments, static_cast(count)}) { - if (rawArgument == nullptr || !validValue(rawArgument)) { - throw std::runtime_error("malformed plugstack argument"); - } - const std::string_view argument{rawArgument}; - const auto separator = argument.find('='); - if (separator == std::string_view::npos) { - throw std::runtime_error("plugstack arguments must use key=value"); - } - const auto key = argument.substr(0, separator); - const auto value = argument.substr(separator + 1); - if (key == "licenses" && licenses_.empty()) { - licenses_ = licenseIds(value); - } else if (key == "qdmi_config_file" && !catalogueDefault_) { - if (!validValue(value)) { - throw std::runtime_error("invalid catalogue reference"); - } - catalogueDefault_ = value; - } else if (key == "reference") { - const auto nameEnd = value.find(':'); - const auto idsEnd = - value.find(':', nameEnd == std::string_view::npos ? value.size() - : nameEnd + 1); - if (nameEnd == std::string_view::npos || - idsEnd == std::string_view::npos) { - throw std::runtime_error("reference must use ENV:LICENSES:DEFAULT"); - } - const auto name = value.substr(0, nameEnd); - if (!validEnvironmentName(name) || name == CATALOGUE_ENVIRONMENT || - name == LICENSE_ENVIRONMENT || - std::ranges::any_of(references_, [&](const auto& reference) { - return reference.environment == name; - })) { - throw std::runtime_error("invalid or duplicate reference name"); - } - const auto defaultValue = value.substr(idsEnd + 1); - if (!validValue(defaultValue)) { - throw std::runtime_error("invalid default reference value"); - } - references_.push_back({ - .environment = std::string{name}, - .licenses = - licenseIds(value.substr(nameEnd + 1, idsEnd - nameEnd - 1)), - .defaultValue = std::string{defaultValue}, - }); - } else { - throw std::runtime_error("unknown or repeated plugstack argument"); - } - } - if (licenses_.empty()) { - throw std::runtime_error("configure concrete license IDs with licenses="); - } - for (const auto& reference : references_) { - for (const auto& id : reference.licenses) { - if (std::ranges::find(licenses_, id) == licenses_.end()) { - throw std::runtime_error("reference names an unconfigured license"); - } - } - } - } - - void inject(spank_t spank) const { - const auto expression = jobEnvironment(spank, LICENSE_ENVIRONMENT); - const auto selected = std::ranges::find_if(licenses_, [&](const auto& id) { - return expression && (*expression == id || *expression == id + ":1"); - }); - if (selected == licenses_.end()) { - return; - } - - if (catalogueDefault_) { - apply(spank, CATALOGUE_ENVIRONMENT, *catalogueDefault_); - } - for (const auto& reference : references_) { - if (std::ranges::find(reference.licenses, *selected) != - reference.licenses.end()) { - apply(spank, reference.environment.c_str(), reference.defaultValue); - } - } - } - -private: - static void apply(spank_t spank, const char* environment, - const std::string& defaultValue) { - const auto value = jobEnvironment(spank, environment); - if (value && !validValue(*value)) { - throw std::runtime_error( - "job environment value is malformed or too long"); - } - if (value) { - return; - } - const auto result = - spank_setenv(spank, environment, defaultValue.c_str(), 0); - if (result != ESPANK_SUCCESS && result != ESPANK_ENV_EXISTS) { - throw std::runtime_error("failed to set a default QDMI reference"); - } - } - - std::vector licenses_; - std::vector references_; - std::optional catalogueDefault_; -}; - -Configuration - configuration; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -bool licenseEnvironmentReady = true; - -} // namespace - -// Names and signatures are fixed by Slurm's plugin ABI. -// NOLINTBEGIN(misc-use-internal-linkage, readability-identifier-naming, -// cppcoreguidelines-avoid-c-arrays) -extern "C" { -int slurm_spank_init(spank_t /*spank*/, const int count, char* arguments[]) { - try { - configuration = Configuration{}; - licenseEnvironmentReady = true; - configuration.parse(count, arguments); - return ESPANK_SUCCESS; - } catch (const std::exception& error) { - fail(error.what()); - } catch (...) { - fail("QDMI SPANK initialization failed"); - } - return TASK_REJECTED; -} - -int slurm_spank_user_init(spank_t spank, int /*count*/, char* /*arguments*/[]) { - if (spank_remote(spank) == 1) { - // Slurm restores allocated licenses before task-init. A submitted value - // must not make a job with no allocated license match a configured device. - const auto result = spank_unsetenv(spank, LICENSE_ENVIRONMENT); - if (result != ESPANK_SUCCESS && result != ESPANK_ENV_NOEXIST) { - // Fail in task-init: a user-init error can drain the node. - licenseEnvironmentReady = false; - fail("could not clear inherited license selection"); - } - } - return ESPANK_SUCCESS; -} - -int slurm_spank_task_init(spank_t spank, int /*count*/, char* /*arguments*/[]) { - if (spank_remote(spank) != 1) { - return ESPANK_SUCCESS; - } - try { - if (!licenseEnvironmentReady) { - throw std::runtime_error("could not clear inherited license selection"); - } - configuration.inject(spank); - return ESPANK_SUCCESS; - } catch (const std::exception& error) { - fail(error.what()); - } catch (...) { - fail("QDMI configuration injection failed"); - } - return TASK_REJECTED; -} -} -// NOLINTEND(misc-use-internal-linkage, readability-identifier-naming, -// cppcoreguidelines-avoid-c-arrays) diff --git a/src/qdmi/CMakeLists.txt b/src/qdmi/CMakeLists.txt index 995976d166..a908a65355 100644 --- a/src/qdmi/CMakeLists.txt +++ b/src/qdmi/CMakeLists.txt @@ -40,10 +40,16 @@ if(NOT TARGET ${TARGET_NAME}) list(APPEND MQT_CORE_TARGETS ${TARGET_NAME}) endif() -if(UNIX) +if(UNIX OR WIN32) add_executable(mqt-core-qdmi-check Check.cpp) target_link_libraries(mqt-core-qdmi-check PRIVATE MQT::CoreQDMI MQT::ProjectWarnings MQT::ProjectOptions) + if(WIN32) + target_compile_definitions(mqt-core-qdmi-check PRIVATE NOMINMAX _WIN32_WINNT=0x0A00) + if(MINGW) + target_link_options(mqt-core-qdmi-check PRIVATE -municode) + endif() + endif() mqt_copy_qdmi_runtime(mqt-core-qdmi-check) if(MQT_CORE_INSTALL) install(TARGETS mqt-core-qdmi-check RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" diff --git a/src/qdmi/Check.cpp b/src/qdmi/Check.cpp index d8238e0791..92b2294be0 100644 --- a/src/qdmi/Check.cpp +++ b/src/qdmi/Check.cpp @@ -9,9 +9,14 @@ */ #include "qdmi/QDMI.hpp" +#include "qdmi/common/Common.hpp" #include "qdmi/constants.h" +#ifdef _WIN32 +#include +#include +#else #include // POSIX signal handling is not part of the C++ interface. #include // NOLINT(modernize-deprecated-headers) @@ -19,6 +24,7 @@ #include #include #include +#endif #ifdef __linux__ #include @@ -28,31 +34,45 @@ #include #include #include +#include #include #include +#include #include #include #include #include #include #include +#include namespace { // A signal handler may only communicate through sig_atomic_t. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -volatile sig_atomic_t interrupted = 0; +volatile std::sig_atomic_t interrupted = 0; extern "C" void handleSignal(const int signal) { interrupted = signal; } constexpr std::string_view USAGE = - "Usage: mqt-core-qdmi-check --device ID [--timeout SECONDS]\n" + "Usage: mqt-core-qdmi-check --device ID [--timeout SECONDS] " + "[--manifest PATH]...\n" "Probe whether a registered QDMI device is operational (default timeout: " "30 seconds).\n" "Exit codes: 0 available, 1 failed, 2 invalid arguments, 124 timeout.\n"; -[[nodiscard]] auto check(const std::string& id) -> int { +[[nodiscard]] auto check(const std::string& id, + const std::span manifests) + -> int { try { + for (const auto& manifest : manifests) { + try { + qdmi::builtin_driver::addManifest(manifest); + } catch (...) { + // Match Python discovery's best-effort handling of installed manifests. + continue; + } + } const auto device = qdmi::Session::openDevice(id); const auto status = device.getStatus(); return status == QDMI_DEVICE_STATUS_IDLE || @@ -64,8 +84,114 @@ constexpr std::string_view USAGE = } } -[[nodiscard]] auto supervise(const std::string& id, - const std::chrono::seconds timeout) -> int { +#ifdef _WIN32 +[[nodiscard]] auto supervise(const std::chrono::seconds timeout) -> int { + if (std::signal(SIGTERM, handleSignal) == SIG_ERR || + std::signal(SIGINT, handleSignal) == SIG_ERR) { + return 1; + } + const auto deadline = std::chrono::steady_clock::now() + timeout; + using Handle = std::unique_ptr; + const Handle job(CreateJobObjectW(nullptr, nullptr), CloseHandle); + JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits{}; + limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + if (!job || + !SetInformationJobObject(job.get(), JobObjectExtendedLimitInformation, + &limits, sizeof(limits))) { + return 1; + } + + std::wstring executable(MAX_PATH, L'\0'); + while (true) { + const auto length = GetModuleFileNameW( + nullptr, executable.data(), static_cast(executable.size())); + if (length == 0) { + return 1; + } + if (length < executable.size()) { + executable.resize(length); + break; + } + executable.resize(executable.size() * 2); + } + // Preserve the original argument quoting after replacing argv[0]. + std::wstring_view arguments(GetCommandLineW()); + bool quoted = false; + while (!arguments.empty()) { + const auto character = arguments.front(); + if (!quoted && (character == L' ' || character == L'\t')) { + break; + } + if (character == L'"') { + quoted = !quoted; + } + arguments.remove_prefix(1); + } + auto command = L"\"" + executable + L"\" --worker" + std::wstring(arguments); + SECURITY_ATTRIBUTES security{}; + security.nLength = sizeof(security); + security.bInheritHandle = TRUE; + const auto sink = CreateFileW(L"NUL", GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, &security, + OPEN_EXISTING, 0, nullptr); + if (sink == INVALID_HANDLE_VALUE) { + return 1; + } + const Handle output(sink, CloseHandle); + STARTUPINFOEXW startup{}; + startup.StartupInfo.cb = sizeof(startup); + startup.StartupInfo.dwFlags = STARTF_USESTDHANDLES; + startup.StartupInfo.hStdInput = output.get(); + startup.StartupInfo.hStdOutput = output.get(); + startup.StartupInfo.hStdError = output.get(); + SIZE_T attributeBytes = 0; + InitializeProcThreadAttributeList(nullptr, 1, 0, &attributeBytes); + std::vector attributes(attributeBytes); + startup.lpAttributeList = + reinterpret_cast(attributes.data()); + if (!InitializeProcThreadAttributeList(startup.lpAttributeList, 1, 0, + &attributeBytes)) { + return 1; + } + // Assign the job during creation so supervisor death cannot orphan a worker. + auto jobHandle = job.get(); + PROCESS_INFORMATION information{}; + const auto created = + UpdateProcThreadAttribute(startup.lpAttributeList, 0, + PROC_THREAD_ATTRIBUTE_JOB_LIST, &jobHandle, + sizeof(jobHandle), nullptr, nullptr) && + CreateProcessW(executable.c_str(), command.data(), nullptr, nullptr, TRUE, + EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW, nullptr, + nullptr, &startup.StartupInfo, &information); + DeleteProcThreadAttributeList(startup.lpAttributeList); + if (!created) { + return 1; + } + const Handle process(information.hProcess, CloseHandle); + const Handle thread(information.hThread, CloseHandle); + int result = 1; + while (interrupted == 0) { + const auto waited = WaitForSingleObject(process.get(), 10); + if (waited == WAIT_OBJECT_0) { + DWORD status = 1; + return GetExitCodeProcess(process.get(), &status) && status == 0 ? 0 : 1; + } + if (waited == WAIT_FAILED) { + break; + } + if (std::chrono::steady_clock::now() >= deadline) { + result = 124; + break; + } + } + TerminateJobObject(job.get(), 1); + WaitForSingleObject(process.get(), INFINITE); + return result; +} +#else +[[nodiscard]] auto +supervise(const std::string& id, const std::chrono::seconds timeout, + const std::span manifests) -> int { struct sigaction action{}; action.sa_handler = handleSignal; sigemptyset(&action.sa_mask); @@ -109,7 +235,7 @@ constexpr std::string_view USAGE = close(sink); } // Run process-exit handlers within the worker deadline. - std::exit(check(id)); // NOLINT(concurrency-mt-unsafe) + std::exit(check(id, manifests)); // NOLINT(concurrency-mt-unsafe) } // Either side can establish the group before the child loads a device. @@ -142,20 +268,38 @@ constexpr std::string_view USAGE = } return result; } +#endif } // namespace +#ifdef _WIN32 +int wmain(const int argc, wchar_t** argv) try { + std::vector utf8Arguments; + for (const auto* argument : std::span(argv, static_cast(argc))) { + utf8Arguments.emplace_back(qdmi::detail::pathToString(argument)); + } + const auto arguments = std::span(utf8Arguments); + bool worker = false; +#else int main(const int argc, char** argv) try { const auto arguments = std::span(argv, static_cast(argc)); +#endif if (argc == 2 && std::string_view(arguments[1]) == "--help") { std::cout << USAGE; return 0; } std::string id; + std::vector manifests; int seconds = 30; bool hasTimeout = false; for (size_t i = 1; i < arguments.size(); ++i) { const std::string_view option(arguments[i]); +#ifdef _WIN32 + if (option == "--worker" && !worker) { + worker = true; + continue; + } +#endif if (++i == arguments.size()) { std::cerr << USAGE; return 2; @@ -163,6 +307,8 @@ int main(const int argc, char** argv) try { const std::string_view value(arguments[i]); if (option == "--device" && id.empty() && !value.empty()) { id = value; + } else if (option == "--manifest" && !value.empty()) { + manifests.emplace_back(qdmi::detail::pathFromString(value)); } else if (option == "--timeout" && !hasTimeout) { // from_chars requires a pointer range within the string view. // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) @@ -185,7 +331,15 @@ int main(const int argc, char** argv) try { std::cerr << USAGE; return 2; } - const auto result = supervise(id, std::chrono::seconds(seconds)); +#ifdef _WIN32 + if (worker) { + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); + return check(id, manifests); + } + const auto result = supervise(std::chrono::seconds(seconds)); +#else + const auto result = supervise(id, std::chrono::seconds(seconds), manifests); +#endif if (result == 124) { std::cerr << "QDMI device check timed out\n"; } else if (result != 0) { diff --git a/test/python/qdmi/test_discovery.py b/test/python/qdmi/test_discovery.py index 69a7067dd9..9c17e26839 100644 --- a/test/python/qdmi/test_discovery.py +++ b/test/python/qdmi/test_discovery.py @@ -10,7 +10,9 @@ from __future__ import annotations +import json import sys +from importlib.metadata import distribution from pathlib import Path, PurePosixPath from types import SimpleNamespace from typing import TYPE_CHECKING @@ -22,6 +24,8 @@ if TYPE_CHECKING: from collections.abc import Iterator + from pytest_console_scripts import ScriptRunner + class _Distribution: def __init__(self, root: Path, files: list[str] | None) -> None: @@ -97,3 +101,37 @@ def test_skips_invalid_manifest_metadata( with pytest.warns(RuntimeWarning, match="Skipping QDMI manifest") as warnings: _qdmi_discovery.discover_qdmi_manifests(lambda _: pytest.fail("must skip")) assert len(warnings) == 1 + + +@pytest.mark.script_launch_mode("subprocess") +def test_availability_discovers_installed_provider( + script_runner: ScriptRunner, monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """Discover installed manifests in the native checker without importing providers.""" + core = distribution("mqt-core") + packaged = next(file for file in core.files or () if file.name == "mqt-core-qdmi-sc-device.qdmi.json") + packaged_path = Path(str(core.locate_file(packaged))) + definition = json.loads(packaged_path.read_text(encoding="utf-8"))["qdmi"]["devices"][0] + definition.update(id="test.installed", library=str(packaged_path.parent / definition["library"])) + + provider = tmp_path / "test_qdmi_provider" + provider.mkdir() + (provider / "__init__.py").write_text("raise RuntimeError('provider must not be imported')\n") + (provider / "device.qdmi.json").write_text(json.dumps({"schema-version": 1, "qdmi": {"devices": [definition]}})) + (provider / "invalid.qdmi.json").write_text("{") + metadata = tmp_path / "test_qdmi_provider-0.0.dist-info" + metadata.mkdir() + (metadata / "METADATA").write_text("Name: test-qdmi-provider\nVersion: 0.0\n") + (metadata / "entry_points.txt").write_text("[mqt.core.qdmi.manifests]\nprobe = test_qdmi_provider\n") + (metadata / "RECORD").write_text("test_qdmi_provider/device.qdmi.json,,\ntest_qdmi_provider/invalid.qdmi.json,,\n") + monkeypatch.setenv("PYTHONPATH", str(tmp_path)) + + result = script_runner.run(["mqt-core-qdmi-check", "--device", "test.installed"]) + assert result.success + assert not result.stdout + + monkeypatch.setenv( + "MQT_CORE_QDMI_CONFIG_JSON", + json.dumps({"schema-version": 1, "qdmi": {"devices": [{"id": "test.installed", "enabled": False}]}}), + ) + assert not script_runner.run(["mqt-core-qdmi-check", "--device", "test.installed"]).success diff --git a/test/python/test_cli.py b/test/python/test_cli.py index 1804a507f1..f431b536ce 100644 --- a/test/python/test_cli.py +++ b/test/python/test_cli.py @@ -20,7 +20,6 @@ import pytest from mqt.core import __version__ as mqt_core_version -from mqt.core import _commands # ruff: ignore[import-private-name] if TYPE_CHECKING: from pytest_console_scripts import ScriptRunner @@ -144,7 +143,7 @@ def test_benchmark_cli(script_runner: ScriptRunner) -> None: [ "mqt-cc", "mqt-core-bench", - pytest.param("mqt-core-qdmi-check", marks=pytest.mark.skipif(sys.platform == "win32", reason="POSIX checker")), + "mqt-core-qdmi-check", ], ) def test_native_tool_entry_point(script_runner: ScriptRunner, tool: str) -> None: @@ -158,15 +157,8 @@ def test_native_tool_entry_point(script_runner: ScriptRunner, tool: str) -> None @pytest.mark.script_launch_mode("subprocess") -@pytest.mark.skipif(sys.platform == "win32", reason="POSIX checker") def test_qdmi_availability(script_runner: ScriptRunner) -> None: """Probe a device through the installed command and catalogue.""" result = script_runner.run(["mqt-core-qdmi-check", "--device", "mqt.sc.default"]) assert result.success assert not result.stdout - - -def test_qdmi_availability_windows() -> None: - """Explain the availability command's platform requirement.""" - with patch.object(_commands.sys, "platform", "win32"), pytest.raises(SystemExit, match="Linux or macOS"): - _commands.qdmi_check() diff --git a/test/python/test_slurm_availability.py b/test/python/test_slurm_availability.py new file mode 100644 index 0000000000..36c0407dc0 --- /dev/null +++ b/test/python/test_slurm_availability.py @@ -0,0 +1,84 @@ +# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM +# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH +# All rights reserved. +# +# SPDX-License-Identifier: MIT +# +# Licensed under the MIT License + +"""Check that unavailable probes leave scheduler admission closed.""" + +from __future__ import annotations + +import importlib.util +import json +import subprocess +from pathlib import Path +from typing import TYPE_CHECKING + +import pytest + +if TYPE_CHECKING: + from types import ModuleType + + +@pytest.fixture +def monitor() -> ModuleType: + """Load the standalone administrative example. + + Returns: + The monitor module. + """ + path = Path(__file__).parents[2] / "examples" / "slurm" / "update_availability.py" + spec = importlib.util.spec_from_file_location("slurm_availability", path) + assert spec is not None + assert spec.loader is not None + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize("failure", [1, subprocess.TimeoutExpired("checker", 1), FileNotFoundError("checker")]) +def test_probe_failure_keeps_block_until_recovery( + monitor: ModuleType, monkeypatch: pytest.MonkeyPatch, failure: int | Exception +) -> None: + """Never probe with admission open, and preserve other devices' reservations.""" + reservations = {"another-device": "other:1"} + name = "qdmi-unavailable-example.device" + + def scontrol(*arguments: str) -> str: + if arguments[0] == "--json": + return json.dumps({"reservations": [{"name": key} for key in reservations]}) + fields = dict(argument.split("=", maxsplit=1) for argument in arguments[1:]) + if arguments[0] == "delete": + del reservations[fields["ReservationName"]] + else: + reservations[fields["ReservationName"]] = fields["Licenses"] + return "" + + def probe(*_args: object, **_kwargs: object) -> subprocess.CompletedProcess[str]: + assert reservations[name] == "example.device:2" + if isinstance(failure, Exception): + raise failure + return subprocess.CompletedProcess("checker", failure) + + monkeypatch.setattr(monitor, "scontrol", scontrol) + monkeypatch.setattr(monitor.subprocess, "run", probe) + arguments = ("--license", "example.device:2") + assert monitor.main(arguments) == 1 + assert reservations[name] == "example.device:2" + failure = 0 + assert monitor.main(arguments) == 0 + assert reservations == {"another-device": "other:1"} + + +def test_controller_failure_never_runs_probe(monitor: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None: + """A failed admission update cannot lead to a healthy, open result.""" + + def unavailable(*_arguments: str) -> str: + msg = "scontrol" + raise subprocess.TimeoutExpired(msg, 10) + + monkeypatch.setattr(monitor, "scontrol", unavailable) + monkeypatch.setattr(monitor.subprocess, "run", lambda *_args, **_kwargs: pytest.fail("probe ran before blocking")) + assert monitor.main(("--license", "example.device:2")) == 2 diff --git a/test/python/test_slurm_integration.py b/test/python/test_slurm_integration.py index 96c6a8c3c0..437c02e80f 100644 --- a/test/python/test_slurm_integration.py +++ b/test/python/test_slurm_integration.py @@ -172,7 +172,7 @@ def run(command: tuple[str, ...], **kwargs: object) -> subprocess.CompletedProce def test_provider_options_preserve_command_arguments(tmp_path: Path) -> None: - """Keep workload arguments separate from fixture and reference options.""" + """Keep workload arguments separate from fixture options.""" setup = tmp_path / "setup.sh" setup.touch() options = runner.parse_arguments(( @@ -182,8 +182,6 @@ def test_provider_options_preserve_command_arguments(tmp_path: Path) -> None: "setup.sh", "--device-license", "provider.device", - "--reference", - "PROVIDER_PROFILE=profile=value", "--", "python3", "probe.py", @@ -191,7 +189,6 @@ def test_provider_options_preserve_command_arguments(tmp_path: Path) -> None: "one argument", )) assert options.workload == tmp_path - assert options.reference == ["PROVIDER_PROFILE=profile=value"] assert options.command == ["python3", "probe.py", "--label", "one argument"] @@ -201,8 +198,6 @@ def test_provider_options_preserve_command_arguments(tmp_path: Path) -> None: ("--device-license", "provider.device"), ("--", "python3", "probe.py"), ("--device-license", "provider.device:2", "--", "/bin/true"), - ("--reference", "PROVIDER_PROFILE"), - ("--reference", "PROVIDER_PROFILE=two words"), ], ) def test_invalid_provider_inputs_fail_before_docker(arguments: tuple[str, ...]) -> None: diff --git a/test/qdmi/driver/session_device.cpp b/test/qdmi/driver/session_device.cpp index 72fbf833ab..435c47749c 100644 --- a/test/qdmi/driver/session_device.cpp +++ b/test/qdmi/driver/session_device.cpp @@ -304,6 +304,13 @@ TEST_SESSION_QDMI_device_session_init(QDMI_Device_Session session) { if (parameter(session, QDMI_DEVICE_SESSION_PARAMETER_CUSTOM4) == "hang") { std::this_thread::sleep_for(std::chrono::hours(1)); } + if (parameter(session, QDMI_DEVICE_SESSION_PARAMETER_CUSTOM4) == + "hang-child" && + std::system( + parameter(session, QDMI_DEVICE_SESSION_PARAMETER_CUSTOM5).c_str()) != + 0) { + return QDMI_ERROR_FATAL; + } session->initialized = true; return QDMI_SUCCESS; } diff --git a/test/qdmi/test_check_cli.cmake b/test/qdmi/test_check_cli.cmake index 9698f4a617..923c6e3417 100644 --- a/test/qdmi/test_check_cli.cmake +++ b/test/qdmi/test_check_cli.cmake @@ -35,6 +35,35 @@ foreach(status idle busy offline hang hang-exit) endif() endforeach() +file(REMOVE "${WORK_DIR}/child-started" "${WORK_DIR}/child-survived") +file( + WRITE "${WORK_DIR}/child.cmake" + "file(WRITE \"${WORK_DIR}/child-started\" \"\")\n" + "execute_process(COMMAND \"${CMAKE_COMMAND}\" -E sleep 3)\n" + "file(WRITE \"${WORK_DIR}/child-survived\" \"\")\n") +set(child_command "\"${CMAKE_COMMAND}\" -P \"${WORK_DIR}/child.cmake\"") +if(WIN32) + set(child_command "\"${child_command}\"") +endif() +string(REPLACE "\"" "\\\"" child_command "${child_command}") +file( + WRITE "${configuration}" + "{\"schema-version\":1,\"qdmi\":{\"devices\":[{\"id\":\"test.check\",\"library\":\"${SESSION_DEVICE}\",\"prefix\":\"TEST_SESSION\",\"session\":{\"custom4\":\"hang-child\",\"custom5\":\"${child_command}\"}}]}}" +) +execute_process( + COMMAND "${CMAKE_COMMAND}" -E env --unset=MQT_CORE_QDMI_DRIVER --unset=MQT_CORE_QDMI_CONFIG_JSON + "MQT_CORE_QDMI_CONFIG_FILE=${configuration}" "${CHECKER}" --device test.check --timeout 2 + RESULT_VARIABLE result + OUTPUT_QUIET ERROR_QUIET + TIMEOUT 8) +if(NOT result STREQUAL "124" OR NOT EXISTS "${WORK_DIR}/child-started") + message(FATAL_ERROR "The descendant cleanup test did not reach its timeout: ${result}") +endif() +execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 2) +if(EXISTS "${WORK_DIR}/child-survived") + message(FATAL_ERROR "A device subprocess survived the availability timeout") +endif() + foreach(arguments IN ITEMS "0;--help" "2;--device" "2;--timeout;1" "2;--device;test.check;--timeout;0" "2;--unknown;value" "1;--device;unknown") diff --git a/test/slurm/provider_probe.py b/test/slurm/provider_probe.py index 42d7cb59db..a7c745838f 100644 --- a/test/slurm/provider_probe.py +++ b/test/slurm/provider_probe.py @@ -6,7 +6,7 @@ # # Licensed under the MIT License -"""Common assertions for provider workloads in the shared Slurm fixture.""" +"""Common assertions for QDMI device workloads in the shared Slurm fixture.""" from __future__ import annotations diff --git a/test/slurm/run_integration.py b/test/slurm/run_integration.py index 4905b14c82..2b87ad22e6 100644 --- a/test/slurm/run_integration.py +++ b/test/slurm/run_integration.py @@ -406,37 +406,50 @@ def test_core() -> None: msg = "The SC job did not complete while both DDSIM licenses remained held" raise AssertionError(msg) - (RUNTIME / "jobs" / f"release-{first}").touch() - wait_for("the released first DDSIM job to finish", lambda: job_finished(first)) - wait_for_result("ddsim", third, "the pending third DDSIM job to execute") - wait_for("the third DDSIM job to finish", lambda: job_finished(third)) + configuration = RUNTIME / "jobs" / "availability.json" + configuration.write_text( + json.dumps({"schema-version": 1, "qdmi": {"devices": [{"id": "mqt.ddsim.default", "enabled": False}]}}), + encoding="utf-8", + ) + monitor = ("python3", "/workspace/examples/slurm/update_availability.py", "--license", "mqt.ddsim.default:2") + assert controller("env", "MQT_CORE_QDMI_CONFIG_FILE=/jobs/availability.json", *monitor, check=False).returncode == 1 + assert license_record("mqt.ddsim.default")["Reserved"] == "2" + assert job_matches(first, "RUNNING") + assert job_matches(second, "RUNNING") + for job_id in (first, second): + (RUNTIME / "jobs" / f"release-{job_id}").touch() + wait_for(f"the released DDSIM job {job_id} to finish", lambda job_id=job_id: job_finished(job_id)) + assert_license("mqt.ddsim.default", total=2, used=0, free=2) + controller(*monitor, "--block-only") + job("srun", "--immediate=5", "--time=1", "--licenses=mqt.sc.default", "/bin/true", timeout=60) + assert job_matches(third, "PENDING", node="", reason="Licenses") + assert job("scontrol", "delete", "ReservationName=qdmi-unavailable-mqt.ddsim.default", check=False).returncode != 0 + controller(*monitor) + wait_for_result("ddsim", third, "the pending DDSIM job to execute after recovery") + wait_for("the third DDSIM job to finish", lambda: job_finished(third)) assert_bell_result(first, NODES[0]) assert_bell_result(second, NODES[1]) assert_bell_result(third) - assert_license("mqt.ddsim.default", total=2, used=1, free=1) - - (RUNTIME / "jobs" / f"release-{second}").touch() - wait_for("the released second DDSIM job to finish", lambda: job_finished(second)) assert_license("mqt.ddsim.default", total=2, used=0, free=2) + assert license_record("mqt.ddsim.default")["Reserved"] == "0" LOGGER.info( - "Slurm 25.11+ admitted two held DDSIM jobs, blocked the third for Licenses, " - "ran the SC job on a free CPU, and executed the third Bell job after release." + "Slurm enforced license capacity, kept unavailable-device jobs pending without allocating nodes, " + "and resumed the pending Bell job after a successful health check." ) def parse_arguments(arguments: Sequence[str]) -> argparse.Namespace: """Read the provider build inputs and the command to run in an allocation.""" parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--workload", type=Path, default=ROOT) + parser.add_argument("--workload", type=Path, default=CLUSTER) parser.add_argument("--dist", type=Path, default=DIST) parser.add_argument("--nodes", type=int, default=2) parser.add_argument("--setup-script", default="") parser.add_argument("--compose-file", type=Path) parser.add_argument("--device-license") parser.add_argument("--qdmi-config-file") - parser.add_argument("--reference", action="append", default=[]) parser.add_argument("command", nargs=argparse.REMAINDER) options = parser.parse_args(arguments) if options.nodes < 2: @@ -451,119 +464,45 @@ def parse_arguments(arguments: Sequence[str]) -> argparse.Namespace: setup = (options.workload / options.setup_script).resolve() if not setup.is_relative_to(options.workload.resolve()) or not setup.is_file(): parser.error("--setup-script must name a file inside --workload") - for reference in options.reference: - name, separator, value = reference.partition("=") - if not separator or re.fullmatch(r"[A-Za-z_][A-Za-z_0-9]*", name) is None or any(c.isspace() for c in value): - parser.error("--reference must use ENV=value without whitespace") - if options.qdmi_config_file and any(c.isspace() for c in options.qdmi_config_file): - parser.error("--qdmi-config-file must not contain whitespace") return options def test_provider(options: argparse.Namespace) -> None: - """Run a provider workload with job configuration and site defaults.""" - environment = list(options.reference) - if options.qdmi_config_file: - environment.append(f"MQT_CORE_QDMI_CONFIG_FILE={options.qdmi_config_file}") + """Execute a device workload with its submission environment.""" + environment = [f"MQT_CORE_QDMI_CONFIG_FILE={options.qdmi_config_file}"] if options.qdmi_config_file else [] allocation = ("srun", "--immediate=5", "--time=5", "--ntasks=1", f"--licenses={options.device_license}:1") job("env", *environment, *allocation, *options.command, timeout=300) - configuration = [ - "required /usr/local/lib/slurm/mqt-core-qdmi-spank.so", - f"licenses={options.device_license}", - ] - if options.qdmi_config_file: - configuration.append(f"qdmi_config_file={options.qdmi_config_file}") - for reference in options.reference: - name, _, value = reference.partition("=") - configuration.append(f"reference={name}:{options.device_license}:{value}") - (RUNTIME / "plugstack.conf").write_text(" ".join(configuration) + "\n", encoding="utf-8") - job(*allocation, *options.command, timeout=300) - (RUNTIME / "plugstack.conf").write_text("", encoding="utf-8") - - -def test_spank_transport() -> None: - """Check license-aware defaults and native overrides through srun and sbatch.""" - selected = "mqt.ddsim.default" - other = "mqt.sc.default" - reference = "MQT_SLURM_TEST_REFERENCE" - catalogue = "/runtime/site.qdmi.json" - (RUNTIME / "plugstack.conf").write_text( - "required /usr/local/lib/slurm/mqt-core-qdmi-spank.so " - f"licenses={selected},{other} qdmi_config_file={catalogue} " - f"reference={reference}:{selected}:site-default\n", - encoding="utf-8", - ) + +def test_job_environment() -> None: + """Export job settings through srun and sbatch independently of daemon settings.""" program = ( - "import json, os; assert os.geteuid() == 10000, os.geteuid(); " - f"print(json.dumps([os.environ.get('{reference}'), os.environ.get('MQT_CORE_QDMI_CONFIG_FILE')]), flush=True)" + "import os; assert os.geteuid() == 10000; " + "assert os.environ['MQT_CORE_QDMI_CONFIG_FILE'] == '/jobs/devices.json'; " + "assert os.environ['MQT_SLURM_TEST_REFERENCE'] == 'job-value'" ) - allocation = ("srun", "--immediate=5", "--time=1", "--ntasks=1") - - def values(*arguments: str) -> list[str | None]: - return json.loads(job(*arguments, "python3", "-c", program, timeout=60).stdout) - - assert values(*allocation, f"--licenses={selected}") == ["site-default", catalogue] - assert values(*allocation, f"--licenses={selected}:1") == ["site-default", catalogue] - assert values(*allocation, f"--licenses={selected}:2") == [None, None] - assert values(*allocation, f"--licenses={selected},{other}") == [None, None] - assert values("env", f"{reference}=job-value", *allocation, f"--licenses={selected}") == ["job-value", catalogue] - assert values("env", "MQT_CORE_QDMI_CONFIG_FILE=/runtime/job.qdmi.json", *allocation, f"--licenses={selected}") == [ - "site-default", - "/runtime/job.qdmi.json", - ] - assert values(*allocation, f"--licenses={other}") == [None, catalogue] - assert values(*allocation) == [None, None] - - # Slurm allocation metadata is not subject to the QDMI reference size limit. - unrelated = ",".join(f"unrelated-{index:03d}-{'x' * 49}:1" for index in range(65)) - slurm_config = RUNTIME / "slurm.conf" - original_config = slurm_config.read_text(encoding="utf-8") - slurm_config.write_text(original_config.replace("Licenses=", f"Licenses={unrelated},", 1), encoding="utf-8") - controller("scontrol", "reconfigure") - try: - assert values(*allocation, f"--licenses={unrelated}") == [None, None] - finally: - slurm_config.write_text(original_config, encoding="utf-8") - controller("scontrol", "reconfigure") - - for value in ("", "multiline\nvalue", "x" * 4096): - result = job( - "env", f"{reference}={value}", *allocation, f"--licenses={selected}", "/bin/true", check=False, timeout=60 - ) - assert result.returncode != 0, "Malformed reference unexpectedly reached the task" - for node in NODES: - wait_for(f"{node} to return to IDLE after rejected tasks", lambda node=node: node_is_idle(node)) - assert "DRAIN" not in node_record(node) - - # A submitted environment value must not turn an unlicensed allocation into - # a matching job in the remote SPANK hook. - assert values("env", f"SLURM_JOB_LICENSES={selected}", *allocation) == [None, None] - - output = RUNTIME / "jobs" / "spank-batch.out" + environment = ("env", "MQT_CORE_QDMI_CONFIG_FILE=/jobs/devices.json", "MQT_SLURM_TEST_REFERENCE=job-value") + job(*environment, "srun", "--immediate=5", "--time=1", "python3", "-c", program, timeout=60) job( - "env", - f"{reference}=batch-value", + *environment, "sbatch", "--wait", "--time=1", - "--ntasks=1", - f"--nodelist={NODES[0]}", - f"--licenses={selected}", - "--output=/jobs/spank-batch.out", + "--output=/jobs/environment.out", "--wrap", shlex.join(("python3", "-c", program)), timeout=120, ) - assert json.loads(output.read_text(encoding="utf-8")) == ["batch-value", catalogue] - (RUNTIME / "plugstack.conf").write_text( - f"required /usr/local/lib/slurm/mqt-core-qdmi-spank.so licenses={selected} reference={reference}:{selected}:\n", - encoding="utf-8", + job( + "srun", + "--immediate=5", + "--time=1", + "python3", + "-c", + "import os; assert 'MQT_SLURM_TEST_REFERENCE' not in os.environ; " + "assert 'MQT_CORE_QDMI_CONFIG_FILE' not in os.environ", + timeout=60, ) - assert job(*allocation, f"--licenses={selected}", "/bin/true", check=False).returncode != 0 - (RUNTIME / "plugstack.conf").write_text("", encoding="utf-8") - for node in NODES: - wait_for(f"{node} to return to IDLE", lambda node=node: node_is_idle(node)) def test_explicit_check() -> None: @@ -666,17 +605,6 @@ def main(arguments: Sequence[str] = ()) -> None: if delegate != "yes": msg = f"The packaged slurmd.service on {node} must set Delegate=yes, got {delegate!r}" raise RuntimeError(msg) - compute( - node, - "python3", - "-c", - "from pathlib import Path; import subprocess; " - "pid = subprocess.check_output(['systemctl', 'show', 'slurmd.service', " - "'--property=MainPID', '--value'], text=True).strip(); " - "environment = Path('/proc/' + pid + '/environ').read_bytes().split(b'\\0'); " - "assert b'MQT_SLURM_TEST_REFERENCE=daemon-only' in environment; " - "assert b'MQT_CORE_QDMI_CONFIG_FILE=/daemon-only/qdmi.json' in environment", - ) wait_for(f"{node} to become IDLE with two processors", lambda node=node: node_is_idle(node)) registered = set(controller("sinfo", "--Node", "--noheader", "--format=%N").stdout.split()) @@ -685,7 +613,7 @@ def main(arguments: Sequence[str] = ()) -> None: test_provider(options) else: test_core() - test_spank_transport() + test_job_environment() test_explicit_check() success = True