MMatrix is my personal C matrix library playground for macOS and Linux, supporting both dense and sparse matrix formats with SIMD and BLAS acceleration.
sm
: Single-precision (float) dense matrixdm
: Double-precision (double) dense matrixdms
: Double-precision sparse matrix (COO format)
All modules support:
- Random matrix creation using PCG (permuted congruential generator)
- Elementwise and matrix operations
- Console-based printing
- I/O for:
- MATLAB
.mat
files (viamatio
andhdf5
) - Matrix Market files
- MATLAB
- [macOS] Optimized with Apple Accelerate (vDSP, BLAS, LAPACK)
- [macOS] Optional use of Metal Performance Shaders (MPS)
- SIMD acceleration with OpenMP and [macOS] ARM NEON intrinsics
- PCG-based random number generation for reproducibility and parallelism
Ensure required dependencies are installed via Homebrew (Linux too):
brew install openblas libomp suitesparse matio llvm
Then build using Bazel on macOS:
bazel build //app/matrix
On Linux use '--config=linux_llvm' to use the LLVM toolchain installed by Homebrew:
bazel build //app/matrix --config=linux_llvm
You can enable optional BLAS backends via Bazel defines:
-
Linux and macOS:
bazel build //app/matrix --define=USE_OPENBLAS=1
-
macOS:
- Use Accelerate framework:
bazel build //app/matrix --define=USE_ACCELERATE=1
- Use Accelerate + Metal (MPS):
bazel build //app/matrix --define=USE_ACCELERATE_MPS=1
- Use Accelerate framework:
You can install the compiled library and headers into a custom directory using the Bazel installer target:
bazel run //:matrix_installer -- /your/installation/path
i.e.
bazel run //:matrix_installer -- $(PWD)/lib/matrix
To also create symbolic links into system-wide directories (e.g., /usr/local/include
, /usr/local/lib
), you can enable system integration in BUILD - File:
installer(
name = "matrix_installer",
data = [
"//app/matrix", # the target to be installed
"//app/matrix:matrix_header", # collected in a Bazel filegroup with all public headers
"//:LICENSE.txt", # if available
],
system_integration = False, # set "True" symlinks into /usr/local/lib and /usr/local/include
)
This will:
- Copy the compiled static library (
libmatrix.a
) and dependencies intolib/
- Install public headers into
include/
- Optionally create symlinks for easy access from standard system locations
Unit tests are written using Unity:
bazel test //...
Tests are located in the app/matrix/tests/
folder. You can add new test files and they will be discovered automatically by Bazel.
FloatMatrix *A = sm_create_random(128, 128);
FloatMatrix *B = sm_transpose(A);
sm_destroy(A);
sm_destroy(B);
- macOS (ARM64 preferred, supports Apple Silicon M1/M2/M3 ...)
- Linux (x86 64bit, tested on Ubuntu + Homebrew)
- Clang/LLVM toolchain with OpenMP support required
This project depends on:
- OpenBLAS
- libomp
- SuiteSparse
- matio
- LLVM (via Homebrew)
All dependencies are pulled via Bazel modules or expected to be installed via Homebrew.
This project is primarily licensed under the MIT License (see LICENSE). It includes files from the Bazel project, licensed under the Apache License 2.0 (see tools/install/*, ).