-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsparsematrix.h
156 lines (145 loc) · 4.52 KB
/
sparsematrix.h
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
/*
__ __ ______ _______ _______ ______ ________
/ \ / | / \ / \ / \ / \ / |
$$ \ /$$ |/$$$$$$ |$$$$$$$ |$$$$$$$ |/$$$$$$ |$$$$$$$$/
$$$ \ /$$$ |$$ | $$/ $$ |__$$ |$$ |__$$ |$$ | $$ |$$ |__
$$$$ /$$$$ |$$ | $$ $$/ $$ $$< $$ | $$ |$$ |
$$ $$ $$/$$ |$$ | __ $$$$$$$/ $$$$$$$ |$$ | $$ |$$$$$/
$$ |$$$/ $$ |$$ \__/ |$$ | $$ | $$ |$$ \__$$ |$$ |
$$ | $/ $$ |$$ $$/ $$ | $$ | $$ |$$ $$/ $$ |
$$/ $$/ $$$$$$/ $$/ $$/ $$/ $$$$$$/ $$/
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
*
*/
#ifndef SPARSEMATRIX_H
#define SPARSEMATRIX_H
#include "globals.h"
#include <iostream>
#include <map>
using namespace std;
class SparseMatrix
{
private:
std::map<std::pair<int, int>, float> matrix;
public:
SparseMatrix(){}
void Insert(int r, int c, float val)
{
std::pair<int, int> cord = make_pair(r,c);
matrix[cord] = val;
}
void inline RecordCommunication(int r, int c, float val)
{
std::pair<int, int> cord = make_pair(r,c);
auto it = matrix.find(cord);
if( it != matrix.end() )
{
it->second = it->second + val;
}
else
{
matrix[cord] = val;
}
}
void Clear()
{
matrix.clear();
}
bool 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)
{
std::pair<int, int> cord = make_pair(pid,cid);
auto it = matrix.find(cord);
if( it != matrix.end() )
{
if( it->second > 0 )
{
result = false;
break;
}
}
}
}
return result;
}
void PrintMatrix(int maxDim)
{
#define ALIGNMENT setw(8)
std::ofstream mout;
string matrixFileName("loopdependences.dat");
OpenOutFile(matrixFileName, mout);
D2ECHO("Printing loop dependences matrix as table in " << matrixFileName);
mout << ALIGNMENT << "#";
for (IDNoType c=1; c<maxDim; c++)
{
mout << ALIGNMENT << c;
}
mout << endl;
for(int r=1; r<maxDim; r++)
{
mout << ALIGNMENT << r;
for(int c=1; c<maxDim; c++)
{
std::pair<int, int> cord = make_pair(r,c);
auto it = matrix.find(cord);
if( it != matrix.end() )
{
mout << ALIGNMENT << it->second;
}
else
{
mout << ALIGNMENT << 0;
}
}
mout << endl;
}
mout.close();
#undef ALIGNMENT
}
void PrintMatrixAsSparse(int maxDim)
{
std::ofstream mout;
string matrixFileName("loopdependences.dat");
OpenOutFile(matrixFileName, mout);
D2ECHO("Printing loop dependences matrix as sparse representation in " << matrixFileName);
for(int r=1; r<maxDim; r++)
{
for(int c=1; c<maxDim; c++)
{
std::pair<int, int> cord = make_pair(r,c);
auto it = matrix.find(cord);
if( it != matrix.end() )
{
mout << r << " " << c << " " << it->second << endl;
}
}
}
mout.close();
#undef ALIGNMENT
}
};
#endif