amaters-core 0.2.0

Core kernel for AmateRS - Fully Homomorphic Encrypted Database
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
//! GPU acceleration module for FHE operations
//!
//! This module provides GPU-accelerated FHE operations using CUDA and Metal backends.
//! It automatically detects available GPU hardware and falls back to CPU when needed.
//!
//! # Architecture
//!
//! - **CUDA Backend**: NVIDIA GPU acceleration on Linux/Windows (via tfhe-cuda-backend)
//! - **Metal Backend**: Apple GPU acceleration on macOS (custom implementation)
//! - **CPU Fallback**: Automatic fallback when GPU is unavailable
//!
//! # Example
//!
//! ```rust,ignore
//! use amaters_core::compute::gpu::{GpuExecutor, GpuBackend};
//!
//! let executor = GpuExecutor::new()?;
//! let backend = executor.backend();
//! println!("Using backend: {:?}", backend);
//! ```

use crate::error::{AmateRSError, ErrorContext, Result};
use parking_lot::RwLock;
use std::sync::Arc;

#[cfg(feature = "compute")]
use crate::compute::operations::{
    EncryptedBool, EncryptedU8, EncryptedU16, EncryptedU32, EncryptedU64,
};

/// GPU backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GpuBackend {
    /// NVIDIA CUDA backend (Linux, Windows)
    Cuda,
    /// Apple Metal backend (macOS)
    Metal,
    /// CPU fallback (all platforms)
    Cpu,
}

impl std::fmt::Display for GpuBackend {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GpuBackend::Cuda => write!(f, "CUDA"),
            GpuBackend::Metal => write!(f, "Metal"),
            GpuBackend::Cpu => write!(f, "CPU"),
        }
    }
}

/// GPU configuration options
#[derive(Debug, Clone)]
pub struct GpuConfig {
    /// Preferred backend (None = auto-detect)
    pub preferred_backend: Option<GpuBackend>,
    /// Device ID for multi-GPU systems
    pub device_id: usize,
    /// Enable batch processing
    pub enable_batch: bool,
    /// Batch size for operations
    pub batch_size: usize,
    /// Memory pool size in bytes (0 = auto)
    pub memory_pool_size: usize,
}

impl Default for GpuConfig {
    fn default() -> Self {
        Self {
            preferred_backend: None,
            device_id: 0,
            enable_batch: true,
            batch_size: 64,
            memory_pool_size: 0,
        }
    }
}

/// GPU device information
#[derive(Debug, Clone)]
pub struct GpuDeviceInfo {
    /// Backend type
    pub backend: GpuBackend,
    /// Device name
    pub name: String,
    /// Compute capability (CUDA) or Metal version
    pub compute_capability: String,
    /// Total memory in bytes
    pub total_memory: u64,
    /// Available memory in bytes
    pub available_memory: u64,
    /// Number of compute units
    pub compute_units: u32,
}

/// GPU executor for FHE operations
///
/// Manages GPU resources and executes FHE operations on the selected backend.
/// Automatically handles memory management, batch processing, and fallback to CPU.
#[derive(Clone)]
pub struct GpuExecutor {
    backend: GpuBackend,
    config: GpuConfig,
    device_info: Option<GpuDeviceInfo>,
    #[cfg(all(feature = "cuda", feature = "compute"))]
    cuda_context: Option<Arc<RwLock<cuda::CudaContext>>>,
    #[cfg(all(feature = "metal", feature = "compute"))]
    metal_context: Option<Arc<RwLock<metal::MetalContext>>>,
}

impl GpuExecutor {
    /// Create a new GPU executor with default configuration
    ///
    /// Automatically detects available GPU hardware and selects the best backend.
    pub fn new() -> Result<Self> {
        Self::with_config(GpuConfig::default())
    }

    /// Create a new GPU executor with custom configuration
    pub fn with_config(config: GpuConfig) -> Result<Self> {
        let backend = if let Some(preferred) = config.preferred_backend {
            // Use preferred backend if specified
            preferred
        } else {
            // Auto-detect best available backend
            Self::detect_backend()?
        };

        let device_info = Self::get_device_info(backend, config.device_id)?;

        let mut executor = Self {
            backend,
            config,
            device_info: Some(device_info),
            #[cfg(all(feature = "cuda", feature = "compute"))]
            cuda_context: None,
            #[cfg(all(feature = "metal", feature = "compute"))]
            metal_context: None,
        };

        // Initialize backend-specific context
        executor.initialize_backend()?;

        Ok(executor)
    }

    /// Detect the best available GPU backend
    fn detect_backend() -> Result<GpuBackend> {
        #[cfg(feature = "cuda")]
        {
            if Self::is_cuda_available() {
                return Ok(GpuBackend::Cuda);
            }
        }

        #[cfg(feature = "metal")]
        {
            if Self::is_metal_available() {
                return Ok(GpuBackend::Metal);
            }
        }

        // Fallback to CPU
        Ok(GpuBackend::Cpu)
    }

    /// Check if CUDA backend is available
    #[cfg(feature = "cuda")]
    fn is_cuda_available() -> bool {
        // Check for CUDA runtime and devices
        #[cfg(feature = "compute")]
        {
            cuda::detect_cuda_devices().is_ok()
        }
        #[cfg(not(feature = "compute"))]
        {
            false
        }
    }

    #[cfg(not(feature = "cuda"))]
    fn is_cuda_available() -> bool {
        false
    }

    /// Check if Metal backend is available
    #[cfg(feature = "metal")]
    fn is_metal_available() -> bool {
        // Metal is only available on macOS
        #[cfg(target_os = "macos")]
        {
            #[cfg(feature = "compute")]
            {
                metal::detect_metal_devices().is_ok()
            }
            #[cfg(not(feature = "compute"))]
            {
                false
            }
        }
        #[cfg(not(target_os = "macos"))]
        {
            false
        }
    }

    #[cfg(not(feature = "metal"))]
    fn is_metal_available() -> bool {
        false
    }

    /// Get device information for the selected backend
    fn get_device_info(backend: GpuBackend, device_id: usize) -> Result<GpuDeviceInfo> {
        match backend {
            #[cfg(feature = "cuda")]
            GpuBackend::Cuda => {
                #[cfg(feature = "compute")]
                {
                    cuda::get_device_info(device_id)
                }
                #[cfg(not(feature = "compute"))]
                {
                    Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
                        "CUDA backend requires 'compute' feature".to_string(),
                    )))
                }
            }

            #[cfg(feature = "metal")]
            GpuBackend::Metal => {
                #[cfg(feature = "compute")]
                {
                    metal::get_device_info(device_id)
                }
                #[cfg(not(feature = "compute"))]
                {
                    Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
                        "Metal backend requires 'compute' feature".to_string(),
                    )))
                }
            }

            GpuBackend::Cpu => {
                let cpus = std::thread::available_parallelism()
                    .map(|n| n.get())
                    .unwrap_or(1);
                Ok(GpuDeviceInfo {
                    backend: GpuBackend::Cpu,
                    name: "CPU".to_string(),
                    compute_capability: format!("{} cores", cpus),
                    total_memory: 0,
                    available_memory: 0,
                    compute_units: cpus as u32,
                })
            }

            #[allow(unreachable_patterns)]
            _ => Err(AmateRSError::Configuration(ErrorContext::new(format!(
                "Backend {} is not available (feature not enabled)",
                backend
            )))),
        }
    }

    /// Initialize backend-specific context
    fn initialize_backend(&mut self) -> Result<()> {
        match self.backend {
            #[cfg(all(feature = "cuda", feature = "compute"))]
            GpuBackend::Cuda => {
                let context =
                    cuda::CudaContext::new(self.config.device_id, self.config.memory_pool_size)?;
                self.cuda_context = Some(Arc::new(RwLock::new(context)));
                Ok(())
            }

            #[cfg(all(feature = "metal", feature = "compute"))]
            GpuBackend::Metal => {
                let context =
                    metal::MetalContext::new(self.config.device_id, self.config.memory_pool_size)?;
                self.metal_context = Some(Arc::new(RwLock::new(context)));
                Ok(())
            }

            GpuBackend::Cpu => {
                // CPU backend doesn't need initialization
                Ok(())
            }

            #[allow(unreachable_patterns)]
            _ => Err(AmateRSError::Configuration(ErrorContext::new(format!(
                "Cannot initialize backend {} (feature not enabled)",
                self.backend
            )))),
        }
    }

    /// Get the current backend
    pub fn backend(&self) -> GpuBackend {
        self.backend
    }

    /// Get device information
    pub fn device_info(&self) -> Option<&GpuDeviceInfo> {
        self.device_info.as_ref()
    }

    /// Get configuration
    pub fn config(&self) -> &GpuConfig {
        &self.config
    }

    /// Check if GPU acceleration is enabled
    pub fn is_gpu_enabled(&self) -> bool {
        !matches!(self.backend, GpuBackend::Cpu)
    }

    /// Execute FHE operation with GPU acceleration
    ///
    /// This method automatically routes the operation to the appropriate backend
    /// and handles memory transfers between CPU and GPU.
    #[cfg(feature = "compute")]
    pub fn execute_operation<F, R>(&self, operation: F) -> Result<R>
    where
        F: FnOnce() -> Result<R> + Send,
        R: Send,
    {
        match self.backend {
            #[cfg(feature = "cuda")]
            GpuBackend::Cuda => {
                #[cfg(feature = "compute")]
                {
                    if let Some(context) = &self.cuda_context {
                        let ctx = context.read();
                        ctx.execute_operation(operation)
                    } else {
                        Err(AmateRSError::GpuError(ErrorContext::new(
                            "CUDA context not initialized".to_string(),
                        )))
                    }
                }
                #[cfg(not(feature = "compute"))]
                {
                    Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
                        "CUDA backend requires 'compute' feature".to_string(),
                    )))
                }
            }

            #[cfg(feature = "metal")]
            GpuBackend::Metal => {
                #[cfg(feature = "compute")]
                {
                    if let Some(context) = &self.metal_context {
                        let ctx = context.read();
                        ctx.execute_operation(operation)
                    } else {
                        Err(AmateRSError::GpuError(ErrorContext::new(
                            "Metal context not initialized".to_string(),
                        )))
                    }
                }
                #[cfg(not(feature = "compute"))]
                {
                    Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
                        "Metal backend requires 'compute' feature".to_string(),
                    )))
                }
            }

            GpuBackend::Cpu => {
                // Execute on CPU directly
                operation()
            }

            #[allow(unreachable_patterns)]
            _ => Err(AmateRSError::Configuration(ErrorContext::new(format!(
                "Backend {} is not available",
                self.backend
            )))),
        }
    }

    /// Stub implementation when compute feature is disabled
    #[cfg(not(feature = "compute"))]
    pub fn execute_operation<F, R>(&self, _operation: F) -> Result<R>
    where
        F: FnOnce() -> Result<R> + Send,
        R: Send,
    {
        Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
            "FHE compute feature is not enabled".to_string(),
        )))
    }

    /// Execute batch of FHE operations with GPU acceleration
    #[cfg(feature = "compute")]
    pub fn execute_batch<F, R>(&self, operations: Vec<F>) -> Result<Vec<R>>
    where
        F: FnOnce() -> Result<R> + Send,
        R: Send,
    {
        if !self.config.enable_batch || operations.is_empty() {
            return operations
                .into_iter()
                .map(|op| self.execute_operation(op))
                .collect();
        }

        match self.backend {
            #[cfg(feature = "cuda")]
            GpuBackend::Cuda => {
                #[cfg(feature = "compute")]
                {
                    if let Some(context) = &self.cuda_context {
                        let ctx = context.read();
                        ctx.execute_batch(operations, self.config.batch_size)
                    } else {
                        Err(AmateRSError::GpuError(ErrorContext::new(
                            "CUDA context not initialized".to_string(),
                        )))
                    }
                }
                #[cfg(not(feature = "compute"))]
                {
                    Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
                        "CUDA backend requires 'compute' feature".to_string(),
                    )))
                }
            }

            #[cfg(feature = "metal")]
            GpuBackend::Metal => {
                #[cfg(feature = "compute")]
                {
                    if let Some(context) = &self.metal_context {
                        let ctx = context.read();
                        ctx.execute_batch(operations, self.config.batch_size)
                    } else {
                        Err(AmateRSError::GpuError(ErrorContext::new(
                            "Metal context not initialized".to_string(),
                        )))
                    }
                }
                #[cfg(not(feature = "compute"))]
                {
                    Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
                        "Metal backend requires 'compute' feature".to_string(),
                    )))
                }
            }

            GpuBackend::Cpu => {
                // Execute batch on CPU using rayon if available
                #[cfg(feature = "parallel")]
                {
                    use rayon::prelude::*;
                    operations.into_par_iter().map(|op| op()).collect()
                }
                #[cfg(not(feature = "parallel"))]
                {
                    operations.into_iter().map(|op| op()).collect()
                }
            }

            #[allow(unreachable_patterns)]
            _ => Err(AmateRSError::Configuration(ErrorContext::new(format!(
                "Backend {} is not available",
                self.backend
            )))),
        }
    }

    /// Stub implementation when compute feature is disabled
    #[cfg(not(feature = "compute"))]
    pub fn execute_batch<F, R>(&self, _operations: Vec<F>) -> Result<Vec<R>>
    where
        F: FnOnce() -> Result<R> + Send,
        R: Send,
    {
        Err(AmateRSError::FeatureNotEnabled(ErrorContext::new(
            "FHE compute feature is not enabled".to_string(),
        )))
    }
}

impl Default for GpuExecutor {
    fn default() -> Self {
        Self::new().expect("Failed to create default GPU executor")
    }
}

impl std::fmt::Debug for GpuExecutor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GpuExecutor")
            .field("backend", &self.backend)
            .field("config", &self.config)
            .field("device_info", &self.device_info)
            .finish()
    }
}

/// GPU device detection module
///
/// Provides real hardware detection using platform-specific tools:
/// - macOS: `system_profiler SPDisplaysDataType` and `sysctl hw.memsize`
/// - Linux: `nvidia-smi` and sysfs fallbacks
mod detection {
    use super::{GpuBackend, GpuDeviceInfo};
    use std::process::Command;

    /// Detect macOS GPU via system_profiler
    #[cfg(target_os = "macos")]
    pub fn detect_macos_gpu() -> Option<GpuDeviceInfo> {
        let output = Command::new("system_profiler")
            .arg("SPDisplaysDataType")
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }
        let text = String::from_utf8_lossy(&output.stdout);
        let mut info = parse_system_profiler(&text)?;

        // For Apple Silicon (unified memory), get total system memory via sysctl
        if info.name.starts_with("Apple") {
            if let Some(mem) = get_macos_system_memory() {
                info.total_memory = mem;
                // Estimate ~90% available (conservative)
                info.available_memory = mem * 9 / 10;
            }
        }

