#!/bin/bash
#PBS -N ISIS_MACCO_Eq
#PBS -q omp
#PBS -l select=1:ncpus=56:mem=240gb
#PBS -l walltime=48:0:0
#PBS -S /bin/bash
#PBS -j oe
#PBS -o debug/debugPBS_equilibrium_omp.log

# ATTENTION: j���utilise BASH pour cr��er des raccourcis en tant que fonctions,
# tandis que CSH ne permet pas de les cr��er.
# Donc ce script ne peut pas ��tre r����crit pour CSH

###############################################################################

# aller dans le bon dossier
cd $PBS_O_WORKDIR

export RERUN_SIMUL=true

# nom simulation (pour utiliser dans R)
export SIMUL_BASENAME="SQ1_equilibrium_prep"
# full simul name = base name + iteration

# simulation blanche de base (absolute path, see myIsis for reason)
export SIMUL_FILE=$PBS_O_WORKDIR/prep/$SIMUL_BASENAME.zip

# endroit o�� sauvegard��s des simul (pour utiliser dans R)
export ISIS_BD=$TMPDIR/myISISdb
# de pr��f��rence sur DATAWORK ou SCRATCH

# ex��cutable ISIS
export ISIS_BIN=/appli/emh-commun/isis-fish/isis-fish-4.4.8.1/isis-fish-4.4.8.1.jar

# ex��cutable JAVA
export JAVA_BIN=/appli/emh-commun/isis-fish/jdk17/bin/java

# shortcut to call ISIS from R
myIsis () { # pass the full simul name as the 1st arg
	# ATTENTION: when this function is called, we are in RUN_*/i* folder
	# relative paths are a bit difficult, so I use absolute paths
	$JAVA_BIN -Xmx2G -XX:+UseSerialGC -jar $ISIS_BIN \
		--option launch.ui false \
		--option perform.vcsupdate false \
		--option perform.migration false \
		--option perform.cron false \
		--option isis.home.directory $ISIS_BD \
		--simulateWithSimulation $1 $SIMUL_FILE
	# ces options viennent du code source ISIS > src/main/resources/templates/ssh/qsub-script.ftl
	# cf README pour la raison de fixer m��moire java �� 2GB
}
export -f myIsis

# calculer le nombre de c��urs, car utiliser $NCPUS pas fiable (cf tout en bas)
export mpiproc=$(cat $PBS_NODEFILE | wc -l)
# normalement = 28 �� n�� queue MPI choisie

###############################################################################

# supprimer qq fichiers
rm -f debug/debugR_equilibrium_omp.log debug/debugPBS_equilibrium_omp.log
mkdir -p $ISIS_BD

if [ "$RERUN_SIMUL" = true ] ; then
	rm -rf "RUN_${SIMUL_BASENAME}"
	# puis supprimer les simul pr��c��dentes
	find $ISIS_BD/simulations -type d -name "${SIMUL_BASENAME}_i*" -prune -exec rm -rf {} +
	#find $ISIS_BD/isis-database/simulations -type d -name "${SIMUL_BASENAME}_i*" -prune -exec rm -rf {} +
fi

. /usr/share/Modules/3.2.10/init/bash
module purge
module load R/3.6.3-intel-cc-17.0.2.174
module list
# enlever la version pour avoir R plus r��cent, mais �� v��rfier si {snow} est install��

# shortcut to call R, for MPI only
myR () {
	mpiexec -np $mpiproc $(R RHOME)/library/snow/RMPISNOW --no-restore --no-save --quiet $@
	# shouldn���t use --vanilla because errors don���t stop execution
}

time Rscript equilibrium_run_omp.R &> debug/debugR_equilibrium_omp.log

###############################################################################

echo simul: $SIMUL_BASENAME
echo job id: $PBS_JOBID
echo job name: $PBS_JOBNAME
echo

echo job submitted: $(qstat -f $PBS_JOBID | grep qtime) $(date +'%Z')
echo job started: $(qstat -f $PBS_JOBID | grep stime) $(date +'%Z')
echo current dir: $(pwd)
echo originating queue: $PBS_O_QUEUE
echo executing queue: $PBS_QUEUE
echo current machine: $PBS_O_HOST
echo how many cores: $NCPUS
echo how many threads: $OMP_NUM_THREADS
echo how many MPI process: $mpiproc
echo
