The --ntasks is useful if you have commands that you want to execute in parallel in one batch script. These can be two separate commands, separated by & or two commands used in the bash ( | ) channel.
for example
Using the default ntasks = 1
#!/bin/bash
Throw a warning
Job step creation is temporarily disabled, retrying
The default number of tasks was set to one, and so the second task cannot be started until the first task is completed. This work will end in about 22 seconds. To break this:
sacct -j515058 --format=JobID,Start,End,Elapsed,NCPUS JobID Start End Elapsed NCPUS ------------ ------------------- ------------------- ---------- ---------- 515058 2018-12-13T20:51:44 2018-12-13T20:52:06 00:00:22 1 515058.batch 2018-12-13T20:51:44 2018-12-13T20:52:06 00:00:22 1 515058.0 2018-12-13T20:51:44 2018-12-13T20:51:56 00:00:12 1 515058.1 2018-12-13T20:51:56 2018-12-13T20:52:06 00:00:10 1
Here task 0 started and completed (after 12 seconds), and then task 1 (after 10 seconds). So that the total user time is 22 seconds.
To run both of these commands at the same time:
#!/bin/bash
Execution of the same sacred command as above
sacct -j 515064 --format=JobID,Start,End,Elapsed,NCPUS JobID Start End Elapsed NCPUS ------------ ------------------- ------------------- ---------- ---------- 515064 2018-12-13T21:34:08 2018-12-13T21:34:20 00:00:12 2 515064.batch 2018-12-13T21:34:08 2018-12-13T21:34:20 00:00:12 2 515064.0 2018-12-13T21:34:08 2018-12-13T21:34:20 00:00:12 1 515064.1 2018-12-13T21:34:08 2018-12-13T21:34:18 00:00:10 1
Here, the overall work took 12 seconds. There is no risk that tasks are waiting for resources, since the number of tasks is indicated in the batch script, and therefore the task has resources for simultaneously executing such a number of commands.
Each task inherits the parameters specified for the batch script. That is why --ntasks=1 must be specified for each srun task, otherwise each task uses --ntasks=2 so the second command will not be executed until the first task is completed.
Another caveat for tasks that inherit package options if --export=NONE specified as the package parameter. In this case, --export=ALL must be specified for each srun command; otherwise, the environment variables set in the sbatch script are not inherited by the srun command.
Additional notes:
When using bash channels, you may need to specify --nodes = 1 to prohibit commands on both sides of channels operating on separate nodes.
When using & to run commands at the same time, wait is vital. In this case, without the wait command, task 0 will be canceled if task 1 is successful.