コード例 #1
0
# A Storage can have zero or more views

s = Storage(n=10, kind="bool")  # 10 elements
s = Storage(n=100, kind="int64")
s = Storage(n=1000, kind="float64")
s = Storage(n=10000, kind="string")
s = Storage([0, 1, 1, 0], kind="bool")  # data provided
s = Storage(place="memory")
s = Storage(place="GPU")
s = Storage(place=("disk", "path/to/file"))

# There will be ufunc equivalents that apply to Storage

# Vectors are 1D views of parts of or all of a Storage

v = Vector(storage=s)  # all elements, same len as underlying storage
v = Vector(storage=s, shape=[8])  # first 8 elements
v = Vector(storage=s, shape=[5], offsets=[0], strides=[2])  # every other
r = add(v, v)  # ufunc-like capability, result is new view of new storage
v2 = v.deepcopy().make_contiguous()  # new storage, view had stride = 1
add(v2, v2)      # potentially faster than operating on v

# Illustration: what offsets allow
def first_delta(v):
    # require v to have an extra element at the end of its storage
    w = Vector(storage=v.storage, shape=v.shape, offset=1)
    return v - w

# indexing returns same shape as the indexer
x = v[3]       # x is a python scalar
x = v[[0, 3]]  # x is a Vector, a new Storage
コード例 #2
0
# Storage holds the actual data
# Vectors, Matrices, and Tensors are views of Storage
# A Storage can have zero or more views

s = Storage(n=10, kind="bool")  # 10 elements
s = Storage(n=100, kind="int64")
s = Storage(n=1000, kind="float64")
s = Storage(n=10000, kind="string")
s = Storage([0, 1, 1, 0], kind="bool")  # data provided
s = Storage(place="memory")
s = Storage(place="GPU")
s = Storage(place=("disk", "path/to/file"))

# Vectors are 1D views of parts of or all of a Storage

v = Vector(storage=s)  # all elements, same len as underlying storage
v = Vector(storage=s, shape=[8])  # first 8 elements
v = Vector(storage=s, shape=[5], offsets=[0], strides=[2])  # every other

# TODO: illustrate what offset adds to the capabilities

# indexing returns same shape as the indexer
x = v[3]       # x is a python scalar
x = v[[0, 3]]  # x is a Vector, a new Storage
x = v[Storage([0, 1, 0, 0, 1, 1, 0, 0, 0, 1], kind="bool")]  # masked selection

# Matrices are 2D views of parts of or all of a Storage
m = Matrix(storage=s, shape=[3, 4], offsets=[0, 0], strides=[1, 1])  # dense
x = m[:, 3]  # one column
x = m[2, :]  # one row
row_mask = Vector([0, 1])