Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ rtcx_embed_includes(
cudf/cpp/src/transform/jit INCLUDE_DIRECTORIES cudf/cpp/src
)

rtcx_embed_includes(
cudf_cuda_embed SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/reductions/jit DEST_DIRECTORY
cudf/cpp/src/reductions/jit INCLUDE_DIRECTORIES cudf/cpp/src
)

get_target_property(LIBCUDACXX_RAW_INCLUDE_DIRS CCCL::libcudacxx INTERFACE_INCLUDE_DIRECTORIES)

foreach(INC_DIR IN LISTS LIBCUDACXX_RAW_INCLUDE_DIRS)
Expand Down Expand Up @@ -1021,6 +1026,7 @@ add_library(
src/reductions/nunique.cpp
src/reductions/product.cu
src/reductions/quantile.cu
src/reductions/reduce_lto.cu
src/reductions/reductions.cpp
src/reductions/scan/ewm.cu
src/reductions/scan/rank_scan.cu
Expand Down
82 changes: 82 additions & 0 deletions cpp/include/cudf/detail/reduce_udf.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <cudf/errc.hpp>

#ifndef CUDF_REDUCE_INIT_SYMBOL
#define CUDF_REDUCE_INIT_SYMBOL cudf_reduce_init
#endif

#ifndef CUDF_REDUCE_FILTER_SYMBOL
#define CUDF_REDUCE_FILTER_SYMBOL cudf_reduce_filter
#endif

#ifndef CUDF_REDUCE_UPDATE_SYMBOL
#define CUDF_REDUCE_UPDATE_SYMBOL cudf_reduce_update
#endif

#ifndef CUDF_REDUCE_MERGE_SYMBOL
#define CUDF_REDUCE_MERGE_SYMBOL cudf_reduce_merge
#endif

#ifndef CUDF_REDUCE_FINALIZE_SYMBOL
#define CUDF_REDUCE_FINALIZE_SYMBOL cudf_reduce_finalize
#endif

#ifndef CUDF_REDUCE_INIT_UDF_PARAMS
#define CUDF_REDUCE_INIT_UDF_PARAMS
#endif

#ifndef CUDF_REDUCE_FILTER_UDF_PARAMS
#define CUDF_REDUCE_FILTER_UDF_PARAMS
#endif

#ifndef CUDF_REDUCE_UPDATE_UDF_PARAMS
#define CUDF_REDUCE_UPDATE_UDF_PARAMS
#endif

#ifndef CUDF_REDUCE_MERGE_UDF_PARAMS
#define CUDF_REDUCE_MERGE_UDF_PARAMS
#endif

#ifndef CUDF_REDUCE_FINALIZE_UDF_PARAMS
#define CUDF_REDUCE_FINALIZE_UDF_PARAMS
#endif

#ifndef CUDF_DISPATCH_REDUCE_INIT
#define CUDF_DISPATCH_REDUCE_INIT ::CUDF_REDUCE_INIT_SYMBOL
#endif

#ifndef CUDF_DISPATCH_REDUCE_FILTER
#define CUDF_DISPATCH_REDUCE_FILTER ::CUDF_REDUCE_FILTER_SYMBOL
#endif

#ifndef CUDF_DISPATCH_REDUCE_UPDATE
#define CUDF_DISPATCH_REDUCE_UPDATE ::CUDF_REDUCE_UPDATE_SYMBOL
#endif

#ifndef CUDF_DISPATCH_REDUCE_MERGE
#define CUDF_DISPATCH_REDUCE_MERGE ::CUDF_REDUCE_MERGE_SYMBOL
#endif

#ifndef CUDF_DISPATCH_REDUCE_FINALIZE
#define CUDF_DISPATCH_REDUCE_FINALIZE ::CUDF_REDUCE_FINALIZE_SYMBOL
#endif

#ifndef CUDF_DISPATCH_REDUCE_STATE
#define CUDF_DISPATCH_REDUCE_STATE ::cudf::jit::reduce::state<>
#endif

extern "C" __device__ ::cudf::errc CUDF_REDUCE_INIT_SYMBOL(CUDF_REDUCE_INIT_UDF_PARAMS);

extern "C" __device__ ::cudf::errc CUDF_REDUCE_FILTER_SYMBOL(CUDF_REDUCE_FILTER_UDF_PARAMS);

extern "C" __device__ ::cudf::errc CUDF_REDUCE_UPDATE_SYMBOL(CUDF_REDUCE_UPDATE_UDF_PARAMS);

extern "C" __device__ ::cudf::errc CUDF_REDUCE_MERGE_SYMBOL(CUDF_REDUCE_MERGE_UDF_PARAMS);

extern "C" __device__ ::cudf::errc CUDF_REDUCE_FINALIZE_SYMBOL(CUDF_REDUCE_FINALIZE_UDF_PARAMS);
8 changes: 8 additions & 0 deletions cpp/include/cudf/detail/udf_dispatch.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

// Placeholder for JIT-generated UDF forwarding definitions.
43 changes: 43 additions & 0 deletions cpp/include/cudf/detail/utilities/string_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <functional>
#include <string>
#include <string_view>

namespace cudf::detail {

/**
* @brief Joins an iterator range, optionally mapping each element before appending it.
*
* The mapper is invoked exactly once per element. Its result must be appendable to a
* `std::string`. Empty ranges produce an empty string; separators appear only between elements,
* including elements that map to empty strings. Supports single-pass iterators.
*
* @param first Iterator to the first element
* @param last Sentinel marking the end of the range
* @param separator Text inserted between adjacent elements
* @param mapper Optional callable or member pointer; defaults to the identity function
* @return The joined string
*/
template <typename Iterator, typename Sentinel, typename Mapper = std::identity>
std::string join_strings(Iterator first,
Sentinel last,
std::string_view separator,
Mapper mapper = {})
{
std::string result;
if (first == last) { return result; }
result += std::invoke(mapper, *first);
while (++first != last) {
result += separator;
result += std::invoke(mapper, *first);
}
return result;
}

} // namespace cudf::detail
87 changes: 87 additions & 0 deletions cpp/include/cudf/reduction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@
#pragma once

#include <cudf/aggregation.hpp>
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/column/scalar_column_view.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/types.hpp>
#include <cudf/udf.hpp>
#include <cudf/user_data.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/export.hpp>
#include <cudf/utilities/memory_resource.hpp>
#include <cudf/utilities/span.hpp>

#include <functional>
#include <memory>
#include <optional>
#include <span>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>

/**
* @file
Expand Down Expand Up @@ -263,6 +278,78 @@ namespace reduction {
bool is_valid_aggregation(data_type source, aggregation::Kind kind);
} // namespace reduction

/**
* @brief Non-owning columns, broadcast scalars and opaque device pointers.
*
* Keep referenced device storage alive until stream work completes.
*/
using reduce_input = std::variant<column_view, scalar_column_view, user_data>;

/**
* @brief Compiled callbacks defining a reduction; each returns @c cudf::errc.
*
* Parameters are ordered as follows; each ellipsis expands fields in specification order.
*
* | Callback | Parameters |
* |----------|------------|
* | init | state pointers... |
* | filter | row index, bool* selected, input values... |
* | update | row index, state pointers..., input values... |
* | merge | destination state pointers..., source state values... |
* | finalize | output pointers..., state values... |
*
* Initialize every independent state to its identity. Merge must be associative and preserve
* that identity; update/merge order is unspecified. An empty filter symbol selects all rows.
* A false selection or filter error skips update. Row indices refer to the input views.
*
* Inputs follow transform's decoded types, including dictionary keys and @c cudf::string_view.
* State and outputs support fixed-width types and strings. Keep referenced string characters
* alive until stream work completes; returned string columns own a copy.
*
* With @c null_aware::YES, inputs, state and outputs use @c cuda::std::optional<T>, with mutable
* parameters passed as pointers to optionals. Empty optionals represent nulls. Callbacks define
* null-state identities and output validity. With @c null_aware::NO, use plain values; rows with
* any null column/scalar input are skipped before filtering. Indices, the filter's bool pointer
* and @c void* user-data inputs are never optional. Filter and update share one set of row loads.
*
* Fragment bytes and symbol names are non-owning and must outlive the call. See @ref lto_fragment.
*/
struct reduce_udf {
lto_fragments fragments{}; ///< Device-code fragments
std::string_view init{}; ///< Initializes independent state
std::string_view update{}; ///< Incorporates a selected row
std::string_view merge{}; ///< Combines partial states
std::string_view finalize{}; ///< Writes output values
std::string_view filter{}; ///< Optional row predicate
};

/**
* @brief Reduces rows into one single-row column per requested output, in specification order.
*
* Column lengths must match; scalars and user data broadcast. With no column input there are
* zero rows. Empty inputs and selections finalize the identity state; callbacks determine
* values and validity. Input/state/output types and callback signatures follow @ref reduce_udf.
*
* @throws std::invalid_argument for mismatched input lengths or unsupported state/output types
* @throws cudf::evaluation_error for callback failures
* @param udf Compiled fragments and callback symbols
* @param is_null_aware Whether values use optional wrappers
* @param inputs Inputs in callback order
* @param state_types State fields in callback order
* @param outputs Returned types in finalization order
* @param stream Execution stream
* @param mr Output and temporary memory resources
* @return One single-row column per output
*/
std::vector<std::unique_ptr<column>> reduce_lto(
reduce_udf const& udf,
null_aware is_null_aware,
std::span<reduce_input const> inputs,
std::span<data_type const> state_types,
std::span<data_type const> outputs,
cuda::stream_ref stream = cudf::get_default_stream(),
cudf::memory_resources mr = cudf::get_current_device_resource_ref());

/** @} */ // end of group

} // namespace CUDF_EXPORT cudf
25 changes: 25 additions & 0 deletions cpp/include/cudf/user_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <cudf/utilities/export.hpp>

namespace CUDF_EXPORT cudf {

/**
* @brief A non-owning pointer to user-defined data passed to a UDF.
*
* A user-data input maps to a `void*` parameter at its specified input position in the UDF
* signature. The same pointer is passed for every row.
*
* The caller owns the referenced storage and must keep it accessible to device code until
* all operations using it have completed on the execution stream.
*/
struct user_data {
void* ptr = nullptr; ///< User-defined data pointer; may be null.
};

} // namespace CUDF_EXPORT cudf
42 changes: 42 additions & 0 deletions cpp/include/cudf/utilities/memory_layout.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#include <algorithm>
#include <cstddef>

namespace cudf::detail {

/**
* @brief Byte extent and alignment for sequentially placed objects.
*
* append() aligns each new object; aligned() adds aggregate tail padding for storage/array
* stride. Finish a nested aggregate with aligned() before appending it to its parent.
* The default layout is empty; memory_layout_of<T> includes sizeof(T)'s tail padding.
*/
struct memory_layout {
size_t alignment = 1; ///< Nonzero power-of-two base alignment
size_t size = 0; ///< Extent before optional aggregate tail padding

/// @brief Appends an already complete object layout at its next aligned offset.
constexpr memory_layout append(memory_layout const& other) const
{
auto const new_alignment = std::max(alignment, other.alignment);
auto const offset = (size + other.alignment - 1) & ~(other.alignment - 1);
return {.alignment = new_alignment, .size = offset + other.size};
}

/// @brief Includes tail padding so size is a multiple of alignment.
constexpr memory_layout aligned() const
{
return {.alignment = alignment, .size = (size + alignment - 1) & ~(alignment - 1)};
}
};

/// @brief Complete compiler-provided size and alignment of T, including empty classes.
template <typename T>
inline constexpr memory_layout memory_layout_of{.alignment = alignof(T), .size = sizeof(T)};

} // namespace cudf::detail
19 changes: 19 additions & 0 deletions cpp/src/jit/column_accessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
namespace cudf {
namespace jit {

/** @brief An opaque pointer input, broadcast to every row without optional wrapping. */
template <int32_t Index>
struct user_data_accessor {
using element_type = void*;
static constexpr int32_t index = Index;

static __device__ void* element(column_device_view_core const* cols, size_type)
{
return const_cast<void*>(cols[index].head());
}

static __device__ void* nullable_element(column_device_view_core const* cols, size_type row)
{
return element(cols, row);
}

static __device__ bool is_valid(column_device_view_core const*, size_type) { return true; }
};

template <int32_t Index, typename Column, typename Element, bool AsScalar, int32_t TableIndex>
struct column_accessor {
static constexpr int32_t index = Index;
Expand Down
18 changes: 18 additions & 0 deletions cpp/src/jit/udf_dispatch.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <cudf/errc.hpp>
#include <cudf/fixed_point/fixed_point.hpp>
#include <cudf/strings/string_view.cuh>
#include <cudf/types.hpp>
#include <cudf/wrappers/durations.hpp>
#include <cudf/wrappers/timestamps.hpp>

#include <cuda/std/optional>

#pragma nv_hdrstop // Only the target names and logical/physical signatures vary per dispatcher.

#include <cudf/detail/udf_dispatch.cuh>
Loading
Loading