Running VSCode remotely on compute nodes (via tunnels)

Running VSCode remotely on compute nodes (via tunnels)

Introduction

You may wish to use VSCode to do work on compute nodes on the Athena cluster. This is certainly possible, and will enable you to use the powerful GPUs and other hardware available, while remaining in the development environment that you are used to working with. First, there are some things to make sure are set up correctly.

Preliminaries

First of course, you will need some version of VSCode installed on your local machine. Here is a good place to start for that.

Assuming that VSCode is installed, then you will need to ensure that it is set up to create and handle a secure “remote tunnel” to a node on the cluster. First, you need to install the “Remote - Tunnels” extension for VSCode, if it hasn’t been installed yet.

  • To do that, in an open VSCode window, press Ctrl+Shift+X, or click the “Extensions” icon on the left side of the window, as shown circled in red in this image to search for an extension to install:

    VSCode-Remote-Tunnels-1.png
  • In the search bar that appears, type “tunnels” and hit the Enter key. Click on the “Remote-Tunnels” item in the list, and then click the “Install” button to install the extension into VSCode.

    VSCode-Remote-Tunnels-2.png

That takes care of things on your machine, but you will also need to do some work in your account on Athena. You will need to install part of VSCode under your account on Athena too, these steps describe that process:

  • Download the VSCode cli (command line interface, make sure it’s the “cli” version that you get!), this link works as of the date of this writing:

curl -Lk 'https://code.visualstudio.com/thank-you?dv=linux64cli' --output vscode_cli_stable.tar.gz

Feel free to save the download as you wish, of course.

  • Put the downloaded file somewhere that makes sense, such as a “~/bin” directory under your account; create this if needed:

mkdir ~/bin mv vscode_cli_stable.tar.gz ~/bin
  • Change to the location the file was moved to, and extract its contents. You can run the 'ls' command to see what was extracted (should see the ‘code’ executable there):

user@athena1:$ cd ~/bin user@athena1:~/bin$ tar -xvf vscode_cli_stable.tar.gz user@athena1:~/bin$ ls -lah total 41M drwxrwx---. 2 user user 4.0K Apr 6 16:39 . drwx------. 107 user user 20K Apr 7 09:45 .. -rwxrwx---. 1 user user 30M Apr 1 05:34 code -rw-rw----. 1 user user 11M Apr 6 16:33 vscode_cli_stable.tar.gz

After this is done, hopefully everything is in place to create a VSCode tunnel to use with an Athena node, as will be discussed next.

Open a tunnel on a remote machine

Now that you have VSCode and the code CLI executable in place, you should be ready to open a tunnel to a node on the cluster. Starting with a CPU node is simplest. And, beginning with an interactive job on a node, is simplest also so that is what will be described next.

From your account on Athena, logged into the head node, enter a command like the following, to launch an interactive job on a node in the cluster:

srun --cpus-per-task 4 --mem 100G -p cpu --pty bash

This should result in you now being at a command prompt on a remote machine in the ‘cpu’ partition. Change to the directory, if need be, where you placed the ‘code’ executable, and enter the following command as shown in this example (you can enter ‘github’ instead of microsoft for provider if you like):

user@athena213:~/bin$ ./code tunnel user login --provider microsoft To sign in, use a web browser to open the page https://login.microsoft.com/device and enter the code B33ZD8C3L to authenticate.

Follow the instructions in the output. When you enter the link in a browser you will see a page such as this:

Authenticate_Microsoft.png

 

Enter the code in the web page, in this case it was ‘B33ZD8C3L’, and click ‘Next’. The rest of the process will be familiar to you as you go through the login steps just as with other things at VCU.

Now enter the following command to launch the tunnel using the VSCode CLI, skipping the server license agreement stuff that would pop up and wait for input. Also, it’s good to give your tunnel a meaningful name (the default is the hostname of the node the tunnel launches on…), here, the name is set to ‘my_tunnel’:

user@athena213:~/bin$ ./code tunnel --accept-server-license-terms --name my_tunnel * * Visual Studio Code Server * * By using the software, you agree to * the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and * the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement). * Open this link in your browser https://vscode.dev/tunnel/my_tunnel/lustre/home/user/bin

