pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Capability-based hardware classification for performance normalization

use std::fmt;

/// Hardware classification for fuzzy matching
#[derive(Debug, Clone, PartialEq)]
pub struct HardwareClass {
    pub cpu_family: CpuFamily,
    pub core_count_class: CoreClass,
    pub cache_class: CacheClass,
}

impl HardwareClass {
    /// Create hardware class from system info
    #[must_use]
    pub fn from_system() -> Self {
        let cpu_count = num_cpus::get();

        Self {
            cpu_family: CpuFamily::detect(),
            core_count_class: CoreClass::from_count(cpu_count),
            cache_class: CacheClass::detect(),
        }
    }

    /// Calculate similarity score with another hardware class (0.0-1.0)
    #[must_use]
    pub fn similarity(&self, other: &HardwareClass) -> f64 {
        let mut score = 0.0;

        // CPU family match is most important (50% weight)
        if self.cpu_family == other.cpu_family {
            score += 0.5;
        } else if self.cpu_family.compatible_with(&other.cpu_family) {
            score += 0.25;
        }

        // Core count similarity (30% weight)
        let core_distance = self.core_count_class.distance(&other.core_count_class);
        score += 0.3 * (1.0 - (core_distance as f64 / 4.0).min(1.0));

        // Cache class similarity (20% weight)
        let cache_distance = self.cache_class.distance(&other.cache_class);
        score += 0.2 * (1.0 - (cache_distance as f64 / 3.0).min(1.0));

        score
    }

    /// Calculate performance correction factor relative to baseline
    #[must_use]
    pub fn performance_factor(&self, baseline: &HardwareClass) -> f64 {
        // Empirically derived correction factors
        let core_factor = self.core_count_class.speedup() / baseline.core_count_class.speedup();
        let cache_factor = 1.0 + (self.cache_class.mb() - baseline.cache_class.mb()) * 0.02;

        core_factor * cache_factor
    }
}

/// CPU family classification
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CpuFamily {
    IntelCore,
    IntelXeon,
    AmdRyzen,
    AmdEpyc,
    AppleSilicon,
    ArmCortex,
    Unknown,
}

impl CpuFamily {
    /// Detect CPU family from system
    #[must_use]
    pub fn detect() -> Self {
        // This would use actual CPU detection in production
        // Simplified for now
        #[cfg(target_arch = "x86_64")]
        {
            Self::IntelCore // Default for x86_64
        }
        #[cfg(target_arch = "aarch64")]
        {
            Self::AppleSilicon // Default for ARM64
        }
        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        {
            Self::Unknown
        }
    }

    /// Check if two CPU families are compatible
    #[must_use]
    pub fn compatible_with(&self, other: &CpuFamily) -> bool {
        use CpuFamily::{AmdEpyc, AmdRyzen, AppleSilicon, ArmCortex, IntelCore, IntelXeon};

        match (self, other) {
            // Intel families are compatible
            (IntelCore, IntelXeon) | (IntelXeon, IntelCore) => true,
            // AMD families are compatible
            (AmdRyzen, AmdEpyc) | (AmdEpyc, AmdRyzen) => true,
            // ARM variants are compatible
            (AppleSilicon, ArmCortex) | (ArmCortex, AppleSilicon) => true,
            // Same family is always compatible
            _ => self == other,
        }
    }
}

/// Core count classification
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CoreClass {
    Single, // 1 core
    Dual,   // 2 cores
    Quad,   // 3-4 cores
    Octa,   // 5-8 cores
    Many,   // 9+ cores
}

impl CoreClass {
    /// Create from actual core count
    #[must_use]
    pub fn from_count(count: usize) -> Self {
        match count {
            1 => Self::Single,
            2 => Self::Dual,
            3..=4 => Self::Quad,
            5..=8 => Self::Octa,
            _ => Self::Many,
        }
    }

    /// Distance between core classes
    #[must_use]
    pub fn distance(&self, other: &CoreClass) -> usize {
        use CoreClass::{Dual, Many, Octa, Quad, Single};

        let self_idx: usize = match self {
            Single => 0,
            Dual => 1,
            Quad => 2,
            Octa => 3,
            Many => 4,
        };

        let other_idx: usize = match other {
            Single => 0,
            Dual => 1,
            Quad => 2,
            Octa => 3,
            Many => 4,
        };

        self_idx.abs_diff(other_idx)
    }

    /// Expected speedup factor for parallel workloads
    #[must_use]
    pub fn speedup(&self) -> f64 {
        match self {
            Self::Single => 1.0,
            Self::Dual => 1.8,
            Self::Quad => 3.2,
            Self::Octa => 5.5,
            Self::Many => 8.0,
        }
    }

    /// Representative core count
    #[must_use]
    pub fn representative_count(&self) -> usize {
        match self {
            Self::Single => 1,
            Self::Dual => 2,
            Self::Quad => 4,
            Self::Octa => 8,
            Self::Many => 16,
        }
    }
}

/// Cache size classification
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CacheClass {
    Small,  // <4MB L3
    Medium, // 4-8MB L3
    Large,  // 8-16MB L3
    Huge,   // >16MB L3
}

impl CacheClass {
    /// Detect cache class from system
    #[must_use]
    pub fn detect() -> Self {
        // This would use actual cache detection in production
        // Simplified for now
        Self::Medium
    }

    /// Distance between cache classes
    #[must_use]
    pub fn distance(&self, other: &CacheClass) -> usize {
        use CacheClass::{Huge, Large, Medium, Small};

        let self_idx: usize = match self {
            Small => 0,
            Medium => 1,
            Large => 2,
            Huge => 3,
        };

        let other_idx: usize = match other {
            Small => 0,
            Medium => 1,
            Large => 2,
            Huge => 3,
        };

        self_idx.abs_diff(other_idx)
    }

    /// Representative cache size in MB
    #[must_use]
    pub fn mb(&self) -> f64 {
        match self {
            Self::Small => 2.0,
            Self::Medium => 6.0,
            Self::Large => 12.0,
            Self::Huge => 24.0,
        }
    }
}

impl fmt::Display for HardwareClass {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:?}/{:?}/{:?}",
            self.cpu_family, self.core_count_class, self.cache_class
        )
    }
}

/// Hardware profile for benchmarking
#[derive(Debug, Clone)]
pub struct HardwareProfile {
    pub class: HardwareClass,
    pub cpu_name: String,
    pub physical_cores: usize,
    pub logical_cores: usize,
    pub cache_sizes: CacheSizes,
    pub memory_gb: f64,
}

/// Cache size details
#[derive(Debug, Clone)]
pub struct CacheSizes {
    pub l1_data_kb: u32,
    pub l1_inst_kb: u32,
    pub l2_kb: u32,
    pub l3_kb: u32,
}

impl HardwareProfile {
    /// Create profile from current system
    #[must_use]
    pub fn from_system() -> Self {
        let logical_cores = num_cpus::get();
        let physical_cores = num_cpus::get_physical();

        Self {
            class: HardwareClass::from_system(),
            cpu_name: "Unknown CPU".to_string(), // Would use actual detection
            physical_cores,
            logical_cores,
            cache_sizes: CacheSizes {
                l1_data_kb: 32,
                l1_inst_kb: 32,
                l2_kb: 256,
                l3_kb: 8192,
            },
            memory_gb: 16.0, // Would use actual detection
        }
    }
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_core_class_from_count() {
        assert_eq!(CoreClass::from_count(1), CoreClass::Single);
        assert_eq!(CoreClass::from_count(2), CoreClass::Dual);
        assert_eq!(CoreClass::from_count(3), CoreClass::Quad);
        assert_eq!(CoreClass::from_count(4), CoreClass::Quad);
        assert_eq!(CoreClass::from_count(5), CoreClass::Octa);
        assert_eq!(CoreClass::from_count(8), CoreClass::Octa);
        assert_eq!(CoreClass::from_count(16), CoreClass::Many);
    }

    #[test]
    fn test_core_class_distance() {
        assert_eq!(CoreClass::Single.distance(&CoreClass::Single), 0);
        assert_eq!(CoreClass::Single.distance(&CoreClass::Dual), 1);
        assert_eq!(CoreClass::Single.distance(&CoreClass::Many), 4);
        assert_eq!(CoreClass::Quad.distance(&CoreClass::Octa), 1);
    }

    #[test]
    fn test_core_class_speedup() {
        assert!((CoreClass::Single.speedup() - 1.0).abs() < f64::EPSILON);
        assert!((CoreClass::Dual.speedup() - 1.8).abs() < f64::EPSILON);
        assert!(CoreClass::Octa.speedup() > CoreClass::Quad.speedup());
    }

    #[test]
    fn test_core_class_representative_count() {
        assert_eq!(CoreClass::Single.representative_count(), 1);
        assert_eq!(CoreClass::Dual.representative_count(), 2);
        assert_eq!(CoreClass::Quad.representative_count(), 4);
        assert_eq!(CoreClass::Octa.representative_count(), 8);
        assert_eq!(CoreClass::Many.representative_count(), 16);
    }

    #[test]
    fn test_cache_class_distance() {
        assert_eq!(CacheClass::Small.distance(&CacheClass::Small), 0);
        assert_eq!(CacheClass::Small.distance(&CacheClass::Medium), 1);
        assert_eq!(CacheClass::Small.distance(&CacheClass::Huge), 3);
    }

    #[test]
    fn test_cache_class_mb() {
        assert!(CacheClass::Small.mb() < CacheClass::Medium.mb());
        assert!(CacheClass::Medium.mb() < CacheClass::Large.mb());
        assert!(CacheClass::Large.mb() < CacheClass::Huge.mb());
    }

