简体   繁体   中英

Determine number of processors assigned to job

I am trying to right a python program that will use a pool of workers to perform tasks on a Linux hpc computing environment. I want to set the number of workers in the pool equal to the number of threads that I have requested for my job.

Commands such as multiprocessing.cup_count() and num_procs return 64, which is the number of threads on a node, but not the number of threads assigned to my job.

My current solution is to grep the command in the batch file for designating number of threads, and then examining the output:

os.system('grep -i "batch -n" * > output')

file_ = open('output','r')
file_lines = file_.readlines()
file_.close()

for line in file_lines:
  elems = line.strip().split()
  if '-n' in elems:
    position = elems.index('-n')
    num_procs = elems[position+1]

Is there a more succinct and straightforward way of doing this though?

Depending on the scheduler you use, you can query the number of cores your job requested from the scheduler. For SGE - $NSLOTS ; for SLURM - $SLURM_CPUS_PER_TASK ; for PBS - $PBS_NP .

For example for SGE:

import os
#for SGE:
nslots = int(os.environ['NSLOTS']))

# for SLURM:
nslots = int(os.environ['SLURM_CPUS_PER_TASK']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM