Skip to content

Commit

Permalink
version for running by executable or by container
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnTools committed Dec 15, 2024
1 parent 9f34209 commit de72337
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 31 deletions.
2 changes: 2 additions & 0 deletions simulator/args-run
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
-input ./input
-output ./output
-sim ./sim-dir
-container pces/sim
-extern ./
96 changes: 65 additions & 31 deletions simulator/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
import argparse
import subprocess
import time
import shutil
import datetime
import yaml
Expand Down Expand Up @@ -89,6 +90,49 @@ def main():
if errs > 0:
exit(1)


# if we're using a container and it doesn't exist, build it
if containerTag is not None:
cmdList = ["docker", "image", "ls", containerTag]
process = subprocess.Popen(cmdList,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()

if len(stdout) == 0:
print('Build image for tag {}'.format(containerTag))

cmdList = ["docker", "build", "../"]
process = subprocess.Popen(cmdList,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()

if process.returncode != 0:
print('Build image for tag {} failed'.format(containerTag))
exit(1)

print(stdout)
print('Build image for tag {} succeeded'.format(containerTag))

else:

simExec = os.path.join(simDir,"sim")
if not os.path.isfile(simExec):
cwd = os.getcwd()
os.chdir(os.path.join(cwd,'sim-dir'))
compileList = ["go","build","sim.go","exp.go"]
print("building simulation executable")
process = subprocess.Popen(compileList,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()

if process.returncode != 0:
print("Error building ./sim")
if len(stderr) > 0:
print(stderr)
exit(1)

os.chdir(cwd)

# get the dictionary describing the experiments
experiment_input_file = os.path.join(templateDir, 'experiments.yaml')
with open(experiment_input_file, 'r') as rf:
Expand Down Expand Up @@ -171,56 +215,46 @@ def main():

simArgs = os.path.join(argsDir, "args-sim")

time.sleep(1.5)
print('running experiment {}'.format(exprmntName))

# if containerTag is not None, run the container to execute the simulation
if containerTag is not None:
cwd = os.getcwd()
paths = os.path.split(os.getcwd())
outsideDir = os.path.join(paths[:len(paths)-1])
mountCmd = '{}:{}'.format(outsideDir, externDir)
#cTag = "ghcr.io/iti/pcesapps-dev"
outsideDir = paths[0]

for directory in paths[1:len(paths)]:
outsideDir = os.path.join(outsideDir, directory)

mountCmd = '{}:{}'.format(outsideDir, "/tmp/extern")

simExecArgs = ["docker","run","-it", "--rm" "-v", mountCmd, containerTag]
simExecArgs = ["docker","run","-it", "--rm", "-v", mountCmd, containerTag]

process = subprocess.Popen(simExecArgs,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

else:
simExec = os.path.join(simDir,"sim")
if not os.path.isfile(simExec):
cwd = os.getcwd()
os.chdir(os.path.join(cwd,'sim-dir'))
compileList = ["go","build","sim.go","exp.go"]
process = subprocess.Popen(compileList,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()

if process.returncode != 0:
print("Error building ./sim")
if len(stderr) > 0:
print(stderr)
exit(1)

os.chdir(cwd)

process = subprocess.Popen([simExec, "-is", simArgs],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

stdout, stderr = process.communicate()

if process.returncode != 0:
print("Error from simulation run")
else:
if len(stderr) > 0 :
print(stderr)

# append the msr file in output to output/aggMsr
outputMsr = os.path.join(outputDir, 'msr.yaml')

with open(outputMsr, 'r') as fsrc:
nxtMsr = yaml.safe_load(fsrc)
allMsr.append(nxtMsr)

os.remove(outputMsr)
if len(stderr) > 0 :
print(stderr)

# append the msr file in output to output/aggMsr
outputMsr = os.path.join(outputDir, 'msr.yaml')

with open(outputMsr, 'r') as fsrc:
nxtMsr = yaml.safe_load(fsrc)
allMsr.append(nxtMsr)

os.remove(outputMsr)

with open(aggOutFile, 'a') as fdst:
yaml.dump(allMsr, fdst)
Expand Down

0 comments on commit de72337

Please sign in to comment.