-
Notifications
You must be signed in to change notification settings - Fork 3
/
Helpers.cpp
263 lines (226 loc) · 7.08 KB
/
Helpers.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "Helpers.h"
#include <vector>
/**
* @brief A helper function to retrieve all PatternOccurrence objects from a list of PatternCodeRegions.
* On demand, the list is turned into a set.
*
* @param CodeRegions List of PatternCodeRegion objects.
* @param MakeUnique If true, all duplicates are removed from the container.
*
* @return A list of PatternOccurrence objects, free from duplicates iff flag is set.
**/
std::vector<PatternOccurrence*> PatternHelpers::GetPatternOccurrences(std::vector<PatternCodeRegion*> CodeRegions, bool MakeUnique)
{
/* Retrieve all pattern occurrences from the code regions */
std::vector<PatternOccurrence*> PatternOccurrences;
for (PatternCodeRegion* CodeReg : CodeRegions)
{
PatternOccurrences.push_back(CodeReg->GetPatternOccurrence());
}
/* Clean the list from duplicates if the parameter is set */
if (MakeUnique)
{
PatternOccurrences = SetAlgorithms::GetUniquePatternOccList(PatternOccurrences);
}
return PatternOccurrences;
}
/**
* @brief A tree operation that marks every tree node with the label corresponding to its connected component.
* Calls GraphAlgorithms::MarkConnectedComponents(PatternGraphNode*, int).
*/
void GraphAlgorithms::MarkConnectedComponents()
{
std::vector<PatternCodeRegion*> PatternCodeRegs = PatternGraph::GetInstance()->GetAllPatternCodeRegions();
int ConnectedComponents = 0;
for (PatternCodeRegion* PatternCodeReg : PatternCodeRegs)
{
if (PatternCodeReg->GetConnectedComponent() == -1)
{
MarkConnectedComponents(PatternCodeReg, ConnectedComponents);
ConnectedComponents++;
}
}
}
/**
* @brief Marks every tree node with label corresponding to connected component
*
* @param Node The current node.
* @param ComponentID ID of the connected component.
**/
void GraphAlgorithms::MarkConnectedComponents(PatternGraphNode* Node, int ComponentID)
{
if (Node->GetConnectedComponent() == -1)
{
Node->SetConnectedComponent(ComponentID);
for (PatternGraphNode* Child : Node->GetChildren())
{
MarkConnectedComponents(Child, ComponentID);
}
for (PatternGraphNode* Parent : Node->GetParents())
{
MarkConnectedComponents(Parent, ComponentID);
}
}
}
/**
* @brief Finds the parent patterns, beginning from a PatternCodeRegion.
* Saves the parent patterns in the list of PatternOccurrence passed as second parameter.
*
* @param Start Initial PatternCodeRegion from which the search is started.
* @param Parents Reference to a std::vector of PatternOccurrence* in which the encountered occurrences are saved.
* @param maxdepth Maximum depth of the recursion.
**/
void GraphAlgorithms::FindParentPatternCodeRegions(PatternCodeRegion* Start, std::vector<PatternCodeRegion*>& Parents, int maxdepth)
{
int a = 0;
int* p = &a;
FindNeighbourPatternCodeRegionss(Start, Parents, DIR_Parents, p, maxdepth);
}
/**
* @brief Finds the child patterns from a starting point. See GraphAlgorithms::FindParentPatternCodeRegions().
**/
void GraphAlgorithms::FindChildPatternCodeRegions(PatternCodeRegion* Start, std::vector<PatternCodeRegion*>& Children, int maxdepth)
{
int a = 0;
int* p = &a;
FindNeighbourPatternCodeRegionss(Start, Children, DIR_Children, p, maxdepth);
}
/**
* @brief Core functionality for finding neightbouring PatternCodeRegions of a PatternGraphNode (in this case: PatternCodeRegion).
* The direction of the recursive descent is passed as a parameter.
*
* @param Current The starting point of the recursive descent.
* @param Results Vector of PatternCodeRegion neighbours.
* @param dir The search direction (Children or parents)
* @param depth The current depth of recursion.
* @param maxdepth The maximum recursion depth.
**/
void GraphAlgorithms::FindNeighbourPatternCodeRegions(PatternGraphNode* Current, std::vector<PatternCodeRegion*>& Results, GraphSearchDirection dir, int depth, int maxdepth)
{
/* Check, if we reached the maximum depth */
if (depth >= maxdepth)
{
return;
}
PatternCodeRegion* PatternReg = clang::dyn_cast<PatternCodeRegion>(Current);
if (depth > 0 && PatternReg != NULL)
{
Results.push_back(PatternReg);
}
else
{
/* Get the neighbouring nodes depending on the defined search direction */
std::vector<PatternGraphNode*> Neighbours;
if (dir == DIR_Parents)
{
Neighbours = Current->GetParents();
}
else if (dir == DIR_Children)
{
Neighbours = Current->GetChildren();
}
/* Visit all the neighbouring nodes according to the given direction */
if (Neighbours.size() > 0 && Neighbours[0] != NULL && Neighbours[0] != 0){
for (PatternGraphNode* Neighbour : Neighbours)
{
FindNeighbourPatternCodeRegions(Neighbour, Results, dir, depth + 1, maxdepth);
}
}
}
return;
}
void GraphAlgorithms::FindNeighbourPatternCodeRegionss(PatternGraphNode* Current, std::vector<PatternCodeRegion*>& Results, GraphSearchDirection dir, int* depth, int maxdepth)
{
/* Check, if we reached the maximum depth */
if (*depth >= maxdepth)
{
return;
}
PatternCodeRegion* PatternReg = clang::dyn_cast<PatternCodeRegion>(Current);
if (*depth > 0 && PatternReg != NULL)
{
Results.push_back(PatternReg);
}
else
{
/* Get the neighbouring nodes depending on the defined search direction */
std::vector<PatternGraphNode*> Neighbours;
if (dir == DIR_Parents)
{
Neighbours = Current->GetParents();
}
else if (dir == DIR_Children)
{
Neighbours = Current->GetChildren();
}
/* Visit all the neighbouring nodes according to the given direction */
if (Neighbours.size() > 0 && Neighbours[0] != NULL && Neighbours[0] != 0){
for (PatternGraphNode* Neighbour : Neighbours)
{
*depth = *depth + 1;
FindNeighbourPatternCodeRegionss(Neighbour, Results, dir, depth , maxdepth);
}
}
}
return;
}
/**
* @brief Remove duplicates from a list of PatternOccurrence.
* The criterion is defined by PatternOccurrence::Equals().
*
* @param PatternOccs List of PatternOccurrence objects containing duplicates.
*
* @return List of PatternOccurrence objects free from duplicates.
**/
std::vector<PatternOccurrence*> SetAlgorithms::GetUniquePatternOccList(std::vector<PatternOccurrence*> PatternOccs)
{
std::vector<PatternOccurrence*> Res;
for (PatternOccurrence* PatternOcc : PatternOccs)
{
/* Search the pattern list, whether this is a duplicate */
bool duplicate = false;
for (PatternOccurrence* ResOcc : Res)
{
if (PatternOcc == ResOcc || PatternOcc->Equals(ResOcc))
{
duplicate = true;
break;
}
}
if (!duplicate)
{
Res.push_back(PatternOcc);
}
}
return Res;
}
/**
* @brief Removes duplicates from the input set.
* The criterion is HPCParallelPattern::Equals().
*
* @param Patterns Input set.
*
* @return Set free of duplicates.
**/
std::vector<HPCParallelPattern*> SetAlgorithms::GetUniquePatternList(std::vector<HPCParallelPattern*> Patterns)
{
std::vector<HPCParallelPattern*> Res;
for (HPCParallelPattern* Pattern : Patterns)
{
bool duplicate = false;
/* Check, if the pattern has already been added to the output set */
for (HPCParallelPattern* Pattern2 : Res)
{
if (Pattern->Equals(Pattern2))
{
duplicate = true;
break;
}
}
if (!duplicate)
{
Res.push_back(Pattern);
}
}
return Res;
}