Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition in factorize #196

Merged
merged 4 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion flox/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,12 @@ def factorize_(
for groupvar, expect in zip(by, expected_groups):
flat = groupvar.reshape(-1)
if isinstance(expect, pd.RangeIndex):
idx = flat
# idx is a view of the original `by` aray
# copy here so we don't have a race condition with the
# group_idx[nanmask] = nan_sentinel assignment later
# this is important in shared-memory parallelism with dask
# TODO: figure out how to avoid this
idx = flat.copy()
found_groups.append(np.array(expect))
# TODO: fix by using masked integers
idx[idx > expect[-1]] = -1
Expand Down
27 changes: 27 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,3 +1282,30 @@ def test_1d_blockwise_sort_optimization():
array, time.dt.dayofyear.values[::-1], sort=False, method="blockwise", func="count"
)
assert all("getitem" not in k for k in actual.dask.layers)


@requires_dask
def test_negative_index_factorize_race_condition():
# shape = (10, 2000)
# chunks = ((shape[0]-1,1), 10)
shape = (101, 174000)
chunks = ((101,), 8760)
eps = dask.array.random.random_sample(shape, chunks=chunks)
N2 = dask.array.random.random_sample(shape, chunks=chunks)
S2 = dask.array.random.random_sample(shape, chunks=chunks)

bins = np.arange(-5, -2.05, 0.1)
func = ["mean", "count", "sum"]

out = [
groupby_reduce(
eps,
N2,
S2,
func=f,
expected_groups=(bins, bins),
isbin=(True, True),
)
for f in func
]
[dask.compute(out, scheduler="threads") for _ in range(5)]