I'm implementing Boruvka's algorithm in C++ to find minimum spanning tree for a graph. This algorithm finds a minimum-weight edge for each supervertex (a supervertex is a connected component, it is simply a vertex in the first iteration) and adds them into the MST. Once an edge is added, we update the connected components and repeat the find-min-edge, and merge-supervertices process, until all the vertices in the graph are in one connected component.
Since find-min-edge for each supervertex can be done in parallel, I want to use OpenMP to do this. Here is the omp for
loop I would like to use for parallel find-min.
int index[NUM_VERTICES]; #pragma omp parallel private(nthreads, tid, index, min) shared(minedgeindex, setcount, forest, EV, umark) { #pragma omp for for(int k = 0; k < setcount; k++){ //iterate over supervertices, omp for here min = 9999; std::fill_n(index, NUM_VERTICES, -1); /* Gets minimum edge for each supervertex */ for(int i = 0; i < NUM_VERTICES; i++) { if(forest[i]->mark == umark[k]){ //find vertices with mark k for(int j = 0; j < NUM_EDGES; j++) { //check min edge for each vertex in the supervertex k if(EV[j].v1==i){ if(Find(forest[EV[j].v1])!= Find(forest[EV[j].v2])){ if(EV[j].w <= min ){ min = EV[j].w; index[k] = j; break; //break looping over edges for current vertex i, go to next vertex i+1 } } } } } } //end finding min disjoint-connecting edge for the supervertex with mark k if(index[k] != -1){ minedgeindex.insert(minedgeindex.begin(), index[k]); } } //omp for end }
Since I'm new to OpenMP, I currently cannot make it work as I expected.
Let me briefly explain what I'm doing in this block of code: setcount
is the number of supervertices. EV
is a vector containing all edges (Edge
is a struct I defined previously, has attributes v1, v2, w
which correspond to the two nodes it connects and its weight). minedgeindex
is a vector, I want each thread to find min edge for each connected component, and add the index (index j in EV
) of the min edge to vector minedgeindex
at the same time. So I think minedgeindex
should be shared. forest
is a struct for each vertex, it has a set mark umark
indicating which supervertex it's in. I use Union-Find
to mark all supervertices, but it is not relevant in this block of omp code.
The ultimate goal I need for this block of code is to give me the correct vector minedgeindex
containing all min edges for each supervertex.
To be more clear and ignore the graph background, I just have a large vector of numbers, I separate them into a bunch of sets, then I need some parallel threads to find the min for each set of numbers and give me back the indices for those mins, store them in a vector minedgeindex
.
If you need more clarification just ask me. Please help me make this work, I think the main issue is the declaration of private and shared variables which I don't know if I'm doing right.
Thank you in advance!