Package: kslurm
Utility functions to make working with SLURM easier.InstallationThe recommend way to install kslurm is viapipx, a tool for installing python applications.
This will make kslurm globally available without infecting your global python environment.
Installation instructions for pipx can be found on theirwebsite.
Once installed, simply runpipxinstallkslurmNote that kslurm requires Python 3.9 or higher.
If pipx was installed using a lower version (e.g. 3.8), you will need to manually specify the python executable to use.
Activate the appropriate python version (e.g.module load python/3.10) so that when you runpython --version, the correct version appears.
Then runpipxinstallkslurm--python$(whichpython)For full kslurm features, including integration withpip, you need to source the init script, preferably in your~.bash_profile(the init script contains commands that may not be available on non-login nodes). You can do this by running:kpybash>>$HOME/.bash_profileFinally, you need to complete some basic configuration. First, set your SLURM account. Runkslurmconfigaccount-iThis will begin an interactive session letting you choose from the accounts available to you. Each account will be listed with it's LevelFS. The higher the LevelFS, the more underused the account is, so prefer accounts with higher values.Next, set your pipdir. This will be used to store python wheels and virtual envs. It should be in a permanent storage or project directory. For instance, on ComputeCanada servers, it should go in$HOME/projects/<account_name>/<user_name>/.kslurm. Use the following command:kslurmconfigpipdir<dir>Upgrading and uninstallingThe app can be updated by runningpipxupgradekslurmand removed usingpipxuninstallkslurmNeuroglia-helpers IntegrationSee thededicated page.Legacy Installerkslurm includes an installation script that, previously, was the recommended install method.
While it should technically still work, it is no longer supported and may be removed in the future.
Its instructions are included, for reference, below.Users who previously installed kslurm via this script should switch to a pipx install for long term support. Simply uninstallkslurmusing the instructions below, then install via pipx as described aboveInstallation is via the following command:curl -sSL https://raw.githubusercontent.com/pvandyken/kslurm/master/install_kslurm.py | python -If you wish to uninstall, run the same command with--uninstalladded to the end.The package can be updated by runningkslurm update.FeaturesCurrently offers four commands:kbatch: for batch submission jobs (no immediate output)krun: for interactive submissionkjupyter: for Jupyter sessionskpy: for python environment managementAll three use a regex-based argument parsing, meaning that instead of writing a SLURM file or supplying confusing--arguments, you can request resources with an intuitive syntax:krun 4 3:00 15G gpuThis command will request an interactive session with4cores, for3hr, using15GBof memory, and agpu.Anything not specfically requested will fall back to a default. For instance, by default the commands will request 3hr jobs using 1 core with 4GB of memory. You can also run a predefined job template using -jtemplate. Run either command with -J to get a list of all templates. Any template values can be overriden simply by providing the appropriate argument.The full list of possible requests, their syntaxes, and their defaults can be found at the bottom of the README.krunkrun is used for interactive sessions on the cluster. If you run krun all by itself, it will fire up an interactive session on the cluster:krunYou'll notice the server name in your terminal prompt will be changed to the cluster assigned to you. To end the session, simply use theexitcommand.You can also submit a specific program to run:krun 1:00 1G python my_program.pyThis will request a 1hr session with one core and 1 GB of memory. The output of the job will be displayed on the console. Note that your terminal will be tied to the job, so if you quit, or get disconnected, your job will end. (tmux can be used to help mitigate this, see thistutorial from Yalefor an excellent overview).Note that you should never request more than the recommended amount of time for interactive jobs as specified by your cluster administrator. For ComputeCanada servers, you should never request more than 3 hr. If you do, you'll be placed in the general pool for resource assignment, and the job could take hours to start. Jobs of 3hr or less typically start in less than a minute.kbatchJobs that don't require monitoring of the output or immediate submission, or will run for more than three hours, should be submitted usingkbatch. This command schedules the job, then returns control of the terminal. Output from the job will be placed in a file in your current working directory entitledslurm-[jobid].out.Improving onsbatch,kbatchdoes not require a script file. You can directly submit a command:kbatch 2-00:00 snakemake --profile slurmThis will schedule a 2 day job running snakemake.Of course, complicated jobs can still be submitted using a script. Note that kbatch explictely specifies the resources it knows about in the command line. Command line args override#SBATCH --directivesin the submit script, so at this time, you cannot use such directives to request resources unless they are not currently supported by kslurm. This may change in a future release.kjupyterThis command requests an interactive job running a jupyter server. As with krun, you should not request a job more than the recommended maximum time for your cluster (3hr for ComputeCanada). If you need more time than that, just request a new job when the old one expires.In addition to the desired resources, you should use the--venvflag to request a saved virtual environment (seekpy save). Jupyter will be started in whatever environment you request.jupyter-labshould already be installed in the venv.kjupyter 32G 2 --venv <your_venv_name>This will start a jupyter session with 32 GB of memory and 2 cores.If no venv is specified,kjupyterwill assume that thejupyter-labcommand is already available on the$PATH. This is useful to run a global instance of jupyter, or jupyter installed in an active venv. Note that this prevents installing jupyter on local scratch, so performance will take a hit.Unsupported SLURM argsCurrently, the only way to supply arguments to SLURM beyond the items listed below is to list it as an#SBATCH --directivein a submission script. This only works withkbatch, notkrunorkjupyter. A future release may support a method to supply these arguments directly on the command line. If you frequently use an option not listed below, make an issue and we can discuss adding support!Slurm SyntaxThe full syntax is outlined below. You can always run a command with-hto get help.ResourceSyntaxDefaultDescriptionTime[d-]dd:dd -> [days-]hh:mm3hrThe amount of time requestedCPUSd -> just a number1The number of CPUs requestedMemoryd(G/M)[B] -> e.g. 4G, 500MB4GBThe amount of memory requestedAccount--account <account name>The account under which to submit the job. A default account can be configured usingkslurm config account <account_name>GPUgpuFalseProvide flag to request 1 GPU instanceDirectory<any valid directory>./Change the current working directory before submitting the jobx11--x11FalseRequests x11 forwarding for GUI applicationskpykpy bundles a set of commands to help manage pip virtual environments on Slurm compute clusters, specifically addressing a few issues unique to such servers:Ephemeral venvsIn most use cases, python venvs are installed on compute clusters, ideally on local scratch storage.
This makes venvs inherently ephemeral.
Because installing a venv can take an appreciable amount of time, kpy packs tools to archive entire venvs for storage in a permanent local repository (ideally located in project-specific or permanent storage). Once saved, venvs can be quickly reloaded into a new compute environment.Note that copying venvs from one location to another is not a trivial task. The current setup has been tested on ComputeCanada servers without any issues so far, but problems may arise on another environment.No internetCompute clusters often don't have an internet connection, limiting our install repertoire to locally available wheels.
With kpy, venvs can be created on a login node (using the available internet connection), then saved and loaded onto a compute node.
Kpy also includes some optional bash tools (seekpy bash), including a wrapper around pip that prevents it from accessing the internet on compute nodes, and connecting it with a local private wheelhouse.Commandscreate# usagekpycreate[<version|3.x>][<name>]Create a new environment.
Name is optional; if not provided, a placeholder name will be created.
Version must be of the form3.xwhere x is any number (e.g3.8,3.10).
If provided, the corresponding python version will be used in the virtual env.
Note that an appropriate python executable must be somewhere on your path (e.g. for3.8->python3.8).
If not provided, the python version used to install kslurm will be used.If run on a login node, the env will be created in a$TMPDIR.
If run on a compute node, it will be created in$SLURM_TMPDIR.save# usagekpysave[-f]<name>Save the venv to your permanent cache.
This requires settingpipdirin the kslurm config (see below).
By default,savewill not oversave an existing cache, but-fcan be included to override this behaviour.
If a new name is provided, it will be used to update the current venv name and prompt.load# usagekpyload[<name>][--as<newname>]Load a saved venv from the cache.
If a venv called<name>already exists, the command will fail, as each name can only be used once.--as <newname>works around this by changing the name of the loaded venv (the name of the saved venv will remain the same)
Callingloadwithout any<name>will print a list of current cached venvs.activate# usagekpyactivate[<name>]Activate venv initialized usingcreateorload.
Name will be the same as the name appearing in the venv prompt (i.e. the name provided on initial loading or creation, through--as, or the last saved name).
This command only works on a compute node.
Venvs created on a login node cannot be directly activated using kpy.
Call without a name to list the venvs you can activate.list# usagekpylistList all saved venvs (i.e. venvs you canload)rm# usagekpyrm<name>Delete a saved venv.bash# usagekpybashEchos a line of bash script that can be added to your.bashrcfile:kpybash>>$HOME/.bashrcThis adds a few features to your command line environment:pip wrapper: Adds a wrapper around pip that detects if you are on a login node when runninginstall,wheel, ordownload. If not on a login node, the--no-indexflag will be appended to the command, preventing the use of an internet connection.wheelhouse management: Ifpipdiris configured in the kslurm config, a wheelhouse will be created in your pip repository. Any wheels downloaded usingpip wheelwill be placed in that wheelhouse, and all wheels in the wheelhouse will be discoverable bypip install, both on login and compute nodes.KappKapp provides a set of tools to manage singularity containers. Pull images from docker hub without worring about image size, pulling the same image twice, or tracking whether your ":latest" image is up to date. Kapp manages your.sifimage files so you can run them from anywhere on the cluster, without managing environment variables or remembering paths. Kapp managed images can be seamlessly consumed by snakemake workflows using the provided--singularity-prefixdirectory.pull# usagekapppull<image_uri>[-a<alias>][--mem<memory>]Pull an image from a repository. Currently, only docker-hub is supported. The image uri should look like this:[<scheme>://[<organization>/]<repo>:<tag>]# examplesdocker://ubuntu:latest
nipreps/fmriprep:21.0.2
busybox:latestNote that the scheme is optional, and defaults todocker. The organization should be omitted for official docker images.When you callkapp pull, the tag gets resolved to the specific container it points to. Thus, if you pull multiple tags pointing to the same container (e.g.:latestand its associated version tag), the container will only be pulled once. Plus, if tags get updated (e.g.:latestwhen a new release comes out),kapp pullwill download the latest version of the tag, even if you've pulled that tag before.When pulling a container, you can use-a <alias>to set an alias for the uri. This alias can be used in place of the uri in future kapp commands (except forkapp pull). For instance, you could download fmriprep and run it using the following:kapppullnipreps/fmriprep:latest-afmriprep
kapprunfmriprepBuilding.siffiles from docker containers can consume a significant amount of memory and resources, making an unsuitable operation for login nodes. kapp works around this by first downloading the container on the login node, then scheduling a build step on an interactive compute node. It will automatically try to estimate how much memory will be needed, but if a build fails due to lack of memory, you can specify how much memory to request using the--mem <memory>parameter. Note that very small containers will be built directly on the login node without a compute step.path# usagekapppath<uri_or_alias>Prints the path of the specified container. This creates an easy way to use kapp managed containers with any arbitrary singularity command:singularity-b/path/to/bind/dir$(kapppathmy_container)image# usagekappimage(list|rm<uri_or_alias>)Has two subcommandslistandrm <container>to list all pulled containers and remove a container.kapp image rmdoes not actually remove any data, it just removes the supplied uri (along with any aliases that point to it). This is because the underlying data may be used by other image tags. Dangling containers, which aren't pointed to by any local uris, can be deleted usingkapp purge danglingpurge# usagekapppurgedanglingDelete all dangling image files: i.e. files that aren't pointed to by any local uris. This command also removes any snakemake aliases pointing to the data.alias# usagekappaliaslistList all aliases currently in use, along with the containers they point to.exec,shell,run# usagekapp(exec|shell|run)<uri_or_alias>[args...]Simple wrapper aroundsingularity (exec|shell|run). No singularity args can be specified, only args for the container. If you need to specify singularity args, call singularity directly and usekapp path <container>to get the container path. Note that most singularity args (e.g. directory bids) can be specified using environment variable, and such variables will be consumed bykappas normal.snakemakePrints the path to the snakemake directory. This path can be supplied to the snakemake parameter--singularity-prefix, allowing snakemake to seamlessly consume containers downloaded using kapp. This is especially usefull for cluster execution without internet connection: containers can be pulled in advance on a login node, then used by snakemake later.Configurationkslurm currently supports a few basic configuration values, and more will come with time. All configuration can be set using the commandkslurmconfig<key><value>You can print the value of a configuration usingkslurmconfig<key>Current valuesaccount: Default account to use for kslurm commands (e.g.kbatch,krun, etc)pipdir: Directory to store cached venvs and wheels. Should be a project or permanent storage dir.