pub struct MlxDevice { /* private fields */ }Expand description
Wraps a Metal device and its command queue.
§Thread Safety
MlxDevice is Send + Sync — you can share it across threads. The
underlying Metal device and command queue are thread-safe on Apple Silicon.
Implementations§
Source§impl MlxDevice
impl MlxDevice
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Initialize the Metal GPU device and create a command queue.
Returns Err(MlxError::DeviceNotFound) if no Metal device is available
(e.g. running on a non-Apple-Silicon machine or in a headless Linux VM).
Sourcepub fn command_encoder(&self) -> Result<CommandEncoder>
pub fn command_encoder(&self) -> Result<CommandEncoder>
Create a CommandEncoder for batching GPU dispatches.
The encoder wraps a fresh Metal command buffer from the device’s command
queue. Encode one or more kernel dispatches, then call
CommandEncoder::commit_and_wait to submit and block until completion.
Sourcepub fn alloc_buffer(
&self,
byte_len: usize,
dtype: DType,
shape: Vec<usize>,
) -> Result<MlxBuffer>
pub fn alloc_buffer( &self, byte_len: usize, dtype: DType, shape: Vec<usize>, ) -> Result<MlxBuffer>
Allocate a new GPU buffer with StorageModeShared.
§Arguments
byte_len— Size of the buffer in bytes. Must be > 0.dtype— Element data type for metadata tracking.shape— Tensor dimensions for metadata tracking.
§Errors
Returns MlxError::InvalidArgument if byte_len is zero.
Returns MlxError::BufferAllocationError if Metal cannot allocate.
Sourcepub fn metal_device(&self) -> &DeviceRef
pub fn metal_device(&self) -> &DeviceRef
Borrow the underlying metal::Device for direct Metal API calls
(e.g. kernel compilation in KernelRegistry).
Sourcepub fn metal_queue(&self) -> &CommandQueue
pub fn metal_queue(&self) -> &CommandQueue
Borrow the underlying metal::CommandQueue.