-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS370Homework3.cpp
More file actions
245 lines (203 loc) · 7.31 KB
/
Copy pathCS370Homework3.cpp
File metadata and controls
245 lines (203 loc) · 7.31 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
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
//Byung Hun Lee
//CS370 Homework 3
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <vector>
struct range{
int start;
int end;
int value;
};
//function declaration
int calculateT (vector<int> newVector, int threadNum, range T[]);
int calculateM (vector<int> newVector, int threadNum, range M[]);
int calculateI (vector<int> newVector, int threadNum, range I[]);
int calculateF (vector<int> newVector, int threadNum, range F[]);
//main function
int main (int argc, char *argv[]){
//find number of threads, usually specified in command line
//assume we have enough thread to each thread only looks at 1 or 2 index
int threadCount = strtol(argv[1], nullptr, 10);
//ourVector will contain the integers
//since this is pseudocode we can just assume the vectors has some random numbers
vector <int> ourVector = {some numbers};
//Allocate 4 arrays that will store total, maximal, initial, and final range
range T[threadCount];
range M[threadCount];
range I[threadCount];
range F[threadCount];
/*Divide the vector until each thread holds a new array with 1 or 2 index
if array size is odd, last thread will hold 1 index while rest hold 2, if even all thread will hold 2 index
After dividing, calculate each new vector's T, M , I, F in parallel using nested threads(Assume nested thread is turned on)*/
if (ourVector.size()%2 == 0){
# pragma omp parallel num_threads(threadCount)
{
int threadNum = omp_get_thread_num();
vector <int> ourVector = {ourVector[threadNum*2], ourVector[threadNum*2+1]};
# pragma omp parallel for num_threads(4)
for (int i = 0; i < 4; i++){
if (i == 0)
calculateT (newVector, threadNum, T);
else if (i == 1)
calculateM (newVector, threadNum, M);
else if (i == 2)
calculateI (newVector, threadNum, I);
else
calculateF (newVector, threadNum, F);
}
}
}
else{
# pragma omp parallel num_threads(threadCount)
{
int threadNum = omp_get_thread_num();
if (omp_get_thread_num() != threadCount-1){
vector <int> ourVector = {ourVector[threadNum*2], ourVector[threadNumx2*1]};
}
else{
vector <int> ourVector = {ourVector[threadNum*2]};
}
# pragma omp parallel for num_threads(4)
for (int i = 0; i < 4; i++){
if (i == 0)
calculateT (newVector, threadNum, T);
else if (i == 1)
calculateM (newVector, threadNum, M);
else if (i == 2)
calculateI (newVector, threadNum, I);
else
calculateF (newVector, threadNum, F);
}
}
}
int loopCounter = 1;
//Loop until only 1 thread remains
//In each loop, find the new T, M, I, F from merging two
while (threadCount != 1){
# pragma omp parallel for num_threads(threadCount)
for (int i = 0; i < threadCount; i++){
int j = i*loopCounter;
//check for out of bound
if (j+loopCounter > T.size()){
continue;
}
//New T will always be addition of two T
T[j].value = T[j].value + T[j+loopCounter].value;
T[j].start = T[j].start;
T[j].end = T[j+loopCounter].end;
//New M is from M1, M2, or from combining F1 and I2
if (M[j].value < M[j+loopCounter].value){
M[j].value = M[j+loopCounter].value;
M[j].start = M[j+loopCounter].start;
M[j].end = M[j+loopCounter].end;
}
if (M[j].value < F[j].value + I[j+loopCounter].value){
M[j].value = F[j].value + I[j+loopCounter].value;
M[j].start = F[j].start;
M[j].end = I[j+loopCounter].end;
}
//New I is from I1, I2, or from combining T1 and I2
if (I[j].value < I[j+loopCounter].value){
I[j].value = I[j+loopCounter].value;
I[j].start = I[j+loopCounter].start;
I[j].end = I[j+loopCounter].end;
}
if (I[j].value < T[j].value + I[j+loopCounter].value){
I[j].value = T[j].value + I[j+loopCounter].value;
I[j].start = T[j].start;
I[j].end = I[j+loopCounter].end;
}
//New F is from F1, F2, or from combining F1 and T2
if (F[j].value < F[j+loopCounter].value){
F[j].value = F[j+loopCounter].value;
F[j].start = F[j+loopCounter].start;
F[j].end = F[j+loopCounter].end;
}
if (F[j].value < F[j].value + T[j+loopCounter].value){
F[j].value = F[j].value + T[j+loopCounter].value;
F[j].start = F[j].start;
F[j].end = T[j+loopCounter].end;
}
}
//variable loopCounter keeps track of which index the array to be merged starts at (index+1, index+2, index+4, etc.)
//variable threadCount keeps track of how many vectors still need to be merged (and how many threads needs to run next time)
loopCounter = loopCounter * 2;
threadCount = threadCount/2 + threadCount%1;
}
cout << "Max subarray value:" << M[0].value << endl;
cout << "Max subarray starting index:" << M[0].start << endl;
cout << "Max subarray ending index:" << M[0].end << endl;
//end program
return 0;
}
//method to calculate T
int calculateT (vector<int> newVector, int threadNum, range T[]){
# pragma omp parallel for num_threads(newVector.size()) reduction(+, T)
for (int i = 0; i < ourVector.size(); i++){
T[threadNum].value += ourVector[i];
}
T[threadNum].start = threadNumber *2;
T[threadNum].end = T[threadNum].start + ourVector.size()-1;
}
//method to calculate M
int calculateM (vector<int> newVector, int threadNum, range M[]){
int currentSum = 0;
bool restart = false;
int currentStart = 0;
M[threadNum].start = threadNumber *2;
M[threadNum].end = threadNumber*2;
for (int i = 0; i < ourVector.size(); i++){
//add current sum and current value at index
currentSum = currentSum + ourVector[i];
//if a value was previously skipped (checked by flag), update start
if (restart){
currentStart = i;
restart = false;
}
//if new sum exceed current sum, update the value, end, and start
if (M[threadNum].value < currentSum){
M[threadNum].value = currentSum;
M[threadNum].start = M[threadNum].start + currentStart;
M[threadNum].end = M[threadNum].end + i;
}
//if current sum is negative, place a flag
//flag is used to keep track of where new start index will be
if (currentSum < 0){
restart = true;
currentSum = 0;
}
}
}
//method to calculate I
int calculateI (vector<int> newVector, int threadNum, range I[]){
int currentSum = 0;
int currentStart = 0;
I[threadNum].start = threadNumber *2;
I[threadNum].end = threadNumber*2;
for (int i = 0; i < ourVector.size(); i++){
//add current sum and current value at index
currentSum = currentSum + ourVector[i];
//if new sum exceed current sum, update the value, end, and start
if (I[threadNum].value < currentSum){
I[threadNum].value = currentSum;
I[threadNum].end = I[threadNum].end + i;
}
}
}
//method to calculate F
int calculateF (vector<int> newVector, int threadNum, range F[]){
int currentSum = 0;
int currentStart = 0;
F[threadNum].start = threadNumber *2;;
F[threadNum].end = threadNumber*2;
for (int i = ourVector.size()-1; i > =0; i--){
//add current sum and current value at index
currentSum = currentSum + ourVector[i];
//if new sum exceed current sum, update the value, end, and start
if (F[threadNum].value < currentSum){
F[threadNum].value = currentSum;
F[threadNum].start = F[threadNum].end + i;
}
}
}