Grasshopper

algorithmic modeling for Rhino

Parallel execution of code within C# scripting components with List inputs

Is it possible to utilize the System.Threading.Tasks.Parallel namespace within a scripting component (c#,VB,python) when the inputs are list(s) of parameters.  Currently, the default behavior of the compiled source appears to iterate over the input data lists and calls the RunScript method with single input items from the list. This seems to preclude any parallel execution as each call to the RunScript does not have access to the entire list.  My goal is to see if I can get c# scripting component to run faster when the work it is doing would fit nicely in a Parallel.For loop.  This idea works well when the inputs are single items and the output is a List (see http://python.rhino3d.com/entries/607-Multithreaded-Python).  Am I out of luck with lists?

This would be an example of a parallel version of the Sweep 1 component.  The c# fails as R and S seem to be never larger than 1 item during execution. 

private void RunScript(List<Curve> R, List<Curve> S, ref object Swp)
{
double Ang_Tol = doc.ModelAngleToleranceRadians;
double Abs_Tol = doc.ModelAbsoluteTolerance;
var results = new List<Rhino.Geometry.Brep>();
int count = R.Count;


System.Threading.Tasks.Parallel.For(0, count - 1, i =>
{
var sweep = new Rhino.Geometry.SweepOneRail();
sweep.AngleToleranceRadians = Ang_Tol;
sweep.ClosedSweep = false;
sweep.SweepTolerance = Abs_Tol;
var breps = sweep.PerformSweep(R[i], S[i]);
results.Add(breps[0]);
});


Swp = results;

}

Views: 1474

Replies to This Discussion

Did you right click on your input variables and make sure they are set to "List Access"?

Yes I did have "List Access" but I had not made sure that the incoming data was simplified and matching appropriately.  Once I flattened the incoming streams then the code above works great.  

I Need to go back and review more my understanding of the data structures.  I am assuming I could also get the "Tree Access" to work as well as long as they incoming streams match.

Thanks  

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