Mojo struct
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 (
UnsafePointer[SIMD[type, 1], address_space, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): 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: UnsafePointer[SIMD[type, 1], address_space, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1])
Constructs a Buffer with statically known size and type.
Constraints:
The size is known.
Args:
- βptr (
UnsafePointer[SIMD[type, 1], address_space, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): Pointer to the data.
__init__(inout self: Self, ptr: UnsafePointer[SIMD[type, 1], address_space, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1], in_size: Int)
Constructs a Buffer with statically known type.
Constraints:
The size is unknown.
Args:
- βptr (
UnsafePointer[SIMD[type, 1], address_space, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): 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 = 1, alignment: Int = _default_alignment[::Int]()](self: Self, idx: Int) -> SIMD[type, $0]
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 = 1, alignment: Int = _default_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.
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.
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.
Args:
- βpath (
Path
): Path to the output file.
stack_allocation
β
static stack_allocation[*, alignment: Int = alignof[::DType,__mlir_type.!kgen.target]()]() -> Self
Constructs a buffer instance backed by stack allocated memory space.
Parameters:
- βalignment (
Int
): Address alignment requirement for the allocation.
Returns:
Constructed buffer with the allocated space.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?