Skip to content

Commit a5b19ba

Browse files
committed
fixup prefill logic that could miss adding pkgs
1 parent dc55b6f commit a5b19ba

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/rechunk/alg.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def prefill_layers(
7777
curr = []
7878
l_upd = np.zeros(n_segments, dtype=np.bool)
7979
l_size = 0
80-
while todo:
80+
while True:
8181
# We will fill layers in two steps:
8282
# If the layer is emptly, we will insert the largest package
8383
# in todo.
@@ -87,23 +87,26 @@ def prefill_layers(
8787
# There will be packages left over in the end, which will be handled
8888
# differently.
8989

90-
if not curr:
91-
p = max(todo, key=lambda p: p.size)
92-
todo.pop(p)
93-
curr.append(p)
94-
l_upd |= upd_matrix[p.index]
95-
l_size += p.size
96-
elif l_size > fill_size:
97-
layers.append(curr)
98-
logger.info(
99-
f"Layer {dedi+len(layers):2d}: {l_size / 1e9:.3f} GB with {len(curr):3d} packages."
100-
)
101-
if len(layers) >= max_layers:
90+
if l_size > fill_size or not todo:
91+
if curr:
92+
# Since this also gets hit with not todo
93+
# curr might be empty, avoid creating a layer
94+
layers.append(curr)
95+
logger.info(
96+
f"Layer {dedi+len(layers):2d}: {l_size / 1e9:.3f} GB with {len(curr):3d} packages."
97+
)
98+
if len(layers) >= max_layers or not todo:
10299
break
103100
curr = []
104101
l_upd = np.zeros(n_segments, dtype=np.bool)
105102
l_size = 0
106103
pbar.update(1)
104+
elif not curr:
105+
p = max(todo, key=lambda p: p.size)
106+
todo.pop(p)
107+
curr.append(p)
108+
l_upd |= upd_matrix[p.index]
109+
l_size += p.size
107110
else:
108111
# Calculate the bandwidth increase for each package
109112
# and select the one with the smallest increase

0 commit comments

Comments
 (0)