oxigdal-algorithms 0.1.7

High-performance SIMD-optimized raster and vector algorithms for OxiGDAL - Pure Rust geospatial processing
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
//! Comprehensive benchmarks for SIMD-optimized algorithms
//!
//! This benchmark suite measures the performance of SIMD-accelerated implementations
//! of focal, texture, terrain, hydrology, and cost-distance algorithms.
#![allow(
    missing_docs,
    clippy::expect_used,
    clippy::panic,
    clippy::unit_arg,
    clippy::unnecessary_cast
)]

use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use oxigdal_algorithms::simd::{
    cost_distance_simd, focal_simd, hydrology_simd, terrain_simd, texture_simd,
};
use std::hint::black_box;

/// Benchmark focal statistics operations
fn bench_focal_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("focal_simd");

    for size in [100, 500, 1000] {
        let pixels = size * size;
        let src = vec![100.0_f32; pixels];
        let mut dst = vec![0.0_f32; pixels];

        group.throughput(Throughput::Elements(pixels as u64));

        // Focal mean (separable)
        group.bench_with_input(
            BenchmarkId::new("focal_mean_separable", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    focal_simd::focal_mean_separable_simd(
                        black_box(&src),
                        black_box(&mut dst),
                        s,
                        s,
                        3,
                        3,
                    )
                    .expect("focal mean benchmark failed");
                });
            },
        );

        // Focal variance
        group.bench_with_input(BenchmarkId::new("focal_variance", size), &size, |b, &s| {
            b.iter(|| {
                focal_simd::focal_variance_simd(black_box(&src), black_box(&mut dst), s, s, 3)
                    .expect("focal variance benchmark failed");
            });
        });

        // Focal standard deviation
        group.bench_with_input(BenchmarkId::new("focal_stddev", size), &size, |b, &s| {
            b.iter(|| {
                focal_simd::focal_stddev_simd(black_box(&src), black_box(&mut dst), s, s, 3)
                    .expect("focal stddev benchmark failed");
            });
        });

        // Focal min/max
        let mut min_out = vec![0.0_f32; pixels];
        let mut max_out = vec![0.0_f32; pixels];

        group.bench_with_input(BenchmarkId::new("focal_min_max", size), &size, |b, &s| {
            b.iter(|| {
                focal_simd::focal_min_max_simd(
                    black_box(&src),
                    black_box(&mut min_out),
                    black_box(&mut max_out),
                    s,
                    s,
                    3,
                )
                .expect("focal min max benchmark failed");
            });
        });

        // Focal convolution
        let kernel = vec![1.0_f32 / 9.0; 9]; // 3x3 uniform kernel

        group.bench_with_input(BenchmarkId::new("focal_convolve", size), &size, |b, &s| {
            b.iter(|| {
                focal_simd::focal_convolve_simd(
                    black_box(&src),
                    black_box(&mut dst),
                    s,
                    s,
                    black_box(&kernel),
                    3,
                    3,
                    true,
                )
                .expect("focal convolve benchmark failed");
            });
        });
    }

    group.finish();
}

