scirs2-cluster 0.3.4

Clustering algorithms module for SciRS2 (scirs2-cluster)
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
//! GPU memory management and allocation strategies
//!
//! This module provides efficient GPU memory management including memory pooling,
//! allocation strategies, and memory usage tracking.

use crate::error::{ClusteringError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;

/// GPU memory allocation strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MemoryStrategy {
    /// Use unified memory (CUDA/HIP)
    Unified,
    /// Explicit host-device transfers
    Explicit,
    /// Memory pooling for reuse
    Pooled { pool_size_mb: usize },
    /// Zero-copy memory (if supported)
    ZeroCopy,
    /// Adaptive strategy based on data size
    Adaptive,
}

impl Default for MemoryStrategy {
    fn default() -> Self {
        MemoryStrategy::Adaptive
    }
}

/// GPU memory pool for efficient allocation reuse
#[derive(Debug)]
pub struct GpuMemoryManager {
    /// Memory pools indexed by size ranges
    pools: HashMap<usize, Vec<GpuMemoryBlock>>,
    /// Total allocated memory
    total_allocated: usize,
    /// Peak memory usage
    peak_usage: usize,
    /// Memory alignment requirement
    alignment: usize,
    /// Maximum pool size per allocation size
    max_pool_size: usize,
    /// Memory statistics
    stats: MemoryStats,
}

/// GPU memory block for pooling
#[derive(Debug, Clone)]
pub struct GpuMemoryBlock {
    /// Device pointer
    pub device_ptr: usize,
    /// Size in bytes
    pub size: usize,
    /// Whether currently in use
    pub in_use: bool,
    /// Allocation timestamp
    pub allocated_at: Instant,
}

/// Memory usage statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MemoryStats {
    /// Total bytes allocated
    pub total_allocated: usize,
    /// Peak memory usage
    pub peak_usage: usize,
    /// Number of allocations
    pub allocation_count: usize,
    /// Number of deallocations
    pub deallocation_count: usize,
    /// Number of pool hits
    pub pool_hits: usize,
    /// Number of pool misses
    pub pool_misses: usize,
}

impl GpuMemoryManager {
    /// Create new memory manager
    pub fn new(alignment: usize, max_pool_size: usize) -> Self {
        Self {
            pools: HashMap::new(),
            total_allocated: 0,
            peak_usage: 0,
            alignment,
            max_pool_size,
            stats: MemoryStats::default(),
        }
    }

    /// Allocate memory with efficient pooling
    pub fn allocate(&mut self, size: usize) -> Result<GpuMemoryBlock> {
        let aligned_size = (size + self.alignment - 1) & !(self.alignment - 1);
        let size_class = self.get_size_class(aligned_size);

        self.stats.allocation_count += 1;

        // Try to find existing block in pool
        if let Some(pool) = self.pools.get_mut(&size_class) {
            for block in pool.iter_mut() {
                if !block.in_use && block.size >= aligned_size {
                    block.in_use = true;
                    self.stats.pool_hits += 1;
                    return Ok(GpuMemoryBlock {
                        device_ptr: block.device_ptr,
                        size: block.size,
                        in_use: true,
                        allocated_at: Instant::now(),
                    });
                }
            }
        }

        // Pool miss - allocate new block
        self.stats.pool_misses += 1;
        let device_ptr = self.allocate_device_memory(aligned_size)?;
        self.total_allocated += aligned_size;
        self.peak_usage = self.peak_usage.max(self.total_allocated);
        self.stats.total_allocated = self.total_allocated;
        self.stats.peak_usage = self.peak_usage;

        Ok(GpuMemoryBlock {
            device_ptr,
            size: aligned_size,
            in_use: true,
            allocated_at: Instant::now(),
        })
    }

    /// Return memory block to pool
    pub fn deallocate(&mut self, mut block: GpuMemoryBlock) -> Result<()> {
        block.in_use = false;
        self.stats.deallocation_count += 1;

        let size_class = self.get_size_class(block.size);

        let pool = self.pools.entry(size_class).or_insert_with(Vec::new);
        if pool.len() < self.max_pool_size {
            pool.push(block);
        } else {
            // Pool full, actually free memory
            self.free_device_memory(block.device_ptr, block.size)?;
            self.total_allocated -= block.size;
            self.stats.total_allocated = self.total_allocated;
        }

        Ok(())
    }

