PandA-2024.02
bellmanford.c
Go to the documentation of this file.
1 
2 #include <limits.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 
6 /* Let INFINITY be the maximum value for an int */
7 
8 typedef struct {
9  int source;
10  int dest;
11  int weight;
12 } Edge;
13 
14 #define EXT_DATASET
15 #ifndef EXT_DATASET
16 #define N_dest 10
17 #define N_dist 5
18 #else
19 #define N_dest 21
20 #define N_dist 15
21 #endif
22 
23 #ifndef INT_MAX
24 #define INT_MAX 2147483647
25 #endif
26 
27 #define INFINITY INT_MAX
28 
29 #pragma map generate_hw 0
30 _Bool
31 __attribute__ ((noinline))
32 bellmanford(int source[N_dest], int dest[N_dest], int weight [N_dest], int distance[N_dist], int edgecount, int nodecount, int src)
33 {
34  int i, j;
35  _Bool succeeded = 1;
36 
37  for (i=0; i < nodecount; ++i)
38  distance[i] = INFINITY;
39  distance[src] = 0;
40 
41  for (i=0; i < nodecount; ++i) {
42  for (j=0; j < edgecount; ++j) {
43  if (distance[source[j]] != INFINITY) {
44  int new_distance = distance[source[j]] + weight[j];
45  if (new_distance < distance[dest[j]])
46  distance[dest[j]] = new_distance;
47  }
48  }
49  }
50 
51  for (i=0; i < edgecount; ++i) {
52  if (distance[dest[i]] > distance[source[i]] + weight[i]) {
53  succeeded = 0;
54  i = edgecount;
55  }
56  }
57 
58  return succeeded;
59 }
60 
61 int main(void)
62 {
63  int *source, *dest, *weight, *dist, i, edgecount, nodecount, src, success;
64 
65 #ifndef EXT_DATASET
66  edgecount = 10;
67  nodecount = 5;
68  src = 0; // value should be 0< & <5
69 
70  source = (int*) malloc( 10 * sizeof( int ) );
71  dest = (int*) malloc( edgecount * sizeof( int ) );
72  weight = (int*) malloc( edgecount * sizeof( int ) );
73  dist = (int*) malloc( nodecount * sizeof( int ) );
74 
75  source[0] = 0; dest[0] = 1; weight[0] = 5;
76  source[1] = 0; dest[1] = 2; weight[1] = 8;
77  source[2] = 0; dest[2] = 3; weight[2] = -4;
78  source[3] = 1; dest[3] = 0; weight[3] = -2;
79  source[4] = 2; dest[4] = 1; weight[4] = -3;
80  source[5] = 2; dest[5] = 3; weight[5] = 9;
81  source[6] = 3; dest[6] = 1; weight[6] = 7;
82  source[7] = 3; dest[7] = 4; weight[7] = 2;
83  source[8] = 4; dest[8] = 0; weight[8] = 6;
84  source[9] = 4; dest[9] = 2; weight[9] = 7;
85 #else
86  edgecount = 21;
87  nodecount = 15;
88  src = 0;
89 
90  source = (int*) malloc( 21 * sizeof( int ) );
91  dest = (int*) malloc( edgecount * sizeof( int ) );
92  weight = (int*) malloc( edgecount * sizeof( int ) );
93  dist = (int*) malloc( nodecount * sizeof( int ) );
94 
95  source[0] = 0; dest[0] = 2; weight[0] = 2;
96  source[1] = 0; dest[1] = 13; weight[1] = 30;
97  source[2] = 1; dest[2] = 12; weight[2] = -2;
98  source[3] = 2; dest[3] = 1; weight[3] = -1;
99  source[4] = 2; dest[4] = 11; weight[4] = 9;
100  source[5] = 3; dest[5] = 13; weight[5] = -10;
101  source[6] = 3; dest[6] = 10; weight[6] = 7;
102  source[7] = 5; dest[7] = 3; weight[7] = 3;
103  source[8] = 5; dest[8] = 6; weight[8] = 2;
104  source[9] = 6; dest[9] = 8; weight[9] = 3;
105  source[10] = 7; dest[10] = 6; weight[10] = -4;
106  source[11] = 7; dest[11] = 8; weight[11] = 6;
107  source[12] = 8; dest[12] = 7; weight[12] = 3;
108  source[13] = 9; dest[13] = 7; weight[13] = 2;
109  source[14] = 9; dest[14] = 4; weight[14] = -5;
110  source[15] = 10; dest[15] = 11; weight[15] = 1;
111  source[16] = 10; dest[16] = 14; weight[16] = 2;
112  source[17] = 11; dest[17] = 5; weight[17] = 8;
113  source[18] = 12; dest[18] = 0; weight[18] = -3;
114  source[19] = 13; dest[19] = 9; weight[19] = 3;
115  source[20] = 14; dest[20] = 1; weight[20] = 6;
116 
117 #endif
118 
119  #pragma map call_hw VIRTEX5 0
120  success = bellmanford(source, dest, weight, dist, edgecount, nodecount, src);
121 
122 #ifndef EXT_DATASET
123  if (success != 1)
124  printf("FAIL!\n");
125  else
126  printf("PASS!\n");
127 #else
128  if (success != 0)
129  printf("FAIL!\n");
130  else
131  printf("PASS!\n");
132 #endif
133 
134  return 0;
135 }
#define INFINITY
Definition: bellmanford.c:27
#define N_dest
Definition: bellmanford.c:19
_Bool __attribute__((noinline))
Definition: bellmanford.c:31
int weight
Definition: bellmanford.c:11
struct Edge Edge
#define N_dist
Definition: bellmanford.c:20
int dest
Definition: bellmanford.c:10
TYPE distance(TYPE position_x[nAtoms], TYPE position_y[nAtoms], TYPE position_z[nAtoms], int i, int j)
Definition: md_kernel_test.c:3
int main(void)
Definition: bellmanford.c:61
int source
Definition: bellmanford.c:9

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