-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute.c
More file actions
385 lines (330 loc) · 8.34 KB
/
compute.c
File metadata and controls
385 lines (330 loc) · 8.34 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// Created by Tommy O'Brien on October 30, 2019
//
#include "compute.h"
int numJobsSent = 0;
int numJobsRec = 0;
int numThreads = 0;
int workCount = 0;
int main(int argc, char *argv[])
{
signal(SIGINT, CtrlC); //set interrupt
if( ! ((argc == 3) || (argc == 2)) )
{
fprintf(stderr, "USAGE ERROR: ./compute <Num of Threads> OR ./compute <Num of Threads> -n\n");
Goodbye();
}
int msgid;
int n;
int rcPthread = 0;
//Creating message queue or at least getting the id of an already created message queue
key_t key = ftok("ttobrien", 11);//need file "ttobrien"
if(key == -1)
{
fprintf(stderr, "ERROR: Key not produced\n");
Goodbye();
}
msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
if(msgid == -1)
{
msgid = msgget(key, 0);
}
if(msgid == -1)
{
fprintf(stderr, "ERROR: Message queue id not produced\n");
Goodbye();
}
if(argc == 3)
{
checkArg3(argv[2]);
n = 1;
}
else
{
n = 0;
}
numThreads = GetNumOfThreads(argv[1]);
tpool_t* tm;
tm = tpool_create(numThreads);
ComArgs comInfo;
comInfo.mqID = &msgid;
comInfo.nFlag = &n;
//compute will run indefinitely as a service until terminated or encountering a fatal error
while(1)
{
//Synchronized with DotProduct
rcPthread = pthread_mutex_lock(&workControl);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: workControl locking failed\n");
Goodbye();
}
//limiting amount of work to prevent an out of control population of the work queue
while(workCount == numThreads)
{
rcPthread = pthread_cond_wait(&empty, &workControl);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: cond_wait failed\n");
Goodbye();
}
}
tpool_add_work(tm, DotProduct, &comInfo);
workCount++;
rcPthread = pthread_mutex_unlock(&workControl);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: workControl unlocking failed\n");
Goodbye();
}
}
return 0;
}
void DotProduct(void* param)
{
int id = 0, row = 0, col = 0, inner = 0, dp = 0;
int n = 0, msgid = 0;
int rc1 = 0, rc2 = 0, rcPthread = 0;
Entry sendBack;
Msg message;
ComArgs* comArgs = (ComArgs*) param;
n = *(comArgs->nFlag);
msgid = *(comArgs->mqID);
//calls of msgrcv and incrementing of numJobsRec are synchronized
rcPthread = pthread_mutex_lock(&lock4);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: lock4 locking failed\n");
Goodbye();
}
rc1 = msgrcv(msgid, &message, 104 * sizeof(int), 1, 0);
if(rc1 == -1)
{
printf("ERROR: Message not recieved\n");
Goodbye();
}
numJobsRec++;
pthread_cond_broadcast(&cond);
printf("Receiving job id %d type %ld size %ld\n", message.jobid, message.type, (4 + 2 * message.innerDim) * sizeof(int));
rcPthread = pthread_mutex_unlock(&lock4);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: lock4 unlocking failed\n");
Goodbye();
}
id = message.jobid;
row = message.rowvec;
col = message.colvec;
inner = message.innerDim;
for(int i = 0; i < inner; i++)
{
dp = dp + message.data[i] * message.data[inner + i];
}
sendBack.dotProduct = dp;
sendBack.type = 2;
sendBack.jobid = id;
sendBack.rowvec = row;
sendBack.colvec = col;
if (n == 1)//if switch is activated nothing is put back on the message queue but dot product information is shown
{
printf("Sum for cell %d,%d is %d\n", row, col, dp);
}
else
{
//calls of msgsnd and incrementing of numJobsSent are syncronized
rcPthread = pthread_mutex_lock(&lock3);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: lock3 locking failed\n");
Goodbye();
}
struct msqid_ds ds;
msgctl(msgid, IPC_STAT, &ds);
int sizeOfMessage = 4 * sizeof(int);
//ensure the byte limit on the message queue will not be surpassed
while((sizeOfMessage + ds.__msg_cbytes) > ds.msg_qbytes)
{
pthread_cond_wait(&cond, &lock3);
msgctl(msgid, IPC_STAT, &ds);
}
rc2 = msgsnd(msgid, &sendBack, 4 * sizeof(int), 0);
if(rc2 == -1)
{
printf("ERROR: Message not sent\n");
}
numJobsSent++;
printf("Sending job id %d type %ld size %ld (rc=%d)\n", id, sendBack.type, 4 * sizeof(int), rc2);
rcPthread = pthread_mutex_unlock(&lock3);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: lock3 unlocking failed\n");
Goodbye();
}
}
//Now that this work is done, another can start. Synchronized with main.
rcPthread = pthread_mutex_lock(&workControl);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: workControl locking failed\n");
Goodbye();
}
workCount--;
rcPthread = pthread_cond_signal(&empty);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: cond_signal failed\n");
Goodbye();
}
rcPthread = pthread_mutex_unlock(&workControl);
if(rcPthread == -1)
{
fprintf(stderr, "ERROR: workControl unlocking failed\n");
Goodbye();
}
return;
}
void CtrlC(int sig_num)
{
signal(SIGINT, CtrlC); //reset interrupt
printf("Jobs Sent %d Jobs Received %d\n", numJobsSent, numJobsRec);
fflush(stdout);
}
void Goodbye()
{
fprintf(stderr, "Program terminating\n");
exit(0);
}
int GetNumOfThreads(char* arg2)
{
int num;
int len = strlen(arg2);
for(int i = 0; i < len; i++)
{
if(! isdigit(arg2[i]))
{
fprintf(stderr, "ERROR: Argument 2 should be an integer\n");
Goodbye();
}
}
sscanf(arg2, "%d", &num);//coverting c string to int if safe
return num;
}
void checkArg3(char* arg3)
{
if(strlen(arg3) != 2)
{
fprintf(stderr, "ERROR: Argument 3 should be \"-n\"\n");
Goodbye();
}
else
{
if((arg3[0] != '-') || (arg3[1] != 'n'))
{
fprintf(stderr, "ERROR: Argument 3 should be \"-n\"\n");
Goodbye();
}
}
return;
}
static tpool_work_t *tpool_work_create(thread_func_t func, void *arg)
{
tpool_work_t *work;
if (func == NULL)
return NULL;
work = (tpool_work_t*) malloc(sizeof(tpool_work_t));
work->func = func;
work->arg = arg;
work->next = NULL;
return work;
}
static void tpool_work_destroy(tpool_work_t *work)
{
if (work == NULL)
return;
free(work);
}
static tpool_work_t *tpool_work_get(tpool_t *tm)
{
tpool_work_t *work;
if (tm == NULL)
return NULL;
work = tm->work_first;
if (work == NULL)
return NULL;
if (work->next == NULL) {
tm->work_first = NULL;
tm->work_last = NULL;
} else {
tm->work_first = work->next;
}
return work;
}
static void *tpool_worker(void *arg)
{
tpool_t *tm = arg;
tpool_work_t *work;
while (1) {
pthread_mutex_lock(&(tm->work_mutex));
if (tm->stop)
break;
while (tm->work_first == NULL)
pthread_cond_wait(&(tm->work_cond), &(tm->work_mutex));
work = tpool_work_get(tm);
tm->working_cnt++;
pthread_mutex_unlock(&(tm->work_mutex));
if (work != NULL) {
work->func(work->arg);
tpool_work_destroy(work);
}
pthread_mutex_lock(&(tm->work_mutex));
tm->working_cnt--;
if (!tm->stop && tm->working_cnt == 0 && tm->work_first == NULL)
pthread_cond_signal(&(tm->working_cond));
pthread_mutex_unlock(&(tm->work_mutex));
}
tm->thread_cnt--;
pthread_cond_signal(&(tm->working_cond));
pthread_mutex_unlock(&(tm->work_mutex));
return NULL;
}
tpool_t *tpool_create(size_t num)
{
tpool_t *tm;
pthread_t thread;
size_t i;
if (num == 0)
num = 2;
tm = calloc(1, sizeof(*tm));
tm->thread_cnt = num;
pthread_mutex_init(&(tm->work_mutex), NULL);
pthread_cond_init(&(tm->work_cond), NULL);
pthread_cond_init(&(tm->working_cond), NULL);
tm->work_first = NULL;
tm->work_last = NULL;
for (i=0; i<num; i++) {
pthread_create(&thread, NULL, tpool_worker, tm);
pthread_detach(thread);
}
tm->working_cnt = 0;
return tm;
}
bool tpool_add_work(tpool_t *tm, thread_func_t func, void *arg)
{
tpool_work_t *work;
if (tm == NULL)
return false;
work = tpool_work_create(func, arg);
if (work == NULL)
return false;
pthread_mutex_lock(&(tm->work_mutex));
if (tm->work_first == NULL) {
tm->work_first = work;
tm->work_last = tm->work_first;
} else {
tm->work_last->next = work;
tm->work_last = work;
}
pthread_cond_broadcast(&(tm->work_cond));
pthread_mutex_unlock(&(tm->work_mutex));
return true;
}