scirs2-integrate 0.4.2

Numerical integration module for SciRS2 (scirs2-integrate)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Node management for distributed computing
//!
//! This module provides abstractions for compute nodes in the distributed
//! integration system, including node lifecycle management, resource tracking,
//! and health monitoring.

use crate::common::IntegrateFloat;
use crate::distributed::types::{
    ChunkResult, DistributedError, DistributedResult, NodeCapabilities, NodeId, NodeInfo,
    NodeStatus, SimdCapability, WorkChunk,
};
use crate::error::IntegrateResult;
use scirs2_core::ndarray::Array1;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::{Duration, Instant};

/// Manager for compute nodes in the distributed system
pub struct NodeManager {
    /// Registered nodes
    nodes: RwLock<HashMap<NodeId, NodeInfo>>,
    /// Next node ID to assign
    next_node_id: AtomicU64,
    /// Node health check timeout
    health_check_timeout: Duration,
    /// Shutdown flag
    shutdown: AtomicBool,
    /// Health monitor handle
    health_monitor: Mutex<Option<thread::JoinHandle<()>>>,
    /// Node failure callbacks
    failure_callbacks: RwLock<Vec<Arc<dyn Fn(NodeId) + Send + Sync>>>,
}

impl NodeManager {
    /// Create a new node manager
    pub fn new(health_check_timeout: Duration) -> Self {
        Self {
            nodes: RwLock::new(HashMap::new()),
            next_node_id: AtomicU64::new(1),
            health_check_timeout,
            shutdown: AtomicBool::new(false),
            health_monitor: Mutex::new(None),
            failure_callbacks: RwLock::new(Vec::new()),
        }
    }

    /// Start the health monitoring background thread
    pub fn start_health_monitoring(&self) -> IntegrateResult<()> {
        let nodes = unsafe { &*(&self.nodes as *const RwLock<HashMap<NodeId, NodeInfo>>) };
        let timeout = self.health_check_timeout;
        let shutdown = unsafe { &*(&self.shutdown as *const AtomicBool) };
        let callbacks = unsafe {
            &*(&self.failure_callbacks as *const RwLock<Vec<Arc<dyn Fn(NodeId) + Send + Sync>>>)
        };

        // Create references for the thread
        let nodes_ptr = nodes as *const RwLock<HashMap<NodeId, NodeInfo>> as usize;
        let shutdown_ptr = shutdown as *const AtomicBool as usize;
        let callbacks_ptr =
            callbacks as *const RwLock<Vec<Arc<dyn Fn(NodeId) + Send + Sync>>> as usize;

        let handle = thread::spawn(move || {
            let nodes = unsafe { &*(nodes_ptr as *const RwLock<HashMap<NodeId, NodeInfo>>) };
            let shutdown = unsafe { &*(shutdown_ptr as *const AtomicBool) };
            let callbacks = unsafe {
                &*(callbacks_ptr as *const RwLock<Vec<Arc<dyn Fn(NodeId) + Send + Sync>>>)
            };

            while !shutdown.load(Ordering::Relaxed) {
                // Check node health
                let failed_nodes = {
                    let mut nodes_write = match nodes.write() {
                        Ok(guard) => guard,
                        Err(_) => continue,
                    };

                    let mut failed = Vec::new();
                    for (id, info) in nodes_write.iter_mut() {
                        if !info.is_healthy(timeout) && info.status != NodeStatus::Failed {
                            info.status = NodeStatus::Failed;
                            failed.push(*id);
                        }
                    }
                    failed
                };

                // Invoke failure callbacks
                if !failed_nodes.is_empty() {
                    if let Ok(cbs) = callbacks.read() {
                        for node_id in &failed_nodes {
                            for cb in cbs.iter() {
                                cb(*node_id);
                            }
                        }
                    }
                }

                thread::sleep(Duration::from_secs(1));
            }
        });

        if let Ok(mut monitor) = self.health_monitor.lock() {
            *monitor = Some(handle);
        }

        Ok(())
    }

    /// Stop health monitoring
    pub fn stop_health_monitoring(&self) {
        self.shutdown.store(true, Ordering::Relaxed);
        if let Ok(mut monitor) = self.health_monitor.lock() {
            if let Some(handle) = monitor.take() {
                let _ = handle.join();
            }
        }
    }

