Mojo buffer的实现及接口描述

buffer

Implements the Buffer class.

You can import these APIs from the memory package. For example:

from buffer import Buffer

Buffer

Defines a Buffer which can be parametrized on a static size and Dtype.

The Buffer does not own its underlying pointer.

Parameters:

  • type (DType): The element type of the Buffer.
  • size (Dim): The static size (if known) of the Buffer.
  • address_space (AddressSpace): The address space of the Buffer.

Fields:

  • data (DTypePointer[type, address_space]): The underlying data pointer of the data.

  • dynamic_size (Int): The dynamic size of the buffer.

  • dtype (DType): The dynamic data type of the buffer.

Implemented traits:

AnyType, Copyable, Movable, Sized

Methods:

__init__

__init__(inout self: Self)

Default initializer for Buffer. By default the fields are all initialized to 0.

__init__(inout self: Self, ptr: Pointer[SIMD[type, 1], address_space])

Constructs a Buffer with statically known size and type.

Constraints:

The size is known.

Args:

  • ptr (Pointer[SIMD[type, 1], address_space]): Pointer to the data.

__init__(inout self: Self, ptr: DTypePointer[type, address_space])

Constructs a Buffer with statically known size and type.

Constraints:

The size is known.

Args:

  • ptr (DTypePointer[type, address_space]): Pointer to the data.

__init__(inout self: Self, ptr: Pointer[SIMD[type, 1], address_space], in_size: Int)

Constructs a Buffer with statically known type.

Constraints:

The size is unknown.

Args:

  • ptr (Pointer[SIMD[type, 1], address_space]): Pointer to the data.
  • in_size (Int): Dynamic size of the buffer.

__init__(inout self: Self, ptr: DTypePointer[type, address_space], in_size: Int)

Constructs a Buffer with statically known type.

Constraints:

The size is unknown.

Args:

  • ptr (DTypePointer[type, address_space]): Pointer to the data.
  • in_size (Int): Dynamic size of the buffer.

__getitem__

__getitem__(self: Self, idx: Int) -> SIMD[type, 1]

Loads a single element (SIMD of size 1) from the buffer at the specified index.

Args:

  • idx (Int): The index into the Buffer.

Returns:

The value at the idx position.

__setitem__

__setitem__(self: Self, idx: Int, val: scalar<#lit.struct.extract<:_stdlib::_builtin::_dtype::_DType type, "value">>)

Stores a single value into the buffer at the specified index.

Args:

  • idx (Int): The index into the Buffer.
  • val (scalar<#lit.struct.extract<:_stdlib::_builtin::_dtype::_DType type, "value">>): The value to store.

__setitem__(self: Self, idx: Int, val: SIMD[type, 1])

Stores a single value into the buffer at the specified index.

Args:

  • idx (Int): The index into the Buffer.
  • val (SIMD[type, 1]): The value to store.

__len__

__len__(self: Self) -> Int

Gets the size if it is a known constant, otherwise it gets the dynamic_size.

This method is used by Buffer.__len__ to get the size of the buffer. If the Buffer size is a known constant, then the size is returned. Otherwise, the dynamic_size is returned.

Returns:

The size if static otherwise dynamic_size.

load

load[width: Int, alignment: Int](self: Self, idx: Int) -> SIMD[type, width]

Loads a simd value from the buffer at the specified index.

Parameters:

  • width (Int): The simd_width of the load.
  • alignment (Int): The alignment value.

Args:

  • idx (Int): The index into the Buffer.

Returns:

The simd value starting at the idx position and ending at idx+width.

store

store[width: Int, alignment: Int](self: Self, idx: Int, val: SIMD[type, width])

Stores a simd value into the buffer at the specified index.

Parameters:

  • width (Int): The width of the simd vector.
  • alignment (Int): The alignment value.

Args:

  • idx (Int): The index into the Buffer.
  • val (SIMD[type, width]): The value to store.

simd_nt_store

simd_nt_store[width: Int](self: Self, idx: Int, val: SIMD[type, width])

Stores a simd value using non-temporal store.

Constraints:

The address must be properly aligned, 64B for avx512, 32B for avx2, and 16B for avx.

Parameters:

  • width (Int): The width of the simd vector.

Args:

  • idx (Int): The index into the Buffer.
  • val (SIMD[type, width]): The value to store.

prefetch

prefetch[params: PrefetchOptions](self: Self, idx: Int)

Prefetches the data at the given index.

Parameters:

  • params (PrefetchOptions): The prefetch configuration.

Args:

  • idx (Int): The index of the prefetched location.

bytecount

bytecount(self: Self) -> Int

Returns the size of the Buffer in bytes.

Returns:

The size of the Buffer in bytes.

Mojo中文网:mojocn.org Mojo Dev社区:mojoo.org

zero

zero(self: Self)

Sets all bytes of the Buffer to 0.

fill

fill(self: Self, val: SIMD[type, 1])

Assigns val to all elements in the Buffer.

The fill is performed in chunks of size N, where N is the native SIMD width of type on the system.

Args:

  • val (SIMD[type, 1]): The value to store.

tofile

tofile(self: Self, path: Path)

Write values to a file

  • 11
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启航学途

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值