quantrs2_tytan/realtime_quantum_integration/
resource.rs

1//! Resource allocation types for Real-time Quantum Computing Integration
2//!
3//! This module provides resource allocation and management types.
4
5use serde::{Deserialize, Serialize};
6use std::collections::{HashMap, VecDeque};
7use std::time::{Duration, SystemTime};
8
9use super::config::RealtimeConfig;
10use super::types::{JobPriority, PatternType, ResourceConstraint, ResourceType, Trend};
11
12/// Dynamic resource allocator
13#[allow(dead_code)]
14pub struct ResourceAllocator {
15    /// Available resources
16    pub(crate) available_resources: HashMap<String, ResourceInfo>,
17    /// Resource allocation map
18    pub(crate) allocation_map: HashMap<String, AllocationInfo>,
19    /// Allocation strategy
20    pub(crate) strategy: super::types::AllocationStrategy,
21    /// Allocation history
22    pub(crate) allocation_history: VecDeque<AllocationEvent>,
23    /// Predictor for resource needs
24    pub(crate) resource_predictor: ResourcePredictor,
25}
26
27impl ResourceAllocator {
28    pub fn new(config: &RealtimeConfig) -> Self {
29        Self {
30            available_resources: HashMap::new(),
31            allocation_map: HashMap::new(),
32            strategy: config.allocation_strategy.clone(),
33            allocation_history: VecDeque::new(),
34            resource_predictor: ResourcePredictor::new(),
35        }
36    }
37
38    pub fn allocate_resources(
39        &mut self,
40        job_id: &str,
41        _requirements: super::queue::ResourceRequirements,
42    ) -> Result<Vec<String>, String> {
43        // Simplified resource allocation
44        let allocated_resources = vec!["resource_1".to_string(), "resource_2".to_string()];
45
46        let allocation = AllocationInfo {
47            job_id: job_id.to_string(),
48            allocated_resources: allocated_resources.clone(),
49            allocation_time: SystemTime::now(),
50            expected_completion: SystemTime::now() + Duration::from_secs(3600),
51            priority: JobPriority::Normal,
52            resource_usage: ResourceUsage {
53                cpu_usage: 0.5,
54                memory_usage: 0.3,
55                network_usage: 0.1,
56                custom_usage: HashMap::new(),
57            },
58        };
59
60        self.allocation_map.insert(job_id.to_string(), allocation);
61
62        Ok(allocated_resources)
63    }
64}
65
66/// Resource information
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct ResourceInfo {
69    /// Resource ID
70    pub resource_id: String,
71    /// Resource type
72    pub resource_type: ResourceType,
73    /// Total capacity
74    pub total_capacity: ResourceCapacity,
75    /// Available capacity
76    pub available_capacity: ResourceCapacity,
77    /// Current utilization
78    pub current_utilization: f64,
79    /// Performance characteristics
80    pub performance_characteristics: PerformanceCharacteristics,
81    /// Constraints
82    pub constraints: Vec<ResourceConstraint>,
83}
84
85/// Resource capacity specification
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct ResourceCapacity {
88    /// Compute units
89    pub compute_units: f64,
90    /// Memory (in GB)
91    pub memory_gb: f64,
92    /// Storage (in GB)
93    pub storage_gb: f64,
94    /// Network bandwidth (in Mbps)
95    pub network_mbps: f64,
96    /// Custom metrics
97    pub custom_metrics: HashMap<String, f64>,
98}
99
100/// Performance characteristics of a resource
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct PerformanceCharacteristics {
103    /// Processing speed
104    pub processing_speed: f64,
105    /// Latency
106    pub latency: Duration,
107    /// Reliability score
108    pub reliability_score: f64,
109    /// Energy efficiency
110    pub energy_efficiency: f64,
111    /// Scalability factor
112    pub scalability_factor: f64,
113}
114
115/// Allocation information for a job
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct AllocationInfo {
118    /// Job ID
119    pub job_id: String,
120    /// Allocated resources
121    pub allocated_resources: Vec<String>,
122    /// Allocation timestamp
123    pub allocation_time: SystemTime,
124    /// Expected completion time
125    pub expected_completion: SystemTime,
126    /// Priority
127    pub priority: JobPriority,
128    /// Resource usage
129    pub resource_usage: ResourceUsage,
130}
131
132/// Resource usage metrics
133#[derive(Debug, Clone, Serialize, Deserialize)]
134pub struct ResourceUsage {
135    /// CPU usage
136    pub cpu_usage: f64,
137    /// Memory usage
138    pub memory_usage: f64,
139    /// Network usage
140    pub network_usage: f64,
141    /// Custom usage metrics
142    pub custom_usage: HashMap<String, f64>,
143}
144
145/// Allocation event for tracking
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct AllocationEvent {
148    /// Event timestamp
149    pub timestamp: SystemTime,
150    /// Event type
151    pub event_type: AllocationEventType,
152    /// Job ID
153    pub job_id: String,
154    /// Resources involved
155    pub resources: Vec<String>,
156    /// Metadata
157    pub metadata: HashMap<String, String>,
158}
159
160/// Types of allocation events
161#[derive(Debug, Clone, Serialize, Deserialize)]
162pub enum AllocationEventType {
163    Allocated,
164    Deallocated,
165    Modified,
166    Failed,
167    Preempted,
168}
169
170/// Resource predictor for forecasting needs
171#[derive(Debug, Clone)]
172pub struct ResourcePredictor {
173    /// Historical usage patterns
174    pub(crate) usage_patterns: HashMap<String, UsagePattern>,
175    /// Prediction models
176    pub(crate) prediction_models: HashMap<String, PredictionModel>,
177    /// Forecast horizon
178    pub(crate) forecast_horizon: Duration,
179}
180
181impl Default for ResourcePredictor {
182    fn default() -> Self {
183        Self::new()
184    }
185}
186
187impl ResourcePredictor {
188    pub fn new() -> Self {
189        Self {
190            usage_patterns: HashMap::new(),
191            prediction_models: HashMap::new(),
192            forecast_horizon: Duration::from_secs(3600),
193        }
194    }
195}
196
197/// Usage pattern for prediction
198#[derive(Debug, Clone)]
199pub struct UsagePattern {
200    /// Pattern name
201    pub pattern_name: String,
202    /// Historical data points
203    pub data_points: VecDeque<(SystemTime, f64)>,
204    /// Pattern type
205    pub pattern_type: PatternType,
206    /// Seasonality
207    pub seasonality: Option<Duration>,
208    /// Trend
209    pub trend: Trend,
210}
211
212/// Prediction model
213#[derive(Debug, Clone)]
214pub struct PredictionModel {
215    /// Model name
216    pub model_name: String,
217    /// Model parameters
218    pub parameters: HashMap<String, f64>,
219    /// Prediction accuracy
220    pub accuracy: f64,
221    /// Last training time
222    pub last_training: SystemTime,
223}