/// Benchmark texture analysis operations
fn bench_texture_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("texture_simd");

    for gray_levels in [8, 16, 32] {
        let size = 100;
        let pixels = size * size;
        let quantized = vec![0_u8; pixels];
        let mut glcm = vec![0.0_f32; gray_levels * gray_levels];

        group.throughput(Throughput::Elements(pixels as u64));

        // GLCM construction
        group.bench_with_input(
            BenchmarkId::new("glcm_construct", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::glcm_construct_simd(
                        black_box(&quantized),
                        black_box(&mut glcm),
                        size,
                        size,
                        gl,
                        1,
                        0,
                    )
                    .expect("GLCM construct benchmark failed");
                });
            },
        );

        // GLCM normalization
        let mut glcm_to_normalize = vec![1.0_f32; gray_levels * gray_levels];

        group.bench_with_input(
            BenchmarkId::new("glcm_normalize", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::glcm_normalize_simd(black_box(&mut glcm_to_normalize), gl)
                        .expect("GLCM normalize benchmark failed");
                });
            },
        );

        // Individual Haralick features
        let normalized_glcm =
            vec![1.0_f32 / (gray_levels * gray_levels) as f32; gray_levels * gray_levels];

        group.bench_with_input(
            BenchmarkId::new("texture_contrast", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::texture_contrast_simd(black_box(&normalized_glcm), gl)
                        .expect("texture contrast benchmark failed");
                });
            },
        );

        group.bench_with_input(
            BenchmarkId::new("texture_energy", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::texture_energy_simd(black_box(&normalized_glcm), gl)
                        .expect("texture energy benchmark failed");
                });
            },
        );

        group.bench_with_input(
            BenchmarkId::new("texture_entropy", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::texture_entropy_simd(black_box(&normalized_glcm), gl)
                        .expect("texture entropy benchmark failed");
                });
            },
        );

        group.bench_with_input(
            BenchmarkId::new("texture_homogeneity", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::texture_homogeneity_simd(black_box(&normalized_glcm), gl)
                        .expect("texture homogeneity benchmark failed");
                });
            },
        );

        group.bench_with_input(
            BenchmarkId::new("texture_correlation", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::texture_correlation_simd(black_box(&normalized_glcm), gl)
                        .expect("texture correlation benchmark failed");
                });
            },
        );

        // All Haralick features
        group.bench_with_input(
            BenchmarkId::new("haralick_features_complete", gray_levels),
            &gray_levels,
            |b, &gl| {
                b.iter(|| {
                    texture_simd::compute_haralick_features_simd(black_box(&normalized_glcm), gl)
                        .expect("Haralick features benchmark failed");
                });
            },
        );
    }

    group.finish();
}

/// Benchmark terrain analysis operations
fn bench_terrain_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("terrain_simd");

    for size in [100, 500, 1000] {
        let pixels = size * size;
        let dem = vec![100.0_f32; pixels];
        let mut output = vec![0.0_f32; pixels];

        group.throughput(Throughput::Elements(pixels as u64));

        // Slope
        group.bench_with_input(BenchmarkId::new("terrain_slope", size), &size, |b, &s| {
            b.iter(|| {
                terrain_simd::terrain_slope_simd(
                    black_box(&dem),
                    black_box(&mut output),
                    s,
                    s,
                    30.0,
                )
                .expect("terrain slope benchmark failed");
            });
        });

        // Aspect
        group.bench_with_input(BenchmarkId::new("terrain_aspect", size), &size, |b, &s| {
            b.iter(|| {
                terrain_simd::terrain_aspect_simd(
                    black_box(&dem),
                    black_box(&mut output),
                    s,
                    s,
                    30.0,
                )
                .expect("terrain aspect benchmark failed");
            });
        });

        // TPI (Topographic Position Index)
        group.bench_with_input(BenchmarkId::new("terrain_tpi", size), &size, |b, &s| {
            b.iter(|| {
                terrain_simd::terrain_tpi_simd(black_box(&dem), black_box(&mut output), s, s, 3)
                    .expect("terrain TPI benchmark failed");
            });
        });

        // TRI (Terrain Ruggedness Index)
        group.bench_with_input(BenchmarkId::new("terrain_tri", size), &size, |b, &s| {
            b.iter(|| {
                terrain_simd::terrain_tri_simd(black_box(&dem), black_box(&mut output), s, s)
                    .expect("terrain TRI benchmark failed");
            });
        });

        // Roughness
        group.bench_with_input(
            BenchmarkId::new("terrain_roughness", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    terrain_simd::terrain_roughness_simd(
                        black_box(&dem),
                        black_box(&mut output),
                        s,
                        s,
                        3,
                    )
                    .expect("terrain roughness benchmark failed");
                });
            },
        );
    }

    group.finish();
}