    /// Clear all memory pools
    pub fn clear_pools(&mut self) -> Result<()> {
        for pool in self.pools.values() {
            for block in pool {
                if !block.in_use {
                    self.free_device_memory(block.device_ptr, block.size)?;
                    self.total_allocated -= block.size;
                }
            }
        }
        self.pools.clear();
        self.stats.total_allocated = self.total_allocated;
        Ok(())
    }

    /// Get memory statistics
    pub fn get_stats(&self) -> &MemoryStats {
        &self.stats
    }

    /// Get memory efficiency (pool hit rate)
    pub fn pool_efficiency(&self) -> f64 {
        if self.stats.allocation_count == 0 {
            0.0
        } else {
            self.stats.pool_hits as f64 / self.stats.allocation_count as f64
        }
    }

    /// Get current memory usage
    pub fn current_usage(&self) -> usize {
        self.total_allocated
    }

    /// Get peak memory usage
    pub fn peak_usage(&self) -> usize {
        self.peak_usage
    }

    /// Get size class for pooling
    fn get_size_class(&self, size: usize) -> usize {
        // Round up to next power of 2 for size classing
        if size == 0 {
            return 1;
        }
        let mut class = 1;
        while class < size {
            class <<= 1;
        }
        class
    }

    /// Platform-specific device memory allocation (stub)
    fn allocate_device_memory(&self, size: usize) -> Result<usize> {
        // This is a stub implementation
        // Real implementation would call CUDA malloc, OpenCL buffer creation, etc.

        if size == 0 {
            return Err(ClusteringError::InvalidInput(
                "Cannot allocate zero bytes".to_string(),
            ));
        }

        if size > 16 * 1024 * 1024 * 1024 {
            return Err(ClusteringError::InvalidInput(
                "Allocation too large".to_string(),
            ));
        }

        // Simulate device memory allocation by returning a fake pointer
        Ok(0x1000_0000 + size) // Fake device pointer
    }

    /// Platform-specific device memory deallocation (stub)
    fn free_device_memory(&self, _device_ptr: usize, _size: usize) -> Result<()> {
        // This is a stub implementation
        // Real implementation would call CUDA free, OpenCL release, etc.
        Ok(())
    }
}

impl MemoryStats {
    /// Get allocation efficiency (deallocations / allocations)
    pub fn allocation_efficiency(&self) -> f64 {
        if self.allocation_count == 0 {
            1.0
        } else {
            self.deallocation_count as f64 / self.allocation_count as f64
        }
    }

    /// Get average allocation size
    pub fn average_allocation_size(&self) -> f64 {
        if self.allocation_count == 0 {
            0.0
        } else {
            self.total_allocated as f64 / self.allocation_count as f64
        }
    }

    /// Check if there are potential memory leaks
    pub fn has_potential_leaks(&self) -> bool {
        self.allocation_count > self.deallocation_count // Any unmatched allocation is a potential leak
    }
}

/// Memory transfer operations
#[derive(Debug, Clone)]
pub enum MemoryTransfer {
    /// Host to device transfer
    HostToDevice {
        /// Source host pointer
        host_ptr: *const u8,
        /// Destination device pointer
        device_ptr: usize,
        /// Size in bytes
        size: usize,
    },
    /// Device to host transfer
    DeviceToHost {
        /// Source device pointer
        device_ptr: usize,
        /// Destination host pointer
        host_ptr: *mut u8,
        /// Size in bytes
        size: usize,
    },
    /// Device to device transfer
    DeviceToDevice {
        /// Source device pointer
        src_device_ptr: usize,
        /// Destination device pointer
        dst_device_ptr: usize,
        /// Size in bytes
        size: usize,
    },
}

impl MemoryTransfer {
    /// Get transfer size in bytes
    pub fn size(&self) -> usize {
        match self {
            MemoryTransfer::HostToDevice { size, .. } => *size,
            MemoryTransfer::DeviceToHost { size, .. } => *size,
            MemoryTransfer::DeviceToDevice { size, .. } => *size,
        }
    }

    /// Execute the memory transfer (stub)
    pub fn execute(&self) -> Result<()> {
        // This is a stub implementation
        // Real implementation would call platform-specific transfer functions
        match self {
            MemoryTransfer::HostToDevice { .. } => {
                // Simulate host-to-device transfer
                Ok(())
            }
            MemoryTransfer::DeviceToHost { .. } => {
                // Simulate device-to-host transfer
                Ok(())
            }
            MemoryTransfer::DeviceToDevice { .. } => {
                // Simulate device-to-device transfer
                Ok(())
            }
        }
    }
}

/// Memory bandwidth monitoring
#[derive(Debug, Clone, Default)]
pub struct BandwidthMonitor {
    /// Total bytes transferred
    pub total_transferred: usize,
    /// Number of transfers
    pub transfer_count: usize,
    /// Total transfer time in microseconds
    pub total_time_us: u64,
}

impl BandwidthMonitor {
    /// Record a memory transfer
    pub fn record_transfer(&mut self, size: usize, duration_us: u64) {
        self.total_transferred += size;
        self.transfer_count += 1;
        self.total_time_us += duration_us;
    }

    /// Get average bandwidth in GB/s
    pub fn average_bandwidth_gbps(&self) -> f64 {
        if self.total_time_us == 0 {
            0.0
        } else {
            let total_gb = self.total_transferred as f64 / (1024.0 * 1024.0 * 1024.0);
            let total_seconds = self.total_time_us as f64 / 1_000_000.0;
            total_gb / total_seconds
        }
    }

    /// Get average transfer size
    pub fn average_transfer_size(&self) -> f64 {
        if self.transfer_count == 0 {
            0.0
        } else {
            self.total_transferred as f64 / self.transfer_count as f64
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_memory_manager_creation() {
        let manager = GpuMemoryManager::new(256, 10);
        assert_eq!(manager.alignment, 256);
        assert_eq!(manager.max_pool_size, 10);
        assert_eq!(manager.current_usage(), 0);
    }

    #[test]
    fn test_memory_allocation() {
        let mut manager = GpuMemoryManager::new(256, 10);

        let block = manager.allocate(1024).expect("Operation failed");
        assert!(block.size >= 1024);
        assert!(block.in_use);

        assert_eq!(manager.get_stats().allocation_count, 1);
        assert_eq!(manager.get_stats().pool_misses, 1);
    }

    #[test]
    fn test_memory_pooling() {
        let mut manager = GpuMemoryManager::new(256, 10);

        // Allocate and deallocate
        let block = manager.allocate(1024).expect("Operation failed");
        manager.deallocate(block).expect("Operation failed");

        // Second allocation should hit pool
        let _block2 = manager.allocate(1024).expect("Operation failed");
        assert!(manager.get_stats().pool_hits > 0);
    }

    #[test]
    fn test_memory_stats() {
        let stats = MemoryStats {
            allocation_count: 10,
            deallocation_count: 8,
            total_allocated: 1024,
            pool_hits: 5,
            ..Default::default()
        };

        assert_eq!(stats.allocation_efficiency(), 0.8);
        assert_eq!(stats.average_allocation_size(), 102.4);
        assert!(stats.has_potential_leaks());
    }

    #[test]
    fn test_bandwidth_monitor() {
        let mut monitor = BandwidthMonitor::default();

        // Record a 1GB transfer in 1 second
        monitor.record_transfer(1024 * 1024 * 1024, 1_000_000);

        assert_eq!(monitor.average_bandwidth_gbps(), 1.0);
        assert_eq!(monitor.average_transfer_size(), 1024.0 * 1024.0 * 1024.0);
    }

    #[test]
    fn test_memory_transfer() {
        let transfer = MemoryTransfer::HostToDevice {
            host_ptr: std::ptr::null(),
            device_ptr: 0x1000,
            size: 1024,
        };

        assert_eq!(transfer.size(), 1024);
        assert!(transfer.execute().is_ok());
    }
}