How to use the dace.graph.nodes function in dace

To help you get started, we’ve selected a few dace examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spcl / dace / diode / property_renderer.py View on Github external
def delete_data_cb(self, button, data):
        name = data[0]
        sdfg = data[1]

        # Traverse the SDFG, find any occurance of data "name", if it exists
        # show an error and do not delete
        for state in sdfg.nodes():
            for node in state.nodes():
                if isinstance(node, dace.graph.nodes.AccessNode):
                    if str(node.data) == name:
                        self.show_delete_error(sdfg, state, node)
                        return None
            for memlet in state.edges():
                if str(memlet.data) == name:
                    self.show_delete_error(sdfg, state, memlet)
                    return None
        sdfg.remove_data(name)
        self.render_free_symbols(sdfg)
github spcl / dace / dace / sdfg.py View on Github external
def scope_symbols(dfg):
    """ Returns all symbols used in scopes within the given DFG, separated
        into (iteration variables, symbols used in subsets). """
    iteration_variables = collections.OrderedDict()
    subset_symbols = collections.OrderedDict()
    for n in dfg.nodes():
        if isinstance(n, dace.graph.nodes.NestedSDFG):
            iv, ss = n.sdfg.scope_symbols()
            iteration_variables.update(iv)
            subset_symbols.update(ss)
            continue
        if not isinstance(n, dace.graph.nodes.EntryNode):
            continue
        if isinstance(n, dace.graph.nodes.MapEntry):
            for param in n.params:
                iteration_variables[param] = dt.Scalar(
                    symbolic.symbol(param).dtype)
            for dim in n.map.range:
                try:
                    for i in dim:
                        if isinstance(i, sp.Expr):
                            subset_symbols.update((k.name, dt.Scalar(k.dtype))
                                                  for k in i.free_symbols)
                except TypeError:  # X object is not iterable
                    if isinstance(dim, sp.Expr):
                        result.update((k.name, dt.Scalar(k.dtype))
                                      for k in dim.free_symbols)
                    else:
github spcl / dace / dace / codegen / targets / xilinx.py View on Github external
in_ptr = ("{}_in".format(argname) if has_in_ptr else "nullptr")
                out_ptr = ("{}_out".format(argname)
                           if has_out_ptr else "nullptr")
                module_body_stream.write(
                    "dace::ArrayInterface<{}, {}> {}({}, {});".format(
                        arg.dtype.ctype, self._memory_widths[argname], argname,
                        in_ptr, out_ptr))
            module_body_stream.write("\n")

        # Allocate local transients
        data_to_allocate = (set(subgraph.top_level_transients()) -
                            set(sdfg.shared_transients()) -
                            set([p[1] for p in parameters]))
        allocated = set()
        for node in subgraph.nodes():
            if not isinstance(node, dace.graph.nodes.AccessNode):
                continue
            if node.data not in data_to_allocate or node.data in allocated:
                continue
            allocated.add(node.data)
            self._dispatcher.dispatch_allocate(sdfg, state, state_id, node,
                                               module_stream,
                                               module_body_stream)
            self._dispatcher.dispatch_initialize(sdfg, state, state_id, node,
                                                 module_stream,
                                                 module_body_stream)

        self._dispatcher.dispatch_subgraph(sdfg,
                                           subgraph,
                                           state_id,
                                           module_stream,
                                           module_body_stream,
github spcl / dace / dace / codegen / targets / cuda.py View on Github external
kernelmap_entry = dfg_scope.source_nodes()[0]
        grid_size = kernelmap_entry.map.range.size(True)[::-1]
        block_size = None

        # Linearize (flatten) rest of dimensions to third
        if len(grid_size) > 3:
            grid_size[2] = functools.reduce(sympy.mul.Mul, grid_size[2:], 1)
            del grid_size[3:]

        # Extend to 3 dimensions if necessary
        grid_size = grid_size + [1] * (3 - len(grid_size))

        # Obtain thread-block maps for case (2)
        tb_maps = [
            node.map for node, parent in dfg_scope.scope_dict().items()
            if parent == kernelmap_entry and isinstance(node, nodes.EntryNode)
            and node.schedule == dtypes.ScheduleType.GPU_ThreadBlock
        ]
        # Append thread-block maps from nested SDFGs
        for node in dfg_scope.scope_subgraph(kernelmap_entry).nodes():
            if isinstance(node, nodes.NestedSDFG):
                set_default_schedule_and_storage_types(node.sdfg,
                                                       node.schedule)

                tb_maps.extend([
                    n.map for state in node.sdfg.nodes()
                    for n in state.nodes() if isinstance(n, nodes.MapEntry)
                    and n.schedule == dtypes.ScheduleType.GPU_ThreadBlock
                ])

        # Case (1): no thread-block maps
        if len(tb_maps) == 0:
github spcl / dace / dace / sdfg.py View on Github external
def interstate_symbols(dfg):
    """ Returns all symbols used in interstate edges in nested SDFGs within
        this state. """
    assigned = collections.OrderedDict()
    used = collections.OrderedDict()
    for node in dfg.nodes():
        if isinstance(node, dace.graph.nodes.NestedSDFG):
            a, u = node.sdfg.interstate_symbols()
            assigned.update(a)
            used.update(u)
    return assigned, used
github spcl / dace / dace / codegen / targets / xilinx.py View on Github external
arrays.add(edge.dst_conn)

        callsite_stream.write('\n', sdfg, state_id, node)

        # Use outgoing edges to preallocate output local vars
        for edge in dfg.out_edges(node):
            v = edge.dst
            memlet = edge.data

            if edge.src_conn:

                if edge.src_conn in arrays:  # Disallow duplicates
                    continue

                # Special case: code->code
                if isinstance(edge.dst, dace.graph.nodes.CodeNode):
                    raise NotImplementedError(
                        "Tasklet to tasklet memlets not implemented")

                else:
                    dst_node = find_output_arraynode(state_dfg, edge)
                    self._dispatcher.dispatch_copy(node, dst_node, edge, sdfg,
                                                   state_dfg, state_id,
                                                   function_stream,
                                                   callsite_stream)

                # Also define variables in the C++ unparser scope
                self._cpu_codegen._locals.define(edge.src_conn, -1,
                                                 self._cpu_codegen._ldepth + 1)
                arrays.add(edge.src_conn)

        callsite_stream.write("\n////////////////////\n", sdfg, state_id, node)
github spcl / dace / dace / frontend / python / newast.py View on Github external
else:
                    new_name = memlet.data

                read_node = state.add_read(new_name)
                entry_node.add_in_connector(conn)
                state.add_edge(read_node, None, entry_node, conn, memlet)

        # Parse internal node inputs and indirect memory accesses
        if inputs:
            for conn, v in inputs.items():
                if v is None:  # Input already handled outside
                    continue
                if isinstance(v, nodes.Tasklet):
                    # Create a code->code node
                    new_scalar = self.sdfg.temp_data_name()
                    if isinstance(internal_node, nodes.NestedSDFG):
                        dtype = internal_node.sdfg.arrays[conn].dtype
                    else:
                        raise SyntaxError(
                            'Cannot determine connector type for '
                            'tasklet input dependency')
                    self.sdfg.add_scalar(new_scalar, dtype, transient=True)
                    state.add_edge(v, conn, internal_node, conn,
                                   dace.Memlet.simple(new_scalar, '0'))
                    if entry_node is not None:
                        state.add_edge(entry_node, None, v, None,
                                       dace.EmptyMemlet())
                    continue

                if isinstance(v, tuple):
                    memlet, inner_indices = v
                else:
github spcl / dace / dace / transformation / dataflow / map_collapse.py View on Github external
from dace.graph import nodes, nxutil
from dace.transformation import pattern_matching
from dace.properties import make_properties
from typing import Tuple


@registry.autoregister_params(singlestate=True)
@make_properties
class MapCollapse(pattern_matching.Transformation):
    """ Implements the Map Collapse pattern.

        Map-collapse takes two nested maps with M and N dimensions respectively,
        and collapses them to a single M+N dimensional map.
    """

    _outer_map_entry = nodes.MapEntry(nodes.Map("", [], []))
    _inner_map_entry = nodes.MapEntry(nodes.Map("", [], []))

    @staticmethod
    def expressions():
        return [
            nxutil.node_path_graph(
                MapCollapse._outer_map_entry,
                MapCollapse._inner_map_entry,
            )
        ]

    @staticmethod
    def can_be_applied(graph, candidate, expr_index, sdfg, strict=False):
        # Check the edges between the entries of the two maps.
        outer_map_entry = graph.nodes()[candidate[
            MapCollapse._outer_map_entry]]
github spcl / dace / dace / codegen / targets / cuda.py View on Github external
return (streams + 1) % concurrent_streams
            return streams + 1

        state_streams = []
        state_subsdfg_events = []

        for state in sdfg.nodes():
            # Start by annotating source nodes
            source_nodes = state.source_nodes()

            # Concurrency can only be found in each state
            max_streams = default_stream
            max_events = default_event

            for i, node in enumerate(source_nodes):
                if isinstance(node, nodes.AccessNode):
                    continue
                if isinstance(node, nodes.NestedSDFG):
                    if node.schedule == dtypes.ScheduleType.GPU_Device:
                        continue
                node._cuda_stream = max_streams
                node._cs_childpath = False
                max_streams = increment(max_streams)

            # Maintain the same CUDA stream in DFS order, add more when
            # possible.
            for e in state.dfs_edges(source_nodes):
                if hasattr(e.dst, '_cuda_stream'):
                    continue
                if hasattr(e.src, '_cuda_stream'):
                    c = e.src._cuda_stream
                    if e.src._cs_childpath == True:
github spcl / dace / dace / libraries / blas / nodes / transpose.py View on Github external
elif dtype == dace.float64:
            func = "domatcopy"
            alpha = "1.0"
        elif dtype == dace.complex64:
            func = "comatcopy"
            alpha = "dace::blas::BlasConstants::Get().Complex64Pone()"
        elif dtype == dace.complex128:
            func = "zomatcopy"
            alpha = "dace::blas::BlasConstants::Get().Complex128Pone()"
        else:
            raise ValueError("Unsupported type for MKL omatcopy extension: " +
                             str(dtype))
        _, _, (m, n) = _get_transpose_input(node, state, sdfg)
        code = ("mkl_{f}('R', 'T', {m}, {n}, {a}, _inp, "
                "{n}, _out, {m});").format(f=func, m=m, n=n, a=alpha)
        tasklet = dace.graph.nodes.Tasklet(node.name,
                                           node.in_connectors,
                                           node.out_connectors,
                                           code,
                                           language=dace.dtypes.Language.CPP)
        return tasklet