-
Notifications
You must be signed in to change notification settings - Fork 3
/
HPCParallelPattern.cpp
435 lines (378 loc) · 10.8 KB
/
HPCParallelPattern.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "HPCParallelPattern.h"
#include <iostream>
#include "clang/AST/ODRHash.h"
#ifndef HPCERROR_H
#include "HPCError.h"
#endif
//#define PRINT_ONLYPATTERNDENUG
/*
* HPC Parallel Pattern Class Functions
*/
HPCParallelPattern::HPCParallelPattern(DesignSpace DesignSp, std::string PatternName)
{
this->DesignSp = DesignSp;
this->PatternName = PatternName;
this->Occurrences = std::vector<PatternOccurrence*>();
}
/**
* @brief Prints design space, pattern name and number of occurrences.
**/
void HPCParallelPattern::Print()
{
std::cout << "Pattern Info" << std::endl;
std::cout << "Pattern Design Space: " << DesignSpaceToStr(this->DesignSp) << std::endl;
std::cout << "Pattern Name: " << this->PatternName << std::endl;
std::cout << this->Occurrences.size() << " Occurrences." << std::endl;
}
/**
* @brief Like Print() but output is only a single line.
**/
void HPCParallelPattern::PrintShort()
{
std::cout << "\033[33m" << DesignSpaceToStr(this->DesignSp) << "\033[0m" << this->PatternName;
}
void HPCParallelPattern::AddOccurrence(PatternOccurrence* Occurrence)
{
this->Occurrences.push_back(Occurrence);
}
/**
* @brief Returns the sum of lines of code from all pattern occurrences.
*
* @return Sum of lines of code.
**/
int HPCParallelPattern::GetTotalLinesOfCode()
{
int LOC = 0;
for (PatternOccurrence* PatternOcc : this->Occurrences)
{
LOC += PatternOcc->GetTotalLinesOfCode();
}
return LOC;
}
/**
* @brief Compares two patterns for design space and pattern name.
*
* @return True if equal, else false.
**/
bool HPCParallelPattern::Equals(HPCParallelPattern* Pattern)
{
if (this->DesignSp == Pattern->GetDesignSpace() && !this->PatternName.compare(Pattern->GetPatternName()))
{
return true;
}
return false;
}
std::vector<PatternOccurrence*> HPCParallelPattern::GetOccurrences() {
if(!this->Occurrences.empty()){
return this->Occurrences;
}
else{
return std::vector<PatternOccurrence*>();
}
}
/**
* @brief Get all code regions from all pattern occurrences.
*
* @return Return pointers to all PatternCodeRegion objects from all PatternOccurrence objects.
**/
std::vector<PatternCodeRegion*> HPCParallelPattern::GetCodeRegions()
{
std::vector<PatternCodeRegion*> CodeRegions = std::vector<PatternCodeRegion*>();
for (PatternOccurrence* PatternOcc : this->GetOccurrences())
{
for (PatternCodeRegion* CodeReg : PatternOcc->GetCodeRegions())
{
CodeRegions.push_back(CodeReg);
}
}
return CodeRegions;
}
void HPCParallelPattern::incrementNumOfOperators(){
this->numOfOperators++;
}
int HPCParallelPattern::GetNumOfOperators(){
return this->numOfOperators;
}
/*
* Pattern Occurrence Class Functions
*/
PatternOccurrence::PatternOccurrence(HPCParallelPattern* Pattern, std::string ID)
{
this->Pattern = Pattern;
this->ID = ID;
}
/**
* @brief Get the lines of code for all PatternCodeRegion objects registered with this PatternOccurrence.
*
* @return Sum of lines of code.
**/
int PatternOccurrence::GetTotalLinesOfCode()
{
int LOC = 0;
for (PatternCodeRegion* CodeReg : this->CodeRegions)
{
LOC += *((CodeReg->getCorrespondingCallTreeNodes())->front())->getLOCTillPatternEnd();
#ifdef LOCDEBUG
for(CallTreeNode* Node : *getCorrespondingCallTreeNodes()){
std::cout << "FOR "<< Node->GetID()<< "printing" << '\n';
std::cout << Node->getLOCTillPatternEnd() << '\n';
}
#endif
}
return LOC;
}
/**
* @brief Compare a PatternOccurrence object with this object. The ID and the underlying HPCParallelPattern are compared.
*
* @param PatternOcc The PatternOccurrence object to compare with.
*
* @return True if equal, false elsewise.
**/
bool PatternOccurrence::Equals(PatternOccurrence* PatternOcc)
{
if (!this->ID.compare(PatternOcc->GetID()) && this->Pattern->Equals(PatternOcc->GetPattern()))
{
return true;
}
return false;
}
/**
* @brief Prints the ID of this pattern occurrence as well as all information from HPCParallelPattern::Print().
**/
void PatternOccurrence::Print()
{
this->Pattern->Print();
std::cout << this->GetID() << std::endl;
}
/*
* Pattern Code Region Class Functions
*/
PatternCodeRegion::~PatternCodeRegion(){std::cout << "Deleted CodeRegion" << '\n';}
PatternCodeRegion::PatternCodeRegion(PatternOccurrence* PatternOcc) : PatternGraphNode(GNK_Pattern), Parents(), Children()
{
this->PatternOcc = PatternOcc;
}
void PatternCodeRegion::AddChild(PatternGraphNode* Child)
{
/*Before we add a child we look if this child has not been registered already
*/
if(PatternCodeRegion* ChildCodeReg = clang::dyn_cast<PatternCodeRegion>(Child))
{
for(PatternGraphNode* PatFor : this->Children){
if(PatternCodeRegion* PatRegFor = clang::dyn_cast<PatternCodeRegion>(PatFor))
{
if(PatRegFor->GetID()== ChildCodeReg->GetID()) return;
}
}
}
else
{
FunctionNode* ChildFunc = clang::dyn_cast<FunctionNode>(Child);
for(PatternGraphNode* PatFor : this->Children){
if(FunctionNode* PatFuncFor = clang::dyn_cast<FunctionNode>(PatFor))
{
if(PatFuncFor->GetHash() == ChildFunc->GetHash()) return;
}
}
}
Children.push_back(Child);
}
void PatternCodeRegion::AddParent(PatternGraphNode* Parent)
{
/*Before we add a child we look if this child has not been registered already
*/
if(PatternCodeRegion* ParentCodeReg = clang::dyn_cast<PatternCodeRegion>(Parent))
{
for(PatternGraphNode* PatFor : this->Parents){
if(PatternCodeRegion* PatRegFor = clang::dyn_cast<PatternCodeRegion>(PatFor))
{
if(PatRegFor->GetID()== ParentCodeReg->GetID()) return;
}
}
}
else
{
FunctionNode* ParentFunc = clang::dyn_cast<FunctionNode>(Parent);
for(PatternGraphNode* PatFor : this->Parents){
if(FunctionNode* PatFuncFor = clang::dyn_cast<FunctionNode>(PatFor))
{
if(PatFuncFor->GetHash() == ParentFunc->GetHash()) return;
}
}
}
Parents.push_back(Parent);
}
void PatternCodeRegion::AddOnlyPatternChild(PatternGraphNode* PatChild)
{/*Before we add a child we look if this child has not been registered already
*/
PatternCodeRegion* PatternChild = clang::dyn_cast<PatternCodeRegion>(PatChild);
for(PatternCodeRegion* PatRegFor : this->PatternChildren){
if(PatRegFor->GetID() == PatternChild->GetID()) return;
}
this->PatternChildren.push_back(PatternChild);
}
void PatternCodeRegion::AddOnlyPatternParent(PatternGraphNode* PatParent)
{/*Before we add a parent we look if this parent has not been registered already
*/
PatternCodeRegion* PatternParent = clang::dyn_cast<PatternCodeRegion>(PatParent);
for(PatternCodeRegion* PatRegFor : this->PatternParents){
if(PatRegFor->GetID() == PatternParent->GetID()) return;
}
this->PatternParents.push_back(PatternParent);
}
/**
* @brief Save the first line of the code region to keep track of the lines of code.
**/
void PatternCodeRegion::SetFirstLine(int FirstLine)
{
this->LinesOfCode = FirstLine;
}
/**
* @brief See PatternCodeRegion::SetFirstLine.
**/
void PatternCodeRegion::SetLastLine(int LastLine)
{
this->LinesOfCode = (LastLine - this->LinesOfCode) - 1;
}
void PatternCodeRegion::SetStartSourceLoc(clang::SourceLocation StartLoc)
{
this->StartSLocation = StartLoc;
}
void PatternCodeRegion::SetEndSourceLoc(clang::SourceLocation EndLoc){
this->EndSLocation = EndLoc;
}
/**
* @brief Print the lines of code plus all information from PatternOccurrence::Print().
**/
void PatternCodeRegion::Print()
{
this->PatternOcc->Print();
std::cout << this->GetLinesOfCode() << " lines of code." << std::endl;
}
clang::SourceLocation PatternCodeRegion::GetStartLoc(){
return this->StartSLocation;
}
clang::SourceLocation PatternCodeRegion::GetEndLoc(){
return this->EndSLocation;
}
bool PatternCodeRegion::HasNoPatternParents(){
if(this->PatternParents.size()){
return false;
}
return true;
}
bool PatternCodeRegion::HasNoPatternChildren(){
if(this->PatternChildren.size()){
return false;
}
return true;
}
void PatternCodeRegion::PrintVecOfPattern(std::vector<PatternCodeRegion*> RegionVec){
for(PatternCodeRegion* CodeReg : RegionVec){
HPCParallelPattern* Pattern = CodeReg->GetPatternOccurrence()->GetPattern();
std::cout << "\033[36m" << Pattern->GetDesignSpaceStr() << ":\33[33m " << Pattern->GetPatternName() << "\33[0m";
std::cout << "(" << CodeReg->GetPatternOccurrence()->GetID() << ")" << std::endl;
}
}
/*
* Pattern Stack Management
*/
std::vector<PatternCodeRegion*> PatternContext;
std::vector<PatternCodeRegion*> OnlyPatternContext;
/**
* @brief Add a PatternCodeRegion to the top of the pattern context stack.
*
* @param PatternReg Code Region to be placed on the stack.
**/
void AddToPatternStack(PatternCodeRegion* PatternReg)
{
PatternContext.push_back(PatternReg);
}
void AddToOnlyPatternStack(PatternCodeRegion* PatternReg)
{
OnlyPatternContext.push_back(PatternReg);
}
/**
* @brief Get the top of the pattern context stack.
*
* @return Top PatternCodeRegion or NULL if stack is empty.
**/
PatternCodeRegion* GetTopPatternStack()
{
if (!PatternContext.empty())
{
return PatternContext.back();
}
return NULL;
}
PatternCodeRegion* GetTopOnlyPatternStack(){
if (!OnlyPatternContext.empty())
{
return OnlyPatternContext.back();
}
return NULL;
}
/**
* @brief Remove top of the pattern context from the stack if the ID matches with the function input. Prints an error message if not.
*
* @param ID The suspected ID of the pattern context top.
**/
void RemoveFromPatternStack(std::string ID)
{
if (!PatternContext.empty())
{
try{
int i = 0;
for(PatternCodeRegion* PatCodeReg : PatternContext){
if (!ID.compare(PatCodeReg->GetID()))
{
PatternContext.erase(PatternContext.begin()+i);
return;
}
i++;
}
throw TooManyEndsException(ID);
}
catch(TooManyEndsException& endsExeption){
endsExeption.what();
throw TerminateEarlyException();
}
}
}
void RemoveFromOnlyPatternStack(std::string ID){
if(!OnlyPatternContext.empty())
{
// we need to compare if the ID is the same as the ID of the Pattern that we inserted first in the stack
/*usually the WrongNestingException is encountered before this function*/
try{
int i = 0;
for(PatternCodeRegion* PatCodeReg : OnlyPatternContext){
if (!ID.compare(PatCodeReg->GetID()))
{
OnlyPatternContext.erase(OnlyPatternContext.begin()+i);
return;
}
i++;
}
throw WrongNestingException(ID, ID);
}
catch(WrongNestingException& wrongNest){
std::cout << wrongNest.what() << std::endl;
throw TerminateEarlyException();
}
}
else{
//std::cout << "You probably added one end of a patten to much.\n" << ID << " ends outside of any Pattern." << std::endl;
}
}
PatternCodeRegion* PatternIDisUsed(std::string ID){
std::vector<PatternCodeRegion*> PatternCodeRegions = PatternGraph::GetInstance()->GetAllPatternCodeRegions();
for(PatternCodeRegion* PatCodeReg : PatternCodeRegions){
if(!(ID.compare(PatCodeReg->GetID()))){
return PatCodeReg;
}
}
return NULL;
}
/*Stack for Halstead */
std::vector<PatternOccurrence*> OccStackForHalstead;