Ejemplo n.º 1
0
    def make_vertices(self, model, n_steps):
        """Make vertices for the filter."""
        # Get the complete matrix to be applied by the filter
        out_signals = model.get_signals_from_object(self)

        # Get the filter and filter routing regions
        filter_region, filter_routing_region = make_filter_regions(
            model.get_signals_to_object(self)[InputPort.standard],
            model.dt,
            True,
            model.keyspaces.filter_routing_tag,
            width=self.size_in)
        self._routing_region = filter_routing_region

        # Generate the vertices
        vertices = flatinsertionlist()

        for group in self.groups:
            vertices.append(
                group.make_vertices(out_signals, model.machine_timestep,
                                    filter_region, filter_routing_region))

        # Return the netlist specification
        return netlistspec(vertices=vertices,
                           load_function=self.load_to_machine)
Ejemplo n.º 2
0
    def make_vertices(self, model, n_steps):
        """Make vertices for the filter."""
        # Get the complete matrix to be applied by the filter
        out_signals = model.get_signals_from_object(self)

        # Get the filter and filter routing regions
        filter_region, filter_routing_region = make_filter_regions(
            model.get_signals_to_object(self)[InputPort.standard],
            model.dt, True,
            model.keyspaces.filter_routing_tag,
            width=self.size_in
        )
        self._routing_region = filter_routing_region

        # Generate the vertices
        vertices = flatinsertionlist()

        for group in self.groups:
            vertices.append(
                group.make_vertices(out_signals,
                                    model.machine_timestep,
                                    filter_region,
                                    filter_routing_region)
            )

        # Return the netlist specification
        return netlistspec(vertices=vertices,
                           load_function=self.load_to_machine)
Ejemplo n.º 3
0
def test_flatinsertionlist():
    """Test a list which will always flatten the items it inserts."""
    fil = nscollections.flatinsertionlist()

    # We can append items normally
    fil.append(123)
    assert fil == [123]

    # Unless they're lists
    fil.append([1, 2, 3])
    assert fil == [123, 1, 2, 3]
Ejemplo n.º 4
0
def test_flatinsertionlist():
    """Test a list which will always flatten the items it inserts."""
    fil = nscollections.flatinsertionlist()

    # We can append items normally
    fil.append(123)
    assert fil == [123]

    # Unless they're lists
    fil.append([1, 2, 3])
    assert fil == [123, 1, 2, 3]
Ejemplo n.º 5
0
    def make_netlist(self, *args, **kwargs):
        """Convert the model into a netlist for simulating on SpiNNaker.

        Returns
        -------
        :py:class:`~nengo_spinnaker.netlist.Netlist`
            A netlist which can be placed and routed to simulate this model on
            a SpiNNaker machine.
        """
        # Remove any passthrough Nodes which don't connect to anything
        from nengo_spinnaker import operators
        removed_operators = model.remove_sinkless_objects(
            self.connection_map, operators.Filter)

        # Call each operator to make vertices
        operator_vertices = dict()
        load_functions = collections_ext.noneignoringlist()
        before_simulation_functions = collections_ext.noneignoringlist()
        after_simulation_functions = collections_ext.noneignoringlist()
        constraints = collections_ext.flatinsertionlist()

        # Prepare to build a list of signal constraints
        id_constraints = collections.defaultdict(set)

        for op in itertools.chain(itervalues(self.object_operators),
                                  self.extra_operators):
            # Skip any operators that were previously removed
            if op in removed_operators:
                continue

            # Otherwise call upon the operator to build vertices for the
            # netlist. The vertices should always be returned as an iterable.
            vxs, load_fn, pre_fn, post_fn, constraint = op.make_vertices(
                self, *args, **kwargs)
            operator_vertices[op] = tuple(vxs)

            load_functions.append(load_fn)
            before_simulation_functions.append(pre_fn)
            after_simulation_functions.append(post_fn)

            if constraint is not None:
                constraints.append(constraint)

            # Get the constraints on signal identifiers
            if hasattr(op, "get_signal_constraints"):
                # Ask the operator what constraints exist upon the keys it can
                # accept.
                for u, vs in iteritems(op.get_signal_constraints()):
                    id_constraints[u].update(vs)
            else:
                # Otherwise assume that all signals arriving at the operator
                # must be uniquely identified.
                incoming_all = itertools.chain(
                    *itervalues(self.connection_map.get_signals_to_object(op)))
                for (u, _), (v, _) in itertools.combinations(incoming_all, 2):
                    if u != v:
                        id_constraints[u].add(v)
                        id_constraints[v].add(u)

        # Construct nets from the signals
        nets = dict()
        id_to_signal = dict()
        for signal, transmission_parameters in \
                self.connection_map.get_signals():
            # Get the source and sink vertices
            original_sources = operator_vertices[signal.source]
            if not isinstance(original_sources, collections.Iterable):
                original_sources = (original_sources, )

            # Filter out any sources which have an `accepts_signal` method and
            # return False when this is called with the signal and transmission
            # parameters.
            sources = list()
            for source in original_sources:
                # For each source which either doesn't have a
                # `transmits_signal` method or returns True when this is called
                # with the signal and transmission parameters add a new net to
                # the netlist.
                if (hasattr(source, "transmits_signal")
                        and not source.transmits_signal(
                            signal, transmission_parameters)):
                    pass  # This source is ignored
                else:
                    # Add the source to the final list of sources
                    sources.append(source)

            sinks = collections_ext.flatinsertionlist()
            for sink in signal.sinks:
                # Get all the sink vertices
                sink_vertices = operator_vertices[sink]
                if not isinstance(sink_vertices, collections.Iterable):
                    sink_vertices = (sink_vertices, )

                # Include any sinks which either don't have an `accepts_signal`
                # method or return true when this is called with the signal and
                # transmission parameters.
                sinks.append(
                    s for s in sink_vertices
                    if not hasattr(s, "accepts_signal")
                    or s.accepts_signal(signal, transmission_parameters))

            # Create the net(s)
            id_to_signal[id(signal._params)] = signal  # Yuck
            nets[signal] = NMNet(sources, list(sinks), signal.weight)

        # Get the constraints on the signal identifiers
        signal_id_constraints = dict()
        for u, vs in iteritems(id_constraints):
            signal_id_constraints[id_to_signal[u]] = {
                id_to_signal[v]
                for v in vs
            }

        # Return a netlist
        return Netlist(nets=nets,
                       operator_vertices=operator_vertices,
                       keyspaces=self.keyspaces,
                       constraints=constraints,
                       load_functions=load_functions,
                       before_simulation_functions=before_simulation_functions,
                       after_simulation_functions=after_simulation_functions,
                       signal_id_constraints=signal_id_constraints)
Ejemplo n.º 6
0
    def make_netlist(self, *args, **kwargs):
        """Convert the model into a netlist for simulating on SpiNNaker.

        Returns
        -------
        :py:class:`~nengo_spinnaker.netlist.Netlist`
            A netlist which can be placed and routed to simulate this model on
            a SpiNNaker machine.
        """
        # Call each operator to make vertices
        operator_vertices = dict()
        vertices = collections_ext.flatinsertionlist()
        load_functions = collections_ext.noneignoringlist()
        before_simulation_functions = collections_ext.noneignoringlist()
        after_simulation_functions = collections_ext.noneignoringlist()

        for op in itertools.chain(itervalues(self.object_operators),
                                  self.extra_operators):
            vxs, load_fn, pre_fn, post_fn = op.make_vertices(
                self, *args, **kwargs
            )

            operator_vertices[op] = vxs
            vertices.append(vxs)

            load_functions.append(load_fn)
            before_simulation_functions.append(pre_fn)
            after_simulation_functions.append(post_fn)

        # Construct the groups set
        groups = list()
        for vxs in itervalues(operator_vertices):
            # If multiple vertices were provided by an operator then we add
            # them as a new group.
            if isinstance(vxs, collections.Iterable):
                groups.append(set(vxs))

        # Construct nets from the signals
        nets = list()
        for signal in itertools.chain(itervalues(self.connections_signals),
                                      self.extra_signals):
            # Get the source and sink vertices
            sources = operator_vertices[signal.source.obj]
            if not isinstance(sources, collections.Iterable):
                sources = (sources, )

            sinks = collections_ext.flatinsertionlist()
            for sink in signal.sinks:
                sinks.append(operator_vertices[sink.obj])

            # Create the net(s)
            for source in sources:
                nets.append(Net(source, list(sinks),
                            signal.weight, signal.keyspace))

        # Return a netlist
        return Netlist(
            nets=nets,
            vertices=vertices,
            keyspaces=self.keyspaces,
            groups=groups,
            load_functions=load_functions,
            before_simulation_functions=before_simulation_functions,
            after_simulation_functions=after_simulation_functions
        )
Ejemplo n.º 7
0
    def make_netlist(self, *args, **kwargs):
        """Convert the model into a netlist for simulating on SpiNNaker.

        Returns
        -------
        :py:class:`~nengo_spinnaker.netlist.Netlist`
            A netlist which can be placed and routed to simulate this model on
            a SpiNNaker machine.
        """
        # Remove any passthrough Nodes which don't connect to anything
        from nengo_spinnaker import operators
        removed_operators = model.remove_sinkless_objects(self.connection_map,
                                                          operators.Filter)

        # Apply the default keyspace to any signals without keyspaces
        self.connection_map.add_default_keyspace(self.keyspaces["nengo"])

        # Call each operator to make vertices
        operator_vertices = dict()
        vertices = collections_ext.flatinsertionlist()
        load_functions = collections_ext.noneignoringlist()
        before_simulation_functions = collections_ext.noneignoringlist()
        after_simulation_functions = collections_ext.noneignoringlist()
        constraints = collections_ext.flatinsertionlist()

        for op in itertools.chain(itervalues(self.object_operators),
                                  self.extra_operators):
            # Skip any operators that were previously removed
            if op in removed_operators:
                continue

            # Otherwise call upon the operator to build vertices for the
            # netlist.
            vxs, load_fn, pre_fn, post_fn, constraint = op.make_vertices(
                self, *args, **kwargs
            )

            operator_vertices[op] = vxs
            vertices.append(vxs)

            load_functions.append(load_fn)
            before_simulation_functions.append(pre_fn)
            after_simulation_functions.append(post_fn)

            if constraint is not None:
                constraints.append(constraint)

        # Construct the groups set
        groups = list()
        for vxs in itervalues(operator_vertices):
            # If multiple vertices were provided by an operator then we add
            # them as a new group.
            if isinstance(vxs, collections.Iterable):
                groups.append(set(vxs))

        # Construct nets from the signals
        nets = list()
        for signal, transmission_parameters in \
                self.connection_map.get_signals():
            # Get the source and sink vertices
            original_sources = operator_vertices[signal.source]
            if not isinstance(original_sources, collections.Iterable):
                original_sources = (original_sources, )

            # Filter out any sources which have an `accepts_signal` method and
            # return False when this is called with the signal and transmission
            # parameters.
            sources = list()
            for source in original_sources:
                # For each source which either doesn't have a
                # `transmits_signal` method or returns True when this is called
                # with the signal and transmission parameters add a new net to
                # the netlist.
                if (hasattr(source, "transmits_signal") and not
                        source.transmits_signal(signal,
                                                transmission_parameters)):
                    pass  # This source is ignored
                else:
                    # Add the source to the final list of sources
                    sources.append(source)

            sinks = collections_ext.flatinsertionlist()
            for sink in signal.sinks:
                # Get all the sink vertices
                sink_vertices = operator_vertices[sink]
                if not isinstance(sink_vertices, collections.Iterable):
                    sink_vertices = (sink_vertices, )

                # Include any sinks which either don't have an `accepts_signal`
                # method or return true when this is called with the signal and
                # transmission parameters.
                sinks.append(s for s in sink_vertices if
                             not hasattr(s, "accepts_signal") or
                             s.accepts_signal(signal, transmission_parameters))

            # Create the net(s)
            nets.append(NMNet(sources, list(sinks),
                              signal.weight, signal.keyspace))

        # Return a netlist
        return Netlist(
            nets=nets,
            vertices=vertices,
            keyspaces=self.keyspaces,
            groups=groups,
            constraints=constraints,
            load_functions=load_functions,
            before_simulation_functions=before_simulation_functions,
            after_simulation_functions=after_simulation_functions
        )
