Thread management advice - Is TPL a good idea?
c#, heuristics, parallel-processing
Solution
I would definitely look at the TPL. It allows you to abstract your problem. You can think about tasks and how they do work and share data, rather than spending as much time on the underlying thread model and creating your wn threads and managing them. The TPL will allow you to create tasks which it assigns to a thread pool. The TPL then manages the pool and will adjust the number of running tasks to maximize performance. It will do this on a variety of hardware configurations (cores) which makes it easier to develop and application which will not need a major rewrite when moving between different hardware.
There's still a lot you have to think about, especially around sharing state. The the TPL is usually a better approach than rolling your own unless you are very experienced with threading and/or have some special case application which the TPL is poorly suited.
1.They need to be initalized with a different 'optimization metric'. In other words they are attempting to optimize different things, with a precedence level set within code. This means they all run slightly different calculation engines. I'm not sure if I can do this with the TPL..
You can do this by creating tasks and passing them different starting conditions.
2.If one of the threads finds a better solution that the currently best known solution (which needs to be shared across all threads) then it needs to update the best solution, and force a number of other threads to restart (again this depends on precedence levels of the optimization metrics).
It is possible to cancel tasks and start new ones.
3.I may also wish to combine certain calculations across threads (e.g. keep a union of probabilities for a certain approach to the problem). This is probably more optional though.
Not sure I understand this requirement.
4.The whole system needs to be thread safe obviously and I want it to be running as fast as possible.
Even with the TPL is you share data across tasks (threads) then this still your responsibility to do this in a threadsafe way. However, the TPL comes with several threadsafe classes for queue, collection, bag etc.
From the sounds of it this a variant of the master/worker pattern with some speculative execution and work stealing thrown in. You can find more details on this and other patterns at http://parallelpatterns.codeplex.com/ there's a link at the bottom of the page to a white paper by Stephen Toub covering additional details too.
Problem
I'm hoping to get some advice on the use of thread managment and hopefully the task parallel library, because I'm not sure I've been going down the correct route. Probably best is that I give an outline of what I'm trying to do. Given a Problem I need to generate a Solution using a heuristic based algorithm. I start of by calculating a base solution, this operation I don't think can be parallelised so we don't need to worry about. Once the inital solution has been generated, I want to trigger n threads, which attempt to find a better solution. These threads need to do a couple of things: - They need to be initalized with a different 'optimization metric'. In other words they are attempting to optimize different things, with a precedence level set within code. This means they all run slightly different calculation engines. I'm not sure if I can do this with the TPL.. - If one of the threads finds a better solution that the currently best known solution (which needs to be shared across all threads) then it needs to update the best solution, and force a number of other threads to restart (again this depends on precedence levels of the optimization metrics). - I may also wish to combine certain calculations across threads (e.g. keep a union of probabilities for a certain approach to the problem). This is probably more optional though. - The whole system needs to be thread safe obviously and I want it to be running as fast as possible. I tried quite an implementation that involved managing my own threads and shutting them down etc, but it started getting quite complicated, and I'm now wondering if the TPL might be better. I'm wondering if anyone can offer any general guidance? Thanks...