        Some(info)
    }

    /// Get macOS system memory via `sysctl hw.memsize`
    #[cfg(target_os = "macos")]
    fn get_macos_system_memory() -> Option<u64> {
        let output = Command::new("sysctl")
            .arg("-n")
            .arg("hw.memsize")
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }
        let text = String::from_utf8_lossy(&output.stdout);
        text.trim().parse::<u64>().ok()
    }

    /// Detect NVIDIA GPU on Linux via nvidia-smi
    #[cfg(target_os = "linux")]
    pub fn detect_nvidia_gpu(device_id: usize) -> Option<GpuDeviceInfo> {
        let output = Command::new("nvidia-smi")
            .args([
                "--query-gpu=name,memory.total,memory.free",
                "--format=csv,noheader,nounits",
            ])
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }
        let text = String::from_utf8_lossy(&output.stdout);
        let devices = parse_nvidia_smi(&text);
        devices.into_iter().nth(device_id)
    }

    /// Detect NVIDIA GPU count on Linux via nvidia-smi
    #[cfg(target_os = "linux")]
    pub fn detect_nvidia_device_count() -> Option<usize> {
        let output = Command::new("nvidia-smi")
            .args(["--query-gpu=name", "--format=csv,noheader"])
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }
        let text = String::from_utf8_lossy(&output.stdout);
        let count = text.lines().filter(|l| !l.trim().is_empty()).count();
        if count > 0 { Some(count) } else { None }
    }

    /// Linux sysfs fallback: detect NVIDIA devices via /sys/class/drm
    #[cfg(target_os = "linux")]
    pub fn detect_nvidia_sysfs() -> Vec<GpuDeviceInfo> {
        let mut devices = Vec::new();
        let drm_path = std::path::Path::new("/sys/class/drm");
        if !drm_path.exists() {
            return devices;
        }

        let entries = match std::fs::read_dir(drm_path) {
            Ok(e) => e,
            Err(_) => return devices,
        };

        for entry in entries.flatten() {
            let name = entry.file_name();
            let name_str = name.to_string_lossy();
            if !name_str.starts_with("card") || name_str.contains('-') {
                continue;
            }

            let vendor_path = entry.path().join("device/vendor");
            if let Ok(vendor) = std::fs::read_to_string(&vendor_path) {
                let vendor_trimmed = vendor.trim();
                // 0x10de = NVIDIA
                if vendor_trimmed == "0x10de" {
                    let device_name = read_nvidia_proc_name(devices.len())
                        .unwrap_or_else(|| format!("NVIDIA GPU (card {})", name_str));

                    devices.push(GpuDeviceInfo {
                        backend: GpuBackend::Cuda,
                        name: device_name,
                        compute_capability: "unknown".to_string(),
                        total_memory: 0,
                        available_memory: 0,
                        compute_units: 0,
                    });
                }
            }
        }

        devices
    }

    /// Try to read NVIDIA GPU name from /proc/driver/nvidia/gpus/*/information
    #[cfg(target_os = "linux")]
    fn read_nvidia_proc_name(index: usize) -> Option<String> {
        let nvidia_path = std::path::Path::new("/proc/driver/nvidia/gpus");
        if !nvidia_path.exists() {
            return None;
        }

        let entries: Vec<_> = std::fs::read_dir(nvidia_path).ok()?.flatten().collect();

        let entry = entries.get(index)?;
        let info_path = entry.path().join("information");
        let content = std::fs::read_to_string(info_path).ok()?;

        for line in content.lines() {
            if let Some(stripped) = line.strip_prefix("Model:") {
                return Some(stripped.trim().to_string());
            }
        }

        None
    }

    /// Parse `system_profiler SPDisplaysDataType` output
    ///
    /// Extracts GPU name, compute units, and memory information from the
    /// macOS system_profiler output.
    pub fn parse_system_profiler(text: &str) -> Option<GpuDeviceInfo> {
        let mut chipset_model: Option<String> = None;
        let mut total_cores: Option<u32> = None;
        let mut vram_bytes: Option<u64> = None;
        let mut is_apple_silicon = false;

        for line in text.lines() {
            let trimmed = line.trim();

            // Extract chipset model name
            if let Some(value) = trimmed.strip_prefix("Chipset Model:") {
                chipset_model = Some(value.trim().to_string());
                if value.trim().starts_with("Apple") {
                    is_apple_silicon = true;
                }
            }

            // Extract total number of GPU cores
            if let Some(value) = trimmed.strip_prefix("Total Number of Cores:") {
                total_cores = value.trim().parse::<u32>().ok();
            }

            // Extract VRAM (for discrete GPUs)
            if trimmed.starts_with("VRAM") {
                // Formats: "VRAM (Total): 8 GB", "VRAM (Dynamic, Max): 1536 MB"
                if let Some(colon_pos) = trimmed.find(':') {
                    let value_part = trimmed[colon_pos + 1..].trim();
                    vram_bytes = parse_memory_string(value_part);
                }
            }
        }

        let name = chipset_model?;

        let compute_capability = if is_apple_silicon {
            "Metal 3".to_string()
        } else if name.contains("Intel") {
            "Metal 2".to_string()
        } else {
            "Metal".to_string()
        };

        let compute_units = total_cores.unwrap_or(0);

        // For Apple Silicon, memory will be set later from sysctl
        // For discrete GPUs, use VRAM
        let total_memory = vram_bytes.unwrap_or(0);
        let available_memory = if total_memory > 0 {
            total_memory * 9 / 10
        } else {
            0
        };

        Some(GpuDeviceInfo {
            backend: GpuBackend::Metal,
            name,
            compute_capability,
            total_memory,
            available_memory,
            compute_units,
        })
    }

    /// Parse a memory string like "8 GB", "1536 MB", "16384 MB" into bytes
    fn parse_memory_string(s: &str) -> Option<u64> {
        let s = s.trim();
        let parts: Vec<&str> = s.split_whitespace().collect();
        if parts.len() < 2 {
            return None;
        }

        let value = parts[0].replace(',', "").parse::<u64>().ok()?;
        let unit = parts[1].to_uppercase();

        match unit.as_str() {
            "GB" => Some(value * 1_073_741_824),
            "MB" => Some(value * 1_048_576),
            "KB" => Some(value * 1024),
            "TB" => Some(value * 1_099_511_627_776),
            _ => None,
        }
    }

    /// Parse nvidia-smi CSV output
    ///
    /// Expected input format (from `--format=csv,noheader,nounits`):
    /// ```text
    /// NVIDIA GeForce RTX 4090, 24564, 23456
    /// ```
    ///
    /// Each line: name, total_memory_mb, free_memory_mb
    pub fn parse_nvidia_smi(text: &str) -> Vec<GpuDeviceInfo> {
        let mut devices = Vec::new();

        for line in text.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }

            let parts: Vec<&str> = trimmed.splitn(3, ',').collect();
            if parts.len() < 3 {
                continue;
            }

            let name = parts[0].trim().to_string();
            let total_mb = match parts[1].trim().parse::<u64>() {
                Ok(v) => v,
                Err(_) => continue,
            };
            let free_mb = match parts[2].trim().parse::<u64>() {
                Ok(v) => v,
                Err(_) => continue,
            };

            // Convert MB to bytes
            let total_memory = total_mb * 1_048_576;
            let available_memory = free_mb * 1_048_576;

            devices.push(GpuDeviceInfo {
                backend: GpuBackend::Cuda,
                name,
                compute_capability: "unknown".to_string(),
                total_memory,
                available_memory,
                compute_units: 0,
            });
        }

        devices
    }
}

