-
Notifications
You must be signed in to change notification settings - Fork 0
/
single.cpp
223 lines (190 loc) · 6.61 KB
/
single.cpp
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
#include <mpi.h>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <set>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#define INSIDE_TAG 101
#define REQUEST_TAG 102
#define AGREE_TAG 103
using namespace std;
// DECLARATIONS
struct State {
int rank, size;
bool ready;
int lamport;
mutex mtx;
condition_variable cv;
};
// HELPER FUNCTIONS
void randomize(int rank)
{
struct timeval tv;
gettimeofday(&tv, NULL);
// printf("%d: initing srand with seed %d\n", rank, tv.tv_usec + rank);
srand(tv.tv_usec + rank);
}
long microseconds()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (long) tv.tv_sec * (long) 1000000 + (long) tv.tv_usec;
}
void log(struct State *state, char *fmt, ...) {
char *str = NULL;
va_list args;
va_start(args, fmt);
vasprintf(&str, fmt, args);
va_end(args);
printf("%*d, %*d: %s\n", 4, state->lamport, 4, state->rank, str);
free(str);
}
// Thread routine responsible for MPI communication
void *mpi_thread(void *arg) {
struct State *state = (struct State *)arg;
int buf;
MPI::Status status;
set<int> queue;
bool inside = false;
while (1) {
MPI::COMM_WORLD.Recv(&buf, 1, MPI::INT, MPI::ANY_SOURCE, MPI::ANY_TAG, status);
state->lamport = max(state->lamport, buf) + 1;
switch (status.Get_tag()) {
case INSIDE_TAG: // enter/exit
if (!inside) {
for (int i = 0; i < state->size; i++) {
if (i != state->rank) {
MPI::COMM_WORLD.Send(&state->lamport, 1, MPI::INT, i, REQUEST_TAG);
}
}
int request_clock = state->lamport;
int replies_received = 0;
while (replies_received < state->size - 1) {
MPI::COMM_WORLD.Recv(&buf, 1, MPI::INT, MPI::ANY_SOURCE, MPI::ANY_TAG, status);
state->lamport = max(state->lamport, buf) + 1;
switch (status.Get_tag()) {
case REQUEST_TAG:
if (request_clock < buf || (buf == request_clock && state->rank < status.Get_source())) {
// current process has higher priority
queue.insert(status.Get_source());
} else {
// other process has higher priority
MPI::COMM_WORLD.Send(&state->lamport, 1, MPI::INT, status.Get_source(), AGREE_TAG);
}
break;
case AGREE_TAG:
if (buf > request_clock) {
replies_received++;
log(state, "comm: Agree %d received from %d", buf, status.Get_source());
}
break;
default:
log(state, "comm: Unknown message tag %d", status.Get_tag());
}
}
inside = true;
unique_lock<mutex> lck(state->mtx);
state->ready = true;
state->cv.notify_all();
lck.unlock();
} else {
// broadcast agree to all in queue
char *repr = (char *)malloc(1024);
*repr = '\0';
for (int p : queue) {
sprintf(repr + strlen(repr), "%d, ", p);
}
state->lamport++;
log(state, "comm: !!! LEFT, %s", repr);
free(repr);
for (int p : queue) {
MPI::COMM_WORLD.Send(&state->lamport, 1, MPI::INT, p, AGREE_TAG);
}
queue.clear();
inside = false;
}
break;
case REQUEST_TAG:
if (inside) {
queue.insert(status.Get_source());
} else {
MPI::COMM_WORLD.Send(&state->lamport, 1, MPI::INT, status.Get_source(), AGREE_TAG);
state->lamport++;
}
break;
case AGREE_TAG:
break;
default:
log(state, "comm: Unknown message tag %d", status.Get_tag());
}
}
}
// Main program loop and state machine
void mainloop(struct State *state)
{
int buf = 0;
while (1) {
int interval = rand() % 8;
log(state, "main: Outside sleep: %d", interval);
sleep(interval);
// notify_critical_section();
log(state, "main: Sending enter INSIDE");
MPI::COMM_WORLD.Send(&buf, 1, MPI::INT, state->rank, INSIDE_TAG);
// wait_for_enter_critical_section();
log(state, "main: Waiting for critical section...");
unique_lock<mutex> lck(state->mtx);
while (!state->ready) {
state->cv.wait(lck);
}
lck.unlock();
state->ready = false;
interval = rand() % 8;
log(state, "main: !!! ENTERED (sleep: %d)", interval);
sleep(interval);
// exit_critical_section();
// log(state, "main: Sending exit INSIDE");
MPI::COMM_WORLD.Send(&buf, 1, MPI::INT, state->rank, INSIDE_TAG);
}
}
// INITIALIZATION
int main(int argc, char **argv)
{
int thread_support_provided;
mutex mtx;
struct State state;
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &thread_support_provided);
state.ready = false;
state.rank = MPI::COMM_WORLD.Get_rank();
state.size = MPI::COMM_WORLD.Get_size();
state.lamport = 0;
randomize(state.rank);
if (state.rank == 0) {
printf("Thread support provided: ", thread_support_provided);
switch (thread_support_provided) {
case MPI_THREAD_SINGLE:
printf("single");
break;
case MPI_THREAD_FUNNELED:
printf("funneled");
break;
case MPI_THREAD_SERIALIZED:
printf("serialized");
break;
case MPI_THREAD_MULTIPLE:
printf("multiple");
break;
}
putchar('\n');
}
thread t = thread(mpi_thread, &state);
mainloop(&state);
t.join();
MPI::Finalize();
}