-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_reliability.cpp
executable file
·162 lines (127 loc) · 5.37 KB
/
calc_reliability.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
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
#include <mpi.h>
#include "graph.h"
#include "graph.cpp"
#include <stdlib.h>
#include <time.h>
bool Dijkstra(int source, int destination, Graph G, int diameter);
int main(int argc, char *argv[])
{
double expectedR = 0.234521,
calcR;
int diameter = 8,
numVertices = 25,
source = 0,
destination = 18,
totalHits = 0,
myRank,
numProcs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
double randomNum,
start,
end,
totaltime,
outputtime;
const int localTrials = 1000000,
totalTrials = localTrials * numProcs;
int hits = 0;
Graph G(numVertices);
G.create();
MPI_Barrier(MPI_COMM_WORLD);
start = MPI_Wtime();
srand(time(NULL)*myRank);
//looping over number of times
for ( int i = 0 ; i < localTrials; i++)
{
for ( int j = 0; j < G.getTotalEdges(); j++ )
{
randomNum = rand() % 100 + 1;
//maybe just make this a function to determine it and send it the randnumber, so it can account for the number of edges currently in the graph which would help with finding the ri and ru stuff in
if ( randomNum > G.edge[j].successRate * 100 )
G.edge[j].determined = 0;
else
G.edge[j].determined = 1;
}
if ( Dijkstra(source, destination, G, diameter) )
{
hits++;
}
}
printf("\nProcessor: %d hits: %d\n\n", myRank, hits);
end = MPI_Wtime();
totaltime = end - start;
MPI_Reduce(&totaltime, &outputtime, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce(&hits, &totalHits, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (myRank == 0)
{
calcR = (double) totalHits / totalTrials;
cout << "Total number of proccessors = " << numProcs << endl;
cout << "Total number of hits = " << totalHits << endl;
cout << "Each Proccessor performs " << localTrials << " Trials" << endl;
cout << "Total number of trials = " << totalTrials << endl;
cout << "\nMonte Carlo Result " << endl;
cout << "5x5grid" << endl; //change
cout << "Diameter Constraint = " << diameter << endl;
cout << "Source Node = " << source << endl;
cout << "Destination Node = " << destination << endl;
cout << "Calculated Reliability = " << calcR << endl;
cout << "Expected Reliability = " << expectedR << endl;
cout << "Difference = " << (calcR - expectedR) << endl;
cout << "Time = " << outputtime << endl;
}
MPI_Finalize();
return 0;
}
bool Dijkstra(int source, int destination, Graph G, int diameter)
{
int totalVertices = G.getTotalVertices();
bool *T = new bool[totalVertices];
int *father = new int[totalVertices];
int *Lambda = new int[totalVertices];
int minIndex= source; // u is the vertex with smallest Lambda, which at beginning is s.
bool minIndexIsSet = true; //used to make sure that the minIndex is set to the first avail value each time
for (int i = 0; i < totalVertices; i++)
{
T[i] = true; // all the vertices are eligible to be visited;
father[i] = -1; // at the beginning every vertex's father is -1;
Lambda[i] = INT_MAX; // every vertex is assumed to be far away from s.
}
Lambda[source] = 0; // s is at distance 0 of itself.
//can this loop be while numOfVerticesToVisit !=0 or > 0!?
for (int numVerticesToVisit = totalVertices; numVerticesToVisit>0; numVerticesToVisit--)
{
/* set u to the vertex with the smallest Lambda. */
for (int i = 0; i < totalVertices; i++)
{
//cant this be done with something else?
if (!minIndexIsSet && T[i])
{
minIndex= i; //to set minIndexto the first available vertex.
minIndexIsSet = true;
}
else if (minIndexIsSet && T[i] && Lambda[i] < Lambda[minIndex])
minIndex= i; //to set minIndexto the smallest Lambda (aka distance from source).
}
//itterate over all adjacent nodes
for( node* adjnode = G.headnodes[minIndex].next; adjnode; adjnode = adjnode->next)
{
if (abs(G.edge[adjnode->edgeID].determined) == 1 &&
Lambda[minIndex] != INT_MAX && T[adjnode->vertex] &&
(Lambda[adjnode->vertex] > Lambda[minIndex] + adjnode->edgeWeight))
{
Lambda[adjnode->vertex] = Lambda[minIndex] + adjnode->edgeWeight;
father[adjnode->vertex] = minIndex;
}
}
minIndexIsSet = false; // to indicate minIndexis not set, I.E TAKE VIRST AVAIL INDEX IN FOR LOOP ABOVE
T[minIndex] = false; //minIndexis not available to be visited (aka. to delete minIndex from T).
}
/* Dijkstra Results: */
bool results = (father[destination] != -1 && Lambda[destination] <= diameter);
/* delete dynamically allocated variables. */
delete[] T;
delete[] father;
delete[] Lambda;
return results;
}