Skip to main content
Log in

Mojo struct

MemoryElement

struct MemoryElement[dtype: DType, layout: Layout, address_space: AddressSpace, alignment: Int, /, *, bitwidth: Int = bitwidthof[AnyTrivialRegType,__mlir_type.!kgen.target]()]

Represents data in memory organized according to a specific layout.

The MemoryElement struct provides a high-level interface for accessing data in memory with a specific layout. It encapsulates a pointer to the memory location and the runtime layout information needed to access the data correctly.

This abstraction enables efficient memory operations that respect the underlying memory organization, supporting vectorized loads and stores while handling different memory layouts transparently.

Parameters

  • dtype (DType): The data type of the elements.
  • layout (Layout): The memory layout describing how elements are organized.
  • address_space (AddressSpace): The memory address space where the data is located.
  • alignment (Int): The memory alignment requirement for the data.
  • bitwidth (Int): The bit width used for runtime layout calculations.

Fields

  • ptr (UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment]): Pointer to the memory location where the data is stored.
  • runtime_layout (RuntimeLayout[layout, bitwidth=bitwidth]): Runtime layout information used for memory access calculations.

Implemented traits

AnyType, UnknownDestructibility

Methods

__init__

__init__(out self, ptr: UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment], runtime_layout: RuntimeLayout[layout, bitwidth=bitwidth])

Initializes a MemoryElement with the given pointer and runtime layout.

Args:

  • ptr (UnsafePointer[SIMD[dtype, 1], address_space=address_space, alignment=alignment]): Pointer to the memory location of the element.
  • runtime_layout (RuntimeLayout[layout, bitwidth=bitwidth]): The runtime layout to use for memory access.

load

load(self) -> Element[dtype, layout, bitwidth=bitwidth]

Loads data from memory according to the specified layout.

This method performs a layout-aware load operation, reading data from memory following the access patterns defined by the layout. It optimizes memory reads based on the layout's stride patterns to maximize performance.

The method leverages the underlying Element.load implementation which handles different memory layout patterns including contiguous and strided access.

Returns:

An Element containing the loaded data organized according to the layout.

store

store(self, src: Element[dtype, layout, bitwidth=bitwidth])

Stores element data to the memory location of this MemoryElement.

This method performs a layout-aware store operation, writing data to memory following the access patterns defined by the layout. It optimizes memory writes based on the layout's stride patterns to maximize performance.

The method delegates to the Element.store implementation which handles different memory layout patterns including vectorized stores for contiguous memory and element-by-element stores for non-contiguous layouts.

Args:

  • src (Element[dtype, layout, bitwidth=bitwidth]): The Element containing the data to store.

transfer

transfer(self, src: MemoryElement[dtype, layout, address_space, alignment, bitwidth=bitwidth])

Transfers data from another MemoryElement to this one.

This method efficiently transfers data between memory locations with potentially different layouts and data types. It performs the following operations:

  1. Loads data from the source MemoryElement using its layout
  2. Converts the data to the destination data type if necessary
  3. Stores the converted data to the destination memory location using its layout

This provides a high-performance way to copy and convert data between different memory representations while respecting both source and destination memory layouts.

Args:

  • src (MemoryElement[dtype, layout, address_space, alignment, bitwidth=bitwidth]): The source MemoryElement to transfer data from.