scirs2_series/gpu_acceleration/
utils.rs

1//! GPU acceleration utility functions
2//!
3//! This module provides utility functions for GPU operations including
4//! system capability checking, memory optimization, and configuration.
5
6use super::{
7    GpuConfig, GpuDeviceManager, GraphOptimizationLevel, MemoryStrategy, TensorCoresConfig,
8};
9
10/// Check if GPU acceleration is supported on this system
11pub fn is_gpu_supported() -> bool {
12    // Check for actual GPU framework availability
13    if let Ok(device_manager) = GpuDeviceManager::new() {
14        device_manager.is_gpu_available()
15    } else {
16        false
17    }
18}
19
20/// Get recommended batch size for GPU operations
21pub fn get_recommended_batch_size(_data_size: usize, memorylimit: usize) -> usize {
22    let element_size = std::mem::size_of::<f64>(); // Assume f64 for estimation
23    let max_batch = memorylimit / element_size;
24    std::cmp::min(_data_size, max_batch)
25}
26
27/// Estimate GPU memory requirements for operation
28pub fn estimate_memory_usage(_data_size: usize, operationoverhead: f64) -> usize {
29    let base_memory = _data_size * std::mem::size_of::<f64>();
30    (base_memory as f64 * (1.0 + operationoverhead)) as usize
31}
32
33/// Choose optimal GPU configuration based on data characteristics
34pub fn optimize_gpu_config(_data_size: usize, availablememory: usize) -> GpuConfig {
35    let batch_size = get_recommended_batch_size(_data_size, availablememory / 4);
36
37    GpuConfig {
38        device_id: 0,
39        memory_pool_size: Some(availablememory / 2),
40        enable_memory_optimization: true,
41        batch_size,
42        use_half_precision: _data_size > 100_000,
43        enable_async: true,
44        tensor_cores: TensorCoresConfig::default(),
45        memory_strategy: MemoryStrategy::PreAllocated {
46            pool_size: availablememory / 2,
47        },
48        dynamic_batching: true,
49        graph_optimization: GraphOptimizationLevel::Extended,
50    }
51}