dace.codegen.targets package

Submodules

dace.codegen.targets.cpu module

class dace.codegen.targets.cpu.CPUCodeGen(frame_codegen, sdfg)

Bases: TargetCodeGenerator

SDFG CPU code generator.

allocate_array(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, declaration_stream, allocation_stream, allocate_nested_data=True)

Generates code for allocating an array, outputting to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (AccessNode) – The data node to generate allocation for.

  • nodedesc (Data) – The data descriptor to allocate.

  • global_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • declaration_stream (CodeIOStream) – A CodeIOStream object that points to the point of array declaration.

  • allocation_stream (CodeIOStream) – A CodeIOStream object that points to the call-site of array allocation.

Return type:

None

allocate_reference(sdfg, cfg, dfg, state_id, node, global_stream, declaration_stream, allocation_stream)
Return type:

None

allocate_view(sdfg, cfg, dfg, state_id, node, global_stream, declaration_stream, allocation_stream, decouple_array_interfaces=False)

Allocates (creates pointer and refers to original) a view of an existing array, scalar, or view.

Return type:

None

static cmake_options()

Returns a list of CMake options that this target needs to be passed into the cmake command during configuration.

copy_memory(sdfg, cfg, dfg, state_id, src_node, dst_node, edge, function_stream, callsite_stream)

Generates code for copying memory, either from a data access node (array/stream) to another, a code node (tasklet/nested SDFG) to another, or a combination of the two.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • src_node (Tasklet | AccessNode) – The source node to generate copy code for.

  • dst_node (Tasklet | AccessNode) – The destination node to generate copy code for.

  • edge (Tuple[Node, str | None, Node, str | None, Memlet]) – The edge representing the copy (in the innermost scope, adjacent to either the source or destination node).

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

deallocate_array(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, callsite_stream)

Generates code for deallocating an array, outputting to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (AccessNode) – The data node to generate deallocation for.

  • nodedesc (Data) – The data descriptor to deallocate.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

declare_array(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, declaration_stream)

Generates code for declaring an array without allocating it, outputting to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (Node) – The data node to generate allocation for.

  • nodedesc (Data) – The data descriptor to allocate.

  • global_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • declaration_stream (CodeIOStream) – A CodeIOStream object that points to the point of array declaration.

Return type:

None

define_out_memlet(sdfg, cfg, state_dfg, state_id, src_node, dst_node, edge, function_stream, callsite_stream)
Return type:

None

emit_interstate_variable_declaration(name, dtype, callsite_stream, sdfg)

Emits the declaration of an interstate variable at the given call-site.

Parameters:
  • name (str) – The name of the variable.

  • dtype (typeclass) – The data type of the variable.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

  • sdfg (SDFG) – The SDFG in which the variable is declared.

generate_node(sdfg, cfg, dfg, state_id, node, function_stream, callsite_stream)

Generates code for a single node, outputting it to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (ScopeSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (Node) – The node to generate code from.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

generate_nsdfg_arguments(sdfg, cfg, dfg, state, node)
generate_nsdfg_call(sdfg, cfg, state, node, memlet_references, sdfg_label, state_struct=True)
generate_nsdfg_header(sdfg, cfg, state, state_id, node, memlet_references, sdfg_label, state_struct=True)
generate_scope(sdfg, cfg, dfg_scope, state_id, function_stream, callsite_stream)

Generates code for an SDFG state scope (from a scope-entry node to its corresponding scope-exit node), outputting it to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg_scope (ScopeSubgraphView) – The ScopeSubgraphView to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

generate_scope_postamble(sdfg, dfg_scope, state_id, function_stream, outer_stream, inner_stream)

Generates code for the end of an SDFG scope, outputting it to the given code streams.

Parameters:
  • sdfg – The SDFG to generate code from.

  • dfg_scope – The ScopeSubgraphView to generate code from.

  • state_id – The node ID of the state in the given SDFG.

  • function_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • outer_stream – A CodeIOStream object that points to the code after the scope (e.g., after for-loop closing braces or kernel invocations).

  • inner_stream – A CodeIOStream object that points to the end of the inner scope code (e.g., before for-loop closing braces or end of kernel).

generate_scope_preamble(sdfg, dfg_scope, state_id, function_stream, outer_stream, inner_stream)

Generates code for the beginning of an SDFG scope, outputting it to the given code streams.

Parameters:
  • sdfg – The SDFG to generate code from.

  • dfg_scope – The ScopeSubgraphView to generate code from.

  • state_id – The node ID of the state in the given SDFG.

  • function_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • outer_stream – A CodeIOStream object that points to the code before the scope generation (e.g., before for-loops or kernel invocations).

  • inner_stream – A CodeIOStream object that points to the beginning of the scope code (e.g., inside for-loops or beginning of kernel).

generate_tasklet_postamble(sdfg, cfg, dfg_scope, state_id, node, function_stream, before_memlets_stream, after_memlets_stream)

Generates code for the end of a tasklet. This method is intended to be overloaded by subclasses.

Parameters:
  • sdfg – The SDFG to generate code from.

  • dfg_scope – The ScopeSubgraphView to generate code from.

  • state_id – The node ID of the state in the given SDFG.

  • node – The tasklet node in the state.

  • function_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • before_memlets_stream – A CodeIOStream object that will emit code before output memlets are generated.

  • after_memlets_stream – A CodeIOStream object that will emit code after output memlets are generated.

generate_tasklet_preamble(sdfg, cfg, dfg_scope, state_id, node, function_stream, before_memlets_stream, after_memlets_stream)

Generates code for the beginning of a tasklet. This method is intended to be overloaded by subclasses.

Parameters:
  • sdfg – The SDFG to generate code from.

  • dfg_scope – The ScopeSubgraphView to generate code from.

  • state_id – The node ID of the state in the given SDFG.

  • node – The tasklet node in the state.

  • function_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • before_memlets_stream – A CodeIOStream object that will emit code before input memlets are generated.

  • after_memlets_stream – A CodeIOStream object that will emit code after input memlets are generated.

get_generated_codeobjects()

Returns a list of generated CodeObject classes corresponding to files with generated code. If an empty list is returned (default) then this code generator does not create new files.

See:

CodeObject

property has_finalizer

Returns True if the target generates a __dace_exit_<TARGET> function that should be called on finalization.

property has_initializer

Returns True if the target generates a __dace_init_<TARGET> function that should be called on initialization.

language = 'cpp'
make_ptr_assignment(src_expr, src_dtype, dst_expr, dst_dtype, codegen=None)

Write source to destination, where the source is a scalar, and the destination is a pointer.

Returns:

String of C++ performing the write.

make_ptr_vector_cast(*args, **kwargs)
memlet_ctor(sdfg, memlet, dtype, is_output)
Return type:

str

memlet_definition(sdfg, memlet, output, local_name, conntype=None, allow_shadowing=False, codegen=None)
memlet_stream_ctor(sdfg, memlet)
Return type:

str

memlet_view_ctor(sdfg, memlet, dtype, is_output)
Return type:

str

process_out_memlets(sdfg, cfg, state_id, node, dfg, dispatcher, result, locals_defined, function_stream, skip_wcr=False, codegen=None)
ptr(name, desc, sdfg=None, subset=None, is_write=None, ancestor=0)

Returns a string that points to the data based on its name and descriptor.

Parameters:
  • name (str) – Data name.

  • desc (Data) – Data descriptor.

  • sdfg (SDFG) – SDFG in which the data resides.

  • subset (Subset | None) – Optional subset associated with the data.

  • is_write (bool | None) – Whether the access is a write access.

  • ancestor (int) – Scope ancestor level.

Return type:

str

Returns:

C-compatible name that can be used to access the data.

target_name = 'cpu'
title = 'CPU'
unparse_tasklet(sdfg, cfg, state_id, dfg, node, function_stream, inner_stream, locals, ldepth, toplevel_schedule)
write_and_resolve_expr(sdfg, memlet, nc, outname, inname, indices=None, dtype=None)

Emits a conflict resolution call from a memlet.

dace.codegen.targets.cuda module

class dace.codegen.targets.cuda.CUDACodeGen(frame_codegen, sdfg)

Bases: TargetCodeGenerator

GPU (CUDA/HIP) code generator.

allocate_array(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, declaration_stream, allocation_stream)

Generates code for allocating an array, outputting to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (AccessNode) – The data node to generate allocation for.

  • nodedesc (Data) – The data descriptor to allocate.

  • global_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • declaration_stream (CodeIOStream) – A CodeIOStream object that points to the point of array declaration.

  • allocation_stream (CodeIOStream) – A CodeIOStream object that points to the call-site of array allocation.

Return type:

None

allocate_stream(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, declaration_stream, allocation_stream)
Return type:

None

static cmake_options()

Returns a list of CMake options that this target needs to be passed into the cmake command during configuration.

copy_memory(sdfg, cfg, dfg, state_id, src_node, dst_node, memlet, function_stream, callsite_stream)

Generates code for copying memory, either from a data access node (array/stream) to another, a code node (tasklet/nested SDFG) to another, or a combination of the two.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • src_node (Tasklet | AccessNode) – The source node to generate copy code for.

  • dst_node (CodeNode | AccessNode) – The destination node to generate copy code for.

  • edge – The edge representing the copy (in the innermost scope, adjacent to either the source or destination node).

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

deallocate_array(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, callsite_stream)

Generates code for deallocating an array, outputting to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (AccessNode) – The data node to generate deallocation for.

  • nodedesc (Data) – The data descriptor to deallocate.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

deallocate_stream(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, callsite_stream)
Return type:

None

declare_array(sdfg, cfg, dfg, state_id, node, nodedesc, function_stream, declaration_stream)

Generates code for declaring an array without allocating it, outputting to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (AccessNode) – The data node to generate allocation for.

  • nodedesc (Data) – The data descriptor to allocate.

  • global_stream – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • declaration_stream (CodeIOStream) – A CodeIOStream object that points to the point of array declaration.

Return type:

None

define_out_memlet(sdfg, cfg, state_dfg, state_id, src_node, dst_node, edge, function_stream, callsite_stream)
Return type:

None

emit_interstate_variable_declaration(name, dtype, callsite_stream, sdfg)

Emits the declaration of an interstate variable at the given call-site.

Parameters:
  • name (str) – The name of the variable.

  • dtype (typeclass) – The data type of the variable.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

  • sdfg (SDFG) – The SDFG in which the variable is declared.

generate_devicelevel_scope(sdfg, cfg, dfg_scope, state_id, function_stream, callsite_stream)
Return type:

None

generate_devicelevel_state(sdfg, cfg, state, function_stream, callsite_stream)
Return type:

None

generate_kernel_scope(sdfg, cfg, dfg_scope, state_id, kernel_map, kernel_name, grid_dims, block_dims, has_tbmap, has_dtbmap, kernel_params, function_stream, kernel_stream)
Return type:

None

generate_node(sdfg, cfg, dfg, state_id, node, function_stream, callsite_stream)

Generates code for a single node, outputting it to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg (StateSubgraphView) – The SDFG state to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • node (Node) – The node to generate code from.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

generate_nsdfg_arguments(sdfg, cfg, dfg, state, node)
generate_nsdfg_call(sdfg, cfg, state, node, memlet_references, sdfg_label)
generate_nsdfg_header(sdfg, cfg, state, state_id, node, memlet_references, sdfg_label)
generate_scope(sdfg, cfg, dfg_scope, state_id, function_stream, callsite_stream)

Generates code for an SDFG state scope (from a scope-entry node to its corresponding scope-exit node), outputting it to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg_scope (StateSubgraphView) – The ScopeSubgraphView to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

generate_state(sdfg, cfg, state, function_stream, callsite_stream, generate_state_footer=False)

Generates code for an SDFG state, outputting it to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • state (SDFGState) – The SDFGState to generate code from.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

get_generated_codeobjects()

Returns a list of generated CodeObject classes corresponding to files with generated code. If an empty list is returned (default) then this code generator does not create new files.

See:

CodeObject

get_kernel_dimensions(dfg_scope)

Determines a GPU kernel’s grid/block dimensions from map scopes.

Ruleset for kernel dimensions:

  1. If only one map (device-level) exists, of an integer set S,

    the block size is 32x1x1 and grid size is ceil(|S|/32) in 1st dimension.

  2. If nested thread-block maps exist (T_1,...,T_n), grid

    size is |S| and block size is max(|T_1|,...,|T_n|) with block specialization.

  3. If block size can be overapproximated, it is (for

    dynamically-sized blocks that are bounded by a predefined size).

  4. If nested device maps exist, they generate extra grid dimensions (block size 1)

    as the sum of all their sizes (|T_1| + ... + |T_n|)

Note:

Kernel dimensions are separate from the map variables, and they should be treated as such.

Note:

To make use of the grid/block 3D registers, we use multi- dimensional kernels up to 3 dimensions, and flatten the rest into the third dimension.

get_next_scope_entries(dfg, scope_entry)
get_tb_maps_recursive(subgraph)
property has_finalizer

Returns True if the target generates a __dace_exit_<TARGET> function that should be called on finalization.

property has_initializer

Returns True if the target generates a __dace_init_<TARGET> function that should be called on initialization.

make_ptr_vector_cast(*args, **kwargs)
node_dispatch_predicate(sdfg, state, node)
preprocess(sdfg)

Called before code generation on any target that will be dispatched. Used for making modifications on the SDFG prior to code generation.

Note:

Post-conditions assume that the SDFG will NOT be changed after this point.

Parameters:

sdfg (SDFG) – The SDFG to modify in-place.

Return type:

None

process_out_memlets(*args, **kwargs)
ptr(name, desc, sdfg=None, subset=None, is_write=None, ancestor=0)

Returns a string that points to the data based on its name and descriptor.

Parameters:
  • name (str) – Data name.

  • desc (Data) – Data descriptor.

  • sdfg (SDFG) – SDFG in which the data resides.

  • subset (Subset | None) – Optional subset associated with the data.

  • is_write (bool | None) – Whether the access is a write access.

  • ancestor (int) – Scope ancestor level.

Return type:

str

Returns:

C-compatible name that can be used to access the data.

state_dispatch_predicate(sdfg, state)
target_name = 'cuda'
title = 'CUDA'
dace.codegen.targets.cuda.cpu_to_gpu_cpred(sdfg, state, src_node, dst_node)

Copy predicate from CPU to GPU that determines when a copy is illegal. Returns True if copy is illegal, False otherwise.

dace.codegen.targets.cuda.prod(iterable)

dace.codegen.targets.framecode module

class dace.codegen.targets.framecode.DaCeCodeGenerator(sdfg)

Bases: object

DaCe code generator class that writes the generated code for SDFG state machines, and uses a dispatcher to generate code for individual states based on the target.

allocate_arrays_in_scope(sdfg, cfg, scope, function_stream, callsite_stream)
Return type:

None

deallocate_arrays_in_scope(sdfg, cfg, scope, function_stream, callsite_stream)
determine_allocation_lifetime(top_sdfg)

Determines where (at which scope/state/SDFG) each data descriptor will be allocated/deallocated.

Parameters:

top_sdfg (SDFG) – The top-level SDFG to determine for.

property dispatcher
free_symbols(obj)
generate_code(sdfg, schedule, cfg_id='')

Generate frame code for a given SDFG, calling registered targets’ code generation callbacks for them to generate their own code.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code for.

  • schedule (ScheduleType | None) – The schedule the SDFG is currently located, or None if the SDFG is top-level.

  • cfg_id (str) – An optional string id given to the SDFG label

Return type:

Tuple[str, str, Set[TargetCodeGenerator], Set[str]]

Returns:

A tuple of the generated global frame code, local frame code, and a set of targets that have been used in the generation of this SDFG.

generate_constants(sdfg, callsite_stream)
generate_external_memory_management(sdfg, callsite_stream)

If external data descriptors are found in the SDFG (or any nested SDFGs), this function will generate exported functions to (1) get the required memory size per storage location (__dace_get_external_memory_size_<STORAGE>, where <STORAGE> can be CPU_Heap or any other dtypes.StorageType); and (2) set the externally-allocated pointer to the generated code’s internal state (__dace_set_external_memory_<STORAGE>).

generate_fileheader(sdfg, global_stream, backend='frame')

Generate a header in every output file that includes custom types and constants.

Parameters:
  • sdfg (SDFG) – The input SDFG.

  • global_stream (CodeIOStream) – Stream to write to (global).

  • backend (str) – Whose backend this header belongs to.

Generate the footer of the frame-code. Code exists in a separate function for overriding purposes.

Parameters:
  • sdfg (SDFG) – The input SDFG.

  • global_stream (CodeIOStream) – Stream to write to (global).

  • callsite_stream (CodeIOStream) – Stream to write to (at call site).

generate_header(sdfg, global_stream, callsite_stream)

Generate the header of the frame-code. Code exists in a separate function for overriding purposes.

Parameters:
  • sdfg (SDFG) – The input SDFG.

  • global_stream (CodeIOStream) – Stream to write to (global).

  • callsite_stream (CodeIOStream) – Stream to write to (at call site).

generate_state(sdfg, cfg, state, global_stream, callsite_stream, generate_state_footer=True)
generate_states(sdfg, global_stream, callsite_stream)
Return type:

Set[SDFGState]

preprocess(sdfg)

Called before code generation. Used for making modifications on the SDFG prior to code generation.

Note:

Post-conditions assume that the SDFG will NOT be changed after this point.

Parameters:

sdfg (SDFG) – The SDFG to modify in-place.

Return type:

None

symbols_and_constants(sdfg)

dace.codegen.targets.mpi module

class dace.codegen.targets.mpi.MPICodeGen(frame_codegen, sdfg)

Bases: TargetCodeGenerator

An MPI code generator.

static cmake_options()

Returns a list of CMake options that this target needs to be passed into the cmake command during configuration.

generate_scope(sdfg, cfg, dfg_scope, state_id, function_stream, callsite_stream)

Generates code for an SDFG state scope (from a scope-entry node to its corresponding scope-exit node), outputting it to the given code streams.

Parameters:
  • sdfg (SDFG) – The SDFG to generate code from.

  • dfg_scope (StateSubgraphView) – The ScopeSubgraphView to generate code from.

  • state_id (int) – The node ID of the state in the given SDFG.

  • function_stream (CodeIOStream) – A CodeIOStream object that will be generated outside the calling code, for use when generating global functions.

  • callsite_stream (CodeIOStream) – A CodeIOStream object that points to the current location (call-site) in the code.

Return type:

None

get_generated_codeobjects()

Returns a list of generated CodeObject classes corresponding to files with generated code. If an empty list is returned (default) then this code generator does not create new files.

See:

CodeObject

property has_finalizer

Returns True if the target generates a __dace_exit_<TARGET> function that should be called on finalization.

property has_initializer

Returns True if the target generates a __dace_init_<TARGET> function that should be called on initialization.

language = 'cpp'
target_name = 'mpi'
title = 'MPI'

Module contents