oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
//! FAISS GPU Integration for Massive Performance Acceleration
//!
//! This module provides comprehensive GPU acceleration integration with FAISS GPU capabilities,
//! enabling massive performance improvements for large-scale vector operations.
//!
//! Features:
//! - Multi-GPU support with automatic load balancing
//! - GPU memory management and optimization
//! - Asynchronous GPU operations with streaming
//! - GPU-CPU hybrid processing
//! - Dynamic workload distribution
//! - GPU performance monitoring and tuning

use crate::{
    faiss_integration::{FaissConfig, FaissSearchParams},
    gpu::GpuExecutionConfig,
};
use anyhow::{Error as AnyhowError, Result};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc, Mutex, RwLock,
};
use std::time::{Duration, Instant};
use tokio::sync::oneshot;
use tracing::{debug, error, info, span, warn, Level};

/// GPU configuration for FAISS integration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FaissGpuConfig {
    /// GPU device IDs to use
    pub device_ids: Vec<i32>,
    /// Memory allocation per device (bytes)
    pub memory_per_device: usize,
    /// Enable multi-GPU distributed processing
    pub enable_multi_gpu: bool,
    /// GPU memory management strategy
    pub memory_strategy: GpuMemoryStrategy,
    /// Compute stream configuration
    pub stream_config: GpuStreamConfig,
    /// Performance optimization settings
    pub optimization: GpuOptimizationConfig,
    /// Error handling and recovery
    pub error_handling: GpuErrorConfig,
}

/// GPU memory management strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GpuMemoryStrategy {
    /// Pre-allocate fixed memory pools
    FixedPool,
    /// Dynamic allocation as needed
    Dynamic,
    /// Unified memory management
    Unified,
    /// Memory streaming for large datasets
    Streaming { chunk_size: usize },
}

/// GPU compute stream configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuStreamConfig {
    /// Number of compute streams per device
    pub streams_per_device: usize,
    /// Enable stream overlapping
    pub enable_overlapping: bool,
    /// Stream priority levels
    pub priority_levels: Vec<i32>,
    /// Synchronization strategy
    pub sync_strategy: SyncStrategy,
}

/// Stream synchronization strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SyncStrategy {
    /// Block until completion
    Blocking,
    /// Non-blocking with callbacks
    NonBlocking,
    /// Event-based synchronization
    EventBased,
    /// Cooperative synchronization
    Cooperative,
}

/// GPU optimization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuOptimizationConfig {
    /// Enable Tensor Core utilization
    pub enable_tensor_cores: bool,
    /// Enable mixed precision (FP16/FP32)
    pub enable_mixed_precision: bool,
    /// Memory coalescing optimization
    pub enable_coalescing: bool,
    /// Kernel fusion optimization
    pub enable_kernel_fusion: bool,
    /// Cache optimization settings
    pub cache_config: GpuCacheConfig,
    /// Batch processing optimization
    pub batch_optimization: BatchOptimizationConfig,
}

/// GPU cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuCacheConfig {
    /// L1 cache configuration
    pub l1_cache_config: CacheConfig,
    /// Shared memory configuration
    pub shared_memory_config: CacheConfig,
    /// Enable cache prefetching
    pub enable_prefetching: bool,
    /// Cache line size optimization
    pub cache_line_size: usize,
}

/// Cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CacheConfig {
    /// Prefer L1 cache
    PreferL1,
    /// Prefer shared memory
    PreferShared,
    /// Equal allocation
    Equal,
    /// Disable cache
    Disabled,
}

/// Batch optimization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchOptimizationConfig {
    /// Optimal batch size for each operation
    pub optimal_batch_sizes: HashMap<String, usize>,
    /// Enable dynamic batch sizing
    pub enable_dynamic_batching: bool,
    /// Batch coalescence threshold
    pub coalescence_threshold: usize,
    /// Maximum batch size
    pub max_batch_size: usize,
}

/// GPU error handling configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuErrorConfig {
    /// Enable automatic error recovery
    pub enable_auto_recovery: bool,
    /// Maximum retry attempts
    pub max_retries: usize,
    /// Fallback to CPU on GPU failure
    pub fallback_to_cpu: bool,
    /// Error logging level
    pub error_logging_level: String,
}

impl Default for FaissGpuConfig {
    fn default() -> Self {
        Self {
            device_ids: vec![0],
            memory_per_device: 2 * 1024 * 1024 * 1024, // 2GB
            enable_multi_gpu: false,
            memory_strategy: GpuMemoryStrategy::Dynamic,
            stream_config: GpuStreamConfig {
                streams_per_device: 4,
                enable_overlapping: true,
                priority_levels: vec![0, 1, 2],
                sync_strategy: SyncStrategy::NonBlocking,
            },
            optimization: GpuOptimizationConfig {
                enable_tensor_cores: true,
                enable_mixed_precision: true,
                enable_coalescing: true,
                enable_kernel_fusion: true,
                cache_config: GpuCacheConfig {
                    l1_cache_config: CacheConfig::PreferL1,
                    shared_memory_config: CacheConfig::PreferShared,
                    enable_prefetching: true,
                    cache_line_size: 128,
                },
                batch_optimization: BatchOptimizationConfig {
                    optimal_batch_sizes: {
                        let mut sizes = HashMap::new();
                        sizes.insert("search".to_string(), 1024);
                        sizes.insert("add".to_string(), 512);
                        sizes.insert("train".to_string(), 256);
                        sizes
                    },
                    enable_dynamic_batching: true,
                    coalescence_threshold: 64,
                    max_batch_size: 4096,
                },
            },
            error_handling: GpuErrorConfig {
                enable_auto_recovery: true,
                max_retries: 3,
                fallback_to_cpu: true,
                error_logging_level: "warn".to_string(),
            },
        }
    }
}