    /// Register a new node
    pub fn register_node(
        &self,
        address: SocketAddr,
        capabilities: NodeCapabilities,
    ) -> DistributedResult<NodeId> {
        let node_id = NodeId::new(self.next_node_id.fetch_add(1, Ordering::SeqCst));

        let mut node_info = NodeInfo::new(node_id, address);
        node_info.capabilities = capabilities;
        node_info.status = NodeStatus::Available;

        match self.nodes.write() {
            Ok(mut nodes) => {
                nodes.insert(node_id, node_info);
                Ok(node_id)
            }
            Err(_) => Err(DistributedError::CommunicationError(
                "Failed to acquire nodes lock".to_string(),
            )),
        }
    }

    /// Deregister a node
    pub fn deregister_node(&self, node_id: NodeId) -> DistributedResult<()> {
        match self.nodes.write() {
            Ok(mut nodes) => {
                nodes.remove(&node_id);
                Ok(())
            }
            Err(_) => Err(DistributedError::CommunicationError(
                "Failed to acquire nodes lock".to_string(),
            )),
        }
    }

    /// Update node heartbeat
    pub fn update_heartbeat(&self, node_id: NodeId) -> DistributedResult<()> {
        match self.nodes.write() {
            Ok(mut nodes) => {
                if let Some(node) = nodes.get_mut(&node_id) {
                    node.last_heartbeat = Instant::now();
                    if node.status == NodeStatus::Failed {
                        node.status = NodeStatus::Available;
                    }
                    Ok(())
                } else {
                    Err(DistributedError::NodeFailure(
                        node_id,
                        "Node not found".to_string(),
                    ))
                }
            }
            Err(_) => Err(DistributedError::CommunicationError(
                "Failed to acquire nodes lock".to_string(),
            )),
        }
    }

    /// Update node status
    pub fn update_status(&self, node_id: NodeId, status: NodeStatus) -> DistributedResult<()> {
        match self.nodes.write() {
            Ok(mut nodes) => {
                if let Some(node) = nodes.get_mut(&node_id) {
                    node.status = status;
                    Ok(())
                } else {
                    Err(DistributedError::NodeFailure(
                        node_id,
                        "Node not found".to_string(),
                    ))
                }
            }
            Err(_) => Err(DistributedError::CommunicationError(
                "Failed to acquire nodes lock".to_string(),
            )),
        }
    }

