I just taught myself some OpenMP and this could be stupid. Basically I'm trying to parallelize a breadth first search program in c++, with each node taking a long time to process. Here's an example code:
queue<node*> q;
q.push(head);
while (!q.empty()) {
qSize = q.size();
for (int i = 0; i < qSize; i++) {
node* currNode = q.front();
q.pop();
doStuff(currNode);
q.push(currNode);
}
}
The processing function doStuff() is quite expensive and I want to parallelize it. However if I parallelize the for loop by putting #pragma omp parallel for
right before the for line, all kinds of weird error pop up at runtime. I'm guessing the reason is that this way q.front()
and q.push()
will also get parallelized, and multiple threads will possibly get the same node through q.front()
(because they all got processed before any q.push
has been processed).
How can I get around this?
See Question&Answers more detail:os