Interactive work on the compute nodes

  • Understand what an interactive session is

  • Understand why one may need an interactive session

  • Start an interactive session

  • Test to be on an interactive node with the right amount of cores

  • End an interactive session

  • Start an interactive session with multiple cores

  • Test to be on an interactive node with multiple cores

  • Run an interactive-friendly Python script on multiple cores

  • Run an interactive-unfriendly Python script on multiple cores

  • End an interactive session with multiple cores

Compute allocations in this workshop

  • Rackham: naiss2024-22-415

  • Kebnekaise: hpc2n2024-052

Storage space for this workshop

  • Rackham: /proj/hpc-python

  • Kebnekaise: /proj/nobackup/python-hpc

Introduction

Some users develop Python code in a line-by-line fashion.

  • These users typically want to run a (calculation-heavy) script frequently, to test if the code works.

  • However, scheduling each new line is too slow, as it can take minutes before the new code is run.

  • Instead, there is a way to directly work with such code: use an interactive session.

Some other users want to run programs that (1) use a lot of CPU and memory, and (2) need to be persistent/available. One good example is Jupyter.

  • Running such a program on a login nodes would harm all other users on the login node.

  • Running such a program on a computer node using sbatch would not allow a user to connect to it.

  • In such a case: use an interactive session.

About Jupyter

  • It will be shown in the parallel session how to run Jupyter on the compute nodes.

  • You can also check the lesson about Jupyter on compute nodes in our Introduction to running R, Python and Julia in HPC workshop)

  • Documentation about Jupyter on HPC2N

  • Documentation about Jupyter on UPPMAX

An interactive session is a session with direct access to a compute node. Or alternatively: an interactive session is a session, in which there is no queue before a command is run on a compute node.

In this session, we show how to: - the different way HPC2N and UPPMAX provide for an interactive session - start an interactive session - check to be in an interactive session - check to have booked the expected amount of cores - end the interactive session

The different way HPC2N and UPPMAX provide for an interactive session

Here we define an interactive session as a session with direct access to a compute node. Or alternatively: an interactive session is a session, in which there is no queue before a command is run on a compute node.

This differs between HPC2N and UPPMAX:

  • HPC2N: the user remains on a login node. All commands can be sent directly to the compute node using srun

  • UPPMAX: the user is actually on a computer node. Whatever command is done, it is run on the compute node

Start an interactive session

To start an interactive session, one needs to allocate resources on the cluster first.

The command to request an interactive node differs per HPC cluster:

Cluster

interactive

salloc

HPC2N

Works

Recommended

UPPMAX

Recommended

Works

Start an interactive session in the simplest way

To start an interactive session in the simplest way, is shown here:

Use:

interactive -A [project_name]

Where [project_name] is the UPPMAX project name, for example interactive -A naiss2024-22-415.

The output will look similar to this:

[richel@rackham4 ~]$ interactive -A naiss2024-22-415
You receive the high interactive priority.
You may run for at most one hour.
Your job has been put into the devcore partition and is expected to start at once.
(Please remember, you may not simultaneously have more than one devel/devcore job, running or queued, in the batch system.)

Please, use no more than 8 GB of RAM.

salloc: Pending job allocation 9093699
salloc: job 9093699 queued and waiting for resources
salloc: job 9093699 has been allocated resources
salloc: Granted job allocation 9093699
salloc: Waiting for resource configuration
salloc: Nodes r314 are ready for job
 _   _ ____  ____  __  __    _    __  __
| | | |  _ \|  _ \|  \/  |  / \   \ \/ /   | System:    r314
| | | | |_) | |_) | |\/| | / _ \   \  /    | User:      richel
| |_| |  __/|  __/| |  | |/ ___ \  /  \    |
 \___/|_|   |_|   |_|  |_/_/   \_\/_/\_\   |

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

              User Guides: http://www.uppmax.uu.se/support/user-guides
              FAQ: http://www.uppmax.uu.se/support/faq

              Write to support@uppmax.uu.se, if you have questions or comments.


[richel@r314 ~]$

Note that the prompt has changed to show that one is on an interactive node.

Indeed, all you need is the UPPMAX/HPC2N project name. However, this simplest way may have some defaults settings that do not fit you.

Start an interactive session in a more elaborate way

The simplest way to start an interactive session may have some defaults settings that do not fit you:

  • session duration is too short

  • the session has too few cores available

Tip

Type along!

Here we show how start an interactive session in a more elaborate way, with a custom session duration and a custom amount of cores:

Here we start an interactive session on the devcore partition, with a custom session duration and a custom amount of cores:

interactive -p devcore -n [n_tasks] --time=[duration] -A naiss2024-22-415

where [n_tasks] is the number of tasks, [duration] is the time given in HHH:MM:SS format, and [project_name] is the UPPMAX project name.

