-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.cpp
More file actions
266 lines (234 loc) · 6.91 KB
/
Copy pathcounter.cpp
File metadata and controls
266 lines (234 loc) · 6.91 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* counter.cpp
* program launches 'NUM_THREADS' and each increments the counter 'NUM_ITERATIONS' times
* the counter is synchronized using either the 'bar' or 'lock' argument
* keeps track of time taken to increment the counter to its total (of program execution)
* * printed to standard out in nanoseconds, then showing final counter value to file
*/
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <cstdlib>
#include <atomic>
#include <mutex>
#include <cmath>
#include <thread>
#include <fstream>
#include <time.h> // for timing execution (timespec), clock_t, clock()
#include "pthread_add.h" // for when running on macOS
#include "cnt_arg_parser.h" // help with parsing data
using namespace std;
#define SEQ_CST memory_order_seq_cst
#define RELAXED memory_order_relaxed
/* initialization */
size_t NUM_THREADS = 0;
int NUM_ITERATIONS = 0;
atomic<int> COUNTER (0);
int numberOver = 0;
pthread_barrier_t bar;
pthread_mutex_t mutexLock;
atomic<int> next_num (0), now_serving (0);
atomic<int> sense (0), cnt (0);
atomic<bool> tas_flag (0);
mutex b_lock; // pthread_mutex_t issues?
/* execution time struct and function */
typedef chrono::high_resolution_clock Clock;
/* bar functions */
void sense_wait();
void *counter_sense(void *);
void *counter_bar_pthread(void *);
/* lock functions */
bool tas();
void tas_lock();
void tas_unlock();
void *counter_TAS(void *);
void ttas_lock();
void *counter_TTAS(void *);
void ticket_lock();
void ticket_unlock();
void *counter_ticket_lock(void *);
void *counter_lock_pthread(void *);
/* *************** Main Function *************** */
int main(int argc, const char* argv[]){
struct arg_params args_parsed = arg_parser(argc, argv);
int argument = args_parsed.argument;
string outputFile = args_parsed.outputFile;
NUM_THREADS = args_parsed.NUM_THREADS;
NUM_ITERATIONS = args_parsed.NUM_ITERATIONS;
pthread_t threads[NUM_THREADS];
// printf("All the data we want:\n%i\n%i\n%i\n%s\n", NUM_THREADS, NUM_ITERATIONS, argument, outputFile.c_str());
// execution start time
auto start_time = Clock::now();
/* argument statement (from parser) is as follows:
* bar: 1-sense, 2-pthread
* lock: 3-tas, 4-ttas, 5-ticket, 6-pthread */
switch (argument) {
// bar sense
case 1:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, counter_sense, (void*)NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// bar pthread
case 2:
pthread_barrier_init(&bar, NULL, NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, counter_bar_pthread, (void*)NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
pthread_barrier_destroy(&bar);
break;
// lock tas
case 3:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, counter_TAS, (void*)NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// lock ttas
case 4:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, counter_TTAS, (void*)NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// lock ticket
case 5:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, counter_ticket_lock, (void*)NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// lock pthread
case 6:
pthread_mutex_init(&mutexLock, NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, counter_lock_pthread, (void*)NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
pthread_mutex_destroy(&mutexLock);
break;
// something didn't match up
default:
printf("An error occured in MAIN.");
exit(-1);
}
// execution end time
auto end_time = Clock::now();
// unsigned int 4,294,967,295, which is only 4.3 seconds
// unsigned long, plan on never running out (over 5 centuries)
unsigned long time_spent = chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
printf("Time elapsed is %lu nanoseconds\n", time_spent);
printf(" %f seconds\n", time_spent/1e9);
printf("Counter is: %u\n", unsigned(COUNTER));
// For when running long repeats, comment above and uncomment this
// if (COUNTER != NUM_ITERATIONS*NUM_THREADS)
// printf("Counter is: %u\n", unsigned(COUNTER));
// write to file
FILE * fp;
fp = fopen(outputFile.c_str(), "w");
if (fp == NULL) return -1;
fprintf(fp, "%u\n", unsigned(COUNTER));
fclose(fp);
return 0;
}
/* *************** BAR FUNCTIONS *************** */
/* ****** sense ****** */
void sense_wait() {
thread_local bool cur_sense=0;
if (cur_sense==0) cur_sense=1;
else cur_sense=0;
int cnt_copy = atomic_fetch_add(&cnt, 1);
if (cnt_copy == NUM_THREADS - 1) {
cnt.store(0, RELAXED);
sense.store(cur_sense, SEQ_CST);
} else while(sense.load(SEQ_CST) != cur_sense){};
}
void *counter_sense(void *) {
for(int i = 0; i < NUM_ITERATIONS; ++i) {
COUNTER++;
sense_wait();
}
return NULL;
}
/* ****** pthread ****** */
void *counter_bar_pthread(void *) {
for (int i = 0; i < NUM_ITERATIONS; i++) {
COUNTER++;
pthread_barrier_wait(&bar);
}
return NULL;
}
/* *************** LOCK FUNCTIONS *************** */
/* ****** tas ****** */
bool tas() {
if (tas_flag == false){
tas_flag = true;
return true;
} else return false;
}
void tas_lock() {
while(tas() == false) {}
}
void tas_unlock() {
tas_flag.store(false, SEQ_CST);
}
void *counter_TAS(void *) {
tas_lock();
for(int i = 0; i < NUM_ITERATIONS; ++i) {
COUNTER++;
tas_unlock();
tas_lock();
}
tas_unlock();
return NULL;
}
/* ****** ttas ****** */
void ttas_lock() {
while(tas_flag.load(SEQ_CST) == true
|| tas() == false) {}
}
void *counter_TTAS(void *) {
ttas_lock();
for(int i = 0; i < NUM_ITERATIONS; ++i) {
COUNTER++;
tas_unlock();
ttas_lock();
}
tas_unlock();
return NULL;
}
/* ****** ticket ****** */
void ticket_lock() {
// fai is fetch and increment, but not in C++?
int num = atomic_fetch_add(&next_num, 1);
while(now_serving.load(SEQ_CST) != num) {};
}
void ticket_unlock() {
// fai is fetch and increment, but not in C++?
atomic_fetch_add(&now_serving, 1);
}
void *counter_ticket_lock(void *) {
ticket_lock();
for(int i = 0; i < NUM_ITERATIONS; ++i) {
COUNTER++;
ticket_unlock();
ticket_lock();
}
ticket_unlock();
return NULL;
}
/* ****** pthread ****** */
void *counter_lock_pthread(void *) {
pthread_mutex_lock(&mutexLock);
for(int i = 0; i < NUM_ITERATIONS; i++) {
COUNTER++;
pthread_mutex_unlock(&mutexLock);
pthread_mutex_lock(&mutexLock);
}
pthread_mutex_unlock(&mutexLock);
return NULL;
}