-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (114 loc) · 3.68 KB
/
main.cpp
File metadata and controls
132 lines (114 loc) · 3.68 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
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <omp.h>
using namespace std;
static string schedulingMethod;
static int chunkSize;
static int M;
static int executionTime;
static int t;
// Calculates prime numbers in range [start, end].
int primeNumberGenerator(vector<int> &primesIn, vector<int> &primesOut, int start, int end) {
int realStart = start;
if (start <= 2) {
primesOut.push_back(2);
realStart = 1;
} else if (start % 2 == 0) {
realStart -= 1;
} else {
realStart -= 2;
}
int j;
int k;
int n;
int quo, rem;
n = realStart;
for (j = 1; n <= end; j++) {// P2
if (j != 1) primesOut.push_back(n);
bool check = true;
while (check) {//p4
n += 2;
for (k = 1; k < primesIn.size(); k++) {//p6
quo = n / primesIn.at(k);
rem = n % primesIn.at(k);
if (rem == 0) {
break;
}
if (quo <= primesIn.at(k)) {
check = false;
break;
}
}
if (k >= primesIn.size()) check = false;
}
}
}
// Calculates prime numbers up to end.
int primeNumberGenerator(vector<int> &primes, int end) {
primeNumberGenerator(primes, primes, 1, end);
}
int main(int argc, char *argv[]) {
if (argc < 5) {
cout << "All arguments are required: Scheduling method, chunk size, M, number of threads.";
return 0;
}
time_t begin_time;
time(&begin_time);
schedulingMethod = argv[1];
transform(schedulingMethod.begin(), schedulingMethod.end(), schedulingMethod.begin(), ::toupper);
chunkSize = stoi(argv[2]);
M = stoi(argv[3]);
t = stoi(argv[4]);
static bool print = false;
try {
string option = argv[5];
if (option.compare( "--print") == 0) print = true;
} catch (exception e) {
}
vector<int> prime;
int squareRootM = (int) sqrt(M);
primeNumberGenerator(prime, squareRootM);
omp_set_num_threads(t);
/*
* For the schedule kinds static, dynamic, and guided the chunk_size is set to the value of the second argument,
* or to the default chunk_size if the value of the second argument is less than 1; for the schedule kind auto the
* second argument has no meaning; for implementation specific schedule kinds, the values and associated meanings
* of the second argument are implementation defined. */
if (schedulingMethod.compare("STATIC")) {
omp_set_schedule(omp_sched_static, chunkSize);
} else if (schedulingMethod.compare("DYNAMIC")) {
omp_set_schedule(omp_sched_dynamic, chunkSize);
} else if (schedulingMethod.compare("GUIDED")) {
omp_set_schedule(omp_sched_guided, chunkSize);
} else if (schedulingMethod.compare("AUTO")) {
omp_set_schedule(omp_sched_auto, chunkSize);
}
vector<vector<int>> threads_prime;
#pragma omp parallel shared(threads_prime, M, squareRootM, prime)
{
vector<int> primesOut;
#pragma omp for schedule(runtime) nowait
for (int i = squareRootM + 1; i <= M; i += 2) {
primeNumberGenerator(prime, primesOut, i, i + 1);
}
#pragma omp critical
threads_prime.push_back(primesOut);
}
for (auto tmp : threads_prime) {
for (int j : tmp) {
prime.push_back(j);
}
}
time_t end_time;
time(&end_time);
executionTime = end_time - begin_time;
sort(prime.begin(), prime.end());
if (print) {
for (int j : prime)
printf("%d\n", j);
}
printf("Execution time: %d\n", executionTime);
return 0;
}