-
Notifications
You must be signed in to change notification settings - Fork 0
/
geckoHierarchicalTree.cpp
174 lines (139 loc) · 4.47 KB
/
geckoHierarchicalTree.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
#include "geckoHierarchicalTree.h"
#include <algorithm>
unordered_map<string, GeckoLocation*> GeckoLocation::geckoListOfAllNodes;
vector<GeckoLocation*> GeckoLocation::childrenInCategories[GECKO_DEVICE_LEN];
bool GeckoLocation::treeHasBeenModified;
vector<GeckoLocation*> GeckoLocation::finalChildListForThreads;
GeckoLocation::GeckoLocation(string locationName, GeckoLocation *parent, GeckoLocationType locationObj,
int locIndex, int async_id) {
this->locationName = locationName;
this->locationObj = locationObj;
this->parent = parent;
this->locationIndex = locIndex;
this->async_id = async_id;
geckoListOfAllNodes[locationName] = this;
}
GeckoLocation::~GeckoLocation() {
//#ifdef INFO
// fprintf(stderr, "===GECKO: Destructor on '%s' is called.\n", this->locationName.c_str());
//#endif
// auto iter = geckoListOfAllNodes.find(this->locationName);
// if(iter != geckoListOfAllNodes.end())
// geckoListOfAllNodes.erase(iter);
}
void GeckoLocation::appendChild(GeckoLocation *location) {
if(std::find(children.begin(), children.end(), location) != children.end())
return;
treeHasBeenModified = true;
children.push_back(location);
GeckoLocationArchTypeEnum type = location->getLocationType().type;
childrenInCategories[type].push_back(location);
}
void GeckoLocation::removeChild(GeckoLocation *location) {
auto iter = std::find(children.begin(), children.end(), location);
if(iter == children.end())
return;
treeHasBeenModified = true;
children.erase(iter);
int type = (int) location->getLocationType().type;
vector<GeckoLocation *> &childCategory = childrenInCategories[type];
auto iter2 = std::find(childCategory.begin(), childCategory.end(), location);
if(iter2 != childCategory.end())
childCategory.erase(iter2);
}
// static
GeckoLocation *GeckoLocation::find(string name) {
auto iter = geckoListOfAllNodes.find(name);
if(iter != geckoListOfAllNodes.end())
return iter->second;
return NULL;
}
string GeckoLocation::getLocationName() {
return this->locationName;
}
GeckoLocationType GeckoLocation::getLocationType() {
return this->locationObj;
}
GeckoLocation *GeckoLocation::getParent() {
return parent;
}
void GeckoLocation::setParent(GeckoLocation *p) {
parent = p;
}
vector<GeckoLocation*> GeckoLocation::getChildren() {
return children;
}
int GeckoLocation::getLocationIndex() {
return locationIndex;
}
GeckoLocation *GeckoLocation::findRoot() {
if(geckoListOfAllNodes.size() == 0)
return NULL;
unordered_map<string, GeckoLocation*>::iterator iter = geckoListOfAllNodes.begin();
GeckoLocation *node = iter->second;
GeckoLocation *prev;
while(node) {
prev = node;
node = node->getParent();
}
return prev;
}
int GeckoLocation::getAsyncID() {
return async_id;
}
void GeckoLocation::setAsyncID(int id) {
async_id = id;
}
int GeckoLocation::getThreadID() {
return thread_id;
}
void GeckoLocation::setThreadID(int id) {
thread_id = id;
}
bool GeckoLocation::getAllLeavesOnce(int *numDevices) {
if(!treeHasBeenModified) {
*numDevices = finalChildListForThreads.size();
return false;
}
treeHasBeenModified = false;
finalChildListForThreads.clear();
const int listOfTypesCount = 3;
GeckoLocationArchTypeEnum listOfTypes[listOfTypesCount] = {GECKO_X32, GECKO_X64, GECKO_NVIDIA};
*numDevices = 0;
for(int devTypeIndex=0;devTypeIndex<listOfTypesCount; devTypeIndex++) {
*numDevices += childrenInCategories[listOfTypes[devTypeIndex]].size();
}
#ifdef INFO
fprintf(stderr, "===GECKO: Found %d leaves\n", *numDevices);
#endif
if(*numDevices == 0)
return true;
#ifdef INFO
int child_index = 0;
#endif
for(int devTypeIndex=0;devTypeIndex<listOfTypesCount; devTypeIndex++) {
vector<GeckoLocation *> &childCategory = childrenInCategories[listOfTypes[devTypeIndex]];
int sz = childCategory.size();
for(int i=0;i<sz;i++) {
finalChildListForThreads.push_back(childCategory[i]);
#ifdef INFO
fprintf(stderr, "===GECKO: \tLeaf %d is %s\n", child_index++, childCategory[i]->getLocationName().c_str());
#endif
}
}
return true;
}
vector<GeckoLocation*> &GeckoLocation::getChildListForThreads() {
return finalChildListForThreads;
}
unordered_map<string, GeckoLocation*> GeckoLocation::getAllLocations() {
return geckoListOfAllNodes;
}
void GeckoLocation::dumpTable() {
auto iter = geckoListOfAllNodes.begin();
fprintf(stderr, "===GECKO: Dump location table:\n");
for(;iter != geckoListOfAllNodes.end(); iter++) {
fprintf(stderr, "===\t\t%s at %p\n", iter->first.c_str(), iter->second);
}
fprintf(stderr, "===GECKO: Dump location table ... Done\n");
}