oxigdal-gpu-advanced 0.1.4

Advanced GPU computing with multi-GPU support, memory pooling, and shader optimization for OxiGDAL
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
//! Advanced GPU compute kernels and WGSL shaders.
//!
//! This module provides access to optimized WGSL shaders for various
//! geospatial and image processing operations.

/// WGSL shader for matrix operations (GEMM)
pub const MATRIX_OPS_SHADER: &str = include_str!("advanced/matrix_ops.wgsl");

/// WGSL shader for Fast Fourier Transform
pub const FFT_SHADER: &str = include_str!("advanced/fft.wgsl");

/// WGSL shader for histogram equalization
pub const HISTOGRAM_EQ_SHADER: &str = include_str!("advanced/histogram_eq.wgsl");

/// WGSL shader for morphological operations
pub const MORPHOLOGY_SHADER: &str = include_str!("advanced/morphology.wgsl");

/// WGSL shader for edge detection
pub const EDGE_DETECTION_SHADER: &str = include_str!("advanced/edge_detection.wgsl");

/// WGSL shader for texture analysis
pub const TEXTURE_ANALYSIS_SHADER: &str = include_str!("advanced/texture_analysis.wgsl");

/// Kernel registry for managing and accessing shaders
pub struct KernelRegistry {
    shaders: std::collections::HashMap<String, String>,
}

impl KernelRegistry {
    /// Create a new kernel registry with built-in shaders
    pub fn new() -> Self {
        let mut shaders = std::collections::HashMap::new();

        shaders.insert("matrix_ops".to_string(), MATRIX_OPS_SHADER.to_string());
        shaders.insert("fft".to_string(), FFT_SHADER.to_string());
        shaders.insert("histogram_eq".to_string(), HISTOGRAM_EQ_SHADER.to_string());
        shaders.insert("morphology".to_string(), MORPHOLOGY_SHADER.to_string());
        shaders.insert(
            "edge_detection".to_string(),
            EDGE_DETECTION_SHADER.to_string(),
        );
        shaders.insert(
            "texture_analysis".to_string(),
            TEXTURE_ANALYSIS_SHADER.to_string(),
        );

        Self { shaders }
    }

    /// Get a shader by name
    pub fn get_shader(&self, name: &str) -> Option<&str> {
        self.shaders.get(name).map(|s| s.as_str())
    }

    /// Register a custom shader
    pub fn register_shader(&mut self, name: String, source: String) {
        self.shaders.insert(name, source);
    }

    /// List all available shader names
    pub fn list_shaders(&self) -> Vec<&str> {
        self.shaders.keys().map(|k| k.as_str()).collect()
    }

    /// Check if a shader exists
    pub fn has_shader(&self, name: &str) -> bool {
        self.shaders.contains_key(name)
    }

    /// Remove a shader
    pub fn remove_shader(&mut self, name: &str) -> bool {
        self.shaders.remove(name).is_some()
    }

    /// Get the number of registered shaders
    pub fn shader_count(&self) -> usize {
        self.shaders.len()
    }
}

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

/// Kernel execution parameters
#[derive(Debug, Clone)]
pub struct KernelParams {
    /// Workgroup size (x, y, z)
    pub workgroup_size: (u32, u32, u32),
    /// Number of workgroups to dispatch (x, y, z)
    pub dispatch_size: (u32, u32, u32),
    /// Entry point function name
    pub entry_point: String,
}

impl Default for KernelParams {
    fn default() -> Self {
        Self {
            workgroup_size: (8, 8, 1),
            dispatch_size: (1, 1, 1),
            entry_point: "main".to_string(),
        }
    }
}

impl KernelParams {
    /// Create new kernel parameters
    pub fn new(workgroup_size: (u32, u32, u32), dispatch_size: (u32, u32, u32)) -> Self {
        Self {
            workgroup_size,
            dispatch_size,
            entry_point: "main".to_string(),
        }
    }

    /// Set workgroup size
    pub fn with_workgroup_size(mut self, x: u32, y: u32, z: u32) -> Self {
        self.workgroup_size = (x, y, z);
        self
    }

    /// Set dispatch size
    pub fn with_dispatch_size(mut self, x: u32, y: u32, z: u32) -> Self {
        self.dispatch_size = (x, y, z);
        self
    }

    /// Set entry point
    pub fn with_entry_point(mut self, entry_point: impl Into<String>) -> Self {
        self.entry_point = entry_point.into();
        self
    }

    /// Calculate total number of threads
    pub fn total_threads(&self) -> u64 {
        let (wg_x, wg_y, wg_z) = self.workgroup_size;
        let (d_x, d_y, d_z) = self.dispatch_size;

        (wg_x as u64 * d_x as u64) * (wg_y as u64 * d_y as u64) * (wg_z as u64 * d_z as u64)
    }

    /// Calculate optimal dispatch size for a given data size
    pub fn calculate_dispatch_size(
        data_width: u32,
        data_height: u32,
        workgroup_size: (u32, u32, u32),
    ) -> (u32, u32, u32) {
        let (wg_x, wg_y, _wg_z) = workgroup_size;

        let dispatch_x = data_width.div_ceil(wg_x);
        let dispatch_y = data_height.div_ceil(wg_y);
        let dispatch_z = 1;

        (dispatch_x, dispatch_y, dispatch_z)
    }
}

/// Matrix multiplication kernel helper
pub struct MatrixMultiplyKernel;

impl MatrixMultiplyKernel {
    /// Get shader source
    pub fn shader() -> &'static str {
        MATRIX_OPS_SHADER
    }

    /// Create parameters for matrix multiplication
    pub fn params(m: u32, n: u32, _k: u32, tiled: bool) -> KernelParams {
        if tiled {
            let workgroup_size = (16, 16, 1);
            let dispatch_x = n.div_ceil(16);
            let dispatch_y = m.div_ceil(16);

            KernelParams {
                workgroup_size,
                dispatch_size: (dispatch_x, dispatch_y, 1),
                entry_point: "matrix_multiply_tiled".to_string(),
            }
        } else {
            let workgroup_size = (8, 8, 1);
            let dispatch_x = n.div_ceil(8);
            let dispatch_y = m.div_ceil(8);

            KernelParams {
                workgroup_size,
                dispatch_size: (dispatch_x, dispatch_y, 1),
                entry_point: "matrix_multiply_naive".to_string(),
            }
        }
    }
}

/// FFT kernel helper
pub struct FftKernel;

impl FftKernel {
    /// Get shader source
    pub fn shader() -> &'static str {
        FFT_SHADER
    }

    /// Create parameters for FFT
    pub fn params(n: u32) -> KernelParams {
        let workgroup_size = (256, 1, 1);
        let dispatch_size = (n.div_ceil(256), 1, 1);

        KernelParams {
            workgroup_size,
            dispatch_size,
            entry_point: "fft_cooley_tukey".to_string(),
        }
    }

    /// Calculate number of FFT stages needed
    pub fn num_stages(n: u32) -> u32 {
        (n as f32).log2() as u32
    }
}

/// Histogram equalization kernel helper
pub struct HistogramEqKernel;

impl HistogramEqKernel {
    /// Get shader source
    pub fn shader() -> &'static str {
        HISTOGRAM_EQ_SHADER
    }

    /// Create parameters for histogram computation
    pub fn compute_histogram_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "compute_histogram".to_string(),
        }
    }

    /// Create parameters for equalization
    pub fn equalize_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "histogram_equalize".to_string(),
        }
    }
}

/// Edge detection kernel helper
pub struct EdgeDetectionKernel;

impl EdgeDetectionKernel {
    /// Get shader source
    pub fn shader() -> &'static str {
        EDGE_DETECTION_SHADER
    }

    /// Create parameters for Sobel edge detection
    pub fn sobel_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "sobel".to_string(),
        }
    }

    /// Create parameters for Canny edge detection (gradient step)
    pub fn canny_gradient_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "canny_gradient".to_string(),
        }
    }
}

