简体   繁体   中英

OpenMP C++ program with Vectors

I wrote a OpenMP program in C++ which basically finds the suffix-prefix overlap of a given length. All my strings are stored in a vector and I have two for loops for checking the overlap (all against all). I am trying to make the for loop parallel, but it does not improve the time. Following is my program

    vector<string> Reads; // contains all strings 
    vector<int> *AdjList = new vector<int>[Reads.size()];
    vector<int> *OLL = new vector<int>[Reads.size()];

     // int i,j;
    /*# pragma omp parallel \
      shared ( AdjList, OLL ) \
      private ( i, j )*/
    #pragma omp parallel for
    for(int i=0; i<Reads.size(); i++){
      string suff = Reads.at(i).substr(Reads.at(i).length() - minOLL, minOLL);
      for(int j=0; j<Reads.size(); j++){
        if(i != j){
          size_t found = rabin_karp(suff, Reads.at(j));
          if(found != -1){
            string pref1 = Reads.at(j).substr(0, found);
            string suff1 = Reads.at(i).substr(Reads.at(i).length() - minOLL - found, found);
            if(pref1.compare(suff1) == 0){
              AdjList[i].push_back(j);
              OLL[i].push_back(found + minOLL);
            }
          }
        }
      }
    }

I guess reduction might help, but I am clueless about how to use it

1.since size of strings may be different you may use schedule(dynamic) so the tasks dynamically assigned to threads. 2. you can split inner loop into two loops to get rid of if statement. 3. substr is not a good choice because leads to creation of new string so you may use and save character positions to speed the code. However below applied 1, 2 mentioned cases:

#pragma omp parallel for schedule(dynamic)
for(int i=0; i<Reads.size(); i++){
  string suff = Reads.at(i).substr(Reads.at(i).length() - minOLL, minOLL);
  for(int j=0; j< i; j++){
      size_t found = rabin_karp(suff, Reads.at(j));
      if(found != -1){
        string pref1 = Reads.at(j).substr(0, found);
        string suff1 = Reads.at(i).substr(Reads.at(i).length() - minOLL - found, found);
        if(pref1.compare(suff1) == 0){
          AdjList[i].push_back(j);
          OLL[i].push_back(found + minOLL);
        }
      }
  }
  for(int j=i+1; j< Reads.size(); j++){
      size_t found = rabin_karp(suff, Reads.at(j));
      if(found != -1){
        string pref1 = Reads.at(j).substr(0, found);
        string suff1 = Reads.at(i).substr(Reads.at(i).length() - minOLL - found, found);
        if(pref1.compare(suff1) == 0){
          AdjList[i].push_back(j);
          OLL[i].push_back(found + minOLL);
        }
      }
  }
}

Here's an example.

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <omp.h>

using namespace std;

class Monitorius {
private:
    int M[50];
    int count = 0;
    int suma = 0;
public:
    Monitorius()
    {
        for (int i = 0; i < 50; i++)
            M[i] = 0; // nusinulinamas masyvas
    }

    // Funkcijos ---------------------------------------

    void Prideti(int skaicius) 
    {
        #pragma omp critical
        {
            M[count] = skaicius;
            count++;
            Suma();
        }
    }

    void Suma() 
    {
        suma = 0;
        for (int i = 0; i < 50; i++) {
            suma += M[i];
        }
        cout << "Suma: " << suma << endl;
    }
};

void paleistiGijas(Monitorius *monitorius) {
    #pragma omp parallel num_threads(5)
    {
        for (int i = 1; i <= 10; i++) {
            monitorius->Prideti(i);
        }
    }
}

int main()
{
    // Monitoriaus sukurimas bei giju paleidimas
    Monitorius *monitorius = new Monitorius();

    // Pradedamas giju darbas
    paleistiGijas(monitorius);

    // Atlaisvinama atmintis
    delete(monitorius);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM