Grasshopper

algorithmic modeling for Rhino

I am trying to multithread a very simple mathematical calculation in C#. I use Parallel.ForEach which I guess is why the number of output results is different from the input. I would appreciate some help on the matter.

http://snag.gy/b6tXX.jpg


Thanks!

Views: 865

Attachments:

Replies to This Discussion

List.Add probably isn't a thread-safe method. List in general isn't. You should consider using a concurrent collection of some sort.

I am sorry for asking you to spell it out for me but is your suggestion to try to split the list into 8 separate lists perform a loop with one thread on each list and at the end join the lists together? 

There are several ways to approach it. One is to populate one list per thread, then aggregate all lists afterwards. Another solution is to use a concurrent collection, where adding items is threadsafe. Another solution still is to prepare a list or array ahead of time, and assign values to the appropriate index during your loops.

The last method is fast, retains ordinality, and conceptually easy, though it may be harder to implement given the details of the problem at hand.
Using a Parallel.Foreach or Task based approach tends not to yield too much improvement when the iterations are many and short. There's too much overhead to make it worth the multithreading. Instead it makes more sense to split the array into N runs and perform each run in a single task. In my (limited) experience the best value for N is about twice the number of logical processors on your machine.

Well if someone is interested in my benchmarks here is the file with 5 different ways of multithreading with C#, a python option and two ways of doing the same operation with native components. Oddly for me it seems that python multithreading works quicker than everything tested here!

Attachments:

A note on profiling. You should be careful with relying too much on the GH profiler when timing code within a script. Especially if you have an output parameter with many items as this may be a larger bottleneck than the actual code! One way of getting around this is to wrap the data in a Python list and read it downstream as an item (see attached for an example).

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service