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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Comprehensive GPU performance benchmarks
//!
//! This example compares performance across CPU (scalar), SIMD, and GPU implementations
//! for various operations in NumRS2.
//!
//! To run:
//! ```
//! cargo run --example gpu_benchmark --features "gpu scirs"
//! ```

#![allow(clippy::result_large_err)]

use numrs2::error::Result;
use numrs2::prelude::*;
use scirs2_core::ndarray::{Array1, Array2};
use std::time::Instant;

#[cfg(feature = "gpu")]
use numrs2::gpu;

#[cfg(feature = "scirs")]
use numrs2::optimized_ops::{
    enhanced_exp, enhanced_math, get_optimization_info, simd_elementwise_ops, simd_matmul,
};

/// Benchmark result struct
#[derive(Debug)]
struct BenchmarkResult {
    cpu_time: f64,
    #[cfg(feature = "scirs")]
    simd_time: Option<f64>,
    #[cfg(feature = "gpu")]
    gpu_time: Option<f64>,
}

impl BenchmarkResult {
    fn print_summary(&self, operation: &str, size: usize) {
        println!("\n{} (size: {})", operation, size);
        println!("{:-<50}", "");
        println!("CPU time:  {:>10.3} ms", self.cpu_time * 1000.0);

        #[cfg(feature = "scirs")]
        if let Some(simd_time) = self.simd_time {
            let speedup = self.cpu_time / simd_time;
            println!(
                "SIMD time: {:>10.3} ms (speedup: {:.2}x)",
                simd_time * 1000.0,
                speedup
            );
        }

        #[cfg(feature = "gpu")]
        if let Some(gpu_time) = self.gpu_time {
            let speedup = self.cpu_time / gpu_time;
            println!(
                "GPU time:  {:>10.3} ms (speedup: {:.2}x)",
                gpu_time * 1000.0,
                speedup
            );
        }
    }
}

fn benchmark_element_wise_operations(size: usize) -> Result<()> {
    println!("\n=== Element-wise Operations ===");

    // Create test data
    let a = Array::from_vec(vec![1.0f32; size]).reshape(&[size]);
    let b = Array::from_vec(vec![2.0f32; size]).reshape(&[size]);

    // Addition benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU addition
    let start = Instant::now();
    let _cpu_result = a.add(&b);
    result.cpu_time = start.elapsed().as_secs_f64();

    // SIMD addition
    #[cfg(feature = "scirs")]
    {
        // Convert f32 to f64 for SIMD operations
        let a_f64 = a.map(|x| x as f64);
        let b_f64 = b.map(|x| x as f64);
        let a_ndarray = Array1::from_vec(a_f64.to_vec());
        let b_ndarray = Array1::from_vec(b_f64.to_vec());
        let start = Instant::now();
        let simd_result = simd_elementwise_ops(&a_ndarray.view(), &b_ndarray.view())?;
        let _ = simd_result.add;
        result.simd_time = Some(start.elapsed().as_secs_f64());
    }

    // GPU addition
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_a = gpu::GpuArray::from_array(&a)?;
        let gpu_b = gpu::GpuArray::from_array(&b)?;
        let gpu_result = gpu::add(&gpu_a, &gpu_b)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Addition", size);

    // Multiplication benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU multiplication
    let start = Instant::now();
    let _cpu_result = a.multiply(&b);
    result.cpu_time = start.elapsed().as_secs_f64();

    // SIMD multiplication
    #[cfg(feature = "scirs")]
    {
        // Convert f32 to f64 for SIMD operations
        let a_f64 = a.map(|x| x as f64);
        let b_f64 = b.map(|x| x as f64);
        let a_ndarray = Array1::from_vec(a_f64.to_vec());
        let b_ndarray = Array1::from_vec(b_f64.to_vec());
        let start = Instant::now();
        let simd_result = simd_elementwise_ops(&a_ndarray.view(), &b_ndarray.view())?;
        let _ = simd_result.mul;
        result.simd_time = Some(start.elapsed().as_secs_f64());
    }

    // GPU multiplication
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_a = gpu::GpuArray::from_array(&a)?;
        let gpu_b = gpu::GpuArray::from_array(&b)?;
        let gpu_result = gpu::multiply(&gpu_a, &gpu_b)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Multiplication", size);

    Ok(())
}

fn benchmark_transcendental_functions(size: usize) -> Result<()> {
    println!("\n=== Transcendental Functions ===");

    // Create test data
    let data = Array::from_vec(
        (0..size)
            .map(|x| (x as f32 * 0.001).min(10.0))
            .collect::<Vec<_>>(),
    )
    .reshape(&[size]);

    // Exponential benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU exponential
    let start = Instant::now();
    let _cpu_result = data.map(|x| x.exp());
    result.cpu_time = start.elapsed().as_secs_f64();

    // SIMD exponential
    #[cfg(feature = "scirs")]
    {
        let data_f64 = data.map(|x| x as f64);
        let data_ndarray = Array1::from_vec(data_f64.to_vec());
        let start = Instant::now();
        let _ = enhanced_exp::parallel_exp(&data_ndarray.view());
        result.simd_time = Some(start.elapsed().as_secs_f64());
    }

    // GPU exponential
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_data = gpu::GpuArray::from_array(&data)?;
        let gpu_result = gpu::exp(&gpu_data)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Exponential", size);

    // Sine benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU sine
    let start = Instant::now();
    let _cpu_result = data.map(|x| x.sin());
    result.cpu_time = start.elapsed().as_secs_f64();

    // SIMD sine
    #[cfg(feature = "scirs")]
    {
        let data_f64 = data.map(|x| x as f64);
        let data_ndarray = Array1::from_vec(data_f64.to_vec());
        let start = Instant::now();
        let _ = enhanced_math::parallel_sin(&data_ndarray.view());
        result.simd_time = Some(start.elapsed().as_secs_f64());
    }

    // GPU sine
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_data = gpu::GpuArray::from_array(&data)?;
        let gpu_result = gpu::sin(&gpu_data)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Sine", size);

    // Square root benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU sqrt
    let start = Instant::now();
    let _cpu_result = data.map(|x| x.sqrt());
    result.cpu_time = start.elapsed().as_secs_f64();

    // SIMD sqrt
    #[cfg(feature = "scirs")]
    {
        let data_f64 = data.map(|x| x as f64);
        let data_ndarray = Array1::from_vec(data_f64.to_vec());
        let start = Instant::now();
        let _ = enhanced_exp::simd_sqrt(&data_ndarray.view());
        result.simd_time = Some(start.elapsed().as_secs_f64());
    }

    // GPU sqrt
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_data = gpu::GpuArray::from_array(&data)?;
        let gpu_result = gpu::sqrt(&gpu_data)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Square Root", size);

    Ok(())
}

fn benchmark_matrix_operations(size: usize) -> Result<()> {
    println!("\n=== Matrix Operations ===");

    // Create test matrices
    let a = Array::from_vec(
        (0..size * size)
            .map(|x| x as f32 * 0.001)
            .collect::<Vec<_>>(),
    )
    .reshape(&[size, size]);
    let b = Array::from_vec(
        (0..size * size)
            .map(|x| x as f32 * 0.002)
            .collect::<Vec<_>>(),
    )
    .reshape(&[size, size]);

    // Matrix multiplication benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU matmul
    let start = Instant::now();
    let _cpu_result = a.dot(&b)?;
    result.cpu_time = start.elapsed().as_secs_f64();

    // SIMD matmul
    #[cfg(feature = "scirs")]
    {
        let a_vec: Vec<f32> = a.to_vec();
        let b_vec: Vec<f32> = b.to_vec();
        let a_ndarray = Array2::from_shape_vec((size, size), a_vec).unwrap();
        let b_ndarray = Array2::from_shape_vec((size, size), b_vec).unwrap();
        let start = Instant::now();
        let _ = simd_matmul(&a_ndarray.view(), &b_ndarray.view())?;
        result.simd_time = Some(start.elapsed().as_secs_f64());
    }

    // GPU matmul
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_a = gpu::GpuArray::from_array(&a)?;
        let gpu_b = gpu::GpuArray::from_array(&b)?;
        let gpu_result = gpu::matmul(&gpu_a, &gpu_b)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Matrix Multiplication", size);

    // Matrix transpose benchmark
    let mut result = BenchmarkResult {
        cpu_time: 0.0,
        #[cfg(feature = "scirs")]
        simd_time: None,
        #[cfg(feature = "gpu")]
        gpu_time: None,
    };

    // CPU transpose
    let start = Instant::now();
    let _cpu_result = a.transpose();
    result.cpu_time = start.elapsed().as_secs_f64();

    // GPU transpose
    #[cfg(feature = "gpu")]
    {
        let start = Instant::now();
        let gpu_a = gpu::GpuArray::from_array(&a)?;
        let gpu_result = gpu::transpose(&gpu_a)?;
        let _ = gpu_result.to_array()?;
        result.gpu_time = Some(start.elapsed().as_secs_f64());
    }

    result.print_summary("Matrix Transpose", size);

    Ok(())
}

