如何使用openMP使这段代码线程安全?

fpg*_*ost 1 c++ pragma openmp thread-safety

下面显示的示意图代码可以正常工作,如果我删除 #pragma omp parallel for,但有了这个代码编译,但然后执行二进制文件我得到像*** glibc detected *** ./testBin: double free or corruption (!prev): 0x0c43d8d8 ***和的错误core dumped.我猜的原因是多个线程试图写入变量omega, ell, ....lineVec.我该如何解决?有没有办法告诉它变量是共享的?或者通常只有另一种方法可以并行执行此循环.我对`openmp完全不熟悉,这是我第一次使用它.

#include <omp.h>


int main( int argc , char **argv )
{ 
 vector <vector<string>> fileVec;
 //some code that reads in a CSV file lines into elements of fileVec

//variables constituting a line:
//my_float has been typedef to be a high precision class in real code
my_float omega;
my_float ell;

my_float init1Real;
my_float init1Imag;
my_float dinit1Real;
my_float dinit1Imag;

my_float init2Real;
my_float init2Imag;
my_float dinit2Real;
my_float dinit2Imag;

#pragma omp parallel for private(lineVec,fileVec,ell,omega,init1Real,init1Imag,dinit1Real,dinit1Imag,init2Real,init2Imag,dinit2Real,dinit2Imag)
 for (size_t i=0; i< fileVec.size(); i++) 
    { 

        lineVec=fileVec[i];


         ell=lineVec[0];
         omega=lineVec[1];

         init1Real=lineVec[2];
         init1Imag=lineVec[3];
         dinit1Real=lineVec[4];
         dinit1Imag=lineVec[5];
         init2Real=lineVec[6];
         init2Imag=lineVec[7];
         dinit2Real=lineVec[8];
         dinit2Imag=lineVec[9];


        // cout<<"OUTPUT ell=" << ell<< " omega=" << omega <<" init1Real="<<init1Real<<endl;

         //do some other calc involving these variables

    }     
   }
Run Code Online (Sandbox Code Playgroud)

Hri*_*iev 5

从共享中读取fileVec 线程安全的.只my_float应该使用类型的变量,private甚至更好 - 在循环内声明:

int main(int argc, char **argv)
{
    vector<vector<string>> fileVec;

    //some code that reads in a CSV file lines into elements of fileVec

    #pragma omp parallel for private(lineVec)
    for (size_t i = 0; i < fileVec.size(); i++)
    {
        lineVec = fileVec[i];

        //my_float has been typedef to be a high precision class in real code

        my_float ell = lineVec[0];
        my_float omega = lineVec[1];

        my_float init1Real = lineVec[2];
        my_float init1Imag = lineVec[3];
        my_float dinit1Real = lineVec[4];
        my_float dinit1Imag = lineVec[5];
        my_float init2Real = lineVec[6];
        my_float init2Imag = lineVec[7];
        my_float dinit2Real = lineVec[8];
        my_float dinit2Imag = lineVec[9];

        cout << "OUTPUT ell=" << ell << " omega=" << omega
             << " init1Real=" << init1Real << endl;

        //do some other calc involving these variables
    }
}
Run Code Online (Sandbox Code Playgroud)

除非my_float不是线程安全的,否则我在这里看不到任何比赛//do some other calc involving these variables.

请注意,使用最近的OpenMP版本,您甚至可以使用迭代器来遍历向量,因为它提供了一个随机访问迭代器:

typedef vector<vector<string>>::const_iterator iterType;

#pragma omp parallel for private(lineVec)
for (iterType it = lineVec.begin(); it != lineVec.end(); it++)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)