Пример #1
0
    def collect(self, size, top_of_stack):
        """
        Collect garbage by copying all live data to tospace, starting with
        `roots`. If there is no or little garbage and we're running out of heap
        space, grow the heap. `size` indicates the size of the requested memory.

        This method may raise MemoryError.

        For every root, we determine the trace function and call it on the root.
        The trace function traces the object itself (since only it knows its
        size), and traces its children:

            def __flypy_trace__(self, gc):
                obj = gc.trace((int8 *) self, sizeof(self))
                obj.field1 = obj.field1.trace(gc)
                ...
                obj.fieldN = obj.fieldN.trace(gc)
                return obj
        """
        # Copy all live data
        for item in roots.find_roots(top_of_stack):
            obj = head(item)
            trace = head(tail(item))
            trace(obj)

        # Swap spaces and adjust heap if necessary
        self.fromspace, self.tospace = self.tospace, self.fromspace
        if self.resize_heap(size):
            self.init_forwarding_table()
        else:
            self.forwarding_table.clear()
Пример #2
0
def add(a, b):
    arrays = broadcast(a, b)
    a = head(arrays)
    b = head(tail(arrays))
    out = np.empty_like(a, unify(a.dtype, b.dtype))
    _add(a, b, out)
    return out
Пример #3
0
def add(a, b):
    arrays = broadcast(a, b)
    a = head(arrays)
    b = head(tail(arrays))
    out = np.empty_like(a, unify(a.dtype, b.dtype))
    _add(a, b, out)
    return out
Пример #4
0
    def collect(self, size, top_of_stack):
        """
        Collect garbage by copying all live data to tospace, starting with
        `roots`. If there is no or little garbage and we're running out of heap
        space, grow the heap. `size` indicates the size of the requested memory.

        This method may raise MemoryError.

        For every root, we determine the trace function and call it on the root.
        The trace function traces the object itself (since only it knows its
        size), and traces its children:

            def __flypy_trace__(self, gc):
                obj = gc.trace((int8 *) self, sizeof(self))
                obj.field1 = obj.field1.trace(gc)
                ...
                obj.fieldN = obj.fieldN.trace(gc)
                return obj
        """
        # Copy all live data
        for item in roots.find_roots(top_of_stack):
            obj = head(item)
            trace = head(tail(item))
            trace(obj)

        # Swap spaces and adjust heap if necessary
        self.fromspace, self.tospace = self.tospace, self.fromspace
        if self.resize_heap(size):
            self.init_forwarding_table()
        else:
            self.forwarding_table.clear()
Пример #5
0
def slice_dim(dim, p, indices, dtype):
    # TODO: wraparound
    s = head(indices)

    data = p
    extent = dim.extent
    stride = dim.stride

    t = normalize(s, extent)
    start = t[0]
    stop = t[1]
    step = t[2]  # TODO: tuple unpacking
    extent = len(xrange(start, stop, step))

    # Process start
    if s.start is not None:
        data += start * stride

    # Process step
    if s.step is not None:
        stride *= step

    array = dim.base.index(data, tail(indices), dtype)
    # TODO: Without 'step', ContigDim should remain a ContigDim
    dims = Dimension(array.dims, extent, stride)
    return NDArray(array.data, dims, dtype)
Пример #6
0
    def index(self, p, indices, dtype):
        idx = head(indices)
        if 0 <= idx < self.base.extent:
            return self.base.index(p, indices, dtype)

        # TODO: Exceptions
        print("Index out of bounds: index", end="")
        print(idx, end=", extent ")
        print(self.base.extent)
Пример #7
0
    def index(self, p, indices, dtype):
        idx = head(indices)
        if 0 <= idx < self.base.extent:
            return self.base.index(p, indices, dtype)

        # TODO: Exceptions
        print("Index out of bounds: index", end="")
        print(idx, end=", extent ")
        print(self.base.extent)
Пример #8
0
def slice_dim(dim, p, indices, dtype):
    # TODO: wraparound
    s = head(indices)

    data = p
    extent = dim.extent
    stride = dim.stride

    t = normalize(s, extent)
    start = t[0] ; stop = t[1] ; step = t[2] # TODO: tuple unpacking
    extent = len(range(start, stop, step))

    # Process start
    if s.start is not None:
        data += start * stride

    # Process step
    if s.step is not None:
        stride *= step

    array = dim.base.index(data, tail(indices), dtype)
    # TODO: Without 'step', ContigDim should remain a ContigDim
    dims = Dimension(array.dims, extent, stride)
    return NDArray(array.data, dims, dtype)
Пример #9
0
def c_layout_from_shape(shape, dtype):
    extent = head(shape)
    return Dimension(EmptyDim(), extent, 1) # TODO: ContigDim
Пример #10
0
def c_layout_from_shape(shape, dtype):
    """Construct dimensions for an array of shape `shape` with a C layout"""
    dim = c_layout_from_shape(tail(shape), dtype)
    extent = head(shape)
    stride = dim.stride * dim.extent
    return Dimension(dim, extent, stride)
Пример #11
0
 def index(self, p, indices, dtype):
     idx = head(indices)
     #if idx < 0 or idx > self.extent:
     #    print("Index out of bounds!")
     #    return self.base.index(p, tail(indices))
     return self.base.index(p + idx * self.stride, tail(indices), dtype)
Пример #12
0
 def index(self, p, indices, dtype):
     idx = head(indices)
     #if idx < 0 or idx > self.extent:
     #    print("Index out of bounds!")
     #    return self.base.index(p, tail(indices))
     return self.base.index(p + idx * self.stride, tail(indices), dtype)
Пример #13
0
def c_layout_from_shape(shape, dtype):
    extent = head(shape)
    return Dimension(EmptyDim(), extent, 1)  # TODO: ContigDim
Пример #14
0
def c_layout_from_shape(shape, dtype):
    """Construct dimensions for an array of shape `shape` with a C layout"""
    dim = c_layout_from_shape(tail(shape), dtype)
    extent = head(shape)
    stride = dim.stride * dim.extent
    return Dimension(dim, extent, stride)