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

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