### Run library benchmarks Source: https://context7.com/bluss/matrixmultiply/llms.txt Commands to execute the included benchmark example with various configurations. ```bash # Basic benchmark: 1000×1000×1000 matrix multiplication cargo run --release --example benchmark 1000 1000 1000 # Specify data type (f32, f64, c32, c64) cargo run --release --example benchmark --type f64 512 512 512 # Specify memory layout (c=row-major, f=column-major for A, B, C) cargo run --release --example benchmark --layout fcf 1000 1000 1000 # CSV output for recording measurements cargo run --release --example benchmark --csv 1000 1000 1000 # With threading enabled cargo run --release --example benchmark --features threading 2000 2000 2000 ``` -------------------------------- ### Build with native CPU optimizations Source: https://context7.com/bluss/matrixmultiply/llms.txt Use this command to compile the project with native CPU instruction set optimizations. ```bash RUSTFLAGS="-C target-cpu=native" cargo build --release ``` -------------------------------- ### Perform matrix multiplication in no_std Source: https://context7.com/bluss/matrixmultiply/llms.txt Demonstrates using sgemm in a no_std environment with manual memory management. ```rust // Works in no_std environments #![no_std] extern crate alloc; use matrixmultiply::sgemm; use alloc::vec; use alloc::vec::Vec; fn multiply_matrices() { let a: Vec = vec![1.0, 2.0, 3.0, 4.0]; let b: Vec = vec![1.0, 0.0, 0.0, 1.0]; let mut c: Vec = vec![0.0; 4]; unsafe { sgemm(2, 2, 2, 1.0, a.as_ptr(), 2, 1, b.as_ptr(), 2, 1, 0.0, c.as_mut_ptr(), 2, 1); } } ``` -------------------------------- ### Accumulate Results with Beta Source: https://context7.com/bluss/matrixmultiply/llms.txt Use the beta parameter to compute C = αAB + βC, allowing for incremental matrix operations. ```rust use matrixmultiply::dgemm; let m = 2; let k = 2; let n = 2; let a: Vec = vec![1.0, 0.0, 0.0, 1.0]; // Identity matrix let b: Vec = vec![5.0, 6.0, 7.0, 8.0]; let mut c: Vec = vec![1.0, 2.0, 3.0, 4.0]; // Initial values in C unsafe { // Compute C = 1.0 * A * B + 2.0 * C // Since A is identity: C = B + 2*C = [5+2, 6+4, 7+6, 8+8] = [7, 10, 13, 16] dgemm( m, k, n, 1.0, // alpha a.as_ptr(), k as isize, 1, b.as_ptr(), n as isize, 1, 2.0, // beta: multiply existing C by 2 before adding c.as_mut_ptr(), n as isize, 1, ); } assert_eq!(c, vec![7.0, 10.0, 13.0, 16.0]); ``` -------------------------------- ### Configure no_std Support Source: https://context7.com/bluss/matrixmultiply/llms.txt Disable default features in Cargo.toml to use the library in no_std environments. ```toml # Cargo.toml for no_std usage [dependencies] matrixmultiply = { version = "0.3", default-features = false } # To enable CPU features at compile time for no_std: ``` -------------------------------- ### Perform Double Precision Matrix Multiplication with dgemm Source: https://context7.com/bluss/matrixmultiply/llms.txt Executes C ← α A B + β C for f64 matrices using column-major layout. Requires an unsafe block for raw pointer access. ```rust use matrixmultiply::dgemm; // Column-major layout example (Fortran-style) // For column-major: row stride = 1, column stride = number of rows let m = 3; let k = 2; let n = 4; // A is 3×2 in column-major: elements stored column by column let a: Vec = vec![ 1.0, 2.0, 3.0, // first column 4.0, 5.0, 6.0, // second column ]; // B is 2×4 in column-major let b: Vec = vec![ 1.0, 2.0, // first column 3.0, 4.0, // second column 5.0, 6.0, // third column 7.0, 8.0, // fourth column ]; let mut c: Vec = vec![0.0; m * n]; unsafe { dgemm( m, k, n, // dimensions 2.0, // alpha = 2: compute 2 * A * B a.as_ptr(), 1, // row stride (column-major) m as isize, // column stride (column-major) b.as_ptr(), 1, // row stride (column-major) k as isize, // column stride (column-major) 0.0, // beta = 0: overwrite C c.as_mut_ptr(), 1, // row stride (column-major) m as isize, // column stride (column-major) ); } // Result is 2 * (A * B) in column-major layout // First element: 2 * (1*1 + 4*2) = 2 * 9 = 18 assert_eq!(c[0], 18.0); ``` -------------------------------- ### Configure threading dependency Source: https://context7.com/bluss/matrixmultiply/llms.txt Add the threading feature to Cargo.toml to enable parallel matrix multiplication. ```toml # Cargo.toml [dependencies] matrixmultiply = { version = "0.3", features = ["threading"] } ``` -------------------------------- ### Perform Single Precision Matrix Multiplication with sgemm Source: https://context7.com/bluss/matrixmultiply/llms.txt Executes C ← α A B + β C for f32 matrices using row-major layout. Requires an unsafe block as it interacts with raw pointers. ```rust use matrixmultiply::sgemm; // Matrix dimensions: A is m×k, B is k×n, C is m×n let m = 4; // rows of A and C let k = 3; // columns of A, rows of B let n = 2; // columns of B and C // Row-major layout: row stride = number of columns, column stride = 1 let a: Vec = vec![ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, ]; let b: Vec = vec![ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, ]; let mut c: Vec = vec![0.0; m * n]; unsafe { sgemm( m, k, n, // dimensions 1.0, // alpha: scaling factor for A*B a.as_ptr(), // pointer to first element of A k as isize, // row stride of A (row-major) 1, // column stride of A b.as_ptr(), // pointer to first element of B n as isize, // row stride of B (row-major) 1, // column stride of B 0.0, // beta: scaling factor for C (0 means overwrite) c.as_mut_ptr(), // pointer to first element of C n as isize, // row stride of C (row-major) 1, // column stride of C ); } // Result: C = A * B // c = [22.0, 28.0, 49.0, 64.0, 76.0, 100.0, 103.0, 136.0] assert_eq!(c[0], 22.0); // 1*1 + 2*3 + 3*5 = 22 assert_eq!(c[1], 28.0); // 1*2 + 2*4 + 3*6 = 28 ``` -------------------------------- ### Multiply with Custom Strides Source: https://context7.com/bluss/matrixmultiply/llms.txt Utilize row and column strides to perform operations on submatrices or matrices with padding. ```rust use matrixmultiply::sgemm; // A larger allocation containing a 2×2 submatrix we want to multiply // The submatrix starts at index 0 but has gaps (stride > size) let full_a: Vec = vec![ 1.0, 2.0, 999.0, // row 0: elements at [0] and [1], padding at [2] 3.0, 4.0, 999.0, // row 1: elements at [3] and [4], padding at [5] ]; let b: Vec = vec![1.0, 0.0, 0.0, 1.0]; // 2×2 identity let mut c: Vec = vec![0.0; 4]; let m = 2; let k = 2; let n = 2; unsafe { sgemm( m, k, n, 1.0, full_a.as_ptr(), 3, // row stride = 3 (skip padding element) 1, // column stride = 1 b.as_ptr(), n as isize, 1, 0.0, c.as_mut_ptr(), n as isize, 1, ); } // Result extracts the 2×2 submatrix and multiplies by identity assert_eq!(c, vec![1.0, 2.0, 3.0, 4.0]); ``` -------------------------------- ### Perform multithreaded matrix multiplication Source: https://context7.com/bluss/matrixmultiply/llms.txt Uses dgemm with the threading feature enabled. Set the MATMUL_NUM_THREADS environment variable to control concurrency. ```rust use matrixmultiply::dgemm; // Set thread count before running (shell): // export MATMUL_NUM_THREADS=4 // Large matrix multiplication benefits from threading let n = 1000; let a: Vec = vec![1.0; n * n]; let b: Vec = vec![1.0; n * n]; let mut c: Vec = vec![0.0; n * n]; unsafe { // Automatically uses multiple threads for large matrices dgemm( n, n, n, 1.0, a.as_ptr(), n as isize, 1, b.as_ptr(), n as isize, 1, 0.0, c.as_mut_ptr(), n as isize, 1, ); } // Each element of C will be n (sum of n ones) assert_eq!(c[0], n as f64); ``` -------------------------------- ### dgemm - Double Precision Matrix Multiplication Source: https://context7.com/bluss/matrixmultiply/llms.txt Performs general matrix multiplication on f64 matrices using the formula C ← α A B + β C. ```APIDOC ## dgemm ### Description Performs general matrix multiplication on f64 (double precision) matrices using the formula C ← α A B + β C. ### Parameters - **m** (usize) - Required - Number of rows of A and C - **k** (usize) - Required - Number of columns of A and rows of B - **n** (usize) - Required - Number of columns of B and C - **alpha** (f64) - Required - Scaling factor for A*B - **a_ptr** (*const f64) - Required - Pointer to first element of A - **a_rs** (isize) - Required - Row stride of A - **a_cs** (isize) - Required - Column stride of A - **b_ptr** (*const f64) - Required - Pointer to first element of B - **b_rs** (isize) - Required - Row stride of B - **b_cs** (isize) - Required - Column stride of B - **beta** (f64) - Required - Scaling factor for C - **c_ptr** (*mut f64) - Required - Pointer to first element of C - **c_rs** (isize) - Required - Row stride of C - **c_cs** (isize) - Required - Column stride of C ``` -------------------------------- ### Perform Complex Double Precision Multiplication Source: https://context7.com/bluss/matrixmultiply/llms.txt Multiply complex f64 matrices using the cgemm feature for high-precision scientific computing. ```rust // Enable with: matrixmultiply = { version = "0.3", features = ["cgemm"] } use matrixmultiply::{zgemm, CGemmOption}; type C64 = [f64; 2]; let m = 2; let k = 2; let n = 1; // A = [[1+1i], [2+2i]] (2×1 stored as 2×2 for k=2 compatibility) // Actually: A = [[1+1i, 0], [0, 1+1i]] let a: Vec = vec![ [1.0, 1.0], [0.0, 0.0], [0.0, 0.0], [1.0, 1.0], ]; // B = [[2+3i], [4+5i]] let b: Vec = vec![ [2.0, 3.0], [4.0, 5.0], ]; let mut c: Vec = vec![[0.0, 0.0]; m * n]; // alpha = 1+0i, beta = 0 let alpha: C64 = [1.0, 0.0]; let beta: C64 = [0.0, 0.0]; unsafe { zgemm( CGemmOption::Standard, CGemmOption::Standard, m, k, n, alpha, a.as_ptr(), k as isize, 1, b.as_ptr(), n as isize, 1, beta, c.as_mut_ptr(), n as isize, 1, ); } // (1+1i)*(2+3i) = 2+3i+2i+3i² = 2+5i-3 = -1+5i assert_eq!(c[0], [-1.0, 5.0]); ``` -------------------------------- ### sgemm - Single Precision Matrix Multiplication Source: https://context7.com/bluss/matrixmultiply/llms.txt Performs general matrix multiplication on f32 matrices using the formula C ← α A B + β C. ```APIDOC ## sgemm ### Description Performs general matrix multiplication on f32 (single precision) matrices using the formula C ← α A B + β C. ### Parameters - **m** (usize) - Required - Number of rows of A and C - **k** (usize) - Required - Number of columns of A and rows of B - **n** (usize) - Required - Number of columns of B and C - **alpha** (f32) - Required - Scaling factor for A*B - **a_ptr** (*const f32) - Required - Pointer to first element of A - **a_rs** (isize) - Required - Row stride of A - **a_cs** (isize) - Required - Column stride of A - **b_ptr** (*const f32) - Required - Pointer to first element of B - **b_rs** (isize) - Required - Row stride of B - **b_cs** (isize) - Required - Column stride of B - **beta** (f32) - Required - Scaling factor for C - **c_ptr** (*mut f32) - Required - Pointer to first element of C - **c_rs** (isize) - Required - Row stride of C - **c_cs** (isize) - Required - Column stride of C ``` -------------------------------- ### Perform Complex Single Precision Multiplication Source: https://context7.com/bluss/matrixmultiply/llms.txt Multiply complex f32 matrices using the cgemm feature. Elements are represented as [real, imaginary] pairs. ```rust // Enable with: matrixmultiply = { version = "0.3", features = ["cgemm"] } use matrixmultiply::{cgemm, CGemmOption}; // Complex type: [real, imaginary] type C32 = [f32; 2]; let m = 2; let k = 2; let n = 2; // A = [[1+2i, 3+4i], [5+6i, 7+8i]] let a: Vec = vec![ [1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0], ]; // B = [[1+0i, 0+0i], [0+0i, 1+0i]] (identity) let b: Vec = vec![ [1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [1.0, 0.0], ]; let mut c: Vec = vec![[0.0, 0.0]; m * n]; let alpha: C32 = [1.0, 0.0]; // alpha = 1 let beta: C32 = [0.0, 0.0]; // beta = 0 unsafe { cgemm( CGemmOption::Standard, CGemmOption::Standard, m, k, n, alpha, a.as_ptr(), k as isize, 1, b.as_ptr(), n as isize, 1, beta, c.as_mut_ptr(), n as isize, 1, ); } // Result: C = A * I = A assert_eq!(c[0], [1.0, 2.0]); // 1+2i assert_eq!(c[1], [3.0, 4.0]); // 3+4i ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.