This lesson is in the early stages of development (Alpha version)

Scheduling jobs

Overview

Teaching: 45 min
Exercises: 30 min
Questions
  • What is a scheduler and why are they used?

  • How do I launch a program to run on any one node in the cluster?

  • How do I capture the output of a program that is run on a node in the cluster?

Objectives
  • Run a simple Hello World style program on the cluster.

  • Submit a simple Hello World style script to the cluster.

  • Use the batch system command line tools to monitor the execution of your job.

  • Inspect the output and error files of your jobs.

Job scheduler

An HPC system might have thousands of nodes and thousands of users. How do we decide who gets what and when? How do we ensure that a task is run with the resources it needs? This job is handled by a special piece of software called the scheduler. On an HPC system, the scheduler manages which jobs run where and when.

The following illustration compares these tasks of a job scheduler to a waiter in a restaurant. If you can relate to an instance where you had to wait for a while in a queue to get in to a popular restaurant, then you may now understand why sometimes your job do not start instantly as in your laptop.

/hpc-intro/Compare%20a%20job%20scheduler%20to%20a%20waiter%20in%20a%20restaurant

Job scheduling roleplay (optional)

Your instructor will divide you into groups taking on different roles in the cluster (users, compute nodes and the scheduler). Follow their instructions as they lead you through this exercise. You will be emulating how a job scheduling system works on the cluster.

notes for the instructor here

The scheduler used in this lesson is SGE. Although SGE is not used everywhere, running jobs is quite similar regardless of what software is being used. The exact syntax might change, but the concepts remain the same.

Running a batch job

The most basic use of the scheduler is to run a command non-interactively. Any command (or series of commands) that you want to run on the cluster is called a job, and the process of using a scheduler to run the job is called batch job submission.

In this case, the job we want to run is just a shell script. Let’s create a demo shell script to run as a test. The landing pad will have a number of terminal-based text editors installed. Use whichever you prefer. Unsure? nano is a pretty good, basic choice.

[yourUsername@login12 ~]$  cat example-job.sh
[yourUsername@login12 ~]$  chmod +x example-job.sh
#!/bin/bash -l

echo -n "This script is running on "
hostname

Creating our test job

Run the script. Does it execute on the cluster or just our login node?

Solution

[yourUsername@login12 ~]$  ./example-job.sh
This script is running on 

This job runs on the login node.

If you completed the previous challenge successfully, you probably realise that there is a distinction between running the job through the scheduler and just “running it”. To submit this job to the scheduler, we use the qsub command.

[yourUsername@login12 ~]$  qsub  example-job.sh
Your job 36855 ("example-job.sh") has been submitted

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 qstat -u yourUsername.

[yourUsername@login12 ~]$  qstat -u yourUsername
job-ID  prior   name	   user         state submit/start at     queue                          slots ja-task-ID
-----------------------------------------------------------------------------------------------------------------
3979883 3.50000 example-jo yourUser     r     06/25/2020 11:36:30 Arya@node-b00a-003                 1

We can see all the details of our job, most importantly that it is in the r or running state. Sometimes our jobs might need to wait in a queue (w or waiting) or have an error (E).

The best way to check our job’s status is with qstat. Of course, running qstat repeatedly to check on things can be a little tiresome. To see a real-time view of our jobs, we can use the watch command. watch reruns a given command at 2-second intervals. This is too frequent, and will likely upset your system administrator. You can change the interval to a more reasonable value, for example 15 seconds, with the -n 15 parameter. Let’s try using it to monitor another job.

[yourUsername@login12 ~]$  qsub  example-job.sh
[yourUsername@login12 ~]$  watch -n 15 qstat -u yourUsername

You should see an auto-updating display of your job’s status. When it finishes, it will disappear from the queue. Press Ctrl-C when you want to stop the watch command.

Where’s the output?

On the login node, this script printed output to the terminal – but when we exit watch, there’s nothing. Where’d it go?

Cluster job output is typically redirected to a file in the directory you launched it from. Use ls to find and read the file.

Customising a job

The job we just ran used all of the scheduler’s default options. In a real-world scenario, that’s probably not what we want. The default options represent a reasonable minimum. Chances are, we will need more cores, more memory, more time, among other special considerations. To get access to these resources we must customize our job script.

Comments in UNIX shell scripts (denoted by #) are typically ignored, but there are exceptions. For instance the special #! comment at the beginning of scripts specifies what program should be used to run it (you’ll typically see #!/bin/bash). Schedulers like SGE also have a special comment used to denote special scheduler-specific options. Though these comments differ from scheduler to scheduler, SGE’s special comment is #$ . Anything following the #$ comment is interpreted as an instruction to the scheduler.

Let’s illustrate this by example. By default, a job’s name is the name of the script, but the -N option can be used to change the name of a job. Add an option to the script:

[yourUsername@login12 ~]$  cat example-job.sh
#!/bin/bash -l
#$  -N new_name

echo -n "This script is running on "
hostname
echo "This script has finished successfully."

Submit the job (using qsub example-job.sh) and monitor it:

[yourUsername@login12 ~]$  qstat -u yourUsername
job-ID  prior   name       user         state submit/start at     queue                          slots ja-task-ID 
-----------------------------------------------------------------------------------------------------------------
38191   0.00000 new_name   yourUser     qw    06/25/2020 13:25:26                                    1

Fantastic, we’ve successfully changed the name of our job!

Setting up email notifications

Jobs on an HPC system might run for days or even weeks. We probably have better things to do than constantly check on the status of our job with qstat. Looking at the manual page for qsub, can you set up our test job to send you an email when it finishes?

Resource requests

But what about more important changes, such as the number of cores and memory for our jobs? One thing that is absolutely critical when working on an HPC system is specifying the resources required to run a job. This allows the scheduler to find the right time and place to schedule our job. If you do not specify requirements (such as the amount of time you need), you will likely be stuck with your site’s default resources, which is probably not what you want.

The following are several key resource requests:

Note that just requesting these resources does not make your job run faster! We’ll talk more about how to make sure that you’re using resources effectively in a later episode of this lesson.

Submitting resource requests

Submit a job that will use 1 full node and 1 minute of walltime.

Solution

[yourUsername@login12 ~]$  cat example-job.sh
#!/bin/bash -l
#$  -l h_rt= 00:01:10

echo -n "This script is running on "
sleep 60 # time in seconds
hostname
echo "This script has finished successfully."
[yourUsername@login12 ~]$  qsub  example-job.sh

Why are the SGE runtime and sleep time not identical?

Job environment variables

When SGE runs a job, it sets a number of environment variables for the job. One of these will let us check what directory our job script was submitted from. The SGE_O_WORKDIR variable is set to the directory from which our job was submitted. Using the SGE_O_WORKDIR variable, modify your job so that it prints (to stdout) the location from which the job was submitted.

Solution

Resource requests are typically binding. If you exceed them, your job will be killed. Let’s use walltime as an example. We will request 30 seconds of walltime, and attempt to run a job for two minutes.

[yourUsername@login12 ~]$  cat example-job.sh
#!/bin/bash -l
#$  -N long_job
#$  -l h_rt= 00:00:30

echo -n "This script is running on ..."
sleep 120 # time in seconds
hostname
echo "This script has finished successfully."

Submit the job and wait for it to finish. Once it is has finished, check the log file.

[yourUsername@login12 ~]$  qsub  example-job.sh
[yourUsername@login12 ~]$  watch -n 15 qstat -u yourUsername
[yourUsername@login12 ~]$  cat long_job.o*
This script is running on:
node-d00a-007.myriad.ucl.ac.uk

Our job was killed for exceeding the amount of resources it requested. Although this appears harsh, this is actually a feature. Strict adherence to resource requests allows the scheduler to find the best possible place for your jobs. Even more importantly, it ensures that another user cannot use more resources than they’ve been given. If another user messes up and accidentally attempts to use all of the cores or memory on a node, SGE will either restrain their job to the requested resources or kill the job outright. Other jobs on the node will be unaffected. This means that one user cannot mess up the experience of others, the only jobs affected by a mistake in scheduling will be their own.

Cancelling a job

Sometimes we’ll make a mistake and need to cancel a job. This can be done with the qdel 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!).

[yourUsername@login12 ~]$  qsub  example-job.sh
[yourUsername@login12 ~]$  qstat -u yourUsername
Your job 38759 ("example-job.sh") has been submitted

job-ID  prior   name       user         state submit/start at     queue                          slots ja-task-ID 
-----------------------------------------------------------------------------------------------------------------
38759   0.00000 example-jo yourUser     qw    06/25/2020 14:27:46                                    1

Now cancel the job with its job number (printed in your terminal). A clean return of your command prompt indicates that the request to cancel the job was successful.

[yourUsername@login12 ~]$  qdel 38759
# ... Note that it might take a minute for the job to disappear from the queue ...
[yourUsername@login12 ~]$  qstat -u yourUsername
# ...(no output from qstat when there are no jobs to display)...

Cancelling multiple jobs

We can also cancel all of our jobs at once using the -u option. This will delete all jobs for a specific user (in this case us). Note that you can only delete your own jobs.

Try submitting multiple jobs and then cancelling them all with ` -u yourUsername`.

Solution

First, submit a trio of jobs:

[yourUsername@login12 ~]$  qsub  example-job.sh
[yourUsername@login12 ~]$  qsub  example-job.sh
[yourUsername@login12 ~]$  qsub  example-job.sh

Then, cancel them all:

[yourUsername@login12 ~]$  qdel -u yourUsername

Other types of jobs

Up to this point, we’ve focused on running jobs in batch mode. SGE also provides the ability to start an interactive session.

There are very frequently tasks that need to be done interactively. Creating an entire job script might be overkill, but the amount of resources required is too much for a login node to handle. A good example of this might be building a genome index for alignment with a tool like HISAT2. Fortunately, we can run these types of tasks as a one-off with qrsh.

For an interactive session, you reserve some compute nodes via the scheduler and then are logged in live, just like on the login nodes. These can be used for live visualisation, software debugging, or to work up a script to run your program without having to submit each attempt separately to the queue and wait for it to complete.

[yourUsername@login12 ~]$  qrsh -l mem=512M,h_rt=2:00:00

All qsub options are supported like regular job submission with the difference that with qrsh they must be given at the command line, and not with any job script. Once a node is allocated to you, you should be presented with a bash prompt. Note that the prompt will likely change to reflect your new location, in this case the worker node we are logged on. You can also verify this with hostname.

When you are done with the interactive job, type exit to quit your session.

Key Points

  • The scheduler handles how compute resources are shared between users.

  • Everything you do should be run through the scheduler.

  • A job is just a shell script.

  • If in doubt, request more resources than you will need.