2

I am working with the following .sh script which I submit using sbatch. Pairwise_3_P_a.do and Pairwise_3_P_c.do are linear steps that should be run only once, but Pairwise_3_P_b.do uses an array. I am having problems with Pairwise_3_P_c.do being run before the array is done.

Is there a way to fix this script using --wait or the wait bash command? Do I need to use srun, as in this answer: How to hold up a script until a slurm job (start with srun) is completely finished? ? I do not fully understand how to implement wait and --wait as mentioned there and in the documentation.

#!/bin/bash

#SBATCH --array=1-248

module load stata
stata-mp Pairwise_3_P_a.do
stata-mp Pairwise_3_P_b.do $SLURM_ARRAY_TASK_ID
stata-mp Pairwise_3_P_c.do

So far I have been trying to split the parallel step off into a separate .sh script, but this is annoying to run.

Thanks.

Isaac Liu
  • 199
  • 1
  • 2
  • 7
  • The shell `wait` command only does anything if a program was started in the background _by the shell directly_, as when using `&`. Now, if you can run `strata-mp --wait Pairwise_3_P_a.do & 3Pa_pid=$!`, then you can `wait "$3Pa_pid"` later; but I don't know `strata-mp` to know if that's valid usage. – Charles Duffy Oct 27 '20 at 15:36

1 Answers1

1

The way the submission script is written will create 248 jobs that will each do exactly the same first and last steps and only differ in the second step. This is probably not what you want.

You should split the three lines in three different jobs, only the second of which should be a job array. I assume order is important here.

Job1.sh:

#!/bin/bash
module load stata
stata-mp Pairwise_3_P_a.do

Job2.sh:

#!/bin/bash
#SBATCH --array=1-248

module load stata
stata-mp Pairwise_3_P_b.do $SLURM_ARRAY_TASK_ID

Job3.sh:

#!/bin/bash
module load stata
stata-mp Pairwise_3_P_c.do

With those files set, run the following commands:

FIRSTJOBID=$(sbatch --parsable Job1.sh)
SECONDJOBID=$(sbatch --parsable --dependendcy=afterok:$FIRSTJOBID Job2.sh)
sbatch --parsable --dependendcy=afterany:$SECONDJOBID Job3.sh

Also as you start stata-mp, it would make sense to request at least more than one core to enable Stata to use its multithreaded functionalities, with, for instance:

--cpus-per-task=8
damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • Thanks! I assume that I could but the content under "run the following commands" into an sbatch or shell script of its own? Does it matter what resources that script would request? And yeah, I'm using 8 cpus per task/appropriate memory, just omitted that code from this post – Isaac Liu Oct 28 '20 at 15:07
  • Run the commands in a Bash shell on the login/submission node. No need to submit a job to run them. – damienfrancois Oct 28 '20 at 15:28