Skip to content

r.mapcalc: Support parallel computing by OpenMP #5742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

cyliang368
Copy link
Contributor

@cyliang368 cyliang368 commented May 22, 2025

This PR parallelizes a part of the r.mapcalc module using OpenMP. Note that raster3d is still not parallelized yet.

  • add note and benchmark to documentation
  • add openmp to CMakeLists.txt

@github-actions github-actions bot added raster Related to raster data processing Python Related code is in Python C Related code is in C module tests Related to Test Suite labels May 23, 2025
@landam landam requested review from HuidaeCho and marisn May 23, 2025 19:45
@landam landam added this to the 8.5.0 milestone May 23, 2025
@petrasovaa
Copy link
Contributor

petrasovaa commented Jun 6, 2025

NOTE: this is not valid benchmark beyond 4 cores, see the one below

I ran a benchmark on my desktop and it looks very good! I compared different data size and complexity of expression:
r_mapcalc_time
r_mapcalc_speedup
r_mapcalc_efficiency

My machine info:

Architecture:             x86_64
  CPU op-mode(s):         32-bit, 64-bit
  Address sizes:          46 bits physical, 48 bits virtual
  Byte Order:             Little Endian
CPU(s):                   28
  On-line CPU(s) list:    0-27
Vendor ID:                GenuineIntel
  Model name:             Intel(R) Core(TM) i9-10940X CPU @ 3.30GHz
    CPU family:           6
    Model:                85
    Thread(s) per core:   2
    Core(s) per socket:   14
    Socket(s):            1
    Stepping:             7
    CPU(s) scaling MHz:   33%
    CPU max MHz:          4800.0000
    CPU min MHz:          1200.0000

Overall I don't see any slow down with more cores as @cyliang368 showed me before, so it may behave differently on different machines. I will do more testing but this looks very promising!

To clarify the plots, 'simple' means A+B, complex is (A + B - 2 * B + A * B - A / B) / 2 and neighbors is if ((A[5, 5] + B[-5, -5]) > 0, 1.6, 0.1). So the complexity of the expression makes a big difference and given there are often even larger expressions, this has potential to be even more useful.

@echoix
Copy link
Member

echoix commented Jun 6, 2025

So, in the tests you ran, there's not much benefit after 4 cores. Does it apply to everything, or just the expressions tested?

@petrasovaa
Copy link
Contributor

So, in the tests you ran, there's not much benefit after 4 cores. Does it apply to everything, or just the expressions tested?

I suspect much larger data and more complex expression (like with eval function), we would see benefit with couple more cores. But most parallel tools in GRASS in my experience behave like that.

@cyliang368
Copy link
Contributor Author

This reminds me of a similar behavior I observed while running benchmarks for v.surf.rst and r.texture on my laptop last year. I believe my laptop is not powerful enough, so more overheads occur when more cores are used.

Overall, I think the benchmarks from @petrasovaa 's machine look good. I will add notes and these benchmarks to the document when I have time.

@petrasovaa
Copy link
Contributor

So I needed to run new benchmarks because I missed the fact that the code was already constrained to 4 cores max... So I removed locally the restriction and luckily the new results are not that much different:

r_mapcalc_time
r_mapcalc_speedup
r_mapcalc_efficiency

Based on this I would suggest to not constrain it, there may be some slow down for simple expressions, but it doesn't look that significant. Also, the more complex expression was able to take advantage of more than 4 cores.

@github-actions github-actions bot added docs markdown Related to markdown, markdown files CMake labels Jun 13, 2025
cyliang368 and others added 3 commits June 15, 2025 19:53
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@petrasovaa
Copy link
Contributor

Hm, I meant to test it on my fork, but apparently pushed it here...

@petrasovaa
Copy link
Contributor

It looks like test_nmedian_bug_3296.py is failing on windows. One test is crashing and other give incorrect results.

@petrasovaa
Copy link
Contributor

It looks like test_nmedian_bug_3296.py is failing on windows. One test is crashing and other give incorrect results.

My guess is, it is connected to using static variables:
https://github.com/OSGeo/grass/blob/main/lib/calc/xnmedian.c#L47

On my fork I removed the reallocation so it allocates every time and the tests are running now. If that's indeed the cause the question is how to deal with that, @HuidaeCho, @cyliang368 any idea? I assume the constant allocation would slow it down. Maybe there could be a smarter way to allocate it.

@cyliang368
Copy link
Contributor Author

cyliang368 commented Jun 26, 2025

It looks like test_nmedian_bug_3296.py is failing on windows. One test is crashing and other give incorrect results.

My guess is, it is connected to using static variables: https://github.com/OSGeo/grass/blob/main/lib/calc/xnmedian.c#L47

On my fork I removed the reallocation so it allocates every time and the tests are running now. If that's indeed the cause the question is how to deal with that, @HuidaeCho, @cyliang368 any idea? I assume the constant allocation would slow it down. Maybe there could be a smarter way to allocate it.

I haven't had a good idea about this error, which only occurs on Windows, now. 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C CMake docs markdown Related to markdown, markdown files module Python Related code is in Python raster Related to raster data processing tests Related to Test Suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants