Struct PerformanceOptimizer

Source
pub struct PerformanceOptimizer { /* private fields */ }
Expand description

Unified performance optimization manager

Combines all performance optimization techniques including SIMD, memory efficiency, and parallel processing to provide optimal performance for neural network operations.

Implementations§

Source§

impl PerformanceOptimizer

Source

pub fn new( _chunk_size: Option<usize>, _max_memory_mb: Option<usize>, num_threads: Option<usize>, enable_profiling: bool, ) -> Result<Self>

Create a new performance optimizer

§Arguments
  • chunk_size - Chunk size for memory-efficient processing
  • max_memory_mb - Maximum memory usage in MB
  • num_threads - Number of threads for parallel processing
  • enable_profiling - Whether to enable performance profiling
§Examples
use scirs2_neural::performance::PerformanceOptimizer;

let optimizer = PerformanceOptimizer::new(
    Some(256),  // 256 samples per chunk
    Some(1024), // 1GB memory limit
    Some(8),    // 8 threads
    true        // enable profiling
).unwrap();
Examples found in repository?
examples/new_features_showcase.rs (line 162)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129    println!("⚡ Performance Optimizations Demonstration");
130    println!("========================================\n");
131
132    // 1. Thread Pool Manager
133    println!("1. Thread Pool Manager:");
134    let thread_pool = ThreadPoolManager::new(Some(4))?;
135    println!(
136        "   Created thread pool with {} threads",
137        thread_pool.num_threads()
138    );
139
140    // Demonstrate parallel matrix multiplication
141    let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142    let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144    let start_time = std::time::Instant::now();
145    let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146    let elapsed = start_time.elapsed();
147
148    println!(
149        "   Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150        matrix_a.shape()[0],
151        matrix_a.shape()[1],
152        matrix_b.shape()[0],
153        matrix_b.shape()[1],
154        result.shape()[0],
155        result.shape()[1]
156    );
157    println!("   Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158    println!("   Result sample: {:.1}", result[[0, 0]]);
159
160    // 2. Performance Profiler
161    println!("\n2. Performance Profiler:");
162    let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164    // Simulate some operations with profiling
165    {
166        let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167        let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168        let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169        optimizer
170            .profiler_mut()
171            .end_timer("matrix_setup".to_string(), timer);
172
173        let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174    }
175
176    println!("   Performance profile summary:");
177    optimizer.profiler().print_summary();
178
179    // 3. Optimization Capabilities
180    println!("\n3. Optimization Capabilities:");
181    let capabilities = optimizer.get_capabilities();
182    println!("{}", capabilities);
183
184    println!("✅ Performance optimizations demonstration completed!\n");
185    Ok(())
186}
Source

pub fn thread_pool(&self) -> &Arc<ThreadPoolManager>

Get reference to thread pool

Source

pub fn profiler_mut(&mut self) -> &mut PerformanceProfiler

Get mutable reference to profiler

Examples found in repository?
examples/new_features_showcase.rs (line 166)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129    println!("⚡ Performance Optimizations Demonstration");
130    println!("========================================\n");
131
132    // 1. Thread Pool Manager
133    println!("1. Thread Pool Manager:");
134    let thread_pool = ThreadPoolManager::new(Some(4))?;
135    println!(
136        "   Created thread pool with {} threads",
137        thread_pool.num_threads()
138    );
139
140    // Demonstrate parallel matrix multiplication
141    let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142    let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144    let start_time = std::time::Instant::now();
145    let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146    let elapsed = start_time.elapsed();
147
148    println!(
149        "   Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150        matrix_a.shape()[0],
151        matrix_a.shape()[1],
152        matrix_b.shape()[0],
153        matrix_b.shape()[1],
154        result.shape()[0],
155        result.shape()[1]
156    );
157    println!("   Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158    println!("   Result sample: {:.1}", result[[0, 0]]);
159
160    // 2. Performance Profiler
161    println!("\n2. Performance Profiler:");
162    let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164    // Simulate some operations with profiling
165    {
166        let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167        let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168        let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169        optimizer
170            .profiler_mut()
171            .end_timer("matrix_setup".to_string(), timer);
172
173        let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174    }
175
176    println!("   Performance profile summary:");
177    optimizer.profiler().print_summary();
178
179    // 3. Optimization Capabilities
180    println!("\n3. Optimization Capabilities:");
181    let capabilities = optimizer.get_capabilities();
182    println!("{}", capabilities);
183
184    println!("✅ Performance optimizations demonstration completed!\n");
185    Ok(())
186}
Source

pub fn profiler(&self) -> &PerformanceProfiler

Get reference to profiler

