Skip to content

Instantly share code, notes, and snippets.

@codingS3b
Last active April 24, 2018 12:49
Show Gist options
  • Select an option

  • Save codingS3b/4699b0a7b305b1887c112505710caf2d to your computer and use it in GitHub Desktop.

Select an option

Save codingS3b/4699b0a7b305b1887c112505710caf2d to your computer and use it in GitHub Desktop.
Prototype for a slurm submitter as a jupyter widget
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import widgets\n",
"from IPython.display import display\n",
"import os\n",
"import inspect"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"class SLURMSubmitter(widgets.VBox):\n",
" \"\"\"\n",
" \"\"\"\n",
"\n",
" def __init__(self, simex_calc, nnodes=1, time='00:10:00', tasks_per_node=24,\n",
" parallelism=\"mpirun\", partition=\"all\"):\n",
" \n",
" widgets.VBox.__init__(self, layout=widgets.Layout(width='100%'))\n",
" \n",
" self.simex_calc = simex_calc\n",
" self.nodes_widget = widgets.IntSlider(min=1, max=10, value=nnodes)\n",
" self.time_widget = widgets.Text(value=time)\n",
" self.task_widget = widgets.IntSlider(min=1, max=100,\n",
" value=tasks_per_node)\n",
" self.submit_widget = widgets.Button(description=\"Submit job\", button_style=\"success\")\n",
" self.submit_widget.on_click(self.submit_job)\n",
" self.parallelism_widget = widgets.Dropdown(\n",
" options=[\"mpirun\", \"CUDA\"], value=parallelism)\n",
" self.partition_widget = widgets.Dropdown(options=[\"all\",\n",
" \"maxwell\",\n",
" \"cfel\",\n",
" \"exfel\"], \n",
" value=partition)\n",
" # ui representation\n",
" self.children = [\n",
" widgets.HBox([widgets.Label(\"Partition\"), self.partition_widget]),\n",
" widgets.HBox([widgets.Label(\"Nodes\"), self.nodes_widget]),\n",
" widgets.HBox([widgets.Label(\"Time\"), self.time_widget]),\n",
" widgets.HBox([widgets.Label(\"Tasks\"), self.task_widget]),\n",
" widgets.HBox([widgets.Label(\"parallelism\"),\n",
" self.parallelism_widget]),\n",
" self.submit_widget\n",
" ]\n",
"\n",
" def submit_job(self, b=None):\n",
" batch_template = '''#!/bin/sh\n",
"#SBATCH --partition={0}\n",
"#SBATCH --time={1}\n",
"#SBATCH --nodes={2}\n",
"#SBATCH --output={3}\n",
"\n",
"export MODULEPATH=$MODULEPATH:$HOME/simex_dev_workshop/modulefiles\n",
"module load python3/3.4\n",
"module load simex\n",
"\n",
"{4} -np {5} python3 {6} {7}\n",
" '''\n",
" # create a dill file from the simex_calculator\n",
" classname = type(self.simex_calc).__name__\n",
" dfile = classname + '.dill'\n",
" self.simex_calc.dumpToFile(dfile)\n",
"\n",
"\n",
" # get the path to the module from which the simex_calc is an instance of\n",
" path_to_module = inspect.getfile(type(self.simex_calc))\n",
" \n",
" \n",
" final_script = batch_template.format(\n",
" self.partition_widget.value, \n",
" self.time_widget.value, \n",
" self.nodes_widget.value,\n",
" os.getcwd() + \"/\" + classname + '_%A.out', \n",
" self.parallelism_widget.value, \n",
" self.task_widget.value, \n",
" path_to_module, \n",
" dfile)\n",
"\n",
" print(\"Would submit the following script:\\n\", final_script)\n",
" #fname = 'batch.sh'\n",
" #with open(fname, 'w') as ffile:\n",
" # ffile.write(final_script)\n",
"\n",
" \n",
" #slurm = Popen(['sbatch', fname], stdout=PIPE)\n",
" # print(slurm.stdout.read().strip().decode('utf-8'))\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "27318ed2c4fa49f992609669dfeefcbb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Would submit the following script:\n",
" #!/bin/sh\n",
"#SBATCH --partition=maxwell\n",
"#SBATCH --time=00:10:00\n",
"#SBATCH --nodes=1\n",
"#SBATCH --output=/home/starke88/dev/py/notebooks/crystfel_%A.out\n",
"\n",
"export MODULEPATH=$MODULEPATH:$HOME/simex_dev_workshop/modulefiles\n",
"module load python3/3.4\n",
"module load simex\n",
"\n",
"mpirun -np 24 python3 $HOME/simex_platform/Sources/python/SimEx/Calculators/CrystFELPhotonDiffractor.py foo.dill\n",
" \n"
]
}
],
"source": [
"display(SLURMSubmitter())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment