#! /bin/bash
# hpc-testing
# Copyright (C) 2025 SUSE LLC
# 
# 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 <http://www.gnu.org/licenses/>.

test_mpi()
{
    local flavor=$1
    local full_flavor=$flavor
    local host=$2
    local ip1=$3
    local ip2=$4
    local RUN_ARGS=""
    local ENV_ARGS=""

    case $flavor in
	openmpi*--*)
	    RUN_ARGS="${RUN_ARGS} $(echo $flavor | \
		sed -e 's/^[^-]*--/--/' -e 's/--\([^=]\+\)=\([^,-]\+\)/--mca \1 \2 /g' -e 's/%/,/g')"
	    flavor=$(echo $flavor | sed -e 's/^\([^-]*\)--.*$/\1/')
	    ;;
    esac
    case $flavor in
	openmpi|openmpi2)
	    RUN_ARGS="${RUN_ARGS} --allow-run-as-root"
	    ;;
	openmpi3|openmpi4|openmpi5)
	    RUN_ARGS="${RUN_ARGS}  --allow-run-as-root --oversubscribe"
	    ;;
        mvapich2)
            if [ "$IN_VM" == "1" ]; then
	        # MVAPICH2 fails to use process affinity as core are reported with weird IDs
	        # Disable affinity in VMs to work around this
	        ENV_ARGS="MV2_ENABLE_AFFINITY=0 MV2_SUPPRESS_JOB_STARTUP_PERFORMANCE_WARNING=1"
            fi
            ;;
    esac
    RUN_ARGS="--host $ip1,$ip2 -np 2 ${RUN_ARGS}"

    set -o pipefail
    set -e
    tp $host "$ENV_ARGS VERBOSE=1 RUN_ARGS='$RUN_ARGS' SHORT=1 /usr/lib64/mpi/gcc/$flavor/tests/runtests.sh" | tee "results/MPI-${full_flavor}.log"
    set +o pipefail
    ./helpers/common/parse-imb.sh  "results/MPI-${full_flavor}.log"  "results/MPI-${full_flavor}.csv"
}

mpi_filter_flavour(){
    local flavours=$1
    shift

    while [ $# -ne 0 ]; do
   local filter=$1
   flavours=$(echo $flavours | sed -e 's/'$filter'\(--[^,]\+\)\?,//g' -e 's/'$filter'\(--[^,]\+\)\?$//g')
   shift
    done
    echo $flavours
}

mpi_get_flavors()
{
    local host=$1
    local flavours=$2
    local suse_version

    suse_version=$(get_suse_version $host)
    # Generate default flavour list if none is provided
    if [ "$flavours" != "" ]; then
        echo $flavours
        return 0
    fi
    case $suse_version in
	12.3|12.4)
	    flavours="mvapich2,openmpi"
	    ;;
	12.5)
            # Should contain mvapich2, but currently broken
	    flavours="openmpi"
	    ;;
	15|15.1)
	    flavours="mvapich2,mpich,openmpi"
	    ;;
	15.2)
            # Should contain openmpi3, but currently broken
	    flavours="mvapich2,mpich,openmpi2"
	    ;;
	15.3)
	    flavours="mvapich2,mpich,openmpi2,openmpi3"
	    ;;
	15.4)
	    flavours="mvapich2,mpich,openmpi2,openmpi3,openmpi4"
            ;;
	15.5)
	    flavours="mvapich2,mpich,openmpi2,openmpi3,openmpi4,\
openmpi3--btl=openib--pml=^ucx,\
openmpi3--btl=self--pml=ucx,\
openmpi4--btl=ofi--pml=^ucx,\
openmpi4--btl=self--pml=ucx"
	    ;;
	15.6|15.7)
	    flavours="mvapich2,mpich,openmpi4,\
openmpi4--btl=ofi--pml=^ucx,\
openmpi4--btl=self--pml=ucx"
	    ;;
	16.0)
	    flavours="mvapich2,mpich,openmpi4,openmpi5,\
openmpi4--btl=ofi--pml=^ucx,\
openmpi4--btl=self--pml=ucx,\
openmpi5--btl=ofi--pml=cm,\
openmpi5--btl=uct--pml=ucx"
            ;;
	16.1)
	    flavours="mvapich2,mvapich4-ucx,mvapich4-ofi,mpich,openmpi4,openmpi5,\
openmpi4--btl=ofi--pml=^ucx,\
openmpi4--btl=self--pml=ucx,\
openmpi5--btl=ofi--pml=cm,\
openmpi5--btl=uct--pml=ucx"
            ;;
        2025*)
	    flavours="mvapich2,mpich,openmpi4,openmpi5,\
mvapich3-ofi,mvapich3-ucx,\
openmpi4--btl=ofi--pml=^ucx,\
openmpi4--btl=self--pml=ucx,\
openmpi5--btl=ofi--pml=cm,\
openmpi5--btl=uct--pml=ucx"
            ;;
	*)
	    # N/A
	    true
	    ;;
    esac
    echo $flavours
}
