forked from open-power/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_bfs.c
337 lines (284 loc) · 7.29 KB
/
action_bfs.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Simple Breadth-first-search in C
*
* Based on Pseudo code of:
* https://en.wikipedia.org/wiki/Breadth-first_search
*
* Use Adjacency list to describe a graph:
* https://en.wikipedia.org/wiki/Adjacency_list
*
* And takes Queue structure:
* https://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29
*
* Wikipedia's pages are based on "CC BY-SA 3.0"
* Creative Commons Attribution-ShareAlike License 3.0
* https://creativecommons.org/licenses/by-sa/3.0/
*
* Attribution:
* You must give appropriate credit, provide a link to
* the license, and indicate if changes were made. You may do so in
* any reasonable manner, but not in any way that suggests the
* licensor endorses you or your use.
*
* ShareAlike:
* If you remix, transform, or build upon the material, you must
* distribute your contributions under the same license as the original.
*/
/*
* Adopt SNAP's framework for software action part.
*/
/*
* Copyright 2017, International Business Machines
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Example to use the FPGA to traverse a graph with breadth-first-search
* method.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <endian.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libsnap.h>
#include <linux/types.h> /* __be64 */
#include <asm/byteorder.h>
#include <snap_internal.h>
#include <snap_tools.h>
#include <action_bfs.h>
static int mmio_write32(struct snap_card *card,
uint64_t offs, uint32_t data)
{
act_trace(" %s(%p, %llx, %x)\n", __func__, card,
(long long)offs, data);
return 0;
}
static int mmio_read32(struct snap_card *card,
uint64_t offs, uint32_t *data)
{
act_trace(" %s(%p, %llx, %x)\n", __func__, card,
(long long)offs, *data);
return 0;
}
//-------------------------------------
// Queue functions
//-------------------------------------
static Queue * InitQueue()
{
Queue *q= (Queue*)malloc (sizeof(Queue));
if (!q)
{
printf("ERROR: failed to malloc queue.\n");
return NULL;
}
q -> front = NULL;
q -> rear = NULL;
return q;
}
static void DestoryQueue( Queue *q)
{
free (q);
}
static int QueueEmpty(Queue * q)
{
return (q->front == NULL);
}
/*
static void PrintQueue(Queue *q)
{
printf("Queue is ");
if (QueueEmpty(q))
{
printf ("empty.\n");
return;
}
QNode * node = q->front;
while (node != NULL)
{
printf ("%d, ", node->data);
node = node->next;
}
printf("\n");
}
*/
static void EnQueue (Queue *q, ElementType element)
{
QNode * node_ptr = (QNode*) malloc (sizeof(QNode));
if (!node_ptr)
{
printf("ERROR: failed to malloc a queue node.\n");
return;
}
node_ptr->data = element;
node_ptr->next = NULL;
//Append to the tail
if (q->front == NULL)
q->front = node_ptr;
if (q->rear == NULL)
{
q->rear = node_ptr;
}
else
{
q->rear->next = node_ptr;
q->rear = node_ptr;
}
//PrintQueue(q);
}
static void DeQueue (Queue *q, ElementType *ep)
{
// printf("Dequeue\n");
if (QueueEmpty (q))
{
printf ("ERROR: DeQueue: queue is empty.\n");
return;
}
QNode *temp = q->front;
if(q->front == q->rear) //only one element
{
q->front = NULL;
q->rear = NULL;
}
else
{
q->front = q->front->next;
}
*ep = temp->data;
free(temp);
}
//-------------------------------------
// breadth first search
//-------------------------------------
// put one visited vertex to the place of g_out_ptr.
// Last vertex (is_tail=1) will follow an END sign (FFxxxxxx)
// And with the total number of vertices
void output_vex(unsigned int vex, int is_tail)
{
if(is_tail == 0)
{
*g_out_ptr = vex;
g_out_ptr ++;
// printf("Visit Node %d\n", vex);
}
else
{
*g_out_ptr = 0xFF000000 + vex; //here vex means cnt
// printf("End. %x\n", *g_out_ptr);
g_out_ptr += 32 - (((unsigned long long )g_out_ptr & 0x7C) >> 2); //Make paddings.
}
}
/*
int bfs_all (VexNode * vex_list, unsigned int vex_num)
{
unsigned int i;
for (i = 0; i < vex_num; i++)
{
bfs(vex_list, vex_num, i);
}
return 0;
}
*/
//Breadth-first-search from a perticular vertex.
void bfs (VexNode * vex_list, unsigned int vex_num, unsigned int root)
{
EdgeNode *p;
Queue *Q;
unsigned int current, i;
int * visited;
visited = (int *) malloc (vex_num * sizeof(int));
unsigned int cnt = 0;
current = 0;
//initilize to all zero.
for (i = 0; i < vex_num; i++)
visited[i] = 0;
Q = InitQueue();
visited[root] = 1;
output_vex( root,0);
cnt++;
EnQueue(Q, root);
while (! QueueEmpty(Q))
{
/*
printf("vistied = ");
for (i = 0; i < vex_num; i++)
{
printf("%d", visited[i]);
}
printf("\n");
*/
DeQueue(Q, ¤t);
p = vex_list[current].edgelink;
// printf("current = %d\n", current);
while(p)
{
if(!visited[p->adjvex])
{
visited[p->adjvex] = 1;
output_vex(p->adjvex, 0);
cnt++;
EnQueue(Q, p->adjvex);
}
p = p->next;
} //till to NULL of the edge list
}
output_vex(cnt, 1); //Indicate a tail
free(visited);
DestoryQueue(Q);
}
//------------------------------------
// action main
//------------------------------------
static int action_main(struct snap_sim_action *action,
void *job, unsigned int job_len __unused)
{
int rc = 0;
bfs_job_t *js = (bfs_job_t *)job;
VexNode * vex_list = (VexNode *) js->input_adjtable.addr;
unsigned int vex_num = js->vex_num;
g_out_ptr = (unsigned int *)js->output_traverse.addr;
bfs(vex_list, vex_num, js->start_root);
js->status_vex = vex_num;
js->status_pos = (unsigned int)((unsigned long long) g_out_ptr & 0xFFFFFFFFull);
if (rc == 0)
goto out_ok;
else
goto out_err;
out_ok:
action->job.retc = SNAP_RETC_SUCCESS;
return 0;
out_err:
action->job.retc = SNAP_RETC_FAILURE;
return 0;
}
static struct snap_sim_action action = {
.vendor_id = SNAP_VENDOR_ID_ANY,
.device_id = SNAP_DEVICE_ID_ANY,
.action_type = BFS_ACTION_TYPE,
.job = { .retc = SNAP_RETC_FAILURE, },
.state = ACTION_IDLE,
.main = action_main,
.priv_data = NULL, /* this is passed back as void *card */
.mmio_write32 = mmio_write32,
.mmio_read32 = mmio_read32,
.next = NULL,
};
static void _init(void) __attribute__((constructor));
static void _init(void)
{
snap_action_register(&action);
}