#!/bin/bash
# Copyright (c) 1998 Lawrence Livermore National Security, LLC and other
# HYPRE Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

## Should be run from src directory
# Loops over directories with multiprecision files and checks for changes in
# saved multiprecision function objects.
#
# NOTE: Must be run on non-multiprecision build of hypre.

scriptpath=$PWD
opts="--enable-debug"
check_type=""

usage()
{
    echo "Usage: $0 [--cuda|--hip|--sycl]"
}

if [ $# -gt 1 ]; then
    usage
    exit 1
fi

if [ $# -eq 1 ]; then
    case "$1" in
        --cuda)
            opts=(
                $opts 
                --without-umpire 
                --with-cuda
            )
            check_type="GPU"
            ;;
        --hip)
            opts=(
                $opts 
                --without-umpire 
                --with-hip 
                --with-MPI-include=${MPICH_DIR}/include
                --with-MPI-lib-dirs=${MPICH_DIR}/lib 
                --with-MPI-libs=mpi 
                --with-gpu-arch='gfx90a' 
                CC=cc 
                CXX=CC
            )
            check_type="GPU"
            ;;
        --sycl)
            opts=(
                $opts 
                --without-umpire 
                --with-sycl 
                --with-extra-CUFLAGS=-w
            )
            check_type="GPU"
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            usage
            exit 1
            ;;
    esac
fi

DIRS="\
 IJ_mv\
 blas\
 distributed_ls/Euclid\
 distributed_ls/ParaSails\
 distributed_ls/pilut\
 distributed_matrix\
 krylov\
 lapack\
 matrix_matrix\
 multivector\
 parcsr_block_mv\
 parcsr_ls\
 parcsr_mv\
 seq_block_mv\
 seq_mv\
 sstruct_ls\
 sstruct_mv\
 struct_ls\
 struct_mv\
 utilities\
"

# RDF: Need to add '--enable-hopscotch --enable-complex --with-timing' options,
# but this caused some problems for me when I tried it.  Sort out later.
make distclean
./configure "${opts[@]}"
make -s -j

echo -e "********************"
echo -e "*  FUNCTION CHECK  *"
echo -e "********************\n"
exit_code=0
for i in $DIRS
do
    echo -ne "Checking $i ... "
    (cd $i; $scriptpath/config/mup_check_dir.sh $check_type)
    if [ $? -ne 0 ]; then
        exit_code=1
    fi
done
exit $exit_code
