Skip to content

Closes #4075: MergeShuffle - #4094

Merged
ajpotts merged 3 commits into
Bears-R-Us:mainfrom
ajpotts:4075_MergeShuffle
Aug 5, 2025
Merged

ajpotts merged 3 commits into
Bears-R-Us:mainfrom
ajpotts:4075_MergeShuffle

Conversation

@ajpotts

@ajpotts ajpotts commented Feb 12, 2025 •

Copy link
Copy Markdown
Contributor

Summary

This PR extends ak.random.Generator.shuffle with a method parameter, enabling callers to choose between the existing in-place Fisher–Yates shuffle and a new fully distributed merge shuffle implementation.

Motivation

Generator.shuffle previously relied on a Chapel-side Fisher–Yates shuffle that is serial/global and not suitable for large distributed arrays (it effectively assumes the whole array can be shuffled on a single locale). This change adds a scalable alternative that performs a distributed shuffle across locales while remaining deterministic given the RNG state.

What changed

Python client (arkouda/numpy/random/generator.py)

  • Updated signature: shuffle(self, x, method="FisherYates").
  • Expanded docstring to describe supported methods and when to use each:
    • FisherYates — serial/global Chapel shuffle (best for small arrays/tests).
    • MergeShuffle — distributed shuffle for production-scale data.
  • Sends method to the server (method.lower()).
  • Advances RNG state by one step per shuffle call (instead of x.size), because the distributed shuffle derives per-locale/per-task streams from a single generator draw used as the shuffle seed.

Server (src/RandMsg.chpl)

  • Added routing logic in shuffle(...) to dispatch based on method:
    • fisheryates → existing shuffleHelp
    • mergeshuffle → new mergeShuffleHelp
    • otherwise returns a clear error message.
  • Implemented a new distributed merge shuffle algorithm:
    • Performs locale-local hierarchical shuffling (shuffleLocales), then
    • probabilistically merges adjacent locale blocks over multiple rounds (mergeShuffle / merge).
  • Enforced 1D-only support for MergeShuffle (returns an error for array_nd > 1).
  • Added @chplcheck.ignore("UnusedFormal") annotations where appropriate and minor Chapel style cleanups.

Tests (tests/numpy/random_test.py)

  • Expanded test_shuffle coverage:
    • parametrized by method ∈ {"FisherYates", "MergeShuffle"}
    • parametrized by probabilistic test sizes (pytest.prob_size)
    • validates:
      • the array contents are permuted (not identical to the pre-shuffle copy)
      • multiset equality via sorting (values preserved)
      • determinism with the same seed + method

Behavior & compatibility notes

  • Generator.shuffle is now explicitly method-selectable; default remains FisherYates.
  • For MergeShuffle, reproducibility is guaranteed only when the number of locales is unchanged between runs. Changing locale count can change the permutation.
  • MergeShuffle currently supports 1D arrays only.

How to use

>>> import arkouda as ak
>>> rng = ak.random.default_rng(18)
>>> x = ak.arange(10)

# Small arrays / tests
>>> rng.shuffle(x, method="FisherYates")

# Distributed shuffle (recommended for large arrays)
>>> rng.shuffle(x, method="MergeShuffle")

Closes #4075: MergeShuffle

@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch 2 times, most recently from c921328 to 09af72a Compare February 12, 2025 18:11
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch from 5eabdaf to b51c466 Compare March 10, 2025 14:52
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch from b51c466 to 75e14a3 Compare May 9, 2025 17:30
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch 2 times, most recently from c507bf3 to 41d52ae Compare May 22, 2025 18:58
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch 3 times, most recently from 73c7620 to 40ba6bb Compare June 20, 2025 13:24
@ajpotts
ajpotts marked this pull request as ready for review June 23, 2025 15:13
@ajpotts
ajpotts marked this pull request as draft June 23, 2025 15:13
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch from 54aab43 to 41a7167 Compare June 23, 2025 20:20
@ajpotts
ajpotts marked this pull request as ready for review June 23, 2025 20:21
1RyanK
1RyanK previously approved these changes Jul 25, 2025

@1RyanK 1RyanK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few things

Comment thread src/RandMsg.chpl Resolved
Comment thread src/RandMsg.chpl Resolved
Comment thread src/RandMsg.chpl Resolved
Comment thread src/RandMsg.chpl Resolved
Comment thread src/RandMsg.chpl Resolved
@ajpotts
ajpotts changed the base branch from master to main July 25, 2025 14:12
@ajpotts
ajpotts dismissed 1RyanK’s stale review July 25, 2025 14:12

The base branch was changed.

@drculhane drculhane left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm satisfied that this works as advertised. Thanks for doing this.

Comment thread src/RandMsg.chpl Resolved
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch from cbc00af to 196d2c1 Compare August 4, 2025 21:53
@ajpotts
ajpotts force-pushed the 4075_MergeShuffle branch from 196d2c1 to b403e2a Compare August 4, 2025 22:05
@ajpotts
ajpotts added this pull request to the merge queue Aug 5, 2025
Merged via the queue into Bears-R-Us:main with commit af3f48e Aug 5, 2025
drculhane pushed a commit to drculhane/arkouda that referenced this pull request Aug 20, 2025
Closes Bears-R-Us#4075:  MergeShuffle

---------

Co-authored-by: Amanda Potts <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MergeShuffle

3 participants