-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_old.cpp
More file actions
539 lines (427 loc) · 12.8 KB
/
module_old.cpp
File metadata and controls
539 lines (427 loc) · 12.8 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include <type_traits>
///////////////////////////////////////////////////////////////////////////////
// Forward-declare templates
///////////////////////////////////////////////////////////////////////////////
// Like a tuple, but indexes members by a type template parameter
template<typename... BINDINGS>
struct TypeMap;
// Used to help typemaps determine the held type for a given type key
template<typename KEY, typename MAP>
struct TypeMapLookup;
// Used to bind a certain key type to a certain held type for a TypeMap
template<typename KEY, typename TYPE>
struct Binding;
///////////////////////////////////////////////////////////////////////////////
// Define templates
///////////////////////////////////////////////////////////////////////////////
// Holds a template for later application
template<template <typename> typename TEMPLATE>
struct Meta
{
template <typename ARG>
struct Template {
typedef TEMPLATE<ARG> Type;
};
};
// Specializes the template provided
template<typename TYPE, typename ARG>
struct UnMeta
{
typedef TYPE Type;
};
template<template <typename> typename TEMPLATE, typename ARG>
struct UnMeta <Meta<TEMPLATE>,ARG>
{
typedef TEMPLATE<ARG> Type;
};
template<typename KEY, typename TYPE>
struct Binding
{
typedef KEY Key;
typedef TYPE Type;
};
struct UndefinedType {};
template<typename T>
struct AlwaysFalse
{
static constexpr bool VALUE = false;
};
template<typename... ELEMENTS>
struct TypeSet
{
template<typename ITEM>
static constexpr bool has_item()
{
return false;
}
static constexpr bool has_duplicates()
{
return false;
}
template<typename ITEM>
constexpr auto& get ()
{
static_assert(AlwaysFalse<ITEM>::VALUE,"Key does not exist in type map.");
}
};
template<typename HEAD, typename... TAIL>
struct TypeSet <HEAD,TAIL...>
{
typedef HEAD Type;
typedef TypeSet<TAIL...> Tail;
Type data;
Tail tail;
template<typename ITEM>
static constexpr bool has_item()
{
if constexpr (std::is_same<ITEM,HEAD>::value) {
return true;
} else {
return Tail::template has_item<ITEM>();
}
}
static constexpr bool has_duplicates()
{
if constexpr (Tail::template has_item<HEAD>()) {
return true;
} else {
return Tail::has_duplicates();
}
}
template<typename ITEM>
constexpr auto& get ()
{
if constexpr (TypeSet<HEAD,TAIL...>::template has_item<ITEM>()) {
if constexpr (std::is_same<ITEM,Type>::value) {
return data;
} else {
return tail.template get<ITEM>();
}
} else {
static_assert(AlwaysFalse<ITEM>::VALUE,"Key does not exist in type map.");
UndefinedType *dummy = nullptr;
return *dummy;
}
}
};
// Base case
template<typename... BINDINGS>
struct TypeMap
{
template<typename KEY>
static constexpr bool has_key()
{
return false;
}
template<typename KEY>
constexpr auto& get ()
{
static_assert(AlwaysFalse<KEY>::VALUE,"Key does not exist in type map.");
}
};
// Recursive case
template<typename HEAD, typename... TAIL>
struct TypeMap <HEAD,TAIL...>
{
typedef TypeMap<TAIL...> Tail;
typedef typename HEAD::Key Key;
typedef typename HEAD::Type Type;
Type data;
Tail tail;
template<typename KEY>
static constexpr bool has_key()
{
if constexpr (std::is_same<KEY,Key>::value) {
return true;
} else {
return Tail::template has_key<KEY>();
}
}
template<typename KEY>
constexpr auto& get ()
{
if constexpr (TypeMap<HEAD,TAIL...>::has_key<KEY>()) {
if constexpr (std::is_same<KEY,Key>::value) {
return data;
} else {
return tail.template get<KEY>();
}
} else {
static_assert(AlwaysFalse<KEY>::VALUE,"Key does not exist in type map.");
UndefinedType *dummy = nullptr;
return *dummy;
}
}
};
/*
template<typename EXEC_TYPE>
struct Schedule
{
template<typename TASK_TYPE,typename... ARGS>
void exec(RUNTIME rt, )
{
}
};
template<typename RT> struct Odd;
template<typename RT> struct Even;
template<typename RT>
struct Even
{
auto operator()(RT rt, int original, int value, int step_count) {
if (value <= 1) {
return;
}
value /= 2;
if (value%2 == 0) {
rt.as<Schedule<GPU_thread>>().exec<Even<RT>>(original,value,step_count);
} else {
rt.as<Schedule<GPU_thread>>().exec<Odd<RT>> (original,value,step_count);
}
}
};
template<typename RT>
struct Odd
{
auto operator()(RT rt, int original, int value, int step_count) {
if (value <= 1) {
return;
}
value /= 2;
if (value%2 == 0) {
rt.Schedule<GPU_thread>::exec<Even<RT>>(original,value,step_count);
} else {
rt.Schedule<GPU_thread>::exec<Odd<RT>> (original,value,step_count);
}
}
};
*/
template<typename TASK,typename RT>
struct Task {
typedef typename UnMeta<TASK,RT>::Type TaskType;
RT &rt;
TaskType &task;
Task(TaskType &task, RT rt)
: rt(rt)
, task(task)
{}
template<typename...ARGS>
auto operator()(ARGS... args) {
task(rt,args...);
}
};
template<typename RUNTIME> struct Odd;
template<typename RUNTIME> struct Even;
#include <iostream>
template<typename MOD,typename RT>
MOD get(RT& rt) {
return MOD(rt.set.template get<MOD>(),rt);
}
/*
template<template<typename> typename TASK,typename RT>
struct Service {
RT& runtime;
TASK<RT>& task;
Service<TASK,RT>(TASK<RT> &task, RT &rt) : task(task), runtime(rt) {}
};
*/
template<typename RT>
struct Even
{
void operator()(RT rt, int original, int value, int count)
{
value /= 2;
if(value <= 1) {
std::cout << "Value " << original << " took " << count << " steps." << std::endl;
return;
}
count++;
if ((value%2) == 0) {
get<Meta<Even>>(rt)(original,value,count);
} else {
get<Meta<Odd>> (rt)(original,value,count);
}
}
};
template<typename RT>
struct Odd
{
void operator()(RT rt, int original, int value, int count)
{
value = value *3 + 1;
if(value <= 1) {
std::cout << "Value " << original << " took " << count << " steps." << std::endl;
return;
}
count++;
if ((value%2) == 0) {
get<Meta<Even>>(rt)(original,value,count);
} else {
get<Meta<Odd>> (rt)(original,value,count);
}
}
};
template<typename... MODULES>
struct Runtime
{
typedef Runtime<MODULES...> Self;
template<typename TYPE>
struct ModuleType
{
typedef TYPE Type;
};
template<template<typename> typename TEMPLATE>
struct ModuleType <Meta<TEMPLATE>>
{
typedef TEMPLATE<Runtime> Type;
};
typedef TypeSet<ModuleType<MODULES>::typename Type> SetType;
SetType set;
template<typename MOD>
struct get_adapter
{
MOD& get(SetType& set)
{
return set.template get<ModuleType<MOD>::typename Type>();
}
};
template<template <typename> typename MOD>
struct get_adapter <Meta<MOD>>
{
MOD<Runtime<MODULES...>>& get(SetType& set)
{
return set.template get<ModuleType<Meta<MOD>>::typename Type>();
}
};
template<typename MOD>
auto& get()
{
return get_adapter<MOD>::get(set);
}
};
// Type Containers:
// - Meta<template<typename>> : Represents a template taking one type parameter
// - TypeSet<typename...> : Represents a set of types
// - TypeList<typename...> : Represents a sequence of types. Provides `Set` as a conversion to TypeSet.
// - TypeMap<typename...> : Represents a mapping of types to other types. Provides TypeList of keys and their corresponding items as `Key` and `Item`
// Type Processors:
// - TryInstantiate<Input,Arg> : Provides Temp<Arg> as Type if Input is Meta<Temp>. If not, Type is just Input.
// - Filter<Meta<Temp>,TypeSet> : Filters for types in TypeSet where Temp<type>::VALUE == true, provides it as Type
// - Flatten<TypeList<>> : Expands all TypeList types directly cont
// - FlattenRec<TypeList<>> :
// - For<TypeList<>,TypeList<>>
// Component Descriptors:
// - Req<type> : Requires the `type` trait to be implemented to be used.
// - Impl<type> : Directly implements the `type` trait.
// -
// - All<template<typename>> : Corresponds to all specializations of the template.
// - Mixin<template<typename>> : Indicates that the underlying template should have the runtime type plugged in
// -
// Impl<For<Each<Meta<SlabAllocator<RT>>>::Type,TypedAllocator<RT> >::Type>
// Bridge type :
// Used as a handle to represent a set of functionality and as a guard to
// check whether or not a type implements that functionality. Concepts may
// be used by bridges if compiled for C++20 and above.
//
// ComponentType :
// Literally any type.
// ComponentMixin :
// A template which produces components based off of an input runtime
// type. This allows one to write code that can adapt based upon the
// underlying runtime, and its other components.
// ComponentTemplate :
// A template which can supply types implementing
// ComponentTemplateMixin :
// A template which produces a component template based off of an input
// runtime
// Questions to answer :
// - Could we compile something that accomplishes this trait? a.k.a. Is it implemented?
// - Is this trait compiled and available to use in some context? a.k.a. Is it required?
// - Is this trait actually available to use in this specific context? a.k.a. Is it provided?
// Component member types :
//
// nonlocal :
// A TypeSet of Member<X,Y> types, with X being a memory space
// type, and Y being a type that should be held within that memory space.
// This serves as a way of defining members that are "static" within
// the lifetime of the owning runtime, while also specifying their
// memory space. In cases where memory spaces limit mutual accesibility
// between different execution contexts (e.g. private memory, thread-local
// memory, and shared memory), exactly one instance is created for each
// independent region of mutual accessibility.
//
// requires_traits :
// A TypeSet of traits required by the component. If no such type is
// provided, it is assumed that the component may always be included in
// any runtime.
//
// implements_traits :
// A TypeSet of traits directly implemented by the component. If no such
// type is provided, it is assumed that the component does not
// automatically implement any trait simply by being included in a runtime
// that meets all of its requirements.
//
// reifies_traits :
// A TypeSet of traits directly reified by the component. If no such type
// is provided, it is assumed that the component does not reify any trait.
//
// uses_traits :
// A TypeSet of traits directly used by the component, and hence must be
// reified directly or by an outter runtime. If no such type is provided,
// it is assumed that the component does
//
// components :
// A template that maps trait types to components that implement the
// given interface. Traits implemented within the "Components" template
// of a runtime's component also count as implementations for the parent
// runtime. This allows arbitrary sets of implementations to be provided
// to runtimes in response to specific conditions.
//
int main() {
Runtime<Meta<Even>,Meta<Odd>> dummy;
get<Even>(dummy)(16,16,0);
return 0;
}
#if BLAH
template<typename RUNTIME, template <typename> typename DEFINITION>
struct component_factory
{
template<typename RUNTIME>
struct meta {
template<typename TRAIT>
struct component {
typedef DEFINITION<TRAIT>::typename type type;
};
};
};
template<typename Space>
struct storage
{
typedef storage<Space> default_type;
typedef typeSet<Storage<Space>> implements_traits;
typedef typeSet<> requires_traits;
};
struct MyRuntimeSpec {
typedef TypeSet <
rt::Marker<mem::space::CPUGlobal>,
type::Meta<type::BaseAlloc>,
type::Meta<mem::Arena>,
type::Meta<mem::SlabAlloc>,
rt::LazyBind<
type::Meta<mem::GeneralAlloc>,
type::Meta<mem::DefaultGeneralAlloc>,
>,
rt::Bind<
sched::CPUThread,
sched::DefaultCPUThread
>,
rt::Bind<
rng::FastRNG,
rng::DefaultFastRNG
>,
rt::MetaBind<
rng::FastRandom,
rng::DefaultFastRandom
>
> Components;
};
#endif