Skip to content

Commit 65f1ef3

Browse files
jan-janssenpre-commit-ci[bot]Copilotpyiron-runner
authored
[Feature] Accelerate the comparison for batched (#991)
* [Feature] Accelerate the comparison for batched * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix test parameter name: skip_lst -> skip_set with correct data structure * Format black * Change versioning method and update version info Updated versioning system from vcs-versioning to setuptools-scm and modified version details. * Update dependency.py * Update test_batched.py * revert to nested list * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixes * fix type hint * nested list with future --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pyiron-runner <pyiron@mpie.de>
1 parent fd8b9a9 commit 65f1ef3

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/executorlib/standalone/batched.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
from concurrent.futures import Future
22

33

4-
def batched_futures(lst: list[Future], skip_lst: list[list], n: int) -> list[list]:
4+
def batched_futures(
5+
lst: list[Future], nested_skip_lst: list[Future[list]], n: int
6+
) -> list[list]:
57
"""
68
Batch n completed future objects. If the number of completed futures is smaller than n and the end of the batch is
7-
not reached yet, then an empty list is returned. If n future objects are done, which are not included in the skip_lst
9+
not reached yet, then an empty list is returned. If n future objects are done, which are not included in the skip_set
810
then they are returned as batch.
911
1012
Args:
1113
lst (list): list of all future objects
12-
skip_lst (list): list of previous batches of future objects
14+
nested_skip_lst (list): nest list of individual results already assigned to previous batches
1315
n (int): batch size
1416
1517
Returns:
1618
list: results of the batched futures
1719
"""
18-
skipped_ids = {id(item) for items in skip_lst for item in items}
20+
skip_set = {id(item) for f in nested_skip_lst for item in f.result()}
1921

2022
done_lst = []
21-
n_expected = min(n, len(lst) - len(skipped_ids))
23+
n_expected = min(n, len(lst) - len(skip_set))
2224
for v in lst:
23-
if v.done() and id(v.result()) not in skipped_ids:
25+
if v.done() and id(v.result()) not in skip_set:
2426
done_lst.append(v.result())
2527
if len(done_lst) == n_expected:
2628
return done_lst

src/executorlib/task_scheduler/interactive/dependency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _update_waiting_task(
346346
done_lst = batched_futures(
347347
lst=task_wait_dict["kwargs"]["lst"],
348348
n=task_wait_dict["kwargs"]["n"],
349-
skip_lst=[f.result() for f in task_wait_dict["kwargs"]["skip_lst"]],
349+
nested_skip_lst=task_wait_dict["kwargs"]["skip_lst"],
350350
)
351351
if len(done_lst) == 0:
352352
wait_tmp_lst.append(task_wait_dict)

tests/unit/standalone/test_batched.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ def test_batched_futures(self):
1010
f = Future()
1111
f.set_result(i)
1212
lst.append(f)
13-
self.assertEqual(batched_futures(lst=lst, n=3, skip_lst=[]), [0, 1, 2])
14-
self.assertEqual(batched_futures(lst=lst, skip_lst=[[0, 1, 2]], n=3), [3, 4, 5])
15-
self.assertEqual(batched_futures(lst=lst, skip_lst=[[0, 1, 2], [3, 4, 5]], n=3), [6, 7, 8])
16-
self.assertEqual(batched_futures(lst=lst, skip_lst=[[0, 1, 2], [3, 4, 5], [6, 7, 8]], n=3), [9])
13+
batched_lst = [Future(), Future(), Future()]
14+
batched_lst[0].set_result([0, 1, 2])
15+
batched_lst[1].set_result([3, 4, 5])
16+
batched_lst[2].set_result([6, 7, 8])
17+
self.assertEqual(batched_futures(lst=lst, n=3, nested_skip_lst=set()), [0, 1, 2])
18+
self.assertEqual(batched_futures(lst=lst, nested_skip_lst=batched_lst[:1], n=3), [3, 4, 5])
19+
self.assertEqual(batched_futures(lst=lst, nested_skip_lst=batched_lst[:2], n=3), [6, 7, 8])
20+
self.assertEqual(batched_futures(lst=lst, nested_skip_lst=batched_lst, n=3), [9])
1721

1822
def test_batched_futures_not_finished(self):
1923
lst = []
2024
for _ in list(range(10)):
2125
f = Future()
2226
lst.append(f)
23-
self.assertEqual(batched_futures(lst=lst, n=3, skip_lst=[]), [])
27+
self.assertEqual(batched_futures(lst=lst, n=3, nested_skip_lst=set()), [])

0 commit comments

Comments
 (0)