import math


def start_workers(cluster, config, config_file, parallel):
    """Initiate worker nodes on cluster, returning jobs IDs for management.
    """
    # we can manually specify workers or dynamically get as many as needed
    num_workers = config["distributed"].get("num_workers", None)
    if num_workers in [None, "all"]:
        cores_per_host = config["distributed"].get("cores_per_host", 1)
        if cores_per_host == 0:
            raise ValueError("Set num_workers or cores_per_host in YAML config")
        assert parallel["cores"] is not None, \
               "Supply workers needed if not configured in YAML"
        num_workers = int(math.ceil(float(parallel["cores"]) / cores_per_host))
    program_cl = [config["analysis"]["worker_program"], config_file]
    if parallel.get("task_module", None):
        program_cl.append("--tasks={0}".format(parallel["task_module"]))
    if parallel.get("queues", None):
        program_cl.append("--queues={0}".format(parallel["queues"]))
    args = config["distributed"]["platform_args"].split()
    return [cluster.submit_job(args, program_cl) for _ in range(num_workers)]