-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.c
236 lines (192 loc) · 4.14 KB
/
project.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <omp.h>
#include <time.h>
struct korifi
{
int k;
int in_degree;
struct korifi *next;
};
void appen(struct korifi** head_ref, int new_k, int new_d)
{
struct korifi* new_korifi = (struct korifi*) malloc(sizeof(struct korifi));
struct korifi *last = *head_ref;
new_korifi->k = new_k;
new_korifi->in_degree = new_d;
new_korifi->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_korifi;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_korifi;
return;
};
void del(struct korifi **head){
struct korifi *temp=*head;
temp=temp->next;
*head=temp;
};
void delete_k(struct korifi **head,int key_korifi)
{
struct korifi* temp = *head, *prev;
if (temp != NULL && temp->k == key_korifi)
{
*head = temp->next;
return;
}
while (temp != NULL && temp->k != key_korifi)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
};
void printList_k(struct korifi *n) {
while (n != NULL) {
printf(" %d %d\n", n->k,n->in_degree);
n = n->next;
}
};
struct Node
{
int data;
struct Node *next;
};
void append(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
};
void deletehead(struct Node **head){
struct Node *temp=*head;
temp=temp->next;
*head=temp;
}
void printList(struct Node *n) {
while (n != NULL) {
printf(" %d ", n->data);
n = n->next;
}
};
void check_in(struct korifi *head, struct Node **S)
{
#pragma omp task
while(head!=NULL)
{
if(head->in_degree==0) append(S, head->k);
head=head->next;
}
};
void decrease(struct korifi *head, int t)
{
while(head!=NULL)
{
if(head->k==t)
--(head->in_degree);
head=head->next;
}
};
int main()
{
int i = 0;
int tot = 0;
double start, end;
int nodes, trash, num_e;
int RSIZ = 30;
FILE *fptr=fopen("dag.txt", "r");
fscanf(fptr,"%d %d %d", &nodes, &trash, &num_e);
fclose(fptr);
int LSIZ=num_e;
char line[LSIZ][RSIZ];
fptr = fopen("dag.txt", "r");
i=0;
while(fptr!= NULL)
{
fscanf(fptr, "%d %d", &line[i][0], &line[i][1]);
i++;
if(i==LSIZ) break;
}
fclose(fptr);
tot = i;
int matrix[tot][2]; //DO NOT REMOVE!!!!! IF IT IS REMOVED PROGRAM BREAKS!!!!!!!
char const *temp1,*temp2;
int e[num_e][2];
for(i = 0; i<tot; i++)
{
temp1=&line[i+1][0];
temp2=&line[i+1][2];
e[i][1]=atoi(temp2);
e[i][0]=atoi(temp1);
}
int kor_temp[nodes][2];
for(i=0; i<nodes; i++){
kor_temp[i][0]=i;
kor_temp[i][1]=0;
}
int j;
for(i=0; i<nodes; i++)
for(j=0; j<num_e; j++)
if(kor_temp[i][0]==e[j][1])
kor_temp[i][1]++;
struct Node *S;
struct Node *L;
struct korifi *kor;
kor=NULL;
S=NULL;
L=NULL;
for(i=0; i<nodes; i++)
appen(&kor,kor_temp[i][0],kor_temp[i][1]);
check_in(kor,&S);
int temp;
start = omp_get_wtime();
while(S!=NULL)
{
#pragma omp parallel
delete_k(&kor,S->data);
append(&L, S->data);
temp=-1;
#pragma omp parallel for
for(i=0; i<num_e; i++)
{
if(e[i][0]==S->data)
{
temp=e[i][1];
#pragma omp critical
decrease(kor,temp);
}
}
deletehead(&S);
while(S!=NULL)
{
deletehead(&S); //clear S gia na mhn emf 2h fora komvous
}
check_in(kor,&S);
}
end = omp_get_wtime();
printf("\nTopologikh diataksi:\n");
printList(L);
double duration = end - start;
printf("\n\n\tstart = %.16g\n\tend = %.16g\n\tDuration = %.16g\n",start, end, duration);
//oi entoles autes itan gia exagogi dedomenon
/*FILE *output = fopen("output_parallel.csv", "a");
fprintf(output, "%.16g\n",duration);*/
return 0;
}