2016-11-16 46 views
0

我們如何使用openmp標誌一次使用所有處理器運行下面的代碼?如何使用OpenMP標誌將其作爲並行處理?

如果我在使用循環作爲循環for(;!xml.atEnd();)它顯示錯誤轉換:

need to initialisation and increment/decrements

//Need parallel processing for this code. 
while (!xml.atEnd()) { 
     // cerr <<"while loop"; 
     xml.readNext(); 
     if (xml.isStartElement()) { 
      currentXmlElement = xml.name(); 


      if (xml.name() == "sample") { 
       QString fname = xml.attributes().value("filename").toString(); 
       QString sname = xml.attributes().value("name").toString(); 
       QString setname = xml.attributes().value("setName").toString(); 
       QString sampleOrder = xml.attributes().value("sampleOrder").toString(); 
       QString isSelected = xml.attributes().value("isSelected").toString(); 
       //_mainwindow->setStatusText(tr("Loading sample: %1").arg(sname)); 
       //_mainwindow->setProgressBar(tr("Loading Sample Number %1").arg(++currentSampleCount),currentSampleCount,currentSampleCount+1); 

       bool checkLoaded=false; 
       Q_FOREACH(mzSample* loadedFile, _mainwindow->getSamples()) { 
        if (QString(loadedFile->fileName.c_str())== fname) checkLoaded=true; 
       } 

       if(checkLoaded == true) continue; // skip files that have been loaded already 
       // #pragma omp critical { 
       qDebug() << "Checking:" << fname; 
       QFileInfo sampleFile(fname); 

       if (!sampleFile.exists()) { 
        Q_FOREACH(QString path, pathlist) { 
         fname= path + QDir::separator() + sampleFile.fileName(); 
         qDebug() << "Checking if exists:" << fname; 
         if (sampleFile.exists()) break; 
        } 
       } 

       if (!fname.isEmpty()) { 
        // mzFileIO* fileLoader = new mzFileIO(this); 
        // fileLoader->setMainWindow(_mainwindow); 
        // mzSample* sample = fileLoader->loadSample(fname); 
        // delete(fileLoader); 

        mzSample* sample = _mainwindow->fileLoader->loadSample(fname); 
        if (sample) { 
         _mainwindow->addSample(sample); 
         currentSample=sample; 
         if (!sname.isEmpty())  sample->sampleName = sname.toStdString(); 
         if (!setname.isEmpty())   sample->setSetName(setname.toStdString()); 
         if (!sampleOrder.isEmpty())  sample->setSampleOrder(sampleOrder.toInt()); 
         if (!isSelected.isEmpty())  sample->isSelected = isSelected.toInt(); 
        } else { 
         currentSample=NULL; 
        } 
       } 
      } 

    //change sample color 
      if (xml.name() == "color" && currentSample) { 
       currentSample->color[0] = xml.attributes().value("red").toString().toDouble(); 
       currentSample->color[1] = xml.attributes().value("blue").toString().toDouble(); 
       currentSample->color[2] = xml.attributes().value("green").toString().toDouble(); 
       currentSample->color[3] = xml.attributes().value("alpha").toString().toDouble(); 
      } 

    //polynomialAlignmentTransformation vector 
      if (xml.name() == "polynomialAlignmentTransformation" && currentSample) { 
     vector<double>transform; 
     Q_FOREACH(QXmlStreamAttribute coef, xml.attributes()) { 
     double coefValue =coef.value().toString().toDouble(); 
     transform.push_back(coefValue); 
     } 
     qDebug() << "polynomialAlignmentTransformation: "; printF(transform); 
     currentSample->polynomialAlignmentTransformation = transform; 
     currentSample->saveOriginalRetentionTimes(); 
     currentSample->applyPolynomialTransform(); 
    } 
     } 
     if (xml.isCharacters() && currentXmlElement == "projectDescription") { 
      projectDescription.append(xml.text()); 
     } 
    } 
+0

縮進縮進 –

回答

0

OpenMP的並沒有真正處理任意對象。另外,OpenMP模型在這裏不適合你。 OpenMP的基本思想是將for循環的單個迭代遺留給不同的線程。這對於讀取XML數據並不奏效,XML數據本質上必須連續讀取以保持排序。

在你的情況,假設你可以使用OpenMP,並行化外部循環。假設有8個OpenMP線程正在運行。每個將執行xml.readNext();行。我幾乎可以保證readNext函數不是線程安全的,這會給你未定義的結果。即使它是線程安全的,它也必須以原子方式讀取一個完整的項目(不知道它在讀什麼,因爲我不知道xml的類型),這將使您的代碼大部分是串行的。