/// Benchmark hydrology operations
fn bench_hydrology_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("hydrology_simd");

    for size in [100, 500, 1000] {
        let pixels = size * size;
        let dem = vec![100.0_f32; pixels];

        group.throughput(Throughput::Elements(pixels as u64));

        // Flow direction (D8)
        let mut flow_dir = vec![0_u8; pixels];

        group.bench_with_input(
            BenchmarkId::new("flow_direction_d8", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    hydrology_simd::flow_direction_d8_simd(
                        black_box(&dem),
                        black_box(&mut flow_dir),
                        s,
                        s,
                    )
                    .expect("flow direction D8 benchmark failed");
                });
            },
        );

        // Sink detection
        let mut sinks = vec![0_u8; pixels];

        group.bench_with_input(BenchmarkId::new("detect_sinks", size), &size, |b, &s| {
            b.iter(|| {
                hydrology_simd::detect_sinks_simd(black_box(&dem), black_box(&mut sinks), s, s)
                    .expect("detect sinks benchmark failed");
            });
        });

        // Slope computation
        let mut slope = vec![0.0_f32; pixels];

        group.bench_with_input(BenchmarkId::new("compute_slope", size), &size, |b, &s| {
            b.iter(|| {
                hydrology_simd::compute_slope_simd(black_box(&dem), black_box(&mut slope), s, s)
                    .expect("compute slope benchmark failed");
            });
        });

        // Flow accumulation initialization
        let mut flow_acc = vec![0.0_f32; pixels];

        group.bench_with_input(
            BenchmarkId::new("init_flow_accumulation", size),
            &size,
            |b, _| {
                b.iter(|| {
                    hydrology_simd::initialize_flow_accumulation_simd(black_box(&mut flow_acc))
                        .expect("init flow accumulation benchmark failed");
                });
            },
        );

        // Flat detection
        let mut flat = vec![0_u8; pixels];

        group.bench_with_input(BenchmarkId::new("detect_flats", size), &size, |b, &s| {
            b.iter(|| {
                hydrology_simd::detect_flats_simd(black_box(&dem), black_box(&mut flat), s, s, 0.1)
                    .expect("detect flats benchmark failed");
            });
        });
    }

    group.finish();
}

/// Benchmark cost-distance operations
fn bench_cost_distance_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("cost_distance_simd");

    for size in [100, 500, 1000] {
        let pixels = size * size;
        let mut sources = vec![0_u8; pixels];
        sources[pixels / 2] = 1; // Single source in center

        let mut distance = vec![0.0_f32; pixels];

        group.throughput(Throughput::Elements(pixels as u64));

        // Euclidean distance
        group.bench_with_input(
            BenchmarkId::new("euclidean_distance", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    cost_distance_simd::euclidean_distance_simd(
                        black_box(&sources),
                        black_box(&mut distance),
                        s,
                        s,
                        1.0,
                    )
                    .expect("Euclidean distance benchmark failed");
                });
            },
        );

        // Manhattan distance
        group.bench_with_input(
            BenchmarkId::new("manhattan_distance", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    cost_distance_simd::manhattan_distance_simd(
                        black_box(&sources),
                        black_box(&mut distance),
                        s,
                        s,
                        1.0,
                    )
                    .expect("Manhattan distance benchmark failed");
                });
            },
        );

        // Chebyshev distance
        group.bench_with_input(
            BenchmarkId::new("chebyshev_distance", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    cost_distance_simd::chebyshev_distance_simd(
                        black_box(&sources),
                        black_box(&mut distance),
                        s,
                        s,
                        1.0,
                    )
                    .expect("Chebyshev distance benchmark failed");
                });
            },
        );

        // Cost buffer initialization
        let mut buffer = vec![0.0_f32; pixels];

        group.bench_with_input(BenchmarkId::new("init_cost_buffer", size), &size, |b, _| {
            b.iter(|| {
                cost_distance_simd::initialize_cost_buffer_simd(
                    black_box(&mut buffer),
                    f32::INFINITY,
                )
                .expect("init cost buffer benchmark failed");
            });
        });

        // Neighbor costs computation (single cell)
        let cost_surface = vec![1.0_f32; pixels];
        let mut neighbor_costs = [0.0_f32; 8];

        group.bench_with_input(
            BenchmarkId::new("compute_neighbor_costs", size),
            &size,
            |b, &s| {
                b.iter(|| {
                    cost_distance_simd::compute_neighbor_costs_simd(
                        black_box(&cost_surface),
                        s,
                        s,
                        s / 2,
                        s / 2,
                        1.0,
                        black_box(&mut neighbor_costs),
                    )
                    .expect("compute neighbor costs benchmark failed");
                });
            },
        );
    }

    group.finish();
}

criterion_group!(
    benches,
    bench_focal_operations,
    bench_texture_operations,
    bench_terrain_operations,
    bench_hydrology_operations,
    bench_cost_distance_operations
);

criterion_main!(benches);