//===----------------------------------------------------------------------===// // // Part of CUDA Experimental in CUDA C++ Core Libraries, // under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===// #include // cub::detail::choose_offset_t #include // cub::detail::CudaDriverLauncherFactory #include // cub::DispatchSegmentedReduce #include // cub::LoadModifier #include #include #include #include // std::exception #include #include #include // std::string #include // std::string_view #include // std::is_same_v #include // std::format #include // printf #include "jit_templates/templates/input_iterator.h" #include "jit_templates/templates/operation.h" #include "jit_templates/templates/output_iterator.h" #include "jit_templates/traits.h" #include "util/nvjitlink.h" #include "util/serialization.h" #include #include #include // cccl_type_info #include #include #include #include #include #include #include struct device_segmented_reduce_policy; using OffsetT = unsigned long long; static_assert(std::is_same_v, OffsetT>, "OffsetT must be size_t"); // check we can map OffsetT to ::cuda::std::uint64_t static_assert(std::is_unsigned_v); static_assert(sizeof(OffsetT) == sizeof(::cuda::std::uint64_t)); namespace segmented_reduce { static cccl_type_info get_accumulator_type(cccl_op_t /*op*/, cccl_iterator_t /*input_it*/, cccl_value_t init) { // TODO Should be decltype(op(init, *input_it)) but haven't implemented type arithmetic yet // so switching back to the old accumulator type logic for now return init.type; } std::string get_device_segmented_reduce_kernel_name( std::string_view reduction_op_t, std::string_view input_iterator_t, std::string_view output_iterator_t, std::string_view start_offset_iterator_t, std::string_view end_offset_iterator_t, cccl_value_t init, std::string_view accum_t) { std::string policy_selector_t; check(cccl_type_name_from_nvrtc(&policy_selector_t)); std::string offset_t; check(cccl_type_name_from_nvrtc(&offset_t)); const std::string init_t = cccl_type_enum_to_name(init.type.type); /* template // 8 DeviceSegmentedReduceKernel(...); */ return std::format( "cub::detail::segmented_reduce::DeviceSegmentedReduceKernel<{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}>", policy_selector_t, // 0 input_iterator_t, // 1 output_iterator_t, // 2 start_offset_iterator_t, // 3 end_offset_iterator_t, // 4 offset_t, // 5 reduction_op_t, // 6 init_t, // 7 accum_t); // 8 } struct segmented_reduce_kernel_source { cccl_device_segmented_reduce_build_result_t& build; CUkernel SegmentedReduceKernel() const { return build.segmented_reduce_kernel; } }; } // namespace segmented_reduce struct segmented_reduce_input_iterator_tag; struct segmented_reduce_output_iterator_tag; struct segmented_reduce_start_offset_iterator_tag; struct segmented_reduce_end_offset_iterator_tag; struct segmented_reduce_operation_tag; CUresult cccl_device_segmented_reduce_compile( cccl_device_segmented_reduce_build_result_t* build_ptr, cccl_iterator_t input_it, cccl_iterator_t output_it, cccl_iterator_t start_offset_it, cccl_iterator_t end_offset_it, cccl_op_t op, cccl_value_t init, int cc_major, int cc_minor, const char* cub_path, const char* thrust_path, const char* libcudacxx_path, const char* ctk_path, cccl_build_config* config) try { const char* name = "device_segmented_reduce"; const cccl_type_info accum_t = segmented_reduce::get_accumulator_type(op, input_it, init); const auto accum_cpp = cccl_type_enum_to_name(accum_t.type); const auto [input_iterator_name, input_iterator_src] = get_specialization(template_id(), input_it); const auto [output_iterator_name, output_iterator_src] = get_specialization(template_id(), output_it, accum_t); const auto [start_offset_iterator_name, start_offset_iterator_src] = get_specialization( template_id(), start_offset_it); const auto [end_offset_iterator_name, end_offset_iterator_src] = get_specialization(template_id(), end_offset_it); const auto [op_name, op_src] = get_specialization( template_id(), op, accum_t, accum_t, accum_t); // OffsetT is checked to match have 64-bit size const auto offset_t = cccl_type_enum_to_name(cccl_type_enum::CCCL_UINT64); const auto policy_sel = [&] { using namespace cub::detail; const auto accum_type = cccl_type_enum_to_cub_type(accum_t.type); const auto operation_t = cccl_op_kind_to_cub_op(op.type); const int offset_size = int{sizeof(OffsetT)}; return cub::detail::segmented_reduce::policy_selector{ accum_type, operation_t, offset_size, static_cast(accum_t.size)}; }(); // TODO(bgruber): drop this if tuning policies become formattable std::stringstream policy_sel_str; policy_sel_str << policy_sel(cuda::compute_capability{cc_major, cc_minor}); const auto policy_sel_expr = std::format("cub::detail::segmented_reduce::policy_selector_from_types<{}, {}, {}>", accum_cpp, offset_t, op_name); const auto final_src = std::format( R"XXX( #include #include #include {0} struct __align__({2}) storage_t {{ char data[{1}]; }}; {3} {4} {5} {6} {7} using device_segmented_reduce_policy = {8}; using namespace cub; using namespace cub::detail::reduce; using namespace cub::detail::segmented_reduce; static_assert( device_segmented_reduce_policy()(detail::current_tuning_cc()) == {9}, "Host generated and JIT compiled policy mismatch"); )XXX", jit_template_header_contents, // 0 input_it.value_type.size, // 1 input_it.value_type.alignment, // 2 input_iterator_src, // 3 output_iterator_src, // 4 op_src, // 5 start_offset_iterator_src, // 6 end_offset_iterator_src, // 7 policy_sel_expr, // 8 policy_sel_str.view()); // 9 #if false // CCCL_DEBUGGING_SWITCH fflush(stderr); printf("\nCODE4NVRTC BEGIN\n%sCODE4NVRTC END\n", final_src.c_str()); fflush(stdout); #endif std::string segmented_reduce_kernel_name = segmented_reduce::get_device_segmented_reduce_kernel_name( op_name, input_iterator_name, output_iterator_name, start_offset_iterator_name, end_offset_iterator_name, init, accum_cpp); std::string segmented_reduce_kernel_lowered_name; const std::string arch = std::format("-arch=sm_{0}{1}", cc_major, cc_minor); std::vector args = { arch.c_str(), cub_path, thrust_path, libcudacxx_path, ctk_path, "-rdc=true", "-dlto", "-DCUB_DISABLE_CDP", "-std=c++20", "-default-device"}; cccl::detail::extend_args_with_build_config(args, config); constexpr size_t num_lto_args = 2; const char* lopts[num_lto_args] = {"-lto", arch.c_str()}; const bool kernel_only = is_custom_op(op); // Collect all LTO-IRs to be linked (empty in kernel-only mode). nvrtc_linkable_list linkable_list; nvrtc_linkable_list_appender appender{linkable_list}; appender.append_operation(op); appender.add_iterator_definition(input_it); appender.add_iterator_definition(output_it); appender.add_iterator_definition(start_offset_it); appender.add_iterator_definition(end_offset_it); auto post_build = begin_linking_nvrtc_program(kernel_only ? 0 : num_lto_args, kernel_only ? nullptr : lopts) ->add_program(nvrtc_translation_unit{final_src.c_str(), name}) ->add_expression({segmented_reduce_kernel_name}) ->compile_program({args.data(), args.size()}) ->get_name({segmented_reduce_kernel_name, segmented_reduce_kernel_lowered_name}); struct free_deleter { void operator()(void* p) const { std::free(p); } }; static_assert(::cuda::is_trivially_copyable_v); const size_t policy_size = sizeof(policy_sel); std::unique_ptr policy_ptr(std::malloc(policy_size)); if (!policy_ptr) { return CUDA_ERROR_OUT_OF_MEMORY; } std::memcpy(policy_ptr.get(), &policy_sel, sizeof(policy_sel)); auto kernel_name = std::unique_ptr(duplicate_c_string(segmented_reduce_kernel_lowered_name)); build_ptr->cc = cc_major * 10 + cc_minor; build_ptr->accumulator_size = accum_t.size; // Zero-init fields set by _load, not _compile. build_ptr->library = nullptr; build_ptr->segmented_reduce_kernel = nullptr; // All potentially-throwing operations come before any release() calls so that // unique_ptrs automatically clean up on exception. if (kernel_only) { auto [ltoir_size, ltoir_data] = post_build->get_program_ltoir(); build_ptr->payload = ltoir_data.release(); build_ptr->payload_size = ltoir_size; build_ptr->payload_kind = CCCL_PAYLOAD_LTOIR; } else { nvrtc_link_result result = post_build->link_program()->add_link_list(linkable_list)->finalize_program(); build_ptr->payload = (void*) result.data.release(); build_ptr->payload_size = result.size; build_ptr->payload_kind = CCCL_PAYLOAD_CUBIN; } build_ptr->runtime_policy = policy_ptr.release(); build_ptr->runtime_policy_size = policy_size; build_ptr->segmented_reduce_kernel_lowered_name = kernel_name.release(); return CUDA_SUCCESS; } catch (const std::exception& exc) { fflush(stderr); printf("\nEXCEPTION in cccl_device_segmented_reduce_compile(): %s\n", exc.what()); fflush(stdout); return CUDA_ERROR_UNKNOWN; } CUresult cccl_device_segmented_reduce_load(cccl_device_segmented_reduce_build_result_t* build_ptr) try { if (build_ptr == nullptr || build_ptr->payload == nullptr || build_ptr->payload_size == 0 || build_ptr->payload_kind != CCCL_PAYLOAD_CUBIN || build_ptr->segmented_reduce_kernel_lowered_name == nullptr || build_ptr->segmented_reduce_kernel_lowered_name[0] == '\0') { return CUDA_ERROR_INVALID_VALUE; } CUresult status = cuLibraryLoadData(&build_ptr->library, build_ptr->payload, nullptr, nullptr, 0, nullptr, nullptr, 0); if (status != CUDA_SUCCESS) { return status; } try { check(cuLibraryGetKernel( &build_ptr->segmented_reduce_kernel, build_ptr->library, build_ptr->segmented_reduce_kernel_lowered_name)); } catch (...) { cuLibraryUnload(build_ptr->library); build_ptr->library = nullptr; throw; } return CUDA_SUCCESS; } catch (const std::exception& exc) { fflush(stderr); printf("\nEXCEPTION in cccl_device_segmented_reduce_load(): %s\n", exc.what()); fflush(stdout); return CUDA_ERROR_UNKNOWN; } CUresult cccl_device_segmented_reduce_build_ex( cccl_device_segmented_reduce_build_result_t* build_ptr, cccl_iterator_t input_it, cccl_iterator_t output_it, cccl_iterator_t start_offset_it, cccl_iterator_t end_offset_it, cccl_op_t op, cccl_value_t init, int cc_major, int cc_minor, const char* cub_path, const char* thrust_path, const char* libcudacxx_path, const char* ctk_path, cccl_build_config* config) { CUresult r = cccl_device_segmented_reduce_compile( build_ptr, input_it, output_it, start_offset_it, end_offset_it, op, init, cc_major, cc_minor, cub_path, thrust_path, libcudacxx_path, ctk_path, config); if (r != CUDA_SUCCESS) { return r; } CUresult load_r = cccl_device_segmented_reduce_load(build_ptr); if (load_r != CUDA_SUCCESS) { cccl_device_segmented_reduce_cleanup(build_ptr); } return load_r; } CUresult cccl_device_segmented_reduce( cccl_device_segmented_reduce_build_result_t build, void* d_temp_storage, size_t* temp_storage_bytes, cccl_iterator_t d_in, cccl_iterator_t d_out, uint64_t num_segments, cccl_iterator_t start_offset, cccl_iterator_t end_offset, cccl_op_t op, cccl_value_t init, size_t max_segment_size, CUstream stream) { bool pushed = false; CUresult error = CUDA_SUCCESS; try { pushed = try_push_context(); CUdevice cu_device; check(cuCtxGetDevice(&cu_device)); auto exec_status = cub::detail::segmented_reduce::dispatch( d_temp_storage, *temp_storage_bytes, indirect_arg_t{d_in}, indirect_iterator_t{d_out}, num_segments, indirect_iterator_t{start_offset}, indirect_iterator_t{end_offset}, indirect_arg_t{op}, indirect_arg_t{init}, max_segment_size, stream, *static_cast(build.runtime_policy), segmented_reduce::segmented_reduce_kernel_source{build}, cub::detail::CudaDriverLauncherFactory{cu_device, build.cc}); error = static_cast(exec_status); } catch (const std::exception& exc) { fflush(stderr); printf("\nEXCEPTION in cccl_device_segmented_reduce(): %s\n", exc.what()); fflush(stdout); error = CUDA_ERROR_UNKNOWN; } if (pushed) { CUcontext dummy; cuCtxPopCurrent(&dummy); } return error; } CUresult cccl_device_segmented_reduce_build( cccl_device_segmented_reduce_build_result_t* build, cccl_iterator_t d_in, cccl_iterator_t d_out, cccl_iterator_t begin_offset_in, cccl_iterator_t end_offset_in, cccl_op_t op, cccl_value_t init, int cc_major, int cc_minor, const char* cub_path, const char* thrust_path, const char* libcudacxx_path, const char* ctk_path) { return cccl_device_segmented_reduce_build_ex( build, d_in, d_out, begin_offset_in, end_offset_in, op, init, cc_major, cc_minor, cub_path, thrust_path, libcudacxx_path, ctk_path, nullptr); } CUresult cccl_device_segmented_reduce_cleanup(cccl_device_segmented_reduce_build_result_t* build_ptr) try { if (build_ptr == nullptr) { return CUDA_ERROR_INVALID_VALUE; } std::unique_ptr payload(reinterpret_cast(build_ptr->payload)); std::free(build_ptr->runtime_policy); std::unique_ptr kernel_name(build_ptr->segmented_reduce_kernel_lowered_name); if (build_ptr->library != nullptr) { check(cuLibraryUnload(build_ptr->library)); } return CUDA_SUCCESS; } catch (const std::exception& exc) { fflush(stderr); printf("\nEXCEPTION in cccl_device_segmented_reduce_cleanup(): %s\n", exc.what()); fflush(stdout); return CUDA_ERROR_UNKNOWN; } CUresult cccl_device_segmented_reduce_link_ltoir( cccl_device_segmented_reduce_build_result_t* build_ptr, const void** input_blobs, const size_t* input_sizes, size_t num_inputs) try { if (build_ptr == nullptr || build_ptr->payload == nullptr || build_ptr->payload_size == 0 || build_ptr->payload_kind != CCCL_PAYLOAD_LTOIR) { return CUDA_ERROR_INVALID_VALUE; } const int cc_major = build_ptr->cc / 10; const int cc_minor = build_ptr->cc % 10; std::vector all_blobs; std::vector all_sizes; all_blobs.push_back(build_ptr->payload); all_sizes.push_back(build_ptr->payload_size); if (num_inputs > 0 && (input_blobs == nullptr || input_sizes == nullptr)) { return CUDA_ERROR_INVALID_VALUE; } for (size_t i = 0; i < num_inputs; ++i) { if (input_blobs[i] == nullptr || input_sizes[i] == 0) { return CUDA_ERROR_INVALID_VALUE; } all_blobs.push_back(input_blobs[i]); all_sizes.push_back(input_sizes[i]); } auto [cubin, cubin_size] = nvjitlink_link(all_blobs.data(), all_sizes.data(), all_blobs.size(), cc_major, cc_minor); delete[] static_cast(build_ptr->payload); build_ptr->payload = (void*) cubin.release(); build_ptr->payload_size = cubin_size; build_ptr->payload_kind = CCCL_PAYLOAD_CUBIN; return CUDA_SUCCESS; } catch (const std::exception& exc) { printf("\nEXCEPTION in cccl_device_segmented_reduce_link_ltoir(): %s\n", exc.what()); return CUDA_ERROR_UNKNOWN; } CUresult cccl_device_segmented_reduce_serialize( const cccl_device_segmented_reduce_build_result_t* build_ptr, void** out_buf, size_t* out_size) try { if (build_ptr == nullptr || out_buf == nullptr || out_size == nullptr) { return CUDA_ERROR_INVALID_VALUE; } if (build_ptr->payload == nullptr || build_ptr->payload_size == 0 || build_ptr->runtime_policy == nullptr || build_ptr->runtime_policy_size == 0) { *out_buf = nullptr; *out_size = 0; return CUDA_ERROR_INVALID_VALUE; } using namespace cccl::serialization; buffer_writer w; write_header(w, CCCL_SERIALIZATION_ALGO_SEGMENTED_REDUCE, build_ptr->payload_kind, build_ptr->cc); w.write_pod(build_ptr->accumulator_size); w.write_blob(build_ptr->payload, build_ptr->payload_size); w.write_blob(build_ptr->runtime_policy, build_ptr->runtime_policy_size); w.write_cstring(build_ptr->segmented_reduce_kernel_lowered_name); w.release(out_buf, out_size); return CUDA_SUCCESS; } catch (const std::exception& exc) { fflush(stderr); printf("\nEXCEPTION in cccl_device_segmented_reduce_serialize(): %s\n", exc.what()); fflush(stdout); return CUDA_ERROR_UNKNOWN; } CUresult cccl_device_segmented_reduce_deserialize( cccl_device_segmented_reduce_build_result_t* build_ptr, const void* buf, size_t size) try { if (build_ptr == nullptr || buf == nullptr || size == 0) { return CUDA_ERROR_INVALID_VALUE; } using namespace cccl::serialization; buffer_reader r{buf, size}; const auto h = read_and_validate_header(r, CCCL_SERIALIZATION_ALGO_SEGMENTED_REDUCE); const uint64_t accum_size = r.read_pod(); std::unique_ptr payload_owner; size_t payload_size = 0; { void* p = nullptr; r.read_blob_new(&p, &payload_size); payload_owner.reset(static_cast(p)); } if (payload_size == 0) { throw std::runtime_error("serialization blob: empty payload"); } std::unique_ptr policy( static_cast( std::malloc(sizeof(cub::detail::segmented_reduce::policy_selector))), std::free); if (!policy) { return CUDA_ERROR_OUT_OF_MEMORY; } r.read_into(policy.get(), sizeof(cub::detail::segmented_reduce::policy_selector)); std::unique_ptr n_kernel{r.read_cstring_dup()}; cccl_device_segmented_reduce_build_result_t result{}; result.cc = static_cast(h.cc); result.payload_kind = static_cast(h.payload_kind); result.accumulator_size = accum_size; result.payload = payload_owner.release(); result.payload_size = payload_size; result.runtime_policy = policy.release(); result.runtime_policy_size = sizeof(cub::detail::segmented_reduce::policy_selector); result.segmented_reduce_kernel_lowered_name = n_kernel.release(); *build_ptr = result; return CUDA_SUCCESS; } catch (const std::exception& exc) { fflush(stderr); printf("\nEXCEPTION in cccl_device_segmented_reduce_deserialize(): %s\n", exc.what()); fflush(stdout); return CUDA_ERROR_UNKNOWN; }