I want to run an algorithm on large graphs concurrently, using multi-core parallelism. I have been working on it for a while, but haven't been able to come up with a good solution.
This is the naive algorithm:
W - a very large number
double weight = 0
while(weight < W)
- v : get_random_node_from(Graph)
- weight += calculate(v)
- I looked into fork-and-join, but can't figure out a way to divide this problem into smaller subproblems.
- Then I tried using Java 8 streams, for which I need to create a lambda expression. When I tried doing something like this:
double weight = 0
Callable<Object> task = () -> {
can not update weight here, as it needs to be final
}
My question is, is it possible to update a variable like weight in a lambda method? Or is there a better way in which this problem can be solved?
The closest I have got is by using ExecutorService, but run into the problems of synchronization.
------------EDIT--------------
Here is the detailed algorithm:
In a nutshell, what I am trying to do, is traverse a massive graph, perform an operation on randomly selected nodes(as long as weight < W) and update a global structure Index.
This is taking too long as it doesn't utilize the full power of the CPU.
Ideally, all threads/processes on multiple cores would perform the operations on the randomly selected nodes, and update the shared weight and Index.
Note: It doesn't matter if different threads pick up the same node, as it's random without replacement.
Algorithm:
function Serial () {
List<List<Integer>> I (shared data structure which I want to update)
double weight
//// Task which I want to parallelize
while(weight < W) {
v : get_random_node_from(Graph)
bfs(v, affected_nodes) ...// this will fill up affected_nodes by v
foreach(affected_node in affected_nodes) {
// update I related to affected_node
// and do other computation
}
weight += affected_nodes.size()
}
///////// Parallelization ends here
use_index(I) // I is passed now to some other method(not important) to get further results
}
The important thing is, all threads update the same I and weight.
Thanks.
weight < W. You start severalcalculate(v), but after the firstweight += calculate(v)weightbecomes>=W. Should other calculation be cancelled, or they can finish their work? If they can finish their work, can and should they add their results toweight?weight > Wall the processes should stop. I will edit the question to add more details.Callableto pass to executor need not to be a lambda. It can be of any type that implementsCallableand so can access to whatever information.