Skip to main content
Print

Parallelization using OpenMP

Why OpenMP?

Open Multi Processing is designed for parallel programming on shared-memory architectures, where multiple processors or cores share the same address space. Threads within a process can communicate and synchronize through shared variables. OpenMP is designed for parallel programming on shared-memory architectures, where multiple processors or cores share the same address space. In this article we’ll use the python standard multiprocessing library to run a task/process for x times in a single compute node.

Step 1: Create the Python Script

$ cd /path/to/your/directory

Use a text editor of your choice to create a Python script named hello_openmp.py. This script will print “Hello, World!”  but in parallel using OpenMP.

import os 
#import the python standard multiprocessing library 
import multiprocessing as mp 
def print_hello(_):
    print(f"Hello ARCC2 | Process ID: {os.getpid()}")
if __name__ =="__main__":
    # Set the number of tasks or precesses:
    # This is as same as --ntasks-per-node
    num_tasks = 10
    # use mp.pool and run print_hello function:
    with mp.Pool(processes=num_tasks) as pool:
        pool.map(print_hello, range(num_tasks))

Step 2: Create the Slurm Job Script

Create a Slurm job script named hello_openMP.sh using a text editor:

#!/bin/bash
#SBATCH --job-name=hello_openMP
#SBATCH --output=hello_openMP.out 
#SBATCH --error=hello_openMP.err
#SBATCH	--nodes=1
#SBATCH --ntasks-per-node=10
#SBATCH --cpus-per-task=1 
#SBATCH --time=00:01:00
#SBATCH --qos=proj-xxx
# Load the necessary modules: 
# Run the Python Script with OpenMP parallelization: 
python3.6  hello_openMP.py

Now, let’s go through each part of the script:

  1. #!/bin/bash:

    • Purpose: This line is called the shebang, and it specifies the shell to be used to interpret the script. In this case, it’s Bash.
  2. #SBATCH --job-name=openmp_hello:

    • Purpose: Specifies the name of the job. It will be used in job listings and output files.
  3. #SBATCH --output=openmp_hello.out:

    • Purpose: Specifies the file to which the standard output (stdout) of the job will be written.
  4. #SBATCH --error=openmp_hello.err:

    • Purpose: Specifies the file to which the standard error (stderr) output of the job will be written.
  5. #SBATCH --nodes=1:

    • Purpose: Requests a total number of nodes for the job. 1 in this example.
  6. #SBATCH --ntasks-per-node=10:

    • Purpose: Specifies the number of tasks (processes) to be launched on each node. 10 in this example.
  7. #SBATCH --cpus-per-task=1:

    • Purpose: Specifies the number of CPU cores assigned to each task. This means each hello world function in our python script is task and will use 1 CPU cores.
  8. #SBATCH --time=00:01:00:

    • Purpose: Specifies the maximum time the job is allowed to run. 1 minute(s) (00:01:00) in this example.
  9. #SBATCH --qos=proj-xxx:

    • Purpose: Specify the quality of service allocated.
  10. python3.6 hello_openMP.py:

    • Purpose: Executes the Python script hello_openMP.py that contains the “Hello World” program with OpenMP parallelization.

Up Next: Parallelization using MPI (Message Passing Interface)

In the next article parallelization of a simple task across two nodes with distributed memory using Message Passing Library is discussed. 

Table of Contents