/// Morphology kernel helper
pub struct MorphologyKernel;

impl MorphologyKernel {
    /// Get shader source
    pub fn shader() -> &'static str {
        MORPHOLOGY_SHADER
    }

    /// Create parameters for dilation
    pub fn dilate_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "dilate".to_string(),
        }
    }

    /// Create parameters for erosion
    pub fn erode_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "erode".to_string(),
        }
    }
}

/// Texture analysis kernel helper
pub struct TextureAnalysisKernel;

impl TextureAnalysisKernel {
    /// Get shader source
    pub fn shader() -> &'static str {
        TEXTURE_ANALYSIS_SHADER
    }

    /// Create parameters for GLCM computation
    pub fn glcm_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "compute_glcm".to_string(),
        }
    }

    /// Create parameters for LBP (Local Binary Pattern)
    pub fn lbp_params(width: u32, height: u32) -> KernelParams {
        let workgroup_size = (16, 16, 1);
        let dispatch_x = width.div_ceil(16);
        let dispatch_y = height.div_ceil(16);

        KernelParams {
            workgroup_size,
            dispatch_size: (dispatch_x, dispatch_y, 1),
            entry_point: "local_binary_pattern".to_string(),
        }
    }
}

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

    #[test]
    fn test_kernel_registry() {
        let registry = KernelRegistry::new();
        assert_eq!(registry.shader_count(), 6);
        assert!(registry.has_shader("matrix_ops"));
        assert!(registry.has_shader("fft"));
        assert!(registry.has_shader("histogram_eq"));
    }

    #[test]
    fn test_kernel_registry_custom() {
        let mut registry = KernelRegistry::new();
        let initial_count = registry.shader_count();

        registry.register_shader("custom".to_string(), "custom shader code".to_string());
        assert_eq!(registry.shader_count(), initial_count + 1);
        assert!(registry.has_shader("custom"));

        assert!(registry.remove_shader("custom"));
        assert_eq!(registry.shader_count(), initial_count);
    }

    #[test]
    fn test_kernel_params() {
        let params = KernelParams::default();
        assert_eq!(params.workgroup_size, (8, 8, 1));
        assert_eq!(params.entry_point, "main");
    }

    #[test]
    fn test_kernel_params_total_threads() {
        let params = KernelParams::new((8, 8, 1), (10, 10, 1));
        assert_eq!(params.total_threads(), 8 * 8 * 10 * 10);
    }

    #[test]
    fn test_calculate_dispatch_size() {
        let (dx, dy, dz) = KernelParams::calculate_dispatch_size(1920, 1080, (16, 16, 1));
        assert_eq!(dx, 1920_u32.div_ceil(16));
        assert_eq!(dy, 1080_u32.div_ceil(16));
        assert_eq!(dz, 1);
    }

    #[test]
    fn test_matrix_multiply_kernel() {
        let params = MatrixMultiplyKernel::params(1024, 1024, 1024, true);
        assert_eq!(params.entry_point, "matrix_multiply_tiled");
        assert_eq!(params.workgroup_size, (16, 16, 1));
    }

    #[test]
    fn test_fft_kernel() {
        let params = FftKernel::params(1024);
        assert_eq!(params.entry_point, "fft_cooley_tukey");

        let stages = FftKernel::num_stages(1024);
        assert_eq!(stages, 10); // log2(1024) = 10
    }

    #[test]
    fn test_all_shaders_available() {
        assert!(!MATRIX_OPS_SHADER.is_empty());
        assert!(!FFT_SHADER.is_empty());
        assert!(!HISTOGRAM_EQ_SHADER.is_empty());
        assert!(!MORPHOLOGY_SHADER.is_empty());
        assert!(!EDGE_DETECTION_SHADER.is_empty());
        assert!(!TEXTURE_ANALYSIS_SHADER.is_empty());
    }
}