-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
lapack.cpp
402 lines (340 loc) · 12.9 KB
/
lapack.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
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
#include <diol.hpp>
#include <eigen3/Eigen/Core>
#include <mkl/mkl_lapack.h>
#define CAT_(A, B) A##B
#define CAT(A, B) CAT_(A, B)
#define MAKE_LAPACK(prefix, func) \
inline static constexpr auto func = CAT(CAT(prefix, func), _);
using namespace diol;
template <typename T> T const *addr(T const &ptr) { return &ptr; }
template <typename T> T *addr(T &&ptr) { return &ptr; }
MKL_INT max(MKL_INT a, MKL_INT b) { return a > b ? a : b; }
using f32 = float;
using f64 = double;
using c32 = std::complex<f32>;
using c64 = std::complex<f64>;
template <typename T> using Mat = Eigen::Matrix<T, -1, -1>;
#define MAKE_MKL(ty, mkl_ty, prefix, sym) \
template <> struct Mkl<ty> { \
using T = mkl_ty; \
MAKE_LAPACK(prefix, potrf); \
MAKE_LAPACK(prefix, geqrf); \
MAKE_LAPACK(prefix, geqp3); \
MAKE_LAPACK(prefix, getrf); \
MAKE_LAPACK(prefix, getc2); \
MAKE_LAPACK(prefix, gesdd); \
MAKE_LAPACK(CAT(prefix, sym), evd); \
MAKE_LAPACK(prefix, geev); \
};
template <typename E> struct Mkl;
MAKE_MKL(f32, f32, s, sy);
MAKE_MKL(f64, f64, d, sy);
MAKE_MKL(c32, MKL_Complex8, c, he);
MAKE_MKL(c64, MKL_Complex16, z, he);
template <typename T> using Data = typename Mkl<T>::T *;
template <typename T> MKL_INT stride(Mat<T> const &mat) {
return mat.outerStride();
}
template <typename T> auto data(Mat<T> &mat) { return Data<T>(mat.data()); }
template <typename T> Mat<T> rand(MKL_INT m, MKL_INT n) {
Mat<T> A(m, n);
A.setRandom();
return A;
}
template <typename T> Mat<T> rand_pos_def(MKL_INT n) {
Mat<T> A(n, n);
A.setRandom();
Mat<T> H = T(0.5) * (A + A.adjoint());
H += T(n) * Mat<T>::Identity(n, n);
return H;
}
template <typename T> Mat<T> randh(MKL_INT n) {
Mat<T> A(n, n);
A.setRandom();
Mat<T> H = T(0.5) * (A + A.adjoint());
return H;
}
template <typename T> void cholesky(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = rand_pos_def<T>(n);
Mat<T> H = H_orig;
std::move(bencher).bench([&] {
H = H_orig;
Mkl<T>::potrf("L", addr(n), data(H), addr(stride(H)), addr((MKL_INT)0));
});
}
template <typename T> void qr(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = rand<T>(n, n);
Mat<T> H = H_orig;
Mat<T> tau(n, 1);
T lwork_;
Mkl<T>::geqrf(addr(n), addr(n), data(H), addr(stride(H)), data(tau),
Data<T>(&lwork_), addr((MKL_INT)-1), addr((MKL_INT)0));
MKL_INT lwork = 2 * (MKL_INT)std::real(lwork_);
Mat<T> work(lwork, 1);
std::move(bencher).bench([&] {
H = H_orig;
Mkl<T>::geqrf(addr(n), addr(n), data(H), addr(stride(H)), data(tau),
data(work), addr(lwork), addr((MKL_INT)0));
});
}
template <typename T> void piv_qr(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = rand<T>(n, n);
Mat<T> H = H_orig;
Mat<T> tau(n, 1);
Mat<MKL_INT> p(n, 1);
Mat<typename Mat<T>::RealScalar> rwork(2 * n, 1);
T lwork_;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::geqp3(addr(n), addr(n), data(H), addr(stride(H)), p.data(),
data(tau), Data<T>(&lwork_), addr((MKL_INT)-1),
addr((MKL_INT)0));
} else {
Mkl<T>::geqp3(addr(n), addr(n), data(H), addr(stride(H)), p.data(),
data(tau), Data<T>(&lwork_), addr((MKL_INT)-1), data(rwork),
addr((MKL_INT)0));
}
auto lwork = 2 * (MKL_INT)std::real(lwork_);
Mat<T> work(lwork, 1);
std::move(bencher).bench([&] {
H = H_orig;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::geqp3(addr(n), addr(n), data(H), addr(stride(H)), p.data(),
data(tau), data(work), addr(lwork), addr((MKL_INT)0));
} else {
Mkl<T>::geqp3(addr(n), addr(n), data(H), addr(stride(H)), p.data(),
data(tau), data(work), addr(lwork), data(rwork),
addr((MKL_INT)0));
}
});
}
template <typename T> void lu(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = rand<T>(n, n);
Mat<T> H = H_orig;
Mat<MKL_INT> p(n, 1);
Mat<T> work(n, max(n, 16));
std::move(bencher).bench([&] {
H = H_orig;
Mkl<T>::getrf(addr(n), addr(n), data(H), addr(stride(H)), p.data(),
addr((MKL_INT)0));
});
}
template <typename T> void piv_lu(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = rand<T>(n, n);
Mat<T> H = H_orig;
Mat<MKL_INT> p(n, 1);
Mat<MKL_INT> q(n, 1);
Mat<T> work(n, max(n, 16));
std::move(bencher).bench([&] {
H = H_orig;
Mkl<T>::getc2(addr(n), data(H), addr(stride(H)), p.data(), q.data(),
addr((MKL_INT)0));
});
}
template <typename T> void svd(Bencher bencher, PlotArg arg) {
MKL_INT m = arg.n;
MKL_INT n = arg.n;
MKL_INT mn = m * n;
MKL_INT mx = max(m, n);
std::srand(0);
Mat<T> H_orig = rand<T>(m, n);
Mat<T> H = H_orig;
Mat<T> U(m, m);
Mat<T> V(n, n);
Mat<typename Mat<T>::RealScalar> S(m, 1);
Mat<typename Mat<T>::RealScalar> rwork(
max(5 * mn * mn + 5 * mn, 2 * mx * mn + 2 * mn * mn + mn), 1);
Mat<MKL_INT> iwork(8 * max(m, n), 1);
T lwork_;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::gesdd("A", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
Data<T>(&lwork_), addr((MKL_INT)-1), iwork.data(),
addr((MKL_INT)0));
} else {
Mkl<T>::gesdd("A", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
Data<T>(&lwork_), addr((MKL_INT)-1), data(rwork),
iwork.data(), addr((MKL_INT)0));
}
auto lwork = 2 * (MKL_INT)std::real(lwork_);
Mat<T> work(lwork, 1);
std::move(bencher).bench([&] {
H = H_orig;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::gesdd("A", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
data(work), addr(lwork), iwork.data(), addr((MKL_INT)0));
} else {
Mkl<T>::gesdd("A", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
data(work), addr(lwork), data(rwork), iwork.data(),
addr((MKL_INT)0));
}
});
}
template <typename T> void thin_svd(Bencher bencher, PlotArg arg) {
MKL_INT m = 4096;
MKL_INT n = arg.n;
MKL_INT mn = m * n;
MKL_INT mx = max(m, n);
std::srand(0);
Mat<T> H_orig = rand<T>(m, n);
Mat<T> H = H_orig;
Mat<T> U(m, m);
Mat<T> V(n, n);
Mat<typename Mat<T>::RealScalar> S(m, 1);
Mat<typename Mat<T>::RealScalar> rwork(
max(5 * mn * mn + 5 * mn, 2 * mx * mn + 2 * mn * mn + mn), 1);
Mat<MKL_INT> iwork(8 * max(m, n), 1);
T lwork_;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::gesdd("S", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
Data<T>(&lwork_), addr((MKL_INT)-1), iwork.data(),
addr((MKL_INT)0));
} else {
Mkl<T>::gesdd("S", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
Data<T>(&lwork_), addr((MKL_INT)-1), data(rwork),
iwork.data(), addr((MKL_INT)0));
}
auto lwork = 2 * (MKL_INT)std::real(lwork_);
Mat<T> work(lwork, 1);
std::move(bencher).bench([&] {
H = H_orig;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::gesdd("S", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
data(work), addr(lwork), iwork.data(), addr((MKL_INT)0));
} else {
Mkl<T>::gesdd("S", addr(m), addr(n), data(H), addr(stride(H)), data(S),
data(U), addr(stride(U)), data(V), addr(stride(V)),
data(work), addr(lwork), data(rwork), iwork.data(),
addr((MKL_INT)0));
}
});
}
template <typename T> void eigh(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = randh<T>(n);
Mat<T> H = H_orig;
Mat<T> U(n, n);
Mat<typename Mat<T>::RealScalar> W(n, 1);
T lwork_ = 0;
typename Mat<T>::RealScalar lrwork_ = 0;
MKL_INT liwork_ = 0;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::evd("V", "L", addr(n), data(H), addr(stride(H)), data(W),
Data<T>(&lwork_), addr((MKL_INT)-1), &liwork_,
addr((MKL_INT)-1), addr((MKL_INT)0));
} else {
Mkl<T>::evd("V", "L", addr(n), data(H), addr(stride(H)), data(W),
Data<T>(&lwork_), addr((MKL_INT)-1), &lrwork_,
addr((MKL_INT)-1), &liwork_, addr((MKL_INT)-1),
addr((MKL_INT)0));
}
MKL_INT lwork = 2 * (MKL_INT)(std::real(lwork_));
MKL_INT lrwork = 2 * (MKL_INT)(lrwork_);
MKL_INT liwork = 2 * (MKL_INT)(liwork_);
Mat<T> work(lwork, 1);
Mat<typename Mat<T>::RealScalar> rwork(lrwork, 1);
Mat<MKL_INT> iwork(liwork, 1);
std::move(bencher).bench([&] {
H = H_orig;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::evd("V", "L", addr(n), data(H), addr(stride(H)), data(W),
data(work), addr(lwork), iwork.data(), addr(liwork),
addr((MKL_INT)0));
} else {
Mkl<T>::evd("V", "L", addr(n), data(H), addr(stride(H)), data(W),
data(work), addr(lwork), data(rwork), addr(lrwork),
iwork.data(), addr(liwork), addr((MKL_INT)0));
}
});
}
template <typename T> void eig(Bencher bencher, PlotArg arg) {
MKL_INT n = arg.n;
std::srand(0);
Mat<T> H_orig = rand<T>(n, n);
Mat<T> H = H_orig;
Mat<T> U(n, n);
Mat<T> V(n, n);
Mat<T> W(n, 1);
Mat<typename Mat<T>::RealScalar> W_im(n, 1);
Mat<typename Mat<T>::RealScalar> rwork(2 * n, 1);
T lwork_;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::geev("V", "N", addr(n), data(H), addr(stride(H)), data(W),
data(W_im), data(U), addr(stride(U)), data(V), addr(stride(V)),
Data<T>(&lwork_), addr((MKL_INT)-1), addr((MKL_INT)0));
} else {
Mkl<T>::geev("V", "N", addr(n), data(H), addr(stride(H)), data(W), data(U),
addr(stride(U)), data(V), addr(stride(V)), Data<T>(&lwork_),
addr((MKL_INT)-1), data(rwork), addr((MKL_INT)0));
}
MKL_INT lwork = 2 * (MKL_INT)std::real(lwork_);
Mat<T> work(lwork, 1);
std::move(bencher).bench([&] {
H = H_orig;
if constexpr (std::same_as<T, typename Mkl<T>::T>) {
Mkl<T>::geev("V", "N", addr(n), data(H), addr(stride(H)), data(W),
data(W_im), data(U), addr(stride(U)), data(V),
addr(stride(V)), data(work), addr(lwork), addr((MKL_INT)0));
} else {
Mkl<T>::geev("V", "N", addr(n), data(H), addr(stride(H)), data(W),
data(U), addr(stride(U)), data(V), addr(stride(V)),
data(work), addr(lwork), data(rwork), addr((MKL_INT)0));
}
});
}
std::string glue_name(std::string prefix, std::string name, std::string type) {
return prefix + "_" + name + "<" + type + ">";
}
template <typename T>
void register_funcs(std::string prefix, std::string ty, Bench &bench) {
PlotArg args[] = {
4, 8, 12, 16, 24, 32, 48, 64, 128,
256, 512, 1024, 1536, 2048, 2560, 3072, 3584, 4096,
};
auto do_it = [&](std::string name, FnPtr<Bencher, PlotArg> fn) {
bench.register_funcs<PlotArg>({{{glue_name(prefix, name, ty), fn}}}, args);
};
do_it("cholesky", cholesky<T>);
do_it("qr", qr<T>);
do_it("piv_qr", piv_qr<T>);
do_it("lu", lu<T>);
do_it("piv_lu", piv_lu<T>);
do_it("svd", svd<T>);
do_it("thin_svd", thin_svd<T>);
do_it("eigh", eigh<T>);
do_it("eig", eig<T>);
}
f64 flops_per_sec(size_t n, f64 time) {
return f64(n) * f64(n) * f64(n) / time;
}
int main() {
auto config = BenchConfig::from_args();
config.set_metric("n³/s", Monotonicity::HigherIsBetter, flops_per_sec);
auto bench = Bench::from_config(config);
#ifdef BENCH_MKL
std::string prefix = "mkl";
#else
std::string prefix = "openblas";
#endif
register_funcs<f32>(prefix, "f32", bench);
register_funcs<f64>(prefix, "f64", bench);
register_funcs<c32>(prefix, "c32", bench);
register_funcs<c64>(prefix, "c64", bench);
bench.run();
}