That’s it, the tunnel is launched and waiting for you to connect to it using VSCode (or, in your browser as the output suggests, if you like). To do so, click the icon in the very lower left of the VSCode Window, to open a “Remote Window”; after you click it some choices will appear in the “Search” bar, choose “Connect to Tunnel…”

OpenARemoteWindow.png

You should then be offered a drop down with the text “What type of account did you use to start this tunnel?”, in this case, choose “Microsoft”. Once you have been through this process, VSCode will add “Last used” after whichever one was used previously.

Shortly after that, you should see another drop-down list, and your new tunnel should be at the top of the list with its status, “Online”. In the example here, it offers “my_tunnel” with status “Online”.

Choose your tunnel and wait while you are connected; a couple of pieces of information are shown to let you know what’s happening. Eventually, once connected, the icon in the very lower left of the VSCode window will display “my_tunnel” and have turned bright blue, and you are now able to operate on the node you have tunneled to. It will look something like this:

ConnectedToTunnel.png

Now you can use the Explorer panel to view files and open them for editing and so on. Here is an example of opening a Jupyter notebook and running a small Python code:

ConnectedToJupyterNotebook.png

To close the tunnel and move to something else, you can press F1 and enter the command, “Remote: Close Remote Connection”, or, click the blue icon in the lower left labeled “my_tunnel”, and choose “Close Remote Connection” from the drop-down list that appears.

At the command line, from which you launched the tunnel and server, simply press “Ctrl+C” to exit the server. Close any modules that you may have loaded, and exit the machine if you are ready. Now it is time to look at how to make it easier to do, and repeat, these things, using a job script.

Using a job script to tunnel to a node

In case you are unfamiliar with using “job scripts” within the Slurm management system in place on Athena, here is some information about job scripts that may be helpful.

Assuming that you are familiar with using scripts to submit jobs to Slurm, a job script to launch a tunnel on a compute node is shown here without a lot of explanation:

#!/bin/bash ###################################################### # "#" followed by a "SBATCH" == Slurm parameter... # execute script in current directory #SBATCH --chdir=./ # want any .e/.o stuff to show up here too #SBATCH -e ./%x.err #SBATCH -o ./%x.out # name for the job; used by Slurm #SBATCH -J remote_tunnel_starter # set a time limit; don't keep tunnel open forever! #SBATCH -t 02:00:00 # run job in this queue/partition: #SBATCH -p cpu ###################################################### cd ~/bin # authenticate tunnel session with MS ./code tunnel user login --provider microsoft ./code tunnel --accept-server-license-terms --name my_tunnel

The main purpose of the script is to authenticate and launch the tunnel on the remote node to connect to. The ‘#SBATCH’ settings set things in the Slurm system, such as output filenames, job name, and so on. Please note the job time limit! It is set for 2 hours in this example. It would be a very good thing, to limit the time that such a job as this runs for, from the beginning, so that lots of tunnels aren’t left running forever on the cluster… The default MaxTime for the ‘cpu’ partition is unlimited. So please use something reasonable, like the 2 hour limit set here. If you discover that your tunnel needs to run for more time, you can adjust the time limit as described here.

If you want to use that script as-is, just copy it into a file under your account, and save it as, for example, “start_tunnel.sh”. Then to submit it to Slurm and get your tunnel going, enter this command in the directory containing the script:

sbatch start_tunnel.sh

You can monitor the job as usual with the command

squeue -u $USER

Here is a sample workflow of using the script to start a tunnel, and monitor things:

user@athena1:~/bin$ ls code start_tunnel.sh user@athena1:~/bin$ sbatch start_tunnel.sh Submitted batch job 2051333 user@athena1:~/bin$ squeue -u $USER JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) 2051333 cpu remote_t user R 0:24 1 athena215 user@athena1:~/bin$ sacct -j 2051333 --format=JobID,JobName,State,Elapsed,TimeLimit JobID JobName State Elapsed Timelimit ------------ ---------- ---------- ---------- ---------- 2051333 remote_tu+ RUNNING 00:01:48 02:00:00 2051333.bat+ batch RUNNING 00:01:48 user@athena1:~/bin$ ls code remote_tunnel_starter.out remote_tunnel_starter.err start_tunnel.sh

