#!/usr/bin/bash
set -e
set -v on
### 本文件为 linpack 的安装脚本，在执行本脚本之前，需要先指定一些配置
### 当前时间：2022年6月27日16:16:56

## 指定 OpenMPI 的安装包路径【可选】（默认与本脚本位于同一路径）
openmpi_pkg_dir=$(dirname $(readlink -f "$0"))
## 指定 OpenMPI 的 build 路径【可选】（默认与本脚本位于同一路径）
openmpi_build_dir=$(dirname $(readlink -f "$0"))/../build
## 指定 OpenMPI 的版本号
openmpi_version=4.0.4
## 指定 OpenMPI 的安装路径
openmpi_install_dir=/usr/qiql/mpi/openmpi/${openmpi_version}
# /home/wxsc/zhangw/GPFS/qiql/tool/openmpi/${openmpi_version}
## 指定 OpenBLAS 的安装包路径【可选】（默认与本脚本位于同一路径）
openblas_pkg_dir=$(dirname $(readlink -f "$0"))
## 指定 OpenBLAS 的 build 路径【可选】（默认与本脚本位于同一路径）
openblas_build_dir=$(dirname $(readlink -f "$0"))/../build
## 指定 OpenBLAS 的版本号
openblas_version=0.3.20
## 指定 OpenBLAS 的安装路径
openblas_install_dir=/usr/qiql/rely/linpack/openblas/${openblas_version}
# /home/wxsc/zhangw/GPFS/qiql/tool/openblas/${openblas_version}
## 指定 HPL 的安装包路径【可选】（默认与本脚本位于同一路径）
hpl_pkg_dir=$(dirname $(readlink -f "$0"))
## 指定 HPL 的版本号
hpl_version=2.3
## 指定 HPL 的安装路径
hpl_install_dir=/usr/qiql/app
## 指定 hpl 的后缀，可以设置成任意名称
hpl_arch=qiql
### 指定完毕

## 开始安装 OpenMPI
echo "============================================================================="
echo "============================================================================="
echo "=================== start install openmpi-$openmpi_version} ================="
mkdir -p ${openmpi_build_dir}
tar -zxvf ${openmpi_pkg_dir}/openmpi-${openmpi_version}.tar.gz -C ${openmpi_build_dir}
cd ${openmpi_build_dir}/openmpi-${openmpi_version}
./configure --prefix=${openmpi_install_dir}
make -j
make install
## 安装 OpenMPI 完毕
echo "=================== openmpi has been install to ${openmpi_install_dir} ======"
echo "=================== install openmpi-${openmpi_version} finish ==============="
echo "============================================================================="
echo "============================================================================="


## 开始安装 OpenBLAS
echo "============================================================================="
echo "============================================================================="
echo "=================== start install OpenBLAS-$openblas_version} ==============="
mkdir -p ${openblas_build_dir}
tar -zxvf ${openblas_pkg_dir}/OpenBLAS-${openblas_version}.tar.gz -C ${openblas_build_dir}
cd ${openblas_build_dir}/OpenBLAS-${openblas_version}
make -j
make PREFIX=${openblas_install_dir} install
## 安装 OpenBLAS 完毕
echo "=================== OpenBLAS has been install to ${openblas_install_dir} ===="
echo "=================== install OpenBLAS-${openblas_version} finish ============="
echo "============================================================================="
echo "============================================================================="

## 开始安装 hpl
mkdir -p ${hpl_install_dir}
tar -zxvf ${hpl_pkg_dir}/hpl-${hpl_version}.tar.gz -C ${hpl_install_dir}
cd ${hpl_install_dir}/hpl-${hpl_version}

