simulate Namespace Reference

Functions

def runSimulations (smcTable, simDir, buildDir, paramStringTemplate, exeNames, material, onNode=False, nodes=[], cores=0, verbose=False)
 
def numberOfCommands (smcTable, exeNames)
 
def mergeOutputFiles (smcTable, simDir, exeNames, material, verbose=False)
 
def getFreeCores (nodes)
 

Function Documentation

◆ getFreeCores()

def simulate.getFreeCores (   nodes)
174 def getFreeCores(nodes):
175  cwd = os.getcwd()
176  freeCores = []
177  for i in nodes:
178  node = 'node%r' % i
179  num = subprocess.check_output('ssh -Y %s python %s/numOfCores.py exit' % (node,cwd), shell=True)
180  # use local node
181  #num = subprocess.check_output('python %s/numOfCores.py' % (cwd), shell=True)
182  print ('%i free nodes in %s' % (int(num), node))
183  for i in range(int(num)):
184  freeCores.append(node)
185  return freeCores
return int(ret)+1
def getFreeCores(nodes)
Definition: simulate.py:174

References int().

Referenced by runSimulations().

◆ mergeOutputFiles()

def simulate.mergeOutputFiles (   smcTable,
  simDir,
  exeNames,
  material,
  verbose = False 
)
133 def mergeOutputFiles(smcTable, simDir, exeNames, material, verbose=False):
134  # open smcTable
135  file = open(smcTable)
136  # ignore header line
137  params = file.readline()
138  if verbose:
139  print("Writing combined data files in folder %s" % simDir)
140  # reading smcTable line by line
141  for line in file.readlines():
142  # extracting parameters
143  params = line.split()[2:]
144  # convert list to tuple
145  params = tuple(i for i in params)
146  # read in output files
147  paramStringOut = "_"+material+"_"+"_".join(params)+".txt"
148  out=""
149  # read files
150  for executable in exeNames:
151  # skip executables that are labeled -nodata
152  if "-nodata" in executable:
153  continue
154  file = simDir + executable.split()[0] + paramStringOut
155  #print("Reading in file: %s" % file)
156  # merge the output files into a single file
157  try:
158  content = open(file).readline()
159  except:
160  # open(file,'w').write("0 0 0")
161  # content = "0 0 0"
162  raise Exception(
163  "Directory %s doesn't contain the data files *%s.\nRemove the directory %s and run the simulations again" % (
164  simDir, paramStringOut, simDir))
165  sys.exit(-1)
166  out = out + content + " "
167  # file that will be written
168  outFile = simDir + "data" + paramStringOut
169  if verbose:
170  print(" data%s: %s" % (paramStringOut, out))
171  open(outFile, "w").write(out)
172 
173 # get number of free cores
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::enable_if_t< dt !=data_source::global_mem, void > write(PacketType &packet_data, DataScalar ptr)
write, a template function used for storing the data to local memory. This function is used to guaran...
Definition: TensorContractionSycl.h:221
TupleImpl< sizeof...(Types), Types... > tuple
Definition: Tuple.h:267
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet print(const Packet &a)
Definition: GenericPacketMath.h:1166
def mergeOutputFiles(smcTable, simDir, exeNames, material, verbose=False)
Definition: simulate.py:133

References Eigen::internal.print(), and Eigen::TensorSycl::internal.write().

◆ numberOfCommands()

def simulate.numberOfCommands (   smcTable,
  exeNames 
)
125 def numberOfCommands(smcTable, exeNames):
126  # count lines, ignore header line
127  fileCount = open(smcTable)
128  numObs = -1
129  for line in fileCount.readlines(): numObs += 1
130  fileCount.close()
131  return len(exeNames) * numObs
132 
def numberOfCommands(smcTable, exeNames)
Definition: simulate.py:125

Referenced by runSimulations().

◆ runSimulations()

