ringkernel-accnet 1.1.0

GPU-accelerated accounting network analytics with real-time visualization
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
//! GPU kernel executor using ringkernel-cuda.
//!
//! This module provides actual GPU execution of the generated CUDA kernels
//! using the ringkernel-cuda infrastructure.

use std::time::Instant;

use cudarc::driver::{CudaFunction, LaunchConfig, PushKernelArg};
use ringkernel_cuda::{CudaDevice, StencilKernelLoader};

use crate::error::{AccNetError, Result};
use crate::models::AccountingNetwork;

/// GPU-accelerated analysis executor.
pub struct GpuExecutor {
    /// CUDA device.
    device: CudaDevice,
    /// Kernel loader for compiling CUDA code (reserved for dynamic kernel loading).
    #[allow(dead_code)]
    loader: StencilKernelLoader,
    /// Compiled suspense detection kernel.
    suspense_kernel: Option<CompiledKernel>,
    /// Compiled GAAP violation kernel.
    gaap_kernel: Option<CompiledKernel>,
    /// Compiled Benford analysis kernel.
    benford_kernel: Option<CompiledKernel>,
    /// Device name for reporting.
    device_name: String,
    /// Compute capability.
    compute_capability: (u32, u32),
}

/// A compiled CUDA kernel ready for execution.
struct CompiledKernel {
    /// Kernel function handle.
    func: CudaFunction,
}

/// Result of GPU analysis.
#[derive(Debug, Clone, Default)]
pub struct GpuAnalysisResult {
    /// Suspense scores for each account.
    pub suspense_scores: Vec<f32>,
    /// GAAP violation flags for each flow.
    pub gaap_violations: Vec<u8>,
    /// Benford digit counts (1-9).
    pub benford_counts: [u32; 9],
    /// Total GPU execution time in microseconds.
    pub execution_time_us: u64,
    /// Number of accounts processed.
    pub accounts_processed: usize,
    /// Number of flows processed.
    pub flows_processed: usize,
}

/// Benchmark results for a single kernel.
#[derive(Debug, Clone)]
pub struct KernelBenchmark {
    /// Kernel name.
    pub name: String,
    /// Execution time in microseconds.
    pub time_us: u64,
    /// Elements processed.
    pub elements: usize,
    /// Throughput in million elements per second.
    pub throughput_meps: f64,
}

/// Combined benchmark results.
#[derive(Debug, Clone)]
pub struct BenchmarkResults {
    /// Device name.
    pub device_name: String,
    /// Compute capability.
    pub compute_capability: (u32, u32),
    /// Individual kernel benchmarks.
    pub kernels: Vec<KernelBenchmark>,
    /// Total GPU time.
    pub total_gpu_time_us: u64,
    /// Total CPU baseline time.
    pub total_cpu_time_us: u64,
    /// Overall speedup factor.
    pub speedup: f64,
}

impl GpuExecutor {
    /// Create a new GPU executor.
    pub fn new() -> Result<Self> {
        // Check if CUDA is available
        if !ringkernel_cuda::is_cuda_available() {
            return Err(AccNetError::CudaUnavailable);
        }

        // Create device
        let device = CudaDevice::new(0).map_err(|e| AccNetError::DeviceCreation(e.to_string()))?;

        let device_name = device.name().to_string();
        let compute_capability = device.compute_capability();

        let loader = StencilKernelLoader::new(device.clone());

        Ok(Self {
            device,
            loader,
            suspense_kernel: None,
            gaap_kernel: None,
            benford_kernel: None,
            device_name,
            compute_capability,
        })
    }

    /// Get device name.
    pub fn device_name(&self) -> &str {
        &self.device_name
    }

    /// Get compute capability.
    pub fn compute_capability(&self) -> (u32, u32) {
        self.compute_capability
    }

    /// Compile all analysis kernels.
    pub fn compile_kernels(&mut self) -> Result<()> {
        // Generate kernel code
        let kernels = super::codegen::GeneratedKernels::generate()?;

        // Compile suspense detection kernel
        self.suspense_kernel = Some(CompiledKernel {
            func: self.compile_kernel("suspense_detection", &kernels.suspense_detection)?,
        });

        // Compile GAAP violation kernel
        self.gaap_kernel = Some(CompiledKernel {
            func: self.compile_kernel("gaap_violation", &kernels.gaap_violation)?,
        });

        // Compile Benford analysis kernel
        self.benford_kernel = Some(CompiledKernel {
            func: self.compile_kernel("benford_analysis", &kernels.benford_analysis)?,
        });

        Ok(())
    }

    /// Compile a single kernel from CUDA source and return its function handle.
    fn compile_kernel(&self, name: &str, cuda_source: &str) -> Result<CudaFunction> {
        let cuda_context = self.device.inner();

        // Compile CUDA source to PTX using NVRTC
        let ptx =
            cudarc::nvrtc::compile_ptx(cuda_source).map_err(|e| AccNetError::NvrtcCompilation {
                kernel: name.to_string(),
                reason: e.to_string(),
            })?;

        // Load the PTX module
        let module = cuda_context
            .load_module(ptx)
            .map_err(|e| AccNetError::PtxLoad {
                kernel: name.to_string(),
                reason: e.to_string(),
            })?;

        // Load the kernel function from the module
        let func = module
            .load_function(name)
            .map_err(|e| AccNetError::KernelNotFound(format!("{}: {}", name, e)))?;

        Ok(func)
    }

    /// Run GPU analysis on the network.
    pub fn analyze(&self, network: &AccountingNetwork) -> Result<GpuAnalysisResult> {
        let start = Instant::now();

        let n_accounts = network.accounts.len();
        let n_flows = network.flows.len();

        if n_accounts == 0 {
            return Ok(GpuAnalysisResult::default());
        }

        let mut result = GpuAnalysisResult {
            accounts_processed: n_accounts,
            flows_processed: n_flows,
            ..Default::default()
        };

        // === Run Suspense Detection ===
        if let Some(ref kernel) = self.suspense_kernel {
            let suspense_scores = self.run_suspense_detection(network, kernel)?;
            result.suspense_scores = suspense_scores;
        }

        // === Run GAAP Violation Detection ===
        if let Some(ref kernel) = self.gaap_kernel {
            if n_flows > 0 {
                let violations = self.run_gaap_violation(network, kernel)?;
                result.gaap_violations = violations;
            }
        }

        // === Run Benford Analysis ===
        if let Some(ref kernel) = self.benford_kernel {
            if n_flows > 0 {
                let counts = self.run_benford_analysis(network, kernel)?;
                result.benford_counts = counts;
            }
        }

        // Synchronize to ensure all GPU work is done
        self.device
            .synchronize()
            .map_err(|e| AccNetError::GpuSync(e.to_string()))?;

        result.execution_time_us = start.elapsed().as_micros() as u64;

        Ok(result)
    }

    /// Run suspense detection kernel.
    fn run_suspense_detection(
        &self,
        network: &AccountingNetwork,
        kernel: &CompiledKernel,
    ) -> Result<Vec<f32>> {
        let stream = self.device.stream();
        let n = network.accounts.len();

        // Prepare input data
        let balance_debit: Vec<f64> = network
            .accounts
            .iter()
            .map(|a| a.total_debits.to_f64())
            .collect();
        let balance_credit: Vec<f64> = network
            .accounts
            .iter()
            .map(|a| a.total_credits.to_f64())
            .collect();
        let risk_scores: Vec<f32> = network.accounts.iter().map(|a| a.risk_score).collect();
        let inflow_counts: Vec<u32> = network
            .accounts
            .iter()
            .map(|a| a.in_degree as u32)
            .collect();
        let outflow_counts: Vec<u32> = network
            .accounts
            .iter()
            .map(|a| a.out_degree as u32)
            .collect();

        // Allocate GPU memory and copy host data to device
        let d_balance_debit =
            stream
                .clone_htod(&balance_debit)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "balance_debit".into(),
                    reason: e.to_string(),
                })?;
        let d_balance_credit =
            stream
                .clone_htod(&balance_credit)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "balance_credit".into(),
                    reason: e.to_string(),
                })?;
        let d_risk_scores =
            stream
                .clone_htod(&risk_scores)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "risk_scores".into(),
                    reason: e.to_string(),
                })?;
        let d_inflow_counts =
            stream
                .clone_htod(&inflow_counts)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "inflow_counts".into(),
                    reason: e.to_string(),
                })?;
        let d_outflow_counts =
            stream
                .clone_htod(&outflow_counts)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "outflow_counts".into(),
                    reason: e.to_string(),
                })?;

        // Allocate output buffer
        // SAFETY: cudarc's alloc returns properly aligned device memory. The size
        // is computed from the input data and checked by the caller.
        let mut d_suspense_scores =
            unsafe { stream.alloc::<f32>(n) }.map_err(|e| AccNetError::GpuAlloc {
                field: "suspense_scores".into(),
                reason: e.to_string(),
            })?;

        // Calculate grid dimensions
        let block_size = 256u32;
        let grid_size = (n as u32).div_ceil(block_size);

        // Launch kernel
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };

        // SAFETY: Kernel arguments match the compiled PTX signature. Device pointers
        // are valid and allocated with sufficient size. Grid/block dimensions are
        // computed to cover the input data.
        unsafe {
            stream
                .launch_builder(&kernel.func)
                .arg(&d_balance_debit)
                .arg(&d_balance_credit)
                .arg(&d_risk_scores)
                .arg(&d_inflow_counts)
                .arg(&d_outflow_counts)
                .arg(&mut d_suspense_scores)
                .arg(&(n as i32))
                .launch(cfg)
        }
        .map_err(|e| AccNetError::KernelLaunch(e.to_string()))?;

        // Copy results back
        let suspense_scores = stream
            .clone_dtoh(&d_suspense_scores)
            .map_err(|e| AccNetError::DeviceToHost(e.to_string()))?;

        Ok(suspense_scores)
    }

    /// Run GAAP violation detection kernel.
    fn run_gaap_violation(
        &self,
        network: &AccountingNetwork,
        kernel: &CompiledKernel,
    ) -> Result<Vec<u8>> {
        let stream = self.device.stream();
        let n_flows = network.flows.len();

        // Prepare input data
        let flow_source: Vec<u16> = network
            .flows
            .iter()
            .map(|f| f.source_account_index)
            .collect();
        let flow_target: Vec<u16> = network
            .flows
            .iter()
            .map(|f| f.target_account_index)
            .collect();
        let account_types: Vec<u8> = network
            .accounts
            .iter()
            .map(|a| a.account_type as u8)
            .collect();

        // Allocate GPU memory and copy host data to device
        let d_flow_source =
            stream
                .clone_htod(&flow_source)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "flow_source".into(),
                    reason: e.to_string(),
                })?;
        let d_flow_target =
            stream
                .clone_htod(&flow_target)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "flow_target".into(),
                    reason: e.to_string(),
                })?;
        let d_account_types =
            stream
                .clone_htod(&account_types)
                .map_err(|e| AccNetError::HostToDevice {
                    field: "account_types".into(),
                    reason: e.to_string(),
                })?;

        // Allocate output buffer
        // SAFETY: cudarc's alloc returns properly aligned device memory. The size
        // is computed from the input data and checked by the caller.
        let mut d_violation_flags =
            unsafe { stream.alloc::<u8>(n_flows) }.map_err(|e| AccNetError::GpuAlloc {
                field: "violation_flags".into(),
                reason: e.to_string(),
            })?;

        // Calculate grid dimensions
        let block_size = 256u32;
        let grid_size = (n_flows as u32).div_ceil(block_size);

        // Launch kernel
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };

        // SAFETY: Kernel arguments match the compiled PTX signature. Device pointers
        // are valid and allocated with sufficient size. Grid/block dimensions are
        // computed to cover the input data.
        unsafe {
            stream
                .launch_builder(&kernel.func)
                .arg(&d_flow_source)
                .arg(&d_flow_target)
                .arg(&d_account_types)
                .arg(&mut d_violation_flags)
                .arg(&(n_flows as i32))
                .launch(cfg)
        }
        .map_err(|e| AccNetError::KernelLaunch(e.to_string()))?;

        // Copy results back
        let violations = stream
            .clone_dtoh(&d_violation_flags)
            .map_err(|e| AccNetError::DeviceToHost(e.to_string()))?;

        Ok(violations)
    }

    /// Run Benford analysis kernel.
    fn run_benford_analysis(
        &self,
        network: &AccountingNetwork,
        kernel: &CompiledKernel,
    ) -> Result<[u32; 9]> {
        let stream = self.device.stream();
        let n_flows = network.flows.len();

        // Prepare input data - extract amounts from flows
        let amounts: Vec<f64> = network
            .flows
            .iter()
            .map(|f| f.amount.to_f64().abs())
            .collect();

        // Allocate GPU memory and copy host data to device
        let d_amounts = stream
            .clone_htod(&amounts)
            .map_err(|e| AccNetError::HostToDevice {
                field: "amounts".into(),
                reason: e.to_string(),
            })?;

        // Allocate and zero-initialize digit counts
        let mut d_digit_counts =
            stream
                .clone_htod(&vec![0u32; 9])
                .map_err(|e| AccNetError::HostToDevice {
                    field: "digit_counts".into(),
                    reason: e.to_string(),
                })?;

        // Calculate grid dimensions
        let block_size = 256u32;
        let grid_size = (n_flows as u32).div_ceil(block_size);

        // Launch kernel
        let cfg = LaunchConfig {
            grid_dim: (grid_size, 1, 1),
            block_dim: (block_size, 1, 1),
            shared_mem_bytes: 0,
        };

        // SAFETY: Kernel arguments match the compiled PTX signature. Device pointers
        // are valid and allocated with sufficient size. Grid/block dimensions are
        // computed to cover the input data.
        unsafe {
            stream
                .launch_builder(&kernel.func)
                .arg(&d_amounts)
                .arg(&mut d_digit_counts)
                .arg(&(n_flows as i32))
                .launch(cfg)
        }
        .map_err(|e| AccNetError::KernelLaunch(e.to_string()))?;

        // Copy results back
        let counts_vec = stream
            .clone_dtoh(&d_digit_counts)
            .map_err(|e| AccNetError::DeviceToHost(e.to_string()))?;

        let mut counts = [0u32; 9];
        counts.copy_from_slice(&counts_vec);

        Ok(counts)
    }

    /// Run benchmarks comparing CPU vs GPU performance.
    pub fn run_benchmarks(&self, network: &AccountingNetwork) -> Result<BenchmarkResults> {
        let mut kernels = Vec::new();
        let mut total_gpu_time = 0u64;

        // Benchmark Suspense Detection
        if let Some(ref kernel) = self.suspense_kernel {
            let start = Instant::now();
            for _ in 0..10 {
                self.run_suspense_detection(network, kernel)?;
            }
            self.device
                .synchronize()
                .map_err(|e| AccNetError::GpuSync(e.to_string()))?;
            let elapsed = start.elapsed().as_micros() as u64 / 10;

            let n = network.accounts.len();
            kernels.push(KernelBenchmark {
                name: "Suspense Detection".to_string(),
                time_us: elapsed,
                elements: n,
                throughput_meps: if elapsed > 0 {
                    n as f64 / elapsed as f64
                } else {
                    0.0
                },
            });
            total_gpu_time += elapsed;
        }

        // Benchmark GAAP Violation
        if let Some(ref kernel) = self.gaap_kernel {
            if !network.flows.is_empty() {
                let start = Instant::now();
                for _ in 0..10 {
                    self.run_gaap_violation(network, kernel)?;
                }
                self.device
                    .synchronize()
                    .map_err(|e| AccNetError::GpuSync(e.to_string()))?;
                let elapsed = start.elapsed().as_micros() as u64 / 10;

                let n = network.flows.len();
                kernels.push(KernelBenchmark {
                    name: "GAAP Violation".to_string(),
                    time_us: elapsed,
                    elements: n,
                    throughput_meps: if elapsed > 0 {
                        n as f64 / elapsed as f64
                    } else {
                        0.0
                    },
                });
                total_gpu_time += elapsed;
            }
        }

        // Benchmark Benford Analysis
        if let Some(ref kernel) = self.benford_kernel {
            if !network.flows.is_empty() {
                let start = Instant::now();
                for _ in 0..10 {
                    self.run_benford_analysis(network, kernel)?;
                }
                self.device
                    .synchronize()
                    .map_err(|e| AccNetError::GpuSync(e.to_string()))?;
                let elapsed = start.elapsed().as_micros() as u64 / 10;

                let n = network.flows.len();
                kernels.push(KernelBenchmark {
                    name: "Benford Analysis".to_string(),
                    time_us: elapsed,
                    elements: n,
                    throughput_meps: if elapsed > 0 {
                        n as f64 / elapsed as f64
                    } else {
                        0.0
                    },
                });
                total_gpu_time += elapsed;
            }
        }

        // Run CPU baseline
        let cpu_start = Instant::now();
        for _ in 0..10 {
            self.cpu_baseline(network);
        }
        let total_cpu_time = cpu_start.elapsed().as_micros() as u64 / 10;

        let speedup = if total_gpu_time > 0 {
            total_cpu_time as f64 / total_gpu_time as f64
        } else {
            0.0
        };

        Ok(BenchmarkResults {
            device_name: self.device_name.clone(),
            compute_capability: self.compute_capability,
            kernels,
            total_gpu_time_us: total_gpu_time,
            total_cpu_time_us: total_cpu_time,
            speedup,
        })
    }

    /// CPU baseline for comparison.
    fn cpu_baseline(&self, network: &AccountingNetwork) {
        // Suspense detection
        let _suspense: Vec<f32> = network
            .accounts
            .iter()
            .map(|a| {
                let balance = a.total_debits.to_f64() - a.total_credits.to_f64();
                let mut score = 0.0f32;
                if balance.abs() > 0.0 && balance.abs() % 1000.0 < 1.0 {
                    score += 0.3;
                }
                if a.risk_score > 0.5 {
                    score += 0.4;
                }
                let flow_ratio = if a.out_degree > 0 {
                    a.in_degree as f32 / a.out_degree as f32
                } else {
                    10.0
                };
                if flow_ratio > 5.0 {
                    score += 0.3;
                }
                score.min(1.0)
            })
            .collect();

        // GAAP violations
        let _violations: Vec<u8> = network
            .flows
            .iter()
            .map(|f| {
                let src_type = network
                    .accounts
                    .get(f.source_account_index as usize)
                    .map(|a| a.account_type as u8)
                    .unwrap_or(0);
                let tgt_type = network
                    .accounts
                    .get(f.target_account_index as usize)
                    .map(|a| a.account_type as u8)
                    .unwrap_or(0);

                if src_type == 3 && tgt_type == 0 {
                    1
                } else if src_type == 3 && tgt_type == 4 {
                    2
                } else {
                    0
                }
            })
            .collect();

        // Benford analysis
        let mut _digit_counts = [0u32; 9];
        for flow in &network.flows {
            let amount = flow.amount.to_f64().abs();
            if amount >= 1.0 {
                let mut value = amount;
                while value >= 10.0 {
                    value /= 10.0;
                }
                let first_digit = value as usize;
                if (1..=9).contains(&first_digit) {
                    _digit_counts[first_digit - 1] += 1;
                }
            }
        }
    }
}

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

    #[test]
    fn test_gpu_executor_creation() {
        // This test will only pass if CUDA is available
        if ringkernel_cuda::is_cuda_available() {
            let executor = GpuExecutor::new();
            assert!(executor.is_ok());
        }
    }
}