test_parallel()
{
  TESTNAME=$1
  shift

  . ./mpirun.defs
  NP=4
  for arg in "$@"
  do
    np=$(expr "${arg}" : "np:\(.*\)")
    if [ -n "${np}" ]
    then
      NP=${np}
    fi
  done

  NT=1
  for arg in "$@"
  do
    nt=$(expr "${arg}" : "nt:\(.*\)")
    if [ -n "${nt}" ]
    then
      NT=${nt}
    fi
  done

  PATTERN='on rank *0'
  for arg in "$@"
  do
    pattern=$(expr "${arg}" : "pattern:\(.*\)")
    if [ -n "${pattern}" ]
    then
      PATTERN="${pattern}"
    fi
  done

  [ -d run_test_${TESTNAME} ] && rm -r run_test_${TESTNAME}
  mkdir run_test_${TESTNAME}

  SIONDUMP=../siondump

  (
    cd run_test_${TESTNAME}

    export LSION_DEBUG=test_${TESTNAME}
    export LSION_DEBUG_MASK=1023
    export LSION_DEBUG_RANK1=0
    export LSION_DEBUG_RANK2=1
    export LSION_ERROR_MSG_RANK=0
    export LSION_DEBUG_SILENT=1
    export OMP_NUM_THREADS=${NT}

    for arg in "$@"
    do
      hook=$(expr "${arg}" : "pre_hook:\(.*\)")
      if [ -n "${hook}" ]
      then
        ${hook}
      fi
    done

    ${MPIEXEC} ${MPIEXEC_NP} ${NP} ../test_${TESTNAME} 1>> stdout.log 2>> stderr.log

    for arg in "$@"
    do
      hook=$(expr "${arg}" : "post_hook:\(.*\)")
      if [ -n "${hook}" ]
      then
        ${hook}
      fi
    done

    grep "${PATTERN}" stdout.log > stdout_0.log
    grep "${PATTERN}" stderr.log > stderr_0.log

    for arg in "$@"
    do
      file=$(expr "${arg}" : "siondump:\(.*\)")
      if [ -n "${file}" ]
      then
        export LSION_DEBUG=siondump_${TESTNAME}
        ${SIONDUMP} -a -m -l "${file}" 1> stdout_siondump.log 2> stderr_siondump.log

        cat stdout_siondump.log | grep -v 'endianness' | grep -v 'SIONlib version' >> stdout_0.log
        cat stderr_siondump.log | grep -v 'Writing debug output'  >> stdout_0.log
      fi
    done

    failed=False
    for arg in "$@" diff:stderr_0.log:../test_${TESTNAME}_stderr_0.log diff:stdout_0.log:../test_${TESTNAME}_stdout_0.log
    do
      file1=$(expr "${arg}" : "diff:\(.*\):.*")
      file2=$(expr "${arg}" : "diff:.*:\(.*\)")
      if [ -n "${file1}" -a -n "${file2}" ]
      then
        if ! diff "${file1}" "${file2}" > /dev/null
        then
          echo "     test_${TESTNAME}              ... FAILED (diff in ${file1})"
          diff -u "${file2}" "${file1}"
          failed=True
        fi
      fi
    done
    if [ ${failed} = "True" ]
    then
      exit 1
    fi
  )
}
