summaryrefslogtreecommitdiffstats
path: root/tools/cuda-ab/p2/oracle.sh
blob: 80cff940ad8ac8a11bdbade287a8a284bb0f691a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
#
# p2/oracle.sh - build the P2 programs and record host-side reference output
# into p2/expected/<name>.txt.
#
# Two modes:
#   GPU mode  - nvidia-smi works: each program is run bare-metal; the stable
#               lines (RESULT/CHECK/ERR/STATUS/P2PRINT/PROBE/MAIL) are
#               filtered into expected/<name>.txt and the full output is
#               kept as expected/<name>.full.txt.
#   EMIT mode - no usable host GPU (e.g. the device is owned by a running
#               guest): each program is run with --emit-expected, a
#               deterministic CPU-only computation of the stable lines a
#               correct GPU run must produce. This fallback is what
#               distinguishes "oracle not available" from "oracle passed".
#
# The generation mode is recorded in expected/SOURCE.txt.
#
# Usage: ./oracle.sh
set -e

HERE="$(cd "$(dirname "$0")" && pwd)"
EXP="$HERE/expected"
FILTER='^(RESULT|CHECK|ERR|STATUS|P2PRINT|PROBE|MAIL)'

"$HERE/build.sh"
mkdir -p "$EXP"

mode="emit"
if nvidia-smi -L >/dev/null 2>&1; then
	mode="gpu"
fi

FAIL=0
for bin in "$HERE"/bin/p2_*; do
	name="$(basename "$bin")"
	out="$EXP/$name.full.txt"

	if [ "$mode" = "gpu" ]; then
		echo "[oracle] $name (bare-metal GPU)"
		if ! timeout 300 "$bin" > "$out" 2>&1; then
			echo "[oracle] FAIL: $name exited non-zero" >&2
			FAIL=1
			continue
		fi
		if ! grep -q '^STATUS OK$' "$out"; then
			echo "[oracle] FAIL: $name did not print STATUS OK" >&2
			FAIL=1
			continue
		fi
		grep -E "$FILTER" "$out" > "$EXP/$name.txt"
	else
		echo "[oracle] $name (no host GPU: deterministic emit)"
		"$bin" --emit-expected > "$out" 2>&1
		grep -E "$FILTER" "$out" > "$EXP/$name.txt"
	fi
done

if [ "$FAIL" -ne 0 ]; then
	echo "[oracle] FAILED - expected/ is incomplete" >&2
	exit 1
fi

{
	echo "mode=$mode"
	echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
	echo "host=$(hostname)"
	if [ "$mode" = "gpu" ]; then
		nvidia-smi --query-gpu=name,driver_version --format=csv,noheader | head -1
	else
		echo "deterministic CPU computation via --emit-expected"
		echo "(no usable host GPU; rerun ./oracle.sh when one is available)"
	fi
} > "$EXP/SOURCE.txt"

echo "[oracle] done ($mode) -> $EXP"