-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspvec.cpp
173 lines (153 loc) · 4.17 KB
/
spvec.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
#include "spvec.h"
#include "utility.h"
#if (__GNUC__ == 4 && (__GNUC_MINOR__ < 7) )
#include "randgen.h"
#else
#include <random>
#endif
#include <cassert>
// constructor that generates a junk dense vector
template <class T, class ITYPE>
Spvec<T,ITYPE>::Spvec (ITYPE dim)
{
assert(dim != 0);
n = static_cast<ITYPE>(ceil(static_cast<float>(dim)/RBDIM)) * RBDIM;
padding = n-dim;
if(padding)
cout << "Padded vector to size " << n << " for register blocking" << endl;
arr = new T[n];
}
template <class T, class ITYPE>
Spvec<T,ITYPE>::Spvec (T * darr, ITYPE dim)
{
assert(dim != 0);
n = static_cast<ITYPE>(ceil(static_cast<float>(dim)/RBDIM)) * RBDIM;
padding = n-dim;
if(padding)
cout << "Padded vector to size " << n << " for register blocking" << endl;
arr = new T[n](); // zero initialized PID
for(ITYPE i=0; i< n; ++i)
{
arr[i] = darr[i];
}
}
// copy constructor
template <class T, class ITYPE>
Spvec<T,ITYPE>::Spvec (const Spvec<T, ITYPE> & rhs): n(rhs.n),padding(rhs.padding)
{
if(n > 0)
{
arr = new T[n];
for(ITYPE i=0; i< n; ++i)
arr[i]= rhs.arr[i];
}
}
template <class T, class ITYPE>
Spvec<T,ITYPE> & Spvec<T,ITYPE>::operator= (const Spvec<T,ITYPE> & rhs)
{
if(this != &rhs)
{
if(n > 0)
{
delete [] arr;
}
n = rhs.n;
padding = rhs.padding;
if(n > 0)
{
arr = new T[n];
for(ITYPE i=0; i< n; ++i)
arr[i]= rhs.arr[i];
}
}
return *this;
}
template <class T, class ITYPE>
Spvec<T,ITYPE>::~Spvec()
{
if ( n > 0)
{
delete [] arr;
}
}
template <class T, class ITYPE>
Spvec<T,ITYPE> & Spvec<T,ITYPE>::operator+=(const Matmul< Csc<T, ITYPE>, Spvec<T,ITYPE> > & matmul)
{
if((n-padding == matmul.op1.rowsize()) && (matmul.op1.colsize() == matmul.op2.size())) // check compliance
{
csc_gaxpy(matmul.op1, const_cast< T * >(matmul.op2.arr), arr);
}
else
{
cout<< "Detected noncompliant matvec..." << endl;
}
return *this;
}
template <class T, class ITYPE>
Spvec<T,ITYPE> & Spvec<T,ITYPE>::operator+=(const Matmul< BiCsb<T, ITYPE>, Spvec<T,ITYPE> > & matmul)
{
typedef PTSR< T, T> PTDD;
if((n-padding == matmul.op1.rowsize()) && (matmul.op1.colsize() == matmul.op2.size())) // check compliance
{
bicsb_gespmv<PTDD>(matmul.op1, matmul.op2.arr, arr);
}
else
{
cout<< "Detected noncompliant matvec..." << endl;
}
return *this;
}
// populate the vector with random entries
// currently, only works for T "double" and "float"
template <class T, class ITYPE>
void Spvec<T,ITYPE>::fillrandom()
{
#if (__GNUC__ == 4 && (__GNUC_MINOR__ < 7) )
RandGen G;
for(ITYPE i=0; i< n; ++i)
{
arr[i] = G.RandReal();
}
#else
std::uniform_real_distribution<T> distribution(0.0f, 1.0f); //Values between 0 and 1
std::mt19937 engine; // Mersenne twister MT19937
auto generator = std::bind(distribution, engine);
std::generate_n(arr, n, generator);
#endif
}
// populate the vector with zeros
template <class T, class ITYPE>
void Spvec<T,ITYPE>::fillzero()
{
for(ITYPE i=0; i< n; ++i)
{
arr[i] = 0;
}
}
template <typename NT, typename IT>
void Verify(Spvec<NT, IT> & control, Spvec<NT, IT> & test, string name, IT m)
{
vector<NT>error(m);
std::transform(&control[0], (&control[0])+m, &test[0], error.begin(), absdiff<NT>());
auto maxelement = std::max_element(error.begin(), error.end());
cout << "Max error is: " << *maxelement << " on y[" << maxelement-error.begin()<<"]=" << test[maxelement-error.begin()] << endl;
NT machEps = machineEpsilon<NT>();
cout << "Absolute machine epsilon is: " << machEps <<" and y[" << maxelement-error.begin() << "]*EPSILON becomes "
<< machEps * test[maxelement-error.begin()] << endl;
NT sqrtm = sqrt(static_cast<NT>(m));
cout << "sqrt(n) * relative error is: " << abs(machEps * test[maxelement-error.begin()]) * sqrtm << endl;
if ( (abs(machEps * test[maxelement-error.begin()]) * sqrtm) < abs(*maxelement))
{
cout << "*** ATTENTION ***: error is more than sqrt(n) times the relative machine epsilon" << endl;
}
#ifdef DEBUG
cout << "<index, control, test>: \n";
for(IT i=0; i<m; ++i)
{
if(error[i] > abs(sqrtm * machEps * test[i]))
{
cout << i << "\t" << control[i] << " " << test[i] << "\n";
}
}
#endif
}