-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
172 lines (141 loc) · 3.31 KB
/
Copy pathtest.cpp
File metadata and controls
172 lines (141 loc) · 3.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
/*
*
* Jessica Jassal
* Operatig Systems Homework 3
* Spring 2018
*
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
///////////////////// CREATE BARRIER ///////////////////////
class Barrier {
private:
int value, init;
sem_t mutex;
sem_t waitq;
sem_t throttle;
public:
Barrier(int in);
void wait(int newVal);
};
// BARRIER CONSTRUCTOR
Barrier::Barrier(int in){
init = in;
value = init;
sem_init(&mutex, 0, 1);
sem_init(&waitq, 0, 0);
sem_init(&throttle, 0, 0);
}
// WAIT() METHOD
void Barrier::wait(int newVal){
sem_wait(&mutex);
value--;
if(value != 0){ // NOT LAST THREAD
sem_post(&mutex);
sem_wait(&waitq);
sem_post(&throttle);
} else { // LAST THREAD
int i = 0;
for (i = 0; i < init-1; i++){ // RELEASE WAITING THREADS
sem_post(&waitq);
sem_wait(&throttle);
}
init = newVal;
value = init;
sem_post(&mutex);
}
}
///////////////////// GLOBAL VARS ///////////////////////
typedef struct{
float * array;
pthread_t tid;
int first;
int second;
int numThreads;
Barrier * bar;
}thread_arg;
int count = 0;
unsigned int numOfRounds = 0;
///////////////////// HELPER FUNCTION FOR ROUND NUMS ///////////////////////
unsigned int getLog2 (unsigned int n){
unsigned int logVal = 0;
--n;
while (n > 0) {
++logVal;
n >>= 1;
}
return logVal;
}
///////////////////// THREAD FUNCTION ///////////////////////
void *compare(void * arguments){
thread_arg * arg = (thread_arg *) arguments;
int i = 1;
for(i = 1; i <= numOfRounds; i++){
// DECREASE NUMBER OF THREADS TO USE
if((int)arg->tid < arg->numThreads){
// COMPARING
if (arg->array[arg->first] < arg->array[arg->second])
arg->array[arg->first] = arg->array[arg->second];
// BARRIER WAITING
arg->bar->wait(arg->numThreads/2);
// SETTING NEW ARG VALS
arg->first = arg->first*2;
arg->second = arg->second*2;
arg->numThreads = arg->numThreads/2;
}
}
}
int main(){
///////// READ IN INPUT //////////////
char buffer[5];
float * nums = new float[4*8193];
count = 0;
// REAN IN INPUTS
while (1){
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
break;
}
if (sscanf(buffer, "%f", (nums + (count))) != 0) {
count++;
} else {
break;
}
}
///////// SET UP AND CREATE THREADS //////////////
// DECLARE VARIABLES
int numOfThreads = count/2;
numOfRounds = getLog2(count);
pthread_t threadsArr[count/2];
thread_arg argsArr[numOfThreads];
Barrier * b = new Barrier(numOfThreads);
pthread_attr_t attr;
// SET ARGUMENT VALS AND CREATE THREADS
int i = 0;
int j = 0;
pthread_attr_init(&attr);
for (i=0; i < count-1; i+=2){
argsArr[j].array = nums;
argsArr[j].tid = j;
argsArr[j].first = i;
argsArr[j].second = i+1;
argsArr[j].numThreads = numOfThreads;
argsArr[j].bar = b;
// CREATE THREADS
pthread_create(&threadsArr[j], &attr, compare, &argsArr[j]);
j++;
}
// WAIT FOR THREADS TO TERMINATE
int l;
for (l=0; l < count/2; l++){
pthread_join(threadsArr[l], NULL);
}
// PRINT MAX VALUE
printf("%d \n", int(nums[0]));
free(nums);
free(b);
return 0;
}