/// CUDA backend implementation
#[cfg(all(feature = "cuda", feature = "compute"))]
mod cuda {
    use super::*;

    /// CUDA context for GPU operations
    pub struct CudaContext {
        device_id: usize,
        memory_pool_size: usize,
    }

    impl CudaContext {
        pub fn new(device_id: usize, memory_pool_size: usize) -> Result<Self> {
            // Initialize CUDA context
            // Note: tfhe-cuda-backend handles context initialization internally
            Ok(Self {
                device_id,
                memory_pool_size,
            })
        }

        pub fn execute_operation<F, R>(&self, operation: F) -> Result<R>
        where
            F: FnOnce() -> Result<R> + Send,
            R: Send,
        {
            // CUDA operations are executed directly by tfhe-rs when GPU is enabled
            // The tfhe-cuda-backend is automatically used when available
            operation()
        }

        pub fn execute_batch<F, R>(&self, operations: Vec<F>, batch_size: usize) -> Result<Vec<R>>
        where
            F: FnOnce() -> Result<R> + Send,
            R: Send,
        {
            // Process operations in batches, consuming the Vec
            let mut results = Vec::with_capacity(operations.len());
            let mut iter = operations.into_iter().peekable();

            while iter.peek().is_some() {
                let batch: Vec<F> = iter.by_ref().take(batch_size).collect();

                #[cfg(feature = "parallel")]
                {
                    use rayon::prelude::*;
                    let chunk_results: Result<Vec<_>> =
                        batch.into_par_iter().map(|op| op()).collect();
                    results.extend(chunk_results?);
                }
                #[cfg(not(feature = "parallel"))]
                {
                    for op in batch {
                        results.push(op()?);
                    }
                }
            }

            Ok(results)
        }
    }

    /// Detect available CUDA devices
    ///
    /// On Linux, tries nvidia-smi first, then falls back to sysfs detection.
    pub fn detect_cuda_devices() -> Result<Vec<usize>> {
        #[cfg(target_os = "linux")]
        {
            // Try nvidia-smi first
            if let Some(count) = detection::detect_nvidia_device_count() {
                return Ok((0..count).collect());
            }

            // Fallback to sysfs detection
            let sysfs_devices = detection::detect_nvidia_sysfs();
            if !sysfs_devices.is_empty() {
                return Ok((0..sysfs_devices.len()).collect());
            }
        }

        // Fallback: assume device 0 is available when cuda feature is enabled
        Ok(vec![0])
    }

    /// Get CUDA device information
    ///
    /// Attempts real detection via nvidia-smi on Linux, with sysfs fallback.
    /// Returns a placeholder if all detection methods fail.
    pub fn get_device_info(device_id: usize) -> Result<GpuDeviceInfo> {
        // Try real detection on Linux
        #[cfg(target_os = "linux")]
        {
            // Try nvidia-smi first
            if let Some(info) = detection::detect_nvidia_gpu(device_id) {
                return Ok(info);
            }

            // Try sysfs fallback
            let sysfs_devices = detection::detect_nvidia_sysfs();
            if let Some(info) = sysfs_devices.into_iter().nth(device_id) {
                return Ok(info);
            }
        }

        // Fallback to placeholder
        Ok(cuda_placeholder(device_id))
    }

