warp.utils.segmented_sort_pairs#
- warp.utils.segmented_sort_pairs(
- keys,
- values,
- count,
- segment_start_indices,
- segment_end_indices=None,
Sort key-value pairs in place within each segment in ascending key order.
The sort is stable within each segment. Pairs with identical keys retain their original relative order. Elements in
keys[:count]andvalues[:count]outside the segment ranges are not modified.Segments are half-open ranges defined by
segment_start_indicesand optionallysegment_end_indices. Every range must satisfy0 <= start <= end <= count.During direct CPU execution, Warp raises a
ValueErrorfor invalid segment bounds. If invalid bounds are encountered during CPU graph replay,warp.capture_launch()raises aRuntimeError. For execution on a CUDA device, Warp does not currently report invalid segment bounds, so callers must validate segment ranges before calling this function.All provided arrays must be contiguous and reside on the same device. The segment-index arrays must be one-dimensional.
The
keysandvaluesarrays must each contain at least2 * countelements. Thekeys[count:2 * count]andvalues[count:2 * count]regions are scratch storage and may be overwritten.Segment ranges must not overlap one another, and neither
segment_start_indicesnorsegment_end_indicesmay overlap the storage used bykeysorvalues. Warp does not currently detect either type of overlap. Violating either restriction results in undefined behavior.- Parameters:
keys (warp.array[warp.int32] | warp.array[warp.float32]) – Array of keys to sort. Its dtype must be
int32orfloat32.values (warp.array[warp.int32]) – Array of values to reorder with their corresponding keys. Its dtype must be
int32.count (int) – Number of elements available to the segment ranges at the start of
keysandvalues. Must satisfy0 <= count <= 2**31 - 1.segment_start_indices (warp.array[warp.int32]) – Start index of each segment. When
segment_end_indicesisNone, adjacent entries define each segment, so an array of length N defines N - 1 segments.segment_end_indices (warp.array[warp.int32] | None) – End index of each segment. When provided, it must have the same length as
segment_start_indices.
- Raises:
TypeError – If
countis not an integer.ValueError – If
countis outside the signed 32-bit non-negative integer range or a segment range is invalid during direct CPU execution.RuntimeError – If the arrays reside on different devices,
keysorvalueshas insufficient storage, an array is not contiguous, a segment-index array is not one-dimensional, the segment-index arrays have different lengths, or a dtype is unsupported.
- Return type:
None
Example
>>> keys = wp.array([3, 1, 4, 2, 0, 0, 0, 0], dtype=wp.int32) >>> values = wp.array([30, 10, 40, 20, 0, 0, 0, 0], dtype=wp.int32) >>> offsets = wp.array([0, 2, 4], dtype=wp.int32) >>> wp.utils.segmented_sort_pairs(keys, values, 4, offsets) >>> keys.numpy()[:4].tolist() [1, 3, 2, 4] >>> values.numpy()[:4].tolist() [10, 30, 20, 40]