Everything looks good so far. But you still need to finish the authentication process as described above, and you may be wondering how on earth to do that. It’s actually very easy! First we will look a the output files now in the directory, and as is always a good idea, look at error output first for any problems:

user@athena1:~/bin$ cat remote_tunnel_starter.err user@athena1:~/bin$ cat remote_tunnel_starter.out ------------------------------------------------------------------ To sign in, use a web browser to open the page https://login.microsoft.com/device and enter the code BTGR6KWGX to authenticate.

As hoped, nothing in the .err (error) output file. The standard output file, however, has the information needed to authenticate the tunneling session. As you can see, it’s just the same type of information that you used from the command line, when doing this through an interactive job. So, at this point, you would just enter the URL in your browser, and then enter the generated code, just like shown previously. That’s all there is to it!

As before, you can now connect to the compute node with VSCode, as described above. When you are done, please remember to scancel the job to close down the tunnel!

scancel 2051333

Tunneling to a GPU node

You may need to do work on a GPU node in the cluster to make use of CUDA and other technologies. Setting up a tunnel to do so is very similar to the previous example that used a CPU node. A few small changes need to be made to the job script and that’s about it. Here is an example script that you can copy and use:

#!/bin/bash ###################################################### # "#" followed by a "SBATCH" == Slurm parameter... # execute script in current directory #SBATCH --chdir=./ # want any error/output stuff to show up here too #SBATCH -e ./%x.err #SBATCH -o ./%x.out # name for the job; used by squeue #SBATCH -J remote_tunnel_starter_gpu # set a time limit; don't keep tunnel open forever! #SBATCH -t 02:00:00 #SBATCH --partition=gpu #SBATCH --gres=shard:1 ###################################################### cd ~/bin # authenticate tunnel session with github #./code tunnel user login --provider github # authenticate tunnel session with MS ./code tunnel user login --provider microsoft ./code tunnel --accept-server-license-terms --name my_tunnel

Note the differences in this and the previous job script; the partition is different, and the ‘--gres’ setting is added. You may have to adjust these to suit your needs.

Submitting the job, monitoring it, everything is just the same, so that won’t be repeated here. Authenticating the session is the same also, so you will have to check the .out file for the job, for the code to enter into the authentication page (again, link is in the output also).

Now for the difference. Open VSCode again, and connect to the tunnel as before ('my_tunnel' again in this example). Once you are connected, and the blue icon in the lower left reads “my_tunnel', you are ready to do some work. An example is shown below using a user’s PyTorch example code. The Python environment is also shown, which was chosen in the process of running the code; Python 3.11.5 in this case. The run on 1 shard took about 4.5 minutes, and the output is scrolled a bit to show the tensor displayed upon completion.

A-Tunnel-On-GPU-Node.png

Now that the test is over, if you want, close the connection, and then scancel the job to shut down the tunnel. Don’t forget, this job is on a timer!

You can of course do other work from within VSCode while tunneled into the node. For instance, in the hamburger menu at the upper left, choose “File → Terminal → New Terminal” to open a terminal on the node and do work on the command line there, just as if you’d used srun to do an interacive job there. Since you’re on a GPU node you can then do C programming using CUDA, for instance, and so on. Here is a user loading the CUDA module on a GPU node:

VSCodeUseTerminalOnTunnel.png

Conclusion

Hopefully this information helps you to tunnel to a node in the Athena cluster and do your work using VSCode. If there are problems or questions, please feel free to reach out to us. Good luck with your work!

https://research.vcu.edu/cores/hprc/

In case you are interested in learning more about CUDA development, here is a very useful link: https://developer.nvidia.com/blog/even-easier-introduction-cuda/.

Back to the top

Acknowledgement

If you benefit from HPRC resources, please consider adding the following acknowledgement to papers. 

"High Performance Computing resources provided by the High Performance Research Computing (HPRC) core facility at Virginia Commonwealth University (https://hprc.vcu.edu ) were used for conducting the research reported in this work."