-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathsequences.cpp
485 lines (418 loc) · 11.9 KB
/
sequences.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <miopen/config.h> // WORKAROUND_BOOST_ISSUE_392
#include <miopen/rank.hpp>
#include <miopen/sequences.hpp>
#include <driver.hpp>
#include <boost/range/algorithm/find.hpp>
#include <boost/range/irange.hpp>
#include <array>
#include <chrono>
#include <iostream>
namespace miopen {
namespace seq {
struct TestData
{
int x;
};
enum class Modes
{
Standard,
Template,
Native,
Boost,
Unknown,
};
enum class Sequences
{
Seq,
Span,
IRange,
Join,
Unknown,
};
enum class Instances
{
Single,
PerCall,
Static,
Unknown,
};
template <class TType, class TContainer, TType TContainer::*field>
struct MemberPtr<std::integral_constant<TType TContainer::*, field>>
{
using Container = TContainer;
MemberPtr(const std::integral_constant<TType TContainer::*, field>&) {}
const TType& RV(const TContainer& cont) const { return cont.*field; }
TType& LV(TContainer& cont) const { return cont.*field; }
};
template <Sequences seq>
struct NativeImpl
{
};
template <>
struct NativeImpl<Sequences::Seq>
{
bool Next(TestData& v) const
{
if(v.x == 1)
{
v.x = 2;
return false;
}
if(v.x == 2)
{
v.x = 7;
return false;
}
v.x = 1;
return true;
}
};
template <>
struct NativeImpl<Sequences::Span>
{
bool Next(TestData& v) const
{
if(v.x == 8)
{
v.x = 0;
return true;
}
++v.x;
return false;
}
};
template <>
struct NativeImpl<Sequences::IRange>
{
bool Next(TestData&) const
{
std::cerr << "irange is only for boost" << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
}
};
template <>
struct NativeImpl<Sequences::Join>
{
bool Next(TestData& td) const
{
if(td.x < 8)
{
++td.x;
return false;
}
if(td.x == 8)
{
td.x = 10;
return false;
}
if(td.x == 10)
{
td.x = 11;
return false;
}
if(td.x == 10)
{
td.x = 11;
return false;
}
if(td.x == 11)
{
td.x = 14;
return false;
}
if(td.x == 14)
{
td.x = 15;
return false;
}
td.x = 0;
return true;
}
};
template <class TRange, class TValue>
bool Next(const TRange& range, TValue& value)
{
auto it = GenericFind(range, value);
assert(it != range.end());
if(++it == range.end())
{
value = *range.begin();
return true;
}
value = *it;
return false;
}
template <class TValue, TValue... values>
struct BoostSequence
{
using value_type = TValue;
using const_iterator = const TValue*;
#ifdef _MSC_VER
constexpr const_iterator begin() const { return arr.begin()._Unwrapped(); }
constexpr const_iterator end() const { return arr.end()._Unwrapped(); }
#else
constexpr const_iterator begin() const { return arr.begin(); }
constexpr const_iterator end() const { return arr.end(); }
#endif
private:
static constexpr std::size_t count = sizeof...(values);
static constexpr std::array<int, BoostSequence::count> arr = {{values...}};
};
template <class Start, class IntegerSequence>
struct span_impl;
template <class Start, class T, T... Ns>
struct span_impl<Start, std::integer_sequence<T, Ns...>>
{
using value_type = T;
using const_iterator = const T*;
T data[sizeof...(Ns)] = {(Ns + Start{})...};
constexpr const T* begin() const { return data; }
constexpr const T* end() const { return data + sizeof...(Ns); }
constexpr const T* find(T value) const
{
if(value < Start{} || value > (Start{} + sizeof...(Ns)))
{
return end();
}
else
{
return data + (value - Start{});
}
}
};
template <class T, T start, T end>
struct BoostSpan
: span_impl<std::integral_constant<T, start>, std::make_integer_sequence<T, end - start>>
{
};
template <Sequences seq>
struct BoostImpl
{
};
template <>
struct BoostImpl<Sequences::IRange>
{
bool Next(TestData& v) const { return miopen::seq::Next(boost::irange(0, 9), v.x); }
};
struct SpeedTestDriver : public test_driver
{
SpeedTestDriver()
{
add(iterations, "iterations");
add(modeStr, "mode");
add(sequenceStr, "seq");
add(instance_str, "inst");
}
void run()
{
const auto seq = ParseSequence(sequenceStr);
mode = ParseMode(modeStr);
instance = ParseInstance(instance_str);
switch(seq)
{
case Sequences::Seq: RunCore<Sequences::Seq>(); break;
case Sequences::Span: RunCore<Sequences::Span>(); break;
case Sequences::IRange: RunCore<Sequences::IRange>(); break;
case Sequences::Join: RunCore<Sequences::Join>(); break;
case Sequences::Unknown:
std::cerr << "Unknown sequence." << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
}
}
void show_help()
{
test_driver::show_help();
std::cout << "Permitted modes: nat, std, tmpl, boost" << std::endl;
std::cout << "Permitted sequences: seq, span, irange (boost), join(nat, std, tmpl)"
<< std::endl;
std::cout << "Permitted instances: single, percall, static" << std::endl;
}
private:
int iterations = 10;
Instances instance = Instances::Unknown;
Modes mode = Modes::Unknown;
std::string instance_str = "single";
std::string modeStr = "std";
std::string sequenceStr = "seq";
static Modes ParseMode(const std::string& str)
{
if(str == "std")
return Modes::Standard;
if(str == "tmpl")
return Modes::Template;
if(str == "nat")
return Modes::Native;
if(str == "boost")
return Modes::Boost;
return Modes::Unknown;
}
static Sequences ParseSequence(const std::string& str)
{
if(str == "seq")
return Sequences::Seq;
if(str == "span")
return Sequences::Span;
if(str == "join")
return Sequences::Join;
if(str == "irange")
return Sequences::IRange;
return Sequences::Unknown;
}
static Instances ParseInstance(const std::string& str)
{
if(str == "single")
return Instances::Single;
if(str == "percall")
return Instances::PerCall;
if(str == "static")
return Instances::Static;
return Instances::Unknown;
}
template <Sequences seq>
void RunCore() const
{
switch(mode)
{
case Modes::Standard: Standard<seq>(); break;
case Modes::Template: Template<seq>(); break;
case Modes::Native: Native<seq>(); break;
case Modes::Boost: Boost<seq>(); break;
case Modes::Unknown:
std::cerr << "Unknown mode." << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
}
}
template <class TType>
void SaveDeadCode(const TType& value) const
{
static const std::string dead_code_saver;
if(dead_code_saver.data() == nullptr)
{
std::cout << value << std::endl;
std::terminate();
}
}
template <class TRule>
void PrintCollectionImpl(rank<0>, const TRule&) const
{
}
template <class TRule, class = decltype(&TRule::FillBegin)>
void PrintCollectionImpl(rank<1>, const TRule& rule) const
{
TestData td{1};
rule.FillBegin(td);
auto i = 0;
do
{
std::cout << td.x << " ";
} while(!rule.Next(td) && ++i < 1024);
std::cout << std::endl;
}
template <class TRule>
void PrintCollection(const TRule& rule) const
{
PrintCollectionImpl(rank<16>{}, rule);
}
template <class TRuleGetter>
void TestCore(const TRuleGetter& rule_getter) const
{
PrintCollection(rule_getter());
TestData td{1};
const auto start = std::chrono::steady_clock::now();
for(auto i = 0; i < iterations; i++)
{
for(auto j = 0; j < 128 * 1024 * 1024; j++)
rule_getter().Next(td);
}
const auto time = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - start)
.count() *
.001 * .001;
std::cout << "Test time: " << time << " seconds" << std::endl;
SaveDeadCode(td.x); // required in release builds
}
template <class TRuleGetter>
void Test(const TRuleGetter& rule_getter) const
{
switch(instance)
{
case Instances::PerCall: TestCore(rule_getter); break;
case Instances::Single: {
const auto inst = rule_getter();
TestCore([&inst]() -> const auto& { return inst; });
break;
}
case Instances::Static:
TestCore([&rule_getter]() -> const auto& {
static const auto inst = rule_getter();
return inst;
});
break;
case Instances::Unknown:
std::cerr << "Unknown instance type" << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
}
}
template <Sequences seq>
void Native() const
{
Test([]() { return NativeImpl<seq>{}; });
}
static auto MakeSeq() { return Sequence<int, 1, 2, 7>{}; }
static auto MakeBSeq() { return BoostSequence<int, 1, 2, 7>{}; }
static auto MakeSpan() { return Span<int, 0, 8>{}; }
static auto MakeBSpan() { return BoostSpan<int, 0, 9>{}; }
static auto MakeJoin() { return Join<Span<int, 0, 8>, Sequence<int, 10, 11, 14, 15>>{}; }
static auto TmplMember() { return std::integral_constant<int TestData::*, &TestData::x>{}; }
template <class... TParams>
static auto RS(TParams... args)
{
return MakeRuleSet(std::make_tuple(args...));
}
template <Sequences seq>
void Standard() const
{
switch(seq)
{
case Sequences::Seq: Test([]() { return RS(MakeSeq(), &TestData::x); }); break;
case Sequences::Span: Test([]() { return RS(MakeSpan(), &TestData::x); }); break;
case Sequences::Join: Test([]() { return RS(MakeJoin(), &TestData::x); }); break;
case Sequences::IRange:
std::cerr << "irange is only for boost" << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
}
}
template <Sequences seq>
void Template() const
{
switch(seq)
{
case Sequences::Seq: Test([]() { return RS(MakeSeq(), TmplMember()); }); break;
case Sequences::Span: Test([]() { return RS(MakeSpan(), TmplMember()); }); break;
case Sequences::Join: Test([]() { return RS(MakeJoin(), TmplMember()); }); break;
case Sequences::IRange:
std::cerr << "irange is only for boost" << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
}
}
template <Sequences seq>
void Boost() const
{
switch(seq)
{
case Sequences::Seq: Test([]() { return RS(MakeBSeq(), &TestData::x); }); break;
case Sequences::Span: Test([]() { return RS(MakeBSpan(), &TestData::x); }); break;
case Sequences::Join:
std::cerr << "join is only for nat/std/tmpl" << std::endl;
std::exit(-1); // NOLINT (concurrency-mt-unsafe)
case Sequences::IRange: Test([]() { return BoostImpl<Sequences::IRange>{}; }); break;
}
}
};
} // namespace seq
} // namespace miopen
int main(int argc, const char* argv[])
{
test_drive<miopen::seq::SpeedTestDriver>(argc, argv);
return 0;
}