-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComm.cpp
More file actions
321 lines (217 loc) · 6.63 KB
/
Comm.cpp
File metadata and controls
321 lines (217 loc) · 6.63 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
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
#include "Comm.h"
int N=100;
string RANKVAR;
map<string,Condition> VarCondMap;
string MPI_FILE_NAME="MPIProtocol";
/////////////////////////////////////
int LargestKnownRank=0;
bool IsGenericProtocol=true;
set<string> unboundVarList;
void updateLargestKnownRank(int num){
if(num > LargestKnownRank)
LargestKnownRank=num;
}
int getLFP(){return LargestKnownRank + 2;}
bool IsProtocolStable(){
return IsGenericProtocol && N >= getLFP();
}
/////////////////////////////////////
//some functions
int min(int a, int b){if(a<b) return a; else return b;}
int max(int a, int b){if(a<b) return b; else return a;}
int compute(string op, int operand1, int operand2){
if(op=="+")
return operand1+operand2;
if(op=="-")
return operand1-operand2;
if(op=="*")
return operand1*operand2;
if(op=="/")
return operand1/operand2;
if(op=="%")
return operand1%operand2;
if(op=="<<")
return operand1<<operand2;
if(op==">>")
return operand1>>operand2;
return -1;
}
bool areTheseTwoNumsAdjacent(int a, int b){
if(a==b || a==b+1 || a==b-1) return true;
if(a==0 && b==N-1) return true;
if(a==N-1 && b==0) return true;
if(a%N==(b-1+N)%N ||
a%N==(b+1)%N)
return true;
return false;
}
string convertIntToStr(int number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
string convertDoubleToStr(double number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
void writeToFile(string content){
ofstream outputFile("Debug.txt",
ios_base::out | ios_base::app);
outputFile <<"\n"<< content <<"\n";
}
void writeProtocol(string protocol){
ofstream outputFile("Protocol.txt",
ios_base::out | ios_base::trunc);
outputFile <<"\n"<< protocol <<"\n";
outputFile.close();
}
/********************************************************************/
//Class CommManager impl start ****
/********************************************************************/
CommManager::CommManager(CompilerInstance *ci0, int numOfProc0){
this->ci=ci0;
this->numOfProc=numOfProc0;
this->paramRoleNameMapping[WORLD]=new ParamRole();
}
void CommManager::insertVarName(string name){
this->varNames.push_back(name);
}
bool CommManager::isAVar(string name){
for (int i = 0; i < this->varNames.size(); i++)
{
if(varNames[i]==name)
return true;
}
return false;
}
//test whether a string contains a rank-related var name
bool CommManager::containsRankStr(string str){
for (auto &x:this->rankVarOffsetMapping)
{
string rankRelatedVarName=x.first;
size_t found = str.find(rankRelatedVarName);
if (found!=string::npos){
return true;
}
}
return false;
}
void CommManager::insertRankVarAndOffset(string varName, int offset){
if(this->isVarRelatedToRank(varName)){
this->rankVarOffsetMapping.find(varName)->second=offset;
}
else{
this->rankVarOffsetMapping[varName]=offset;
}
}
void CommManager::cancelRelation(string varName){
this->rankVarOffsetMapping.erase(this->rankVarOffsetMapping.find(varName));
}
bool CommManager::isVarRelatedToRank(string varName){
if(this->rankVarOffsetMapping.count(varName)>0)
return true;
else
return false;
}
void CommManager::insertRankVarAndCommGroupMapping(string rankVar0, string commGroup){
this->rankVarCommGroupMapping[rankVar0]=commGroup;
}
string CommManager::getCommGroup4RankVar(string rankVar0){
return this->rankVarCommGroupMapping[rankVar0];
}
void CommManager::insertNonRankVarAndCondtion(string nonRankVar, Condition cond){
if(this->nonRankVarAndStackOfCondMapping.count(nonRankVar)>0)
{
if (this->nonRankVarAndStackOfCondMapping[nonRankVar].size()>0)
{
Condition top=this->getTopCond4NonRankVar(nonRankVar);
cond=cond.AND(top);
}
cond.setNonRankVarName(nonRankVar);
this->nonRankVarAndStackOfCondMapping[nonRankVar].push(cond);
}
else{
stack<Condition> newStack;
cond.setNonRankVarName(nonRankVar);
newStack.push(cond);
this->nonRankVarAndStackOfCondMapping[nonRankVar]=newStack;
}
}
void CommManager::insertTmpNonRankVarCond(string nonRankVar, Condition cond){
if(this->tmpNonRankVarCondMap.count(nonRankVar)>0){
this->tmpNonRankVarCondMap[nonRankVar].push(cond);
}
else{
stack<Condition> newStack;
newStack.push(cond);
this->tmpNonRankVarCondMap[nonRankVar]=newStack;
}
}
void CommManager::clearTmpNonRankVarCondStackMap(){
this->tmpNonRankVarCondMap.clear();
}
map<string,stack<Condition>> CommManager::getTmpNonRankVarCondStackMap()
{
return tmpNonRankVarCondMap;
}
Condition CommManager::getTopCond4NonRankVar(string nonRankVar){
if(this->nonRankVarAndStackOfCondMapping.count(nonRankVar)>0){
if(this->nonRankVarAndStackOfCondMapping[nonRankVar].size()>0)
return this->nonRankVarAndStackOfCondMapping[nonRankVar].top();
else
return Condition(false);
}
else{return Condition(false);}
}
void CommManager::removeTopCond4NonRankVar(string nonRankVar){
if(this->nonRankVarAndStackOfCondMapping.count(nonRankVar)>0){
if(this->nonRankVarAndStackOfCondMapping[nonRankVar].size()>0){
this->nonRankVarAndStackOfCondMapping[nonRankVar].pop();
if(this->nonRankVarAndStackOfCondMapping[nonRankVar].size()==0)
this->nonRankVarAndStackOfCondMapping.erase(nonRankVar);
}
else
throw new MPI_TypeChecking_Error("try to pop from an empty stack of non-rank var condtion");
// this->nonRankVarAndStackOfCondMapping.erase(nonRankVar);
}
else{
throw new MPI_TypeChecking_Error("try to pop from a non-existing non-rank var condtion stack");
}
}
bool CommManager::hasAssociatedWithCondition(string varName){
if(VarCondMap.count(varName))
return true;
if(this->isVarRelatedToRank(varName))
return true;
if(this->nonRankVarAndStackOfCondMapping.count(varName)>0){
if(this->nonRankVarAndStackOfCondMapping[varName].size()>0)
return true;
}
return false;
}
int CommManager::getOffSet4RankRelatedVar(string varName){
if(this->rankVarOffsetMapping.count(varName)>0){
return rankVarOffsetMapping[varName];
}
else{
throw new MPI_TypeChecking_Error("The var "+varName+" is not related to rank but the method getOffSet4RankRelatedVar() is called");
}
}
map<string,ParamRole*> CommManager::getParamRoleMapping() const{
return this->paramRoleNameMapping;
}
ParamRole* CommManager::getParamRoleWithName(string name) const{
if (this->paramRoleNameMapping.count(name)>0)
{
return this->paramRoleNameMapping.at(name);
}
else{
return nullptr;
}
}
/********************************************************************/
//Class CommManager impl end ****
/********************************************************************/