use scirs2_core::memory_efficient::{
CompressedMemMapBuilder, CompressionAlgorithm, PrefetchConfigBuilder, Prefetching,
};
use scirs2_core::ndarray_ext::Array2;
use std::time::{Duration, Instant};
use tempfile::tempdir;
#[allow(dead_code)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Memory-Mapped Array Smart Prefetching Example");
println!("=============================================\n");
let dir = tempdir()?;
let file_path = dir.path().join("test_compressed.cmm");
println!("Creating test data...");
let rows = 1000;
let cols = 1000;
let data = Array2::<f64>::from_shape_fn((rows, cols), |(i, j)| (i * cols + j) as f64);
println!("Creating compressed memory-mapped array...");
let builder = CompressedMemMapBuilder::new()
.with_block_size(1000) .with_algorithm(CompressionAlgorithm::Lz4)
.with_level(1)
.with_cache_size(10) .with_description("Test matrix for prefetching example");
let cmm = builder.create(&data, &file_path)?;
println!("Compressed memory-mapped array created successfully");
println!("\nPart 1: Sequential access without prefetching");
let start = Instant::now();
let mut sum = 0.0;
for i in 0..rows {
for j in 0..cols {
let val = cmm.get(&[i, j])?;
sum += val;
}
}
let elapsed = start.elapsed();
println!("Sum: {}", sum);
println!("Time without prefetching: {:?}", elapsed);
println!("\nPart 2: Sequential access with prefetching");
let config = PrefetchConfigBuilder::new()
.enabled(true)
.prefetch_count(5) .min_pattern_length(3) .async_prefetch(true) .prefetch_timeout(Duration::from_millis(50))
.build();
let prefetching_cmm = cmm.clone().with_prefetching_config(config)?;
let start = Instant::now();
let mut sum = 0.0;
for i in 0..rows {
for j in 0..cols {
let val = prefetching_cmm.get(&[i, j])?;
sum += val;
}
}
let elapsed = start.elapsed();
println!("Sum: {}", sum);
println!("Time with prefetching: {:?}", elapsed);
let stats = prefetching_cmm.prefetch_stats()?;
println!("\nPrefetching Statistics:");
println!("Total prefetch operations: {}", stats.prefetch_count);
println!("Prefetch hits: {}", stats.prefetch_hits);
println!("Prefetch misses: {}", stats.prefetch_misses);
println!("Hit rate: {:.2}%", stats.hit_rate * 100.0);
println!("\nPart 3: Strided access pattern");
let config = PrefetchConfigBuilder::new()
.enabled(true)
.prefetch_count(5)
.history_size(50) .min_pattern_length(5)
.async_prefetch(true)
.prefetch_timeout(Duration::from_millis(50))
.build();
let mut prefetching_cmm = cmm.clone().with_prefetching_config(config)?;
prefetching_cmm.clear_prefetch_state()?;
println!("Accessing with stride 10 pattern...");
let start = Instant::now();
let mut sum = 0.0;
let stride = 10;
for i in (0..rows).step_by(stride) {
for j in (0..cols).step_by(stride) {
let val = prefetching_cmm.get(&[i, j])?;
sum += val;
}
}
let elapsed = start.elapsed();
println!("Sum of strided elements: {}", sum);
println!("Time for strided access with prefetching: {:?}", elapsed);
let stats = prefetching_cmm.prefetch_stats()?;
println!("\nPrefetching Statistics for Strided Access:");
println!("Total prefetch operations: {}", stats.prefetch_count);
println!("Prefetch hits: {}", stats.prefetch_hits);
println!("Prefetch misses: {}", stats.prefetch_misses);
println!("Hit rate: {:.2}%", stats.hit_rate * 100.0);
println!("\nPart 4: Random access pattern");
let config = PrefetchConfigBuilder::new()
.enabled(true)
.prefetch_count(10) .history_size(100) .min_pattern_length(3)
.async_prefetch(true)
.prefetch_timeout(Duration::from_millis(50))
.build();
let mut prefetching_cmm = cmm.clone().with_prefetching_config(config)?;
prefetching_cmm.clear_prefetch_state()?;
println!("Accessing with random pattern...");
let start = Instant::now();
let mut sum = 0.0;
let mut x = 123456789;
let mut y = 362436069;
let mut z = 521288629;
for _ in 0..10000 {
let t = x ^ (x << 11);
x = y;
y = z;
z = z ^ (z >> 19) ^ (t ^ (t >> 8));
let i = (z % rows as u32) as usize;
let j = (x % cols as u32) as usize;
let val = prefetching_cmm.get(&[i, j])?;
sum += val;
}
let elapsed = start.elapsed();
println!("Sum of random elements: {}", sum);
println!("Time for random access with prefetching: {:?}", elapsed);
let stats = prefetching_cmm.prefetch_stats()?;
println!("\nPrefetching Statistics for Random Access:");
println!("Total prefetch operations: {}", stats.prefetch_count);
println!("Prefetch hits: {}", stats.prefetch_hits);
println!("Prefetch misses: {}", stats.prefetch_misses);
println!("Hit rate: {:.2}%", stats.hit_rate * 100.0);
println!("\nExample completed successfully!");
Ok(())
}