-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pothen-Fan-Parallel.cl
277 lines (232 loc) · 9.83 KB
/
Pothen-Fan-Parallel.cl
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
struct node
{
int v1,v2,edge_id;
};
__kernel void initialize_arrays(__global int *visited,
__global int *lookahead,
__global int *parent_edge_id,
__global int *starting_index
)
{
int i=get_global_id(0);
visited[i]=0;
parent_edge_id[i]=-1;
lookahead[i]=starting_index[i];
}
// Match and update step for Karp-Sipser algorithm
__kernel void match_and_update( __global int *Q, //0
__global struct node *edges, //1
__global int* outdegree, //2
__global int* starting_index, //3
__global int* ending_vertex_of_edge, //4
__global int* matched_edges, //5
__global int* lookahead, //6
__global int* visited, //7
__global int* parent_edge_id, //8
__global int* outdegree_copy, //9
__global int* matched) //10
{
int id=get_global_id(0);
int u=Q[id];
int stack[]={-1};
stack[0]=u;
if(atomic_add(&visited[u],1)==0)
{
while(stack[0]!=-1)
{
int current_vertex=stack[0];
stack[0]=-1;
//printf("thread=%d,vertex=%d\n",id,current_vertex);
int end_index_for_current_vertex=starting_index[current_vertex]
+outdegree[current_vertex]-1;
for(int i=lookahead[current_vertex];i!=-1 && i<=end_index_for_current_vertex;i++)
{
//lookahead[current_vertex]=(lookahead[current_vertex]+1)>end_index_for_current_vertex?-1:lookahead[current_vertex]+1;
int dest_vertex=ending_vertex_of_edge[i];
if(atomic_add(&visited[dest_vertex],1)==0)
{
int eid=i;
int actual_eid=edges[eid].edge_id;
//printf("%d %d\n",edges[eid].v1,edges[eid].v2);
matched_edges[actual_eid]=1;
parent_edge_id[dest_vertex]=i;
matched[current_vertex]=eid;
matched[dest_vertex]=eid;
int end_index_for_dest_vertex=starting_index[dest_vertex]+outdegree[dest_vertex]-1;
for(int j=lookahead[dest_vertex];j!=-1 && j<=end_index_for_dest_vertex;j++)
{
//lookahead[dest_vertex]=(lookahead[dest_vertex]+1)>end_index_for_current_vertex?-1:lookahead[dest_vertex]+1;
int next_vertex=ending_vertex_of_edge[j];
if(atomic_add(&outdegree_copy[next_vertex],-1)==2)
{
if(atomic_add(&visited[next_vertex],1)==0)
{
stack[0]=next_vertex;
parent_edge_id[next_vertex]=j;
break;
}
}
}
break;
}
}
if(stack[0]==-1)
{
// int eid=parent_edge_id[current_vertex];
// if(eid==-1)
// stack[0]=-1;
// else
// {
// int parent_vertex=edges[eid].v1+edges[eid].v2-current_vertex;
// int grandparent_eid=parent_edge_id[parent_vertex];
// int grandparent_vertex=edges[grandparent_eid].v1+
// edges[grandparent_eid].v2-
// parent_vertex;
// stack[0]=grandparent_vertex;
// }
return;
}
}
}
}
// finding augmenting paths for Pothen-Fan
int dfs_for_hopcroft_karp(int u,
__global struct node *edges,
__global int* outdegree,
__global int* starting_index,
__global int* ending_vertex_of_edge,
__global int* matched,
__global int* lookahead,
__global int* visited,
__global int* parent_edge_id)
{
int path_end_vertex=-1;
int stack[]={-1};
stack[0]=u;
while(stack[0]!=-1)
{
int current_vertex=stack[0];
stack[0]=-1;
atomic_xchg(&visited[current_vertex],1);
int end_index_for_current_vertex=starting_index[current_vertex]
+outdegree[current_vertex]-1;
for(int i=lookahead[current_vertex];i!=-1 && i<=end_index_for_current_vertex;i++)
{
lookahead[current_vertex]=(lookahead[current_vertex]+1)>end_index_for_current_vertex?-1:lookahead[current_vertex]+1;
int dest_vertex=ending_vertex_of_edge[i];
if(atomic_add(&visited[dest_vertex],1)==0)
{
if(matched[dest_vertex]==-1)
{
parent_edge_id[dest_vertex]=i;
path_end_vertex=dest_vertex;
return path_end_vertex;
}
else
{
int matching_edge_id=matched[dest_vertex];
stack[0]=edges[matching_edge_id].v1+edges[matching_edge_id].v2-dest_vertex;
parent_edge_id[dest_vertex]=i;
parent_edge_id[stack[0]]=matching_edge_id;
// if(i==matching_edge_id)
// {
// std::cout<<"matching edge id:"<<i<<"\n";
// std::cout<<"concerned vertexes:";
// std::cout<<current_vertex<<" "<<dest_vertex<<" "<<stack[0]<<"\n";
// }
break;
}
}
}
if(stack[0]==-1)
{
int eid=parent_edge_id[current_vertex];
if(eid==-1)
stack[0]=-1;
else
{
int parent_vertex=edges[eid].v1+edges[eid].v2-current_vertex;
int grandparent_eid=parent_edge_id[parent_vertex];
int grandparent_vertex=edges[grandparent_eid].v1+
edges[grandparent_eid].v2-
parent_vertex;
stack[0]=grandparent_vertex;
}
}
}
return path_end_vertex;
}
// Performing the operation M= M Xor P
// M= current matching
// P= current augmenting path
void recalculate_matching(__global struct node *edges,
__global int *parent_edge_id,
int path_end_vertex,
int u,
__global int *matched_edges,
__global int *matched)
{
int current_vertex=path_end_vertex;
int parent_vertex;
int flag=0;
while(parent_edge_id[current_vertex]!=-1)
{
int eid=parent_edge_id[current_vertex];
int actual_eid=edges[eid].edge_id;
parent_vertex=edges[eid].v1+edges[eid].v2-current_vertex;
if(!flag)
{
matched_edges[actual_eid]=1;
matched[current_vertex]=eid;
matched[parent_vertex]=eid;
}
else
{
matched_edges[actual_eid]=0;
}
current_vertex=parent_vertex;
// std::cout<<"current_vertex:"<<current_vertex<<"\n";
// std::cout<<eid<<"\n";
flag^=1;
}
}
//driver function
__kernel void find_augmenting_paths(__global struct node* edges,
__global int* outdegree,
__global int* starting_index,
__global int* ending_vertex_of_edge,
__global int* vertices_X,
__global int* matched_edges,
__global int* parent_edge_id,
__global int* lookahead,
__global int* visited,
__global int* path_found,
__global int* matched)
{
int i=get_global_id(0);
int u=vertices_X[i];
if(matched[u]==-1)
{
int path_end_vertex=dfs_for_hopcroft_karp(u,
edges,
outdegree,
starting_index,
ending_vertex_of_edge,
matched,
lookahead,
visited,
parent_edge_id);
//printf("%d %d\n",u,path_end_vertex);
if(path_end_vertex!=-1)
{
atomic_xchg(path_found,1);
recalculate_matching(edges,
parent_edge_id,
path_end_vertex,
u,
matched_edges,
matched
);
}
}
}