-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial.c
177 lines (165 loc) · 6.91 KB
/
serial.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <omp.h>
#include "util/queue.h"
#include "util/hashTable.h"
#include "util/util.h"
extern int errno;
int DEBUG_MODE = 0;
int PRINT_MODE = 0;
int main(int argc, char **argv)
{
int NUM_THREADS = 1;
int HASH_SIZE = 50000;
int QUEUE_TABLE_COUNT = 1;
char files_dir[FILE_NAME_BUF_SIZE] = "./files/";
int file_count = 0;
int repeat_files = 1;
double global_time = -omp_get_wtime();
double local_time;
char csv_out[400] = "";
char tmp_out[200] = ""; // Buffer was small. sprintf caused a buffer overflow and modified the inputs.
// https://stackoverflow.com/questions/3706086/using-sprintf-will-change-the-specified-variable
// Parsing User inputs from run command with getopt
int arg_parse = process_args(argc, argv, files_dir, &repeat_files, &DEBUG_MODE, &PRINT_MODE, &HASH_SIZE,
&QUEUE_TABLE_COUNT, &NUM_THREADS); // NUM_THREADS is dummy here
if (arg_parse == -1)
{
printf("Check inputs and rerun! Exiting!\n");
return 1;
}
/********************** Creating and populating FilesQueue ************************************************/
struct Queue *file_name_queue;
file_name_queue = createQueue();
if (DEBUG_MODE)
{
printf("\nQueuing files in Directory: %s\n", files_dir);
printf("Queuing files %d time(s)\n", repeat_files);
}
local_time = -omp_get_wtime();
for (int i = 0; i < repeat_files; i++)
{
int files = get_file_list(file_name_queue, files_dir);
if (files == -1)
{
printf("Check input directory and rerun! Exiting!\n");
return 1;
}
file_count += files;
}
local_time += omp_get_wtime();
sprintf(tmp_out, "%d, %d, %.4f, ", file_count, HASH_SIZE, local_time);
strcat(csv_out, tmp_out);
if (PRINT_MODE)
printf("Done Queuing %d files! Time taken: %f\n", file_count, local_time);
/**********************************************************************************************************/
// TODO: This should be an array as in Prof's lecture (Week 4: Uniform distribution of Elements)
int files_per_qt = file_count / QUEUE_TABLE_COUNT;
if (PRINT_MODE)
{
printf("\nNumber of Queues and Tables: %d\n", QUEUE_TABLE_COUNT);
printf("Number of files per Q&T: %d\n", files_per_qt);
}
/********************** Queuing Lines by reading files in the FilesQueue **********************************/
if (PRINT_MODE)
printf("\nQueuing Lines by reading files in the FilesQueue\n");
local_time = -omp_get_wtime();
struct Queue **queues;
queues = (struct Queue **)malloc(sizeof(struct Queue *) * QUEUE_TABLE_COUNT);
for (int i = 0; i < QUEUE_TABLE_COUNT; i++)
{
queues[i] = createQueue();
for (int j = 0; j < files_per_qt; j++)
{
populateQueue(queues[i], file_name_queue->front->line);
queues[i]->finished = 1; //TODO: What is this for?
deQueue(file_name_queue);
}
}
local_time += omp_get_wtime();
sprintf(tmp_out, "%.4f, ", local_time);
strcat(csv_out, tmp_out);
if (PRINT_MODE)
printf("Done Populating lines! Time taken: %f\n", local_time);
/**********************************************************************************************************/
/********************** Hashing words by reading words in the LinesQueue **********************************/
if (PRINT_MODE)
{
printf("\nHashing words by reading words in the LinesQueue\n");
printf("HASH_SIZE: %d\n", HASH_SIZE);
}
local_time = -omp_get_wtime();
struct hashtable **hash_tables;
hash_tables = (struct hashtable **)malloc(sizeof(struct hashtable *) * QUEUE_TABLE_COUNT);
for (int i = 0; i < QUEUE_TABLE_COUNT; i++)
{
hash_tables[i] = createtable(HASH_SIZE);
populateHashMap(queues[i], hash_tables[i]);
}
local_time += omp_get_wtime();
sprintf(tmp_out, "%.4f, ", local_time);
strcat(csv_out, tmp_out);
if (PRINT_MODE)
printf("Done Hashing words! Time taken: %f\n", local_time);
/**********************************************************************************************************/
/********************** Reducing the populated words in HashTable *****************************************/
if (PRINT_MODE)
printf("\nReducing the populated words in HashTable\n");
local_time = -omp_get_wtime();
// struct hashtable *final_table = createtable(HASH_SIZE);
for (int i = 0; i < HASH_SIZE; i++)
{
if (QUEUE_TABLE_COUNT == 1)
{
if (DEBUG_MODE)
printf("Skipping reduce as the hash_table count is 1");
break;
}
// Here, I am passing the address of hash_tables[1] to be reduced to hash_tables[0].
// So, total queues count is subtracted by 1
reduce(&hash_tables[1], hash_tables[0], QUEUE_TABLE_COUNT - 1, i);
}
local_time += omp_get_wtime();
sprintf(tmp_out, "%.4f, ", local_time);
strcat(csv_out, tmp_out);
if (PRINT_MODE)
printf("Done Reducing! Time taken: %f\n", local_time);
/**********************************************************************************************************/
/********************** Writing Reduced counts to output file *********************************************/
if (PRINT_MODE)
printf("\nWriting Reduced counts to output file\n");
local_time = -omp_get_wtime();
// printTable(final_table);
writeFullTable(hash_tables[0], "./output/serial/0.txt");
local_time += omp_get_wtime();
sprintf(tmp_out, "%.4f, ", local_time);
strcat(csv_out, tmp_out);
if (PRINT_MODE)
printf("Done Writing! Time taken: %f\n", local_time);
/**********************************************************************************************************/
/********************** Clearing the heap allocations for Queues and HashTables ***************************/
if (DEBUG_MODE)
printf("\nClearing the heap allocations for Queues and HashTables\n");
local_time = -omp_get_wtime();
for (int i = 0; i < QUEUE_TABLE_COUNT; i++)
{
free(queues[i]);
// printTable(hash_tables[i]);
free(hash_tables[i]);
}
free(queues);
free(hash_tables);
local_time += omp_get_wtime();
if (DEBUG_MODE)
printf("Done Clearing! Time taken: %f\n", local_time);
/**********************************************************************************************************/
global_time += omp_get_wtime();
sprintf(tmp_out, "%.4f, ", global_time);
strcat(csv_out, tmp_out);
if (PRINT_MODE)
printf("\nTotal time taken for the execution: %f\n", global_time);
printf("Num_files, Hash_size, Qfiles_time, Qlines_time, HashW_time, Reduce_time, Write_time, Total_time,\n%s\n\n", csv_out);
return EXIT_SUCCESS;
}