run_unfold_ppfx_multiprocess.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import subprocess
4 import multiprocessing
5 import os
6 import argparse
7 def make_cmd(nnodes, ranks_per_node, procid, nuniv_per_rank, nfiles_per_rank, ppfx_batch_idx, totuniv, totfiles):
8 
9  cmd = []
10  name_cmd = []
11  covered = False
12  # ncmd = nuniv*nfiles
13  nfiles_last_batch = totfiles%nfiles_per_rank
14 
15  if nnodes > 1 and int(procid) == nnodes - 1:
16  ifilefirst = totfiles - nfiles_last_batch
17  ifilelast = ifilefirst + nfiles_last_batch -1
18  else:
19  ifilefirst = int(procid) * nfiles_per_rank
20  ifilelast = ifilefirst + nfiles_per_rank - 1
21 
22  for irank in range(ranks_per_node):
23  iunivfirst = ppfx_batch_idx * totuniv + irank * nuniv_per_rank
24  iunivlast = iunivfirst + nuniv_per_rank - 1
25 
26  icmd = "cafe -bq -nr --numuccinc /development/NDAna/numucc_inc/specprod_numuccinc.C ppfx -1 -1 {} {} 0 {} {}".format(
27  iunivfirst, iunivlast, ifilefirst, ifilelast)
28  iname_cmd = "ProcID: {} , Ppfx batch index: {}, cafe -bq -nr --numuccinc /development/NDAna/numucc_inc/specprod_numuccinc.C ppfx -1 -1 {} {} 0 {} {}".format(
29  procid, ppfx_batch_idx, iunivfirst, iunivlast, ifilefirst, ifilelast)
30  cmd.append(icmd)
31  name_cmd.append(iname_cmd)
32 
33  return (name_cmd, cmd)
34 
35 class Consumer(multiprocessing.Process):
36  def __init__(self, task_queue, result_queue):
37  multiprocessing.Process.__init__(self)
38  self.task_queue = task_queue
39  self.result_queue = result_queue
40 
41  def run(self):
42  proc_name = self.name
43  while True:
44  next_task = self.task_queue.get()
45  if next_task is None:
46  print('{}: Exiting'.format(proc_name))
47  break
48  print('{}: {}'.format(proc_name, next_task))
49  answer = next_task()
50  self.result_queue.put(answer)
51  return
52 
53 class Task(object):
54  def __init__(self, a, b):
55  # a is for command name, b is for command
56  self.a = a
57  self.b = b
58 
59  def __call__(self):
60  print('Starting -- {} : {}'.format(self.a, self.b))
61  subprocess.call(self.b, shell=True)
62  print('Finishing -- {} : {}'.format(self.a, self.b))
63  return
64 
65  def __str__(self):
66  return '%s : %s' % (self.a, self.b)
67 
68 
69 def RunMultiProcess(nCMD, lCMD, num_consumers=0):
70  # Establish communication queues
71  tasks = multiprocessing.Queue()
72  results = multiprocessing.Queue()
73 
74  # Start consumers
75  if num_consumers == 0:
76  num_consumers = multiprocessing.cpu_count()
77  print('Creating {} consumers'.format(num_consumers))
78  consumers = [Consumer(tasks, results) for i in range(num_consumers)]
79  for w in consumers:
80  w.start()
81 
82  num_jobs = len(lCMD)
83  for i in range(num_jobs):
84  tasks.put(Task(nCMD[i], lCMD[i]))
85 
86  # Add a poison pill for each consumer
87  for i in range(num_consumers):
88  tasks.put(None)
89  # Start printing results
90  while num_jobs:
91  result = results.get()
92  print('Result: {}'.format(result))
93  num_jobs -= 1
94  return
95 
96 
97 if __name__ == "__main__":
98  parser = argparse.ArgumentParser(
99  prog='make_cafe_cmd',
100  description="""Form cafe cmd in shifter jobs. """,
101  epilog="Questions and Comments to dingpf@fnal.gov")
102 
103  # required arguments
104  parser.add_argument('--nnodes', default=1, type=int,
105  help="number of nodes;")
106  parser.add_argument('--nuniv_per_rank', default=5, type=int,
107  help="number of universes per rank;")
108  parser.add_argument('--nfiles_per_rank', default=100, type=int,
109  help="number of files per rank;")
110  # parser.add_argument('--nuniv-rank', default=20, type=int,
111  # help="number of universes per rank;")
112  parser.add_argument('--totuniv', default=1000, type=int,
113  help="total number of universes;")
114  parser.add_argument('--ppfx_batch_idx', default=0, type=int,
115  help="Batch index for Ppfx universes;")
116  parser.add_argument('--totfiles', default=56324, type=int,
117  help="total number of files;")
118  parser.add_argument('--nranks_per_node', default=25, type=int,
119  help="number of ranks per node;")
120 
121  args = parser.parse_args()
122 
123  if args.nranks_per_node == -1:
124  nranks_per_node = multiprocessing.cpu_count()
125  else:
126  nranks_per_node = args.nranks_per_node
127 
128  if 'JOBSUBJOBID' in os.environ:
129  jsub_id = os.environ['JOBSUBJOBID']
130  jid = jsub_id.split('@')[0].split('.')[0]
131  procid = jsub_id.split('@')[0].split('.')[1]
132  else:
133  jid = os.environ["SLURM_JOB_ID"]
134  procid = os.environ["SLURM_PROCID"]
135 
136  (ncmd, cmd) = make_cmd(args.nnodes, nranks_per_node, procid, args.nuniv_per_rank, args.nfiles_per_rank, args.ppfx_batch_idx, args.totuniv, args.totfiles)
137 
138  for idx in range(len(cmd)):
139  print(ncmd[idx], cmd[idx])
140  RunMultiProcess(ncmd, cmd, nranks_per_node)
141 
142  # subprocess.call("mkdir -p /output/{}/{}/{}".format(args.mode, jid, procid), shell=True)
143  # subprocess.call("mv /dev/shm/{}/{}/*.root /output/{}/{}/{}".format(jid, procid, args.mode, jid, procid), shell=True)
144  # subprocess.call("rm -rf /dev/shm/{}".format(jid), shell=True)
void split(double tt, double *fr)
Definition: novas.h:112
bool print
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
def RunMultiProcess(nCMD, lCMD, num_consumers=0)
def make_cmd(nnodes, ranks_per_node, procid, nuniv_per_rank, nfiles_per_rank, ppfx_batch_idx, totuniv, totfiles)
def __init__(self, task_queue, result_queue)