-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommatrix.cpp
More file actions
405 lines (365 loc) · 12 KB
/
commatrix.cpp
File metadata and controls
405 lines (365 loc) · 12 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
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
/*
__ __ ______ _______ _______ ______ ________
/ \ / | / \ / \ / \ / \ / |
$$ \ /$$ |/$$$$$$ |$$$$$$$ |$$$$$$$ |/$$$$$$ |$$$$$$$$/
$$$ \ /$$$ |$$ | $$/ $$ |__$$ |$$ |__$$ |$$ | $$ |$$ |__
$$$$ /$$$$ |$$ | $$ $$/ $$ $$< $$ | $$ |$$ |
$$ $$ $$/$$ |$$ | __ $$$$$$$/ $$$$$$$ |$$ | $$ |$$$$$/
$$ |$$$/ $$ |$$ \__/ |$$ | $$ | $$ |$$ \__$$ |$$ |
$$ | $/ $$ |$$ $$/ $$ | $$ | $$ |$$ $$/ $$ |
$$/ $$/ $$$$$$/ $$/ $$/ $$/ $$$$$$/ $$/
A Memory and Communication Profiler
* This file is a part of MCPROF.
* https://bitbucket.org/imranashraf/mcprof
*
* Copyright (c) 2014-2016 TU Delft, The Netherlands.
* All rights reserved.
*
* MCPROF is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MCPROF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MCPROF. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Imran Ashraf
*
*/
#include "commatrix.h"
#include "counters.h"
extern map <string,IDNoType> FuncName2ID;
extern bool ShowUnknown;
extern u32 Threshold;
Matrix2D::Matrix2D()
{
IDNoType cols=DEFAULT_SIZE;
IDNoType rows=DEFAULT_SIZE;
CommValType value=0;
D1ECHO("Default Comm Matrix Size = " << DEFAULT_SIZE);
Matrix.resize( cols , vector<CommValType>( rows , value) );
}
Matrix2D::Matrix2D(IDNoType size)
{
IDNoType cols=size;
IDNoType rows=size;
CommValType value=0;
Matrix.resize( cols , vector<CommValType>( rows , value) );
}
CommValType Matrix2D::MaxCommunication(u16 StartID)
{
CommValType currmax=0;
// Following iterations can be optimized to TotalSymbols instead of size
for (IDNoType p=StartID; p<Matrix.size(); p++)
{
for (IDNoType c=StartID; c<Matrix.size(); c++)
{
currmax = std::max(currmax, Matrix[p][c]);
}
}
return currmax;
}
void Matrix2D::Print(ostream &fout)
{
IDNoType TotalSymbols = symTable.TotalSymbolCount();
CHECK_LT(TotalSymbols, Matrix.size());
for (IDNoType p=0; p<TotalSymbols; p++)
{
for (IDNoType c=0; c<TotalSymbols; c++)
{
fout << setw(12) << Matrix[p][c] <<" ";
}
fout<<endl;
}
}
void Matrix2D::UpdateEmptyRowsCols(IDNoType StartID, IDNoType EndID)
{
for (IDNoType p=StartID; p<EndID; p++)
{
CommValType rowsum=0;
for (IDNoType c=StartID; c<EndID; c++)
{
CommValType comm = Matrix[p][c];
if ( comm > Threshold )
rowsum+=comm;
}
if( rowsum == 0 )
FilledRows.insert(p);
}
for (IDNoType c=StartID; c<EndID; c++)
{
CommValType colsum=0;
for (IDNoType p=StartID; p<EndID; p++)
{
CommValType comm = Matrix[p][c];
if ( comm > Threshold )
colsum+=comm;
}
if( colsum == 0 )
FilledCols.insert(c);
}
}
bool Matrix2D::IsFilledRow(IDNoType r)
{
return !(FilledRows.find(r) != FilledRows.end() );
}
bool Matrix2D::IsFilledCol(IDNoType c)
{
return !(FilledCols.find(c) != FilledCols.end() );
}
// Use the following for properly aligned matrix print for visual inspection
// This can be problematic of width not set properly to be processed by gnuplot script
// #define ALIGNMENT (setw(25))
// Use the following to print tabs which will not be visually appealing but it will
// generate the columns properly for further processing by other tools
#define ALIGNMENT (" ")
void Matrix2D::PrintMatrix()
{
std::ofstream mout;
string matrixFileName("matrix.dat");
OpenOutFile(matrixFileName, mout);
ECHO("Printing communication matrix in " << matrixFileName);
IDNoType TotalSymbols = symTable.TotalSymbolCount();
CHECK_LT(TotalSymbols, Matrix.size());
u16 StartID;
if(ShowUnknown)
StartID = 0; //use this if you want to print unknown
else
StartID = 1; //use this if you dont want to print unknown
// first update the map which contains the filled rows and columns
UpdateEmptyRowsCols(StartID, TotalSymbols);
mout << ALIGNMENT << " ";
for (IDNoType c=StartID; c<TotalSymbols; c++)
{
if( IsFilledCol(c) )
{
mout << ALIGNMENT << symTable.GetSymName(c);
}
}
mout << endl;
for (IDNoType p=StartID; p<TotalSymbols; p++)
{
if( IsFilledRow(p) )
{
mout << ALIGNMENT << symTable.GetSymName(p);
for (IDNoType c=StartID; c<TotalSymbols; c++)
{
if( IsFilledCol(c) )
{
mout << ALIGNMENT << Matrix[p][c];
}
}
mout<<endl;
}
}
mout.close();
}
#undef ALIGNMENT
// printing of matrix as simple task dependences
void Matrix2D::PrintDependenceMatrix()
{
string depFileName("taskdependences.dat");
ECHO("Printing dependences in " << depFileName);
std::ofstream depout;
OpenOutFile(depFileName, depout);
IDNoType TotalSymbols = symTable.TotalSymbolCount();
depout << "# producer consumer communication " << endl;
for (IDNoType pid=0; pid<TotalSymbols; pid++)
{
string prod = symTable.GetSymName(pid);
for (IDNoType cid=0; cid<TotalSymbols; cid++)
{
string cons = symTable.GetSymName(cid);
if( Matrix[pid][cid] > 0 )
depout << prod << " "<< cons << " " << Matrix[pid][cid] << endl;
}
}
depout.close();
}
void Matrix2D::PrintDot()
{
std::ofstream dotout;
string dotFileName("communication.dot");
OpenOutFile(dotFileName, dotout);
ECHO("Printing communication graph in dot format in " << dotFileName);
u16 TotalSymbols = GlobalID;
D1ECHO( VAR(TotalSymbols) );
CHECK_LT(TotalSymbols, Matrix.size());
dotout << "digraph {\ngraph [];"
<< "\nedge [fontsize=18, arrowhead=vee, arrowsize=0.5];"
<< endl;
string objNodeStyle(" fontcolor=black, shape=box, fontsize=22");
string ftnNodeStyle(" fontcolor=black, style=filled, fontsize=22");
u16 StartID;
if(ShowUnknown)
StartID = 0; // as 0 is reserved for unknown id
else
StartID = 1;
CommValType maxComm = MaxCommunication(StartID);
#if (FUNCTION_ORDER == ORDERED)
// this sorting of functions is done in the order of insertion to map
// to print them in order in the graph
vector<IDNoType> allFunIDs;
map<IDNoType,string> ID2FunctionName;
map<string,IDNoType>::iterator iter1;
for( iter1 = FuncName2ID.begin(); iter1 != FuncName2ID.end(); iter1++)
{
const string& symname = iter1->first;
IDNoType& fid = iter1->second;
allFunIDs.push_back(fid); // insert in vector
ID2FunctionName[fid] = symname; // make reverse map
}
//sort vector
sort(allFunIDs.begin(), allFunIDs.end());
// now print functions in order
vector<IDNoType>::iterator iter2;
for( iter2 = allFunIDs.begin(); iter2 != allFunIDs.end(); iter2++)
{
IDNoType& fid = *iter2;
const string& symname = ID2FunctionName[fid];
if( !symname.empty() && fid >= StartID )
{
dotout << "\"" << (u16)fid << "\""
<< " [label=\" " << symname
<< " \\n" << GetInstrCountPercent(fid) << "%"
<< ", " << GetCallCount(fid) << "\""
<< ftnNodeStyle
<< "];" << endl;
}
}
#endif
// now print the objects
for (u16 c=StartID; c<TotalSymbols; c++)
{
string symname = symTable.GetSymName(c);
if( !symname.empty() )
{
if ( symTable.SymIsObj(c) )
{
dotout << "\"" << (u16)c << "\"" << " [label=\" " << symname
// << " \\n" << hBytes(symTable.GetTotalSymSize(c)) << "\""
<< " \\n" << hBytes(symTable.GetSymSize(c)) << "\""
<< objNodeStyle
<< "];" << endl;
}
#if (FUNCTION_ORDER == UNORDERED)
else
{
dotout << "\"" << (u16)c << "\""
<< " [label=\" " << symname
<< " \\n" << GetInstrCountPercent(c) << "%"
<< ", " << GetCallCount(c) << "\""
<< ftnNodeStyle
<< "];" << endl;
}
#endif
}
}
int color;
// now print the edges
for (u16 p=StartID; p<TotalSymbols; p++)
{
for (u16 c=StartID; c<TotalSymbols; c++)
{
CommValType comm = Matrix[p][c];
if( comm > Threshold )
{
color = (int) ( 1023 * log( (double)(comm) ) / log( (double)maxComm ) );
dotout << dec
<< "\"" << (u16)p << "\""
<< "->"
<< "\"" << (u16)c << "\""
<< "[ label=\""
<< hBytes(comm) <<"\""
<< " color = \"#"
<< hex
<< setw(2) << setfill('0') << max(0, color-768)
<< setw(2) << setfill('0') << min(255, 512-(int)abs(color-512))
<< setw(2) << setfill('0') << max(0, min(255,512-color))
<< "\""
<< "]"
<< endl;
}
}
}
dotout << "}" << endl;
dotout.close();
}
void Matrix2D::PrintGraph()
{
std::ofstream gfout;
string graphFileName("communication_graph.dat");
OpenOutFile(graphFileName, gfout);
ECHO("Printing communication as text file for filtering in " << graphFileName);
u16 TotalSymbols = GlobalID;
D1ECHO( VAR(TotalSymbols) );
CHECK_LT(TotalSymbols, Matrix.size());
u16 StartID;
if(ShowUnknown)
StartID = 0; // as 0 is reserved for unknown id
else
StartID = 1;
// print objects and functions
for (u16 c=StartID; c<TotalSymbols; c++)
{
string symname = symTable.GetSymName(c);
if( !symname.empty() )
{
if ( symTable.SymIsObj(c) )
{
gfout << "o;" << (u16)c
<< ";" << symname
// << ";" << symTable.GetTotalSymSize(c)
<< ";" << symTable.GetSymSize(c)
<< endl;
}
else
{
gfout << "f;" << (u16)c
<< ";" << symname
<< ";" << GetInstrCount(c)
<< ";" << GetCallCount(c)
<< endl;
}
}
}
// now print the edges
for (u16 p=StartID; p<TotalSymbols; p++)
{
for (u16 c=StartID; c<TotalSymbols; c++)
{
CommValType comm = Matrix[p][c];
// if( comm > Threshold )
if( comm > 0 )
{
gfout << "e;" << (u16)p
<< ";" << (u16)c
<< ";" << comm
<< endl;
}
}
}
gfout.close();
}
bool Matrix2D::CheckLoopIndependence(u32 nIterations)
{
bool result=true;
for (IDNoType pid=1; pid<nIterations-1; ++pid) // iterations start from 1
{
for (IDNoType cid=pid+1; cid<nIterations; ++cid)
{
if( Matrix[pid][cid] > 0 )
{
D2ECHO( pid << " "<< cid << " " << Matrix[pid][cid] );
result = false;
break;
}
}
}
return result;
}