Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient foreach child evaluation in parallel

I have a list of objects, each with a bool ShouldRun() method on them.

I am currently iterating over the list of objects, and checking ShouldRun() on each object, and calling Run() on the first one to return true

foreach (child in Children)
{
   if (child.ShouldRun())
   {
      child.Run();
      break;
    }
 }

I would like to do this in parallel, because evaluating shouldRun can take a decent amount of time, and it is advantageous to let later elements in the collection start their evaluation early.

However, I cannot think of a way to do this that will satisfy these conditions :

1 Only run one item

2 Do not run a later item if an earlier item is true, or has not finished evaluating yet

3 If all "earlier" items have returned false, and a middle item returns true, do not wait on a later item to finish evaluating because you know it can't override anything earlier.

I thought of doing a parallel "where" linq query to retrieve all the items that shouldRun() and then sorting, but this will violate condition #3

Ideas?

Background info :

The system is for a generalized robotics AI system.

Some of the higher priority tasks can be triggered by immediately known sensor variables eg : Im falling over, fix it!

other tasks might be computationally intensive (do image recognition from the camera, and approach visible objectives)

other tasks might be database or remotely driven (look up a list of probable objective locations from a database, and then navigate there to see if you can get into visible range of one of them)

Some of the tasks themselves have child tasks, which is essentially going to start this process over recursively at a subset of tasks, and the grandchild task will be passed up through the chain

like image 788
Jason Coyne Avatar asked Jun 26 '10 22:06

Jason Coyne


1 Answers

I'm not a PLINQ genius, but wouldn't this simpler answer suffice?

var childToRun = Children.AsParallel().AsOrdered()
    .Where(x => x.ShouldRun()).FirstOrDefault();
childToRun.Run();  
like image 75
Stephen Cleary Avatar answered Oct 12 '22 00:10

Stephen Cleary