Created
May 11, 2016 15:08
-
-
Save flocke/c1ecab8e3c23782a5848b72d7271dcee to your computer and use it in GitHub Desktop.
Simple bash script to download, build and deploy an optimized version of OpenBLAS on an Ubuntu system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
############################################################################### | |
## OpenBLAS-deploy.sh (v1.0) | |
## | |
## Simple bash script to download, build and deploy an optimized version of | |
## OpenBLAS on a Ubuntu system (tested with 16.04). | |
## | |
## Usage: ./OpenBLAS-deploy.sh [VERSION] | |
## | |
## VERSION: The version of OpenBLAS to deploy. | |
## | |
## By default this script downloads, builds and installs OpenBLAS and creates | |
## alternatives for libblas.so.3 and liblapack.so.3. To be able to do so it | |
## needs to be run as root, in which case the download and build are done by an | |
## unprivileged user (nobody by default). | |
## To run the script as normal user the environment variable OPENBLAS_AS_USER | |
## needs to be set to 'true'. In this case the script will only download and | |
## build OpenBLAS, skipping the installation and leaving the temporary | |
## directory used for the build in place. | |
## | |
############################################################################### | |
## Configuration | |
OPENBLAS_BUILD_USER=nobody | |
OPENBLAS_MAJOR_VERSION=3 | |
OPENBLAS_MAKE_OPTS="FC=gfortran USE_THREAD=1 USE_OPENMP=0 NO_STATIC=1 MAJOR_VERSION=${OPENBLAS_MAJOR_VERSION} NO_LAPACK=0 BUILD_LAPACK_DEPRECATED=1" | |
OPENBLAS_INSTALL_PREFIX="/opt/OpenBLAS" | |
OPENBLAS_ALTERNATIVES_PRIORITY=100 | |
OPENBLAS_ALTERNATIVES_TARGET="${OPENBLAS_INSTALL_PREFIX}/lib/libopenblas.so.${OPENBLAS_MAJOR_VERSION}" | |
OPENBLAS_ALTERNATIVE_LIBBLAS_NAME="libblas.so.${OPENBLAS_MAJOR_VERSION}" | |
OPENBLAS_ALTERNATIVE_LIBBLAS_PATH="/usr/lib/libblas.so.${OPENBLAS_MAJOR_VERSION}" | |
OPENBLAS_ALTERNATIVE_LIBLAPACK_NAME="liblapack.so.${OPENBLAS_MAJOR_VERSION}" | |
OPENBLAS_ALTERNATIVE_LIBLAPACK_PATH="/usr/lib/liblapack.so.${OPENBLAS_MAJOR_VERSION}" | |
############################################################################### | |
## Colors | |
Color_Reset='\e[0m' | |
Color_BRed='\e[1;31m' | |
Color_BGreen='\e[1;32m' | |
Color_BBlue='\e[1;34m' | |
Color_BYellow='\e[1;33m' | |
############################################################################### | |
## Functions | |
function user_exec { | |
if [[ ${EUID} -ne 0 ]]; then | |
${@} | |
else | |
su -m -s /bin/bash -c "${@}" "${OPENBLAS_BUILD_USER}" | |
fi | |
} | |
function exit_on_fail { | |
if [[ ${?} -ne 0 ]]; then | |
echo -e "${Color_BRed}failed${Color_Reset}!" | |
exit 1 | |
else | |
echo -e "${Color_BGreen}done${Color_Reset}." | |
fi | |
} | |
############################################################################### | |
## Main script | |
if [[ ${EUID} -ne 0 && ${OPENBLAS_AS_USER} != "true" ]]; then | |
echo -e "${Color_BRed}==>${Color_Reset} You need to be root to deploy OpenBLAS on this machine!" | |
exit 1 | |
else | |
if [[ -z ${1} ]]; then | |
echo -e "${Color_BRed}==>${Color_Reset} You need to pass a version of OpenBLAS you whish to deploy as argument!" | |
exit 1 | |
fi | |
if [[ ${EUID} -eq 0 && -d "${OPENBLAS_INSTALL_PREFIX}" ]]; then | |
echo -e "${Color_BRed}==>${Color_Reset} Found an already deployed version of OpenBLAS under ${OPENBLAS_INSTALL_PREFIX}, please remove it before running this script!" | |
exit 1 | |
fi | |
if [[ ${OPENBLAS_AS_USER} != "true" ]]; then | |
echo -e "${Color_BBlue}==>${Color_Reset} Starting deployment of OpenBLAS on ${Color_BYellow}$(hostname)${Color_Reset}:" | |
else | |
echo -e "${Color_BBlue}==>${Color_Reset} Starting deployment of OpenBLAS on ${Color_BYellow}$(hostname)${Color_Reset} (user-mode, will exit after build):" | |
fi | |
OPENBLAS_VERSION="${1}" | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Creating temporary directory ... " | |
TMP_DIR=$(mktemp -d /tmp/OpenBLAS.XXXXXXXXX) | |
if [ ! -d "${TMP_DIR}" ]; then | |
echo -e "${Color_BRed}failed${Color_Reset}!" | |
exit 1 | |
else | |
echo -e "${Color_BGreen}done${Color_Reset}." | |
echo -e " ${Color_BBlue}>${Color_Reset} Temporary directory: ${TMP_DIR}" | |
fi | |
if [[ ${EUID} -eq 0 ]]; then | |
chown -R "${OPENBLAS_BUILD_USER}:" "${TMP_DIR}" | |
fi | |
OLD_PWD=$(pwd) | |
cd "${TMP_DIR}" | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Downloading ... " | |
user_exec "wget https://github.com/xianyi/OpenBLAS/archive/v${OPENBLAS_VERSION}.tar.gz -O OpenBLAS-${OPENBLAS_VERSION}.tar.gz" > "${TMP_DIR}/download.log" 2>&1 | |
exit_on_fail | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Extracting ... " | |
user_exec "tar -xf OpenBLAS-${OPENBLAS_VERSION}.tar.gz" > "${TMP_DIR}/extract.log" 2>&1 | |
exit_on_fail | |
cd "${TMP_DIR}/OpenBLAS-${OPENBLAS_VERSION}" | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Building ... " | |
user_exec "make ${OPENBLAS_MAKE_OPTS}" > "${TMP_DIR}/build.log" 2>&1 | |
exit_on_fail | |
if [[ ${EUID} -eq 0 ]]; then | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Installing ... " | |
make ${OPENBLAS_MAKE_OPTS} PREFIX="${OPENBLAS_INSTALL_PREFIX}" install > "${TMP_DIR}/install.log" 2>&1 | |
exit_on_fail | |
update-alternatives --list "${OPENBLAS_ALTERNATIVE_LIBBLAS_NAME}" | grep "${OPENBLAS_ALTERNATIVES_TARGET}" > /dev/null 2>&1 | |
if [[ ${?} -ne 0 ]]; then | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Adding alternative for ${OPENBLAS_ALTERNATIVE_LIBBLAS_NAME} ... " | |
update-alternatives --install "${OPENBLAS_ALTERNATIVE_LIBBLAS_PATH}" "${OPENBLAS_ALTERNATIVE_LIBBLAS_NAME}" "${OPENBLAS_ALTERNATIVES_TARGET}" ${OPENBLAS_ALTERNATIVES_PRIORITY} > "${TMP_DIR}/alternative-libblas.log" 2>&1 | |
exit_on_fail | |
fi | |
update-alternatives --list "${OPENBLAS_ALTERNATIVE_LIBLAPACK_NAME}" | grep "${OPENBLAS_ALTERNATIVES_TARGET}" > /dev/null 2>&1 | |
if [[ ${?} -ne 0 ]]; then | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Adding alternative for ${OPENBLAS_ALTERNATIVE_LIBLAPACK_NAME} ... " | |
update-alternatives --install "${OPENBLAS_ALTERNATIVE_LIBLAPACK_PATH}" "${OPENBLAS_ALTERNATIVE_LIBLAPACK_NAME}" "${OPENBLAS_ALTERNATIVES_TARGET}" ${OPENBLAS_ALTERNATIVES_PRIORITY} > "${TMP_DIR}/alternative-liblapack.log" 2>&1 | |
exit_on_fail | |
fi | |
cd "${OLD_PWD}" | |
echo -e -n " ${Color_BBlue}-->${Color_Reset} Removing temporary directory ... " | |
rm -r "${TMP_DIR}" | |
exit_on_fail | |
echo -e "${Color_BBlue}==>${Color_Reset} Deployment of OpenBLAS on ${Color_BYellow}$(hostname)${Color_Reset} successful!" | |
else | |
echo -e "${Color_BBlue}==>${Color_Reset} Deployment in user-mode successfully finished, manual installation required." | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment