ringkernel-ecosystem 1.1.0

Ecosystem integrations for RingKernel - actors, web frameworks, data processing, ML
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
//! Polars DataFrame integration for RingKernel.
//!
//! This module provides GPU-accelerated operations for Polars DataFrames,
//! enabling high-performance data processing with familiar Polars APIs.
//!
//! # Example
//!
//! ```ignore
//! use ringkernel_ecosystem::polars::GpuOps;
//!
//! let result = series.gpu_add(&other, &runtime).await?;
//! ```

use polars::prelude::*;
use std::sync::Arc;
use std::time::Duration;

use crate::error::{EcosystemError, Result};

/// Configuration for Polars GPU operations.
#[derive(Debug, Clone)]
pub struct PolarsConfig {
    /// Default timeout for GPU operations.
    pub timeout: Duration,
    /// Enable automatic type conversion.
    pub auto_convert: bool,
    /// Maximum series length for single GPU call.
    pub max_series_length: usize,
}

impl Default for PolarsConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            auto_convert: true,
            max_series_length: 10_000_000,
        }
    }
}

/// Runtime handle trait for Polars integration.
#[async_trait::async_trait]
pub trait RuntimeHandle: Send + Sync + 'static {
    /// Send data to a kernel for binary operation.
    async fn send_binary_op(&self, kernel_id: &str, a: Vec<u8>, b: Vec<u8>, op: &str)
        -> Result<()>;

    /// Receive result from a kernel.
    async fn receive_result(&self, kernel_id: &str, timeout: Duration) -> Result<Vec<u8>>;

    /// Send data for unary operation.
    async fn send_unary_op(&self, kernel_id: &str, data: Vec<u8>, op: &str) -> Result<()>;
}

/// Extension trait for GPU operations on Polars Series.
#[async_trait::async_trait]
pub trait GpuOps {
    /// Add two series on GPU.
    async fn gpu_add<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series>;

    /// Subtract two series on GPU.
    async fn gpu_sub<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series>;

    /// Multiply two series on GPU.
    async fn gpu_mul<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series>;

    /// Divide two series on GPU.
    async fn gpu_div<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series>;

    /// Apply GPU kernel to series.
    async fn gpu_apply<R: RuntimeHandle>(
        &self,
        runtime: &R,
        kernel_id: &str,
        config: &PolarsConfig,
    ) -> Result<Series>;
}

#[async_trait::async_trait]
impl GpuOps for Series {
    async fn gpu_add<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series> {
        gpu_binary_op(self, other, runtime, "add").await
    }

    async fn gpu_sub<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series> {
        gpu_binary_op(self, other, runtime, "sub").await
    }

    async fn gpu_mul<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series> {
        gpu_binary_op(self, other, runtime, "mul").await
    }

    async fn gpu_div<R: RuntimeHandle>(&self, other: &Series, runtime: &R) -> Result<Series> {
        gpu_binary_op(self, other, runtime, "div").await
    }

    async fn gpu_apply<R: RuntimeHandle>(
        &self,
        runtime: &R,
        kernel_id: &str,
        config: &PolarsConfig,
    ) -> Result<Series> {
        let data = series_to_bytes(self)?;
        runtime.send_unary_op(kernel_id, data, "apply").await?;

        let result_bytes = runtime.receive_result(kernel_id, config.timeout).await?;
        bytes_to_series(&result_bytes, self.name(), self.dtype())
    }
}

async fn gpu_binary_op<R: RuntimeHandle>(
    a: &Series,
    b: &Series,
    runtime: &R,
    op: &str,
) -> Result<Series> {
    if a.len() != b.len() {
        return Err(EcosystemError::DataConversion(
            "Series must have the same length".into(),
        ));
    }

    let a_bytes = series_to_bytes(a)?;
    let b_bytes = series_to_bytes(b)?;

    runtime
        .send_binary_op("binary_op", a_bytes, b_bytes, op)
        .await?;

    let result_bytes = runtime
        .receive_result("binary_op", Duration::from_secs(30))
        .await?;

    bytes_to_series(&result_bytes, a.name(), a.dtype())
}

