Skip to content

Commit f383dae

Browse files
authored
Merge pull request #351 from NYU-RTS/robjy-arrayjob
added job array example
2 parents 6aaf20d + 75c40ca commit f383dae

2 files changed

Lines changed: 60 additions & 7 deletions

File tree

docs/hpc/13_tutorial_intro_hpc/04_scheduler_fundamentals.mdx

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ To submit this task to the scheduler, we use the `sbatch` command. This creates
6464
Submitted batch job 137860
6565
```
6666

67-
And that’s all we need to do to submit a job. Our work is done – now the scheduler takes over and tries to run the job for us. While the job is waiting to run, it goes into a list of jobs called the `queue`. To check on our job’s status, we check the queue using the command `squeue -u NetID`.
67+
And that’s all we need to do to submit a job. Our work is done – now the scheduler takes over and tries to run the job for us. While the job is waiting to run, it goes into a list of jobs called the `queue`. To check on our job’s status, we check the queue using the command `squeue --me`.
6868
```bash
69-
[NetID@log-1 ~]$ squeue -u NetID
69+
[NetID@log-1 ~]$ squeue --me
7070
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
7171
137860 normal example- usernm R 0:02 1 c5-59
7272
```
@@ -93,7 +93,7 @@ hostname
9393
Submit the job and monitor its status:
9494
```bash
9595
[NetID@log-1 ~]$ sbatch example-job.sh
96-
[NetID@log-1 ~]$ squeue -u NetID
96+
[NetID@log-1 ~]$ squeue --me
9797
JOBID ACCOUNT NAME ST REASON START_TIME TIME TIME_LEFT NODES CPUS
9898
38191 yourAccount hello-wo PD Priority N/A 0:00 1:00:00 1 1
9999
```
@@ -156,7 +156,7 @@ hostname
156156
Submit the job and wait for it to finish. Once it is has finished, check the log file.
157157
```bash
158158
[NetID@log-1 ~]$ sbatch example-job.sh
159-
[NetID@log-1 ~]$ squeue -u NetID
159+
[NetID@log-1 ~]$ squeue --me
160160
cat slurm-38193.out
161161
This job is running on: c1-14
162162
slurmstepd: error: *** JOB 38193 ON gra533 CANCELLED AT 2017-07-02T16:35:48
@@ -169,7 +169,7 @@ Our job was killed for exceeding the amount of resources it requested. Although
169169
Sometimes we’ll make a mistake and need to cancel a job. This can be done with the `scancel` command. Let’s submit a job and then cancel it using its job number (remember to change the walltime so that it runs long enough for you to cancel it before it is killed!).
170170
```bash
171171
[NetID@log-1 ~]$ sbatch example-job.sh
172-
[NetID@log-1 ~]$ squeue -u NetID
172+
[NetID@log-1 ~]$ squeue --me
173173
Submitted batch job 38759
174174

175175
JOBID ACCOUNT NAME ST REASON TIME TIME_LEFT NODES CPUS
@@ -179,7 +179,7 @@ Now cancel the job with its job number (printed in your terminal). A clean retur
179179
```bash
180180
[NetID@log-1 ~]$ scancel 38759
181181
# It might take a minute for the job to disappear from the queue...
182-
[NetID@log-1 ~]$ squeue -u NetID
182+
[NetID@log-1 ~]$ squeue --me
183183
JOBID USER ACCOUNT NAME ST REASON START_TIME TIME TIME_LEFT NODES CPUS
184184
```
185185

@@ -189,6 +189,59 @@ We can also cancel all of our jobs at once using the `-u` option. This will dele
189189
Try submitting multiple jobs and then cancelling them all with `scancel -u NetID`.
190190
:::
191191

192+
## Job Arrays
193+
> Job arrays offer a mechanism for submitting and managing collections of similar jobs quickly and easily, useful for repetitive workloads that follow a common job pattern. This greatly improves overall performance, since job arrays with millions of tasks can be submitted in milliseconds (subject to configured size limits) and the scheduler can quickly identify cases when no more array tasks are eligible to start.<br />
194+
-- [Slurm documentation](https://slurm.schedmd.com/job_array.html)
195+
196+
### Job Array Example
197+
As stated above, you can have a single sbatch job submit multiple jobs by using Job Arrays. This example shows how you can run the same python file with a range of input parameters from a single sbatch file.
198+
199+
Copy the following code into a file named `run_array.sh`:
200+
```bash
201+
#!/bin/bash
202+
#SBATCH --job-name=array_test
203+
#SBATCH --output=array_%j.out
204+
#SBATCH --nodes=1
205+
#SBATCH --ntasks=1
206+
#SBATCH --cpus-per-task=1
207+
#SBATCH --mem=1M
208+
#SBATCH --time=00:10:00
209+
#SBATCH --account=torch_pr_XXX_XXXXX
210+
#SBATCH --array=0-4
211+
212+
LEARNING_RATES=(0.01 0.05 0.1 0.5 1.0)
213+
CURRENT_LR=${LEARNING_RATES[$SLURM_ARRAY_TASK_ID]}
214+
215+
python array_test.py --lr $CURRENT_LR
216+
217+
```
218+
Be aware that you'll need to put in your own account.
219+
220+
Copy the following code into a file named `array_test.py`:
221+
```bash
222+
import argparse
223+
224+
def main():
225+
parser = argparse.ArgumentParser(description="Job Array Demo")
226+
parser.add_argument('--lr', type=float, default=0.01,
227+
help="Learning Rate")
228+
args = parser.parse_args()
229+
current_lr = args.lr
230+
print(f"Starting training with learning rate: {current_lr}")
231+
232+
if __name__ == '__main__':
233+
main()
234+
235+
```
236+
237+
Place those files in the same directory and then submit with the command:
238+
```bash
239+
sbatch run_array.sh
240+
```
241+
When they're done running you should find 5 files in your directory named `array_job_id.out` where the `job_id` will be a long integer. Each of those files will contain one of the values in the learning rate array in `run_array.sh`
242+
243+
Please see the [Slurm Documentation](https://slurm.schedmd.com/job_array.html) for more details.
244+
192245
## Other Types of Jobs
193246
Up to this point, we’ve focused on running jobs in batch mode. Slurm also provides the ability to start an interactive session.
194247

docs/hpc/13_tutorial_intro_hpc/08_running_parallel_job.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ srun amdahl
109109
```
110110
As before, use the Slurm status commands to check whether your job is running and when it ends:
111111
```bash
112-
[NetID@log-1 amdahl]$ squeue -u NetID
112+
[NetID@log-1 amdahl]$ squeue --me
113113
```
114114
Use `ls` to locate the output file. The `-t` flag sorts in reverse-chronological order: newest first. What was the output?
115115

0 commit comments

Comments
 (0)