1

我正在尝试与 GCC 4.4.7 版本并行运行代码。我使用了 OpenMP 库。我有一个由所有线程共享的只读变量(指向类的指针)。

代码编译执行没有任何错误,但是我在串行序列中运行相同的代码时没有得到相同的结果(串行模式下的结果为真)。代码如下所示:

#include <class1>
#include <class2>
int main(){
    string a;
    int b1,b2,d;
    class1 c1(a,b1);
    c1.compute(d);
    int n_thread = 10; 
    int i,n=10;
    vector<vector<int> > res(n);
    omp_set_dynamic(0);            
    omp_set_num_threads(n_thread);   

    #pragma omp parallel for num_threads(n_thread) private(i)  shared(res) 
    for(i=0;i<n;i++)
    {
        class1 c(c1.tab,b2);
        c.compute(d);

        class2 toto(b1,b2);
        toto.getvect(c1.tab,c.tab);//Inside toto, the c1.tab is read-only 

        #pragma omp critical
        {
            res[i] = vector<int> (toto.p);
        }
    }
//the rest of the program when I use the c1 var and the res matrix.
}

我的第一个想法是问题出在两个引用变量ctoto上,但是这两个变量是在每个线程中创建的,因此它们对每个线程都是私有的。我尝试将c1用作threadprivate,但出现编译错误。如果c1被声明为共享,则输出结果没有变化。也许问题出在同时对 smae 变量的多次访问?我该如何解决这个问题?

4

0 回答 0