    #[test]
    fn test_cpu_family_compatibility() {
        assert!(CpuFamily::IntelCore.compatible_with(&CpuFamily::IntelXeon));
        assert!(CpuFamily::IntelXeon.compatible_with(&CpuFamily::IntelCore));
        assert!(CpuFamily::AmdRyzen.compatible_with(&CpuFamily::AmdEpyc));
        assert!(CpuFamily::AppleSilicon.compatible_with(&CpuFamily::ArmCortex));
        assert!(CpuFamily::IntelCore.compatible_with(&CpuFamily::IntelCore));
        assert!(!CpuFamily::IntelCore.compatible_with(&CpuFamily::AmdRyzen));
    }

    #[test]
    fn test_hardware_class_similarity() {
        let hw1 = HardwareClass {
            cpu_family: CpuFamily::IntelCore,
            core_count_class: CoreClass::Octa,
            cache_class: CacheClass::Medium,
        };
        let hw2 = HardwareClass {
            cpu_family: CpuFamily::IntelCore,
            core_count_class: CoreClass::Octa,
            cache_class: CacheClass::Medium,
        };
        assert!((hw1.similarity(&hw2) - 1.0).abs() < f64::EPSILON);

        let hw3 = HardwareClass {
            cpu_family: CpuFamily::AmdRyzen,
            core_count_class: CoreClass::Single,
            cache_class: CacheClass::Small,
        };
        assert!(hw1.similarity(&hw3) < 0.5);
    }

    #[test]
    fn test_hardware_class_performance_factor() {
        let baseline = HardwareClass {
            cpu_family: CpuFamily::IntelCore,
            core_count_class: CoreClass::Quad,
            cache_class: CacheClass::Medium,
        };
        let faster = HardwareClass {
            cpu_family: CpuFamily::IntelCore,
            core_count_class: CoreClass::Octa,
            cache_class: CacheClass::Large,
        };
        assert!(faster.performance_factor(&baseline) > 1.0);
    }

    #[test]
    fn test_hardware_class_from_system() {
        let hw = HardwareClass::from_system();
        // Should always return valid values
        assert!(hw.core_count_class.speedup() > 0.0);
    }

    #[test]
    fn test_hardware_profile_from_system() {
        let profile = HardwareProfile::from_system();
        assert!(profile.logical_cores > 0);
        assert!(profile.physical_cores > 0);
        assert!(profile.logical_cores >= profile.physical_cores);
    }

    #[test]
    fn test_hardware_class_display() {
        let hw = HardwareClass {
            cpu_family: CpuFamily::IntelCore,
            core_count_class: CoreClass::Octa,
            cache_class: CacheClass::Medium,
        };
        let display = format!("{}", hw);
        assert!(display.contains("IntelCore"));
        assert!(display.contains("Octa"));
        assert!(display.contains("Medium"));
    }

    #[test]
    fn test_cache_class_detect() {
        let cache = CacheClass::detect();
        // Should return Medium as default
        assert_eq!(cache, CacheClass::Medium);
    }

    #[test]
    fn test_cpu_family_detect() {
        let cpu = CpuFamily::detect();
        // Should return valid CPU family
        assert!(
            cpu != CpuFamily::Unknown
                || cfg!(not(any(target_arch = "x86_64", target_arch = "aarch64")))
        );
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn core_class_distance_symmetric(a in 0usize..5, b in 0usize..5) {
            let classes = [CoreClass::Single, CoreClass::Dual, CoreClass::Quad, CoreClass::Octa, CoreClass::Many];
            let class_a = &classes[a];
            let class_b = &classes[b];
            prop_assert_eq!(class_a.distance(class_b), class_b.distance(class_a));
        }

        #[test]
        fn cache_class_distance_symmetric(a in 0usize..4, b in 0usize..4) {
            let classes = [CacheClass::Small, CacheClass::Medium, CacheClass::Large, CacheClass::Huge];
            let class_a = &classes[a];
            let class_b = &classes[b];
            prop_assert_eq!(class_a.distance(class_b), class_b.distance(class_a));
        }

        #[test]
        fn hardware_similarity_symmetric(core_a in 0usize..5, core_b in 0usize..5) {
            let core_classes = [CoreClass::Single, CoreClass::Dual, CoreClass::Quad, CoreClass::Octa, CoreClass::Many];
            let hw1 = HardwareClass {
                cpu_family: CpuFamily::IntelCore,
                core_count_class: core_classes[core_a].clone(),
                cache_class: CacheClass::Medium,
            };
            let hw2 = HardwareClass {
                cpu_family: CpuFamily::IntelCore,
                core_count_class: core_classes[core_b].clone(),
                cache_class: CacheClass::Medium,
            };
            let sim1 = hw1.similarity(&hw2);
            let sim2 = hw2.similarity(&hw1);
            prop_assert!((sim1 - sim2).abs() < 0.001);
        }
    }
}