Multithreading in Python - Again

I’d like to look at some reaction-diffusion patterns, and I’m trying to port
a C# component from this thread to Python: http://www.grasshopper3d.com/forum/topics/reaction-diffusion-on-triangular-mesh
It works, except the Python won’t multithread. On my machine the test case I’ve posted here is 69ms in C#, 6.7s in Python. I understand it won’t be as fast as the C#, but I’d rather write Python…I wouldn’t mind if it took 2-3x long but 100x?

The C# uses System.Threading.Tasks straightforwardly, and I tried this and also ghpythonlib.parallel in Python, and both are slower than no threading. (There are profiling timechecks in the code.) What’s driving me bats is that there are no rhinoscriptsyntax or Rhino functions at all in the code I’m trying to multithread, nothing but arithmetic.

I’ve read a lot of threads here and am stumped as to what is blocking this code from parallelizing. Any hints? If I ever get it working I’ll post the component.

Thanks!

python wont parallelize.gh (54.8 KB)

I have done lots of work on this in Python. The only cases where I get 2-3X speed up are when a slow running rhino function is called. This gives the Python interpreter some time to execute another operation. If you have all arithmetic then my experience is that you will get no speedup and likely slow down the more threads you try to execute in parallel. Going the other way, Python to C# using a DLL, I get up to 300X speed up. So yes, I would say to expect your arithmetic operation in Python to be at least 100X slower. If you want to speed them up then use Visual Studio to make a DLL for executing them. If you want to go this route I can give you more details.

Regards,
Terry.

Thanks. Guess I’ll just use C#, since it’s code that I expect to maul a lot as part of ye olde creative processe, rather than something to optimize and be done with.

1 Like