Ejemplo n.º 1
0
    def _clean_variables(self, variables):
        """ Sorts out variables for processing usage

        :param variables: list of variables names, or 'all', or single.
        :return: ordered set of variables strings.
        """
        # if variable is a base string, plonk into a array for ease of
        # conversion
        if isinstance(variables, string_types):
            variables = [variables]

        # if all are needed to be extracted, extract each and plonk into the
        # neo block segment. ensures whatever was in variables stays in
        # variables, regardless of all
        if 'all' in variables:
            variables = OrderedSet(variables)
            variables.remove('all')
            variables.update(self._get_all_recording_variables())
        return variables
Ejemplo n.º 2
0
def test_set_ness():
    o = OrderedSet()
    assert len(o) == 0
    o.add(123)
    assert len(o) == 1
    o.add(123)
    assert len(o) == 1
    o.add(456)
    assert len(o) == 2
    o.add(456)
    assert len(o) == 2
    o.add(123)
    assert len(o) == 2
    assert o == set([123, 456])
    assert o == set([456, 123])
    assert o == [123, 456]
    assert o == [456, 123]
    o.remove(123)
    assert len(o) == 1
    o.remove(456)
    assert len(o) == 0
    with pytest.raises(KeyError):  # @UndefinedVariable
        o.remove(789)
    o.discard(789)
    assert len(o) == 0
    o.add(789)
    assert len(o) == 1
    assert 789 in o
    o.discard(789)
    assert 789 not in o
    assert len(o) == 0
Ejemplo n.º 3
0
class CoreTracker(object):
    """ Represents the number of cores and sdram left to allocate
    """

    __slots__ = [

        # The number of cores available after preallocation
        "_n_cores",

        # cores available including ones needed for preallocation
        "_cores",

        # keep list of counts of the cores per n_cores_available
        "_cores_counter",
    ]

    def __init__(self, chip, preallocated_resources, cores_counter):
        """
        :param ~spinn_machine.Chip chip:
            chip whose resources can be allocated
        :param preallocated_resources:
        :type preallocated_resources: PreAllocatedResourceContainer or None
        """
        self._cores = OrderedSet()
        for processor in chip.processors:
            if not processor.is_monitor:
                self._cores.add(processor.processor_id)
        self._n_cores = len(self._cores)
        if preallocated_resources:
            if chip.ip_address:
                self._n_cores -= preallocated_resources.cores_ethernet
            else:
                self._n_cores -= preallocated_resources.cores_all
        if chip.virtual:
            self._cores_counter = None
        else:
            self._cores_counter = cores_counter
        if self._cores_counter:
            self._cores_counter[self._n_cores] += 1

    @property
    def n_cores_available(self):
        return self._n_cores

    def is_core_available(self, p):
        if p is None:
            return self.is_available
        else:
            return p in self._cores

    def available_core(self):
        return self._cores.peek()

    @property
    def is_available(self):
        return self._n_cores > 0

    def allocate(self, p):
        if p is None:
            p = self._cores.pop()
        else:
            self._cores.remove(p)
        if self._cores_counter:
            self._cores_counter[self._n_cores] -= 1
        self._n_cores -= 1
        if self._cores_counter:
            self._cores_counter[self._n_cores] += 1

        if self._n_cores <= 0:
            self._cores = OrderedSet()
        return p

    def deallocate(self, p):
        self._cores.add(p)
        if self._cores_counter:
            self._cores_counter[self._n_cores] -= 1
        self._n_cores += 1
        if self._cores_counter:
            self._cores_counter[self._n_cores] += 1