    /// Get list of available nodes
    pub fn get_available_nodes(&self) -> Vec<NodeInfo> {
        match self.nodes.read() {
            Ok(nodes) => nodes
                .values()
                .filter(|n| n.status == NodeStatus::Available)
                .cloned()
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Get all registered nodes
    pub fn get_all_nodes(&self) -> Vec<NodeInfo> {
        match self.nodes.read() {
            Ok(nodes) => nodes.values().cloned().collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Get node by ID
    pub fn get_node(&self, node_id: NodeId) -> Option<NodeInfo> {
        match self.nodes.read() {
            Ok(nodes) => nodes.get(&node_id).cloned(),
            Err(_) => None,
        }
    }

    /// Get number of available nodes
    pub fn available_node_count(&self) -> usize {
        self.get_available_nodes().len()
    }

    /// Register a failure callback
    pub fn on_node_failure<F>(&self, callback: F)
    where
        F: Fn(NodeId) + Send + Sync + 'static,
    {
        if let Ok(mut callbacks) = self.failure_callbacks.write() {
            callbacks.push(Arc::new(callback));
        }
    }

    /// Record job completion for a node
    pub fn record_job_completion(
        &self,
        node_id: NodeId,
        duration: Duration,
    ) -> DistributedResult<()> {
        match self.nodes.write() {
            Ok(mut nodes) => {
                if let Some(node) = nodes.get_mut(&node_id) {
                    let total_time = node.average_job_duration * node.jobs_completed as u32;
                    node.jobs_completed += 1;
                    node.average_job_duration =
                        (total_time + duration) / node.jobs_completed as u32;
                    Ok(())
                } else {
                    Err(DistributedError::NodeFailure(
                        node_id,
                        "Node not found".to_string(),
                    ))
                }
            }
            Err(_) => Err(DistributedError::CommunicationError(
                "Failed to acquire nodes lock".to_string(),
            )),
        }
    }

    /// Select best node for a given workload
    pub fn select_best_node(&self, estimated_cost: f64) -> Option<NodeId> {
        match self.nodes.read() {
            Ok(nodes) => nodes
                .values()
                .filter(|n| n.status == NodeStatus::Available)
                .max_by(|a, b| {
                    a.processing_score()
                        .partial_cmp(&b.processing_score())
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
                .map(|n| n.id),
            Err(_) => None,
        }
    }
}

impl Drop for NodeManager {
    fn drop(&mut self) {
        self.stop_health_monitoring();
    }
}

/// A compute node that can process work chunks
pub struct ComputeNode<F: IntegrateFloat> {
    /// Node information
    info: NodeInfo,
    /// Current work queue
    work_queue: Mutex<Vec<WorkChunk<F>>>,
    /// Results buffer
    results: Mutex<Vec<ChunkResult<F>>>,
    /// Processing thread handles
    workers: Mutex<Vec<thread::JoinHandle<()>>>,
    /// Shutdown flag
    shutdown: Arc<AtomicBool>,
    /// ODE solver function
    solver_fn: Arc<dyn Fn(&WorkChunk<F>) -> IntegrateResult<ChunkResult<F>> + Send + Sync>,
}

impl<F: IntegrateFloat> ComputeNode<F> {
    /// Create a new compute node
    pub fn new<S>(info: NodeInfo, solver_fn: S) -> Self
    where
        S: Fn(&WorkChunk<F>) -> IntegrateResult<ChunkResult<F>> + Send + Sync + 'static,
    {
        Self {
            info,
            work_queue: Mutex::new(Vec::new()),
            results: Mutex::new(Vec::new()),
            workers: Mutex::new(Vec::new()),
            shutdown: Arc::new(AtomicBool::new(false)),
            solver_fn: Arc::new(solver_fn),
        }
    }

    /// Get node ID
    pub fn id(&self) -> NodeId {
        self.info.id
    }

    /// Get node status
    pub fn status(&self) -> NodeStatus {
        self.info.status
    }

    /// Submit a work chunk
    pub fn submit_work(&self, chunk: WorkChunk<F>) -> DistributedResult<()> {
        match self.work_queue.lock() {
            Ok(mut queue) => {
                queue.push(chunk);
                Ok(())
            }
            Err(_) => Err(DistributedError::ResourceExhausted(
                "Failed to acquire work queue lock".to_string(),
            )),
        }
    }

    /// Process all queued work
    pub fn process_all(&self) -> DistributedResult<Vec<ChunkResult<F>>> {
        let chunks = {
            match self.work_queue.lock() {
                Ok(mut queue) => std::mem::take(&mut *queue),
                Err(_) => {
                    return Err(DistributedError::ResourceExhausted(
                        "Failed to acquire work queue lock".to_string(),
                    ))
                }
            }
        };

        let mut results = Vec::with_capacity(chunks.len());
        for chunk in chunks {
            match (self.solver_fn)(&chunk) {
                Ok(result) => results.push(result),
                Err(e) => {
                    return Err(DistributedError::ChunkError(
                        chunk.id,
                        format!("Solver error: {}", e),
                    ))
                }
            }
        }

        Ok(results)
    }

    /// Get pending work count
    pub fn pending_work_count(&self) -> usize {
        match self.work_queue.lock() {
            Ok(queue) => queue.len(),
            Err(_) => 0,
        }
    }

    /// Collect completed results
    pub fn collect_results(&self) -> Vec<ChunkResult<F>> {
        match self.results.lock() {
            Ok(mut results) => std::mem::take(&mut *results),
            Err(_) => Vec::new(),
        }
    }

    /// Shutdown the node
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Relaxed);
    }
}

/// Builder for creating compute nodes with detected capabilities
pub struct NodeBuilder {
    address: SocketAddr,
    capabilities: Option<NodeCapabilities>,
}

impl NodeBuilder {
    /// Create a new node builder
    pub fn new(address: SocketAddr) -> Self {
        Self {
            address,
            capabilities: None,
        }
    }

    /// Set custom capabilities
    pub fn with_capabilities(mut self, capabilities: NodeCapabilities) -> Self {
        self.capabilities = Some(capabilities);
        self
    }

    /// Auto-detect capabilities
    pub fn detect_capabilities(mut self) -> Self {
        self.capabilities = Some(Self::detect_system_capabilities());
        self
    }

    /// Detect system capabilities
    fn detect_system_capabilities() -> NodeCapabilities {
        let cpu_cores = thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);

        // Estimate available memory (simplified)
        #[cfg(target_pointer_width = "32")]
        let memory_bytes = 512 * 1024 * 1024; // 512MB default for 32-bit
        #[cfg(target_pointer_width = "64")]
        let memory_bytes = 8usize * 1024 * 1024 * 1024; // 8GB default for 64-bit

        // Detect SIMD capabilities
        let simd_capabilities = Self::detect_simd();

        NodeCapabilities {
            cpu_cores,
            memory_bytes,
            has_gpu: false, // Would need GPU detection library
            gpu_memory_bytes: None,
            network_bandwidth: 1024 * 1024 * 1024, // 1 Gbps
            latency_us: 100,
            supported_precisions: vec![
                crate::distributed::types::FloatPrecision::F32,
                crate::distributed::types::FloatPrecision::F64,
            ],
            simd_capabilities,
        }
    }

    /// Detect SIMD capabilities
    fn detect_simd() -> SimdCapability {
        SimdCapability {
            has_sse: cfg!(target_feature = "sse"),
            has_sse2: cfg!(target_feature = "sse2"),
            has_avx: cfg!(target_feature = "avx"),
            has_avx2: cfg!(target_feature = "avx2"),
            has_avx512: cfg!(target_feature = "avx512f"),
            has_neon: cfg!(target_feature = "neon"),
        }
    }

    /// Build the node info
    pub fn build(self, node_id: NodeId) -> NodeInfo {
        let capabilities = self
            .capabilities
            .unwrap_or_else(Self::detect_system_capabilities);
        let mut info = NodeInfo::new(node_id, self.address);
        info.capabilities = capabilities;
        info.status = NodeStatus::Available;
        info
    }
}

/// Resource monitor for tracking node resource usage
#[derive(Debug, Clone)]
pub struct ResourceMonitor {
    /// CPU usage (0.0 to 1.0)
    pub cpu_usage: f64,
    /// Memory usage (0.0 to 1.0)
    pub memory_usage: f64,
    /// Network usage (bytes/sec)
    pub network_usage: usize,
    /// GPU usage (0.0 to 1.0), if available
    pub gpu_usage: Option<f64>,
    /// Last update time
    pub last_update: Instant,
}

impl Default for ResourceMonitor {
    fn default() -> Self {
        Self {
            cpu_usage: 0.0,
            memory_usage: 0.0,
            network_usage: 0,
            gpu_usage: None,
            last_update: Instant::now(),
        }
    }
}

impl ResourceMonitor {
    /// Update resource usage (simplified implementation)
    pub fn update(&mut self) {
        // In a real implementation, this would query system resources
        self.last_update = Instant::now();
    }

    /// Check if resources are available
    pub fn has_available_resources(&self, required_memory_fraction: f64) -> bool {
        self.memory_usage + required_memory_fraction <= 1.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr};

    fn test_address() -> SocketAddr {
        SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080)
    }

    #[test]
    fn test_node_manager_registration() {
        let manager = NodeManager::new(Duration::from_secs(30));

        let node_id = manager
            .register_node(test_address(), NodeCapabilities::default())
            .expect("Failed to register node");

        assert_eq!(manager.available_node_count(), 1);

        let node = manager.get_node(node_id);
        assert!(node.is_some());
        assert_eq!(node.map(|n| n.id), Some(node_id));
    }

    #[test]
    fn test_node_manager_deregistration() {
        let manager = NodeManager::new(Duration::from_secs(30));

        let node_id = manager
            .register_node(test_address(), NodeCapabilities::default())
            .expect("Failed to register node");

        assert_eq!(manager.available_node_count(), 1);

        manager
            .deregister_node(node_id)
            .expect("Failed to deregister node");
        assert_eq!(manager.available_node_count(), 0);
    }

    #[test]
    fn test_node_manager_heartbeat() {
        let manager = NodeManager::new(Duration::from_secs(30));

        let node_id = manager
            .register_node(test_address(), NodeCapabilities::default())
            .expect("Failed to register node");

        manager
            .update_heartbeat(node_id)
            .expect("Failed to update heartbeat");

        let node = manager.get_node(node_id).expect("Node not found");
        assert!(node.is_healthy(Duration::from_secs(60)));
    }

    #[test]
    fn test_node_builder() {
        let addr = test_address();
        let node_info = NodeBuilder::new(addr)
            .detect_capabilities()
            .build(NodeId::new(1));

        assert_eq!(node_info.id, NodeId::new(1));
        assert_eq!(node_info.address, addr);
        assert!(node_info.capabilities.cpu_cores > 0);
    }

    #[test]
    fn test_resource_monitor() {
        let mut monitor = ResourceMonitor::default();
        assert!(monitor.has_available_resources(0.5));

        monitor.memory_usage = 0.8;
        assert!(!monitor.has_available_resources(0.3));
    }
}