Skip to main content

NDArrayMatrixMultiplication

Struct NDArrayMatrixMultiplication 

Source
pub struct NDArrayMatrixMultiplication { /* private fields */ }

Implementations§

Source§

impl NDArrayMatrixMultiplication

Source

pub const fn as_ptr(&self) -> *mut c_void

Source§

impl NDArrayMatrixMultiplication

Source

pub fn new(device: &MetalDevice, source_count: usize) -> Option<Self>

Examples found in repository?
examples/06_ndarray_matrix_multiplication.rs (line 51)
26fn main() {
27    let device = MetalDevice::system_default().expect("no Metal device available");
28    let queue = device.new_command_queue().expect("command queue");
29
30    let descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2, 1, 1]).expect("descriptor");
31    let template = NDArray::new(&device, &descriptor).expect("template ndarray");
32    let byte_len = template.resource_size();
33    let rows = descriptor.length_of_dimension(1);
34    let row_stride_floats = byte_len / core::mem::size_of::<f32>() / rows;
35    let left_buffer = buffer_with_f32_values_padded(
36        &device,
37        &[1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0],
38        byte_len,
39    );
40    let right_buffer = buffer_with_f32_values_padded(
41        &device,
42        &[5.0, 6.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0],
43        byte_len,
44    );
45    let destination_buffer = buffer_with_f32_values_padded(&device, &[0.0; 8], byte_len);
46
47    let left = NDArray::new_with_buffer(&left_buffer, 0, &descriptor).expect("left ndarray");
48    let right = NDArray::new_with_buffer(&right_buffer, 0, &descriptor).expect("right ndarray");
49    let destination = NDArray::new_with_buffer(&destination_buffer, 0, &descriptor).expect("destination ndarray");
50
51    let kernel = NDArrayMatrixMultiplication::new(&device, 2).expect("ndarray matmul");
52    kernel.set_alpha(1.0);
53    kernel.set_beta(0.0);
54
55    let command_buffer = queue.new_command_buffer().expect("command buffer");
56    kernel.encode_to_destination(&command_buffer, &[&left, &right], &destination);
57    command_buffer.commit();
58    command_buffer.wait_until_completed();
59
60    let padded_output = read_f32_values(&destination_buffer, row_stride_floats * rows);
61    let output = [
62        padded_output[0],
63        padded_output[1],
64        padded_output[row_stride_floats],
65        padded_output[row_stride_floats + 1],
66    ];
67    println!("{output:?}");
68}
Source

pub fn alpha(&self) -> f64

Source

pub fn set_alpha(&self, alpha: f64)

Examples found in repository?
examples/06_ndarray_matrix_multiplication.rs (line 52)
26fn main() {
27    let device = MetalDevice::system_default().expect("no Metal device available");
28    let queue = device.new_command_queue().expect("command queue");
29
30    let descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2, 1, 1]).expect("descriptor");
31    let template = NDArray::new(&device, &descriptor).expect("template ndarray");
32    let byte_len = template.resource_size();
33    let rows = descriptor.length_of_dimension(1);
34    let row_stride_floats = byte_len / core::mem::size_of::<f32>() / rows;
35    let left_buffer = buffer_with_f32_values_padded(
36        &device,
37        &[1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0],
38        byte_len,
39    );
40    let right_buffer = buffer_with_f32_values_padded(
41        &device,
42        &[5.0, 6.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0],
43        byte_len,
44    );
45    let destination_buffer = buffer_with_f32_values_padded(&device, &[0.0; 8], byte_len);
46
47    let left = NDArray::new_with_buffer(&left_buffer, 0, &descriptor).expect("left ndarray");
48    let right = NDArray::new_with_buffer(&right_buffer, 0, &descriptor).expect("right ndarray");
49    let destination = NDArray::new_with_buffer(&destination_buffer, 0, &descriptor).expect("destination ndarray");
50
51    let kernel = NDArrayMatrixMultiplication::new(&device, 2).expect("ndarray matmul");
52    kernel.set_alpha(1.0);
53    kernel.set_beta(0.0);
54
55    let command_buffer = queue.new_command_buffer().expect("command buffer");
56    kernel.encode_to_destination(&command_buffer, &[&left, &right], &destination);
57    command_buffer.commit();
58    command_buffer.wait_until_completed();
59
60    let padded_output = read_f32_values(&destination_buffer, row_stride_floats * rows);
61    let output = [
62        padded_output[0],
63        padded_output[1],
64        padded_output[row_stride_floats],
65        padded_output[row_stride_floats + 1],
66    ];
67    println!("{output:?}");
68}
Source

pub fn beta(&self) -> f64

Source

