How to write the correct SLURM directives to run Python multiprocessing codes on HPC

I have a question about writing SLURM directives for Python multiprocessing codes to be run on high-performance computing (HPC) platforms. If I want to set the number of processes in the code to 8, should I set ‘ntasks=8’ or ‘cpus-per-task=8’ in the SLURM directive set-up? Some people say that ‘cpus-per-task’ refers to the number of threads, whereas Python multiprocessing does not use multithreading method due to Global Interpreter Lock (GIL).
I am confident that Python multiprocessing definitely works. Because I tried it on my local laptop and I saw a decrease in computing time with the increased number of processes. So, the question is how to set up the SLURM directives correctly in order for codes using multiprocessing to be run on HPC.

Here is an example code called ‘multiproc1.py’:

import multiprocessing as mp
import time
import numpy as np
import sys
def square(x):
    # Three lines below are added to increase the computation time for each process
        random_matrix = np.random.randn(150,150)
        eigenvalues = np.linalg.eigvals(random_matrix)
        a = np.max(np.abs(eigenvalues))
        return x * x
if __name__ == '__main__':
        ntasks = int(sys.argv[1])   # for command line passing
        print('hello world')
        start_time = time.time()
        pool = mp.Pool(ntasks)
        outputs_async = pool.map_async(square, [ki for ki in range(10000)])
        outputs = outputs_async.get()
        end_time = time.time()
        print(end_time-start_time)

Here is the SLURM file to run the above code on a HPC platform.

#!/bin/bash
#SBATCH --partition=cascade
#SBATCH --nodes=1
#SBATCH --ntasks=2

module purge
module load foss/2022a
module load Python/3.10.4
module load SciPy-bundle/2022.05

srun python3 multiproc1.py $SLURM_NTASKS

I tried ‘ntasks=2,4,8’ and they all took around the same time to finish. Here is the output when I tried ‘ntasks=2’:

hello world
hello world
98.75774931907654
102.18375992774963

It outputs ‘hello world’ twice. However, it is supposed to print it only once (when I tried on my local laptop). I suspect that each task just runs the whole code on its own without even doing the parallel programming. I also tried setting ‘cpus-per-task’ (which is the correct directive to set for ‘parfor’ in Matlab, I tried), however, it took longer time to finish a job. I guess ‘cpus-per-task’ refers to multithreading and Python multiprocessing does not use multithreading method.

I tried this code on my local laptop and varied the number of processes (1,2,3,4). For 1 process, it took around 89 seconds; for 2 processes, it took around 57 seconds; for 3 processes, it took around 41 seconds; for 4 processes, it took around 36 seconds. So, I am confident that multiprocessing definitely works on my local laptop. I reckon multiprocessing should also work on HPC platforms.

So, the question is: how to write the SLURM directives correctly for the codes using multiprocessing to be run on HPC platforms?

New contributor

Tony Ding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật