You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[FEA]: cccl.c and cuda.parallel should support indirect_iterator_t which can be advance on both host and device to support streaming algorithms #4148
Is your feature request related to a problem? Please describe.
To attain optimal performance kernels for some algorithms must use 32-bit types to store problem size arguments.
Supporting these algorithms for problem sizes in excess of INT_MAX can be done with streaming approach with streaming logic encoded in algorithm's dispatcher. Dispatcher needs to increment iterators on the host.
This is presently not supported by cccl.c.parallel, since indirect_arg_t does not implement increment operator.
Since indirect_arg_t is used to represent cccl_value_t, cccl_operation_t and cccl_iterator_t, and incrementing only makes sense for iterators, a dedicated type indirect_iterator_t must be introduced, which may implement the operator+=.
If the entirety of iterator state is user-defined, cuda.parallel must provide host function pointer to increment iterator's state by compiling advance function for the host.
If we define the state of a struct that contains size_t linear_id in addition to user-defined state, we could get rid of user-defined advance function altogether, but would need to provide access to linear_id to the dereference function.
It should be noted that for cccl_iterator_kind_t::CCCL_POINTER we can advance the state on the host if the host type representing the type-erased device pointer stored the value size in bytes.
Somehow these two structs have to be combined into a single type to service cccl_iterator_t regardless of the iterator kind it represents.
Approach 2: user-provided host function
We could augment cccl_iterator_t with function pointer to advance_host_fn(void *, uint64_t offset). This pointer will only be used for cccl_iterator_kind_t::ITERATOR.
Numba allows us to compile advance function to get a native function pointer:
We could make linear_id a mandatory member of indirect_iterator_t struct, and generate both device and host functions for mutating linear_id in cccl.c.parallel.
Then the dereference and assign functions will need to become like getitem and setitem functions in python and take linear_id as an argument. So instead of dereference(state) we would be calling dereference(state, linear_id) and instead of assign(state, val) we would be calling assign(state, linear_id, val).
I should comment that since approaches 1 and 3 effectively augment state of iterators with host accessible counter which needs to be copied to the device, these approaches lead to increase in register usage by kernels. There might be kernels where such an increase causes a performance cliff due to register spilling.
For this reason, approach 2 was implemented and merged in gh-4697. It does require host compiler (provided by Numba) and increases the cost of creating the algorithm, but this cost would only incur for those algorithms that employ streaming approach and so far there are few such algorithms only.
The only issue is that we presently always compile host-callable advance function. If some users know in advance that num_segments will never exceed INT_MAX, it might be useful for them to avoid this additional cost.
Perhaps a keyword argument to algorithms.segmented_reduce is in order.
Activity
oleksandr-pavlyk commentedon Mar 17, 2025
Approach 1: augment the state of the iterator
CCCL_POINTERIt should be noted that for
cccl_iterator_kind_t::CCCL_POINTERwe can advance the state on the host if the host type representing the type-erased device pointer stored the value size in bytes.With no modification to the device side, launcher would only copy the pointer value pointed to by
ptr.CCCL_ITERATORFor
CCCL_ITERATORwe need to augment state of the iterator withdiffence_type offsetstruct member on both the host and the device side.On the device side, the
make_input_iteratorneeds to be modified as suggested by @gevtushenko:Somehow these two structs have to be combined into a single type to service
cccl_iterator_tregardless of the iterator kind it represents.Approach 2: user-provided host function
We could augment
cccl_iterator_twith function pointer toadvance_host_fn(void *, uint64_t offset). This pointer will only be used forcccl_iterator_kind_t::ITERATOR.Numba allows us to compile
advancefunction to get a native function pointer:Approach 3: Make
advancefunction internalWe could make
linear_ida mandatory member ofindirect_iterator_tstruct, and generate both device and host functions for mutatinglinear_idin cccl.c.parallel.Then the
dereferenceandassignfunctions will need to become likegetitemandsetitemfunctions in python and takelinear_idas an argument. So instead ofdereference(state)we would be callingdereference(state, linear_id)and instead ofassign(state, val)we would be callingassign(state, linear_id, val).Implement indirect_host_incrementable_iterator_t as outlined as appro…
Implement indirect_host_incrementable_iterator_t as outlined as appro…
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Implement indirect_host_incrementable_iterator_t as outlined as appro…
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Implement indirect_host_incrementable_iterator_t as outlined as appro…
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Implement indirect_host_incrementable_iterator_t as outlined as appro…
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Implement indirect_host_incrementable_iterator_t as outlined as appro…
Checkpoint for implementing approach 2 in NVIDIAgh-4148
Host incrementable iterator approach 2 (#4697)
oleksandr-pavlyk commentedon May 21, 2025
I should comment that since approaches 1 and 3 effectively augment state of iterators with host accessible counter which needs to be copied to the device, these approaches lead to increase in register usage by kernels. There might be kernels where such an increase causes a performance cliff due to register spilling.
For this reason, approach 2 was implemented and merged in gh-4697. It does require host compiler (provided by Numba) and increases the cost of creating the algorithm, but this cost would only incur for those algorithms that employ streaming approach and so far there are few such algorithms only.
The only issue is that we presently always compile host-callable
advancefunction. If some users know in advance thatnum_segmentswill never exceedINT_MAX, it might be useful for them to avoid this additional cost.Perhaps a keyword argument to
algorithms.segmented_reduceis in order.