Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9672a22
Add algorithms.segmented_reduce Python API
oleksandr-pavlyk Feb 21, 2025
dfe317a
Change to input_array fixture
oleksandr-pavlyk Feb 21, 2025
ae9ee6f
Corrected docstring of segmented_reduce function
oleksandr-pavlyk Feb 21, 2025
ad3b103
Add initial tests for segmented_reduce
oleksandr-pavlyk Feb 21, 2025
6937a17
Improve readability of test_segmented_reduce_api example
oleksandr-pavlyk Feb 24, 2025
2753cf5
TransformIteratorKind need not override __eq__/__hash__ methods of th…
oleksandr-pavlyk Feb 24, 2025
5c0ce63
Add AdvancedIterator(it, offset=1) function
oleksandr-pavlyk Feb 24, 2025
bb10d46
Add example for summing rows of a matrix using segmented_reduce
oleksandr-pavlyk Feb 24, 2025
799267a
Implement IteratorBase.__add__(self, offset : int) using make_advance…
oleksandr-pavlyk Feb 24, 2025
57fed46
Use end_offsets = start_offsets + 1
oleksandr-pavlyk Feb 24, 2025
b96e9e2
Add a test for segmented_reduce on gpu_struct
oleksandr-pavlyk Feb 24, 2025
c651a67
Merge branch 'main' into add-segmented-reduce-python-api
oleksandr-pavlyk Feb 24, 2025
ed864d7
Change hash of transform iterator to mix its kind
oleksandr-pavlyk Feb 25, 2025
2a83978
Rename variable n to sample_size
oleksandr-pavlyk Feb 26, 2025
15a3012
Remove __hash__ and __eq__ special methods from some iterator classes
oleksandr-pavlyk Feb 26, 2025
08cbd94
Tweak test_scan_array_input to avoid integer overflows during host ac…
oleksandr-pavlyk Feb 26, 2025
d6d39fa
Add cccl.set_cccl_iterator_state utility function and use in segmente…
oleksandr-pavlyk Feb 26, 2025
13d8d19
Introduce _bindings.call_build utility
oleksandr-pavlyk Feb 26, 2025
9f65dee
Merge branch 'main' into add-segmented-reduce-python-api
oleksandr-pavlyk Feb 26, 2025
ecfca41
Make call_build take *args, **kwargs
oleksandr-pavlyk Feb 26, 2025
af23cca
Merge branch 'main' into add-segmented-reduce-python-api
oleksandr-pavlyk Feb 28, 2025
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
Prev Previous commit
Next Next commit
Add example for summing rows of a matrix using segmented_reduce
  • Loading branch information
oleksandr-pavlyk committed Feb 24, 2025
commit bb10d46b9d78a450909354869efa6af31fa3e56f
51 changes: 51 additions & 0 deletions python/cuda_parallel/tests/test_segmented_reduce_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,54 @@ def min_op(a, b):
expected_output = cp.asarray([0, -4, 1], dtype=d_output.dtype)
assert (d_output == expected_output).all()
# example-end segmented-reduce-min


def test_device_segmented_reduce_for_rowwise_sum():
# example-begin segmented-reduce-rowwise-sum
import cupy as cp
import numpy as np

import cuda.parallel.experimental.algorithms as algorithms
import cuda.parallel.experimental.iterators as iterators

def add_op(a, b):
return a + b

n_rows, n_cols = 67, 12345
rng = np.random.default_rng()
mat = rng.integers(low=-31, high=32, dtype=np.int32, size=(n_rows, n_cols))

def make_scaler(step):
def scale(row_id):
return row_id * step

return scale

zero = np.int32(0)
row_offset = make_scaler(np.int32(n_cols))
start_offsets = iterators.TransformIterator(
iterators.CountingIterator(zero), row_offset
)

end_offsets = iterators.AdvancedIterator(start_offsets, offset=1)

d_input = cp.asarray(mat)
h_init = np.zeros(tuple(), dtype=np.int32)
d_output = cp.empty(n_rows, dtype=d_input.dtype)

alg = algorithms.segmented_reduce(
d_input, d_output, start_offsets, end_offsets, add_op, h_init
)

# query size of temporary storage and allocate
temp_nbytes = alg(
None, d_input, d_output, n_rows, start_offsets, end_offsets, h_init
)
temp_storage = cp.empty(temp_nbytes, dtype=cp.uint8)
# launch computation
alg(temp_storage, d_input, d_output, n_rows, start_offsets, end_offsets, h_init)

# Verify correctness
expected = cp.asarray(np.sum(mat, axis=-1))
assert cp.all(d_output == expected)
# example-end segmented-reduce-columnwise-total