-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank.c
220 lines (208 loc) · 6.11 KB
/
pagerank.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_NODES 40000
struct node {
long id;
double pagerank;
int neighbour_counter;
struct node* next;
};
struct thread_arguments {
int start_node;
int end_node;
};
struct node* node_list[MAX_NODES];
int max_node_id = 0;
pthread_barrier_t barrier;
struct node* create_node(long id){
struct node* new_node;
if(id < 0 || id > MAX_NODES){
printf("Error: Node ID out of bounds.\n");
exit(1);
}
new_node = (struct node*)malloc(sizeof(struct node));
if(new_node == NULL){
printf("Error: Memory allocation failed.\n");
exit(1);
}
new_node->id = id;
new_node->pagerank = 1.0;
new_node->neighbour_counter = 0;
new_node->next = NULL;
node_list[id] = new_node;
return new_node;
}
void add_neighbour(struct node* current_node, struct node* neighbour_node){
struct node* new_neighbor = (struct node*)malloc(sizeof(struct node));
if(new_neighbor == NULL){
printf("Error: Memory allocation failed.\n");
exit(1);
}
new_neighbor->id = neighbour_node->id;
new_neighbor->next = NULL;
if(current_node->next == NULL){
current_node->next = new_neighbor;
}
else{
struct node* temp = current_node->next;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new_neighbor;
}
current_node->neighbour_counter++;
}
struct node* find_node(long id){
if(node_list[id] == NULL){
return NULL;
}
else{
return node_list[id];
}
}
void read_file(char* input_filename){
FILE* input_file = fopen(input_filename, "r");
long id, neighbour_id;
char* line = NULL;
size_t line_capacity = 0;
struct node* current_node;
struct node* neighbour_node;
if(input_file == NULL){
printf("Error: Failed to open input file.\n");
exit(1);
}
while(getline(&line, &line_capacity, input_file) != -1){
if(line[0] == '#'){
continue;
}
if(sscanf(line, "%ld %ld", &id, &neighbour_id) == 2){
if(id > max_node_id){
max_node_id = id;
}
if(neighbour_id > max_node_id){
max_node_id = neighbour_id;
}
current_node = find_node(id);
if(current_node == NULL){
current_node = create_node(id);
}
neighbour_node = find_node(neighbour_id);
if(neighbour_node == NULL){
neighbour_node = create_node(neighbour_id);
}
add_neighbour(current_node, neighbour_node);
}
else{
printf("Error: Invalid line format.\n");
}
}
free(line);
fclose(input_file);
}
void* calculate_pagerank(void* arg){
int i;
double* portions = (double*)malloc(MAX_NODES * sizeof(double));
struct node* current;
struct node* neighbour;
struct thread_arguments* args = (struct thread_arguments*)arg;
int start_node = args->start_node;
int end_node = args->end_node;
if(portions == NULL){
printf("Error: Memory allocation failed.\n");
exit(1);
}
for(i = start_node ; i <= end_node ; i++){
current = node_list[i];
if(current == NULL){
continue;
}
if(current->neighbour_counter != 0){
portions[i] = (0.85 * current->pagerank) / current->neighbour_counter;
}
else{
portions[i] = 0;
}
}
pthread_barrier_wait(&barrier);
for(i = start_node ; i <= end_node ; i++){
current = node_list[i];
if(current == NULL){
continue;
}
current->pagerank -= portions[i] * current->neighbour_counter;
neighbour = current->next;
while(neighbour != NULL){
node_list[neighbour->id]->pagerank += portions[i];
neighbour = neighbour->next;
}
}
free(portions);
free(args);
return NULL;
}
void print_to_file(){
FILE *output_file = fopen("pagerank.csv", "w");
int id;
if(output_file == NULL){
printf("Error: Failed to open output file.\n");
exit(1);
}
fprintf(output_file, "node, pagerank\n");
for(id = 0 ; id < max_node_id + 1 ; id++){
struct node* current = node_list[id];
if(current != NULL){
fprintf(output_file, "%ld, %.3f\n", current->id, current->pagerank);
}
}
}
void free_node_list(){
int i;
for(i = 0 ; i < max_node_id + 1 ; i++){
struct node* current = node_list[i];
while(current != NULL){
struct node* temp = current;
current = current->next;
free(temp);
}
}
}
int main(int argc, char* argv[]){
int i, iterations;
int number_of_threads = 0;
int nodes_per_thread, remainder_nodes, start_node;
pthread_t id[4];
if(argc != 3){
printf("Usage: %s <input_file> <number_of_threads>\n", argv[0]);
return 1;
}
if(atoi(argv[2]) < 1 || atoi(argv[2]) > 4){
printf("Error: number_of_threads must be an integer value between 1 and 4.\n");
return 1;
}
read_file(argv[1]);
number_of_threads = atoi(argv[2]);
nodes_per_thread = max_node_id / number_of_threads;
remainder_nodes = max_node_id % number_of_threads;
pthread_barrier_init(&barrier, NULL, number_of_threads);
for(iterations = 0 ; iterations < 50 ; iterations++){
start_node = 0;
for(i = 0 ; i < number_of_threads ; i++){
struct thread_arguments* args = (struct thread_arguments*)malloc(sizeof(struct thread_arguments));
args->start_node = start_node;
args->end_node = start_node + nodes_per_thread - 1;
if(i == number_of_threads - 1){
args->end_node += remainder_nodes + 1;
}
pthread_create(&id[i], NULL, calculate_pagerank, (void*)args);
start_node = args->end_node + 1;
}
for(i = 0 ; i < number_of_threads ; i++){
pthread_join(id[i], NULL);
}
}
print_to_file();
free_node_list();
return 0;
}