-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteratomicDist-FW-SeqExample.cu
More file actions
237 lines (187 loc) · 8.79 KB
/
Copy pathinteratomicDist-FW-SeqExample.cu
File metadata and controls
237 lines (187 loc) · 8.79 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
#include <cstdlib>
#define ATOMS_IN_MOLECULE 10
//These bounds breaks the physical realism of molecule sizes
#define UNKNOWN_UPPER_BOUND 1.0f
#define UNKNOWN_LOWER_BOUND 0.01f
#define UNKNOWN_ACTUAL_VALUE 0.0f
//Struct representing the weight of an edge in the graph
typedef struct __align__(16)
{
float upper;
float lower;
float actual;
} weight;
float generateRandFloatInWeightRange();
int generateRandInt(int maxRageFromZero);
void generateGraph(weight adjMatrix[][ATOMS_IN_MOLECULE], int atomsRequested);
void printGraphMatrix(weight *adjMatrix, int atomsHeightWidth, bool fwPass);
void floydWarshallTheChemist(weight adjMatrix[][ATOMS_IN_MOLECULE], int atomsHeightWidth);
int main(){
//Initializing the RNG seed:
srand(time(NULL));
//Create matrix of size Atoms x Atoms, for a molecule made of a Atoms
weight moleculeGraph[ATOMS_IN_MOLECULE][ATOMS_IN_MOLECULE];
generateGraph(moleculeGraph, ATOMS_IN_MOLECULE);
printGraphMatrix(moleculeGraph[0], ATOMS_IN_MOLECULE, false);
//Computer goes beep boop beep, and now we might have a better idea as to how the molecule looks
floydWarshallTheChemist(moleculeGraph, ATOMS_IN_MOLECULE);
printf("\n\nNewly Computed Graph: \n\n");
printGraphMatrix(moleculeGraph[0], ATOMS_IN_MOLECULE, true);
return 0;
}
float generateRandFloatInWeightRange(){
int randNumPull = rand();
while (randNumPull == 0){
randNumPull = rand();
}
float randNum = ((float)(randNumPull % 101)) / 100;
return randNum;
}
int generateRandInt(int maxRageFromZero){
return (rand() % maxRageFromZero);
}
//Function to generate graph of molecule
void generateGraph(weight adjMatrix[][ATOMS_IN_MOLECULE], int atomsRequested){
weight noEdgeWeight;
noEdgeWeight.upper = 0.0f;
noEdgeWeight.lower = 0.0f;
noEdgeWeight.actual = 0.0f;
weight unknownMessurementWeight;
unknownMessurementWeight.upper = UNKNOWN_UPPER_BOUND;
unknownMessurementWeight.lower = UNKNOWN_LOWER_BOUND;
unknownMessurementWeight.actual = UNKNOWN_ACTUAL_VALUE;
//Initializing the molecule/graph with unknown messurements/weights
for(int row = 0; row < atomsRequested; row++){
for (int column = 0; column < atomsRequested; column++)
{
//Since the graph is undirected, there should be no edges from a vertex to itself
if (row == column)
{
//Performing 2D arr pointer arithmetic
adjMatrix[row][column] = noEdgeWeight;
}
else
{
//Setting the rest of the edges to their unknow messurement boundaries
adjMatrix[row][column] = unknownMessurementWeight;
}
}
}
//For demo's sake lets say we know the actual distance between 20% of the atom pairs
int atomPairsKnown = ((float)(atomsRequested * atomsRequested)) * 0.2f;
printf("Atom Pairs Known: %d\n", atomPairsKnown);
for (int i = 0; i < atomPairsKnown; i++)
{
int randRow = generateRandInt(atomsRequested);
int randColumn = generateRandInt(atomsRequested);
if (randRow != randColumn)
{
adjMatrix[randRow][randColumn].actual = generateRandFloatInWeightRange();
adjMatrix[randRow][randColumn].upper = adjMatrix[randRow][randColumn].actual;
adjMatrix[randRow][randColumn].lower = adjMatrix[randRow][randColumn].actual;
//Enforcing Symmetry
adjMatrix[randColumn][randRow].actual = adjMatrix[randRow][randColumn].actual;
adjMatrix[randColumn][randRow].upper = adjMatrix[randRow][randColumn].actual;
adjMatrix[randColumn][randRow].lower = adjMatrix[randRow][randColumn].actual;
}
}
}
void printGraphMatrix(weight *adjMatrix, int atomsHeightWidth, bool fwPass){
int atomsInTotal = 0;
int atomsPossiblyUnknown = 0;
int atomsNotConnected = 0;
for(int row = 0; row < atomsHeightWidth; row++){
for (int column = 0; column < atomsHeightWidth; column++)
{
float currUpper = (*(adjMatrix + (row * atomsHeightWidth + column))).upper;
float currLower = (*(adjMatrix + (row * atomsHeightWidth + column))).lower;
float currActual = (*(adjMatrix + (row * atomsHeightWidth + column))).actual;
float symCurrUpper = (*(adjMatrix + (column * atomsHeightWidth + row))).upper;
float symCurrLower = (*(adjMatrix + (column * atomsHeightWidth + row))).lower;
float symCurrActual = (*(adjMatrix + (column * atomsHeightWidth + row))).actual;
printf("adjMatrix[%d][%d] => (Upper: %f, Lower: %f, Actual: %f)", row, column, currUpper, currLower, currActual);
//Checks if we by accidently break physical bounds
if (currLower > currUpper)
{
printf("[BREAKS PHYSICS]");
}
//Checks if symmetry in the undirected graph is upheld
if ((currUpper != symCurrUpper) || (currLower != symCurrLower) || (currActual != symCurrActual))
{
printf("[BREAKS SYMMETRY]");
}
if (currUpper == 1.0f && currLower == 0.01f && currActual == 0.0f)
{
//Checks wheter the graph has has a pass in the bound-smoothing algorithm
if (fwPass == false)
{
printf("[POSSIBLY UNKNOWN]");
atomsPossiblyUnknown++;
}
else
{
//Means that no direct or transitive data including this edge existed
printf("[UNABLE TO INFER FROM AVAILABLE DATA]");
atomsPossiblyUnknown++;
}
}
if (currUpper == 0.0f && currLower == 0.0f && currActual == 0.0f)
{
atomsNotConnected++;
}
printf("\n");
atomsInTotal++;
}
}
printf("Atoms in Total: %d\n", atomsInTotal);
printf("Atoms Possibly Unknown in Total: %d\n", atomsPossiblyUnknown);
printf("Atom Pairs Not Connected: %d\n", atomsNotConnected);
}
void floydWarshallTheChemist(weight adjMatrix[][ATOMS_IN_MOLECULE], int atomsHeightWidth){
for (int atom_k = 0; atom_k < atomsHeightWidth; atom_k++)
{
for (int atom_i = 0; atom_i < atomsHeightWidth; atom_i++)
{
for (int atom_j = 0; atom_j < atomsHeightWidth; atom_j++)
{
//Skipping the nodes attempting to compute distance bounds to themselves, or those who have true data
if ((atom_i == atom_j) || (adjMatrix[atom_i][atom_j].actual != 0.0f))
{
continue;
}
//Computing upper bound
float currUpperDistanceFromItoJ = adjMatrix[atom_i][atom_j].upper;
float upperDistanceFromItoK = adjMatrix[atom_i][atom_k].upper;
float upperDistanceFromKtoJ = adjMatrix[atom_k][atom_j].upper;
//New upper bound for the atom pair
//Seeing wheter the current route is shorther or if the route through other atoms is shorter
adjMatrix[atom_i][atom_j].upper = (currUpperDistanceFromItoJ <= (upperDistanceFromItoK + upperDistanceFromKtoJ)) ? currUpperDistanceFromItoJ : (upperDistanceFromItoK + upperDistanceFromKtoJ);
}
}
}
for (int atom_k = 0; atom_k < atomsHeightWidth; atom_k++)
{
for (int atom_i = 0; atom_i < atomsHeightWidth; atom_i++)
{
for (int atom_j = 0; atom_j < atomsHeightWidth; atom_j++)
{
//Skipping the nodes attempting to compute distance bounds to themselves, or those who have true data
if ((atom_i == atom_j) || (adjMatrix[atom_i][atom_j].actual != 0.0f))
{
continue;
}
//Computing lower bound
float currLowerDistanceFromItoJ = adjMatrix[atom_i][atom_j].lower;
//New lower bound for the atom pair
//Seeing wheter the current route is longest or if the route through other atoms is longer
adjMatrix[atom_i][atom_j].lower = max(currLowerDistanceFromItoJ, max((adjMatrix[atom_i][atom_k].lower - adjMatrix[atom_k][atom_j].upper), (adjMatrix[atom_k][atom_j].lower - adjMatrix[atom_i][atom_k].upper)));
//Ensure that we dont break physics by surpassing the upper bounds
if (adjMatrix[atom_i][atom_j].lower > adjMatrix[atom_i][atom_j].upper)
{
adjMatrix[atom_i][atom_j].lower = adjMatrix[atom_i][atom_j].upper;
}
}
}
}
}