Platform Library
Platform-specific optimizations and utilities for the DiskANN project, providing cross-platform abstractions for file I/O, performance monitoring, and system-specific features.
Overview
The Platform library provides platform-specific optimizations and abstractions that enable DiskANN to achieve high performance across different operating systems and architectures. It handles file I/O, performance monitoring, and system-specific features in a cross-platform manner.
Features
- Cross-platform file I/O - Optimized file operations for different platforms
- Performance monitoring - System-specific performance counters and timing
- Memory management - Platform-optimized memory allocation and management
- I/O completion ports - High-performance asynchronous I/O (Windows)
- File handles - Platform-specific file handle abstractions
- Performance counters - CPU and memory usage monitoring
Quick Start
use ;
// Open a file with platform optimizations
let file = open?;
// Monitor performance
let perf = new;
perf.start_measurement;
// Perform file operations
let data = file.read_at?;
perf.end_measurement;
println!;
File Operations
Basic File I/O
use ;
// Open file for reading
let file = open?;
// Read data
let mut buffer = vec!;
let bytes_read = file.read?;
// Open file for writing
let mut file = create?;
file.write?;
Aligned File I/O
For optimal performance with vector data:
use ;
let file = open?;
// Read aligned data for SIMD operations
let mut buffer = vec!;
let bytes_read = file.read_aligned?; // 32-byte alignment
Asynchronous I/O
use ;
// Create I/O completion port
let iocp = new?;
// Register file with completion port
let file = open?;
iocp.associate_file?;
// Submit async read
let mut buffer = vec!;
iocp.read_async?;
// Wait for completion
let result = iocp.wait_for_completion?;
Performance Monitoring
CPU Performance
use Perf;
let perf = new;
// Start monitoring
perf.start_cpu_measurement;
// Your computation here
let result = compute_vectors;
// End monitoring
perf.end_cpu_measurement;
println!;
println!;
Memory Usage
use Perf;
let perf = new;
// Get current memory usage
let memory_info = perf.get_memory_info?;
println!;
println!;
System Information
use Perf;
let perf = new;
// Get system information
let sys_info = perf.get_system_info?;
println!;
println!;
println!;
Platform-Specific Features
Windows
use ;
// Windows-specific optimizations
let iocp = new?;
let file = open_with_options?;
Linux
use ;
// Linux-specific features
let perf = with_pid?;
let file = open_with_direct_io?;
macOS
use ;
// macOS-specific optimizations
let file = open_with_fcntl?;
let perf = with_mach_port?;
Memory Management
Aligned Allocation
use memory;
// Allocate aligned memory for SIMD operations
let ptr = aligned_alloc?; // 32-byte alignment
let slice = unsafe ;
// Use the memory
// ...
// Free aligned memory
aligned_free;
Memory Mapping
use memory;
// Memory map a file
let mapping = map_file?;
let data = mapping.;
// Access memory-mapped data
for &value in data
Performance Optimization
File I/O Optimization
use ;
let perf = new;
perf.start_measurement;
// Use platform-optimized file operations
let file = open_with_options?;
// Read in large chunks for better performance
let mut buffer = vec!; // 64KB chunks
let mut total_read = 0;
while let Ok = file.read
perf.end_measurement;
println!;
Batch Operations
use ;
let file = open?;
// Batch read operations
let mut vectors = Vec new;
let batch_size = 1000;
let vector_size = 128 * 4; // 128 f32 values
for i in 0..batch_size
Error Handling
use ;
Benchmarks
Performance comparison across platforms (Intel i7-8700K):
Operation | Windows | Linux | macOS |
---|---|---|---|
Sequential Read | 2.1 GB/s | 2.8 GB/s | 2.5 GB/s |
Random Read | 1.8 GB/s | 2.2 GB/s | 2.0 GB/s |
Memory Map | 3.2 GB/s | 3.5 GB/s | 3.1 GB/s |
Async I/O | 2.5 GB/s | 2.9 GB/s | 2.3 GB/s |
Benchmarks on NVMe SSD
Development
Building
Testing
Platform-Specific Testing
# Test on specific platform
API Reference
Core Types
FileHandle
- Platform-optimized file handleFileIO
- File I/O mode enumerationPerf
- Performance monitoringIOCompletionPort
- Asynchronous I/O (Windows)PlatformError
- Platform-specific error types
Main Functions
FileHandle::open()
- Open file with optimizationsFileHandle::read()
- Read data from fileFileHandle::write()
- Write data to filePerf::start_measurement()
- Start performance monitoringPerf::end_measurement()
- End performance monitoring
Utility Functions
memory::aligned_alloc()
- Allocate aligned memorymemory::aligned_free()
- Free aligned memorymemory::map_file()
- Memory map fileget_system_info()
- Get system information
Dependencies
- libc - System calls and constants
- winapi - Windows API (Windows only)
- mach - macOS system calls (macOS only)
- errno - Error handling
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
We welcome contributions! Please see the main README for contribution guidelines.