/// Convert a Series to bytes.
fn series_to_bytes(series: &Series) -> Result<Vec<u8>> {
    match series.dtype() {
        DataType::Float32 => {
            let ca = series
                .f32()
                .map_err(|e| EcosystemError::Polars(e.to_string()))?;
            let values: Vec<f32> = ca.into_iter().map(|v| v.unwrap_or(0.0)).collect();
            Ok(values.iter().flat_map(|v| v.to_le_bytes()).collect())
        }
        DataType::Float64 => {
            let ca = series
                .f64()
                .map_err(|e| EcosystemError::Polars(e.to_string()))?;
            let values: Vec<f64> = ca.into_iter().map(|v| v.unwrap_or(0.0)).collect();
            Ok(values.iter().flat_map(|v| v.to_le_bytes()).collect())
        }
        DataType::Int32 => {
            let ca = series
                .i32()
                .map_err(|e| EcosystemError::Polars(e.to_string()))?;
            let values: Vec<i32> = ca.into_iter().map(|v| v.unwrap_or(0)).collect();
            Ok(values.iter().flat_map(|v| v.to_le_bytes()).collect())
        }
        DataType::Int64 => {
            let ca = series
                .i64()
                .map_err(|e| EcosystemError::Polars(e.to_string()))?;
            let values: Vec<i64> = ca.into_iter().map(|v| v.unwrap_or(0)).collect();
            Ok(values.iter().flat_map(|v| v.to_le_bytes()).collect())
        }
        dt => Err(EcosystemError::DataConversion(format!(
            "Unsupported dtype: {:?}",
            dt
        ))),
    }
}

/// Convert bytes to a Series.
fn bytes_to_series(bytes: &[u8], name: &PlSmallStr, dtype: &DataType) -> Result<Series> {
    match dtype {
        DataType::Float32 => {
            let values: Vec<f32> = bytes
                .chunks_exact(4)
                .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                .collect();
            Ok(Series::new(name.clone(), values))
        }
        DataType::Float64 => {
            let values: Vec<f64> = bytes
                .chunks_exact(8)
                .map(|chunk| {
                    f64::from_le_bytes([
                        chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6],
                        chunk[7],
                    ])
                })
                .collect();
            Ok(Series::new(name.clone(), values))
        }
        DataType::Int32 => {
            let values: Vec<i32> = bytes
                .chunks_exact(4)
                .map(|chunk| i32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                .collect();
            Ok(Series::new(name.clone(), values))
        }
        DataType::Int64 => {
            let values: Vec<i64> = bytes
                .chunks_exact(8)
                .map(|chunk| {
                    i64::from_le_bytes([
                        chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6],
                        chunk[7],
                    ])
                })
                .collect();
            Ok(Series::new(name.clone(), values))
        }
        dt => Err(EcosystemError::DataConversion(format!(
            "Unsupported dtype: {:?}",
            dt
        ))),
    }
}

/// Extension trait for GPU operations on DataFrames.
#[async_trait::async_trait]
pub trait DataFrameGpuOps {
    /// Apply a GPU kernel to all numeric columns.
    async fn gpu_apply_all<R: RuntimeHandle>(
        &self,
        runtime: &R,
        kernel_id: &str,
        config: &PolarsConfig,
    ) -> Result<DataFrame>;
}

#[async_trait::async_trait]
impl DataFrameGpuOps for DataFrame {
    async fn gpu_apply_all<R: RuntimeHandle>(
        &self,
        runtime: &R,
        kernel_id: &str,
        config: &PolarsConfig,
    ) -> Result<DataFrame> {
        let mut result_columns = Vec::with_capacity(self.width());

        for col in self.get_columns() {
            let processed = if col.dtype().is_numeric() {
                col.clone().gpu_apply(runtime, kernel_id, config).await?
            } else {
                col.clone()
            };
            result_columns.push(processed);
        }

        DataFrame::new(result_columns).map_err(|e| EcosystemError::Polars(e.to_string()))
    }
}

/// GPU-accelerated aggregation operations.
pub struct GpuAggregator<R> {
    runtime: Arc<R>,
    config: PolarsConfig,
}

impl<R: RuntimeHandle> GpuAggregator<R> {
    /// Create a new GPU aggregator.
    pub fn new(runtime: Arc<R>) -> Self {
        Self {
            runtime,
            config: PolarsConfig::default(),
        }
    }

    /// Set configuration.
    pub fn with_config(mut self, config: PolarsConfig) -> Self {
        self.config = config;
        self
    }

    /// Sum a series on GPU.
    pub async fn sum(&self, series: &Series) -> Result<f64> {
        let data = series_to_bytes(series)?;
        self.runtime.send_unary_op("sum", data, "sum").await?;

        let result_bytes = self
            .runtime
            .receive_result("sum", self.config.timeout)
            .await?;

        if result_bytes.len() >= 8 {
            Ok(f64::from_le_bytes([
                result_bytes[0],
                result_bytes[1],
                result_bytes[2],
                result_bytes[3],
                result_bytes[4],
                result_bytes[5],
                result_bytes[6],
                result_bytes[7],
            ]))
        } else {
            Err(EcosystemError::DataConversion("Invalid result size".into()))
        }
    }

    /// Mean of a series on GPU.
    pub async fn mean(&self, series: &Series) -> Result<f64> {
        let data = series_to_bytes(series)?;
        self.runtime.send_unary_op("mean", data, "mean").await?;

        let result_bytes = self
            .runtime
            .receive_result("mean", self.config.timeout)
            .await?;

        if result_bytes.len() >= 8 {
            Ok(f64::from_le_bytes([
                result_bytes[0],
                result_bytes[1],
                result_bytes[2],
                result_bytes[3],
                result_bytes[4],
                result_bytes[5],
                result_bytes[6],
                result_bytes[7],
            ]))
        } else {
            Err(EcosystemError::DataConversion("Invalid result size".into()))
        }
    }
}

// ============================================================================
// Enhanced GPU Operations for Polars
// ============================================================================

/// GPU window function type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpuWindowFunction {
    /// Row number
    RowNumber,
    /// Rank (with gaps)
    Rank,
    /// Dense rank (no gaps)
    DenseRank,
    /// Cumulative sum
    CumSum,
    /// Cumulative max
    CumMax,
    /// Cumulative min
    CumMin,
    /// Lead (look ahead)
    Lead,
    /// Lag (look behind)
    Lag,
    /// First value in window
    FirstValue,
    /// Last value in window
    LastValue,
    /// Nth value in window
    NthValue,
}

/// Window specification for GPU operations.
#[derive(Debug, Clone)]
pub struct GpuWindowSpec {
    /// Partition by columns
    pub partition_by: Vec<String>,
    /// Order by columns
    pub order_by: Vec<String>,
    /// Ascending order for each order_by column
    pub ascending: Vec<bool>,
    /// Window frame start (relative to current row)
    pub frame_start: i64,
    /// Window frame end (relative to current row)
    pub frame_end: i64,
}

impl Default for GpuWindowSpec {
    fn default() -> Self {
        Self {
            partition_by: Vec::new(),
            order_by: Vec::new(),
            ascending: Vec::new(),
            frame_start: i64::MIN, // Unbounded preceding
            frame_end: 0,          // Current row
        }
    }
}

impl GpuWindowSpec {
    /// Create a new window specification.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add partition by columns.
    pub fn partition_by(mut self, columns: &[&str]) -> Self {
        self.partition_by = columns.iter().map(|s| s.to_string()).collect();
        self
    }

    /// Add order by columns.
    pub fn order_by(mut self, columns: &[&str], ascending: &[bool]) -> Self {
        self.order_by = columns.iter().map(|s| s.to_string()).collect();
        self.ascending = ascending.to_vec();
        self
    }