The parameters -p devcore mean that the devcore partition is used, which results in jobs that start either faster or just as fast. Nice!

As an example, here an interactive job is started with 4 tasks, for 1 hour, for the UPPMAX project naiss2024-22-415:

interactive -p devcore -n 4 --time=1:00:00 -A naiss2024-22-415

Note that, as Slurm uses 1 task per core by default, we request 4 cores.

The output will be similar to this:

[bjornc@rackham2 ~]$ interactive -A naiss2024-22-415 -p devcore -n 4 -t 10:00
You receive the high interactive priority.
There are free cores, so your job is expected to start at once.

Please, use no more than 6.4 GB of RAM.

Waiting for job 29556505 to start...
Starting job now -- you waited for 1 second.

Check to be in an interactive session

To check to be in an interactive session, do:

hostname

If the output is r[number].uppmax.uu.se, where [number] is a number, you are on a computer node. Well done!

If the output is rackham[number].uppmax.uu.se, where [number] is a number, you are still on a login node.

Check to have booked the expected amount of cores

To check to have booked the expected amount of cores:

srun hostname

The output should be one line of r[number].uppmax.uu.se, where [number] is a number, you have booked one core.

If the output is more than one line of r[number].uppmax.uu.se, where [number] is a number, you have booked more than one core.

If the output is rackham[number].uppmax.uu.se, where [number] is a number, you are still on a login node.

Here is an example of output when 4 cores had been booked:

[bjornc@r483 ~]$ srun hostname
r483.uppmax.uu.se
r483.uppmax.uu.se
r483.uppmax.uu.se
r483.uppmax.uu.se

Running a Python script in an interactive session

To run a Python script in an interactive session, first load the Python modules:

module load python/3.11.8

To run a Python script on 1 core, do:

python [my_script.py]

where [my_script.py] is the path to a Python script, for example srun python ~/my_script.py.

To run a Python script on each of the requested cores, do:

srun python [my_script.py]

where [my_script.py] is the path to a Python script, for example srun python ~/my_script.py.

Not all Python scripts are suitable for an interactive session. This will be demonstrated by two Python example scripts.

Our first example Python script is called sum-2args.py: it is a simple script that adds two numbers from command-line arguments:

import sys

x = int(sys.argv[1])
y = int(sys.argv[2])

sum = x + y

print("The sum of the two numbers is: {0}".format(sum))

Our second example Python script is called add2.py: it is a simple script that adds two numbers from user input:

# This program will add two numbers that are provided by the user

# Get the numbers
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))

# Add the two numbers together
sum = a + b

# Output the sum
print("The sum of {0} and {1} is {2}".format(a, b, sum))

End the interactive session

To end and interactive session, do:

exit

This will look similar to this:

[bjornc@r484 ~]$ exit

exit
[screen is terminating]
Connection to r484 closed.

[bjornc@rackham2 ~]$

Note that the prompt has changed to contain rackham[number].uppmax.uu.se, where [number] is a number, which indicates one is back on a login node.

Exercises

Exercise

10 minutes

In these exercises:

  • we prepare to use two Python example scripts

  • we use a minimal interactive session

  • we use a more optimized interactive session

Exercise 0: be able to use the Python scripts

  • Go to the program directory in your cloned HPC-Python repository

  • There you’ll find the two programs that we will use:

sum-2args.py and add2.py

  • On UPPMAX:

cd /proj/HPC-python/[username]/HPC-python/Exercises/examples/programs
  • On HPC2N:

cd /proj/nobackup/python-hpc/[username]/HPC-python/Exercises/examples/programs``
  • After loading a Python module, run it.

python sum-2args.py 3 14
python add2.py
  • Add numbers according to prompts.

  • If this works you are good to go for the interactive session below!

Exercises

Learning objectives

  • Start an interactive session with multiple cores

  • Test to be on an interactive node with multiple cores

  • Run an interactive-friendly Python script on multiple cores

  • Run an interactive-unfriendly Python script on multiple cores

  • End an interactive session

Exercise 1: start an interactive node

Start an interactive node with 2 cores

Exercise 2: check to be in an interactive session

Confirm to be on a compute node.

Exercise 3: check to have booked the expected amount of cores

Confirm to have booked one core.

Exercise 4.1. Running the first Python script in an interactive session on all cores

Run the first Python example script, sum-2args.py, in an interactive session, on all cores.

Exercise 4.2. Running a second Python script in an interactive session on all cores

Run the second Python example script, add2.py, in an interactive session, on all cores.

Exercise 5: exit

Exit the interactive node

Conclusion

Keypoints

You have:

  • seen how to use a compute node interactively, which differs between HPC2N and UPPMAX

  • checked if we are in an interactive session

  • checked if we have booked the right number of cores

  • run Python scripts in an interactive session, which differs between HPC2N and UPPMAX

  • seen that not all Python scripts can be run interactively on multiples cores

  • exited an interactive session