r.sim: Parallelize dx/dy derivatives computation using OpenMP#7094
Open
Abhi-d-gr8 wants to merge 1 commit intoOSGeo:mainfrom
Open
r.sim: Parallelize dx/dy derivatives computation using OpenMP#7094Abhi-d-gr8 wants to merge 1 commit intoOSGeo:mainfrom
Abhi-d-gr8 wants to merge 1 commit intoOSGeo:mainfrom
Conversation
Member
|
Can you please add the diff into your description to add the time measuring code? |
Contributor
Author
|
Hi @wenzeslaus ! |
Member
wenzeslaus
reviewed
Feb 16, 2026
| @@ -1,4 +1,6 @@ | |||
| #include "simlib.h" | |||
| #include <grass/gis.h> | |||
Member
There was a problem hiding this comment.
Needed? (It is for debug, but here without it...)
Contributor
Author
There was a problem hiding this comment.
Yes it is not needed , I was using different approaches so had included it earlier but yes its not required.
Member
|
Another thing, do the current tests actually cover the two cases for dx-dy and for enabled/disabled parallelism? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Overview
This PR parallelizes the dx/dy slope derivatives computation in
simlib/derivatives.c(Horn 3×3 method), addressing #7039.The main simulation already supports multiple cores, but the dx/dy computation was still serial. This change enables that step to use available CPU cores as well.
What changed
parallel foron the outer row loop.default(none)with explicitshared(...)andprivate(...)scoping.Performance Evaluation
To isolate the impact of this change, timing was performed inside
derivatives()only, excluding raster I/O and the main simulation loop.This avoids masking the effect, since total
r.sim.waterruntime is dominated by the simulation phase.Test Setup
omp_get_wtime()aroundderivatives()onlyResults (Apple M3 Air)
OMP_NUM_THREADS=1: ~0.19 s (average)OMP_NUM_THREADS=8: ~0.06 s (average)This confirms that the dx/dy computation scales across cores as intended.
As expected, the overall
r.sim.waterruntime improvement is smaller sincederivatives()is only one stage of the workflow.Build / OpenMP Notes
To observe multi-threaded behavior in
derivatives():GRASS must be configured with OpenMP support:
The compiler must support OpenMP (e.g.,
libompon macOS).The source includes guarded OpenMP headers:
Control the number of threads via:
If OpenMP is not available, the code compiles and runs serially without any behavior or numerical changes.
Reproducible Benchmark Command
For reference, this is the exact command used for the interleaved benchmark:
Timing instrumentation (added for evaluation)
To isolate the performance of
derivatives()(not masked by the main simulation),I temporarily added the following OpenMP-guarded timing block:It used for benchmarking and does not affect numerical behavior.