-
Notifications
You must be signed in to change notification settings - Fork 0
MEX Acceleration
FastSense includes optional C MEX functions with SIMD intrinsics for maximum performance. All MEX functions have pure-MATLAB fallbacks — behavior is identical.
cd libs/FastSense
build_mex();The build script auto-detects your architecture and compiles all MEX functions with appropriate SIMD optimizations.
| Platform | Compiler |
|---|---|
| macOS | Xcode Command Line Tools |
| Linux | GCC |
| Windows | MSVC |
SQLite3 is bundled as an amalgamation and compiled directly into MEX files that need it — no system installation required.
All MEX functions include a common SIMD abstraction layer that adapts to your CPU:
| Architecture | SIMD Instructions | Fallback |
|---|---|---|
| x86_64 | AVX2 + FMA | SSE2 |
| ARM64 (Apple Silicon) | NEON | - |
| Other | Scalar operations | - |
If AVX2 compilation fails on x86_64, the build script automatically retries with SSE2.
binary_search_mex — O(log n) binary search for visible data range
-
Speedup: 10-20x over MATLAB's
find - Used by: Zoom/pan callbacks to locate visible indices
minmax_core_mex — Per-pixel MinMax reduction with SIMD vectorization
- Speedup: 3-10x over pure MATLAB
- SIMD: Processes 4 doubles (AVX2) or 2 doubles (NEON) per cycle
- Used by: Default downsampling algorithm in FastPlot
lttb_core_mex — Largest Triangle Three Buckets with SIMD triangle area computation
- Speedup: 10-50x over MATLAB implementation
- Used by: LTTB downsampling method
violation_cull_mex — Fused threshold violation detection and pixel culling
- Speedup: Significant (single-pass vs two-pass MATLAB)
- Used by: Violation marker rendering during zoom/pan
compute_violations_mex — Batch threshold violation detection
- Speedup: Significant over per-point MATLAB comparison
- Used by: Sensors resolution pipeline
build_store_mex — Bulk SQLite writer for DataStore initialization
- Speedup: 2-3x (eliminates ~20K MATLAB-to-MEX round-trips)
- SIMD: Accelerated Y min/max computation per chunk
-
Used by:
FastSenseDataStoreconstruction
resolve_disk_mex — SQLite disk-based sensor resolution
-
Used by:
Sensor.resolve()with disk-backed storage - Benefit: Reads chunks from database without loading full datasets
mksqlite — SQLite3 MEX interface with typed BLOB support
- Used by: DataStore, disk-backed sensor resolution
- Features: Serializes MATLAB arrays preserving type and shape
When MEX files are unavailable:
- Each function has a pure-MATLAB equivalent in
libs/FastSense/private/ - Runtime auto-detection switches between MEX and MATLAB seamlessly
- Identical numerical results and API
- Performance remains excellent for datasets under ~10M points
The build_mex() function:
-
Detects architecture — normalizes platform strings (
maca64,aarch64, etc.) into canonical labels - Selects compiler — prefers GCC on Octave for better auto-vectorization; uses MATLAB's default on MATLAB
- Sets SIMD flags — chooses instruction sets based on detected CPU architecture
- Compiles sources — builds all MEX files with bundled SQLite3 amalgamation
- Handles failures — automatically retries x86_64 builds with SSE2 if AVX2 fails
- Copies shared files — distributes MEX binaries to other library directories
Test that MEX functions produce identical results to MATLAB fallbacks:
install;
addpath('tests');
test_mex_parity; % Verify MEX matches MATLAB output
test_mex_edge_cases; % Test edge cases (empty arrays, NaN, etc.)The test suite validates numerical accuracy across all MEX functions and handles edge cases like empty arrays, single points, and NaN values.
FastPlot Wiki
API Reference
Guides
Use Cases
Internals
Resources