/// FAISS GPU-accelerated index
pub struct FaissGpuIndex {
    /// Base FAISS configuration
    faiss_config: FaissConfig,
    /// GPU-specific configuration
    gpu_config: FaissGpuConfig,
    /// GPU runtime for operations
    gpu_runtime: Arc<GpuExecutionConfig>,
    /// GPU memory pools per device
    memory_pools: Arc<RwLock<HashMap<i32, FaissGpuMemoryPool>>>,
    /// Compute streams per device
    compute_streams: Arc<RwLock<HashMap<i32, Vec<GpuComputeStream>>>>,
    /// Performance statistics
    stats: Arc<RwLock<GpuPerformanceStats>>,
    /// Work queue for GPU operations
    work_queue: Arc<Mutex<VecDeque<GpuOperation>>>,
    /// Operation results cache
    results_cache: Arc<RwLock<HashMap<String, CachedResult>>>,
    /// Load balancer for multi-GPU
    load_balancer: Arc<RwLock<GpuLoadBalancer>>,
}

/// GPU memory pool for efficient allocation
#[derive(Debug)]
pub struct FaissGpuMemoryPool {
    /// Device ID
    pub device_id: i32,
    /// Total pool size
    pub total_size: usize,
    /// Currently allocated size
    pub allocated_size: AtomicUsize,
    /// Free blocks
    pub free_blocks: Arc<Mutex<BTreeMap<usize, Vec<GpuMemoryBlock>>>>,
    /// Allocated blocks
    pub allocated_blocks: Arc<RwLock<HashMap<usize, GpuMemoryBlock>>>,
    /// Allocation statistics
    pub allocation_stats: Arc<RwLock<AllocationStatistics>>,
}

/// GPU memory block
#[derive(Debug)]
pub struct GpuMemoryBlock {
    /// Block address on GPU
    pub gpu_address: usize,
    /// Block size in bytes
    pub size: usize,
    /// Allocation timestamp
    pub allocated_at: Instant,
    /// Reference count
    pub ref_count: AtomicUsize,
    /// Block type
    pub block_type: MemoryBlockType,
}

impl Clone for GpuMemoryBlock {
    fn clone(&self) -> Self {
        Self {
            gpu_address: self.gpu_address,
            size: self.size,
            allocated_at: self.allocated_at,
            ref_count: AtomicUsize::new(self.ref_count.load(Ordering::Relaxed)),
            block_type: self.block_type,
        }
    }
}

/// Memory block types
#[derive(Debug, Clone, Copy)]
pub enum MemoryBlockType {
    /// Vector storage
    Vectors,
    /// Index structure
    IndexData,
    /// Temporary computation
    Temporary,
    /// Result buffer
    Results,
}

/// GPU compute stream
#[derive(Debug)]
pub struct GpuComputeStream {
    /// Stream ID
    pub stream_id: usize,
    /// Device ID
    pub device_id: i32,
    /// Stream handle (simulated)
    pub stream_handle: usize,
    /// Stream priority
    pub priority: i32,
    /// Current operation
    pub current_operation: Arc<Mutex<Option<GpuOperation>>>,
    /// Operation history
    pub operation_history: Arc<RwLock<VecDeque<CompletedOperation>>>,
    /// Stream utilization
    pub utilization: Arc<RwLock<StreamUtilization>>,
}

/// GPU operation
#[derive(Debug)]
pub struct GpuOperation {
    /// Operation ID
    pub id: String,
    /// Operation type
    pub operation_type: GpuOperationType,
    /// Input data
    pub input_data: GpuOperationData,
    /// Expected output size
    pub output_size: usize,
    /// Priority level
    pub priority: i32,
    /// Timeout
    pub timeout: Option<Duration>,
    /// Result sender
    pub result_sender: Option<oneshot::Sender<GpuOperationResult>>,
}

/// GPU operation types
#[derive(Debug, Clone)]
pub enum GpuOperationType {
    /// Vector search operation
    Search {
        query_vectors: Vec<Vec<f32>>,
        k: usize,
        search_params: FaissSearchParams,
    },
    /// Vector addition operation
    Add {
        vectors: Vec<Vec<f32>>,
        ids: Vec<String>,
    },
    /// Index training operation
    Train { training_vectors: Vec<Vec<f32>> },
    /// Index optimization operation
    Optimize,
    /// Memory transfer operation
    MemoryTransfer {
        source: TransferSource,
        destination: TransferDestination,
        size: usize,
    },
}

/// GPU operation data
#[derive(Debug, Clone)]
pub enum GpuOperationData {
    /// Raw vector data
    Vectors(Vec<Vec<f32>>),
    /// Serialized index data
    IndexData(Vec<u8>),
    /// Query parameters
    QueryParams(HashMap<String, Vec<u8>>),
    /// Empty operation
    Empty,
}

/// Transfer source/destination
#[derive(Debug, Clone)]
pub enum TransferSource {
    CpuMemory(Vec<u8>),
    GpuMemory { device_id: i32, address: usize },
    Disk(std::path::PathBuf),
}

#[derive(Debug, Clone)]
pub enum TransferDestination {
    CpuMemory,
    GpuMemory { device_id: i32, address: usize },
    Disk(std::path::PathBuf),
}

/// GPU operation result
#[derive(Debug, Clone)]
pub struct GpuOperationResult {
    /// Operation ID
    pub operation_id: String,
    /// Success status
    pub success: bool,
    /// Result data
    pub result_data: GpuResultData,
    /// Execution time
    pub execution_time: Duration,
    /// Memory usage
    pub memory_used: usize,
    /// Error message if failed
    pub error_message: Option<String>,
}

/// GPU result data
#[derive(Debug, Clone)]
pub enum GpuResultData {
    /// Search results
    SearchResults(Vec<Vec<(String, f32)>>),
    /// Training completion
    TrainingComplete,
    /// Addition completion
    AdditionComplete,
    /// Optimization metrics
    OptimizationMetrics(HashMap<String, f64>),
    /// Memory transfer completion
    TransferComplete,
    /// Error result
    Error(String),
}

/// Completed operation record
#[derive(Debug, Clone)]
pub struct CompletedOperation {
    /// Operation ID
    pub operation_id: String,
    /// Operation type
    pub operation_type: String,
    /// Start time
    pub start_time: Instant,
    /// End time
    pub end_time: Instant,
    /// Success status
    pub success: bool,
    /// Memory used
    pub memory_used: usize,
}

/// Stream utilization metrics
#[derive(Debug, Clone, Default)]
pub struct StreamUtilization {
    /// Total operations processed
    pub total_operations: usize,
    /// Total execution time
    pub total_execution_time: Duration,
    /// Average execution time
    pub avg_execution_time: Duration,
    /// Utilization percentage
    pub utilization_percentage: f32,
    /// Idle time
    pub idle_time: Duration,
}

/// GPU performance statistics
#[derive(Debug, Clone, Default)]
pub struct GpuPerformanceStats {
    /// Per-device statistics
    pub device_stats: HashMap<i32, DeviceStats>,
    /// Overall GPU utilization
    pub overall_utilization: f32,
    /// Memory efficiency
    pub memory_efficiency: f32,
    /// Throughput metrics
    pub throughput: ThroughputMetrics,
    /// Error statistics
    pub error_stats: ErrorStatistics,
    /// Performance trends
    pub performance_trends: PerformanceTrends,
}

/// Per-device performance statistics
#[derive(Debug, Clone, Default)]
pub struct DeviceStats {
    /// Device utilization percentage
    pub utilization: f32,
    /// Memory usage
    pub memory_usage: MemoryUsageStats,
    /// Compute performance
    pub compute_performance: ComputePerformanceStats,
    /// Power consumption (watts)
    pub power_consumption: f32,
    /// Temperature (Celsius)
    pub temperature: f32,
}

/// Memory usage statistics
#[derive(Debug, Clone, Default)]
pub struct MemoryUsageStats {
    /// Total memory
    pub total_memory: usize,
    /// Used memory
    pub used_memory: usize,
    /// Free memory
    pub free_memory: usize,
    /// Peak usage
    pub peak_usage: usize,
    /// Fragmentation percentage
    pub fragmentation: f32,
}

/// Compute performance statistics
#[derive(Debug, Clone, Default)]
pub struct ComputePerformanceStats {
    /// FLOPS (floating point operations per second)
    pub flops: f64,
    /// Memory bandwidth utilization
    pub memory_bandwidth_utilization: f32,
    /// Kernel efficiency
    pub kernel_efficiency: f32,
    /// Occupancy percentage
    pub occupancy: f32,
}

/// Throughput metrics
#[derive(Debug, Clone, Default)]
pub struct ThroughputMetrics {
    /// Vectors processed per second
    pub vectors_per_second: f64,
    /// Operations per second
    pub operations_per_second: f64,
    /// Data transfer rate (MB/s)
    pub transfer_rate_mbps: f64,
    /// Search queries per second
    pub search_qps: f64,
}

/// Error statistics
#[derive(Debug, Clone, Default)]
pub struct ErrorStatistics {
    /// Total errors
    pub total_errors: usize,
    /// Recoverable errors
    pub recoverable_errors: usize,
    /// Fatal errors
    pub fatal_errors: usize,
    /// Error rate (errors per operation)
    pub error_rate: f32,
    /// Recovery success rate
    pub recovery_rate: f32,
}

/// Performance trends
#[derive(Debug, Clone, Default)]
pub struct PerformanceTrends {
    /// Utilization trend over time
    pub utilization_trend: Vec<(Instant, f32)>,
    /// Throughput trend over time
    pub throughput_trend: Vec<(Instant, f64)>,
    /// Memory usage trend
    pub memory_trend: Vec<(Instant, usize)>,
    /// Error rate trend
    pub error_trend: Vec<(Instant, f32)>,
}

/// Allocation statistics
#[derive(Debug, Clone, Default)]
pub struct AllocationStatistics {
    /// Total allocations
    pub total_allocations: usize,
    /// Total deallocations
    pub total_deallocations: usize,
    /// Peak memory usage
    pub peak_usage: usize,
    /// Average allocation size
    pub avg_allocation_size: usize,
    /// Fragmentation events
    pub fragmentation_events: usize,
    /// Out of memory events
    pub oom_events: usize,
}

/// Cached result for performance optimization
#[derive(Debug)]
pub struct CachedResult {
    /// Result data
    pub data: GpuResultData,
    /// Cache timestamp
    pub timestamp: Instant,
    /// Hit count
    pub hit_count: AtomicUsize,
    /// Result size
    pub size: usize,
}

impl Clone for CachedResult {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            timestamp: self.timestamp,
            hit_count: AtomicUsize::new(self.hit_count.load(Ordering::Acquire)),
            size: self.size,
        }
    }
}

/// GPU load balancer for multi-GPU operations
#[derive(Debug)]
pub struct GpuLoadBalancer {
    /// Device utilization tracking
    pub device_utilization: HashMap<i32, f32>,
    /// Current workload distribution
    pub workload_distribution: HashMap<i32, usize>,
    /// Load balancing strategy
    pub strategy: LoadBalancingStrategy,
    /// Performance history for decisions
    pub performance_history: HashMap<i32, VecDeque<PerformanceSnapshot>>,
}

/// Load balancing strategies
#[derive(Debug, Clone)]
pub enum LoadBalancingStrategy {
    /// Round-robin distribution
    RoundRobin,
    /// Load-based distribution
    LoadBased,
    /// Performance-based distribution
    PerformanceBased,
    /// Memory-aware distribution
    MemoryAware,
    /// Hybrid strategy
    Hybrid,
}

/// Performance snapshot for load balancing decisions
#[derive(Debug, Clone)]
pub struct PerformanceSnapshot {
    /// Timestamp
    pub timestamp: Instant,
    /// Utilization percentage
    pub utilization: f32,
    /// Memory usage percentage
    pub memory_usage: f32,
    /// Operations per second
    pub ops_per_second: f64,
    /// Average latency
    pub avg_latency: Duration,
}

impl FaissGpuIndex {
    /// Create a new GPU-accelerated FAISS index
    pub async fn new(faiss_config: FaissConfig, gpu_config: FaissGpuConfig) -> Result<Self> {
        let span = span!(Level::INFO, "faiss_gpu_index_new");
        let _enter = span.enter();

        // Convert FaissGpuConfig to GpuConfig for the accelerator
        let _base_gpu_config = crate::gpu::GpuConfig {
            device_id: gpu_config.device_ids.first().copied().unwrap_or(0),
            enable_mixed_precision: true,
            enable_tensor_cores: true,
            batch_size: 1024,
            memory_pool_size: gpu_config.memory_per_device,
            stream_count: gpu_config.stream_config.streams_per_device,
            enable_peer_access: gpu_config.enable_multi_gpu,
            enable_unified_memory: matches!(gpu_config.memory_strategy, GpuMemoryStrategy::Unified),
            enable_async_execution: true,
            enable_multi_gpu: gpu_config.enable_multi_gpu,
            preferred_gpu_ids: gpu_config.device_ids.clone(),
            dynamic_batch_sizing: true,
            enable_memory_compression: false,
            kernel_cache_size: 1024 * 1024,
            optimization_level: crate::gpu::OptimizationLevel::Performance,
            precision_mode: crate::gpu::PrecisionMode::Mixed,
        };

        // Initialize GPU execution configuration
        let gpu_runtime = Arc::new(GpuExecutionConfig::default());

        // Initialize memory pools for each device
        let mut memory_pools = HashMap::new();
        for &device_id in &gpu_config.device_ids {
            let pool = FaissGpuMemoryPool::new(device_id, gpu_config.memory_per_device)?;
            memory_pools.insert(device_id, pool);
        }

        // Initialize compute streams
        let mut compute_streams = HashMap::new();
        for &device_id in &gpu_config.device_ids {
            let streams = Self::create_compute_streams(device_id, &gpu_config.stream_config)?;
            compute_streams.insert(device_id, streams);
        }

        // Initialize load balancer
        let load_balancer =
            GpuLoadBalancer::new(&gpu_config.device_ids, LoadBalancingStrategy::Hybrid);

        let device_count = gpu_config.device_ids.len();
        let index = Self {
            faiss_config,
            gpu_config,
            gpu_runtime,
            memory_pools: Arc::new(RwLock::new(memory_pools)),
            compute_streams: Arc::new(RwLock::new(compute_streams)),
            stats: Arc::new(RwLock::new(GpuPerformanceStats::default())),
            work_queue: Arc::new(Mutex::new(VecDeque::new())),
            results_cache: Arc::new(RwLock::new(HashMap::new())),
            load_balancer: Arc::new(RwLock::new(load_balancer)),
        };

        // Start background worker tasks
        index.start_background_workers().await?;

        info!(
            "Created GPU-accelerated FAISS index with {} devices",
            device_count
        );
        Ok(index)
    }

    /// Create compute streams for a device
    fn create_compute_streams(
        device_id: i32,
        stream_config: &GpuStreamConfig,
    ) -> Result<Vec<GpuComputeStream>> {
        let mut streams = Vec::new();

        for i in 0..stream_config.streams_per_device {
            let priority = stream_config
                .priority_levels
                .get(i % stream_config.priority_levels.len())
                .copied()
                .unwrap_or(0);

            let stream = GpuComputeStream {
                stream_id: i,
                device_id,
                stream_handle: device_id as usize * 1000 + i, // Simulated handle
                priority,
                current_operation: Arc::new(Mutex::new(None)),
                operation_history: Arc::new(RwLock::new(VecDeque::new())),
                utilization: Arc::new(RwLock::new(StreamUtilization::default())),
            };

            streams.push(stream);
        }

        Ok(streams)
    }

    /// Start background worker tasks
    async fn start_background_workers(&self) -> Result<()> {
        let span = span!(Level::DEBUG, "start_background_workers");
        let _enter = span.enter();

        // Start operation processor
        self.start_operation_processor().await?;

        // Start performance monitor
        self.start_performance_monitor().await?;

        // Start memory manager
        self.start_memory_manager().await?;

        // Start load balancer
        if self.gpu_config.enable_multi_gpu {
            self.start_load_balancer().await?;
        }

        debug!("Started background worker tasks");
        Ok(())
    }

    /// Start operation processor task
    async fn start_operation_processor(&self) -> Result<()> {
        let work_queue = Arc::clone(&self.work_queue);
        let compute_streams = Arc::clone(&self.compute_streams);
        let stats = Arc::clone(&self.stats);
        let gpu_config = self.gpu_config.clone();

        tokio::spawn(async move {
            loop {
                // Process pending operations
                if let Some(operation) = {
                    let mut queue = work_queue.lock().expect("lock poisoned");
                    queue.pop_front()
                } {
                    if let Err(e) = Self::process_gpu_operation(
                        operation,
                        &compute_streams,
                        &stats,
                        &gpu_config,
                    )
                    .await
                    {
                        error!("Failed to process GPU operation: {}", e);
                    }
                }

                // Sleep briefly to avoid busy waiting
                tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
            }
        });

        Ok(())
    }

    /// Process a single GPU operation
    async fn process_gpu_operation(
        mut operation: GpuOperation,
        compute_streams: &Arc<RwLock<HashMap<i32, Vec<GpuComputeStream>>>>,
        stats: &Arc<RwLock<GpuPerformanceStats>>,
        gpu_config: &FaissGpuConfig,
    ) -> Result<()> {
        let start_time = Instant::now();

        // Select optimal device and stream
        let (device_id, stream_id) =
            Self::select_optimal_stream(compute_streams, &operation).await?;

        // Extract result_sender before executing operation
        let result_sender = operation.result_sender.take();

        // Execute operation
        let result =
            Self::execute_operation_on_device(operation, device_id, stream_id, gpu_config).await?;

        // Send result back if callback provided
        if let Some(sender) = result_sender {
            let _ = sender.send(result.clone());
        }

        // Update statistics
        Self::update_operation_stats(stats, &result, start_time.elapsed()).await?;

        Ok(())
    }

    /// Select optimal device and stream for operation
    async fn select_optimal_stream(
        compute_streams: &Arc<RwLock<HashMap<i32, Vec<GpuComputeStream>>>>,
        _operation: &GpuOperation,
    ) -> Result<(i32, usize)> {
        let streams = compute_streams.read().expect("lock poisoned");

        // Simple strategy: find device with lowest utilization
        let mut best_device = 0;
        let mut best_stream = 0;
        let mut lowest_utilization = f32::MAX;

        for (&device_id, device_streams) in streams.iter() {
            for (stream_id, stream) in device_streams.iter().enumerate() {
                let utilization = stream
                    .utilization
                    .read()
                    .expect("lock poisoned")
                    .utilization_percentage;
                if utilization < lowest_utilization {
                    lowest_utilization = utilization;
                    best_device = device_id;
                    best_stream = stream_id;
                }
            }
        }

        Ok((best_device, best_stream))
    }

    /// Execute operation on specific device
    async fn execute_operation_on_device(
        operation: GpuOperation,
        _device_id: i32,
        _stream_id: usize,
        _gpu_config: &FaissGpuConfig,
    ) -> Result<GpuOperationResult> {
        let start_time = Instant::now();

        // Simulate GPU operation execution
        let result_data = match &operation.operation_type {
            GpuOperationType::Search {
                query_vectors, k, ..
            } => {
                // Simulate GPU-accelerated search
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

                let mut results = Vec::new();
                for _query in query_vectors {
                    let mut query_results = Vec::new();
                    for i in 0..*k {
                        query_results.push((format!("gpu_result_{i}"), 0.95 - (i as f32 * 0.05)));
                    }
                    results.push(query_results);
                }

                GpuResultData::SearchResults(results)
            }
            GpuOperationType::Add { vectors: _, .. } => {
                // Simulate GPU-accelerated vector addition
                tokio::time::sleep(tokio::time::Duration::from_millis(5)).await;
                GpuResultData::AdditionComplete
            }
            GpuOperationType::Train { .. } => {
                // Simulate GPU-accelerated training
                tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                GpuResultData::TrainingComplete
            }
            GpuOperationType::Optimize => {
                // Simulate GPU optimization
                tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
                let mut metrics = HashMap::new();
                metrics.insert("optimization_improvement".to_string(), 15.0);
                metrics.insert("memory_efficiency".to_string(), 92.0);
                GpuResultData::OptimizationMetrics(metrics)
            }
            GpuOperationType::MemoryTransfer { size, .. } => {
                // Simulate memory transfer
                let transfer_time = *size as f64 / (10.0 * 1024.0 * 1024.0 * 1024.0); // 10 GB/s
                tokio::time::sleep(tokio::time::Duration::from_secs_f64(transfer_time)).await;
                GpuResultData::TransferComplete
            }
        };

        Ok(GpuOperationResult {
            operation_id: operation.id,
            success: true,
            result_data,
            execution_time: start_time.elapsed(),
            memory_used: 1024 * 1024, // Simulated 1MB
            error_message: None,
        })
    }

    /// Update operation statistics
    async fn update_operation_stats(
        stats: &Arc<RwLock<GpuPerformanceStats>>,
        result: &GpuOperationResult,
        execution_time: Duration,
    ) -> Result<()> {
        let mut stats = stats.write().expect("lock poisoned");

        // Update throughput metrics
        stats.throughput.operations_per_second += 1.0 / execution_time.as_secs_f64();

        // Update error statistics if needed
        if !result.success {
            stats.error_stats.total_errors += 1;
        }

        Ok(())
    }

    /// Start performance monitoring task
    async fn start_performance_monitor(&self) -> Result<()> {
        let stats = Arc::clone(&self.stats);
        let device_ids = self.gpu_config.device_ids.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));

            loop {
                interval.tick().await;

                // Collect performance metrics from all devices
                if let Err(e) = Self::collect_performance_metrics(&stats, &device_ids).await {
                    warn!("Failed to collect performance metrics: {}", e);
                }
            }
        });

        Ok(())
    }

    /// Collect performance metrics from devices
    async fn collect_performance_metrics(
        stats: &Arc<RwLock<GpuPerformanceStats>>,
        device_ids: &[i32],
    ) -> Result<()> {
        let mut stats = stats.write().expect("lock poisoned");

        for &device_id in device_ids {
            // Simulate GPU metrics collection
            let device_stats = DeviceStats {
                utilization: 75.0 + (device_id as f32 * 5.0) % 25.0, // Simulated
                memory_usage: MemoryUsageStats {
                    total_memory: 8 * 1024 * 1024 * 1024, // 8GB
                    used_memory: 6 * 1024 * 1024 * 1024,  // 6GB
                    free_memory: 2 * 1024 * 1024 * 1024,  // 2GB
                    peak_usage: 7 * 1024 * 1024 * 1024,   // 7GB
                    fragmentation: 5.0,
                },
                compute_performance: ComputePerformanceStats {
                    flops: 15.5e12, // 15.5 TFLOPS
                    memory_bandwidth_utilization: 80.0,
                    kernel_efficiency: 85.0,
                    occupancy: 75.0,
                },
                power_consumption: 250.0, // Watts
                temperature: 70.0,        // Celsius
            };

            stats.device_stats.insert(device_id, device_stats);
        }

        // Calculate overall utilization
        stats.overall_utilization = stats
            .device_stats
            .values()
            .map(|s| s.utilization)
            .sum::<f32>()
            / stats.device_stats.len() as f32;

        Ok(())
    }

    /// Start memory management task
    async fn start_memory_manager(&self) -> Result<()> {
        let memory_pools = Arc::clone(&self.memory_pools);
        let gpu_config = self.gpu_config.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(5));

            loop {
                interval.tick().await;

                // Perform memory cleanup and optimization
                if let Err(e) = Self::manage_gpu_memory(&memory_pools, &gpu_config).await {
                    warn!("Failed to manage GPU memory: {}", e);
                }
            }
        });

        Ok(())
    }

    /// Manage GPU memory pools
    async fn manage_gpu_memory(
        memory_pools: &Arc<RwLock<HashMap<i32, FaissGpuMemoryPool>>>,
        _gpu_config: &FaissGpuConfig,
    ) -> Result<()> {
        let pools = memory_pools.read().expect("lock poisoned");

        for (device_id, pool) in pools.iter() {
            // Check for memory fragmentation
            let fragmentation = pool.calculate_fragmentation();
            if fragmentation > 20.0 {
                debug!(
                    "High fragmentation detected on device {}: {:.1}%",
                    device_id, fragmentation
                );
                // Trigger defragmentation if needed
            }

            // Check for memory leaks
            let allocated_blocks = pool.allocated_blocks.read().expect("lock poisoned");
            let now = Instant::now();
            for (_, block) in allocated_blocks.iter() {
                if now.duration_since(block.allocated_at) > Duration::from_secs(3600) {
                    warn!(
                        "Potential memory leak detected on device {}: block allocated {} ago",
                        device_id,
                        humantime::format_duration(now.duration_since(block.allocated_at))
                    );
                }
            }
        }

        Ok(())
    }

    /// Start load balancer task
    async fn start_load_balancer(&self) -> Result<()> {
        let load_balancer = Arc::clone(&self.load_balancer);
        let stats = Arc::clone(&self.stats);

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(2));

            loop {
                interval.tick().await;

                // Update load balancing decisions
                if let Err(e) = Self::update_load_balancing(&load_balancer, &stats).await {
                    warn!("Failed to update load balancing: {}", e);
                }
            }
        });

        Ok(())
    }

    /// Update load balancing strategy
    async fn update_load_balancing(
        load_balancer: &Arc<RwLock<GpuLoadBalancer>>,
        stats: &Arc<RwLock<GpuPerformanceStats>>,
    ) -> Result<()> {
        let stats = stats.read().expect("lock poisoned");
        let mut balancer = load_balancer.write().expect("lock poisoned");

        // Update device utilization from stats
        for (&device_id, device_stats) in &stats.device_stats {
            balancer
                .device_utilization
                .insert(device_id, device_stats.utilization);

            // Add performance snapshot
            let snapshot = PerformanceSnapshot {
                timestamp: Instant::now(),
                utilization: device_stats.utilization,
                memory_usage: device_stats.memory_usage.used_memory as f32
                    / device_stats.memory_usage.total_memory as f32
                    * 100.0,
                ops_per_second: 1000.0, // Simulated
                avg_latency: Duration::from_micros(250),
            };

            balancer
                .performance_history
                .entry(device_id)
                .or_default()
                .push_back(snapshot);

            // Keep only recent history
            if balancer.performance_history[&device_id].len() > 100 {
                balancer
                    .performance_history
                    .get_mut(&device_id)
                    .expect("device_id should exist in performance_history")
                    .pop_front();
            }
        }

        Ok(())
    }

    /// Perform GPU-accelerated search
    pub async fn search_gpu(
        &self,
        query_vectors: Vec<Vec<f32>>,
        k: usize,
        search_params: FaissSearchParams,
    ) -> Result<Vec<Vec<(String, f32)>>> {
        let span = span!(Level::DEBUG, "search_gpu");
        let _enter = span.enter();

        // Create GPU operation
        let (result_sender, result_receiver) = oneshot::channel();
        let operation = GpuOperation {
            id: uuid::Uuid::new_v4().to_string(),
            operation_type: GpuOperationType::Search {
                query_vectors: query_vectors.clone(),
                k,
                search_params,
            },
            input_data: GpuOperationData::Vectors(query_vectors),
            output_size: k * std::mem::size_of::<(String, f32)>(),
            priority: 1,
            timeout: Some(Duration::from_secs(30)),
            result_sender: Some(result_sender),
        };

        // Queue operation
        {
            let mut queue = self.work_queue.lock().expect("lock poisoned");
            queue.push_back(operation);
        }

        // Wait for result
        let result = result_receiver
            .await
            .map_err(|_| AnyhowError::msg("GPU operation timeout"))?;

        if !result.success {
            return Err(AnyhowError::msg(
                result
                    .error_message
                    .unwrap_or_else(|| "GPU operation failed".to_string()),
            ));
        }

        match result.result_data {
            GpuResultData::SearchResults(results) => Ok(results),
            _ => Err(AnyhowError::msg("Unexpected result type")),
        }
    }

    /// Add vectors with GPU acceleration
    pub async fn add_vectors_gpu(&self, vectors: Vec<Vec<f32>>, ids: Vec<String>) -> Result<()> {
        let span = span!(Level::DEBUG, "add_vectors_gpu");
        let _enter = span.enter();

        let (result_sender, result_receiver) = oneshot::channel();
        let operation = GpuOperation {
            id: uuid::Uuid::new_v4().to_string(),
            operation_type: GpuOperationType::Add {
                vectors: vectors.clone(),
                ids,
            },
            input_data: GpuOperationData::Vectors(vectors),
            output_size: 0,
            priority: 2,
            timeout: Some(Duration::from_secs(60)),
            result_sender: Some(result_sender),
        };

        {
            let mut queue = self.work_queue.lock().expect("lock poisoned");
            queue.push_back(operation);
        }

        let result = result_receiver
            .await
            .map_err(|_| AnyhowError::msg("GPU operation timeout"))?;

        if !result.success {
            return Err(AnyhowError::msg(
                result
                    .error_message
                    .unwrap_or_else(|| "GPU operation failed".to_string()),
            ));
        }

        Ok(())
    }

    /// Get GPU performance statistics
    pub fn get_gpu_stats(&self) -> Result<GpuPerformanceStats> {
        let stats = self.stats.read().expect("lock poisoned");
        Ok(stats.clone())
    }

    /// Optimize GPU performance
    pub async fn optimize_gpu_performance(&self) -> Result<HashMap<String, f64>> {
        let span = span!(Level::INFO, "optimize_gpu_performance");
        let _enter = span.enter();

        let (result_sender, result_receiver) = oneshot::channel();
        let operation = GpuOperation {
            id: uuid::Uuid::new_v4().to_string(),
            operation_type: GpuOperationType::Optimize,
            input_data: GpuOperationData::Empty,
            output_size: 0,
            priority: 0, // High priority
            timeout: Some(Duration::from_secs(120)),
            result_sender: Some(result_sender),
        };

        {
            let mut queue = self.work_queue.lock().expect("lock poisoned");
            queue.push_back(operation);
        }

        let result = result_receiver
            .await
            .map_err(|_| AnyhowError::msg("GPU optimization timeout"))?;

        if !result.success {
            return Err(AnyhowError::msg("GPU optimization failed"));
        }

        match result.result_data {
            GpuResultData::OptimizationMetrics(metrics) => Ok(metrics),
            _ => Err(AnyhowError::msg("Unexpected result type")),
        }
    }
}

