Skip to content

Optimization/shadow land water probability - #456

Open
NinaHerrmann wants to merge 5 commits into
davidfrantz:developfrom
NinaHerrmann:optimization/shadow-land-water_probability
Open

NinaHerrmann wants to merge 5 commits into
davidfrantz:developfrom
NinaHerrmann:optimization/shadow-land-water_probability

Conversation

@NinaHerrmann

@NinaHerrmann NinaHerrmann commented Nov 24, 2026 •

Copy link
Copy Markdown

Speed up shadow_probability/land_probability/water_probability

Summary

  1. For all mentioned functions: histogram-based quantile (for data structures with the type small ) instead of GDAL full sort.
    The quantile() function from GDAL first sorts a double array and then returns the quantile. While this is efficient for smaller data structures, large double arrays take some time (and come with additional memory when converting small to double). This PR implements a histogram instead. It requires less space and is faster for Satellite image sizes (Sorting is $O(n \log n)$, Quantile creation m=65536 bins is $O(n + m)$ ). The principle is displayed in the image below.
sorting_vs_histogram_4_bins
  1. For shadow_probability Greyscale Reconstruction continue is changed to break to exit the loop early. Moreover, inlining the enqueue and dequeue function allows the compiler to directly insert the function. Finally, reading MARKER[p] once and reusing it should provide slight cache improvements.

This change is applied in:

File Change
force_pr/src/modules/cross-level/stats-cl.c New quantile_short(), quantile_hist() and hist_short() functions
src/modules/low-level/cloud-ll.c shadow_probability(), land_probability() & water_probability
src/modules/cross-level/imagefuns-cl.c greyscale_reconstruction_ slight initalization improvement, and continue vs. break.
src/modules/cross-level/queue-cl.h src/modules/cross-level/queue-cl.c inlining function

Testing

Unit Test

Added the test_quantile-cl unit test, which compares the new quantile_short() with the previously used GDAL quantile() function. I am not sure how many test you would like to have, as those also take some time in the pipeline, so I can easily reduce/increase the configuration tested (make test_quantile-cl). Or also test other functions not yet included.

  • src/tests/test_quantile-cl.c
  • build/tests/test_quantile-cl.mk
  • build/tests.mk

End-to-end test

I ran a test set of Sentinel-2 images and Landsat images with 4 processes and 4 threads. Level-2 output was compared to be bit-identical. One note about the attached graph. For the splitting in Shadow probability, I took the max of the runtime of the threads. So, sometimes greyscale + quantile would be slightly bigger than the overall loop.

combined_sentinel_baseline_vs_pr0 combined_landsat_baseline_vs_pr0

Additional experiment: Downhill filter (not included)

  • I also ran some experiments filling pixels level-based (A downhill filter), which provided slight runtime improvements, but I am not sure whether to include this in the pull request, as it is harder to read and I am not sure how sensitive it is across different caches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant