#!../../../common/bats/bin/bats
# -*-sh-*-

load ./common/test_helper_functions || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

check_rms
rm=$RESOURCE_MANAGER

testname="libs/superLU_dist"

if [ ! -z "$SIMPLE_CI" ]; then
    export OMPI_MCA_btl="self,tcp"
fi
NODES=2
TASKS=4

export MV2_ENABLE_AFFINITY=0

@test "[$testname] PDGSSVX with full (default) options ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive ];then
	flunk "pddrive binary does not exist"
    fi

    run_mpi_binary ./pddrive "-r 2 -c 2 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] pdgssvx_ABglobal with full (default) options ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive_ABglobal ];then
	flunk "pddrive_ABglobal binary does not exist"
    fi

    run_mpi_binary ./pddrive_ABglobal "-r 1 -c 1 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] vary RHS ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive1 ];then
	flunk "pddrive1 binary does not exist"
    fi

    run_mpi_binary ./pddrive1 "-r 2 -c 2 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] vary RHS ABglobal ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive1_ABglobal ];then
	flunk "pddrive1_ABglobal binary does not exist"
    fi

    run_mpi_binary ./pddrive1_ABglobal "-r 1 -c 1 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] re-use permutation vector ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive2 ];then
	flunk "pddrive2 binary does not exist"
    fi

    run_mpi_binary ./pddrive2 "-r 2 -c 2 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] re-use permutation vector ABglobal ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" { 
    if [ ! -s pddrive2_ABglobal ];then
	flunk "pddrive2_ABglobal binary does not exist"
    fi

    run_mpi_binary ./pddrive2_ABglobal "-r 1 -c 1 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] re-use symbolic factorization ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive3 ];then
	flunk "pddrive3 binary does not exist"
    fi

    run_mpi_binary ./pddrive3 "-r 2 -c 2 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] re-use symbolic factorization ABglobal ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s pddrive3_ABglobal ];then
	flunk "pddrive3_ABglobal binary does not exist"
    fi

    run_mpi_binary ./pddrive3_ABglobal "-r 1 -c 1 g20.rua" $NODES $TASKS
    assert_success
}

@test "[$testname] multi-grid ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -z "$SIMPLE_CI" ]; then
        skip "The multi-grid test is too heavy for SIMPLE_CI environments, like Github Actions and Cirrus CI"
    fi

    if [ ! -s pddrive4 ];then
	flunk "pddrive4 binary does not exist"
    fi

    run_mpi_binary ./pddrive4 "g20.rua" $NODES 10
    assert_success
}

