/
SellCSigma.hpp
212 lines (175 loc) · 6.79 KB
/
SellCSigma.hpp
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
#ifndef SELLCSIGMA_HPP
#define SELLCSIGMA_HPP
#include <vector>
#include <tuple>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <cassert>
#include "MMreader.hpp"
/*****Class SELL-C-Sigam******************************************************/
class SellCSigma_Matrix
{
public:
SellCSigma_Matrix( MMreader mmMatrix, int C, int sigma ); // constructor
~SellCSigma_Matrix(); // destructor
int getChunkSize() const { return C_; }
int getSigma() const { return sigma_; }
int getRows() const { return M_; }
int getPaddedRows() const { return numberOfChunks_*C_; }
int getCols() const { return N_; }
int getNonZeros() const { return nz_; }
int getNumberOfChunks() const { return numberOfChunks_; }
int getOverhead() const { return overhead_; }
int getCapasety() const { return capasety_; }
int const * getColInd() const { return colInd_; }
int const * getChankPtr() const { return chunkPtr_; }
int const * getChankLength() const { return chunkLength_; }
int const * getPermutation() const { return permute_; }
double const * getValues() const { return val_; }
// We do not need copy and move symantic for this benchmark
SellCSigma_Matrix(SellCSigma_Matrix const & other) = delete; // copy constructor
SellCSigma_Matrix(SellCSigma_Matrix && other) = delete; // move constructor
SellCSigma_Matrix & operator= (SellCSigma_Matrix const & other) = delete; // copy assignment
SellCSigma_Matrix & operator= (SellCSigma_Matrix && other) = delete; // move assignment
private:
int const C_, sigma_;
int M_, N_, nz_, numberOfChunks_, overhead_, capasety_;
int *colInd_, *chunkPtr_, *chunkLength_;
int *permute_; // Sell-C-sigma row ID -> orginal row ID
int *antiPermute_; // orginal row ID -> Sell row ID
double* val_;
};
/**
* constructor
*/
SellCSigma_Matrix::SellCSigma_Matrix( MMreader mmMatrix, int C, int const sigma )
:C_(C), sigma_(sigma)
,M_(mmMatrix.getRows()), N_(mmMatrix.getCols())
,nz_(mmMatrix.getNonZeros()), numberOfChunks_((M_-1)/C+1)
,colInd_(nullptr)
,chunkPtr_(new int[numberOfChunks_]), chunkLength_(new int[numberOfChunks_])
,permute_(new int[M_]), antiPermute_(new int[M_])
,val_(nullptr)
{
// sort input Matrix by row ID
if( !mmMatrix.isRowSorted() )
sortByRow(mmMatrix);
std::vector< std::tuple<int,int,double> > & mmData = mmMatrix.getMatrx();
std::vector< std::tuple<int, int> > rowLengths = getRowLengths(mmMatrix);
// sort sigam chunks by row length
auto begin = rowLengths.begin();
auto end = rowLengths.begin() + getSigma();
for (; end <= rowLengths.end(); begin += getSigma(), end += getSigma() )
{
std::sort(begin, end,
[](std::tuple<int,int> const & a, std::tuple<int,int> const & b)
{return std::get<1>(a) < std::get<1>(b);}
);
}
begin -= getSigma();
std::sort(begin, rowLengths.end(),
[](std::tuple<int,int> const & a, std::tuple<int,int> const & b)
{return std::get<1>(a) < std::get<1>(b);}
);
// determine chunk length and size
// and set backword permutation
std::vector<int> valuesPerChunk( getNumberOfChunks() );
#ifdef _OPENMP
#pragma omp parallel for schedule(runtime)
#endif
for (int chunk=0; chunk < getNumberOfChunks(); ++chunk)
{
int maxRowLenghth = 0;
for (int i=0, row=chunk*getChunkSize();
i<getChunkSize() && row<getRows();
++i, ++row
)
{
if ( maxRowLenghth < std::get<1>(rowLengths[row]) )
maxRowLenghth = std::get<1>(rowLengths[row]);
// set backword permutation
antiPermute_[ std::get<0>(rowLengths[row]) ] = row;
}
chunkLength_[chunk] = maxRowLenghth;
valuesPerChunk[chunk] = maxRowLenghth * getChunkSize();
}
// calculate memory usage and allocate memmory for values and colum IDs
capasety_ = std::accumulate(std::begin(valuesPerChunk),
std::end(valuesPerChunk),
0
);
val_ = new double[capasety_];
colInd_ = new int [capasety_];
// calulate memory overhead
overhead_ = capasety_ - getNonZeros();
// creat Sell-C-sigma data
std::vector<int> chunkOffset = getOffsets(valuesPerChunk);
std::vector<int> rowOffset = getOffsets(getValsPerRow(mmMatrix));
#ifdef _OPENMP
#pragma omp parallel for schedule(runtime)
#endif
for (int chunk=0; chunk < getNumberOfChunks(); ++chunk)
{
chunkPtr_[chunk] = chunkOffset[chunk];
for (int j=0; j<chunkLength_[chunk]; ++j)
{
for (int i=0, row=chunk*getChunkSize();
i<getChunkSize();
++i, ++row
)
{
int col;
double val;
if (row<getRows())
{
// set permutation
permute_[row] = std::get<0>(rowLengths[row]);
// finde values and collumn index
if ( j < std::get<1>(rowLengths[row]) )
{ // fill with matrix values
int id = rowOffset[ permute_[row] ] + j;
val = std::get<2>( mmData[id] );
col = std::get<1>( mmData[id] );
}
else
{ // fill chunk with 0
val = 0.;
col = 0;
}
}
else
{ // add zero rows to end of matrix fill up last chunk
val = 0.;
col = 0;
}
val_ [chunkPtr_[chunk] + i + j*getChunkSize()] = val;
colInd_[chunkPtr_[chunk] + i + j*getChunkSize()] = antiPermute_[col];
}
}
}
/*
std::cout << "Sell-C-sigma constructed:"
<< "\nC: " << getChunkSize() << " sigma: " << getSigma()
<< "\n(" << getRows() << "," << getCols() << ") " << getNonZeros()
<< ":\n";
for (int i=0; i<valueMemoryUsage; ++i)
{
std::cout << getValues()[i] << " (" << getColInd()[i] << ")\n";
}
std::cout << std::endl;
*/
}
// destructor
SellCSigma_Matrix::~SellCSigma_Matrix()
{
delete[] val_;
delete[] antiPermute_;
delete[] permute_;
delete[] chunkLength_;
delete[] chunkPtr_;
delete[] colInd_;
}
/*****Free Functions*CSR_MATRIX***********************************************/
#endif