forked from svaderia/ParallelDF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
159 lines (135 loc) · 4.64 KB
/
driver.c
File metadata and controls
159 lines (135 loc) · 4.64 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
#include "myTrie.h"
#include "myStack.h"
#include "traversal.h"
#include "myRead.h"
#include "myHeap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <omp.h>
Stack* init_stack(char* path){
Stack* stack = new_stack();
FileNode* stack_init = (FileNode*) malloc(sizeof(FileNode));
stack_init -> name = (char*) malloc((strlen(path) + 1) * sizeof(char));
memset(stack_init -> name, '\0', (strlen(path) + 1) * sizeof(char));
memcpy(stack_init -> name, path, strlen(path) * sizeof(char));
stack_init -> depth = 1;
stack = push(stack, stack_init);
return stack;
}
TrieNode** init_tries(int size){
TrieNode** trie_list = (TrieNode**) malloc(sizeof(TrieNode*) * size );
int i;
for( i = 0; i < size; i++){
trie_list[i] = get_Node();
}
return trie_list;
}
char** load_stopwords(){
FILE* fptr = fopen("stopwords", "r");
int size;
char** list_of_words = read_arr(fptr, &size);
fclose(fptr);
return list_of_words;
}
void process_doc(FILE* doc, TrieNode* doc_root, int current_doc, char** stopwords){
char* b = (char*) malloc(4096 * sizeof(char));
char* word_buffer;
int k = 4096;
int offset = 0;
bool eof = false;
memset(b, 0, k);
while(1){
word_buffer = getWord(doc, b, k, &offset, &eof);
if(search(stopwords, word_buffer, 0, 570) == 1){
free(word_buffer);
continue;
}
// #pragma omp critical (a)
// printf("%s\n", word_buffer);
doc_root = trie_insert(doc_root, word_buffer, current_doc);
free(word_buffer);
if( offset >= strlen(b) && eof ) break;
}
}
TrieNode* merge_all(TrieNode* g_trie, TrieNode** trie_list, int num_threads, int alpha){
int i;
for(i = 0; i < num_threads; i++){
if(trie_list[i] -> children[alpha]){
if(!g_trie -> children[alpha])
g_trie -> children[alpha] = get_Node();
g_trie -> children[alpha] = merge_trie(g_trie -> children[alpha], trie_list[i] -> children[alpha]);
}
}
return g_trie;
}
void main(int argc, char* argv[]){
/*
argv[1] : path to the root directory
argv[2] : number of threads
*/
// Stack Initialization
Stack* stack = init_stack(argv[1]);
// Setting number of threads
int num_threads = atoi(argv[2]);
omp_set_num_threads(num_threads);
// Initialization of Thread-level tries
TrieNode** trie_list = init_tries(num_threads);
// Initialization of Global Trie
TrieNode* g_trie = get_Node();
// Initialization for `get_next_file`
int last_depth = 1;
// Loading the stopwords
char** list_of_words = load_stopwords();
// Making of thread level Tries
#pragma omp parallel default(shared)
{
FileNode* pT;
int last_doc = 0;
FILE* doc;
int id = omp_get_thread_num();
// Update of Thread-Level Trie on receiving a document
while(true){
#pragma omp critical (new)
{
pT = get_next_file(stack, last_depth);
if(pT != NULL){
last_depth = pT -> depth;
int id = omp_get_thread_num();
// printf("Fname: %s : %d \n", pT->name, id);
doc = fopen(pT->name, "r");
if(doc == NULL){
perror("Can not open File");
}
}
}
if(pT == NULL) break;
process_doc(doc, trie_list[id], last_doc+1, list_of_words);
last_doc++;
fclose(doc);
}
#pragma omp barrier
// Merging of all the Thread-Level Tries
int i;
#pragma omp for private(i) schedule(dynamic, 1)
for(i = 0; i < 26; i++){
g_trie = merge_all(g_trie, trie_list, num_threads, i);
}
}
char* buffer = (char*) malloc(30 * sizeof(char));
memset(buffer, '\0', 30 * sizeof(char));
// trie_list[0] = print_trie(trie_list[0], -1, buffer, 0);
// trie_list[1] = print_trie(trie_list[1], -1, buffer, 0);
// trie_list[2] = print_trie(trie_list[2], -1, buffer, 0);
// trie_list[3] = print_trie(trie_list[3], -1, buffer, 0);
// printf("\nfinal\n");
// g_trie = print_trie(g_trie, -1, buffer, 0);
int heapsize = 0, capacity= 2;
MaxHeapNode** test = (MaxHeapNode**) malloc(capacity * sizeof(MaxHeapNode*));
test = make_heap_from_trie(g_trie, test, &heapsize, &capacity, -1, buffer, 0);
// printf("%d %d\n", heapsize, capacity);
print(test, heapsize);
test = buildHeap(test, heapsize - 1);
print_topk(test, &heapsize, 2);
}