ATLAS Offline Software
multicore.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2 
3 from AthenaCommon import Logging
4 from ...decorators import timed
5 from ...utility import FileParser, ProcessManager, SingleProcessThread
6 from functools import partial
7 import os
8 import shutil
9 
10 # Get handle to Athena logging
11 logger = Logging.logging.getLogger("PowhegControl")
12 
13 
14 def multicore(process):
15  """! Run multiple Powheg processes, each in its own thread.
16 
17  @param process PowhegBox process.
18 
19  @author James Robinson <james.robinson@cern.ch>
20  """
21  # Construct random seeds - increment by 1e6 each time
22  logger.info("Running in multicore mode with {} subjobs".format(process.cores))
23  with open("pwgseeds.dat", "w") as random_seed_list:
24  for idx in range(process.cores):
25  random_seed_list.write(str(process.random_seed + int(idx * 1e6)) + "\n")
26 
27  # Remove iseed when providing seeds from pwgseeds.dat
28  FileParser("powheg.input").text_remove("^iseed")
29  logger.debug("Disabling iseed variable when multiple seeds are used")
30 
31  # Construct generation function
32  generation_fn = partial(multicore_untimed, process)
33 
34  if process.powheg_version == "V1":
35  __multicore_v1(generation_fn)
36  elif process.powheg_version == "V2" or process.powheg_version == "RES":
37  __multicore_multistage(process, generation_fn)
38  else:
39  raise ValueError("Powheg version {} is not supported for multicore running!".format(process.powheg_version))
40 
41 @timed("multi-core generation (V1)")
42 def __multicore_v1(generation_fn):
43  """! Run Powheg V1 generation in multi-core mode.
44 
45  @param generation_fn Function that can be called without argument to generate events.
46 
47  @author James Robinson <james.robinson@cern.ch>
48  """
49  generation_fn()
50 
51 @timed("multi-core generation (V2/RES)")
52 def __multicore_multistage(process, generation_fn):
53  """! Run Powheg V2/RES generation in multi-core mode.
54 
55  @param process PowhegBox process.
56  @param generation_fn Function that can be called without argument to generate events.
57 
58  @author James Robinson <james.robinson@cern.ch>
59  """
60  shutil.rmtree("multistage_inputs", ignore_errors=True)
61  os.mkdir("multistage_inputs")
62  shutil.copy("pwgseeds.dat", "multistage_inputs/pwgseeds.dat")
63  # bb4l semileptonic events need to be run on top of dileptonic LHE files
64  if(process.executable != "pwhg_semileptonic" ):
65  if process.stage_is_completed(1):
66  logger.info("=> Skipping multi-core generation (V2/RES): stage 1 <=")
67  else:
68  process.modify_parameter(stage = 1)
69  __multicore_multistage_stage_1(generation_fn, process.itmx1)
70  if process.stage_is_completed(2):
71  logger.info("=> Skipping multi-core generation (V2/RES): stage 2 <=")
72  else:
73  process.modify_parameter(stage = 2)
74  __multicore_multistage_stage_2(generation_fn)
75  if process.stage_is_completed(3):
76  logger.info("=> Skipping multi-core generation (V2/RES): stage 3 <=")
77  else:
78  process.modify_parameter(stage = 3)
79  __multicore_multistage_stage_3(generation_fn)
80  else:
81  process.modify_parameter(stage = 3)
82  __multicore_multistage_stage_3(generation_fn)
83 
84  process.modify_parameter(stage = 4)
85  __multicore_multistage_stage_4(generation_fn)
86 
87 @timed("multi-core generation (V2/RES): stage 1)")
88 def __multicore_multistage_stage_1(generation_fn, n_xgrid_iterations):
89  """! Run Powheg V2/RES (stage 1) generation in multi-core mode.
90 
91  @param generation_fn Function that can be called without argument to generate events.
92  @param n_xgrid_iterations Number of xgrid iterations to perform.
93 
94  @author James Robinson <james.robinson@cern.ch>
95  """
96  FileParser("powheg.input").text_replace("parallelstage.*", "parallelstage 1")
97  # For stage 1, we need n_xgrid_iterations iterations
98  for xgrid_iteration in range(1, n_xgrid_iterations + 1):
99  FileParser("powheg.input").text_replace("xgriditeration.*", "xgriditeration {}".format(xgrid_iteration))
100  shutil.copy("powheg.input", "multistage_inputs/powheg.input.parallelstage{ps}.xgriditeration{xgi}".format(ps=1, xgi=xgrid_iteration))
101  generation_fn()
102 
103 @timed("multi-core generation (V2/RES): stage 2)")
105  """! Run Powheg V2/RES (stage 2) generation in multi-core mode.
106 
107  @param generation_fn Function that can be called without argument to generate events.
108 
109  @author James Robinson <james.robinson@cern.ch>
110  """
111  FileParser("powheg.input").text_replace("parallelstage.*", "parallelstage 2")
112  shutil.copy("powheg.input", "multistage_inputs/powheg.input.parallelstage{ps}".format(ps=2))
113  generation_fn()
114 
115 @timed("multi-core generation (V2/RES): stage 3)")
117  """! Run Powheg V2/RES (stage 3) generation in multi-core mode.
118 
119  @param generation_fn Function that can be called without argument to generate events.
120 
121  @author James Robinson <james.robinson@cern.ch>
122  """
123  FileParser("powheg.input").text_replace("parallelstage.*", "parallelstage 3")
124  shutil.copy("powheg.input", "multistage_inputs/powheg.input.parallelstage{ps}".format(ps=3))
125  generation_fn()
126 
127 @timed("multi-core generation (V2/RES): stage 4)")
129  """! Run Powheg V2/RES (stage 4) generation in multi-core mode.
130 
131  @param generation_fn Function that can be called without argument to generate events.
132 
133  @author James Robinson <james.robinson@cern.ch>
134  """
135  FileParser("powheg.input").text_replace("parallelstage.*", "parallelstage 4")
136  shutil.copy("powheg.input", "multistage_inputs/powheg.input.parallelstage{ps}".format(ps=4))
137  generation_fn(parallel_stage=4)
138 
139 def multicore_untimed(process, parallel_stage=-1):
140  """! Run multiple Powheg processes, each in its own thread.
141 
142  @param process PowhegBox process.
143 
144  @author James Robinson <james.robinson@cern.ch>
145  """
146 
147  if not os.path.isfile(process.executable):
148  raise OSError("Powheg executable {} not found!".format(process.executable))
149  # if "pwhg_semileptonic" in process.executable and parallel_stage == 4:
150  # process.executable = "echo PowhegOTF._1.events | " + process.executable
151  threads = [SingleProcessThread(process.executable, seed_index=idx,
152  warning_output=(process.warning_output if hasattr(process,"warning_output") else None),
153  info_output=(process.info_output if hasattr(process,"info_output") else None),
154  error_output=(process.error_output if hasattr(process,"error_output") else None)) for idx in range(1, process.cores + 1)]
155  manager = ProcessManager(threads)
156  while manager.monitor():
157  pass
python.decorators.timed.timed
def timed(name)
Decorator to output function execution time.
Definition: timed.py:12
python.algorithms.generators.multicore.__multicore_multistage_stage_4
def __multicore_multistage_stage_4(generation_fn)
Run Powheg V2/RES (stage 4) generation in multi-core mode.
Definition: multicore.py:128
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.algorithms.generators.multicore.multicore_untimed
def multicore_untimed(process, parallel_stage=-1)
Run multiple Powheg processes, each in its own thread.
Definition: multicore.py:139
python.algorithms.generators.multicore.multicore
def multicore(process)
Run multiple Powheg processes, each in its own thread.
Definition: multicore.py:14
python.algorithms.generators.multicore.__multicore_multistage_stage_1
def __multicore_multistage_stage_1(generation_fn, n_xgrid_iterations)
Run Powheg V2/RES (stage 1) generation in multi-core mode.
Definition: multicore.py:88
python.algorithms.generators.multicore.__multicore_multistage
def __multicore_multistage(process, generation_fn)
Run Powheg V2/RES generation in multi-core mode.
Definition: multicore.py:52
python.algorithms.generators.multicore.__multicore_multistage_stage_2
def __multicore_multistage_stage_2(generation_fn)
Run Powheg V2/RES (stage 2) generation in multi-core mode.
Definition: multicore.py:104
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.algorithms.generators.multicore.__multicore_v1
def __multicore_v1(generation_fn)
Run Powheg V1 generation in multi-core mode.
Definition: multicore.py:42
Trk::open
@ open
Definition: BinningType.h:40
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
str
Definition: BTagTrackIpAccessor.cxx:11
python.algorithms.generators.multicore.__multicore_multistage_stage_3
def __multicore_multistage_stage_3(generation_fn)
Run Powheg V2/RES (stage 3) generation in multi-core mode.
Definition: multicore.py:116