pub fn set_beta(&self, beta: f64)

Examples found in repository?
examples/06_ndarray_matrix_multiplication.rs (line 53)
26fn main() {
27    let device = MetalDevice::system_default().expect("no Metal device available");
28    let queue = device.new_command_queue().expect("command queue");
29
30    let descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2, 1, 1]).expect("descriptor");
31    let template = NDArray::new(&device, &descriptor).expect("template ndarray");
32    let byte_len = template.resource_size();
33    let rows = descriptor.length_of_dimension(1);
34    let row_stride_floats = byte_len / core::mem::size_of::<f32>() / rows;
35    let left_buffer = buffer_with_f32_values_padded(
36        &device,
37        &[1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0],
38        byte_len,
39    );
40    let right_buffer = buffer_with_f32_values_padded(
41        &device,
42        &[5.0, 6.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0],
43        byte_len,
44    );
45    let destination_buffer = buffer_with_f32_values_padded(&device, &[0.0; 8], byte_len);
46
47    let left = NDArray::new_with_buffer(&left_buffer, 0, &descriptor).expect("left ndarray");
48    let right = NDArray::new_with_buffer(&right_buffer, 0, &descriptor).expect("right ndarray");
49    let destination = NDArray::new_with_buffer(&destination_buffer, 0, &descriptor).expect("destination ndarray");
50
51    let kernel = NDArrayMatrixMultiplication::new(&device, 2).expect("ndarray matmul");
52    kernel.set_alpha(1.0);
53    kernel.set_beta(0.0);
54
55    let command_buffer = queue.new_command_buffer().expect("command buffer");
56    kernel.encode_to_destination(&command_buffer, &[&left, &right], &destination);
57    command_buffer.commit();
58    command_buffer.wait_until_completed();
59
60    let padded_output = read_f32_values(&destination_buffer, row_stride_floats * rows);
61    let output = [
62        padded_output[0],
63        padded_output[1],
64        padded_output[row_stride_floats],
65        padded_output[row_stride_floats + 1],
66    ];
67    println!("{output:?}");
68}
Source

pub fn encode( &self, command_buffer: &MetalCommandBuffer, source_arrays: &[&NDArray], ) -> Option<NDArray>

Source

pub fn encode_to_destination( &self, command_buffer: &MetalCommandBuffer, source_arrays: &[&NDArray], destination: &NDArray, )

Examples found in repository?
examples/06_ndarray_matrix_multiplication.rs (line 56)
26fn main() {
27    let device = MetalDevice::system_default().expect("no Metal device available");
28    let queue = device.new_command_queue().expect("command queue");
29
30    let descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2, 1, 1]).expect("descriptor");
31    let template = NDArray::new(&device, &descriptor).expect("template ndarray");
32    let byte_len = template.resource_size();
33    let rows = descriptor.length_of_dimension(1);
34    let row_stride_floats = byte_len / core::mem::size_of::<f32>() / rows;
35    let left_buffer = buffer_with_f32_values_padded(
36        &device,
37        &[1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0],
38        byte_len,
39    );
40    let right_buffer = buffer_with_f32_values_padded(
41        &device,
42        &[5.0, 6.0, 0.0, 0.0, 7.0, 8.0, 0.0, 0.0],
43        byte_len,
44    );
45    let destination_buffer = buffer_with_f32_values_padded(&device, &[0.0; 8], byte_len);
46
47    let left = NDArray::new_with_buffer(&left_buffer, 0, &descriptor).expect("left ndarray");
48    let right = NDArray::new_with_buffer(&right_buffer, 0, &descriptor).expect("right ndarray");
49    let destination = NDArray::new_with_buffer(&destination_buffer, 0, &descriptor).expect("destination ndarray");
50
51    let kernel = NDArrayMatrixMultiplication::new(&device, 2).expect("ndarray matmul");
52    kernel.set_alpha(1.0);
53    kernel.set_beta(0.0);
54
55    let command_buffer = queue.new_command_buffer().expect("command buffer");
56    kernel.encode_to_destination(&command_buffer, &[&left, &right], &destination);
57    command_buffer.commit();
58    command_buffer.wait_until_completed();
59
60    let padded_output = read_f32_values(&destination_buffer, row_stride_floats * rows);
61    let output = [
62        padded_output[0],
63        padded_output[1],
64        padded_output[row_stride_floats],
65        padded_output[row_stride_floats + 1],
66    ];
67    println!("{output:?}");
68}

Trait Implementations§

Source§

impl Drop for NDArrayMatrixMultiplication

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for NDArrayMatrixMultiplication

Source§

impl Sync for NDArrayMatrixMultiplication

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.