-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpi.cpp
128 lines (100 loc) · 3.18 KB
/
mpi.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <mpi.h>
typedef struct {
int u;
std::vector<int>v;
std::vector<int>w;
} Edge;
typedef struct {
std::vector<Edge> edges;
std::vector<int> weights;
std::vector<int> additional;
} ThreadStruct;
const int INF = 1e7;
int rank, size;
int nodes;
bool negativeCycle = false;
ThreadStruct threadStruct;
void* threadBellmanFord(int firstNode, int lastNode) {
for(int i = firstNode; i < lastNode; ++i)
for (int k = 0; k < threadStruct.edges[i].v.size(); ++k)
if (threadStruct.weights[threadStruct.edges[i].u] + threadStruct.edges[i].w[k] <
threadStruct.weights[threadStruct.edges[i].v[k]]) {
if (negativeCycle) {
printf("\nRank: %d, Graph contains a negative-weight cycle\n", rank);
exit(1);
} else {
threadStruct.additional[threadStruct.edges[i].v[k]] =
threadStruct.weights[threadStruct.edges[i].u] + threadStruct.edges[i].w[k];
}
}
}
void BellmanFord(int src)
{
int step = nodes/size;
int firstNode = rank * step;
int lastNode = rank == size - 1 ? nodes : firstNode + step;
for (int i = 0; i < nodes + 1; ++i)
threadStruct.weights.push_back(INF);
threadStruct.weights[src] = 0;
threadStruct.additional = threadStruct.weights;
for(int i = 0; i < nodes; ++i){
if(rank == 0) {
printf("Step #(%i,%i)\r", i + 1, nodes);
fflush(stdout);
}
negativeCycle = i == nodes;
threadBellmanFord(firstNode, lastNode);
MPI_Reduce(&threadStruct.additional[0], &threadStruct.weights[0],
threadStruct.additional.size(), MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
MPI_Bcast(&threadStruct.weights[0], threadStruct.weights.size(), MPI_INT, 0, MPI_COMM_WORLD);
}
}
void fileOpen(ThreadStruct &threadStruct){
std::fstream bell;
int w;
bell.open("Test//bellman.txt");
if(bell.is_open()) {
bell >> nodes;
if (nodes <= 0)
return;
for (int i = 0; i < nodes; ++i) {
Edge edge;
edge.u = i;
for (int j = 0; j < nodes; ++j) {
bell >> w;
if (w != 0) {
edge.v.push_back(j);
edge.w.push_back(w);
}
}
threadStruct.edges.push_back(edge);
}
}
bell.close();
}
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
std::clock_t start;
fileOpen(threadStruct);
int source_vertex = 0;
start = std::clock();
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
BellmanFord(source_vertex);
double duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
if (rank == 0) {
std::cout << std::endl;
for (int i = 0; i < nodes; ++i)
std::cout << "Node: " << i << " - " << threadStruct.weights[i] << "\n";
std::cout << std::endl;
std::cout << "MPI execution time: " << duration;
}
MPI_Finalize();
return 0;
}