    /// Generate a placeholder CUDA device info when detection fails
    fn cuda_placeholder(device_id: usize) -> GpuDeviceInfo {
        GpuDeviceInfo {
            backend: GpuBackend::Cuda,
            name: format!("CUDA Device {}", device_id),
            compute_capability: "unknown".to_string(),
            total_memory: 0,
            available_memory: 0,
            compute_units: 0,
        }
    }
}

/// Metal backend implementation
#[cfg(all(feature = "metal", feature = "compute", target_os = "macos"))]
mod metal {
    use super::*;

    /// Metal context for GPU operations
    pub struct MetalContext {
        device_id: usize,
        memory_pool_size: usize,
    }

    impl MetalContext {
        pub fn new(device_id: usize, memory_pool_size: usize) -> Result<Self> {
            // Initialize Metal context
            // This would create Metal device, command queue, etc.
            Ok(Self {
                device_id,
                memory_pool_size,
            })
        }

        pub fn execute_operation<F, R>(&self, operation: F) -> Result<R>
        where
            F: FnOnce() -> Result<R> + Send,
            R: Send,
        {
            // Metal operations would be executed here
            // For now, we execute on CPU as Metal backend is not yet implemented
            operation()
        }

        pub fn execute_batch<F, R>(&self, operations: Vec<F>, batch_size: usize) -> Result<Vec<R>>
        where
            F: FnOnce() -> Result<R> + Send,
            R: Send,
        {
            // Process operations in batches, consuming the Vec
            let mut results = Vec::with_capacity(operations.len());
            let mut iter = operations.into_iter().peekable();

            while iter.peek().is_some() {
                let batch: Vec<F> = iter.by_ref().take(batch_size).collect();

                #[cfg(feature = "parallel")]
                {
                    use rayon::prelude::*;
                    let chunk_results: Result<Vec<_>> =
                        batch.into_par_iter().map(|op| op()).collect();
                    results.extend(chunk_results?);
                }
                #[cfg(not(feature = "parallel"))]
                {
                    for op in batch {
                        results.push(op()?);
                    }
                }
            }

            Ok(results)
        }
    }

    /// Detect available Metal devices
    ///
    /// On macOS, runs system_profiler to detect GPU hardware.
    pub fn detect_metal_devices() -> Result<Vec<usize>> {
        if let Some(_info) = detection::detect_macos_gpu() {
            Ok(vec![0])
        } else {
            // Fallback: assume device 0 on macOS (Metal is always available)
            Ok(vec![0])
        }
    }

    /// Get Metal device information
    ///
    /// Attempts real detection via system_profiler on macOS.
    /// Returns a placeholder if detection fails.
    pub fn get_device_info(device_id: usize) -> Result<GpuDeviceInfo> {
        if device_id == 0 {
            if let Some(info) = detection::detect_macos_gpu() {
                return Ok(info);
            }
        }

        // Fallback to placeholder
        Ok(metal_placeholder(device_id))
    }

    /// Generate a placeholder Metal device info when detection fails
    fn metal_placeholder(device_id: usize) -> GpuDeviceInfo {
        GpuDeviceInfo {
            backend: GpuBackend::Metal,
            name: format!("Apple Metal Device {}", device_id),
            compute_capability: "Metal".to_string(),
            total_memory: 0,
            available_memory: 0,
            compute_units: 0,
        }
    }
}

/// Stub Metal module for non-macOS platforms
#[cfg(all(feature = "metal", feature = "compute", not(target_os = "macos")))]
mod metal {
    use super::*;

    pub struct MetalContext;

    impl MetalContext {
        pub fn new(_device_id: usize, _memory_pool_size: usize) -> Result<Self> {
            Err(AmateRSError::GpuError(ErrorContext::new(
                "Metal is only available on macOS".to_string(),
            )))
        }

        pub fn execute_operation<F, R>(&self, _operation: F) -> Result<R>
        where
            F: FnOnce() -> Result<R> + Send,
            R: Send,
        {
            Err(AmateRSError::GpuError(ErrorContext::new(
                "Metal is only available on macOS".to_string(),
            )))
        }

        pub fn execute_batch<F, R>(&self, _operations: Vec<F>, _batch_size: usize) -> Result<Vec<R>>
        where
            F: FnOnce() -> Result<R> + Send,
            R: Send,
        {
            Err(AmateRSError::GpuError(ErrorContext::new(
                "Metal is only available on macOS".to_string(),
            )))
        }
    }

    pub fn detect_metal_devices() -> Result<Vec<usize>> {
        Err(AmateRSError::GpuError(ErrorContext::new(
            "Metal is only available on macOS".to_string(),
        )))
    }

    pub fn get_device_info(_device_id: usize) -> Result<GpuDeviceInfo> {
        Err(AmateRSError::GpuError(ErrorContext::new(
            "Metal is only available on macOS".to_string(),
        )))
    }
}

#[cfg(all(test, feature = "compute"))]
mod tests {
    use super::*;

    #[test]
    fn test_gpu_backend_display() {
        assert_eq!(format!("{}", GpuBackend::Cuda), "CUDA");
        assert_eq!(format!("{}", GpuBackend::Metal), "Metal");
        assert_eq!(format!("{}", GpuBackend::Cpu), "CPU");
    }

