-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathopenmp_bellman_ford_fast_with_queue.cpp
More file actions
178 lines (150 loc) · 5.03 KB
/
openmp_bellman_ford_fast_with_queue.cpp
File metadata and controls
178 lines (150 loc) · 5.03 KB
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
/*
* This is a openmp version of bellman_ford algorithm
* Compile: g++ -std=c++11 -o openmp_bellman_ford openmp_bellman_ford.cpp
* Run: ./openmp_bellman_ford <input file> <number of threads>, you will find the output file 'output.txt'
* */
#include <string>
#include <cassert>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <queue>
#include <sys/time.h>
#include "omp.h"
using std::string;
using std::cout;
using std::endl;
#define INF 1000000
/**
* utils is a namespace for utility functions
* including I/O (read input file and print results) and matrix dimension convert(2D->1D) function
*/
namespace utils {
int N; //number of vertices
int *mat; // the adjacency matrix
void abort_with_error_message(string msg) {
std::cerr << msg << endl;
abort();
}
//translate 2-dimension coordinate to 1-dimension
int convert_dimension_2D_1D(int x, int y, int n) {
return x * n + y;
}
int read_file(string filename) {
std::ifstream inputf(filename, std::ifstream::in);
if (!inputf.good()) {
abort_with_error_message("ERROR OCCURRED WHILE READING INPUT FILE");
}
inputf >> N;
//input matrix should be smaller than 20MB * 20MB (400MB, we don't have too much memory for multi-processors)
assert(N < (1024 * 1024 * 20));
mat = (int *) malloc(N * N * sizeof(int));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
inputf >> mat[convert_dimension_2D_1D(i, j, N)];
}
return 0;
}
int print_result(bool has_negative_cycle, int *dist) {
std::ofstream outputf("output.txt", std::ofstream::out);
if (!has_negative_cycle) {
for (int i = 0; i < N; i++) {
if (dist[i] > INF)
dist[i] = INF;
outputf << dist[i] << '\n';
}
outputf.flush();
} else {
outputf << "FOUND NEGATIVE CYCLE!" << endl;
}
outputf.close();
return 0;
}
}//namespace utils
/**
* Bellman-Ford algorithm. Find the shortest path from vertex 0 to other vertices.
* @param p number of processes
* @param n input size
* @param *mat input adjacency matrix
* @param *dist distance array
* @param *has_negative_cycle a bool variable to recode if there are negative cycles
*/
void bellman_ford(int p, int n, int *mat, int *dist, bool *has_negative_cycle) {
*has_negative_cycle = false;
omp_set_num_threads(p);
//initialize distances
for (int i = 0; i < n; i++) {
dist[i] = INF;
}
//root vertex always has distance 0
dist[0] = 0;
int current_queue = 0;
int *enqueue_counter;
std::queue<int> queue;
bool *in_queue;
in_queue = (bool *) calloc(sizeof(bool), n);
enqueue_counter = (int *) calloc(n, sizeof(int));
in_queue[0] = true;
queue.push(0);
enqueue_counter[0] = 1;
while(!queue.empty() && !*has_negative_cycle){
int u = queue.front(); queue.pop();
in_queue[u] = false;
#pragma omp parallel for
for (int v = 0; v < n; v++) {
int weight = mat[u * n + v];
if (weight < INF) {
int new_dist = weight + dist[u];
if (new_dist < dist[v]) {
dist[v] = new_dist;
enqueue_counter[v]++;
if (in_queue[v] == false) {
in_queue[v] = true;
if (enqueue_counter[v] >= n) {
*has_negative_cycle = true;
}
#pragma omp critical
{
queue.push(v);
}
}
}
}
}
}
//step 4: free memory (if any)
free(enqueue_counter);
}
int main(int argc, char **argv) {
if (argc <= 1) {
utils::abort_with_error_message("INPUT FILE WAS NOT FOUND!");
}
if (argc <= 2) {
utils::abort_with_error_message("NUMBER OF THREADS WAS NOT FOUND!");
}
string filename = argv[1];
int p = atoi(argv[2]);
int *dist;
bool has_negative_cycle = false;
assert(utils::read_file(filename) == 0);
dist = (int *) malloc(sizeof(int) * utils::N);
//time counter
timeval start_wall_time_t, end_wall_time_t;
float ms_wall;
//start timer
gettimeofday(&start_wall_time_t, nullptr);
//bellman-ford algorithm
bellman_ford(p, utils::N, utils::mat, dist, &has_negative_cycle);
//end timer
gettimeofday(&end_wall_time_t, nullptr);
ms_wall = ((end_wall_time_t.tv_sec - start_wall_time_t.tv_sec) * 1000 * 1000
+ end_wall_time_t.tv_usec - start_wall_time_t.tv_usec) / 1000.0;
std::cerr.setf(std::ios::fixed);
std::cerr << std::setprecision(6) << "Time(s): " << (ms_wall / 1000.0) << endl;
utils::print_result(has_negative_cycle, dist);
free(dist);
free(utils::mat);
return 0;
}