Using containers in COMPS¶
You can use the Singularity container files (.sif) for running simulations on COMPS.
Run a job in COMPS with Singularity¶
idmtools includes examples to help you get up and running with Singularity on COMPS. First, you can run the create_ubuntu_sif.py script, located in examples/singularity/ubuntu-20-04/create_ubuntu_sif.py. This script creates an Ubuntu Singularity container based on the included definition file, ubuntu_20_04_base.def, and writes it to an asset collection on COMPS.
if __name__ == '__main__':
platform = Platform("CALCULON")
sbi = SingularityBuildWorkItem(name="Create ubuntu sif with def file", definition_file="ubuntu_20_04_base.def", image_name="ubuntu.sif")
sbi.tags = dict(ubuntu="20.04")
sbi.run(wait_until_done=True, platform=platform)
if sbi.succeeded:
# Write ID file
sbi.asset_collection.to_id_file("ubuntu.id")
Once you have the required Linux .sif container file, you can then add your modeling files. For example, create_covasim_sif.py, located in examples/singularity/covasim/create_covasim_sif.py, uses the pre-created ubuntu container and associated asset collection id to create a new .sif container file for running simulations using Covasim.
if __name__ == '__main__':
platform = Platform("CALCULON")
sbi = SingularityBuildWorkItem(name="Create covasim sif with def file", definition_file="covasim_req.def", image_name="covasim_ubuntu.sif")
# Try to load the ubuntu image from an id file
pwd = PurePath(__file__).parent
ub_base = pwd.joinpath("..", "ubuntu-20-04")
fp = pwd.joinpath("ubuntu.id")
sbi.add_assets(AssetCollection.from_id_file(fp))
sbi.tags = dict(covasim=None)
sbi.run(wait_until_done=True, platform=platform)
if sbi.succeeded:
sbi.asset_collection.to_id_file("covasim.id")
As the following example script, run_covasim_sweep.py, shows you can run simulations in a Singularity container on COMPS using the previously created .sif container file.
import os
import sys
from functools import partial
from idmtools.assets import AssetCollection
from idmtools.builders import SimulationBuilder
from idmtools.core.platform_factory import Platform
from idmtools.entities import CommandLine
from idmtools.entities.command_task import CommandTask
from idmtools.entities.experiment import Experiment
from idmtools.entities.templated_simulation import TemplatedSimulations
def set_value(simulation, name, value):
fix_value = round(value, 2) if isinstance(value, float) else value
# add argument
simulation.task.command.add_raw_argument(str(fix_value))
# add tag with our value
simulation.tags[name] = fix_value
if __name__ == "__main__":
here = os.path.dirname(__file__)
# Create a platform to run the workitem
platform = Platform("CALCULON")
# create commandline input for the task
command = CommandLine(f"singularity exec ./Assets/covasim_ubuntu.sif python3 Assets/run_sim_sweep.py")
task = CommandTask(command=command)
ts = TemplatedSimulations(base_task=task)
# Add our image
task.common_assets.add_assets(AssetCollection.from_id_file("covasim.id"))
sb = SimulationBuilder()
# Add sweeps on 3 parameters. Total of 1680 simulations(6x14x21)
sb.add_sweep_definition(partial(set_value, name="pop_size"), [10000, 20000])
sb.add_sweep_definition(partial(set_value, name="pop_infected"), [10, 100, 1000])
sb.add_sweep_definition(partial(set_value, name="n_days"), [100, 110, 120])
sb.add_sweep_definition(partial(set_value, name="rand_seed"), [1234, 4567])
ts.add_builder(sb)
experiment = Experiment.from_template(ts, name=os.path.split(sys.argv[0])[1])
experiment.add_asset(os.path.join("inputs", "run_sim_sweep.py"))
experiment.add_asset(os.path.join("inputs", "sim_to_inset.py"))
experiment.run(wait_until_done=True)
if experiment.succeeded:
experiment.to_id_file("run_sim_sweep.id")