Skip to content

Add multi-GPU sort - #9982

Open
Jacobfaib wants to merge 24 commits into
NVIDIA:mainfrom
Jacobfaib:jacobf/2026-07-16/mgmn-sort
Open

Add multi-GPU sort#9982
Jacobfaib wants to merge 24 commits into
NVIDIA:mainfrom
Jacobfaib:jacobf/2026-07-16/mgmn-sort

Conversation

@Jacobfaib

@Jacobfaib Jacobfaib commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Description

Needs #10221

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@copy-pr-bot

copy-pr-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Progress in CCCL Jul 16, 2026
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch 2 times, most recently from 9fe9a67 to 84caef1 Compare July 17, 2026 13:20
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

1 similar comment
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@github-actions

This comment has been minimized.

@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@github-actions

This comment has been minimized.

@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch from 3058e3c to 5884e03 Compare July 20, 2026 12:40
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

5 similar comments
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch from c28dfe8 to 0066415 Compare July 20, 2026 18:59
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@github-actions

This comment has been minimized.

@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch from b018e98 to 9fd6abb Compare July 21, 2026 13:15
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

2 similar comments
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@github-actions

This comment has been minimized.

@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch from 0c31886 to 4c65b39 Compare July 22, 2026 18:49
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

1 similar comment
@Jacobfaib

Copy link
Copy Markdown
Contributor Author

/ok to test

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 4

🧹 Nitpick comments (4)
libcudacxx/include/cuda/__runtime/types.h (1)

39-41: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: Repeat the exact preprocessor condition in both branch comments.

The #else and #endif comments do not repeat _CCCL_CTK_AT_LEAST(12, 2), making the branch harder to audit. Use the exact condition text in both comments.

Based on learnings, annotated preprocessor branches must repeat the corresponding condition verbatim.

Source: Learnings

cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/local_setup.h (1)

55-56: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

suggestion: @tparam _Traits no longer exists; the template parameters are _Tp, _Env, _BinaryOp (plus the deduced range parameters). Stale leftover from the traits-based design.

Source: Coding guidelines

cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/merge_k_way.h (1)

66-100: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

suggestion: the loop bound is __displs.size() but the body indexes __counts[__i], and __total assumes __displs is the exclusive scan of __counts. Assert the contract so a caller mismatch fails loudly instead of reading out of bounds.

Proposed assert
+  _CCCL_ASSERT(__counts.size() == __displs.size(), "counts and displs must have equal length");
   const auto __total = __counts.back() + __displs.back();
cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/rebalance.h (1)

284-288: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

suggestion: the transfers being awaited here are D2H (counts/displacements into host vectors), not HtoD — the comment reads backwards.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: ded6089e-0fc1-4912-9a0c-c1d481dd2b66

📥 Commits

Reviewing files that changed from the base of the PR and between e38d803 and 6325967.

📒 Files selected for processing (18)
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/common.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/bucket_count_fn.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/buffer.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/data_exchange.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/execute.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/histogramming.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/ideal_rank_fn.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/local_setup.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/merge_k_way.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/rebalance.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/sorter.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/sort.h
  • cudax/test/multi_gpu/algorithms/CMakeLists.txt
  • cudax/test/multi_gpu/algorithms/sort/CMakeLists.txt
  • cudax/test/multi_gpu/algorithms/sort/range_basic.cu
  • libcudacxx/include/cuda/__algorithm/copy.h
  • libcudacxx/include/cuda/__runtime/types.h
  • libcudacxx/include/cuda/std/__random/linear_congruential_engine.h

Comment thread cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/data_exchange.h Outdated
Comment thread cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/data_exchange.h Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 487ff05f-fd57-4ba5-beaf-c8377929028b

📥 Commits

Reviewing files that changed from the base of the PR and between 6325967 and e64cb0a.

📒 Files selected for processing (10)
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/bucket_count_fn.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/data_exchange.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/execute.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/histogramming.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/ideal_rank_fn.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/local_setup.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/merge_k_way.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/rebalance.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/sorter.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/sort.h
🚧 Files skipped from review as they are similar to previous changes (8)
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/bucket_count_fn.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/ideal_rank_fn.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/execute.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/sort.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/rebalance.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/local_setup.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/data_exchange.h
  • cudax/include/cuda/experimental/__multi_gpu/algorithm/sort/hss/histogramming.h

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note this whole beast will be replaced by #10221

{
__range.resize(__size);
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This probably stays after adding the new resizable buffer, but I don't know how to handle user-provided buffers that cannot resize using one of these methods. We might consider just using a temporary buffer instead? But that probably balloons the already large temporary memory requirement for this algorithm.

Comment on lines +95 to +101
__CUDAX_MULTI_GPU_DISPATCH(
__comm.logical_device(),
CUB_NS_QUALIFIER::DeviceMergeSort::SortKeys,
::cuda::std::to_address(::cuda::std::ranges::begin(__input)),
::cuda::std::ranges::size(__input),
__cmp,
__env);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could consider using radix sort here but... it's a bit painful to set that up (need to allocate a double buffer etc). I leave this for a follow-up

Comment on lines +126 to +129
template <class _CommRange, class _EnvRange, class _InputRange>
_CCCL_HOST_API void _HSSSorter<_Tp, _Env, _BinaryOp>::__rebalance_to_original_counts(
const __local_setup_result_type& __setup, _CommRange&& __comms, _EnvRange&& __envs, _InputRange&& __local_inputs)
{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This function is incredibly painful. Basically, we have just done a whole bunch of work sorting the array but as a side effect have ended up with pretty balanced distributions across all ranks. Since sort()s contract is that whatever size comes in must also come out, we now need to undo that and unbalance all the ranks.

This, in my opinion can probably be done much more intelligently than I do here, it's pretty brute force.

Comment on lines +50 to +56
template <template <class> class _Buffer, class _Tp>
struct _PerCommSplitters
{
_Buffer<_Bracket<_Tp>> __Ls;
_Buffer<_Bracket<_Tp>> __Us;
_Buffer<_Tp> __probes;
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The point of pulling these out is so that they depend only on as many template parameters as needed. However, _Buffer depends on the memory resource which depends on the environment so in reality they maybe doesn't save that much.

The point of fully generic _Buffer here also (they are all some flave of cuda::buffer under the hood) is to respect the various properties that come from the user's memory resource, in case later algorithms take advantage of these.

But maybe this is overkill, and we can just collapse these down to

cuda::buffer<_Bracket<_Tp>, cuda::mr::device_accessible> __Ls

Because keeping them fully generic does have compile-time impact.

@github-actions

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 6h 30m: Pass: 98%/165 | Total: 2d 12h | Max: 6h 00m | Hits: 99%/382531

See results here.

@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch from e64cb0a to b14f491 Compare July 27, 2026 13:41
@Jacobfaib
Jacobfaib force-pushed the jacobf/2026-07-16/mgmn-sort branch from 6c26cb6 to 87185a9 Compare July 28, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

1 participant