    #[test]
    fn test_gpu_config_default() {
        let config = GpuConfig::default();
        assert_eq!(config.preferred_backend, None);
        assert_eq!(config.device_id, 0);
        assert!(config.enable_batch);
        assert_eq!(config.batch_size, 64);
        assert_eq!(config.memory_pool_size, 0);
    }

    #[test]
    fn test_gpu_executor_creation() -> Result<()> {
        let executor = GpuExecutor::new()?;
        assert!(matches!(
            executor.backend(),
            GpuBackend::Cuda | GpuBackend::Metal | GpuBackend::Cpu
        ));
        Ok(())
    }

    #[test]
    fn test_gpu_executor_with_cpu_fallback() -> Result<()> {
        let config = GpuConfig {
            preferred_backend: Some(GpuBackend::Cpu),
            ..Default::default()
        };
        let executor = GpuExecutor::with_config(config)?;
        assert_eq!(executor.backend(), GpuBackend::Cpu);
        assert!(!executor.is_gpu_enabled());
        Ok(())
    }

    #[test]
    fn test_device_info() -> Result<()> {
        let executor = GpuExecutor::new()?;
        let info = executor.device_info();
        assert!(info.is_some());

        if let Some(info) = info {
            assert!(!info.name.is_empty());
            assert!(!info.compute_capability.is_empty());
        }

        Ok(())
    }

    #[test]
    fn test_execute_operation_cpu() -> Result<()> {
        let config = GpuConfig {
            preferred_backend: Some(GpuBackend::Cpu),
            ..Default::default()
        };
        let executor = GpuExecutor::with_config(config)?;

        let result = executor.execute_operation(|| Ok(42))?;
        assert_eq!(result, 42);

        Ok(())
    }