impl FaissGpuMemoryPool {
    /// Create a new GPU memory pool
    pub fn new(device_id: i32, total_size: usize) -> Result<Self> {
        Ok(Self {
            device_id,
            total_size,
            allocated_size: AtomicUsize::new(0),
            free_blocks: Arc::new(Mutex::new(BTreeMap::new())),
            allocated_blocks: Arc::new(RwLock::new(HashMap::new())),
            allocation_stats: Arc::new(RwLock::new(AllocationStatistics::default())),
        })
    }

    /// Allocate memory block
    pub fn allocate(&self, size: usize, block_type: MemoryBlockType) -> Result<GpuMemoryBlock> {
        let aligned_size = (size + 255) & !255; // 256-byte alignment

        if self.allocated_size.load(Ordering::Relaxed) + aligned_size > self.total_size {
            return Err(AnyhowError::msg("Out of GPU memory"));
        }

        let block = GpuMemoryBlock {
            gpu_address: self.allocated_size.load(Ordering::Relaxed), // Simulated address
            size: aligned_size,
            allocated_at: Instant::now(),
            ref_count: AtomicUsize::new(1),
            block_type,
        };

        self.allocated_size
            .fetch_add(aligned_size, Ordering::Relaxed);

        // Update statistics
        {
            let mut stats = self.allocation_stats.write().expect("lock poisoned");
            stats.total_allocations += 1;
            let current_usage = self.allocated_size.load(Ordering::Relaxed);
            if current_usage > stats.peak_usage {
                stats.peak_usage = current_usage;
            }
        }

        Ok(block)
    }

