PandA-2024.02
bfs.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <time.h>
7 #include <omp.h>
8 
9 #include "graph.h"
10 
11 
12 unsigned map[NUM_VERTICES]={0};
13 
14 
15 __attribute__((noinline))
16 unsigned cas(unsigned * addr, unsigned old, unsigned new)
17 {
18  unsigned ret = 1;
19 #pragma omp atomic
20  {
21  if(*addr==old)
22  {
23  *addr=new;
24  ret = 0;
25  }
26  }
27  return ret;
28 }
29 
30 
31 __attribute__((noinline))
32 unsigned ifa(unsigned * addr, unsigned val)
33 {
34  unsigned temp;
35 #pragma omp atomic
36  {
37  temp = *addr;
38  *addr=temp+val;
39  }
40  return temp;
41 }
42 
43 
44 void kernel(unsigned vertex, unsigned *p_Qnext, unsigned *Qnext_N, unsigned * map)
45 {
46  unsigned i;
47  for ( i = offset[vertex]; i < offset[vertex+1]; i++ ) {
48  if ( cas ( &map[edges[i]],0,1 ) == 0 ) {
49  p_Qnext[ifa( Qnext_N, 1 )] = edges[i];
50  }
51  }
52 }
53 
54 
55 __attribute__((noinline))
56 void kernel_wrapper(unsigned int i, unsigned * p_Q, unsigned * p_Qnext, unsigned * Qnext_N)
57 {
58  unsigned vertex = p_Q[i];
59  kernel(vertex, p_Qnext, Qnext_N, map);
60 }
61 
62 
63 __attribute__((noinline))
64 void parallel(unsigned start, unsigned end,unsigned * p_Q, unsigned * p_Qnext, unsigned * Qnext_N)
65 {
66  unsigned i;
67 #pragma omp parallel for
68  for (i = start; i < end; ++i)
69  kernel_wrapper(i, p_Q, p_Qnext, Qnext_N);
70 }
71 
72 
73 int main () {
74  unsigned root = 0;
75 
76  map[root] = 1;
77 
78  unsigned Q[NUM_VERTICES]={0};
79  unsigned Qnext[NUM_VERTICES]={0};
80  unsigned Qnext_N = 0;
81 
82  unsigned Q_N = 0;
83 
84  Q_N = 1;
85  Q[0] = root;
86  Qnext_N = 0;
87 
88  unsigned * p_Q = ( unsigned * ) Q;
89  unsigned * p_Qnext = ( unsigned * ) Qnext;
90 
91  unsigned level = 0;
92 
93  unsigned totExplored = 0;
94 
95  while ( Q_N != 0 ) {
96  totExplored += Q_N;
97 
98  parallel(0, Q_N, p_Q, p_Qnext, &Qnext_N);
99 
100  unsigned * p_tmp;
101 
102  p_tmp = p_Q;
103  p_Q = p_Qnext;
104  p_Qnext = p_tmp;
105 
106  Q_N = Qnext_N;
107  Qnext_N = 0;
108 
109  level++;
110  }
111 
112  return level;
113 }
114 
__attribute__((noinline))
Convert the given fixedpt number to a decimal string.
Definition: bfs.c:15
unsigned edges[4545]
Definition: graph.h:25
unsigned map[NUM_VERTICES]
Definition: bfs.c:12
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
void kernel(unsigned vertex, unsigned *p_Qnext, unsigned *Qnext_N, unsigned *map)
Definition: bfs.c:44
unsigned offset[NUM_VERTICES+1]
Definition: graph.h:3
#define NUM_VERTICES
Definition: graph.h:1
int main()
Definition: bfs.c:73
int level
Definition: main.c:98

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13