    #[cfg(feature = "parallel")]
    #[test]
    fn test_execute_batch_cpu() -> Result<()> {
        let config = GpuConfig {
            preferred_backend: Some(GpuBackend::Cpu),
            enable_batch: true,
            batch_size: 4,
            ..Default::default()
        };
        let executor = GpuExecutor::with_config(config)?;

        let operations: Vec<_> = (0..10).map(|i| move || Ok(i * 2)).collect();

        let results = executor.execute_batch(operations)?;
        assert_eq!(results.len(), 10);
        assert_eq!(results, vec![0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);

        Ok(())
    }

    // ---- GPU detection parsing tests ----

    #[test]
    fn test_parse_system_profiler_m1() {
        let output = "\
Graphics/Displays:

    Apple M1:

      Chipset Model: Apple M1
      Type: GPU
      Bus: Built-In
      Total Number of Cores: 8
      Vendor: Apple (0x106b)
      Metal Support: Metal 3
";
        let info = detection::parse_system_profiler(output);
        assert!(info.is_some());
        let info = info.expect("should parse");
        assert_eq!(info.name, "Apple M1");
        assert_eq!(info.compute_units, 8);
        assert_eq!(info.backend, GpuBackend::Metal);
        assert_eq!(info.compute_capability, "Metal 3");
    }

    #[test]
    fn test_parse_system_profiler_m2_pro() {
        let output = "\
Graphics/Displays:

    Apple M2 Pro:

      Chipset Model: Apple M2 Pro
      Type: GPU
      Bus: Built-In
      Total Number of Cores: 19
      Vendor: Apple (0x106b)
      Metal Support: Metal 3
";
        let info = detection::parse_system_profiler(output);
        assert!(info.is_some());
        let info = info.expect("should parse");
        assert_eq!(info.name, "Apple M2 Pro");
        assert_eq!(info.compute_units, 19);
        assert_eq!(info.compute_capability, "Metal 3");
    }

    #[test]
    fn test_parse_system_profiler_m3_max() {
        let output = "\
Graphics/Displays:

    Apple M3 Max:

      Chipset Model: Apple M3 Max
      Type: GPU
      Bus: Built-In
      Total Number of Cores: 40
      Vendor: Apple (0x106b)
      Metal Support: Metal 3
";
        let info = detection::parse_system_profiler(output);
        assert!(info.is_some());
        let info = info.expect("should parse");
        assert_eq!(info.name, "Apple M3 Max");
        assert_eq!(info.compute_units, 40);
        assert_eq!(info.compute_capability, "Metal 3");
    }

    #[test]
    fn test_parse_system_profiler_intel_gpu() {
        let output = "\
Graphics/Displays:

    Intel Iris Plus Graphics 655:

      Chipset Model: Intel Iris Plus Graphics 655
      Type: GPU
      Bus: Built-In
      VRAM (Dynamic, Max): 1536 MB
      Vendor: Intel (0x8086)
      Device ID: 0x3ea5
      Metal Support: Metal 2
";
        let info = detection::parse_system_profiler(output);
        assert!(info.is_some());
        let info = info.expect("should parse");
        assert_eq!(info.name, "Intel Iris Plus Graphics 655");
        assert_eq!(info.compute_capability, "Metal 2");
        // 1536 MB = 1536 * 1048576 = 1610612736
        assert_eq!(info.total_memory, 1_610_612_736);
        assert!(info.available_memory > 0);
        assert_eq!(info.compute_units, 0); // Intel GPUs don't report cores this way
    }

    #[test]
    fn test_parse_system_profiler_empty() {
        let output = "";
        let info = detection::parse_system_profiler(output);
        assert!(info.is_none());
    }

    #[test]
    fn test_parse_system_profiler_no_gpu_section() {
        let output = "\
Graphics/Displays:

    No GPU found.
";
        let info = detection::parse_system_profiler(output);
        assert!(info.is_none());
    }

    #[test]
    fn test_parse_nvidia_smi_single() {
        let output = "NVIDIA GeForce RTX 4090, 24564, 23456\n";
        let devices = detection::parse_nvidia_smi(output);
        assert_eq!(devices.len(), 1);
        assert_eq!(devices[0].name, "NVIDIA GeForce RTX 4090");
        assert_eq!(devices[0].total_memory, 24564 * 1_048_576);
        assert_eq!(devices[0].available_memory, 23456 * 1_048_576);
        assert_eq!(devices[0].backend, GpuBackend::Cuda);
    }

    #[test]
    fn test_parse_nvidia_smi_multi() {
        let output = "\
NVIDIA GeForce RTX 4090, 24564, 23456
NVIDIA A100-SXM4-80GB, 81920, 79000
";
        let devices = detection::parse_nvidia_smi(output);
        assert_eq!(devices.len(), 2);
        assert_eq!(devices[0].name, "NVIDIA GeForce RTX 4090");
        assert_eq!(devices[0].total_memory, 24564 * 1_048_576);
        assert_eq!(devices[1].name, "NVIDIA A100-SXM4-80GB");
        assert_eq!(devices[1].total_memory, 81920 * 1_048_576);
        assert_eq!(devices[1].available_memory, 79000 * 1_048_576);
    }

    #[test]
    fn test_parse_nvidia_smi_empty() {
        let output = "";
        let devices = detection::parse_nvidia_smi(output);
        assert!(devices.is_empty());
    }

    #[test]
    fn test_parse_nvidia_smi_malformed() {
        let output = "\
this is not valid csv data
also garbage
,,
just-one-field
name, not_a_number, 123
name, 123, not_a_number
";
        let devices = detection::parse_nvidia_smi(output);
        assert!(devices.is_empty());
    }

    #[test]
    fn test_gpu_device_info_fields() {
        let info = GpuDeviceInfo {
            backend: GpuBackend::Cuda,
            name: "Test GPU".to_string(),
            compute_capability: "8.9".to_string(),
            total_memory: 16_000_000_000,
            available_memory: 15_000_000_000,
            compute_units: 128,
        };
        assert_eq!(info.backend, GpuBackend::Cuda);
        assert_eq!(info.name, "Test GPU");
        assert_eq!(info.compute_capability, "8.9");
        assert_eq!(info.total_memory, 16_000_000_000);
        assert_eq!(info.available_memory, 15_000_000_000);
        assert_eq!(info.compute_units, 128);
    }

    #[test]
    fn test_fallback_to_placeholder_cuda() {
        // Parsing empty nvidia-smi output returns empty vec,
        // so the caller would fall back to placeholder
        let devices = detection::parse_nvidia_smi("");
        assert!(devices.is_empty());

        // Malformed data also falls back
        let devices = detection::parse_nvidia_smi("garbage data here");
        assert!(devices.is_empty());
    }

    #[test]
    fn test_fallback_to_placeholder_metal() {
        // Empty system_profiler output returns None,
        // so the caller would fall back to placeholder
        let info = detection::parse_system_profiler("");
        assert!(info.is_none());

        // No chipset model also returns None
        let info = detection::parse_system_profiler("Graphics/Displays:\n    No data\n");
        assert!(info.is_none());
    }

    #[test]
    fn test_detect_on_current_platform() {
        // This test runs real detection on the current platform
        #[cfg(target_os = "macos")]
        {
            let info = detection::detect_macos_gpu();
            // On macOS, we should always detect a GPU
            assert!(info.is_some(), "should detect GPU on macOS");
            let info = info.expect("GPU detected");
            assert!(!info.name.is_empty());
            assert_eq!(info.backend, GpuBackend::Metal);
            // Apple Silicon should have unified memory > 0
            if info.name.starts_with("Apple") {
                assert!(info.total_memory > 0, "Apple Silicon should report memory");
                assert!(info.compute_units > 0, "Apple Silicon should report cores");
            }
        }

        #[cfg(target_os = "linux")]
        {
            // On Linux, detection depends on having NVIDIA hardware
            // Just verify the functions don't panic
            let _nvidia = detection::detect_nvidia_gpu(0);
            let _count = detection::detect_nvidia_device_count();
            let _sysfs = detection::detect_nvidia_sysfs();
        }
    }

    #[test]
    fn test_parse_nvidia_smi_whitespace_handling() {
        let output = "  NVIDIA RTX 3080 ,  10240 ,  9500  \n";
        let devices = detection::parse_nvidia_smi(output);
        assert_eq!(devices.len(), 1);
        assert_eq!(devices[0].name, "NVIDIA RTX 3080");
        assert_eq!(devices[0].total_memory, 10240 * 1_048_576);
        assert_eq!(devices[0].available_memory, 9500 * 1_048_576);
    }
}