Skip to content
Snippets Groups Projects
Commit eb985f89 authored by Rasmus Ringdahl's avatar Rasmus Ringdahl
Browse files

feat: add single core job example

Closes #2
parent 98965b08
No related branches found
No related tags found
1 merge request!1feat: add single core job example
# Single core jobs
A single core job is a job with only a single thread. This type of job is used when it is hard or impossible to make use of multiple cores/threads. A simple example could be a data parser that reads a file and transforms it into a more suitable format.
## How to run
To run the example do the following steps:
1. Log in to Lundgren
2. Change directory to the example code
3. Run `sbatch single_core_job.sh`
4. Check queue status by running `squeue`
5. When the job is completed check the file _single_core_job.log_
## Detailed description of the example
The batch script is the main file for the job allocation and preparation. Inside the python script a few environmental variables are fetched and printed out.
### The batch script
The batch script, _single_core_job.sh_, contains three sections. The first section contains input arguments to the Slurm scheduler. The second section loads Python into environment so it is accessible and lastly the a job step is performed.
The input arguments are defined with a comment beginning with SBATCH followed by the argument key and value. For easier readablility the -- method is used.
- __job-name:__ The name of the job is set to _demo_single_core_
- __time:__ The requeted time is set to 1 minute, _00:01:00_
- __ntasks:__ The number of tasks to be performed in this job is set to _1_.
- __cpus-per-task:__ The requested number of cores per task is set to _1_
- __mem:__ The requested memory is set to _50 MB_
- __output:__ The standard output should be sent to the file _single_core_job.log_
Python needs to be loaded into the environment in order to be accessible this is done in the next step with the __module__ command.
The job step with the single task is allocated and performed with the __srun__ command.
#### The python script
The python script represents the taskt to be done. In this case the task is to print out some environment variables that are set by Slurm.
The environment variable __JOB_ID__ can be used to create temporary files and folders. In this example it creates a file named <JOB_ID>.txt and writes the job name into it.
\ No newline at end of file
#! /bin/bash
#SBATCH --job-name=demo_single_core
#SBATCH --time=00:01:00
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=50MB
#SBATCH --output=single_core_job.log
# Loading Python into the environment
module load python/anaconda3-2024.02-3.11.7
srun python single_core_task.py
\ No newline at end of file
from datetime import datetime
import time
import os
def main():
# Read environment variables.
JOB_NAME = os.environ.get('SLURM_JOB_NAME','Unknown')
JOB_ID = os.environ.get('SLURM_JOB_ID','Unknown')
NUMBER_OF_CORES = os.environ.get('SLURM_CPUS_PER_TASK','Unknown')
MAXIMUM_MEMORY = os.environ.get('SLURM_MEM_PER_NODE','Unknown')
# Sleeping until next minute.
# This represents the calculations
current_time = datetime.now()
sleep_time = 60 - current_time.second
print('{} - Sleeping for {} seconds.'.format(current_time.strftime('%Y-%m-%d %H:%M:%S'), sleep_time))
time.sleep(sleep_time)
# Printing some things to standard output.
print('\nJob ID:\t\t\t{name}\nJob name:\t\t{id}\nAllocated cores:\t{cores}\nAllocated memory:\t{mem}'.format(
id=JOB_ID, name=JOB_NAME, cores=NUMBER_OF_CORES,mem=MAXIMUM_MEMORY))
# Writing some output to a file based on the Slurm job id.
output_file = '{}.txt'.format(JOB_ID)
with open(output_file,'w') as file:
file.write('This file was created by the job {} with id {}\n'.format
(JOB_NAME, JOB_ID))
print('\nJob completed.')
if __name__ == '__main__':
main()
......@@ -13,4 +13,11 @@ All the examples will use the method of creating a job and submit it to the Slur
## Examples
This section describes the different examples briefly so you can find an example that fits your needs.
__Note:__ _Since our Slurm cluster only has one node there can be a queue for running the examples depending on the workload of Lundgren_
\ No newline at end of file
__Note:__ _Since our Slurm cluster only has one node there can be a queue for running the examples depending on the workload of Lundgren._
#### Example 1 - Single core job
A single core job is a job with only a single thread. This type of job is used when it is hard or impossible to make use of multiple cores/threads.
A simple example could be a data parser that reads a file and transforms it into a more suitable format.
Learn more about the [example](https://gitlab.liu.se/rasri17/lundgren-examples/-/blob/main/1_single_core_job/README.md).
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment