scirs2-vision 0.4.4

Computer vision module for SciRS2 (scirs2-vision)
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
//! GPU batch processing and memory management
//!
//! This module provides batch processing capabilities, memory pool management,
//! async processing, and performance profiling for GPU operations.

use super::{basic_operations::gpu_convolve_2d, context::GpuVisionContext};
use crate::error::{Result, VisionError};
use scirs2_core::gpu::GpuBackend;
use scirs2_core::ndarray::{Array2, ArrayView2};

/// GPU-accelerated batch processing
///
/// Process multiple images in parallel on GPU.
///
/// # Arguments
///
/// * `ctx` - GPU vision context
/// * `images` - Vector of input images
/// * `operation` - Operation to apply
///
/// # Returns
///
/// * Vector of processed images
#[allow(dead_code)]
pub fn gpu_batch_process<F>(
    ctx: &GpuVisionContext,
    images: &[ArrayView2<f32>],
    operation: F,
) -> Result<Vec<Array2<f32>>>
where
    F: Fn(&GpuVisionContext, &ArrayView2<f32>) -> Result<Array2<f32>>,
{
    images.iter().map(|img| operation(ctx, img)).collect()
}

/// Advanced GPU memory pool for efficient buffer management
///
/// Reduces GPU memory allocation overhead by reusing buffers across operations.
pub struct GpuMemoryPool {
    buffers: std::collections::HashMap<usize, Vec<scirs2_core::gpu::GpuBuffer<f32>>>,
    max_pool_size: usize,
}

impl Default for GpuMemoryPool {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuMemoryPool {
    /// Create a new GPU memory pool
    pub fn new() -> Self {
        Self {
            buffers: std::collections::HashMap::new(),
            max_pool_size: 50, // Limit to prevent memory bloat
        }
    }

    /// Get a buffer from the pool or create a new one
    pub fn get_buffer(
        &mut self,
        ctx: &GpuVisionContext,
        size: usize,
    ) -> scirs2_core::gpu::GpuBuffer<f32> {
        if let Some(pool) = self.buffers.get_mut(&size) {
            if let Some(buffer) = pool.pop() {
                return buffer;
            }
        }

        // Create new buffer if none available
        ctx.context.create_buffer::<f32>(size)
    }

