Session: Matlab in Jupyter

Questions

  • How to reach the calculation nodes

  • How do I proceed to work interactively?

Objectives

  • Show how to reach the calculation nodes on UPPMAX and HPC2N by using Jupyter-lab

  • Test some commands on the calculation nodes

Note

  • It is possible to run Matlab directly on the login (including ThinLinc) nodes.

  • But this should only be done for shorter jobs or jobs that do not use a lot of resources, as the login nodes can otherwise become slow for all users.

  • If you want to work interactively with your code or data, you should start an interactive session. In the present lesson you will see how to run interactive jobs by using Jupyter-lab.

Compute allocations in this workshop

  • Rackham: naiss2024-22-1202

  • Kebnekaise: hpc2n2024-114

  • Cosmos: lu2024-7-80

Storage space for this workshop

  • Rackham: /proj/r-py-jl-m-rackham

  • Kebnekaise: /proj/nobackup/r-py-jl-m

  • Cosmos: your home directory should have plenty of space

There are several ways to run Matlab interactively

  • Directly on the login nodes: only do this for short jobs that do not take a lot of resources

  • As an interactive job on the computer nodes, launched via the batch system or Desktop On-Demand (LUNARC)

  • Jupyter notebooks (HPC2N, UPPMAX)

  • Like for Python it is possible to run Matlab in a notebook, i.e. in a web interface with possibility of inline

    figures and debugging. An easy way to do this is to load Python and Matlab modules. In shell:

# Load Matlab
$ ml matlab/2023a
# Load a Python version compatible with Matlab
$ ml python/3.10.8
# Create an environment called matlabenv (you can change this name)
$ python -m venv ./matlabenv
# Activate this environment
$ source matlabenv/bin/activate
# Perform installations: upgrade pip, and packages that you will need
$ pip install --upgrade pip
$ pip install -U scikit-learn
# Install Jupyterlab
$ pip install jupyterlab
# Install the Matlab proxy
$ pip install jupyter-matlab-proxy
$ deactivate

Start interactive session to not interfere with other users

# Ask for a suitable amount of time. Remember, this is the time the Jupyter notebook will be available! HHH:MM:SS.
$ interactive -A Project_ID -p core -n 1 -t 1:0:0
# If you use a GPU node add this snippet to the interactive command (for clarity after project ID
# -M snowy --gres:gpu:1

$ ml matlab/2023a
$ ml python/3.10.8

# Source the environment
$ source matlabenv/bin/activate
# Start JupyterLab
$ jupyter lab --no-browser &
$ <press enter to get to active prompt>
$ firefox <url from output above> &

When the Jupyter notebook interface starts, you can choose the MATLAB kernel version from the module you loaded. When you try to run a notebook, Matlab will ask for a type of license. Because you are running this notebook on our HPC center, you can choose the option Existing License and then Start MATLAB. It can take a minute or so to start.

Keypoints

  • There are several ways to run MATLAB jobs interactively. One can use the MATLAB GUI, the interactive sessions: salloc (HPC2N), interactive (UPPMAX), and also by running a Jupyter notebook on the computing nodes.

  • To run a Jupyter notebook on the computing nodes you will need to follow the prerequisites mentioned in the lesson.

  • If the Jupyter job is no longer needed, cancel it with scancel job_ID. It will not be canceled automatically if you just close the web browser.