Example #1
0
def test_get_next_timeout(init):
    @ray.remote
    class MyActor:
        def __init__(self):
            pass

        def f(self, x):
            while True:
                x = x + 1
                time.sleep(1)
            return None

        def double(self, x):
            return 2 * x

    actors = [MyActor.remote() for _ in range(4)]
    pool = ActorPool(actors)
    pool.submit(lambda a, v: a.f.remote(v), 0)
    with pytest.raises(TimeoutError):
        pool.get_next_unordered(timeout=0.1)
Example #2
0
def tqdm_map(actors, actor_tup_function, tups, res=None):
    assert res is not None, "provide way to save partial results"

    initial_len = len(res)
    actor_pool = ActorPool(actors)
    for tup in tups:
        actor_pool.submit(actor_tup_function, tup)

    pbar = tqdm(total=len(tups))
    while True:
        nxt = actor_pool.get_next_unordered()
        ## copy to free up any references at the source
        res.append(copy.deepcopy(nxt))
        pbar.update(1)
        if (len(res) - initial_len) == len(tups):
            print("done with new tups")
            break

    pbar.close()
    return res
Example #3
0
def test_get_next_unordered(init):
    @ray.remote
    class MyActor:
        def __init__(self):
            pass

        def f(self, x):
            return x + 1

        def double(self, x):
            return 2 * x

    actors = [MyActor.remote() for _ in range(4)]
    pool = ActorPool(actors)

    total = []

    for i in range(5):
        pool.submit(lambda a, v: a.f.remote(v), i)
    while pool.has_next():
        total += [pool.get_next_unordered()]

    assert all(elem in [1, 2, 3, 4, 5] for elem in total)
def _main(cnf_dir,
          n_datapoints_per_file,
          dest,
          n_workers,
          dumpfreq=100e3,
          num_subproblems=8,
          random_units=5,
          timeout=300,
          rootdir=None,
          n_datapoints=25000):
    logger = SimpleLogger(
        os.path.join(dest, "logs", "main_loop", f"{str(uuid.uuid4())}.txt"))
    try:
        ray.init(address='auto', redis_password='******')
    except:
        ray.init()
    tms_dict = {
        cnf_path: TaskManager(cnf_path, rootdir)
        for cnf_path in recursively_get_files(
            cnf_dir, forbidden=["bz2", "xz"], exts=["cnf", "gz"])
    }
    logger.write("STARTING DATA GENERATION LOOP WITH", len(tms_dict.keys()),
                 "CNFs")
    time.sleep(5)
    writer = lbdpWriter.remote(n_datapoints_per_file, dest)
    workers = [
        Worker.remote(writer,
                      dumpfreq=dumpfreq,
                      num_subproblems=num_subproblems,
                      random_units=random_units,
                      timeout=timeout) for _ in range(n_workers)
    ]
    pool = ActorPool(workers)
    for tm in tms_dict.values():
        for task in tm:
            if task is not None:
                pool.submit((lambda a, v: a.work.remote(v)), task)
                logger.write(
                    f"SUBMITTED TASK: TYPE {task.task_type}; CNF_PATH {task.cnf_path}"
                )
    try:
        LOOP_COUNT = 0
        while any([(x.waiting > 0) for x in tms_dict.values()]):
            LOOP_COUNT += 1
            if LOOP_COUNT % 100 == 0:
                if ray.get(writer.get_write_count.remote()) > n_datapoints:
                    print(
                        f"NUMBER OF WRITES EXCEEDS N_DATAPOINTS={n_datapoints}, STOPPING"
                    )
                    break
            cnf_path, tasks, original_task = pool.get_next_unordered()
            logger.write(f"GOT TASK RESULT (TYPE {original_task.task_type})")
            if original_task.task_type == 0 and len(tasks) == 0:
                logger.write("WARNING: Task", original_task.cnf_path,
                             "returned no subtasks.")
            tm = tms_dict[cnf_path]
            tm.set_tasks(tasks)
            if (tm.waiting == 0) and len(tm.tasks) == 0:
                print(f"ROOT TASK {tm.cnf_path} FINISHED")
                tms_dict.pop(cnf_path)
                print("POPPED FROM TMS_DICT")
                try:
                    shutil.rmtree(tms.tmpdir.name)
                    os.makedirs(tms.tmpdir.name)
                    time.sleep(1)
                except:
                    pass
            else:
                for task in tm:
                    if task.task_type == 0:
                        logger.write(f"SUBMITTING SUBPROBLEM (TIMEOUT) TASK")
                    elif task.task_type == 1:
                        logger.write(f"SUBMITTING DERIVED FORMULA")
                    elif task.task_type == 2:
                        logger.write(f"SUBMITTING DERIVED SUBFORMULA")
                    pool.submit((lambda a, v: a.work.remote(v)), task)
                logger.write(f"SUBMITTED {len(tasks)} NEW TASKS")
    finally:
        del writer