-2

I've got a number of objects. I need to deal with each pair concurrently (not every pair, arbitrary pairings), but each object should be only dealt with in serial.

I'm struggling to describe an algorithm which can deal with this, preferably without involving a bunch of locks. I've got the PPL from Microsoft, so I have some fairly high-level concurrency functions at my disposal.

Finally, a lot of the time I'll be dealing with very few pairings per object- with zero and one especially common. But many-many pairings are a boundary condition I do have to deal with.

Any suggestions for such an algorithm?

Puppy
  • 138,897
  • 33
  • 232
  • 446

1 Answers1

1

The PPl works really well if you can structure your algorithm as either a parallel loop: parallel_for, parallel_for_each (with no dependencies between iterations) or a parallel_reduction where you are combining / merging solutions.

It also works well if you can structure your algorithm into divide & conquer style recursion.

Can you restructure the serial form of your algorithm to be fit these? It will make it easier to parallelize.

Your description wasn't quite specific enough for me to give specific suggestions, but it seems you are trying to find distinct pairings / prevent duplication of work. A few techniques which may be useful (if you're more specific, you may get more):

1) put all elements into a set / map then iterate over the unique ones. Alternatively, if you're using a concurrent_unordered_set / map then you can check the status of the insert method to check for uniqueness within a parallel loop.

2) you also may benefit from using a shared_ptr, std::future or async_future (see the docs) to assist with the uniqueness.

3) if you have more complex dependdencies, you may want to look at concurrency::task::then or the agents library.

Rick
  • 3,145
  • 15
  • 17