pub struct NDArrayDescriptor { /* private fields */ }Implementations§
Source§impl NDArrayDescriptor
impl NDArrayDescriptor
Sourcepub fn with_dimension_sizes(
data_type: u32,
dimension_sizes: &[usize],
) -> Option<Self>
pub fn with_dimension_sizes( data_type: u32, dimension_sizes: &[usize], ) -> Option<Self>
Examples found in repository?
examples/06_ndarray_matrix_multiplication.rs (line 30)
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}More examples
examples/03_ndarray_identity.rs (line 9)
4fn main() {
5 let device = MetalDevice::system_default().expect("no Metal device available");
6 let queue = device.new_command_queue().expect("command queue");
7
8 let descriptor =
9 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4]).expect("descriptor");
10 assert_eq!(descriptor.number_of_dimensions(), 1);
11 assert_eq!(descriptor.length_of_dimension(0), 4);
12
13 descriptor.reshape_with_dimension_sizes(&[2, 2]);
14 assert_eq!(descriptor.number_of_dimensions(), 2);
15 assert_eq!(descriptor.length_of_dimension(0), 2);
16 assert_eq!(descriptor.length_of_dimension(1), 2);
17
18 let array_descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2])
19 .expect("array descriptor");
20 let array = NDArray::new(&device, &array_descriptor).expect("ndarray");
21 assert_eq!(array.number_of_dimensions(), 2);
22 assert_eq!(array.length_of_dimension(0), 2);
23 assert!(array.resource_size() > 0);
24
25 if let Some(identity) = NDArrayIdentity::new(&device) {
26 let destination_descriptor =
27 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4])
28 .expect("destination descriptor");
29 let destination =
30 NDArray::new(&device, &destination_descriptor).expect("destination ndarray");
31 let command_buffer = queue.new_command_buffer().expect("command buffer");
32 assert!(identity.reshape_into(Some(&command_buffer), &array, &[4], &destination));
33 command_buffer.commit();
34 command_buffer.wait_until_completed();
35 assert_eq!(destination.number_of_dimensions(), 1);
36 assert_eq!(destination.length_of_dimension(0), 4);
37 println!(
38 "ndarray smoke passed: resource_size={} dims={}x{}",
39 array.resource_size(),
40 array.length_of_dimension(0),
41 array.length_of_dimension(1)
42 );
43 } else {
44 println!("ndarray smoke skipped: MPSNDArrayIdentity unavailable on this macOS version");
45 }
46}pub fn data_type(&self) -> u32
pub fn set_data_type(&self, data_type: u32)
Sourcepub fn number_of_dimensions(&self) -> usize
pub fn number_of_dimensions(&self) -> usize
Examples found in repository?
examples/03_ndarray_identity.rs (line 10)
4fn main() {
5 let device = MetalDevice::system_default().expect("no Metal device available");
6 let queue = device.new_command_queue().expect("command queue");
7
8 let descriptor =
9 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4]).expect("descriptor");
10 assert_eq!(descriptor.number_of_dimensions(), 1);
11 assert_eq!(descriptor.length_of_dimension(0), 4);
12
13 descriptor.reshape_with_dimension_sizes(&[2, 2]);
14 assert_eq!(descriptor.number_of_dimensions(), 2);
15 assert_eq!(descriptor.length_of_dimension(0), 2);
16 assert_eq!(descriptor.length_of_dimension(1), 2);
17
18 let array_descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2])
19 .expect("array descriptor");
20 let array = NDArray::new(&device, &array_descriptor).expect("ndarray");
21 assert_eq!(array.number_of_dimensions(), 2);
22 assert_eq!(array.length_of_dimension(0), 2);
23 assert!(array.resource_size() > 0);
24
25 if let Some(identity) = NDArrayIdentity::new(&device) {
26 let destination_descriptor =
27 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4])
28 .expect("destination descriptor");
29 let destination =
30 NDArray::new(&device, &destination_descriptor).expect("destination ndarray");
31 let command_buffer = queue.new_command_buffer().expect("command buffer");
32 assert!(identity.reshape_into(Some(&command_buffer), &array, &[4], &destination));
33 command_buffer.commit();
34 command_buffer.wait_until_completed();
35 assert_eq!(destination.number_of_dimensions(), 1);
36 assert_eq!(destination.length_of_dimension(0), 4);
37 println!(
38 "ndarray smoke passed: resource_size={} dims={}x{}",
39 array.resource_size(),
40 array.length_of_dimension(0),
41 array.length_of_dimension(1)
42 );
43 } else {
44 println!("ndarray smoke skipped: MPSNDArrayIdentity unavailable on this macOS version");
45 }
46}pub fn set_number_of_dimensions(&self, number_of_dimensions: usize)
Sourcepub fn length_of_dimension(&self, dimension_index: usize) -> usize
pub fn length_of_dimension(&self, dimension_index: usize) -> usize
Examples found in repository?
examples/06_ndarray_matrix_multiplication.rs (line 33)
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}More examples
examples/03_ndarray_identity.rs (line 11)
4fn main() {
5 let device = MetalDevice::system_default().expect("no Metal device available");
6 let queue = device.new_command_queue().expect("command queue");
7
8 let descriptor =
9 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4]).expect("descriptor");
10 assert_eq!(descriptor.number_of_dimensions(), 1);
11 assert_eq!(descriptor.length_of_dimension(0), 4);
12
13 descriptor.reshape_with_dimension_sizes(&[2, 2]);
14 assert_eq!(descriptor.number_of_dimensions(), 2);
15 assert_eq!(descriptor.length_of_dimension(0), 2);
16 assert_eq!(descriptor.length_of_dimension(1), 2);
17
18 let array_descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2])
19 .expect("array descriptor");
20 let array = NDArray::new(&device, &array_descriptor).expect("ndarray");
21 assert_eq!(array.number_of_dimensions(), 2);
22 assert_eq!(array.length_of_dimension(0), 2);
23 assert!(array.resource_size() > 0);
24
25 if let Some(identity) = NDArrayIdentity::new(&device) {
26 let destination_descriptor =
27 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4])
28 .expect("destination descriptor");
29 let destination =
30 NDArray::new(&device, &destination_descriptor).expect("destination ndarray");
31 let command_buffer = queue.new_command_buffer().expect("command buffer");
32 assert!(identity.reshape_into(Some(&command_buffer), &array, &[4], &destination));
33 command_buffer.commit();
34 command_buffer.wait_until_completed();
35 assert_eq!(destination.number_of_dimensions(), 1);
36 assert_eq!(destination.length_of_dimension(0), 4);
37 println!(
38 "ndarray smoke passed: resource_size={} dims={}x{}",
39 array.resource_size(),
40 array.length_of_dimension(0),
41 array.length_of_dimension(1)
42 );
43 } else {
44 println!("ndarray smoke skipped: MPSNDArrayIdentity unavailable on this macOS version");
45 }
46}Sourcepub fn reshape_with_dimension_sizes(&self, dimension_sizes: &[usize])
pub fn reshape_with_dimension_sizes(&self, dimension_sizes: &[usize])
Examples found in repository?
examples/03_ndarray_identity.rs (line 13)
4fn main() {
5 let device = MetalDevice::system_default().expect("no Metal device available");
6 let queue = device.new_command_queue().expect("command queue");
7
8 let descriptor =
9 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4]).expect("descriptor");
10 assert_eq!(descriptor.number_of_dimensions(), 1);
11 assert_eq!(descriptor.length_of_dimension(0), 4);
12
13 descriptor.reshape_with_dimension_sizes(&[2, 2]);
14 assert_eq!(descriptor.number_of_dimensions(), 2);
15 assert_eq!(descriptor.length_of_dimension(0), 2);
16 assert_eq!(descriptor.length_of_dimension(1), 2);
17
18 let array_descriptor = NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[2, 2])
19 .expect("array descriptor");
20 let array = NDArray::new(&device, &array_descriptor).expect("ndarray");
21 assert_eq!(array.number_of_dimensions(), 2);
22 assert_eq!(array.length_of_dimension(0), 2);
23 assert!(array.resource_size() > 0);
24
25 if let Some(identity) = NDArrayIdentity::new(&device) {
26 let destination_descriptor =
27 NDArrayDescriptor::with_dimension_sizes(data_type::FLOAT32, &[4])
28 .expect("destination descriptor");
29 let destination =
30 NDArray::new(&device, &destination_descriptor).expect("destination ndarray");
31 let command_buffer = queue.new_command_buffer().expect("command buffer");
32 assert!(identity.reshape_into(Some(&command_buffer), &array, &[4], &destination));
33 command_buffer.commit();
34 command_buffer.wait_until_completed();
35 assert_eq!(destination.number_of_dimensions(), 1);
36 assert_eq!(destination.length_of_dimension(0), 4);
37 println!(
38 "ndarray smoke passed: resource_size={} dims={}x{}",
39 array.resource_size(),
40 array.length_of_dimension(0),
41 array.length_of_dimension(1)
42 );
43 } else {
44 println!("ndarray smoke skipped: MPSNDArrayIdentity unavailable on this macOS version");
45 }
46}pub fn transpose_dimension( &self, dimension_index: usize, other_dimension_index: usize, )
Trait Implementations§
Source§impl Drop for NDArrayDescriptor
impl Drop for NDArrayDescriptor
impl Send for NDArrayDescriptor
impl Sync for NDArrayDescriptor
Auto Trait Implementations§
impl Freeze for NDArrayDescriptor
impl RefUnwindSafe for NDArrayDescriptor
impl Unpin for NDArrayDescriptor
impl UnsafeUnpin for NDArrayDescriptor
impl UnwindSafe for NDArrayDescriptor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more