Skip to content

Commit

Permalink
add example for parallel execution with GNU parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
AWehrhahn committed May 12, 2022
1 parent f412827 commit e41be27
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ examples/*.ech
examples/*.inp
examples/*.html
examples/*.sel
examples/log/

# IDL
dump.bin
Expand Down
79 changes: 79 additions & 0 deletions examples/parallel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
"""
Example of a parallel execution script
The script is called with command line parameters that are
then used to modify the runs. Here those are Teff, logg, and [M/H], but they
can be anything, e.g. names of different input spectra.
Here we simply synthesize different spectra for different input paramters,
but it can also be used to analyse different spectra, etc.
"""
import sys
from os import makedirs
from os.path import dirname, join, realpath

from pysme import sme as SME
from pysme import util
from pysme.gui import plot_plotly
from pysme.synthesize import synthesize_spectrum

if __name__ == "__main__":

# Read the command line parameters
# the first parameter is always the command used to call this script
# i.e. the name of this file, thus we ignore it
# The expected number of parameters is then 1 + 3 = 4
if len(sys.argv) == 4:
# Here we parse the input parameters
# since they are supposed to be numbers, we convert them from strings
teff, logg, monh = sys.argv[1], sys.argv[2], sys.argv[3]
teff = float(teff)
logg = float(logg)
monh = float(monh)
else:
print(
"No (or the wrong number of) command line "
"parameters passed to the script"
)
raise RuntimeError

# Define the location of all your files
# here we use different directories for the different
# products
target = f"sun_{teff}_{logg}_{monh}"
examples_dir = dirname(realpath(__file__))
in_file = join(examples_dir, "sun_6440_grid.inp")
out_file = join(examples_dir, f"out/{target}.sme")
plot_file = join(examples_dir, f"plot/{target}.html")
log_file = join(examples_dir, f"log/{target}.log")

# Start the logging to the file
makedirs(dirname(log_file), exist_ok=True)
util.start_logging(log_file)

# Load your existing SME structure or create your own
sme = SME.SME_Structure.load(in_file)

# Change parameters if your want
sme.vrad = 0.35
sme.vrad_flag = "whole"
sme.cscale_flag = "linear"
sme.cscale_type = "match"

# Apply the parameters set via the command line
sme.teff = teff
sme.logg = logg
sme.monh = monh

# Start SME solver
sme = synthesize_spectrum(sme)

# Save results
makedirs(dirname(out_file), exist_ok=True)
sme.save(out_file)

# Plot results
makedirs(dirname(plot_file), exist_ok=True)
fig = plot_plotly.FinalPlot(sme)
fig.save(filename=plot_file)
22 changes: 22 additions & 0 deletions examples/parallel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
# Run GNU parallel using the parallel.py script in this folder
# by passing 3 parameters to it (teff, logg, monh)
# For each parameter there are three values, resulting in
# a total of 27 different runs

# Format for the call is:
# parallel cmd ::: p1 ::: p2
# where cmd is the command to be executed (here calling the python script with
# three command line parameters), and p1 and p2 are the parameters to pass to the script
# With keywords:
# --bar : show a progress bar
# --joblog parallel.log : save runtime data about the runs in parallel.log
# --header : : the first value of each parameter list is its header, which is not used otherwise
# --results ./log : save the stdout and stderr of each run to logfiles in the log folder.
# These logfiles are seperate from the regular logfiles created by PySME,
#
# The "normal" stdout is passed to dev/null, since we don't need it and only want the progress bar
#
# For the full documentation on GNU parallel see https://www.gnu.org/software/parallel/parallel.html

parallel --bar --joblog parallel.log --header : --results log/ "python parallel.py {teff} {logg} {monh}" ::: teff 5000 5500 6000 ::: logg 4 4.2 4.4 ::: monh -0.5 0 0.5 > /dev/null

0 comments on commit e41be27

Please sign in to comment.