-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
239 lines (158 loc) · 4.81 KB
/
Copy pathmain.cpp
File metadata and controls
239 lines (158 loc) · 4.81 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
#define NDEBUG
#include <iostream>
#include <cstddef>
#include <vector>
#include <chrono>
#include <cassert>
#ifdef __GNUC__
#define ALLWAYS_INLINE __attribute__((always_inline))
#elif _MSC_VER
#define ALLWAYS_INLINE __forceinline
#else
#define ALLWAYS_INLINE
#endif
template<typename T, typename Rep = std::vector<T>>
class Vec {
Rep expr_rep;
public:
explicit Vec(std::size_t s) : expr_rep(s) { }
Vec(const Rep& rb) : expr_rep(rb) { }
std::size_t size() const { return expr_rep.size(); }
Vec& operator=(const Vec& b) {
assert(size() == b.size());
for (std::size_t i = 0 ; i < b.size() ; ++i)
expr_rep[i] = b[i];
return *this;
}
template<typename T2, typename Rep2>
ALLWAYS_INLINE Vec& operator=(const Vec<T2, Rep2>& b) {
assert(size() == b.size());
for (std::size_t i = 0 ; i < b.size() ; ++i)
expr_rep[i] = b[i];
return *this;
}
decltype(auto) operator[](std::size_t i) const {
assert(i < size());
return expr_rep[i];
}
T& operator[](std::size_t i) {
assert(i < size());
return expr_rep[i];
}
const Rep& rep() const { return expr_rep; }
Rep& rep() { return expr_rep; }
};
template<typename T>
class Scalar {
const T& s;
public:
constexpr Scalar(const T& v) : s(v) {}
constexpr const T& operator[](std::size_t) const { return s; }
constexpr std::size_t size() const { return 0; }
};
template<typename T>
struct VecTraits {
using ExprRef = const T&;
};
template<typename T>
struct VecTraits<Scalar<T>> {
using ExprRef = Scalar<T>;
};
template<typename T, typename OP1, typename OP2>
class AddV {
typename VecTraits<OP1>::ExprRef op1;
typename VecTraits<OP2>::ExprRef op2;
public:
AddV(const OP1& a, const OP2& b) : op1(a), op2(b) {}
T operator[](std::size_t i) const { return op1[i] + op2[i]; }
std::size_t size() const {
assert(op1.size() == 0 || op2.size() == 0 || op1.size() == op2.size());
return op1.size() != 0 ? op1.size() : op2.size();
}
};
template<typename T, typename OP1, typename OP2>
class MultV {
typename VecTraits<OP1>::ExprRef op1;
typename VecTraits<OP2>::ExprRef op2;
public:
MultV(const OP1& a, const OP2& b) : op1(a), op2(b) {}
T operator[](std::size_t i) const { return op1[i] * op2[i]; }
std::size_t size() const {
assert(op1.size() == 0 || op2.size() == 0 || op1.size() == op2.size());
return op1.size() != 0 ? op1.size() : op2.size();
}
};
template<typename T, typename Rep1, typename Rep2>
inline auto operator+(const Vec<T, Rep1>& a, const Vec<T, Rep2>& b)
{
using Rep = AddV<T, Rep1, Rep2>;
return Vec<T, Rep>{ Rep{a.rep(), b.rep()} };
}
template<typename T, typename Rep2>
inline auto operator*(const T& a, const Vec<T, Rep2>& b)
{
using Rep = MultV<T, Scalar<T>, Rep2>;
return Vec<T, Rep>{ Rep{Scalar<T>(a), b.rep()} };
}
template<typename T, typename Rep1>
inline auto operator*(const Vec<T, Rep1>& a, const T& b)
{
return b*a;
}
template<typename T, typename Rep1, typename Rep2>
inline auto operator*(const Vec<T, Rep1>& a, const Vec<T, Rep2>& b)
{
using Rep = MultV<T, Rep1, Rep2>;
return Vec<T, Rep>{ Rep{a.rep(), b.rep()} };
}
class Double {
double d;
public:
inline static int count = 0;
Double() { }
Double(double dd) : d(dd) { count++; }
Double(const Double& dd) : d(dd.d) { count++; }
Double& operator=(const Double& dd) { d = dd.d; count++; return *this; }
friend Double operator+(const Double& a, const Double& b) {
return Double{a.d + b.d};
}
friend Double operator*(const Double& a, const Double& b) {
return Double{a.d * b.d};
}
};
int main()
{
constexpr int N = 1000*1000*100;
Vec<double> v1(N);
Vec<double> v2(N);
Vec<double> v3(N);
std::cout << v1.size() << "\n";
for (std::size_t i = 0 ; i < v1.size() ; ++i)
{
v1[i] = 1.0;
v2[i] = 2.0;
}
Double::count = 0;
double a{1.5};
double b{1.25};
auto t0 = std::chrono::steady_clock::now();
v3 = a*v1*b + v2*v2;
/*
using Rep1 = MultV<double, Scalar<double>, Vec<double>>;
using Rep2 = MultV<double, Rep1, Scalar<double>>;
using Rep3 = MultV<double, Vec<double>, Vec<double>>;
using Rep4 = AddV<double, Rep2, Rep3>;
v3 = Vec<double, Rep4>(Rep4(Rep2(Rep1(a,v1),b), Rep3(v2,v2)));
*/
auto t1 = std::chrono::steady_clock::now();
std::chrono::duration<double> dur = t1 - t0;
std::cout << dur.count() << "\n";
t0 = std::chrono::steady_clock::now();
for (std::size_t i = 0 ; i < v1.size() ; ++i)
v3[i] = a*v1[i]*b + v2[i]*v2[i];
t1 = std::chrono::steady_clock::now();
dur = t1 - t0;
std::cout << dur.count() << "\n";
std::cout << Double::count << "\n";
return 0;
}