#!/bin/sh

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2013 EDF S.A.
#
# This program 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 2 of the License, or (at your option) any later
# version.
#
# This program 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
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

#===============================================================================
# A priori detection of the rank of a MPI process
#===============================================================================
#
# In order to get the MPI rank from a script launched
# by mpirun (or prun, mpijob, or equivalent):
#
# MPI_RANK=`runcase_mpi_rank $@`
#
# Mainly useful to launch MPMD applications
# like coupling within MPI environments
# which do not have a command like mpiexec.

# Current systems
# ---------------

# For MPICH2 or MPICH-3 (which also provides the mpiexec command)
if [ "$PMI_RANK" != "" ] ; then
  MPI_RANK="$PMI_RANK"

# For Open MPI (it also provides the mpiexec command)
elif [ "$OMPI_MCA_ns_nds_vpid" != "" ] ; then # Open MPI 1.2
  MPI_RANK="$OMPI_MCA_ns_nds_vpid"
elif [ "$OMPI_COMM_WORLD_RANK" != "" ] ; then # Open MPI 1.3
  MPI_RANK="$OMPI_COMM_WORLD_RANK"

# Otherwise, with SLURM
elif [ "$PMI_ID" != "" ] ; then
  MPI_RANK="$PMI_ID"
elif [ "$SLURM_PROCID" != "" ] ; then
  MPI_RANK="$SLURM_PROCID"

# Current systems for which we have no access (so trust docs)
# -------------------------------------------

# For IBM Platform MPI
elif [ "$MPI_RANKID" != "" ] ; then
  MPI_RANK="$MPI_RANKID"

# For IBM PE
elif [ "$MP_CHILD" != "" ] ; then
  MPI_RANK="$MP_CHILD"

# Obsolete systems (are any of those still used somewhere ?)
# ----------------

# For MVAPICH 1.1
elif [ "$SMPIRUN_RANK" != "" ] ; then
  MPI_RANK="$MPIRUN_RANK"

# For LAM 7.1 (an appschema can also be used)
elif [ "$LAMRANK" != "" ] ; then
  MPI_RANK="$LAMRANK"

# On HP AlphaServer cluster (CCRT: Chrome)
elif [ "$RMS_RANK" != "" ] ; then
  MPI_RANK="$RMS_RANK"

# On Opteron cluster under Linux MPICH-GM
elif [ "$GMPI_ID" != "" ] ; then
  MPI_RANK="$GMPI_ID"

# On cluster with HP-MPI (has become Platform MPI since)
elif [ "$MPI_PROC" != "" ] ; then
  MPI_RANK=`echo $MPI_PROC | cut -f 2 -d,`

# On cluster under Linux with Scali MPI (bought by Platform computing)
elif [ "$SCAMPI_PROCESS_PARAM" != "" ] ; then
  MPI_RANK=`echo $SCAMPI_PROCESS_PARAM | cut -f4 -d' '`

# For "standard" MPICH 1.2 (with "usual" chp4 communication)
else
  MPI_RANK=0
  next_arg_is_rank=""
  for arg in "$@" ; do
    if [ "$arg" = "-p4rmrank" ] ; then
      next_arg_is_rank="1"
    elif [ "$next_arg_is_rank" = "1" ] ; then
      MPI_RANK="$arg"
      next_arg_is_rank="0"
    fi
  done

# End of known cases
fi

# Output of the obtained rank

echo "$MPI_RANK"

