rustkernels 0.4.0

GPU-accelerated kernel library for financial services, analytics, and compliance workloads
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
//! # RustKernels
//!
//! GPU-accelerated kernel library for financial services, analytics, and compliance workloads.
//!
//! RustKernels is built on [RingKernel 0.4.2](https://crates.io/crates/ringkernel-core)
//! and provides 106 kernels across 14 domain-specific crates.
//!
//! ## Features
//!
//! - **14 Domain Categories**: Graph analytics, ML, compliance, risk, temporal analysis, and more
//! - **106 Kernels**: Comprehensive coverage of financial and analytical algorithms
//! - **Dual Execution Modes**:
//!   - **Batch**: CPU-orchestrated, 10-50μs overhead, for periodic heavy computation
//!   - **Ring**: GPU-persistent actor, 100-500ns latency, for high-frequency operations
//! - **Type-Erased Execution**: REST/gRPC dispatch via `TypeErasedBatchKernel`
//! - **Enterprise Features**: Security, observability, resilience, production configuration
//! - **Multi-Backend**: CUDA, WebGPU, and CPU backends via RingKernel
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use rustkernels::prelude::*;
//! use rustkernels::graph::centrality::{BetweennessCentrality, BetweennessCentralityInput};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let kernel = BetweennessCentrality::new();
//!
//!     let input = BetweennessCentralityInput {
//!         num_nodes: 4,
//!         edges: vec![(0, 1), (1, 2), (2, 3), (0, 3)],
//!         normalized: true,
//!     };
//!
//!     let result = kernel.execute(input).await?;
//!     for (node, score) in result.scores.iter().enumerate() {
//!         println!("Node {}: {:.4}", node, score);
//!     }
//!     Ok(())
//! }
//! ```
//!
//! ## Domain Organization
//!
//! Kernels are organized into 14 domains:
//!
//! | Domain | Kernels | Description |
//! |--------|---------|-------------|
//! | GraphAnalytics | 28 | Centrality, GNN inference, community detection |
//! | StatisticalML | 17 | Clustering, NLP embeddings, federated learning |
//! | Compliance | 11 | AML, KYC, sanctions screening |
//! | TemporalAnalysis | 7 | Forecasting, change-point detection |
//! | RiskAnalytics | 5 | Credit risk, VaR, stress testing |
//! | ProcessIntelligence | 7 | DFG, conformance, digital twin |
//! | BehavioralAnalytics | 6 | Profiling, forensics |
//! | Clearing | 5 | Netting, settlement, DVP |
//! | Treasury | 5 | Cash flow, FX hedging |
//! | Accounting | 9 | Network generation, reconciliation |
//! | Payments | 2 | Payment processing |
//! | Banking | 1 | Fraud detection |
//! | OrderMatching | 1 | HFT order book |
//! | FinancialAudit | 2 | Feature extraction |
//!
//! ## Feature Flags
//!
//! ```toml
//! [dependencies]
//! rustkernels = { version = "0.4", features = ["graph", "ml", "risk"] }
//! ```
//!
//! Available features:
//! - `default`: graph, ml, compliance, temporal, risk
//! - `full`: All 14 domains
//! - Individual: `graph`, `ml`, `compliance`, `temporal`, `risk`, `banking`, etc.

#![warn(missing_docs)]
#![warn(clippy::all)]

// Re-export core crate
pub use rustkernel_core as core;

// Re-export derive macros
pub use rustkernel_derive::{KernelMessage, gpu_kernel, kernel_state};

// Re-export ringkernel for direct access
pub use ringkernel;
pub use ringkernel_core;

// Domain re-exports (conditional on features)
#[cfg(feature = "graph")]
pub use rustkernel_graph as graph;

#[cfg(feature = "ml")]
pub use rustkernel_ml as ml;

#[cfg(feature = "compliance")]
pub use rustkernel_compliance as compliance;

#[cfg(feature = "temporal")]
pub use rustkernel_temporal as temporal;

#[cfg(feature = "risk")]
pub use rustkernel_risk as risk;

#[cfg(feature = "banking")]
pub use rustkernel_banking as banking;

#[cfg(feature = "behavioral")]
pub use rustkernel_behavioral as behavioral;

#[cfg(feature = "orderbook")]
pub use rustkernel_orderbook as orderbook;

#[cfg(feature = "procint")]
pub use rustkernel_procint as procint;

#[cfg(feature = "clearing")]
pub use rustkernel_clearing as clearing;

#[cfg(feature = "treasury")]
pub use rustkernel_treasury as treasury;

#[cfg(feature = "accounting")]
pub use rustkernel_accounting as accounting;

#[cfg(feature = "payments")]
pub use rustkernel_payments as payments;

#[cfg(feature = "audit")]
pub use rustkernel_audit as audit;

/// Prelude module for convenient imports.
///
/// Import everything you need with:
/// ```rust,ignore
/// use rustkernel::prelude::*;
/// ```
pub mod prelude {
    // Core types
    pub use rustkernel_core::prelude::*;

    // Derive macros
    pub use rustkernel_derive::{KernelMessage, gpu_kernel, kernel_state};

    // RingKernel types
    pub use ringkernel_core::{
        HlcTimestamp, KernelHandle, KernelId, KernelState, LaunchOptions, MessageId, RingContext,
        RingMessage,
    };
}

/// Version information.
pub mod version {
    /// Crate version.
    pub const VERSION: &str = env!("CARGO_PKG_VERSION");

    /// Minimum supported RingKernel version.
    pub const MIN_RINGKERNEL_VERSION: &str = "0.4.2";
}

/// Kernel catalog providing overview of all available kernels.
pub mod catalog {
    use rustkernel_core::domain::Domain;

    /// Domain information.
    #[derive(Debug, Clone)]
    pub struct DomainInfo {
        /// Domain enum value.
        pub domain: Domain,
        /// Human-readable name.
        pub name: &'static str,
        /// Description.
        pub description: &'static str,
        /// Number of kernels.
        pub kernel_count: usize,
        /// Feature flag to enable.
        pub feature: &'static str,
    }

    /// Get all domain information.
    pub fn domains() -> Vec<DomainInfo> {
        vec![
            DomainInfo {
                domain: Domain::GraphAnalytics,
                name: "Graph Analytics",
                description: "Centrality, GNN inference, community detection, similarity, topology",
                kernel_count: 28,
                feature: "graph",
            },
            DomainInfo {
                domain: Domain::StatisticalML,
                name: "Statistical ML",
                description: "Clustering, NLP embeddings, federated learning, anomaly detection, explainability",
                kernel_count: 17,
                feature: "ml",
            },
            DomainInfo {
                domain: Domain::Compliance,
                name: "Compliance",
                description: "AML pattern detection, KYC scoring, sanctions screening, transaction monitoring",
                kernel_count: 11,
                feature: "compliance",
            },
            DomainInfo {
                domain: Domain::TemporalAnalysis,
                name: "Temporal Analysis",
                description: "ARIMA, Prophet decomposition, change-point detection, seasonal decomposition",
                kernel_count: 7,
                feature: "temporal",
            },
            DomainInfo {
                domain: Domain::RiskAnalytics,
                name: "Risk Analytics",
                description: "Credit risk, Monte Carlo VaR, portfolio risk, stress testing, correlation",
                kernel_count: 5,
                feature: "risk",
            },
            DomainInfo {
                domain: Domain::Banking,
                name: "Banking",
                description: "Fraud pattern matching with graph analysis",
                kernel_count: 1,
                feature: "banking",
            },
            DomainInfo {
                domain: Domain::BehavioralAnalytics,
                name: "Behavioral Analytics",
                description: "Profiling, anomaly profiling, forensics, event correlation",
                kernel_count: 6,
                feature: "behavioral",
            },
            DomainInfo {
                domain: Domain::OrderMatching,
                name: "Order Matching",
                description: "High-frequency order book matching engine",
                kernel_count: 1,
                feature: "orderbook",
            },
            DomainInfo {
                domain: Domain::ProcessIntelligence,
                name: "Process Intelligence",
                description: "DFG construction, conformance checking, digital twin, next-activity prediction",
                kernel_count: 7,
                feature: "procint",
            },
            DomainInfo {
                domain: Domain::Clearing,
                name: "Clearing",
                description: "Settlement, DVP matching, netting calculation",
                kernel_count: 5,
                feature: "clearing",
            },
            DomainInfo {
                domain: Domain::TreasuryManagement,
                name: "Treasury Management",
                description: "Cash flow forecasting, collateral optimization, FX hedging",
                kernel_count: 5,
                feature: "treasury",
            },
            DomainInfo {
                domain: Domain::Accounting,
                name: "Accounting",
                description: "Network generation, reconciliation, duplicate detection, currency conversion",
                kernel_count: 9,
                feature: "accounting",
            },
            DomainInfo {
                domain: Domain::PaymentProcessing,
                name: "Payment Processing",
                description: "Transaction execution, flow analysis",
                kernel_count: 2,
                feature: "payments",
            },
            DomainInfo {
                domain: Domain::FinancialAudit,
                name: "Financial Audit",
                description: "Feature extraction, hypergraph construction",
                kernel_count: 2,
                feature: "audit",
            },
        ]
    }

    /// Get total kernel count across all domains.
    pub fn total_kernel_count() -> usize {
        domains().iter().map(|d| d.kernel_count).sum()
    }

    /// Check if a specific domain is enabled via compile-time features.
    pub fn is_domain_enabled(feature: &str) -> bool {
        enabled_domains().contains(&feature)
    }

    /// Get enabled domains based on compile-time features.
    #[allow(clippy::vec_init_then_push)]
    pub fn enabled_domains() -> Vec<&'static str> {
        let mut enabled = Vec::new();

        #[cfg(feature = "graph")]
        enabled.push("graph");
        #[cfg(feature = "ml")]
        enabled.push("ml");
        #[cfg(feature = "compliance")]
        enabled.push("compliance");
        #[cfg(feature = "temporal")]
        enabled.push("temporal");
        #[cfg(feature = "risk")]
        enabled.push("risk");
        #[cfg(feature = "banking")]
        enabled.push("banking");
        #[cfg(feature = "behavioral")]
        enabled.push("behavioral");
        #[cfg(feature = "orderbook")]
        enabled.push("orderbook");
        #[cfg(feature = "procint")]
        enabled.push("procint");
        #[cfg(feature = "clearing")]
        enabled.push("clearing");
        #[cfg(feature = "treasury")]
        enabled.push("treasury");
        #[cfg(feature = "accounting")]
        enabled.push("accounting");
        #[cfg(feature = "payments")]
        enabled.push("payments");
        #[cfg(feature = "audit")]
        enabled.push("audit");

        enabled
    }
}

/// Register all enabled domain kernels into a registry.
///
/// This is the primary entrypoint for populating a `KernelRegistry` with
/// all available kernels based on compile-time feature flags.
///
/// # Errors
///
/// Returns an error if any kernel registration fails.
pub fn register_all(
    registry: &rustkernel_core::registry::KernelRegistry,
) -> rustkernel_core::error::Result<()> {
    #[cfg(feature = "graph")]
    rustkernel_graph::register_all(registry)?;

    #[cfg(feature = "ml")]
    rustkernel_ml::register_all(registry)?;

    #[cfg(feature = "compliance")]
    rustkernel_compliance::register_all(registry)?;

    #[cfg(feature = "temporal")]
    rustkernel_temporal::register_all(registry)?;

    #[cfg(feature = "risk")]
    rustkernel_risk::register_all(registry)?;

    #[cfg(feature = "banking")]
    rustkernel_banking::register_all(registry)?;

    #[cfg(feature = "behavioral")]
    rustkernel_behavioral::register_all(registry)?;

    #[cfg(feature = "orderbook")]
    rustkernel_orderbook::register_all(registry)?;

    #[cfg(feature = "procint")]
    rustkernel_procint::register_all(registry)?;

    #[cfg(feature = "clearing")]
    rustkernel_clearing::register_all(registry)?;

    #[cfg(feature = "treasury")]
    rustkernel_treasury::register_all(registry)?;

    #[cfg(feature = "accounting")]
    rustkernel_accounting::register_all(registry)?;

    #[cfg(feature = "payments")]
    rustkernel_payments::register_all(registry)?;

    #[cfg(feature = "audit")]
    rustkernel_audit::register_all(registry)?;

    Ok(())
}

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

    #[test]
    fn test_prelude_imports() {
        use crate::prelude::*;

        // Verify core types are accessible
        let _domain = Domain::GraphAnalytics;
        let _mode = KernelMode::Ring;
    }

    #[test]
    #[allow(clippy::const_is_empty)]
    fn test_version() {
        assert!(!version::VERSION.is_empty());
        assert_eq!(version::MIN_RINGKERNEL_VERSION, "0.4.2");
    }

    #[test]
    fn test_catalog() {
        let domains = catalog::domains();
        assert_eq!(domains.len(), 14);
        assert_eq!(catalog::total_kernel_count(), 106);
    }

    #[test]
    fn test_enabled_domains() {
        let enabled = catalog::enabled_domains();
        // Default features include graph, ml, compliance, temporal, risk
        assert!(enabled.contains(&"graph"));
        assert!(enabled.contains(&"ml"));
    }

    #[test]
    fn test_register_all() {
        let registry = rustkernel_core::registry::KernelRegistry::new();
        register_all(&registry).unwrap();
        // Verify kernels were registered (at least default features)
        assert!(registry.total_count() > 0);
    }
}