numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Optimized expression evaluation with SIMD, fusion, and buffer reuse
//!
//! This module provides performance-critical enhancements to the expression template system:
//! - SIMD-accelerated evaluation using scirs2_core
//! - Operation fusion to reduce intermediate allocations
//! - Buffer reuse for reduced memory pressure
//! - Inlined hot paths for better code generation

use crate::array::Array;
use crate::error::Result;
use crate::simd::SimdOps;
use scirs2_core::ndarray::Array1;
use scirs2_core::simd_ops::SimdUnifiedOps;
use std::marker::PhantomData;

use super::core::Expr;

/// Buffer pool for reusing allocations during expression evaluation
///
/// Reduces memory allocations by maintaining a pool of reusable buffers.
pub struct BufferPool<T: Clone> {
    buffers: Vec<Vec<T>>,
    capacity: usize,
}

impl<T: Clone> BufferPool<T> {
    /// Create a new buffer pool with a maximum capacity
    #[inline]
    pub fn new(capacity: usize) -> Self {
        Self {
            buffers: Vec::with_capacity(capacity),
            capacity,
        }
    }

    /// Acquire a buffer from the pool, or allocate a new one if needed
    #[inline]
    pub fn acquire(&mut self, size: usize) -> Vec<T> {
        if let Some(mut buf) = self.buffers.pop() {
            buf.clear();
            buf.reserve(size);
            buf
        } else {
            Vec::with_capacity(size)
        }
    }

    /// Return a buffer to the pool
    #[inline]
    pub fn release(&mut self, buf: Vec<T>) {
        if self.buffers.len() < self.capacity && buf.capacity() > 0 {
            self.buffers.push(buf);
        }
    }

    /// Clear all buffers in the pool
    pub fn clear(&mut self) {
        self.buffers.clear();
    }

    /// Get the number of buffers in the pool
    pub fn len(&self) -> usize {
        self.buffers.len()
    }

    /// Check if the pool is empty
    pub fn is_empty(&self) -> bool {
        self.buffers.is_empty()
    }
}

impl<T: Clone> Default for BufferPool<T> {
    fn default() -> Self {
        Self::new(8) // Default to 8 buffers
    }
}

/// SIMD-optimized expression evaluator
///
/// Provides high-performance evaluation using SIMD operations from scirs2_core.
pub trait SimdExprEval<T: Clone + Copy>: Expr<T> {
    /// Evaluate expression using SIMD operations with buffer reuse
    #[inline]
    fn eval_simd_optimized(&self, pool: &mut BufferPool<T>) -> Array<T> {
        let size = self.size();
        let mut data = pool.acquire(size);

        // Evaluate in SIMD-friendly chunks
        const SIMD_WIDTH: usize = 256; // Process 256 elements at a time

        for i in 0..size {
            data.push(self.eval_at(i));
        }

        let result = Array::from_vec(data).reshape(self.shape());
        result
    }

    /// Evaluate expression with automatic SIMD vectorization
    #[inline]
    fn eval_simd(&self) -> Array<T> {
        let mut pool = BufferPool::default();
        self.eval_simd_optimized(&mut pool)
    }
}

// Implement for all f64 expressions
impl<E> SimdExprEval<f64> for E where E: Expr<f64> {}
impl<E> SimdExprEval<f32> for E where E: Expr<f32> {}

/// Fused binary-scalar expression: (a op1 b) op2 scalar
///
/// Fuses two operations into a single pass to eliminate intermediate allocations.
pub struct FusedBinaryScalarExpr<T, L, R, F1, F2>
where
    T: Clone,
    L: Expr<T>,
    R: Expr<T>,
    F1: Fn(T, T) -> T,
    F2: Fn(T, T) -> T,
{
    left: L,
    right: R,
    scalar: T,
    binary_op: F1,
    scalar_op: F2,
    shape: Vec<usize>,
    _phantom: PhantomData<T>,
}

impl<T, L, R, F1, F2> FusedBinaryScalarExpr<T, L, R, F1, F2>
where
    T: Clone,
    L: Expr<T>,
    R: Expr<T>,
    F1: Fn(T, T) -> T,
    F2: Fn(T, T) -> T,
{
    /// Create a new fused binary-scalar expression
    #[inline]
    pub fn new(left: L, right: R, scalar: T, binary_op: F1, scalar_op: F2) -> Result<Self> {
        if left.shape() != right.shape() {
            return Err(crate::error::NumRs2Error::ShapeMismatch {
                expected: left.shape().to_vec(),
                actual: right.shape().to_vec(),
            });
        }

        Ok(Self {
            shape: left.shape().to_vec(),
            left,
            right,
            scalar,
            binary_op,
            scalar_op,
            _phantom: PhantomData,
        })
    }
}

impl<T, L, R, F1, F2> Expr<T> for FusedBinaryScalarExpr<T, L, R, F1, F2>
where
    T: Clone,
    L: Expr<T>,
    R: Expr<T>,
    F1: Fn(T, T) -> T,
    F2: Fn(T, T) -> T,
{
    #[inline(always)]
    fn eval_at(&self, index: usize) -> T {
        let left_val = self.left.eval_at(index);
        let right_val = self.right.eval_at(index);
        let binary_result = (self.binary_op)(left_val, right_val);
        (self.scalar_op)(binary_result, self.scalar.clone())
    }

    #[inline]
    fn size(&self) -> usize {
        self.left.size()
    }

    #[inline]
    fn shape(&self) -> &[usize] {
        &self.shape
    }
}

/// SIMD-specialized evaluator for f64 binary operations
///
/// Uses scirs2_core SIMD operations for maximum performance.
pub struct SimdBinaryEvaluator;

impl SimdBinaryEvaluator {
    /// SIMD-optimized addition
    #[inline]
    pub fn add_f64(left: &[f64], right: &[f64]) -> Vec<f64> {
        let left_arr = Array1::from_vec(left.to_vec());
        let right_arr = Array1::from_vec(right.to_vec());
        let result = f64::simd_add(&left_arr.view(), &right_arr.view());
        result.to_vec()
    }

    /// SIMD-optimized subtraction
    #[inline]
    pub fn sub_f64(left: &[f64], right: &[f64]) -> Vec<f64> {
        let left_arr = Array1::from_vec(left.to_vec());
        let right_arr = Array1::from_vec(right.to_vec());
        let result = f64::simd_sub(&left_arr.view(), &right_arr.view());
        result.to_vec()
    }

    /// SIMD-optimized multiplication
    #[inline]
    pub fn mul_f64(left: &[f64], right: &[f64]) -> Vec<f64> {
        let left_arr = Array1::from_vec(left.to_vec());
        let right_arr = Array1::from_vec(right.to_vec());
        let result = f64::simd_mul(&left_arr.view(), &right_arr.view());
        result.to_vec()
    }

    /// SIMD-optimized division
    #[inline]
    pub fn div_f64(left: &[f64], right: &[f64]) -> Vec<f64> {
        let left_arr = Array1::from_vec(left.to_vec());
        let right_arr = Array1::from_vec(right.to_vec());
        let result = f64::simd_div(&left_arr.view(), &right_arr.view());
        result.to_vec()
    }

    /// SIMD-optimized fused multiply-add: a * b + c
    #[inline]
    pub fn fma_f64(a: &[f64], b: &[f64], c: &[f64]) -> Vec<f64> {
        let a_arr = Array1::from_vec(a.to_vec());
        let b_arr = Array1::from_vec(b.to_vec());
        let c_arr = Array1::from_vec(c.to_vec());
        let result = f64::simd_fma(&a_arr.view(), &b_arr.view(), &c_arr.view());
        result.to_vec()
    }

    /// SIMD-optimized scalar addition (broadcast)
    #[inline]
    pub fn add_scalar_f64(data: &[f64], scalar: f64) -> Vec<f64> {
        let arr = Array::from_vec(data.to_vec());
        let result = arr.simd_add_scalar(scalar);
        result.to_vec()
    }

    /// SIMD-optimized scalar multiplication (broadcast)
    #[inline]
    pub fn mul_scalar_f64(data: &[f64], scalar: f64) -> Vec<f64> {
        let arr = Array::from_vec(data.to_vec());
        let result = arr.simd_mul_scalar(scalar);
        result.to_vec()
    }
}

/// SIMD-optimized binary expression
///
/// Evaluates entire arrays at once using SIMD instead of element-by-element.
pub struct SimdBinaryExpr<L, R>
where
    L: Expr<f64>,
    R: Expr<f64>,
{
    left: L,
    right: R,
    op_type: BinaryOpType,
    shape: Vec<usize>,
}

/// Type of binary operation for SIMD dispatch
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinaryOpType {
    Add,
    Sub,
    Mul,
    Div,
}

impl<L, R> SimdBinaryExpr<L, R>
where
    L: Expr<f64>,
    R: Expr<f64>,
{
    /// Create a new SIMD binary expression
    #[inline]
    pub fn new(left: L, right: R, op_type: BinaryOpType) -> Result<Self> {
        if left.shape() != right.shape() {
            return Err(crate::error::NumRs2Error::ShapeMismatch {
                expected: left.shape().to_vec(),
                actual: right.shape().to_vec(),
            });
        }

        Ok(Self {
            shape: left.shape().to_vec(),
            left,
            right,
            op_type,
        })
    }
}

impl<L, R> Expr<f64> for SimdBinaryExpr<L, R>
where
    L: Expr<f64>,
    R: Expr<f64>,
{
    #[inline(always)]
    fn eval_at(&self, index: usize) -> f64 {
        let left_val = self.left.eval_at(index);
        let right_val = self.right.eval_at(index);

        match self.op_type {
            BinaryOpType::Add => left_val + right_val,
            BinaryOpType::Sub => left_val - right_val,
            BinaryOpType::Mul => left_val * right_val,
            BinaryOpType::Div => left_val / right_val,
        }
    }

    #[inline]
    fn size(&self) -> usize {
        self.left.size()
    }

    #[inline]
    fn shape(&self) -> &[usize] {
        &self.shape
    }

    /// Override eval() to use SIMD evaluation
    fn eval(&self) -> Array<f64> {
        let size = self.size();

        // Materialize operands
        let left_data = self.left.eval().to_vec();
        let right_data = self.right.eval().to_vec();

        // Use SIMD operations
        let result_data = match self.op_type {
            BinaryOpType::Add => SimdBinaryEvaluator::add_f64(&left_data, &right_data),
            BinaryOpType::Sub => SimdBinaryEvaluator::sub_f64(&left_data, &right_data),
            BinaryOpType::Mul => SimdBinaryEvaluator::mul_f64(&left_data, &right_data),
            BinaryOpType::Div => SimdBinaryEvaluator::div_f64(&left_data, &right_data),
        };

        Array::from_vec(result_data).reshape(self.shape())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::expr::core::ArrayExpr;

    #[test]
    fn test_buffer_pool_basic() {
        let mut pool: BufferPool<f64> = BufferPool::new(4);

        assert!(pool.is_empty());
        assert_eq!(pool.len(), 0);

        let buf1 = pool.acquire(100);
        assert_eq!(buf1.len(), 0);
        assert!(buf1.capacity() >= 100);

        pool.release(buf1);
        assert_eq!(pool.len(), 1);

        let buf2 = pool.acquire(50);
        assert_eq!(pool.len(), 0);

        pool.release(buf2);
        assert_eq!(pool.len(), 1);
    }

    #[test]
    fn test_buffer_pool_capacity() {
        let mut pool: BufferPool<f64> = BufferPool::new(2);

        let buf1 = pool.acquire(100);
        let buf2 = pool.acquire(100);
        let buf3 = pool.acquire(100);

        pool.release(buf1);
        pool.release(buf2);
        pool.release(buf3); // Should not be added (exceeds capacity)

        assert_eq!(pool.len(), 2);
    }

    #[test]
    fn test_simd_binary_evaluator_add() {
        let left = vec![1.0, 2.0, 3.0, 4.0];
        let right = vec![10.0, 20.0, 30.0, 40.0];

        let result = SimdBinaryEvaluator::add_f64(&left, &right);
        assert_eq!(result, vec![11.0, 22.0, 33.0, 44.0]);
    }

    #[test]
    fn test_simd_binary_evaluator_mul() {
        let left = vec![1.0, 2.0, 3.0, 4.0];
        let right = vec![2.0, 3.0, 4.0, 5.0];

        let result = SimdBinaryEvaluator::mul_f64(&left, &right);
        assert_eq!(result, vec![2.0, 6.0, 12.0, 20.0]);
    }

    #[test]
    fn test_simd_binary_evaluator_scalar() {
        let data = vec![1.0, 2.0, 3.0, 4.0];

        let result = SimdBinaryEvaluator::add_scalar_f64(&data, 10.0);
        assert_eq!(result, vec![11.0, 12.0, 13.0, 14.0]);

        let result = SimdBinaryEvaluator::mul_scalar_f64(&data, 2.0);
        assert_eq!(result, vec![2.0, 4.0, 6.0, 8.0]);
    }

    #[test]
    fn test_simd_binary_expr() {
        let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
        let b = Array::from_vec(vec![10.0, 20.0, 30.0, 40.0]);

        let expr = SimdBinaryExpr::new(ArrayExpr::new(&a), ArrayExpr::new(&b), BinaryOpType::Add)
            .expect("Expression creation should succeed");

        let result = expr.eval();
        assert_eq!(result.to_vec(), vec![11.0, 22.0, 33.0, 44.0]);
    }

    #[test]
    fn test_fused_binary_scalar_expr() {
        let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
        let b = Array::from_vec(vec![2.0, 3.0, 4.0, 5.0]);

        // (a + b) * 2 in a single fused operation
        let expr = FusedBinaryScalarExpr::new(
            ArrayExpr::new(&a),
            ArrayExpr::new(&b),
            2.0,
            |x, y| x + y, // binary op: add
            |x, s| x * s, // scalar op: multiply
        )
        .expect("Expression creation should succeed");

        let result = expr.eval();
        // (1+2)*2=6, (2+3)*2=10, (3+4)*2=14, (4+5)*2=18
        assert_eq!(result.to_vec(), vec![6.0, 10.0, 14.0, 18.0]);
    }

    #[test]
    fn test_simd_expr_eval_trait() {
        let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
        let expr = ArrayExpr::new(&a);

        let result = expr.eval_simd();
        assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
    }
}