Examples found in repository?
examples/new_features_showcase.rs (line 177)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129    println!("⚡ Performance Optimizations Demonstration");
130    println!("========================================\n");
131
132    // 1. Thread Pool Manager
133    println!("1. Thread Pool Manager:");
134    let thread_pool = ThreadPoolManager::new(Some(4))?;
135    println!(
136        "   Created thread pool with {} threads",
137        thread_pool.num_threads()
138    );
139
140    // Demonstrate parallel matrix multiplication
141    let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142    let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144    let start_time = std::time::Instant::now();
145    let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146    let elapsed = start_time.elapsed();
147
148    println!(
149        "   Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150        matrix_a.shape()[0],
151        matrix_a.shape()[1],
152        matrix_b.shape()[0],
153        matrix_b.shape()[1],
154        result.shape()[0],
155        result.shape()[1]
156    );
157    println!("   Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158    println!("   Result sample: {:.1}", result[[0, 0]]);
159
160    // 2. Performance Profiler
161    println!("\n2. Performance Profiler:");
162    let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164    // Simulate some operations with profiling
165    {
166        let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167        let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168        let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169        optimizer
170            .profiler_mut()
171            .end_timer("matrix_setup".to_string(), timer);
172
173        let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174    }
175
176    println!("   Performance profile summary:");
177    optimizer.profiler().print_summary();
178
179    // 3. Optimization Capabilities
180    println!("\n3. Optimization Capabilities:");
181    let capabilities = optimizer.get_capabilities();
182    println!("{}", capabilities);
183
184    println!("✅ Performance optimizations demonstration completed!\n");
185    Ok(())
186}
Source

pub fn get_capabilities(&self) -> &OptimizationCapabilities

Get optimization capabilities

Examples found in repository?
examples/new_features_showcase.rs (line 181)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129    println!("⚡ Performance Optimizations Demonstration");
130    println!("========================================\n");
131
132    // 1. Thread Pool Manager
133    println!("1. Thread Pool Manager:");
134    let thread_pool = ThreadPoolManager::new(Some(4))?;
135    println!(
136        "   Created thread pool with {} threads",
137        thread_pool.num_threads()
138    );
139
140    // Demonstrate parallel matrix multiplication
141    let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142    let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144    let start_time = std::time::Instant::now();
145    let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146    let elapsed = start_time.elapsed();
147
148    println!(
149        "   Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150        matrix_a.shape()[0],
151        matrix_a.shape()[1],
152        matrix_b.shape()[0],
153        matrix_b.shape()[1],
154        result.shape()[0],
155        result.shape()[1]
156    );
157    println!("   Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158    println!("   Result sample: {:.1}", result[[0, 0]]);
159
160    // 2. Performance Profiler
161    println!("\n2. Performance Profiler:");
162    let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164    // Simulate some operations with profiling
165    {
166        let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167        let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168        let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169        optimizer
170            .profiler_mut()
171            .end_timer("matrix_setup".to_string(), timer);
172
173        let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174    }
175
176    println!("   Performance profile summary:");
177    optimizer.profiler().print_summary();
178
179    // 3. Optimization Capabilities
180    println!("\n3. Optimization Capabilities:");
181    let capabilities = optimizer.get_capabilities();
182    println!("{}", capabilities);
183
184    println!("✅ Performance optimizations demonstration completed!\n");
185    Ok(())
186}
Source

pub fn optimized_matmul( &mut self, a: &ArrayD<f32>, b: &ArrayD<f32>, ) -> Result<ArrayD<f32>>

Optimized matrix multiplication using all available optimizations

Automatically selects the best optimization strategy based on matrix size, available features, and system capabilities.

Examples found in repository?
examples/new_features_showcase.rs (line 173)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129    println!("⚡ Performance Optimizations Demonstration");
130    println!("========================================\n");
131
132    // 1. Thread Pool Manager
133    println!("1. Thread Pool Manager:");
134    let thread_pool = ThreadPoolManager::new(Some(4))?;
135    println!(
136        "   Created thread pool with {} threads",
137        thread_pool.num_threads()
138    );
139
140    // Demonstrate parallel matrix multiplication
141    let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142    let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144    let start_time = std::time::Instant::now();
145    let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146    let elapsed = start_time.elapsed();
147
148    println!(
149        "   Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150        matrix_a.shape()[0],
151        matrix_a.shape()[1],
152        matrix_b.shape()[0],
153        matrix_b.shape()[1],
154        result.shape()[0],
155        result.shape()[1]
156    );
157    println!("   Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158    println!("   Result sample: {:.1}", result[[0, 0]]);
159
160    // 2. Performance Profiler
161    println!("\n2. Performance Profiler:");
162    let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164    // Simulate some operations with profiling
165    {
166        let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167        let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168        let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169        optimizer
170            .profiler_mut()
171            .end_timer("matrix_setup".to_string(), timer);
172
173        let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174    }
175
176    println!("   Performance profile summary:");
177    optimizer.profiler().print_summary();
178
179    // 3. Optimization Capabilities
180    println!("\n3. Optimization Capabilities:");
181    let capabilities = optimizer.get_capabilities();
182    println!("{}", capabilities);
183
184    println!("✅ Performance optimizations demonstration completed!\n");
185    Ok(())
186}
Source

pub fn optimized_conv2d( &mut self, input: &ArrayD<f32>, kernel: &ArrayD<f32>, bias: Option<&[f32]>, stride: (usize, usize), padding: (usize, usize), ) -> Result<ArrayD<f32>>

Optimized convolution using all available optimizations

Source

pub fn get_performance_stats(&self) -> PerformanceStats

Get comprehensive performance statistics

Source

pub fn reset_stats(&mut self)

Reset all performance tracking

Source

pub fn benchmark_strategies( &mut self, a: &ArrayD<f32>, b: &ArrayD<f32>, iterations: usize, ) -> Result<BenchmarkResults>

Benchmark different optimization strategies

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V