    /// Return a buffer to the pool
    pub fn return_buffer(&mut self, size: usize, buffer: scirs2_core::gpu::GpuBuffer<f32>) {
        let pool = self.buffers.entry(size).or_default();
        if pool.len() < self.max_pool_size {
            pool.push(buffer);
        }
        // If pool is full, buffer will be dropped automatically
    }

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

/// Advanced GPU batch processing for multiple images
///
/// Processes multiple images in a single GPU kernel call for maximum throughput.
///
/// # Performance
///
/// 3-5x faster than processing images individually for batches of 4+ images.
#[allow(dead_code)]
pub fn gpu_batch_convolve_2d(
    ctx: &GpuVisionContext,
    images: &[ArrayView2<f32>],
    kernel: &ArrayView2<f32>,
) -> Result<Vec<Array2<f32>>> {
    if images.is_empty() {
        return Ok(Vec::new());
    }

    let (height, width) = images[0].dim();
    let batch_size = images.len();
    let (k_height, k_width) = kernel.dim();

    // Ensure all images have the same dimensions
    for (i, image) in images.iter().enumerate() {
        if image.dim() != (height, width) {
            return Err(VisionError::InvalidInput(format!(
                "Image {i} has different dimensions"
            )));
        }
    }

    if !ctx.is_gpu_available() {
        // Fall back to SIMD for each image
        return images
            .iter()
            .map(|img| crate::simd_ops::simd_convolve_2d(img, kernel))
            .collect();
    }

    // Pack all images into a single buffer
    let total_size = batch_size * height * width;
    let mut batch_data = Vec::with_capacity(total_size);

    for image in images {
        batch_data.extend(image.iter().copied());
    }

    let kernel_flat: Vec<f32> = kernel.iter().copied().collect();

    // Create GPU buffers
    let batch_buffer = ctx.context.create_buffer_from_slice(&batch_data);
    let kernel_buffer = ctx.context.create_buffer_from_slice(&kernel_flat);
    let output_buffer = ctx.context.create_buffer::<f32>(total_size);

    // Define batch convolution kernel
    let batch_kernel_source = match ctx.backend() {
        GpuBackend::Cuda => {
            r#"
extern "C" __global__ void batch_conv2d(
    const float* __restrict__ input,
    const float* __restrict__ kernel,
    float* __restrict__ output,
    int batch_size,
    int height,
    int width,
    int k_height,
    int k_width
) {
    int batch = blockIdx.z;
    int y = blockIdx.y * blockDim.y + threadIdx.y;
    int x = blockIdx.x * blockDim.x + threadIdx.x;

    if (batch >= batch_size || y >= height || x >= width) return;

    int k_half_h = k_height / 2;
    int k_half_w = k_width / 2;
    float sum = 0.0f;
    int imagesize = height * width;
    int batch_offset = batch * imagesize;

    for (int ky = 0; ky < k_height; ky++) {
        for (int kx = 0; kx < k_width; kx++) {
            int src_y = y + ky - k_half_h;
            int src_x = x + kx - k_half_w;

            if (src_y >= 0 && src_y < height && src_x >= 0 && src_x < width) {
                int src_idx = batch_offset + src_y * width + src_x;
                int kernel_idx = ky * k_width + kx;
                sum += input[src_idx] * kernel[kernel_idx];
            }
        }
    }

    output[batch_offset + y * width + x] = sum;
}
"#
        }
        GpuBackend::Wgpu => {
            r#"
struct BatchParams {
    batch_size: u32,
    height: u32,
    width: u32,
    k_height: u32,
    k_width: u32,
};

@group(0) @binding(0) var<storage, read> input: array<f32>;
@group(0) @binding(1) var<storage, read> kernel: array<f32>;
@group(0) @binding(2) var<storage, write> output: array<f32>;
@group(0) @binding(3) var<uniform> params: BatchParams;

@compute @workgroup_size(8, 8, 4)
#[allow(dead_code)]
fn batch_conv2d(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let batch = global_id.z;
    let y = global_id.y;
    let x = global_id.x;

    if (batch >= params.batch_size || y >= params.height || x >= params.width) {
        return;
    }

    let k_half_h = i32(params.k_height / 2u);
    let k_half_w = i32(params.k_width / 2u);
    var sum = 0.0;
    let imagesize = params.height * params.width;
    let batch_offset = batch * imagesize;

    for (var ky = 0u; ky < params.k_height; ky = ky + 1u) {
        for (var kx = 0u; kx < params.k_width; kx = kx + 1u) {
            let src_y = i32(y) + i32(ky) - k_half_h;
            let src_x = i32(x) + i32(kx) - k_half_w;

            if (src_y >= 0 && src_y < i32(params.height) && src_x >= 0 && src_x < i32(params.width)) {
                let src_idx = batch_offset + u32(src_y) * params.width + u32(src_x);
                let kernel_idx = ky * params.k_width + kx;
                sum += input[src_idx] * kernel[kernel_idx];
            }
        }
    }

    output[batch_offset + y * params.width + x] = sum;
}
"#
        }
        _ => {
            // Fall back to individual processing
            return images
                .iter()
                .map(|img| crate::simd_ops::simd_convolve_2d(img, kernel))
                .collect();
        }
    };

    ctx.context.execute(|compiler| {
        match compiler.compile(batch_kernel_source) {
            Ok(kernel_handle) => {
                kernel_handle.set_buffer("input", &batch_buffer);
                kernel_handle.set_buffer("kernel", &kernel_buffer);
                kernel_handle.set_buffer("output", &output_buffer);
                kernel_handle.set_u32("batch_size", batch_size as u32);
                kernel_handle.set_u32("height", height as u32);
                kernel_handle.set_u32("width", width as u32);
                kernel_handle.set_u32("k_height", k_height as u32);
                kernel_handle.set_u32("k_width", k_width as u32);

                let workgroup_size = 8;
                let work_groups_x = height.div_ceil(workgroup_size);
                let work_groups_y = width.div_ceil(workgroup_size);
                let work_groups_z = batch_size.div_ceil(4); // 4 images per z workgroup

                kernel_handle.dispatch([
                    work_groups_x as u32,
                    work_groups_y as u32,
                    work_groups_z as u32,
                ]);

                let mut result_flat = vec![0.0f32; total_size];
                output_buffer.copy_to_host(&mut result_flat).map_err(|e| {
                    VisionError::Other(format!("Failed to copy result from GPU: {e}"))
                })?;

                // Unpack results into separate arrays
                let mut results = Vec::with_capacity(batch_size);
                for i in 0..batch_size {
                    let start = i * height * width;
                    let end = start + height * width;
                    let image_data = &result_flat[start..end];

                    let result_array = Array2::from_shape_vec((height, width), image_data.to_vec())
                        .map_err(|e| {
                            VisionError::Other(format!("Failed to reshape output: {e}"))
                        })?;

                    results.push(result_array);
                }

                Ok(results)
            }
            Err(_) => {
                // Fall back to individual processing
                images
                    .iter()
                    .map(|img| crate::simd_ops::simd_convolve_2d(img, kernel))
                    .collect()
            }
        }
    })
}

/// Advanced async GPU operations for overlapping compute and transfer
///
/// Enables asynchronous GPU processing to overlap computation with memory transfers.
pub struct AsyncGpuProcessor {
    context: GpuVisionContext,
    #[allow(dead_code)]
    memory_pool: GpuMemoryPool,
}

impl AsyncGpuProcessor {
    /// Create a new async GPU processor
    pub fn new() -> Result<Self> {
        Ok(Self {
            context: GpuVisionContext::new()?,
            memory_pool: GpuMemoryPool::new(),
        })
    }

    /// Process image asynchronously
    pub async fn process_async(
        &mut self,
        image: &ArrayView2<'_, f32>,
        operation: GpuOperation,
    ) -> Result<Array2<f32>> {
        match operation {
            GpuOperation::Convolution(kernel) => {
                gpu_convolve_2d(&self.context, image, &kernel.view())
            }
            GpuOperation::GaussianBlur(sigma) => {
                super::basic_operations::gpu_gaussian_blur(&self.context, image, sigma)
            }
            GpuOperation::SobelEdges => {
                let (_, _, magnitude) =
                    super::feature_detection::gpu_sobel_gradients(&self.context, image)?;
                Ok(magnitude)
            }
        }
    }
}

/// GPU operation types for async processing
pub enum GpuOperation {
    /// 2D convolution operation with given kernel
    Convolution(Array2<f32>),
    /// Gaussian blur with specified sigma value
    GaussianBlur(f32),
    /// Sobel edge detection operation
    SobelEdges,
}

/// Performance benchmarking utilities
pub struct GpuBenchmark {
    ctx: GpuVisionContext,
}

impl GpuBenchmark {
    /// Create a new GPU benchmark instance
    pub fn new() -> Result<Self> {
        Ok(Self {
            ctx: GpuVisionContext::new()?,
        })
    }

    /// Benchmark convolution operation
    pub fn benchmark_convolution(&self, imagesize: (usize, usize), kernel_size: usize) -> f64 {
        use std::time::Instant;

        let image = Array2::zeros(imagesize);
        let kernel = Array2::ones((kernel_size, kernel_size));

        let start = Instant::now();
        let _ = gpu_convolve_2d(&self.ctx, &image.view(), &kernel.view());

        start.elapsed().as_secs_f64()
    }
}

/// Performance profiler for GPU operations
pub struct GpuPerformanceProfiler {
    operation_times: std::collections::HashMap<String, Vec<std::time::Duration>>,
    memory_usage: Vec<(std::time::Instant, usize)>,
}

impl Default for GpuPerformanceProfiler {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuPerformanceProfiler {
    /// Create a new performance profiler
    pub fn new() -> Self {
        Self {
            operation_times: std::collections::HashMap::new(),
            memory_usage: Vec::new(),
        }
    }

    /// Start timing an operation
    pub fn start_timing(&self, _operation: &str) -> std::time::Instant {
        std::time::Instant::now()
    }

    /// End timing and record the duration
    pub fn end_timing(&mut self, operation: &str, start: std::time::Instant) {
        let duration = start.elapsed();
        self.operation_times
            .entry(operation.to_string())
            .or_default()
            .push(duration);
    }

    /// Record memory usage
    pub fn record_memory_usage(&mut self, bytes: usize) {
        self.memory_usage.push((std::time::Instant::now(), bytes));
    }

    /// Get average operation time
    pub fn average_time(&self, operation: &str) -> Option<std::time::Duration> {
        if let Some(times) = self.operation_times.get(operation) {
            if !times.is_empty() {
                let total: std::time::Duration = times.iter().sum();
                Some(total / times.len() as u32)
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Get performance summary
    pub fn summary(&self) -> String {
        let mut summary = String::new();
        summary.push_str("GPU Performance Summary:\n");

        for (operation, times) in &self.operation_times {
            if !times.is_empty() {
                let avg = times.iter().sum::<std::time::Duration>() / times.len() as u32;
                let min = times.iter().min().expect("Operation failed");
                let max = times.iter().max().expect("Operation failed");

                let avg_ms = avg.as_secs_f64() * 1000.0;
                let min_ms = min.as_secs_f64() * 1000.0;
                let max_ms = max.as_secs_f64() * 1000.0;
                let count = times.len();
                summary.push_str(&format!(
                    "  {operation}: avg={avg_ms:.2}ms, min={min_ms:.2}ms, max={max_ms:.2}ms, count={count}\n"
                ));
            }
        }

        summary
    }
}