13.8.5. Macro “relauncherCodeFlowrateThreaded.py”
13.8.5.1. Objective
The goal of this macro is to show how to handle a code run on several threads. In order to this, the
usual sequential runner will be removed and another runner will be called to do the job. The
flowrate code is provided with Uranie and has been also used and discussed throughout these
macros.
13.8.5.2. Macro
"""
Example of code launching in threaded mode
"""
from URANIE import DataServer, Relauncher
import ROOT
# Create input attributes
rw = DataServer.TAttribute("rw")
r = DataServer.TAttribute("r")
tu = DataServer.TAttribute("tu")
tl = DataServer.TAttribute("tl")
hu = DataServer.TAttribute("hu")
hl = DataServer.TAttribute("hl")
lvar = DataServer.TAttribute("l")
kw = DataServer.TAttribute("kw")
# Create the output attribute
yhat = DataServer.TAttribute("yhat")
d = DataServer.TAttribute("d")
# Set the reference input file and the key for each input attributes
fin = Relauncher.TFlatScript("flowrate_input_with_values_rows.in")
fin.addInput(rw)
fin.addInput(r)
fin.addInput(tu)
fin.addInput(tl)
fin.addInput(hu)
fin.addInput(hl)
fin.addInput(lvar)
fin.addInput(kw)
# The output file of the code
fout = Relauncher.TFlatResult("_output_flowrate_withRow_.dat")
fout.addOutput(yhat)
fout.addOutput(d) # Passing the attributes to the output file
# Constructing the code
mycode = Relauncher.TCodeEval("flowrate -s -r")
mycode.setOldTmpDir()
mycode.addInputFile(fin) # Adding the input file
mycode.addOutputFile(fout) # Adding the output file
# Fix the number of threads
nthread = 3
# Create the Threaded runner
run = Relauncher.TThreadedRun(mycode, nthread)
run.startSlave() # Start the master
if run.onMaster():
# Define the DataServer
tds = DataServer.TDataServer("tdsflowrate", "Design of Experiments")
mycode.addAllInputs(tds)
tds.fileDataRead("flowrateUniformDesign.dat", False, True)
lanceur = Relauncher.TLauncher2(tds, run)
# resolution
lanceur.solverLoop()
run.stopSlave() # Stop the slaves (necessary even for a sequential)
# Draw the result
can = ROOT.TCanvas("pouet", "foo", 1)
tds.Draw("yhat:rw", "", "colZ")
The only difference when comparing this macro to the previous one (see Macro) is the runner creation:
# Fix the number of threads
nthread = 3
# Create the Threaded runner
run = Relauncher.TThreadedRun(mycode, nthread)
The TSequentialRun object becomes a TThreadedRun object whose construction request on top of the
assessor, the number of threads to be used. Apart from that, the master is created and the rest is
crystal clear, leading to the following plot.
13.8.5.3. Graph
Figure 13.49 Representation of the output as a function of the first input with a colZ option