-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinator.c
More file actions
191 lines (166 loc) · 8.4 KB
/
coordinator.c
File metadata and controls
191 lines (166 loc) · 8.4 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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
#include <unistd.h>
// Message type definitions for MPI communication
#define MSG_CONNECT 1
#define MSG_ACK 3
#define MSG_START_LE 10
#define MSG_LE_LIBR_DONE 14
#define MSG_START_LE_LOANERS 20
#define MSG_LE_LOANERS_DONE 23
#define MSG_TAKE_BOOK 30
#define MSG_DONATE_BOOK 40
#define MSG_DONATE_BOOKS_DONE 38
#define MSG_GET_MOST_POPULAR_BOOK 42
#define MSG_MOST_POPULAR_RESULT 47
#define MSG_GET_MOST_POPULAR_BOOK_DONE 45
#define MSG_CHECK_NUM_BOOKS_LOAN 46
#define MSG_BOOKS_LOANED_RESULT 48
#define MSG_CHECK_NUM_BOOKS_LOAN_DONE 51
#define MSG_DONE_FIND_BOOK 36
#define MSG_SHUTDOWN 99
int library_leader = -1; // Rank of the library leader process
int loaner_leader = -1; // Rank of the loaner leader process
// Main coordinator logic
void run_coordinator(int world_size) {
FILE *fp = fopen("testfile.txt", "r");
if (!fp) {
perror("Failed to open testfile.txt");
MPI_Abort(MPI_COMM_WORLD, 1);
}
// Calculate N based on the number of processes
int N = -1;
for (int n = 1; n <= 100; ++n) {
if (n * n + (n * n * n) / 2 + 1 == world_size) {
N = n;
break;
}
}
if (N == -1) {
fprintf(stderr, "Invalid number of processes\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
// Calculate ranks for libraries and clients
int first_library = 1;
int last_library = N * N;
int first_client = N * N + 1;
int last_client = N * N + (N * N * N) / 2;
char line[256];
char cmd[64];
int a, b, c;
// Read and process each command from the file
while (fgets(line, sizeof(line), fp) != NULL) {
if (strlen(line) <= 1) continue;
if (sscanf(line, "%s", cmd) != 1) continue;
// Handle CONNECT command between clients
if (strcmp(cmd, "CONNECT") == 0) {
if (sscanf(line, "%s %d %d", cmd, &a, &b) == 3) {
if (a >= first_client && a <= last_client && b >= first_client && b <= last_client) {
int msg[2] = {MSG_CONNECT, b};
MPI_Send(msg, 2, MPI_INT, a, 0, MPI_COMM_WORLD);
int ack;
MPI_Recv(&ack, 1, MPI_INT, a, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
// Start leader election for libraries
} else if (strcmp(cmd, "START_LE_LIBR") == 0) {
int msg = MSG_START_LE;
for (int i = first_library; i <= last_library; ++i) {
MPI_Send(&msg, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
MPI_Recv(&library_leader, 1, MPI_INT, MPI_ANY_SOURCE, MSG_LE_LIBR_DONE,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Start leader election for loaners
} else if (strcmp(cmd, "START_LE_LOANERS") == 0) {
int msg = MSG_START_LE_LOANERS;
for (int i = first_client; i <= last_client; ++i) {
MPI_Send(&msg, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
MPI_Recv(&loaner_leader, 1, MPI_INT, MPI_ANY_SOURCE, MSG_LE_LOANERS_DONE,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Handle TAKE_BOOK command from a client
} else if (strcmp(cmd, "TAKE_BOOK") == 0 || strcmp(cmd, "takeBook") == 0) {
if (sscanf(line, "%s %d %d", cmd, &a, &b) == 3) {
if (a >= first_client && a <= last_client) {
int msg[2] = {MSG_TAKE_BOOK, b};
MPI_Send(msg, 2, MPI_INT, a, 0, MPI_COMM_WORLD);
int done_msg;
MPI_Status recv_status;
MPI_Recv(&done_msg, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &recv_status);
}
}
// Handle DONATE_BOOK command from a client
} else if (strcmp(cmd, "DONATE_BOOK") == 0 || strcmp(cmd, "donateBook") == 0) {
if (sscanf(line, "%s %d %d %d", cmd, &a, &b, &c) == 4) {
if (a >= first_client && a <= last_client) {
int msg[3] = {MSG_DONATE_BOOK, b, c};
MPI_Send(msg, 3, MPI_INT, a, 0, MPI_COMM_WORLD);
int done_msg;
MPI_Status recv_status;
MPI_Recv(&done_msg, 1, MPI_INT, MPI_ANY_SOURCE, MSG_DONATE_BOOKS_DONE,
MPI_COMM_WORLD, &recv_status);
}
}
// Handle GET_MOST_POPULAR_BOOK command
} else if (strcmp(cmd, "GET_MOST_POPULAR_BOOK") == 0 || strcmp(cmd, "getMostPopularBook") == 0) {
if (loaner_leader != -1) {
int msg = MSG_GET_MOST_POPULAR_BOOK;
MPI_Send(&msg, 1, MPI_INT, loaner_leader, 0, MPI_COMM_WORLD);
int result[2];
MPI_Status recv_status;
MPI_Recv(result, 2, MPI_INT, MPI_ANY_SOURCE, MSG_MOST_POPULAR_RESULT,
MPI_COMM_WORLD, &recv_status);
int done_msg;
MPI_Recv(&done_msg, 1, MPI_INT, MPI_ANY_SOURCE, MSG_GET_MOST_POPULAR_BOOK_DONE,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// Handle CHECK_NUM_BOOKS_LOANED command
} else if (strcmp(cmd, "CHECK_NUM_BOOKS_LOANED") == 0 || strcmp(cmd, "checkNumberOfBooksLoaned") == 0) {
if (loaner_leader != -1 && library_leader != -1) {
int msg = MSG_CHECK_NUM_BOOKS_LOAN;
MPI_Send(&msg, 1, MPI_INT, loaner_leader, 0, MPI_COMM_WORLD);
MPI_Send(&msg, 1, MPI_INT, library_leader, 0, MPI_COMM_WORLD);
long long client_loans = -1, library_loans = -1;
MPI_Status recv_status;
// Receive loan counts from both leaders
for (int i = 0; i < 2; i++) {
long long loans;
MPI_Recv(&loans, 1, MPI_LONG_LONG_INT, MPI_ANY_SOURCE, MSG_BOOKS_LOANED_RESULT,
MPI_COMM_WORLD, &recv_status);
if (recv_status.MPI_SOURCE == loaner_leader) {
client_loans = loans;
} else if (recv_status.MPI_SOURCE == library_leader) {
library_loans = loans;
}
}
// Wait for both done messages
int done_count = 0;
while (done_count < 2) {
int done_msg;
MPI_Recv(&done_msg, 1, MPI_INT, MPI_ANY_SOURCE, MSG_CHECK_NUM_BOOKS_LOAN_DONE,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
done_count++;
}
printf("Loaner Books %lld\n", client_loans);
printf("Library Books %lld\n", library_loans);
if (client_loans == library_loans && client_loans != -1) {
printf("CheckNumBooksLoaned SUCCESS\n");
} else {
printf("CheckNumBooksLoaned FAILED\n");
}
}
}
}
fclose(fp);
// Send shutdown signals to all libraries and clients
int shutdown_msg = MSG_SHUTDOWN;
for (int i = first_library; i <= last_library; ++i) {
MPI_Send(&shutdown_msg, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
for (int i = first_client; i <= last_client; ++i) {
MPI_Send(&shutdown_msg, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
}