[Discussion] Provide public, templated APIs for libcudf features #6495
Labels
feature request
New feature or request
libcudf
Affects libcudf (C++/CUDA) code.
proposal
Change current process or code
Description
Today, libcudf does not provide function templates in it's public interface. This is because for most users, the input data type is runtime information. Therefore, libcudf APIs operate on type-erased
column_view
objects where the runtime type information is stored in thedata_type
member and internally we dispatch to the appropriately typed code path, e.g., via thetype_dispatcher
.However, this design lacks the power and flexibility of iterator based interfaces. E.g., there is no way to fuse operations with something like a
thrust::transform_iterator
and instead requires materializing intermediate results. Internally, we side-step this issue with function templates in thedetail::
API that do operate on iterators. We can do this because inside a type-dispatched code path we know the type of the underlying data and can invoke the proper template instantiation (or use device-side dispatch in some cases like with theindexalator
). This allows greater flexibility and reuse of functionality within the library.For example, the public
cudf::gather
API expects thegather_map
to be specified as acolumn_view
. The implementation of this public API uses theindexalator
to convert the type-erased inputcolumn_view
into an iterator and forwards to adetail::gather
API that expects thegather_map
to be an iterator.It would be nice if we could offer external users some of this same power and flexibility, e.g., there may be cases where a user knows that a particular input will always be a certain type (or small set of types) and could invoke a function template with an iterator that fuses some operations to avoid intermediate materializations.
gather
is a good example of this where this would be useful. Today,gather
implements non-standard C++ behavior where a negative index wraps around from the end of the array:cudf/cpp/include/cudf/copying.hpp
Lines 46 to 47 in 421fddb
This is accomplished internally via a
transform_iterator
over the elements of thegather_map
. Instead, if there were a public iterator-basedgather
function, users could provide thetransform_iterator
themselves and thegather
implementation wouldn't be required to do the negative value wrapping. This is desirable because there are situations where we don't want negative values to wrap around, see #6479.Summary
I would like to see libcudf essentially provide two complementary public APIs:
column_view
s that just calls the APIs in 1.To a certain extent, libcudf is already designed this way. Many public APIs call
detail::
APIs that operate on iterators, e.g.,gather
,apply_boolean_mask
,copy_if
, etc. This is a convention typically followed in libcudf (though not often enough). We should document and standardize more formally on this pattern (see #6470).Additional Points of Consideration
The inability of Cython to define
__device__
functors/lambdas limits the ability of Cython/Python to take advantage of any function template API__device__
keyword and then compile the Cython lib withnvcc
, but there's some amount of work here.Testing
Outputs
column/table
s.Nullable inputs
thrust::optional<T>
, see [FEA] Replacepair_iterator
withthrust::optional
#6470Additional Context
This is an idea I've had bouncing around for quite a while now. I wanted to capture all of my thoughts in a single issue for discussion. It's not something I believe we must do, but we should at least explore it.
The text was updated successfully, but these errors were encountered: