docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
sdfg::gpu::GPUReduceDispatcher Class Referenceabstract

Atomics-based GPU code generator for structured_control_flow::Reduce. More...

#include <sdfg/targets/gpu/gpu_reduce_dispatcher.h>

Inheritance diagram for sdfg::gpu::GPUReduceDispatcher:
[legend]

Public Member Functions

 GPUReduceDispatcher (codegen::LanguageExtension &language_extension, StructuredSDFG &sdfg, analysis::AnalysisManager &analysis_manager, structured_control_flow::Reduce &node, codegen::InstrumentationPlan &instrumentation_plan, codegen::ArgCapturePlan &arg_capture_plan)
void dispatch_node (codegen::PrettyPrinter &main_stream, codegen::PrettyPrinter &globals_stream, codegen::CodeSnippetFactory &library_snippet_factory) override
codegen::InstrumentationInfo instrumentation_info () const override
 Bad design.
Public Member Functions inherited from sdfg::codegen::NodeDispatcher
 NodeDispatcher (LanguageExtension &language_extension, StructuredSDFG &sdfg, analysis::AnalysisManager &analysis_manager, structured_control_flow::ControlFlowNode &node, InstrumentationPlan &instrumentation_plan, ArgCapturePlan &arg_capture_plan)
virtual ~NodeDispatcher ()=default
InstrumentationInfo instrumentation_info_for_group () const
virtual void dispatch (PrettyPrinter &main_stream, PrettyPrinter &globals_stream, CodeSnippetFactory &library_snippet_factory)

Protected Member Functions

virtual std::string schedule_value () const =0
 Schedule value string identifying this backend's GPU schedule ("CUDA"/"ROCM").
virtual codegen::TargetType target_type () const =0
 Target type used for instrumentation info.
virtual std::unique_ptr< codegen::LanguageExtension > create_device_language_extension () const =0
 Create the backend device-code language extension (CUDA/HIP).
virtual bool is_device_pointer_storage (const types::StorageType &storage) const =0
 True if storage designates a device-resident generic pointer for this backend.
virtual std::string kernel_file_extension () const =0
 File extension for the kernel translation unit ("cu"/"rocm.cpp").
virtual void emit_kernel_includes (codegen::CodeSnippetFactory &library_snippet_factory) const =0
 Emit the backend include directives required by the generated kernel.
virtual void emit_library_preamble (codegen::PrettyPrinter &library_stream) const =0
 Emit any extra preamble at the top of the kernel translation unit.
virtual void emit_kernel_call (codegen::PrettyPrinter &main_stream, const std::string &kernel_name, symbolic::Expression &num_blocks, symbolic::Expression &block_size, std::vector< std::string > &arguments_device)=0
 Emit the host-side kernel launch.
bool is_nested_in_gpu_kernel ()
 True if this Reduce is nested inside an enclosing GPU kernel (an ancestor Map with this backend's GPU schedule).
void dispatch_header (codegen::PrettyPrinter &stream, const std::string &kernel_name, std::vector< std::string > &arguments_declaration)
void dispatch_reduce_core (codegen::CodeSnippetFactory &library_snippet_factory, codegen::PrettyPrinter &library_stream, GPUDimension dimension, bool declare_scope_variables, std::vector< std::string > &scope_variables)
 Emit the privatize + grid-stride reduce loop + atomic merge into the given (device) stream, parallelized on dimension.
void dispatch_atomic_merge (codegen::PrettyPrinter &library_stream, codegen::CodeSnippetFactory &library_snippet_factory, const structured_control_flow::ReductionInfo &reduction)
 Emit the device-side helper(s) and the atomic merge statement that combines a thread's private partial into the global accumulator.
Protected Member Functions inherited from sdfg::codegen::NodeDispatcher
virtual bool begin_node (PrettyPrinter &stream)
virtual void end_node (PrettyPrinter &stream, bool has_declaration)

Protected Attributes

structured_control_flow::Reduce & node_
Protected Attributes inherited from sdfg::codegen::NodeDispatcher
LanguageExtension & language_extension_
StructuredSDFG & sdfg_
InstrumentationPlan & instrumentation_plan_
ArgCapturePlan & arg_capture_plan_
analysis::AnalysisManager & analysis_manager_

Detailed Description

Atomics-based GPU code generator for structured_control_flow::Reduce.

This is the target-agnostic core shared by the CUDA and ROCm reduce dispatchers. A Reduce loop carries one or more associative/commutative reductions (structured_control_flow::ReductionInfo) into a loop-invariant accumulator; this dispatcher handles a Reduce whose own schedule is a GPU schedule, i.e. the reduction dimension is parallelized across threads on its configured GPU dimension.

Correctness strategy ("atomics baseline"):

  • The reduction dimension is parallelized with a grid-stride loop on the schedule's dimension: thread t starts at init + t*stride and advances by num_threads*stride. This is correct for any launch geometry (even a single thread), so the lowering composes whether the Reduce is top-level or nested inside a GPU map.
  • For every reduction the accumulator is privatized: a thread-local partial is declared and initialized to the operator's identity, and the accumulator container is shadowed to point at that private storage so the in-body combine (acc = acc OP x) accumulates into the register.
  • After the loop, each thread merges its private partial into the real device-resident accumulator with an atomic combine (native atomicAdd where available, otherwise a compare-and-swap loop). Associativity and commutativity (guaranteed by the Reduce node) make the merge order-free.

Subclasses supply only the backend-specific policy (language extension, device-pointer storage, kernel launch syntax and includes).

Constructor & Destructor Documentation

◆ GPUReduceDispatcher()

sdfg::gpu::GPUReduceDispatcher::GPUReduceDispatcher ( codegen::LanguageExtension & language_extension,
StructuredSDFG & sdfg,
analysis::AnalysisManager & analysis_manager,
structured_control_flow::Reduce & node,
codegen::InstrumentationPlan & instrumentation_plan,
codegen::ArgCapturePlan & arg_capture_plan )

Member Function Documentation

◆ create_device_language_extension()

virtual std::unique_ptr< codegen::LanguageExtension > sdfg::gpu::GPUReduceDispatcher::create_device_language_extension ( ) const
protectedpure virtual

Create the backend device-code language extension (CUDA/HIP).

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ dispatch_atomic_merge()

void sdfg::gpu::GPUReduceDispatcher::dispatch_atomic_merge ( codegen::PrettyPrinter & library_stream,
codegen::CodeSnippetFactory & library_snippet_factory,
const structured_control_flow::ReductionInfo & reduction )
protected

Emit the device-side helper(s) and the atomic merge statement that combines a thread's private partial into the global accumulator.

◆ dispatch_header()

void sdfg::gpu::GPUReduceDispatcher::dispatch_header ( codegen::PrettyPrinter & stream,
const std::string & kernel_name,
std::vector< std::string > & arguments_declaration )
protected

◆ dispatch_node()

void sdfg::gpu::GPUReduceDispatcher::dispatch_node ( codegen::PrettyPrinter & main_stream,
codegen::PrettyPrinter & globals_stream,
codegen::CodeSnippetFactory & library_snippet_factory )
overridevirtual

◆ dispatch_reduce_core()

void sdfg::gpu::GPUReduceDispatcher::dispatch_reduce_core ( codegen::CodeSnippetFactory & library_snippet_factory,
codegen::PrettyPrinter & library_stream,
GPUDimension dimension,
bool declare_scope_variables,
std::vector< std::string > & scope_variables )
protected

Emit the privatize + grid-stride reduce loop + atomic merge into the given (device) stream, parallelized on dimension.

Parameters
declare_scope_variablesWhen true (top-level kernel), declare the loop-local scope variables here. When false (nested), the enclosing map already declared them.

◆ emit_kernel_call()

virtual void sdfg::gpu::GPUReduceDispatcher::emit_kernel_call ( codegen::PrettyPrinter & main_stream,
const std::string & kernel_name,
symbolic::Expression & num_blocks,
symbolic::Expression & block_size,
std::vector< std::string > & arguments_device )
protectedpure virtual

Emit the host-side kernel launch.

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ emit_kernel_includes()

virtual void sdfg::gpu::GPUReduceDispatcher::emit_kernel_includes ( codegen::CodeSnippetFactory & library_snippet_factory) const
protectedpure virtual

Emit the backend include directives required by the generated kernel.

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ emit_library_preamble()

virtual void sdfg::gpu::GPUReduceDispatcher::emit_library_preamble ( codegen::PrettyPrinter & library_stream) const
protectedpure virtual

Emit any extra preamble at the top of the kernel translation unit.

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ instrumentation_info()

codegen::InstrumentationInfo sdfg::gpu::GPUReduceDispatcher::instrumentation_info ( ) const
overridevirtual

Bad design.

We already have fields in this class and have to bind an instance to a single node. Just set a field to sth. of the instrumentation plan where the node can register more details. But this is a breaking change

Reimplemented from sdfg::codegen::NodeDispatcher.

◆ is_device_pointer_storage()

virtual bool sdfg::gpu::GPUReduceDispatcher::is_device_pointer_storage ( const types::StorageType & storage) const
protectedpure virtual

True if storage designates a device-resident generic pointer for this backend.

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ is_nested_in_gpu_kernel()

bool sdfg::gpu::GPUReduceDispatcher::is_nested_in_gpu_kernel ( )
protected

True if this Reduce is nested inside an enclosing GPU kernel (an ancestor Map with this backend's GPU schedule).

◆ kernel_file_extension()

virtual std::string sdfg::gpu::GPUReduceDispatcher::kernel_file_extension ( ) const
protectedpure virtual

File extension for the kernel translation unit ("cu"/"rocm.cpp").

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ schedule_value()

virtual std::string sdfg::gpu::GPUReduceDispatcher::schedule_value ( ) const
protectedpure virtual

Schedule value string identifying this backend's GPU schedule ("CUDA"/"ROCM").

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

◆ target_type()

virtual codegen::TargetType sdfg::gpu::GPUReduceDispatcher::target_type ( ) const
protectedpure virtual

Target type used for instrumentation info.

Implemented in sdfg::cuda::CUDAReduceDispatcher, and sdfg::rocm::ROCMReduceDispatcher.

Member Data Documentation

◆ node_

structured_control_flow::Reduce& sdfg::gpu::GPUReduceDispatcher::node_
protected

The documentation for this class was generated from the following file: