#!/bin/sh

testsdir="/usr/libexec/rocm/librccl1-tests"

# If autopkgtest called us, pass some extra options if not set
if [ -n "$AUTOPKGTEST_ARTIFACTS" ]; then
    # Save output as XML to artifacts, for download
    [ -n "${GTEST_OUTPUT:-}" ] || export GTEST_OUTPUT="xml:${AUTOPKGTEST_ARTIFACTS}/"
fi

# Specific rccl circumstances
gpu_count="$(grep 'gfx_target_version [^0]' /sys/devices/virtual/kfd/kfd/topology/nodes/*/properties | wc -l)"
if [ "$gpu_count" -lt 2 ]
then
	echo "The rccl tests require at least two GPUs."
	echo "Skipping tests."
	exit 77
elif [ ! -f /sys/module/amdgpu/parameters/pcie_p2p ] || [ "Y" != "$(cat /sys/module/amdgpu/parameters/pcie_p2p)" ]
then
	echo "The rccl tests require AMDGPU P2P."
	echo "Skipping tests."
	exit 77
fi
export RCCL_ENABLE_SIGNALHANDLER=1
export NCCL_DEBUG="INFO"
export HSA_FORCE_FINE_GRAIN_PCIE=1

# Pass on all arguments to this script to the test binary
# Any individual failure is overall failure
EXITCODE=0
"$testsdir"/UnitTests "$@" || EXITCODE=1
exit $EXITCODE
