torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
//! CUDA Occupancy Optimization and Analysis Tools
//!
//! This module provides comprehensive CUDA occupancy analysis and optimization
//! capabilities to maximize GPU utilization and performance.
//!
//! Features:
//! - Theoretical occupancy calculation
//! - Runtime occupancy measurement
//! - Occupancy bottleneck analysis
//! - Automatic launch parameter optimization
//! - Register and shared memory usage analysis
//! - Performance correlation analysis

use crate::cuda::device::CudaDevice;
use crate::cuda::error::CudaResult;
use std::collections::HashMap;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String, vec::Vec};

/// CUDA occupancy analyzer for kernel optimization
#[derive(Debug)]
pub struct CudaOccupancyAnalyzer {
    device: CudaDevice,
    cached_results: HashMap<String, OccupancyResult>,
    optimization_heuristics: OptimizationHeuristics,
}

/// Result of occupancy analysis
#[derive(Debug, Clone)]
pub struct OccupancyResult {
    /// Theoretical occupancy percentage (0.0 - 1.0)
    pub theoretical_occupancy: f32,
    /// Achieved occupancy percentage (0.0 - 1.0)
    pub achieved_occupancy: Option<f32>,
    /// Maximum active blocks per multiprocessor
    pub max_active_blocks: u32,
    /// Maximum theoretical blocks per multiprocessor
    pub max_theoretical_blocks: u32,
    /// Optimal block size
    pub optimal_block_size: u32,
    /// Minimum grid size for maximum occupancy
    pub min_grid_size: u32,
    /// Limiting factors
    pub limiting_factors: Vec<LimitingFactor>,
    /// Resource usage breakdown
    pub resource_usage: ResourceUsage,
    /// Performance metrics (if available)
    pub performance_metrics: Option<PerformanceMetrics>,
}

/// Factors that limit occupancy
#[derive(Debug, Clone, PartialEq)]
pub enum LimitingFactor {
    /// Limited by register usage per thread
    Registers { used: u32, limit: u32 },
    /// Limited by shared memory usage per block
    SharedMemory { used: u32, limit: u32 },
    /// Limited by maximum threads per block
    ThreadsPerBlock { used: u32, limit: u32 },
    /// Limited by maximum blocks per multiprocessor
    BlocksPerSM { used: u32, limit: u32 },
    /// Limited by warp allocation
    WarpAllocation { used: u32, limit: u32 },
}

/// Resource usage information
#[derive(Debug, Clone)]
pub struct ResourceUsage {
    /// Registers per thread
    pub registers_per_thread: u32,
    /// Shared memory per block (bytes)
    pub shared_memory_per_block: u32,
    /// Local memory per thread (bytes)
    pub local_memory_per_thread: u32,
    /// Threads per block
    pub threads_per_block: u32,
    /// Constant memory usage (bytes)
    pub constant_memory: u32,
}

/// Performance metrics for occupancy correlation
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    /// Kernel execution time (milliseconds)
    pub execution_time_ms: f32,
    /// Memory bandwidth utilization (0.0 - 1.0)
    pub memory_bandwidth_utilization: f32,
    /// Compute utilization (0.0 - 1.0)
    pub compute_utilization: f32,
    /// Instructions per clock cycle
    pub ipc: f32,
    /// Warp execution efficiency (0.0 - 1.0)
    pub warp_execution_efficiency: f32,
}

/// Optimization heuristics configuration
#[derive(Debug, Clone)]
pub struct OptimizationHeuristics {
    /// Target occupancy threshold (0.0 - 1.0)
    pub target_occupancy: f32,
    /// Prefer higher occupancy vs lower resource usage
    pub prefer_high_occupancy: bool,
    /// Weight given to register optimization
    pub register_optimization_weight: f32,
    /// Weight given to shared memory optimization
    pub shared_memory_optimization_weight: f32,
    /// Enable dynamic block size adjustment
    pub dynamic_block_sizing: bool,
    /// Maximum block size to consider
    pub max_block_size: u32,
    /// Minimum block size to consider
    pub min_block_size: u32,
}

impl Default for OptimizationHeuristics {
    fn default() -> Self {
        Self {
            target_occupancy: 0.75, // 75% occupancy target
            prefer_high_occupancy: true,
            register_optimization_weight: 0.6,
            shared_memory_optimization_weight: 0.4,
            dynamic_block_sizing: true,
            max_block_size: 1024,
            min_block_size: 32,
        }
    }
}

/// Kernel launch configuration optimized for occupancy
#[derive(Debug, Clone)]
pub struct OptimizedLaunchConfig {
    /// Optimized block size
    pub block_size: (u32, u32, u32),
    /// Optimized grid size
    pub grid_size: (u32, u32, u32),
    /// Shared memory size per block
    pub shared_memory_size: u32,
    /// Expected occupancy
    pub expected_occupancy: f32,
    /// Optimization rationale
    pub optimization_notes: String,
}

impl CudaOccupancyAnalyzer {
    /// Create a new occupancy analyzer for the given device
    pub fn new(device: CudaDevice) -> Self {
        Self {
            device,
            cached_results: HashMap::new(),
            optimization_heuristics: OptimizationHeuristics::default(),
        }
    }

    /// Set optimization heuristics
    pub fn set_heuristics(&mut self, heuristics: OptimizationHeuristics) {
        self.optimization_heuristics = heuristics;
    }

    /// Analyze occupancy for a kernel with given parameters
    pub fn analyze_kernel_occupancy(
        &mut self,
        kernel_name: &str,
        block_size: (u32, u32, u32),
        shared_memory_size: u32,
        registers_per_thread: Option<u32>,
    ) -> CudaResult<OccupancyResult> {
        let cache_key = format!(
            "{}_{}_{}_{}_{}_{}",
            kernel_name,
            block_size.0,
            block_size.1,
            block_size.2,
            shared_memory_size,
            registers_per_thread.unwrap_or(0)
        );

        if let Some(cached) = self.cached_results.get(&cache_key) {
            return Ok(cached.clone());
        }

        let result = self.calculate_occupancy(
            kernel_name,
            block_size,
            shared_memory_size,
            registers_per_thread,
        )?;

        self.cached_results.insert(cache_key, result.clone());
        Ok(result)
    }

    /// Calculate theoretical occupancy for given parameters
    fn calculate_occupancy(
        &self,
        kernel_name: &str,
        block_size: (u32, u32, u32),
        shared_memory_size: u32,
        registers_per_thread: Option<u32>,
    ) -> CudaResult<OccupancyResult> {
        let device_props = self.device.properties()?;
        let threads_per_block = block_size.0 * block_size.1 * block_size.2;

        // Get register usage (estimated if not provided)
        let registers_per_thread =
            registers_per_thread.unwrap_or(self.estimate_register_usage(kernel_name)?);

        // Calculate limits
        let max_blocks_registers = if registers_per_thread > 0 {
            device_props.registers_per_multiprocessor
                / (registers_per_thread * threads_per_block).max(1)
        } else {
            u32::MAX
        };

        let max_blocks_shared_memory = if shared_memory_size > 0 {
            (device_props.shared_memory_per_multiprocessor / (shared_memory_size as usize).max(1))
                as u32
        } else {
            u32::MAX
        };

        let max_blocks_threads = device_props.max_threads_per_multiprocessor / threads_per_block;

        let max_blocks_physical = device_props.max_blocks_per_multiprocessor;

        // Find the most restrictive limit
        let max_active_blocks = max_blocks_registers
            .min(max_blocks_shared_memory)
            .min(max_blocks_threads)
            .min(max_blocks_physical);

        // Calculate theoretical occupancy
        let theoretical_occupancy = (max_active_blocks * threads_per_block) as f32
            / device_props.max_threads_per_multiprocessor as f32;

        // Identify limiting factors
        let mut limiting_factors = Vec::new();

        if max_active_blocks == max_blocks_registers {
            limiting_factors.push(LimitingFactor::Registers {
                used: registers_per_thread,
                limit: device_props.registers_per_multiprocessor / threads_per_block,
            });
        }

        if max_active_blocks == max_blocks_shared_memory {
            limiting_factors.push(LimitingFactor::SharedMemory {
                used: shared_memory_size,
                limit: device_props.shared_memory_per_multiprocessor as u32,
            });
        }

        if max_active_blocks == max_blocks_threads {
            limiting_factors.push(LimitingFactor::ThreadsPerBlock {
                used: threads_per_block,
                limit: device_props.max_threads_per_block,
            });
        }

        if max_active_blocks == max_blocks_physical {
            limiting_factors.push(LimitingFactor::BlocksPerSM {
                used: max_active_blocks,
                limit: device_props.max_blocks_per_multiprocessor,
            });
        }

        // Calculate warp efficiency
        let warps_per_block =
            (threads_per_block + device_props.warp_size - 1) / device_props.warp_size;
        let max_warps_per_sm = device_props.max_threads_per_multiprocessor / device_props.warp_size;
        let active_warps = max_active_blocks * warps_per_block;

        if active_warps > max_warps_per_sm {
            limiting_factors.push(LimitingFactor::WarpAllocation {
                used: active_warps,
                limit: max_warps_per_sm,
            });
        }

        let resource_usage = ResourceUsage {
            registers_per_thread,
            shared_memory_per_block: shared_memory_size,
            local_memory_per_thread: 0, // Would need profiling to determine
            threads_per_block,
            constant_memory: 0, // Would need analysis to determine
        };

        Ok(OccupancyResult {
            theoretical_occupancy,
            achieved_occupancy: None, // Would be filled by runtime measurement
            max_active_blocks,
            max_theoretical_blocks: device_props.max_blocks_per_multiprocessor,
            optimal_block_size: threads_per_block,
            min_grid_size: device_props.multiprocessor_count * max_active_blocks,
            limiting_factors,
            resource_usage,
            performance_metrics: None,
        })
    }

    /// Find optimal launch configuration for maximum occupancy
    pub fn optimize_launch_config(
        &mut self,
        kernel_name: &str,
        total_threads: u64,
        shared_memory_size: u32,
        registers_per_thread: Option<u32>,
    ) -> CudaResult<OptimizedLaunchConfig> {
        let mut best_config = OptimizedLaunchConfig {
            block_size: (32, 1, 1),
            grid_size: (1, 1, 1),
            shared_memory_size,
            expected_occupancy: 0.0,
            optimization_notes: String::new(),
        };

        let mut best_score = 0.0f32;
        let mut optimization_notes = Vec::new();

        // Try different block sizes
        for block_size in (self.optimization_heuristics.min_block_size
            ..=self.optimization_heuristics.max_block_size)
            .step_by(32)
        // Warp-aligned increments
        {
            let block_config = (block_size, 1, 1);

            let occupancy_result = self.analyze_kernel_occupancy(
                kernel_name,
                block_config,
                shared_memory_size,
                registers_per_thread,
            )?;

            // Calculate grid size for this block size
            let blocks_needed =
                ((total_threads + block_size as u64 - 1) / block_size as u64) as u32;
            let grid_size = (blocks_needed, 1, 1);

            // Score this configuration
            let score = self.score_configuration(&occupancy_result, block_size, blocks_needed);

            if score > best_score {
                best_score = score;
                best_config = OptimizedLaunchConfig {
                    block_size: block_config,
                    grid_size,
                    shared_memory_size,
                    expected_occupancy: occupancy_result.theoretical_occupancy,
                    optimization_notes: format!(
                        "Optimized for {} occupancy with {} limiting factors",
                        (occupancy_result.theoretical_occupancy * 100.0) as u32,
                        occupancy_result.limiting_factors.len()
                    ),
                };

                optimization_notes.clear();
                optimization_notes.push(format!(
                    "Block size {}: {:.1}% occupancy",
                    block_size,
                    occupancy_result.theoretical_occupancy * 100.0
                ));

                for factor in &occupancy_result.limiting_factors {
                    optimization_notes.push(format!("Limiting factor: {:?}", factor));
                }
            }
        }

        best_config.optimization_notes = optimization_notes.join("; ");

        // Use CUDA occupancy calculator if available
        if let Ok(cuda_optimal) =
            self.cuda_occupancy_max_potential_block_size(kernel_name, shared_memory_size)
        {
            if cuda_optimal.block_size > 0 {
                let cuda_blocks_needed = ((total_threads + cuda_optimal.block_size as u64 - 1)
                    / cuda_optimal.block_size as u64)
                    as u32;
                let cuda_grid_size = (cuda_blocks_needed, 1, 1);

                // Verify this configuration
                let cuda_occupancy = self.analyze_kernel_occupancy(
                    kernel_name,
                    (cuda_optimal.block_size, 1, 1),
                    shared_memory_size,
                    registers_per_thread,
                )?;

                if cuda_occupancy.theoretical_occupancy > best_config.expected_occupancy {
                    best_config = OptimizedLaunchConfig {
                        block_size: (cuda_optimal.block_size, 1, 1),
                        grid_size: cuda_grid_size,
                        shared_memory_size,
                        expected_occupancy: cuda_occupancy.theoretical_occupancy,
                        optimization_notes: format!(
                            "CUDA-optimized: {:.1}% occupancy, {} blocks",
                            cuda_occupancy.theoretical_occupancy * 100.0,
                            cuda_blocks_needed
                        ),
                    };
                }
            }
        }

        Ok(best_config)
    }

    /// Score a configuration based on occupancy and heuristics
    fn score_configuration(
        &self,
        occupancy: &OccupancyResult,
        block_size: u32,
        _blocks_needed: u32,
    ) -> f32 {
        let mut score = occupancy.theoretical_occupancy;

        // Bonus for achieving target occupancy
        if occupancy.theoretical_occupancy >= self.optimization_heuristics.target_occupancy {
            score += 0.1;
        }

        // Penalty for very small block sizes (poor warp utilization)
        if block_size < 64 {
            score *= 0.9;
        }

        // Penalty for very large block sizes (reduced flexibility)
        if block_size > 512 {
            score *= 0.95;
        }

        // Bonus for power-of-2 block sizes (often optimal)
        if block_size.is_power_of_two() {
            score += 0.05;
        }

        // Consider resource efficiency
        let register_efficiency = if occupancy.resource_usage.registers_per_thread > 0 {
            1.0 - (occupancy.resource_usage.registers_per_thread as f32 / 64.0).min(1.0)
        } else {
            1.0
        };

        score +=
            register_efficiency * self.optimization_heuristics.register_optimization_weight * 0.1;

        score
    }

    /// Estimate register usage for a kernel (placeholder for actual analysis)
    fn estimate_register_usage(&self, kernel_name: &str) -> CudaResult<u32> {
        // In a real implementation, this would use:
        // 1. CUDA compiler output parsing
        // 2. cuobjdump analysis
        // 3. Runtime profiling
        // 4. Historical data

        // For now, provide conservative estimates based on kernel name patterns
        let estimated = if kernel_name.contains("matmul") || kernel_name.contains("gemm") {
            48 // Matrix multiplication kernels typically use more registers
        } else if kernel_name.contains("conv") || kernel_name.contains("fft") {
            32 // Convolution and FFT kernels moderate usage
        } else if kernel_name.contains("reduce") || kernel_name.contains("scan") {
            24 // Reduction operations
        } else {
            16 // Simple element-wise operations
        };

        Ok(estimated)
    }

    /// Use CUDA's occupancy calculator (requires CUDA runtime)
    fn cuda_occupancy_max_potential_block_size(
        &self,
        _kernel_name: &str,
        dynamic_shared_memory: u32,
    ) -> CudaResult<CudaOptimalConfig> {
        // This would use cudaOccupancyMaxPotentialBlockSize
        // For now, return a reasonable estimate

        let device_props = self.device.properties()?;
        let max_threads = device_props.max_threads_per_block;

        // Start with maximum and work down based on constraints
        let mut optimal_block_size = max_threads;

        // Adjust for shared memory constraints
        if dynamic_shared_memory > 0 {
            let max_blocks_for_shared_mem = (device_props.shared_memory_per_multiprocessor
                / (dynamic_shared_memory as usize).max(1))
                as u32;
            let max_threads_for_shared_mem = max_blocks_for_shared_mem * max_threads;
            optimal_block_size = optimal_block_size.min(max_threads_for_shared_mem);
        }

        // Round down to nearest multiple of warp size
        optimal_block_size = (optimal_block_size / device_props.warp_size) * device_props.warp_size;

        // Ensure minimum size
        optimal_block_size = optimal_block_size.max(device_props.warp_size);

        let min_grid_size = device_props.multiprocessor_count
            * (device_props.max_threads_per_multiprocessor / optimal_block_size);

        Ok(CudaOptimalConfig {
            block_size: optimal_block_size,
            min_grid_size,
        })
    }

    /// Measure actual occupancy during kernel execution
    pub fn measure_runtime_occupancy(
        &mut self,
        kernel_name: &str,
        launch_config: &OptimizedLaunchConfig,
    ) -> CudaResult<f32> {
        // This would use CUDA profiling APIs or CUPTI to measure actual occupancy
        // For now, estimate based on theoretical occupancy with some variance

        let theoretical = self
            .analyze_kernel_occupancy(
                kernel_name,
                launch_config.block_size,
                launch_config.shared_memory_size,
                None,
            )?
            .theoretical_occupancy;

        // Simulate measurement variance (real implementation would use CUPTI)
        let variance = 0.05; // 5% measurement variance
                             // Use a simple deterministic "randomness" for testing
        let pseudo_random = (kernel_name.len() % 1000) as f32 / 1000.0;
        let actual = theoretical * (1.0 - variance + 2.0 * variance * pseudo_random);

        Ok(actual.min(1.0).max(0.0))
    }

    /// Generate occupancy optimization report
    pub fn generate_optimization_report(
        &mut self,
        kernel_configs: &[(String, OptimizedLaunchConfig)],
    ) -> String {
        let mut report = String::new();
        report.push_str("=== CUDA Occupancy Optimization Report ===\n\n");

        let mut total_theoretical_occupancy = 0.0;
        let mut total_kernels = 0;

        for (kernel_name, config) in kernel_configs {
            report.push_str(&format!("Kernel: {}\n", kernel_name));
            report.push_str(&format!("  Block Size: {:?}\n", config.block_size));
            report.push_str(&format!("  Grid Size: {:?}\n", config.grid_size));
            report.push_str(&format!(
                "  Expected Occupancy: {:.1}%\n",
                config.expected_occupancy * 100.0
            ));
            report.push_str(&format!(
                "  Shared Memory: {} bytes\n",
                config.shared_memory_size
            ));
            report.push_str(&format!("  Notes: {}\n", config.optimization_notes));
            report.push_str("\n");

            total_theoretical_occupancy += config.expected_occupancy;
            total_kernels += 1;
        }

        if total_kernels > 0 {
            report.push_str(&format!(
                "Average Theoretical Occupancy: {:.1}%\n",
                (total_theoretical_occupancy / total_kernels as f32) * 100.0
            ));
        }

        report.push_str(&format!("Target Device: {}\n", self.device.name()));
        report.push_str(&format!(
            "Compute Capability: {}.{}\n",
            self.device.compute_capability().0,
            self.device.compute_capability().1
        ));

        report
    }

    /// Clear occupancy cache
    pub fn clear_cache(&mut self) {
        self.cached_results.clear();
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> (usize, usize) {
        (self.cached_results.len(), self.cached_results.capacity())
    }
}

/// CUDA optimal configuration from occupancy calculator
#[derive(Debug, Clone)]
struct CudaOptimalConfig {
    block_size: u32,
    min_grid_size: u32,
}

/// Device properties needed for occupancy calculation
#[derive(Debug, Clone)]
pub struct DeviceProperties {
    pub multiprocessor_count: u32,
    pub max_threads_per_multiprocessor: u32,
    pub max_threads_per_block: u32,
    pub max_blocks_per_multiprocessor: u32,
    pub registers_per_multiprocessor: u32,
    pub shared_memory_per_multiprocessor: u32,
    pub warp_size: u32,
    pub compute_capability: (u32, u32),
}

/// Extension trait for CudaDevice to provide occupancy-related properties
pub trait CudaDeviceOccupancy {
    fn properties(&self) -> CudaResult<DeviceProperties>;
    fn compute_capability(&self) -> (u32, u32);
    fn name(&self) -> CudaResult<String>;
}

impl CudaDeviceOccupancy for CudaDevice {
    fn properties(&self) -> CudaResult<DeviceProperties> {
        // This would query actual CUDA device properties
        // For now, provide realistic defaults for common architectures
        Ok(DeviceProperties {
            multiprocessor_count: 108, // Example: RTX 4090
            max_threads_per_multiprocessor: 2048,
            max_threads_per_block: 1024,
            max_blocks_per_multiprocessor: 16,
            registers_per_multiprocessor: 65536,
            shared_memory_per_multiprocessor: 102400, // 100KB
            warp_size: 32,
            compute_capability: (8, 9), // Ada Lovelace
        })
    }

    fn compute_capability(&self) -> (u32, u32) {
        (8, 9) // Example: Ada Lovelace
    }

    fn name(&self) -> CudaResult<String> {
        Ok("NVIDIA RTX 4090".to_string()) // Example device name
    }
}

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

    #[test]
    #[ignore = "Requires CUDA hardware - run with --ignored flag"]
    fn test_occupancy_analyzer_creation() {
        if crate::cuda::is_available() {
            let device = CudaDevice::new(0).expect("Cuda Device should succeed");
            let analyzer = CudaOccupancyAnalyzer::new(device);
            assert_eq!(analyzer.cached_results.len(), 0);
        }
    }

    #[test]
    fn test_optimization_heuristics_default() {
        let heuristics = OptimizationHeuristics::default();
        assert_eq!(heuristics.target_occupancy, 0.75);
        assert!(heuristics.prefer_high_occupancy);
        assert!(heuristics.dynamic_block_sizing);
    }

    #[test]
    fn test_resource_usage_creation() {
        let usage = ResourceUsage {
            registers_per_thread: 32,
            shared_memory_per_block: 1024,
            local_memory_per_thread: 0,
            threads_per_block: 256,
            constant_memory: 0,
        };
        assert_eq!(usage.registers_per_thread, 32);
        assert_eq!(usage.shared_memory_per_block, 1024);
        assert_eq!(usage.threads_per_block, 256);
    }

    #[test]
    fn test_limiting_factor_identification() {
        let factor = LimitingFactor::Registers {
            used: 64,
            limit: 32,
        };
        assert_eq!(
            factor,
            LimitingFactor::Registers {
                used: 64,
                limit: 32
            }
        );
    }

    #[test]
    fn test_occupancy_calculation() {
        if crate::cuda::is_available() {
            let device = CudaDevice::new(0).expect("Cuda Device should succeed");
            let mut analyzer = CudaOccupancyAnalyzer::new(device);

            let result = analyzer.analyze_kernel_occupancy("test_kernel", (256, 1, 1), 0, Some(32));

            assert!(result.is_ok());
            let occupancy = result.expect("operation should succeed");
            assert!(occupancy.theoretical_occupancy >= 0.0);
            assert!(occupancy.theoretical_occupancy <= 1.0);
            assert!(occupancy.max_active_blocks > 0);
        }
    }

    #[test]
    fn test_launch_config_optimization() {
        if crate::cuda::is_available() {
            let device = CudaDevice::new(0).expect("Cuda Device should succeed");
            let mut analyzer = CudaOccupancyAnalyzer::new(device);

            let config = analyzer.optimize_launch_config("test_kernel", 1000000, 0, Some(24));

            assert!(config.is_ok());
            let optimized = config.expect("operation should succeed");
            assert!(optimized.block_size.0 >= 32);
            assert!(optimized.block_size.0 <= 1024);
            assert!(optimized.expected_occupancy > 0.0);
        }
    }

    #[test]
    fn test_cache_functionality() {
        if crate::cuda::is_available() {
            let device = CudaDevice::new(0).expect("Cuda Device should succeed");
            let mut analyzer = CudaOccupancyAnalyzer::new(device);

            // First call should compute
            let _result1 = analyzer
                .analyze_kernel_occupancy("test", (128, 1, 1), 0, Some(16))
                .expect("operation should succeed");
            assert_eq!(analyzer.cache_stats().0, 1);

            // Second call should use cache
            let _result2 = analyzer
                .analyze_kernel_occupancy("test", (128, 1, 1), 0, Some(16))
                .expect("operation should succeed");
            assert_eq!(analyzer.cache_stats().0, 1);

            analyzer.clear_cache();
            assert_eq!(analyzer.cache_stats().0, 0);
        }
    }
}