    /// Deallocate memory block
    pub fn deallocate(&self, block: &GpuMemoryBlock) -> Result<()> {
        self.allocated_size.fetch_sub(block.size, Ordering::Relaxed);

        {
            let mut stats = self.allocation_stats.write().expect("lock poisoned");
            stats.total_deallocations += 1;
        }

        Ok(())
    }

    /// Calculate memory fragmentation percentage
    pub fn calculate_fragmentation(&self) -> f32 {
        // Simplified fragmentation calculation
        let allocated = self.allocated_size.load(Ordering::Relaxed);
        let free_blocks = self.free_blocks.lock().expect("lock poisoned");
        let num_free_blocks = free_blocks.len();

        if allocated == 0 {
            return 0.0;
        }

        (num_free_blocks as f32 / (allocated / 1024) as f32) * 100.0
    }
}

impl GpuLoadBalancer {
    /// Create a new GPU load balancer
    pub fn new(device_ids: &[i32], strategy: LoadBalancingStrategy) -> Self {
        let mut device_utilization = HashMap::new();
        let mut workload_distribution = HashMap::new();
        let mut performance_history = HashMap::new();

        for &device_id in device_ids {
            device_utilization.insert(device_id, 0.0);
            workload_distribution.insert(device_id, 0);
            performance_history.insert(device_id, VecDeque::new());
        }

        Self {
            device_utilization,
            workload_distribution,
            strategy,
            performance_history,
        }
    }

    /// Select optimal device for operation
    pub fn select_device(&self, operation: &GpuOperation) -> i32 {
        match self.strategy {
            LoadBalancingStrategy::RoundRobin => self.select_round_robin(),
            LoadBalancingStrategy::LoadBased => self.select_load_based(),
            LoadBalancingStrategy::PerformanceBased => self.select_performance_based(),
            LoadBalancingStrategy::MemoryAware => self.select_memory_aware(),
            LoadBalancingStrategy::Hybrid => self.select_hybrid(operation),
        }
    }

    fn select_round_robin(&self) -> i32 {
        // Simple round-robin selection
        let total_workload: usize = self.workload_distribution.values().sum();
        let device_count = self.device_utilization.len();
        let target_device_index = total_workload % device_count;

        *self
            .device_utilization
            .keys()
            .nth(target_device_index)
            .unwrap_or(&0)
    }

    fn select_load_based(&self) -> i32 {
        // Select device with lowest utilization
        self.device_utilization
            .iter()
            .min_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(&device_id, _)| device_id)
            .unwrap_or(0)
    }

    fn select_performance_based(&self) -> i32 {
        // Select device with best recent performance
        let mut best_device = 0;
        let mut best_score = f64::MIN;

        for (&device_id, history) in &self.performance_history {
            if let Some(recent_snapshot) = history.back() {
                let score = recent_snapshot.ops_per_second
                    / (recent_snapshot.avg_latency.as_secs_f64() + 1e-6);
                if score > best_score {
                    best_score = score;
                    best_device = device_id;
                }
            }
        }

        best_device
    }

    fn select_memory_aware(&self) -> i32 {
        // Select device with most available memory (simplified)
        self.device_utilization
            .iter()
            .min_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(&device_id, _)| device_id)
            .unwrap_or(0)
    }

    fn select_hybrid(&self, operation: &GpuOperation) -> i32 {
        // Combine multiple factors for selection
        match &operation.operation_type {
            GpuOperationType::Search { .. } => self.select_performance_based(),
            GpuOperationType::Add { .. } => self.select_memory_aware(),
            GpuOperationType::Train { .. } => self.select_load_based(),
            _ => self.select_round_robin(),
        }
    }
}

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

    #[tokio::test]
    async fn test_faiss_gpu_index_creation() {
        let faiss_config = FaissConfig::default();
        let gpu_config = FaissGpuConfig::default();

        let result = FaissGpuIndex::new(faiss_config, gpu_config).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_gpu_memory_pool() -> Result<()> {
        let pool = FaissGpuMemoryPool::new(0, 1024 * 1024)?; // 1MB pool

        let block = pool.allocate(1024, MemoryBlockType::Vectors)?;
        assert_eq!(block.size, 1024);

        pool.deallocate(&block)?;
        assert_eq!(pool.allocated_size.load(Ordering::Relaxed), 0);
        Ok(())
    }

    #[test]
    fn test_gpu_load_balancer() {
        let device_ids = vec![0, 1, 2];
        let balancer = GpuLoadBalancer::new(&device_ids, LoadBalancingStrategy::RoundRobin);

        assert_eq!(balancer.device_utilization.len(), 3);

        let operation = GpuOperation {
            id: "test".to_string(),
            operation_type: GpuOperationType::Optimize,
            input_data: GpuOperationData::Empty,
            output_size: 0,
            priority: 0,
            timeout: None,
            result_sender: None,
        };

        let selected_device = balancer.select_device(&operation);
        assert!(device_ids.contains(&selected_device));
    }
}