def simulate.runSimulations (   smcTable,
  simDir,
  buildDir,
  paramStringTemplate,
  exeNames,
  material,
  onNode = False,
  nodes = [],
  cores = 0,
  verbose = False 
)
17 def runSimulations(smcTable, simDir, buildDir, paramStringTemplate, exeNames, material, onNode=False, nodes=[], cores=0, verbose=False):
18  # run make to produce the executables
19  print("Running make in the build directory %s" % buildDir)
20  try:
21  output = subprocess.check_output('make -j 8 -C ' + buildDir, shell=True)
22  except:
23  print(output)
24  raise RuntimeError('Calling make in the buildDirectory failed')
25 
26  # make simulation directory absolute
27  simDir = os.getcwd() + "/" + simDir
28  # build directory is already absolute
29 
30  # make the simulation directory if necessary
31  if not os.path.exists(simDir):
32  if verbose:
33  print('Creating directory for simulation output: ' + simDir)
34  os.mkdir(simDir)
35 
36  # check if params file exists
37  if not os.path.exists(smcTable):
38  raise RuntimeError('smcTable does not exist')
39 
40 
41  # if cores>0 a script is produced, splitting the code into #cores executables
42  if cores:
43  # todo failure if there are not enough commands per cores
44  numCmdTotal = numberOfCommands(smcTable, exeNames)
45  numCmd = int(ceil(numCmdTotal/cores))
46  cores = int(ceil(numCmdTotal/numCmd))
47  print("Number of commands %d, per script %d, number of scripts: %d" % (numCmdTotal, numCmd, cores))
48  #override buildDir (only necessary on mercuryCloud, since we cannot run the python script there)
49  onMercuryCloud = False
50  if onMercuryCloud:
51  print("Adjusting script for mercuryCloud")
52  buildDir = open('build').read().split()[0]
53  #buildDir = "~/MercuryLab/release/Drivers/USER/MercuryLab/Buttercup/Calibration"
54  print("Copy files to cluster (rsync -avz %s $cloud:) and execute ./run.sh in the sim directory before continuing" % material)
55  else:
56  print("Adjusting script for msm3")
57  buildDir = open('build').read().split()[0]
58  #buildDir = "~/Code/Lab/build/Drivers/USER/MercuryLab/Buttercup/Calibration"
59  print("1) cd %r\n2) Adjust node numbers run.sh.\n3) Execute 'source ./run.sh'" % simDir)
60  # set counter into which file the next command is written
61  coresCounter = -1
62  numCmdCounter = 0
63  # write run.sh and create an empty file $coresCounter.sh
64  open(simDir+"/run.sh", "w").write("#!/bin/bash\nchmod +x *.sh\nmake -j 16 -C %s\n" % buildDir)
65  for i in range(cores):
66  if onMercuryCloud:
67  open(simDir+"/run.sh", "a").write("nohup ./"+str(i)+".sh &\n")
68  else:
69  n = nodes[i%len(nodes)]
70  if n.isdigit():
71  n = "node%s" % n
72  open(simDir + "/run.sh", "a").write("sleep 3\n ssh %s \"cd \"`pwd`\"; nohup ./%d.sh > %s.out\"&\n" % (n,i,i))
73 
74  # open smcTable
75  #print("Opening smcTable: %s" % smcTable)
76  file = open(smcTable)
77  # ignore header line
78  params = file.readline()
79 
80  # reading smcTable line by line
81  for line in file.readlines():
82  # extracting parameters
83  params = line.split()[2:]
84  # convert list to tuple
85  params = tuple(i for i in params)
86  # set simulation parameters
87  paramString = paramStringTemplate % params
88  paramString += "-param _"+material+"_"+"_".join(params)
89  #print("Running simulations for: %s" % paramString)
90 
91  if cores:
92  for executable in exeNames:
93  if numCmdCounter == 0:
94  coresCounter += 1
95  open(simDir + str(coresCounter) + ".sh", "w").write("")
96  cmd = '%s/%s %s\n' % (buildDir, executable, paramString)
97  #print("Writing to %r, %r" % (simDir+str(coresCounter)+".sh", coresCounter))
98  open(simDir+str(coresCounter)+".sh", "a").write(cmd)
99  numCmdCounter = (numCmdCounter+1)%numCmd
100  elif onNode:
101  # initialise the freeCores list
102  freeCores = getFreeCores(nodes)
103  for executable in exeNames:
104  cmd = 'cd %s; %s/%s %s' % (simDir, buildDir, executable, paramString)
105  # look for new free cores when needed
106  while len(freeCores) == 0:
107  print("No free cores, waiting for nodes to free up")
108  # time to wait in seconds between checking for free nodes
109  time.sleep(300)
110  freeCores = getFreeCores(nodes)
111  node = freeCores.pop()
112  outname = paramString.replace(" ", "")
113  sshcmd = "sleep 1\n ssh %s \"%s\"" % (node, cmd + ' &> ' + executable + '_' + outname + '.out &')
114  print(sshcmd)
115  subprocess.check_output(sshcmd, shell=True)
116  else:
117  for executable in exeNames:
118  cmd = 'cd %s && %s/%s %s' % (simDir, buildDir, executable, paramString)
119  if verbose:
120  print("Running in serial: ./%s %s" % (executable, paramString))
121  subprocess.check_output(cmd, shell=True)
122  print("Runs started. Rerun this script for analysis.")
123 
124 # computes number of commands to be executed
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::enable_if_t< PacketLoad, PacketType > read(const TensorMapper &tensorMapper, const StorageIndex &NCIndex, const StorageIndex &CIndex, const StorageIndex &ld)
read, a template function used for loading the data from global memory. This function is used to guar...
Definition: TensorContractionSycl.h:162
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 ceil(const bfloat16 &a)
Definition: BFloat16.h:644
str
Definition: compute_granudrum_aor.py:141
void split(const DoubleVector &in_vector, Vector< DoubleVector * > &out_vector_pt)
Definition: double_vector.cc:1413
def runSimulations(smcTable, simDir, buildDir, paramStringTemplate, exeNames, material, onNode=False, nodes=[], cores=0, verbose=False)
Definition: simulate.py:17

References Eigen::bfloat16_impl.ceil(), getFreeCores(), int(), numberOfCommands(), Eigen::internal.print(), Eigen::TensorSycl::internal.read(), oomph::DoubleVectorHelpers.split(), compute_granudrum_aor.str, and Eigen::TensorSycl::internal.write().