omp use in ubuntu c++
I'm trying to write a code with omp; it has a section like this (the Brandes's Betweeness Centrality algorithm):
int main(int argc, char* argv[]){
{//file reading section
(...)
}
A = new vector<int>[V];
int conteudo;
for(i=0;i<E;i++){
conteudo = grafo[i][0];
A[conteudo].push_back(grafo[i][1]);
A[grafo[i][1]].push_back(conteudo);
}
float *Cb = new float[V];
// CÓDIGO DE BRANDES
#pragma omp parallel default(none) shared(i,A,j,V) private(Cb)
{
float *Cb = new float[V];
float *sigma = new float[V];
int *d = new int[V];
int v, w;
for (i = 0; i<V; i++) {
Cb[i] = 0;
}
#pragma omp parallel for
for (int s = 0; s<V; s++) {
for (i = 0; i<V; i++) {
sigma[i] = 0;
d[i] = -1;
}
vector<int> *P;
vector<int> S;
vector<int> Q;
P = new vector<int>[V];
sigma[s] = 1;
d[s] = 0;
Q.push_back(s);
while(!Q.empty()){
v = Q[0];
Q.erase(Q.begin());
S.push_back(v);
for(j=0;j<A[v].size();j++){
w = A[v][j];
if (d[w]<0) {
Q.push_back(w);
d[w] = d[v]+1;
}
if (d[w] == (d[v] + 1)) {
sigma[w] = sigma[w] + sigma[v];
P[w].push_back(v);
}
}
}
float *delta = new float[V];
for (i = 0; i<V; i++) {
delta[i] = 0;
}
while(!S.empty()) {
w = S[S.size()-1];
S.pop_back();
for (i = 0; i<P[w].size(); i++){
v = P[w][i];
delta[v] = delta[v] + (sigma[v] / sigma[w])*(1 + delta[w]);
}
if(w != s) {
Cb[w] = (Cb[w] + delta[w]);
}
}
}
}
...
}
The sequential code runs fine; however, I want to parallelize the for loop above; but, in the last line
if(w != s) {
Cb[w] = (Cb[w] + delta[w]);
}
The Cb array must be private for each process, and, after all I want to make a reducion summing all the Cb's. But when I declare
#pragma omp parallel for private(Cb)
And compile
$ g++ BC_omp.cpp -fopenmp
$ ./a.out file.txt
I get the error:
segmentation fault (core dumped)
I think I do not understood how omp works: - The variables declared before the pragma are shared? - The variables declared with private are local to the process; but how can I reduce an array, in this case? (In the end, I must reduce all the Cb in al the process to a single process, and sum it's values.)
链接地址: http://www.djcxy.com/p/79220.html上一篇: 如何在openmp中为每个线程分配一个特定的工作以添加矩阵
下一篇: omp在ubuntu c ++中使用