C++ OpenMP emergency

Hi,

I have an emergency as I have to submit a C++ code using OpenMP for the multithreaded part by sunday!

Description of task:

Via command line argument, we have to pass two files. the first .txt file is the sequence file, which contains an int (length of the sequence string) the second line contains the string itself.
The second .txt file to provide, is the pattern file. In there, the first line is int telling number of patterns inside file, and after that it's always the same: first int (length of pattern) then string with pattern itself.
There are 3 modes (passed through command line argument as argv[4]); mode=0 is the serial part, where the sequence has to be searched for the given patterns. The other two modes are done by multithreading. The number of threads is (argv[3]). mode = 1 indicates, that the sequence is split among the threads, so each thread is responsible for comparing its range of sequence with the different patterns.
Mode = 2 in turn, splits the patterns among the threads. So that each thread is responsible for 1 or more patterns and compares this to the whole string.
So, by now, I got the serial part working.
But I'm reaally struggling with the openMP part. SO maybe one of you guys sees the mistakes more quickly and could help me with some hints!

Here's the whole code, but the relevant part is after mode=1 and mode=2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <string.h>

using namespace std;

// Global variables
int no_threads;
pair <int, string> patPair;
vector <pair <int, string> > patVec;
pair <int, string> seqPair;
pair <int, int> contPair;
int mode;
int numberOfPatterns;
vector < pair < int, int> > occurences;

// Allow for passing through command line argument:

int main(int argc, char *argv[])
{

// Get Number of threads and mode via command line argument

int no_threads = atoi(argv[3]);
mode = atoi(argv[4]);

    // Handle Sequence String
    if (argv[1])
    {
    // Ifstream first File =(argv[1]
        ifstream ifile1(argv[1]);
        
        
        //Declare Variables
        string buffer;
        int slength;
        string sCharacters;
      
        
        // Process file and store values in seqPair
        while( getline(ifile1, buffer) )
        {
           stringstream strs(buffer);

         //Filling up pair
            getline(ifile1, buffer);
            strs >> slength;
            getline(ifile1, buffer);
            strs >> sCharacters;
            
            seqPair = make_pair (slength, sCharacters);
            
            cout << slength << endl;
            cout << sCharacters << endl;
        }
        
        ifile1.close();
  }

    // Handle Pattern String
    if (argv[2])
    {
        
        // Ifstream first File =(argv[1]
        ifstream ifile2(argv[2]);
        

        //Declare Variables
        string buffer;
        string temporary;
        int plength;
        string pCharacters;
        
        // Get first line and store in variable number of Patterns
        
        getline(ifile2, temporary);
        numberOfPatterns = atoi(temporary.c_str());
        cout << "The number of Patterns is " << numberOfPatterns << endl;
        
        
        // Process rest of file, first is int length of pattern, second is string containing pattern itself
        
        for (int i=0; i <= numberOfPatterns - 1; i++)
        {
        
            // Fill up the Vector patVec with pairs patPair
            getline(ifile2, buffer);
            stringstream int_reader(buffer);
            int_reader >> plength;
            
            getline(ifile2, buffer);
            stringstream str_reader(buffer);
            str_reader >> pCharacters;
        
        
            patPair = make_pair (plength, pCharacters);
            patVec.push_back(patPair);
            
            cout << "length of pattern " << i << " is " << plength << endl;
           
        }
        ifile2.close();
  }


    //If mode is 0, serial pattern matching

   if (mode == 0)
    {
    int p;
    int i;
        
        for (patVec[p=0]; p < patVec.size(); p++)
        {
            string pchar = patVec[p].second;
            string schar = seqPair.second;
            
            cout << "hier" << endl;
 
            for (schar.at(i=0); i <= seqPair.first - patVec[p].first; i++)
    
            {
                stringstream range;
                int x = i;
                
                //Here for example if i=0, it processes i from 0 untill "3 (pattern length) - 1" = 2;
              for (schar.at(x); x <= i + patVec[p].first - 1 ; x++)
                

                {  //stringstream range;
                    string buffer;
                    range << schar.at(x);
                }
                
                
                if (range.str() == pchar)
                {
               
                // Store the the number of pattern and the starting point in the sequence (second) using pairs contPair
                    
                    int pattern;
                    int startingPoint;
                    pattern = p;
                    startingPoint = i;
                    
                    contPair = make_pair (pattern, startingPoint);
                    occurences.push_back(contPair);
                    
                }
               else
                {
             }
               
            
         }
 
    }
   
//If mode is 1, parallel matching is executed, whereby each thread has range in sequence and looks for occurences in all patterns
    if (mode == 1)
        
    {
        int p;
        int i;
        
        for (patVec[p=0]; p < patVec.size(); p++)
        {
            string pchar = patVec[p].second;
            string schar = seqPair.second;
            
            // Here starts the parallel region
        
       #pragma omp num_thread(no_threads) parallel for shared(seqPair, patVec, occurences) private(i)        
            {
            
            for (schar.at(i=0); i <= seqPair.first - patVec[p].first; i++)
            {
            stringstream range;
            int x = i;
                    
            //Here for example if i=0, it processes i from 0 untill "3 (pattern length) - 1" = 2;
                for (schar.at(x); x <= i + patVec[p].first - 1 ; x++)
                        
                {
                string buffer;
                range << schar.at(x);
                }
                
                if (range.str() == pchar)
                {
        // Store the the number of pattern and the starting point in the sequence
                        
        
                    //Filling up pair
                    int pattern;
                    int startingPoint;
                    pattern = p;
                    startingPoint = i;
                    
                    contPair = make_pair (pattern, startingPoint);
                    occurences.push_back(contPair);
                }
                
                else
                {
            
                }
                    
                    
            }
                
                
         }
            
     }
 }

//If mode is 2, parallel matching is executed, whereby each thread looks at the whole string but has its own pattern to look for occurences
    
    
    //
    if (mode == 2)
    {
        int p;
        int i;
        
        // Here starts parallel region
        
   #pragma omp num_thread(no_threads) parallel for shared(seqPair, patVec, occurences) private(p)
 for (patVec[p=0]; p < patVec.size(); p++)
        {
            string pchar = patVec[p].second;
            string schar = seqPair.second;
   
            for (schar.at(i=0); i <= seqPair.first - patVec[p].first; i++)
            {
                stringstream range;
                int x = i;
                
                for (schar.at(x); x <= i + patVec[p].first - 1 ; x++)
                    
                {  //stringstream range;
                    string buffer;
                    range << schar.at(x);
                }
                if (range.str() == pchar)
                {
                // Store the the number of pattern and the starting point in the sequence
                    
                    int pattern;
                    int startingPoint;
                    pattern = p;
                    startingPoint = i;
                    
                    contPair = make_pair (pattern, startingPoint);
                    occurences.push_back(contPair);
                }
                else
                {
      
             }      
        }
    }
 }


Thanks a lot in advance! Appreciate every hint or idea!
Topic archived. No new replies allowed.