Commit 92579c29 authored by André Anjos's avatar André Anjos 💬

Merge branch '84_loop_output' into 'master'

Loop block output

Closes #84

See merge request !83
parents 103ee393 2c015839
Pipeline #32142 passed with stages
in 22 minutes and 8 seconds
......@@ -182,6 +182,8 @@ class Algorithm(BackendAlgorithm):
"""
dataformat_klass = dataformat.DataFormat
def __init__(self, prefix, data, dataformat_cache=None, library_cache=None):
super(Algorithm, self).__init__(prefix, data, dataformat_cache, library_cache)
......@@ -303,116 +305,52 @@ class Algorithm(BackendAlgorithm):
"declaration: %s" % (self.name, ", ".join(all_output_names))
)
def _validate_format(self, type_name, group_name, entry_name, dataformat):
if dataformat.errors:
self.errors.append(
"found error validating data format `%s' "
"for %s `%s' on algorithm `%s': %s"
% (
type_name,
group_name,
entry_name,
self.name,
"\n".join(dataformat.errors),
)
)
def _validate_dataformats(self, group, group_name, dataformat_cache):
for name, entry in group[group_name].items():
type_name = entry["type"]
thisformat = self._update_dataformat_cache(type_name, dataformat_cache)
self._validate_format(type_name, group_name, name, thisformat)
def _validate_required_dataformats(self, dataformat_cache):
"""Makes sure we can load all requested formats
"""
for group in self.groups:
for name, input in group["inputs"].items():
if input["type"] in self.dataformats:
continue
if dataformat_cache and input["type"] in dataformat_cache: # reuse
thisformat = dataformat_cache[input["type"]]
else: # load it
thisformat = dataformat.DataFormat(self.prefix, input["type"])
if dataformat_cache is not None: # update it
dataformat_cache[input["type"]] = thisformat
self.dataformats[input["type"]] = thisformat
if thisformat.errors:
self.errors.append(
"found error validating data format `%s' "
"for input `%s' on algorithm `%s': %s"
% (input["type"], name, self.name, "\n".join(thisformat.errors))
)
for name, input_ in group["inputs"].items():
self._validate_dataformats(group, "inputs", dataformat_cache)
if "outputs" in group:
for name, output in group["outputs"].items():
if output["type"] in self.dataformats:
continue
if dataformat_cache and output["type"] in dataformat_cache: # reuse
thisformat = dataformat_cache[output["type"]]
else: # load it
thisformat = dataformat.DataFormat(self.prefix, output["type"])
if dataformat_cache is not None: # update it
dataformat_cache[output["type"]] = thisformat
self.dataformats[output["type"]] = thisformat
if thisformat.errors:
self.errors.append(
"found error validating data format `%s' "
"for output `%s' on algorithm `%s': %s"
% (
output["type"],
name,
self.name,
"\n".join(thisformat.errors),
)
)
self._validate_dataformats(group, "outputs", dataformat_cache)
if "loop" in group:
for name, entry in group["loop"].items():
entry_format = entry["type"]
if entry_format in self.dataformats:
continue
if dataformat_cache and entry_format in dataformat_cache:
thisformat = dataformat_cache[entry_format]
else:
thisformat = dataformat.DataFormat(self.prefix, entry_format)
if dataformat_cache is not None:
dataformat_cache[entry_format] = thisformat
self.dataformats[entry_format] = thisformat
if thisformat.errors:
self.errors.append(
"found error validating data format `%s' "
"for loop `%s' on algorithm `%s': %s"
% (
entry_format,
name,
self.name,
"\n".join(thisformat.errors),
)
)
self._validate_dataformats(group, "loop", dataformat_cache)
if self.results:
for name, result in self.results.items():
if result["type"].find("/") != -1:
if result["type"] in self.dataformats:
continue
if dataformat_cache and result["type"] in dataformat_cache: # reuse
thisformat = dataformat_cache[result["type"]]
else:
thisformat = dataformat.DataFormat(self.prefix, result["type"])
if dataformat_cache is not None: # update it
dataformat_cache[result["type"]] = thisformat
self.dataformats[result["type"]] = thisformat
if thisformat.errors:
self.errors.append(
"found error validating data format `%s' "
"for result `%s' on algorithm `%s': %s"
% (
result["type"],
name,
self.name,
"\n".join(thisformat.errors),
)
)
result_type = result["type"]
# results can only contain base types and plots therefore, only
# process plots
if result_type.find("/") != -1:
thisformat = self._update_dataformat_cache(
result_type, dataformat_cache
)
self._validate_format(result_type, "result", name, thisformat)
def _convert_parameter_types(self):
"""Converts types to numpy equivalents, checks defaults, ranges and
......
......@@ -230,6 +230,17 @@ class BaseExecutor(object):
"The input '%s' doesn't exist in the loop algorithm" % name
)
if len(loop["outputs"]) != len(self.loop_algorithm.output_map):
self.errors.append(
"The number of outputs of the loop algorithm doesn't correspond"
)
for name in self.data["outputs"].keys():
if name not in self.algorithm.output_map.keys():
self.errors.append(
"The output '%s' doesn't exist in the loop algorithm" % name
)
# Check that the mapping in coherent
if len(self.data["inputs"]) != len(self.algorithm.input_map):
self.errors.append(
......
......@@ -437,7 +437,7 @@ class DockerExecutor(RemoteExecutor):
if self.loop_algorithm is not None:
cmd.append(
"tcp://%s:%d"
"--loop=tcp://%s:%d"
% (loop_algorithm_container_ip, loop_algorithm_container_port)
)
......
......@@ -191,6 +191,7 @@ class LocalExecutor(BaseExecutor):
def __cleanup(self):
if self.loop_executor:
self.loop_executor.wait()
self.loop_executor.close()
for handler in [self.message_handler, self.loop_message_handler]:
if handler:
......
......@@ -384,7 +384,9 @@ class SubprocessExecutor(RemoteExecutor):
)
if self.loop_algorithm is not None:
cmd.append("tcp://" + self.ip_address + (":%d" % loop_algorithm_port))
cmd.append(
"--loop=tcp://" + self.ip_address + (":%d" % loop_algorithm_port)
)
if logger.getEffectiveLevel() <= logging.DEBUG:
cmd.insert(1, "--debug")
......
......@@ -275,6 +275,10 @@ class Experiment(object):
if self.errors:
return
self._crosscheck_toolchain_loops()
if self.errors:
return
self._crosscheck_toolchain_analyzers()
if self.errors:
return
......@@ -284,6 +288,10 @@ class Experiment(object):
return
self._crosscheck_block_algorithm_pertinence()
if self.errors:
return
self._crosscheck_loop_algorithm_pertinence()
def _check_datasets(self, database_cache, dataformat_cache):
"""checks all datasets are valid"""
......@@ -427,52 +435,69 @@ class Experiment(object):
def _check_loops(self, algorithm_cache, dataformat_cache, library_cache):
"""checks all loops are valid"""
loops = self.data.get("loops", {})
for loopname, loop in loops.items():
for key in ["", "loop_"]:
algoname = loop[key + "algorithm"]
if algoname not in self.algorithms:
# loads the algorithm
if algoname in algorithm_cache:
thisalgo = algorithm_cache[algoname]
else:
thisalgo = algorithm.Algorithm(
self.prefix, algoname, dataformat_cache, library_cache
)
algorithm_cache[algoname] = thisalgo
if "loops" not in self.data:
return
for loopname, loop in self.data["loops"].items():
algoname = loop["algorithm"]
if algoname not in self.algorithms:
self.algorithms[algoname] = thisalgo
if thisalgo.errors:
self.errors.append(
"/loops/%s: algorithm `%s' is invalid:\n%s"
% (loopname, algoname, "\n".join(thisalgo.errors))
)
continue
# loads the algorithm
if algoname in algorithm_cache:
thisalgo = algorithm_cache[algoname]
else:
thisalgo = algorithm.Algorithm(
self.prefix, algoname, dataformat_cache, library_cache
)
algorithm_cache[algoname] = thisalgo
self.algorithms[algoname] = thisalgo
if thisalgo.errors:
self.errors.append(
"/loops/%s: algorithm `%s' is invalid:\n%s"
% (loopname, algoname, "\n".join(thisalgo.errors))
)
continue
else:
thisalgo = self.algorithms[algoname]
if thisalgo.errors:
continue # already done
thisalgo = self.algorithms[algoname]
if thisalgo.errors:
continue # already done
# checks all inputs correspond
for algoin, loop_input in loop[key + "inputs"].items():
if algoin not in thisalgo.input_map:
self.errors.append(
"/loop/%s/inputs/%s: algorithm `%s' does "
"not have an input named `%s' - valid algorithm inputs "
"are %s"
% (
loopname,
loop_input,
algoname,
algoin,
", ".join(thisalgo.input_map.keys()),
)
)
# checks all inputs correspond
for algoin, loop_input in loop["inputs"].items():
if algoin not in thisalgo.input_map:
self.errors.append(
"/analyzers/%s/inputs/%s: algorithm `%s' does "
"not have an input named `%s' - valid algorithm inputs "
"are %s"
% (
loopname,
loop_input,
algoname,
algoin,
", ".join(thisalgo.input_map.keys()),
# checks all outputs correspond
for algout, loop_output in loop[key + "outputs"].items():
if (
hasattr(thisalgo, "output_map")
and algout not in thisalgo.output_map
):
self.errors.append(
"/loops/%s/outputs/%s: algorithm `%s' does not "
"have an output named `%s' - valid algorithm outputs are "
"%s"
% (
loopname,
loop_output,
algoname,
algout,
", ".join(thisalgo.output_map.keys()),
)
)
)
# checks if parallelization makes sense
if loop.get("nb_slots", 1) > 1 and not thisalgo.splittable:
......@@ -685,6 +710,38 @@ class Experiment(object):
)
)
def _crosscheck_toolchain_loops(self):
"""There must exist a 1-to-1 relation to existing loops"""
toolchain_loops = self.toolchain.loops
if sorted(toolchain_loops.keys()) != sorted(self.loops.keys()):
self.errors.append(
"mismatch between the toolchain loop names (%s)"
" and the experiment's (%s)"
% (
", ".join(sorted(toolchain_loops.keys())),
", ".join(sorted(self.loops.keys())),
)
)
# the number of block endpoints and the toolchain's must match
for block_name, block in self.loops.items():
for prefix in ["", "loop_"]:
block_input_count = len(block[prefix + "inputs"])
toolchain_input_block = len(
toolchain_loops[block_name][prefix + "inputs"]
)
if block_input_count != toolchain_input_block:
self.errors.append(
"/loops/{}: toolchain loops has {} {}inputs "
"while the experiment has {} inputs".format(
block_name, toolchain_input_block, prefix, block_input_count
)
)
def _crosscheck_toolchain_analyzers(self):
"""There must exist a 1-to-1 relation to existing analyzers"""
......@@ -741,6 +798,20 @@ class Experiment(object):
algout = imapping[from_endpt[1]] # name of output on algorithm
from_dtype = self.algorithms[block["algorithm"]].output_map[algout]
from_name = "block"
elif from_endpt[0] in self.loops:
loop = self.loops[from_endpt[0]]
for prefix in ["", "loop_"]:
mapping = loop[prefix + "outputs"]
imapping = dict(zip(mapping.values(), mapping.keys()))
if from_endpt[1] in imapping:
algout = imapping[from_endpt[1]] # name of output on algorithm
from_dtype = self.algorithms[
loop[prefix + "algorithm"]
].output_map[algout]
break
from_name = "loop"
else:
self.errors.append("Unknown endpoint %s" % from_endpt[0])
continue
......@@ -757,10 +828,15 @@ class Experiment(object):
elif to_endpt[0] in self.loops:
loop = self.loops[to_endpt[0]]
mapping = loop["inputs"]
imapping = dict(zip(mapping.values(), mapping.keys()))
algoin = imapping[to_endpt[1]] # name of input on algorithm
to_dtype = self.algorithms[loop["algorithm"]].input_map[algoin]
for prefix in ["", "loop_"]:
mapping = loop[prefix + "inputs"]
imapping = dict(zip(mapping.values(), mapping.keys()))
if to_endpt[1] in imapping:
algoin = imapping[to_endpt[1]] # name of input on algorithm
to_dtype = self.algorithms[
loop[prefix + "algorithm"]
].input_map[algoin]
break
to_name = "loop"
elif to_endpt[0] in self.analyzers: # it is an analyzer
......@@ -852,6 +928,86 @@ class Experiment(object):
% (name, self.blocks[name]["algorithm"])
)
def _crosscheck_loop_algorithm_pertinence(self):
"""The number of groups and the input-output connectivity must respect
the individual synchronization channels and the block's.
"""
loops = self.data.get("loops", {})
for name, loop in loops.items():
# filter connections that end on the visited block - remember, each
# input is checked for receiving a single input connection. It is
# illegal to connect an input multiple times. At this point, you
# already know that is not the case.
input_connections = [
k["channel"]
for k in self.toolchain.connections
if k["to"].startswith(name + ".")
]
# filter connections that start on the visited block, retain output
# name so we can check synchronization and then group
output_connections = set(
[
(k["from"].replace(name + ".", ""), k["channel"])
for k in self.toolchain.connections
if k["from"].startswith(name + ".")
]
)
output_connections = [k[1] for k in output_connections]
# note: dataformats have already been checked - only need to check
# for the grouping properties between inputs and outputs
# create channel groups
chain_in = collections.Counter(input_connections)
chain_out = collections.Counter(output_connections)
chain_groups_count = [(v, chain_out.get(k, 0)) for k, v in chain_in.items()]
# now check the algorithms for conformance
algorithm_name = loop["algorithm"]
loop_algorithm_name = loop["loop_algorithm"]
algo_groups_list = self.algorithms[algorithm_name].groups
loop_algo_groups_list = self.algorithms[loop_algorithm_name].groups
groups_count = [
(
len(algo_groups["inputs"]) + len(loop_algo_groups["inputs"]),
len(algo_groups["outputs"]) + len(loop_algo_groups["outputs"]),
)
for algo_groups, loop_algo_groups in zip(
algo_groups_list, loop_algo_groups_list
)
]
if collections.Counter(chain_groups_count) != collections.Counter(
groups_count
):
self.errors.append(
"synchronization mismatch in input/output "
"grouping between loop `{}', algorithm `{}' "
"and loop algorithm `{}'".format(
name, algorithm_name, loop_algorithm_name
)
)
for algo_groups, loop_algo_groups in zip(
algo_groups_list, loop_algo_groups_list
):
algo_loop = algo_groups["loop"]
loop_algo_loop = loop_algo_groups["loop"]
for channel in ["request", "answer"]:
if algo_loop[channel]["type"] != loop_algo_loop[channel]["type"]:
self.errors.append(
"{} loop channel type incompatible between {} and {}".format(
channel, algorithm_name, loop_algorithm_name
)
)
def _crosscheck_analyzer_algorithm_pertinence(self):
"""
The number of groups and the input-output connectivity must respect the
......@@ -908,7 +1064,7 @@ class Experiment(object):
return not bool(self.errors)
def _inputs(self, name):
def _inputs(self, name, get_loop_data=False):
"""Calculates and returns the inputs for a given block"""
# filter connections that end on the visited block
......@@ -942,9 +1098,14 @@ class Experiment(object):
break
if config_data is None:
raise KeyError("did not find `%s' among blocks or analyzers" % name)
raise KeyError("did not find `%s' among blocks, loops or analyzers" % name)
if get_loop_data:
inputs = config_data["loop_inputs"]
else:
inputs = config_data["inputs"]
for algo_endpt, block_endpt in config_data["inputs"].items():
for algo_endpt, block_endpt in inputs.items():
block, output, channel = connections[block_endpt]
if block in self.toolchain.datasets:
......@@ -985,9 +1146,17 @@ class Experiment(object):
return retval
def _block_outputs(self, name):
def _block_outputs(self, name, get_loop_data=False):
"""Calculates and returns the outputs for a given block"""
for item in [self.blocks, self.loops]:
if name in item:
config_data = item[name]
break
if config_data is None:
raise KeyError("did not find `%s' among blocks or loops" % name)
# filter connections that end on the visited block
connections = [
k for k in self.toolchain.connections if k["from"].startswith(name + ".")
......@@ -1007,7 +1176,12 @@ class Experiment(object):
retval = dict()
# notice: there can be multiply connected outputs
for algo_endpt, block_endpt in self.blocks[name]["outputs"].items():
if get_loop_data:
outputs = config_data["loop_outputs"]
else:
outputs = config_data["outputs"]
for algo_endpt, block_endpt in outputs.items():
block, input, channel = connections[block_endpt]
retval[algo_endpt] = dict(
channel=channel, endpoint=block_endpt # the block outtake name
......@@ -1070,26 +1244,24 @@ class Experiment(object):
nb_slots=nb_slots,
)
loop = self.toolchain.get_loop_for_block(name)
if loop is not None:
loop_name = loop["name"]
loop_toolchain_data = self.toolchain.algorithm_item(loop_name)
loop_config_data = self.data["loops"][loop_name]
loop_algorithm = loop_config_data["algorithm"]
parameters = self.data["globals"].get(loop_algorithm, dict())
parameters.update(loop_config_data.get("parameters", dict()))
if name in self.loops:
loop_environment = config_data.get(
"loop_environment", self.data["globals"]["environment"]
)
loop_data = dict(
inputs=self._inputs(loop_name),
channel=loop_toolchain_data["synchronized_channel"],
algorithm=loop_algorithm,
parameters=parameters,
inputs=self._inputs(name, get_loop_data=True),
outputs=self._block_outputs(name, get_loop_data=True),
channel=retval["channel"],
algorithm=config_data["loop_algorithm"],
parameters=config_data["loop_parameters"],
queue=queue,
environment=loop_environment,
)
retval["loop"] = loop_data
retval["outputs"] = self._block_outputs(name)
elif name in self.blocks:
if name in self.blocks:
retval["outputs"] = self._block_outputs(name)
else:
# Analyzers have only 1 output file/cache. This is the result of an
......@@ -1125,47 +1297,63 @@ class Experiment(object):
exec_order[key] = dict(
dependencies=exec_order[key], configuration=self._configuration(key)
)
# import ipdb; ipdb.set_trace()
for key, value in exec_order.items():
# now compute missing hashes - because we're in execution order,
# there should be no missing input hashes in any of the blocks.
config = value["configuration"]
if "outputs" in config: # it is a block
block_outputs = {}
for output, output_value in config["outputs"].items():
output_value["hash"] = hash.hashBlockOutput(
key,
config["algorithm"],
self.algorithms[config["algorithm"]].hash(),
config["parameters"],
config["environment"],
dict([(k, v["hash"]) for k, v in config["inputs"].items()]),
output,
)
output_value["path"] = hash.toPath(output_value["hash"], "")
# set the inputs for the following blocks
block_outputs[
"%s.%s" % (key, output_value["endpoint"])
] = output_value
dependents = [
exec_order[k]["configuration"]
for k in exec_order
if key in exec_order[k]["dependencies"]
]
# updates inputs which have not yet been updated
for dependent in dependents:
for input_name, input_value in dependent["inputs"].items():
if input_value.get("from") in block_outputs.keys():
input_value["hash"] = block_outputs[
input_value.get("from")
]["hash"]
input_value["path"] = block_outputs[
input_value.get("from")
]["path"]
del input_value["from"] # no need for further update
def process_config(config):
block_outputs = {}
for output, output_value in config["outputs"].items():
output_value["hash"] = hash.hashBlockOutput(
key,
config["algorithm"],
self.algorithms[config["algorithm"]].hash(),
config["parameters"],
config["environment"],
dict([(k, v["hash"]) for k, v in config["inputs"].items()]),
output,
)
output_value["path"] = hash.toPath(output_value["hash"], "")
# set the inputs for the following blocks
block_outputs[
"%s.%s" % (key, output_value["endpoint"])
] = output_value
dependents = [
exec_order[k]["configuration"]
for k in exec_order
if key in exec_order[k]["dependencies"]
]
# updates inputs which have not yet been updated
for dependent in dependents:
def process_inputs(inputs):
for input_name, input_value in inputs.items():
if input_value.get("from") in block_outputs.keys():
input_value["hash"] = block_outputs[
input_value.get("from")
]["hash"]
input_value["path"] = block_outputs[
input_value.get("from")
]["path"]
del input_value[
"from"
] # no need for further update
inputs = dependent["inputs"]
process_inputs(inputs)
if "loop" in dependent:
process_inputs(dependent["loop"]["inputs"])
process_config(config)