    /// Set window frame.
    pub fn frame(mut self, start: i64, end: i64) -> Self {
        self.frame_start = start;
        self.frame_end = end;
        self
    }

    /// Rolling window (last n rows).
    pub fn rolling(mut self, size: i64) -> Self {
        self.frame_start = -(size - 1);
        self.frame_end = 0;
        self
    }
}

/// GPU groupby aggregation type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpuGroupByAgg {
    /// Sum
    Sum,
    /// Mean
    Mean,
    /// Min
    Min,
    /// Max
    Max,
    /// Count
    Count,
    /// First value
    First,
    /// Last value
    Last,
    /// Standard deviation
    Std,
    /// Variance
    Var,
    /// Median
    Median,
}

/// Configuration for GPU groupby operations.
#[derive(Debug, Clone)]
pub struct GpuGroupByConfig {
    /// Aggregations to perform
    pub aggregations: Vec<(String, GpuGroupByAgg)>,
    /// Use hash-based groupby
    pub use_hash: bool,
    /// Maximum number of groups
    pub max_groups: usize,
    /// Sort output by group keys
    pub sort_output: bool,
}

impl Default for GpuGroupByConfig {
    fn default() -> Self {
        Self {
            aggregations: Vec::new(),
            use_hash: true,
            max_groups: 1_000_000,
            sort_output: false,
        }
    }
}

impl GpuGroupByConfig {
    /// Create a new groupby config.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an aggregation.
    pub fn agg(mut self, column: &str, agg: GpuGroupByAgg) -> Self {
        self.aggregations.push((column.to_string(), agg));
        self
    }

    /// Sort output by keys.
    pub fn sorted(mut self) -> Self {
        self.sort_output = true;
        self
    }
}

/// Extended runtime handle for enhanced Polars GPU operations.
#[async_trait::async_trait]
pub trait GpuPolarsOps: Send + Sync + 'static {
    /// GPU-accelerated window function.
    async fn gpu_window(
        &self,
        kernel_id: &str,
        data: Vec<u8>,
        func: GpuWindowFunction,
        spec: &GpuWindowSpec,
    ) -> Result<Vec<u8>>;

    /// GPU-accelerated groupby.
    async fn gpu_groupby(
        &self,
        kernel_id: &str,
        keys: Vec<u8>,
        values: Vec<u8>,
        config: &GpuGroupByConfig,
    ) -> Result<(Vec<u8>, Vec<u8>)>;

    /// GPU-accelerated join.
    async fn gpu_join(
        &self,
        kernel_id: &str,
        left: Vec<u8>,
        right: Vec<u8>,
        join_type: GpuJoinType,
    ) -> Result<Vec<u8>>;

    /// GPU-accelerated sort.
    async fn gpu_sort(&self, kernel_id: &str, data: Vec<u8>, descending: bool) -> Result<Vec<u8>>;
}

/// GPU join type for Polars.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpuJoinType {
    /// Inner join
    Inner,
    /// Left join
    Left,
    /// Right join
    Right,
    /// Outer join
    Outer,
    /// Cross join
    Cross,
    /// Semi join
    Semi,
    /// Anti join
    Anti,
}

/// Result of GPU groupby operation.
#[derive(Debug, Clone)]
pub struct GpuGroupByResult {
    /// Group keys
    pub keys: DataFrame,
    /// Aggregated values
    pub values: DataFrame,
    /// Number of groups
    pub num_groups: usize,
}

/// Result of GPU window operation.
#[derive(Debug, Clone)]
pub struct GpuWindowResult {
    /// Result series
    pub result: Series,
    /// Number of partitions processed
    pub num_partitions: usize,
}

/// Enhanced Polars GPU executor.
pub struct GpuPolarsExecutor<R> {
    runtime: Arc<R>,
    config: PolarsConfig,
}

impl<R: RuntimeHandle + GpuPolarsOps> GpuPolarsExecutor<R> {
    /// Create a new GPU Polars executor.
    pub fn new(runtime: Arc<R>) -> Self {
        Self {
            runtime,
            config: PolarsConfig::default(),
        }
    }

