December 13, 2009More on multithreaded topological sorting
The @Priority annotation couldn't be adapted to say which free methods get scheduled first? BTW, the responsibility of knowing which nodes are free couldn't be moved to the graph of test methods?This is pushing my current algorithm even further in the sense that we not only want to schedule free nodes as they become available, we also want to schedule them in order of importance. What makes a node more important than another? Its level of dependencies. The more a node is depended upon, the more beneficial it is to schedule it as soon as possible since this will end up freeing more nodes. Admittedly, you are still bounded by the size of your thread pool, but this is exactly what we want: increasing the pool size should lead to more parallelism and therefore better performance, but the current scheduling algorithm being fair (or random) means that we are not guaranteed to see this performance increase.
My first reaction was to modify my Executor but as it turns out, you can actually do this with the existing implementation. The constructor of all the Executors takes a
All you need to do is to use that queue instead of the default one when you create your Executor and then make sure that the workers you pass it have a natural ordering. In this case, the weight of a worker is how many other workers depend on it, which is very easy to calculate.
On a related topic, I wanted to get a closer look at how the algorithm I described in my previous blog post actually works. I described the theory and I have tests that show that it seems to work as expected, but it occurred to me that I could actually "view it from the inside" with little effort.
First, I added a method called toDot which generates a Graphviz file representing the current graph. This turned out to be trivial:
A yellow node is "free", green means that the node is "ready" (to be run in the thread pool), grey is "finished" and white nodes haven't been processed yet. Dotted arrows represent dependencies that have been satisfied.
As you can see, the execution matches very closely what you would expect based on my description of the algorithm and I confirmed that changing the size of the thread pool creates different executions.
Comments
Your example always schedules every free test, which would happen with the random scheduler as well. Do you have an example where scheduling random free nodes differs from scheduling free nodes based on their dependencies? Posted by: Nish at December 13, 2009 01:21 PMNish, Take a look at this new graph: beust.com/topo/Topo8.png When a1 completes, it frees both b1 and c1, but with this scheme, c1 would have a weight of 0 (no dependees) while b1 has a weight of 2 (2 nodes depend on it), so b1 would get scheduled first, and when it completes, it will free 2 more nodes (b2 and b3). Post a comment
|