-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_brute_force.cpp
More file actions
76 lines (61 loc) · 1.56 KB
/
parallel_brute_force.cpp
File metadata and controls
76 lines (61 loc) · 1.56 KB
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
#include<bits/stdc++.h>
#include<omp.h>
#define NUM_THREADS 4
using namespace std;
vector<int> v;
int main() {
ifstream fin;
string a, t;
fin.open("1.txt");
getline(fin, t);
fin.close();
cin>>a;
int n = t.size(), m = a.size();
int tid, start, end;
int partSize = n / NUM_THREADS;
int remainder = n % NUM_THREADS;
omp_set_num_threads(NUM_THREADS);
double ts = omp_get_wtime();
#pragma omp parallel num_threads(NUM_THREADS) private(tid,start,end) shared(t,a,remainder,partSize,m)
{
tid=omp_get_thread_num();
long long j;
if(tid == 0) {
start = tid;
end = partSize - 1;
} else {
start = (tid * partSize) - m;
end = (tid * partSize) + partSize - 1;
}
for(long long i=start;i<=end-m;i++) {
for(j=0;j<m;j++)
if(t[i+j] != a[j])
break;
if(j == m) {
#pragma omp critical
v.push_back(i);
}
}
}
if(remainder != 0) {
long long j;
start = (NUM_THREADS+1) * partSize;
end = n;
for(long long i=start;i<=end-m;i++) {
for(j=0;j<m;j++)
if(t[i+j] != a[j])
break;
if(j == m) {
#pragma omp critical
v.push_back(i);
}
}
}
double te = omp_get_wtime();
for(auto it : v) {
cout<<it<<" ";
}
cout<<"\n";
cout<<"Time: "<<te-ts<<"\n";
return 0;
}