-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
464 lines (320 loc) · 9.96 KB
/
main.c
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <omp.h>
#define WORK_PER_PROCESSOR 1000
#define PARALLEL 1
//compile with -> /usr/local/bin/gcc -fopenmp main.c -o test
int printCount = 0;
double processing_time = 0;
typedef struct node {
char class[50];
char element[50];
char id[50];
struct node *parent;
struct node *first_child;
struct node *right_sibling;
//parallel variable
int count;
int numberOfChild;
char *content;
//print variable
int printed;
} node;
void parseNode (struct node *node, struct node *parent, char *string) {
//Init flags
node->count = 0;
node->numberOfChild = 0;
node->printed = 0;
//Initialize pointer
node->parent = parent;
node->first_child = NULL;
node->right_sibling = NULL;
node->content = NULL;
switch (string[0]) {
case '#' :
strcpy(node->id, ++string);
strcpy(node->element, "div");
break;
case '.' :
strcpy(node->class, ++string);
strcpy(node->element, "div");
break;
default :
strcpy(node->element, string);
}
}
double elapse_time (struct timeval start) {
struct timeval end;
gettimeofday(&end, NULL);
return (end.tv_sec + (((double) end.tv_usec)/1e6)) - (start.tv_sec + (((double) start.tv_usec)/1e6));
}
void computeBeginningNode (char* result, node *current_node, int level, int threadID) {
char element[100];
int i = 0;
node *child = current_node->first_child;
struct timeval start;
result[0] = '\0';
for (i = 0; i < level; i++) {
strcat(result, "\t");
}
// printf("Computing <%s id=\"%s\" class=\"%s\">\n", current_node->element, current_node->id, current_node->class);
gettimeofday(&start, NULL);
sprintf(element, "<%s id=\"%s\" class=\"%s\">\n", current_node->element, current_node->id, current_node->class);
strcat(result, element);
if (threadID == 0) {
processing_time += elapse_time(start);
}
// Adding children content
while (child != NULL) {
strcat(result, child->content);
// free(child->content);
// printf("here\n");
child = child->right_sibling;
}
// printf("here\n");
for (i = 0; i < level; i++) {
strcat(result, "\t");
}
gettimeofday(&start, NULL);
sprintf(element, "</%s>\n", current_node->element);
strcat(result, element);
if (threadID == 0) {
processing_time += elapse_time(start);
}
}
void computeNodeToHtml (node *root, int level, char *buf, int threadID) {
char *parsedString = buf;
int i = 0;
node *current_node = root;
node *parent_node = current_node->parent;
node *child;
int length = 0;
struct timeval start;
root->parent = NULL;
do {
if (current_node->first_child != NULL && current_node->first_child->content == NULL) {
current_node = current_node->first_child;
level++;
}
else {
// make sure all children have content
child = current_node->first_child;
while (child != NULL) {
if (child->content == NULL) {
break;
}
else
child = child->right_sibling;
}
if (child != NULL)
current_node = child;
else { // all children have content
if (current_node->right_sibling != NULL && current_node != root) {
// compute current node before moving to its neighbor
computeBeginningNode(parsedString, current_node, level, threadID);
current_node->content = malloc(strlen(parsedString) + 1);
strcpy(current_node->content, parsedString);
// moving to the neighbor
current_node = current_node->right_sibling;
}
else if (current_node->parent != NULL) {
// compute current node before moving to its parent
computeBeginningNode(parsedString, current_node, level, threadID);
current_node->content = malloc(strlen(parsedString) + 1);
strcpy(current_node->content, parsedString);
current_node = current_node->parent;
level--;
}
}
}
} while (current_node->parent != NULL);
// compute the root
computeBeginningNode(parsedString, current_node, level, threadID);
current_node->content = malloc(strlen(parsedString) + 1);
strcpy(current_node->content, parsedString);
root->parent = parent_node;
// printf("------done with node computation----------\n");
}
void buildTree (struct node *root) {
FILE *ptr_file;
char buf[100];
ptr_file =fopen("input.hnrl", "r");
node *current_node = root;
while (fscanf(ptr_file, "%s", buf) !=EOF) {
node *new_node;
switch (buf[0]) {
case '{':
current_node->first_child = malloc(sizeof(node));
current_node->first_child->parent = current_node;
current_node = current_node->first_child;
break;
case '}':
current_node = current_node->parent;
if (buf[1] == ',')
{
current_node->right_sibling = malloc(sizeof(node));
current_node->right_sibling->parent = current_node->parent;
current_node = current_node->right_sibling;
}
break;
default :
new_node = malloc(sizeof(node));
parseNode(new_node, current_node->parent, buf);
memcpy(current_node, new_node, sizeof(node));
}
}
fclose(ptr_file);
}
void computeOutputInParallel (struct node *root) {
node *current_node = root;
int workByProcessor = WORK_PER_PROCESSOR;
int i,j, level = 0;
char parsedString[700000] = {};
struct timeval total;
char buf[500000] = {};
gettimeofday(&total, NULL);
while(1) {
// move down to the last child
if (current_node->first_child != NULL && current_node->first_child->count == 0) {
current_node = current_node->first_child;
level++;
} else {
if (current_node->count == 0) //means it is a leaf
current_node->count = 1;
current_node->parent->count += current_node->count;
current_node->parent->numberOfChild++;
// move to right sibling
if (current_node->right_sibling != NULL ) {
current_node = current_node->right_sibling;
}
// move up to parent
else {
current_node = current_node->parent;
level--;
// current node is now the parent
current_node->count++; //adding 1 for parent node
if (current_node->count >= workByProcessor || current_node->parent == NULL) {
// if (current_node->parent == NULL)
// printf("T-node %d\n", current_node->count);
//need to parallelize the tree
#pragma omp parallel num_threads(current_node->numberOfChild) shared(processing_time) private(buf, i)
{
node *current_child = current_node->first_child;
int threadID = omp_get_thread_num();
// selecting the right child for each process
for (i = 0; i < threadID; i++) {
current_child = current_child->right_sibling;
}
if (current_child != NULL && current_child->content == NULL) {
// Get thread number
computeNodeToHtml(current_child, level + 1, buf, threadID);
// printf("%s\n", current_child->content);
}
// }
}
// Compute parent node
computeBeginningNode(parsedString, current_node, level, 0);
current_node->content = malloc(strlen(parsedString) + 1);
strcpy(current_node->content, parsedString);
current_node->count = 1;
if (current_node->parent == NULL) {
break;
}
}
}
}
}
}
/*
*********SERIAL COMPUTATION FUNCTIONS***********
*/
void printBeginningNode (node *current_node, int level,FILE *file) {
int i;
// Indentation
for (i = 0; i < level; i++) {
fprintf(file, "\t");
}
//Node
fprintf(file,"<%s id=\"%s\" class=\"%s\">\n", current_node->element, current_node->id, current_node->class);
if (current_node->right_sibling == NULL && current_node->first_child == NULL && current_node->parent != NULL) {
for (i = 0; i < level; i++) {
fprintf(file, "\t");
}
fprintf(file,"</%s>\n",current_node->element);
}
}
void printEndNode (node *current_node, int level,FILE *file) {
int i;
// Indentation
for (i = 0; i < level; i++) {
fprintf(file, "\t");
}
fprintf(file,"</%s>\n", current_node->element);
}
void printTree (node *root, FILE *file) {
int level = 0, i = 0;
node *current_node = root, temp;
printBeginningNode(current_node, level,file);
do {
if (current_node->first_child != NULL) {
current_node = current_node->first_child;
printBeginningNode(current_node, ++level,file);
}
else if (current_node->right_sibling != NULL) {
current_node = current_node->right_sibling;
printBeginningNode(current_node, level,file);
}
else {
current_node = current_node->parent;
current_node->first_child = NULL;
level--;
printEndNode(current_node, level,file);
}
} while (current_node->parent != NULL);
}
int main(int argc, char **argv) {
FILE *output_p;
node *root = malloc(sizeof(node));
buildTree(root);
struct timeval total_start;
double communication_time = 0, write_time = 0, total_time = 0;
int parallel = PARALLEL;
if (parallel == 1) {
printf("\n-------------------------------------------\n");
printf("Parallel version with %d node per processor\n", WORK_PER_PROCESSOR);
printf("-------------------------------------------\n");
gettimeofday(&total_start, NULL);
computeOutputInParallel(root);
// Print content to a file
if (root->content != NULL) {
output_p = fopen("output_parallel.html", "w");
if (output_p != NULL) {
write_time = elapse_time(total_start);
communication_time = write_time - processing_time;
fputs(root->content, output_p);
fclose(output_p);
total_time = elapse_time(total_start);
printf("Total communication time = %f\n", communication_time);
printf("Total processing time = %f\n", processing_time);
printf("Write to file time = %f \n", total_time - write_time);
printf(" ------------\n");
printf("Total time %f\n\n", total_time);
}
else
printf("Unable to open the output file\n");
}
else
printf("Error: no content\n");
} else {
printf("\n----------------------\n");
printf("Running serial version\n");
printf("----------------------\n");
gettimeofday(&total_start, NULL);
output_p = fopen("output_serial.html", "w");
printTree(root,output_p);
printf("Total time: %f microseconds\n\n", elapse_time(total_start));
}
return 0;
};