Ghpython and parallel

Hello , is parallel enabled by default in Ghpython ?
If not, how to use it with python script?

Have you checked this blog post?

Yes but don’t work when the function have many input and it looks work only with ghpythonlib.components

import Rhino.Geometry as rg
from ghpythonlib.parallel import run
def arc(args):
    center, radius, angle = args
    c = rg.Circle(center, radius)
    return rg.Arc(c, angle)
a = run(arc, zip(centers, radiuses, angles), True)

Seghier.gh (5.9 KB)

1 Like

Thank you @Mahdiyar
So the inputs must have list access? i did similar thing with item access but i got duplicate result 3 times;
and for a complex function it is not easy to use parallel

That’s exactly the problem – writing parallel code that actually works is very complicated. Sometimes it’s easier to write single-threaded code. The time to debug outweighs the time to execute.

4 Likes

There is also the Parallel.For & Parallel.ForEach part of the System.Threading.Tasks namespace. You just provide a lambda function…

I usually do this if it really needs to be fast:

Write Code as readable as possible → Measure performance → Identify the bottleneck → Put the critical code in one function → Find a better single-threaded solution by improving the algorithm (Solves it for 90%) → Rewrite generalized library functionality (e.g. from Rhinocommon) to only do the things required (Solves it for 99%) → If it can be multi-threaded, implement parallel solution → Try to apply any sort of ‘micro’-optimisations (no safety checks, ‘branchless’, directly access of memory, SIMD) → Rewrite portions in C and/or Assembly.

But I only had 2 situations where any of these steps were required. None of them where part of Rhino/GH. It just doesn’t make sense to improve to perfection, just to gain 200 ms.

2 Likes

The ghpythonlib.parallel.run function is actually just a friendly wrapper around System.Threading.Tasks.Parallel.ForEach.

3 Likes