#[allow(dead_code)]
fn benchmark_memory_transfer(size: usize) -> Result<()> {
    #[cfg(feature = "gpu")]
    {
        println!("\n=== Memory Transfer Overhead ===");

        // Create test data
        let data = Array::from_vec(vec![1.0f32; size]).reshape(&[size]);

        // Measure CPU to GPU transfer
        let start = Instant::now();
        let gpu_data = gpu::GpuArray::from_array(&data)?;
        let to_gpu_time = start.elapsed().as_secs_f64();

        // Measure GPU to CPU transfer
        let start = Instant::now();
        let _cpu_data = gpu_data.to_array()?;
        let to_cpu_time = start.elapsed().as_secs_f64();

        println!(
            "Data size: {} elements ({:.2} MB)",
            size,
            (size * std::mem::size_of::<f32>()) as f64 / (1024.0 * 1024.0)
        );
        println!(
            "CPU → GPU transfer: {:.3} ms ({:.2} GB/s)",
            to_gpu_time * 1000.0,
            (size * std::mem::size_of::<f32>()) as f64 / to_gpu_time / 1e9
        );
        println!(
            "GPU → CPU transfer: {:.3} ms ({:.2} GB/s)",
            to_cpu_time * 1000.0,
            (size * std::mem::size_of::<f32>()) as f64 / to_cpu_time / 1e9
        );
    }

    Ok(())
}

fn print_performance_recommendations() {
    println!("\n=== Performance Recommendations ===");
    println!("Based on the benchmark results:");

    println!("\n1. Element-wise operations:");
    println!("   - Small arrays (< 1000): Use CPU");
    println!("   - Medium arrays (1000-100000): Use SIMD");
    println!("   - Large arrays (> 100000): Use GPU");

    println!("\n2. Matrix operations:");
    println!("   - Small matrices (< 100x100): Use CPU");
    println!("   - Medium matrices (100x100 - 500x500): Use SIMD");
    println!("   - Large matrices (> 500x500): Use GPU");

    println!("\n3. Transcendental functions:");
    println!("   - Always benefit from SIMD when available");
    println!("   - GPU provides best performance for large arrays");

    println!("\n4. Consider memory transfer overhead:");
    println!("   - GPU is most beneficial for operations that can be batched");
    println!("   - Avoid frequent CPU ↔ GPU transfers");
}

fn main() -> Result<()> {
    println!("NumRS2 GPU Performance Benchmarks");
    println!("=================================");

    // Print system information
    #[cfg(feature = "scirs")]
    println!("\n{}", get_optimization_info());

    #[cfg(feature = "gpu")]
    {
        if let Some(gpu_info) = gpu::get_gpu_info() {
            println!("GPU: {}", gpu_info);
        }
    }

    // Run benchmarks with different sizes
    let sizes = vec![
        100,       // Very small
        1_000,     // Small
        10_000,    // Medium
        100_000,   // Large
        1_000_000, // Very large
    ];

    for &size in &sizes {
        println!("\n\n╔══════════════════════════════════════════╗");
        println!("║  Benchmarking with {} elements", size);
        println!("╚══════════════════════════════════════════╝");

        benchmark_element_wise_operations(size)?;
        benchmark_transcendental_functions(size)?;

        // Matrix operations only for reasonable sizes
        if size <= 1000 {
            let matrix_size = (size as f64).sqrt() as usize;
            benchmark_matrix_operations(matrix_size)?;
        }

        #[cfg(feature = "gpu")]
        benchmark_memory_transfer(size)?;
    }

    print_performance_recommendations();

    println!("\n\nBenchmark completed successfully!");

    #[cfg(not(all(feature = "gpu", feature = "scirs")))]
    {
        println!("\nNote: To see all optimizations, run with:");
        println!("cargo run --example gpu_benchmark --features \"gpu scirs\"");
    }

    Ok(())
}