    /// Apply window function on GPU.
    pub async fn window(
        &self,
        series: &Series,
        func: GpuWindowFunction,
        spec: &GpuWindowSpec,
    ) -> Result<GpuWindowResult> {
        let data = series_to_bytes(series)?;

        let result_bytes = self.runtime.gpu_window("window", data, func, spec).await?;

        let result = bytes_to_series(&result_bytes, series.name(), series.dtype())?;

        Ok(GpuWindowResult {
            result,
            num_partitions: if spec.partition_by.is_empty() { 1 } else { 0 }, // Estimated
        })
    }

    /// Sort series on GPU.
    pub async fn sort(&self, series: &Series, descending: bool) -> Result<Series> {
        let data = series_to_bytes(series)?;

        let result_bytes = self.runtime.gpu_sort("sort", data, descending).await?;

        bytes_to_series(&result_bytes, series.name(), series.dtype())
    }

    /// Rolling mean on GPU.
    pub async fn rolling_mean(&self, series: &Series, window_size: i64) -> Result<Series> {
        let spec = GpuWindowSpec::new().rolling(window_size);
        let result = self
            .window(series, GpuWindowFunction::CumSum, &spec)
            .await?;

        // Divide by window size for mean
        Ok(result.result)
    }

    /// Cumulative sum on GPU.
    pub async fn cumsum(&self, series: &Series) -> Result<Series> {
        let spec = GpuWindowSpec::default();
        let result = self
            .window(series, GpuWindowFunction::CumSum, &spec)
            .await?;
        Ok(result.result)
    }

    /// Rank on GPU.
    pub async fn rank(&self, series: &Series, descending: bool) -> Result<Series> {
        let spec = GpuWindowSpec::new().order_by(&[series.name()], &[!descending]);
        let result = self.window(series, GpuWindowFunction::Rank, &spec).await?;
        Ok(result.result)
    }
}

/// GPU-accelerated lazy frame operations.
pub struct GpuLazyOps<R> {
    runtime: Arc<R>,
    _config: PolarsConfig,
}

impl<R: RuntimeHandle + GpuPolarsOps> GpuLazyOps<R> {
    /// Create new GPU lazy ops.
    pub fn new(runtime: Arc<R>) -> Self {
        Self {
            runtime,
            _config: PolarsConfig::default(),
        }
    }

    /// Get runtime reference.
    pub fn runtime(&self) -> &R {
        &self.runtime
    }
}

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

    struct MockRuntime;

    #[async_trait::async_trait]
    impl RuntimeHandle for MockRuntime {
        async fn send_binary_op(
            &self,
            _kernel_id: &str,
            _a: Vec<u8>,
            _b: Vec<u8>,
            _op: &str,
        ) -> Result<()> {
            Ok(())
        }

        async fn receive_result(&self, _kernel_id: &str, _timeout: Duration) -> Result<Vec<u8>> {
            // Return test values
            Ok(vec![0, 0, 128, 64, 0, 0, 0, 65]) // [4.0f32, 8.0f32]
        }

        async fn send_unary_op(&self, _kernel_id: &str, _data: Vec<u8>, _op: &str) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_polars_config_default() {
        let config = PolarsConfig::default();
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert!(config.auto_convert);
    }

    #[test]
    fn test_series_to_bytes_f32() {
        let series = Series::new("test".into(), vec![1.0f32, 2.0, 3.0]);
        let bytes = series_to_bytes(&series).unwrap();
        assert_eq!(bytes.len(), 12); // 3 * 4 bytes
    }

    #[test]
    fn test_bytes_to_series_f32() {
        // 1.0f32 and 2.0f32 in little-endian
        let bytes = vec![0, 0, 128, 63, 0, 0, 0, 64];
        let series = bytes_to_series(&bytes, &"test".into(), &DataType::Float32).unwrap();
        assert_eq!(series.len(), 2);
    }
}