Skip to content
Snippets Groups Projects
wmakeScheduler 6.71 KiB
Newer Older
mattijs's avatar
mattijs committed
#!/bin/bash
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
Henry Weller's avatar
Henry Weller committed
#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM 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.
#
#     OpenFOAM 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 OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     wmakeScheduler
#
# Usage
#     wmakeScheduler COMMAND
#         run 'COMMAND' on one of the slots listed in WM_HOSTS
#
#     wmakeScheduler -count
#         count the total number of slots available in WM_HOSTS
#         eg,  export WM_NCOMPPROCS=$(wmakeScheduler -count)
#
# Description
#     Scheduler for network distributed compilations using wmake.
#     - WM_HOSTS contains a list of hosts and number of concurrent processes
#     eg,
#         export WM_HOSTS="hostA:1 hostB:2 hostC:1"
mattijs's avatar
mattijs committed
#     - WM_COLOURS contains a list of colours to cycle through
#         export WM_COLOURS="black blue green cyan red magenta yellow"
#
#     Sources the relevant cshrc/bashrc if not set.
#
#     WM_PROJECT_DIR, WM_PROJECT and WM_PROJECT_VERSION will have been set
#     before calling this routine.
#     FOAM_INST_DIR may possibly have been set (to find installation)
#
#-------------------------------------------------------------------------------

# csh sets HOST, bash sets HOSTNAME
: ${HOST:=$HOSTNAME}

lockDir=$HOME/.$WM_PROJECT/.wmake

# Fallback - 1 core on current host
: ${WM_HOSTS:=$HOST:1}

# Count the total number of slots available and exit
if [ "$1" = "-count" ]
then
mattijs's avatar
mattijs committed
    expr $(
        for slotGroup in $WM_HOSTS
        do
            n=${slotGroup##*:}
            [ "$n" = "${slotGroup%%:*}" ] && n=1  # missing ':'
            echo "+ ${n:-1}"
        done
    )
    exit 0
# Where to source WM_PROJECT settings in a remote shell
# This code tries to figure out which cshrc or bashrc to execute.
# !! Assumes remote computer running same shell and startup files
# in same location

sourceFoam=false    # Fallback command
case $SHELL in
*/csh | */tcsh )    # [t]csh vs bash|ksh|sh
mattijs's avatar
mattijs committed
    shellRc=cshrc
    ;;
mattijs's avatar
mattijs committed
    shellRc=bashrc
    ;;
# Check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
# Check ~/.$WM_PROJECT/
# Check <installedProject>/etc/
mattijs's avatar
mattijs committed
if [ "$WM_PROJECT" ]
then
mattijs's avatar
mattijs committed
    for i in \
        $HOME/.$WM_PROJECT/$WM_PROJECT_VERSION \
        $HOME/.$WM_PROJECT \
        $WM_PROJECT_DIR/etc \
        ;
    do
        if [ -f "$i/$shellRc" ]
        then
            sourceFoam="$i/$shellRc"
            break
        fi
    done
fi

# Construct test string for remote execution.
# Source WM_PROJECT settings if WM_PROJECT environment not set.
Henry Weller's avatar
Henry Weller committed
# Attempt to preserve the installation directory 'FOAM_INST_DIR'
# Use FOAM_SETTINGS to pass command-line settings
case $sourceFoam in
mattijs's avatar
mattijs committed
    if [ "$FOAM_INST_DIR" ]
    then
        sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam $FOAM_SETTINGS"
mattijs's avatar
mattijs committed
    else
        sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam $FOAM_SETTINGS"
mattijs's avatar
mattijs committed
    fi
    ;;
mattijs's avatar
mattijs committed
    # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
    sourceFoam='if ( ! $?WM_PROJECT ) source '"$sourceFoam $FOAM_SETTINGS"
mattijs's avatar
mattijs committed
    ;;
# Quote double-quotes for remote command line
rcmd=$(echo $* | sed -e s/\"/\'\"\'/g)
# The same, without forking (not ksh, maybe not /bin/sh either)
# rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)

mattijs's avatar
mattijs committed

# Convert WM_COLOURS into an array
mattijs's avatar
mattijs committed
declare colourList
mattijs's avatar
mattijs committed
nColours=0
for col in $WM_COLOURS
do
mattijs's avatar
mattijs committed
    colourList[$nColours]=$col
    ((nColours = $nColours + 1))
mattijs's avatar
mattijs committed
done

mattijs's avatar
mattijs committed
# Bashism: make pipe fail early.
# This ensures the return value of the command is returned and not of the
# colouring pipe etc.
mattijs's avatar
mattijs committed
set -o pipefail

mattijs's avatar
mattijs committed

#------------------------------------------------------------------------------
# Colour output by argument 1
#------------------------------------------------------------------------------
mattijs's avatar
mattijs committed
colourPipe()
{
mattijs's avatar
mattijs committed

    if tty -s <&1       #    [ "$1" ]
mattijs's avatar
mattijs committed
    then
    (
        while read line
        do
            setterm -foreground $1
            echo "$line"
        done
        setterm -foreground default
    )
    else
        cat
    fi
mattijs's avatar
mattijs committed
}


mattijs's avatar
mattijs committed
colourIndex=0

# Check for existence of lockfile binary
if ! type -p lockfile >/dev/null
then
    echo "$Script: Could not find executable 'lockfile'"
mattijs's avatar
mattijs committed
    for slotGroup in $WM_HOSTS
    do
        # Split 'host:N', but catch 'host:' and 'host' too
mattijs's avatar
mattijs committed
        host=${slotGroup%%:*}
        n=${slotGroup##*:}
        [ "$n" = "$host" ] && n=1  # missing ':'
        : ${n:=1}

        i=0
        while [ "$i" -lt "$n" ]
        do
            lockFile="$lockDir/$host:$i"
            if lockfile -r0 "$lockFile" 2>/dev/null
mattijs's avatar
mattijs committed
            then
mattijs's avatar
mattijs committed
                if [ "$nColours" -gt 0 ]
                then
                    # Set colour
                    colour="${colourList[$colourIndex]}"

                    if [ "$host" = "$HOST" ]; then
mattijs's avatar
mattijs committed
                        eval $* 2>&1 | colourPipe "$colour"
                    else
                        ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" \
                            2>&1 | colourPipe "$colour"
mattijs's avatar
mattijs committed
                    fi
                    retval=$?
                else
                    if [ "$host" = "$HOST" ]; then
mattijs's avatar
mattijs committed
                        eval $* 2>&1
                    else
                        ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" \
                            2>&1
mattijs's avatar
mattijs committed
                    fi
                    retval=$?
                fi

                # Release lock
                rm -f "$lockFile" 2>/dev/null
                exit $retval
mattijs's avatar
mattijs committed
            fi
mattijs's avatar
mattijs committed
            i=$(expr $i + 1)
mattijs's avatar
mattijs committed

mattijs's avatar
mattijs committed
            # Cycle through colours. Note: outside lock clause!
            colourIndex=$(expr $colourIndex + 1)
            [ "$colourIndex" -lt "$nColours" ] || colourIndex=0

        done
    done
    # Did not find any free slots. Rest a bit.
    sleep 1
mattijs's avatar
mattijs committed
if [ "$nColours" -gt 0 ]
then
mattijs's avatar
mattijs committed
    setterm -foreground default
mattijs's avatar
mattijs committed
fi
exit 0 # clean exit
#------------------------------------------------------------------------------