## 新建一个环境变量脚本，后面如果要使用hpl，需要 source 该脚本，脚本放置在hpl的根目录下
touch env.hpl.sh
## 将 OpenMPI 添加到环境变量中去
echo export OPENMPI_HOME=${openmpi_install_dir} > env.hpl.sh
echo export PATH=\$OPENMPI_HOME/bin:\$PATH >> env.hpl.sh
echo export LD_LIBRARY_PATH=\$OPENMPI_HOME/lib:\$LD_LIBRARY_PATH >> env.hpl.sh
echo export LIBRARY_PATH=\$OPENMPI_HOME/lib:\$LIBRARY_PATH >> env.hpl.sh
echo export CPATH=\$OPENMPI_HOME/include:\$CPATH >> env.hpl.sh
echo export C_INCLUDE_PATH=\$OPENMPI_HOME/include:\$C_INCLUDE_PATH >> env.hpl.sh
## OpenMPI 添加环境变量完毕
## 将编译得到的 OpenBLAS 库路径添加到环境变量中去
echo export LD_LIBRARY_PATH=${openblas_install_dir}/lib:\$LD_LIBRARY_PATH >> env.hpl.sh
## 添加完毕

cd ${hpl_install_dir}/hpl-${hpl_version}
source ./env.hpl.sh

# 进入setup目录选择一个后缀名和机器环境相近的文件复制到hpl的根目录，且将后缀进行更改，这个后缀很重要！
cp setup/Make.Linux_PII_FBLAS Make.${hpl_arch}

## 修改ARCH字段
sed -i "s/ARCH         = Linux_PII_FBLAS/ARCH         = ${hpl_arch}/g" ./Make.${hpl_arch}

## 修改TOPdir 为 hpl 的实际安装路径
# sed -i "s/TOPdir       = \$(HOME)\/hpl/TOPdir       = ${hpl_install_dir}\/hpl-${hpl_version}/g" ./Make.${hpl_arch}
sed -i "/TOPdir       = \$(HOME)\/hpl/a\\TOPdir       = ${hpl_install_dir}\/hpl-${hpl_version}" ./Make.${hpl_arch}
## 修改MPdir 为mpicc
sed -i "s/MPdir        = \/usr\/local\/mpi/MPdir        = mpicc/g" ./Make.${hpl_arch}

## 在MPinc字段前加注释#
sed -i 's/MPinc  */#&/g' ./Make.${hpl_arch}
## 在MPlib字段前加注释
sed -i 's/MPlib  */#&/g' ./Make.${hpl_arch}
## 在LAdir字段前加注释
sed -i 's/LAdir  */#&/g' ./Make.${hpl_arch}
## 在LAinc字段前加注释
sed -i 's/LAinc  */#&/g' ./Make.${hpl_arch}
## 在LAlib字段后添加 OpenBLAS 正确的路径
sed -i "/LAlib        =/a\\LAlib        = -L${openblas_install_dir}\/lib -lopenblas" ./Make.${hpl_arch}
## 将 CC 字段修改为 mpicc
sed -i 's/CC           = \/usr\/bin\/gcc/CC           = mpicc/g' ./Make.${hpl_arch}
## 将 LINKER 字段修改为mpicc
sed -i 's/LINKER       = \/usr\/bin\/g77/LINKER       = mpicc/g' ./Make.${hpl_arch}
## Make.${hpl_arch} 文件修改完毕

## 修改 Make.top 文件中的 arch 字段
sed -i "s/arch             = UNKNOWN/arch             = ${hpl_arch}/g" ./Make.top
## Make.top 文件修改完毕

## 编译 hpl
make arch=${hpl_arch}
## 编译完毕
echo "============================================================================="
echo "============================================================================="
echo "============================================================================="
echo "============================================================================="
echo "============================================================================="
echo "hpl has been install to ${hpl_install_dir}/hpl-${hpl_version}/bin/${hpl_arch}"
echo "openmpi-${openmpi_version} has been install to ${openmpi_install_dir}, openblas-${openblas_version} has been install to ${openblas_install_dir}"
echo "Before using hpl, please source the env.hpl.sh script in the ${hpl_install_dir}/hpl-${hpl_version} to add environment variables to the session"
echo "============================================================================="
echo "============================================================================="
echo "============================================================================="

set +e
### END