Ejemplo n.º 8
0
    def make_netlist(self, *args, **kwargs):
        """Convert the model into a netlist for simulating on SpiNNaker.

        Returns
        -------
        :py:class:`~nengo_spinnaker.netlist.Netlist`
            A netlist which can be placed and routed to simulate this model on
            a SpiNNaker machine.
        """
        # Call each operator to make vertices
        operator_vertices = dict()
        load_functions = collections_ext.noneignoringlist()
        before_simulation_functions = collections_ext.noneignoringlist()
        after_simulation_functions = collections_ext.noneignoringlist()
        constraints = collections_ext.flatinsertionlist()

        # Prepare to build a list of signal constraints
        id_constraints = collections.defaultdict(set)

        for op in itertools.chain(itervalues(self.object_operators),
                                  self.extra_operators):
            # If the operator is a passthrough Node then skip it
            if isinstance(op, model.PassthroughNode):
                continue

            # Otherwise call upon the operator to build vertices for the
            # netlist. The vertices should always be returned as an iterable.
            vxs, load_fn, pre_fn, post_fn, constraint = op.make_vertices(
                self, *args, **kwargs
            )
            operator_vertices[op] = tuple(vxs)

            load_functions.append(load_fn)
            before_simulation_functions.append(pre_fn)
            after_simulation_functions.append(post_fn)

            if constraint is not None:
                constraints.append(constraint)

            # Get the constraints on signal identifiers
            if hasattr(op, "get_signal_constraints"):
                # Ask the operator what constraints exist upon the keys it can
                # accept.
                for u, vs in iteritems(op.get_signal_constraints()):
                    id_constraints[u].update(vs)
            else:
                # Otherwise assume that all signals arriving at the operator
                # must be uniquely identified.
                incoming_all = itertools.chain(*itervalues(
                    self.get_signals_to_object(op)))
                for (u, _), (v, _) in itertools.combinations(incoming_all, 2):
                    if u != v:
                        id_constraints[u].add(v)
                        id_constraints[v].add(u)

        # Construct nets from the signals
        nets = dict()
        id_to_signal = dict()
        for signal, transmission_parameters in \
                self.connection_map.get_signals():
            # Get the source and sink vertices
            original_sources = operator_vertices[signal.source]
            if not isinstance(original_sources, collections.Iterable):
                original_sources = (original_sources, )

            # Filter out any sources which have an `accepts_signal` method and
            # return False when this is called with the signal and transmission
            # parameters.
            sources = list()
            for source in original_sources:
                # For each source which either doesn't have a
                # `transmits_signal` method or returns True when this is called
                # with the signal and transmission parameters add a new net to
                # the netlist.
                if (hasattr(source, "transmits_signal") and not
                        source.transmits_signal(signal,
                                                transmission_parameters)):
                    pass  # This source is ignored
                else:
                    # Add the source to the final list of sources
                    sources.append(source)

            sinks = collections.deque()
            for sink in signal.sinks:
                # Get all the sink vertices
                sink_vertices = operator_vertices[sink]
                if not isinstance(sink_vertices, collections.Iterable):
                    sink_vertices = (sink_vertices, )

                # Include any sinks which either don't have an `accepts_signal`
                # method or return true when this is called with the signal and
                # transmission parameters.
                sinks.extend(s for s in sink_vertices if
                             not hasattr(s, "accepts_signal") or
                             s.accepts_signal(signal, transmission_parameters))

            # Create the net(s)
            id_to_signal[id(signal._params)] = signal  # Yuck
            nets[signal] = NMNet(sources, list(sinks), signal.weight)

        # Get the constraints on the signal identifiers
        signal_id_constraints = dict()
        for u, vs in iteritems(id_constraints):
            signal_id_constraints[id_to_signal[u]] = {
                id_to_signal[v] for v in vs
            }

        # Return a netlist
        return Netlist(
            nets=nets,
            operator_vertices=operator_vertices,
            keyspaces=self.keyspaces,
            constraints=constraints,
            load_functions=load_functions,
            before_simulation_functions=before_simulation_functions,
            after_simulation_functions=after_simulation_functions,
            signal_id_constraints=signal_id_constraints
        )