harmonize-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,747 @@
1
+
2
+
3
+
4
+ #ifndef HARMONIZE_ASYNC_PROMISE
5
+ #define HARMONIZE_ASYNC_PROMISE
6
+
7
+ #include "../preamble/mod.h"
8
+
9
+
10
+ //! The type used as a discriminant (identifying value) between operation
11
+ //! types.
12
+ using OpDisc = unsigned int;
13
+
14
+ //! The `OpUnion` template struct serves to represent a set of operations as a type
15
+ //! so that it may be passed as a template parameter.
16
+ template<typename... TYPES>
17
+ struct OpUnion {};
18
+
19
+ //! The `Promise` template struct represents all information associated with an async
20
+ //! call to its opertion type, including all passed arguments as well as return
21
+ //! addresses.
22
+ template<typename OPERATION>
23
+ struct Promise;
24
+
25
+ //! The `PromiseUnion` template struct represents a union across a set of different
26
+ //! promise types. Through this template, a field can generically store data
27
+ //! corresponding to a variety of different async calls.
28
+ template <typename OP_UNION>
29
+ union PromiseUnion;
30
+
31
+ //! The `PromiseEnum` template struct is simply the combination of a `PromiseUnion` and
32
+ //! a discriminant that annotates the contained type.
33
+ template <typename OP_UNION>
34
+ struct PromiseEnum;
35
+
36
+ //! The `VoidState` struct is an empty struct, used as a default state type, for states
37
+ //! that are not defined the supplied program specification.
38
+ struct VoidState {};
39
+
40
+ //! The `ReturnOp` operation type is an internal type used to represent the resolution
41
+ //! of dependencies and the fullfilment of futures.
42
+ struct ReturnOp;
43
+
44
+ //! The `RamappingBarrier` template struct is a barrier that can store an arbitrary number
45
+ //! of promises with any operation type contained by its operation set, automatically
46
+ //! coalescing promises of equivalent type into work links.
47
+ template<typename OP_SET, typename ADR_TYPE = unsigned int>
48
+ struct RemappingBarrier;
49
+
50
+ //! The `UnitBarrier` template struct is a barrier that can hold up to one promise with
51
+ //! an operation type contained by its operation set.
52
+ template<typename OP_SET, typename ADR_TYPE = unsigned int>
53
+ struct UnitBarrier;
54
+
55
+ //! The `Future` template struct ties a value field of a certain type to a barrier, allowing
56
+ //! operations to await the definition of the value.
57
+ template<typename TYPE, typename BARRIER>
58
+ struct Future;
59
+
60
+
61
+
62
+ //! The `OpUnionLookup` template struct is used to derive type information about
63
+ //! `OpUnion` specializations, particularly whether or not a given type is contained
64
+ //! within the union.
65
+ template <typename QUERY, typename OP_UNION>
66
+ struct OpUnionLookup;
67
+
68
+ //! The base case of the `OpUnionLookup` template struct. This terminates all recursively
69
+ //! driven definitions from non-base cases.
70
+ template <typename QUERY>
71
+ struct OpUnionLookup <QUERY,OpUnion<>>
72
+ {
73
+ static const bool CONTAINED = false;
74
+ static const OpDisc DEPTH = 0;
75
+ static const OpDisc Q_LEVEL = DEPTH;
76
+ static const OpDisc DISC = DEPTH;
77
+ };
78
+
79
+ //! The recursive case of the `OpUnionLookup` template struct. This struct reports information
80
+ //! such as whether or not a type is contained within the supplied `OpUnion` specialization,
81
+ //! how many operations are stored in the `OpUnion`, and what the discriminant value of a
82
+ //! given operation type is for the union.
83
+ template <typename QUERY, typename HEAD, typename... TAIL>
84
+ struct OpUnionLookup <QUERY, OpUnion<HEAD, TAIL...> >
85
+ {
86
+ static const bool MATCHES = std::is_same<QUERY,HEAD>::value ;
87
+ static const bool CONTAINED = MATCHES || (OpUnionLookup<QUERY,OpUnion<TAIL...>>::CONTAINED);
88
+ static const OpDisc DEPTH = OpUnionLookup<QUERY,OpUnion<TAIL...>>::DEPTH + 1;
89
+ static const OpDisc Q_LEVEL = MATCHES ? DEPTH : (OpUnionLookup<QUERY,OpUnion<TAIL...>>::Q_LEVEL);
90
+ static const OpDisc DISC = DEPTH - Q_LEVEL;
91
+ };
92
+
93
+
94
+
95
+
96
+ //! The `OpUnionAppend` template struct simply defines an internal type which is the
97
+ //! concatenation of the first type parameter onto the second `OpUnion` specialization
98
+ //! type parameter.
99
+ template<typename HEAD, typename TAIL_UNION>
100
+ struct OpUnionAppend;
101
+
102
+ template<typename HEAD, typename... TAIL>
103
+ struct OpUnionAppend<HEAD,OpUnion<TAIL...>>
104
+ {
105
+ using Type = OpUnion<HEAD,TAIL...>;
106
+ };
107
+
108
+
109
+ //! The `OpUnionPair` template is used to determine information about pairs of
110
+ //! `OpUnion` specializations at compile time.
111
+ template <typename LEFT, typename RIGHT>
112
+ struct OpUnionPair;
113
+
114
+ template <>
115
+ struct OpUnionPair<OpUnion<>,OpUnion<>>
116
+ {
117
+ static const bool LEFT_SUBSET = true;
118
+ static const bool RIGHT_SUBSET = true;
119
+ static const bool EQUAL = true;
120
+ };
121
+
122
+ template <typename LEFT_HEAD, typename... LEFT_TAIL>
123
+ struct OpUnionPair<OpUnion<LEFT_HEAD,LEFT_TAIL...>,OpUnion<>>
124
+ {
125
+ static const bool LEFT_SUBSET = false;
126
+ static const bool RIGHT_SUBSET = true;
127
+ static const bool EQUAL = false;
128
+ };
129
+
130
+ template <typename RIGHT_HEAD, typename... RIGHT_TAIL>
131
+ struct OpUnionPair<OpUnion<>,OpUnion<RIGHT_HEAD,RIGHT_TAIL...>>
132
+ {
133
+ static const bool LEFT_SUBSET = true;
134
+ static const bool RIGHT_SUBSET = false;
135
+ static const bool EQUAL = false;
136
+ };
137
+
138
+ template <typename LEFT_HEAD, typename... LEFT_TAIL, typename RIGHT_HEAD, typename... RIGHT_TAIL>
139
+ struct OpUnionPair<OpUnion<LEFT_HEAD,LEFT_TAIL...>,OpUnion<RIGHT_HEAD,RIGHT_TAIL...>>
140
+ {
141
+ using Left = OpUnion<LEFT_HEAD,LEFT_TAIL...>;
142
+ using LeftTail = OpUnion<LEFT_TAIL...>;
143
+ using Right = OpUnion<RIGHT_HEAD,RIGHT_TAIL...>;
144
+ using RightTail = OpUnion<RIGHT_TAIL...>;
145
+
146
+ static const bool LEFT_SUBSET = OpUnionLookup<LEFT_HEAD,Right>::CONTAINED && OpUnionPair<LeftTail,Right>::LEFT_SUBSET;
147
+ static const bool RIGHT_SUBSET = OpUnionLookup<RIGHT_HEAD,Left>::CONTAINED && OpUnionPair<RightTail,Left>::LEFT_SUBET;
148
+ static const bool EQUAL = LEFT_SUBSET && RIGHT_SUBSET;
149
+ };
150
+
151
+
152
+
153
+ //! The `OpReturnFilter` tempalate struct is used to find the subset of a given
154
+ //! operation union that has a given return type. This is useful for checking
155
+ //! the validity of promises being used as return values for an operation.
156
+ template<typename RETURN, typename OP_UNION>
157
+ struct OpReturnFilter;
158
+
159
+ template<typename RETURN>
160
+ struct OpReturnFilter<RETURN,OpUnion<>>
161
+ {
162
+ using Type = OpUnion<>;
163
+ };
164
+
165
+ template<typename RETURN, typename HEAD, typename... TAIL>
166
+ struct OpReturnFilter<RETURN,OpUnion<HEAD,TAIL...>>
167
+ {
168
+
169
+ template<typename H,typename... T>
170
+ static typename std::enable_if<
171
+ ! ( std::is_same< typename Promise<H>::Return,RETURN>::value ) ,
172
+ typename OpReturnFilter<RETURN,OpUnion<T...>>::Type
173
+ >::type
174
+ filtered ();
175
+
176
+ template<typename H,typename... T>
177
+ static typename std::enable_if<
178
+ std::is_same< typename Promise<H>::Return,RETURN>::value,
179
+ typename OpUnionAppend<H,typename OpReturnFilter<RETURN,OpUnion<T...>>::Type>::Type
180
+ >::type
181
+ filtered ();
182
+
183
+ using Type = decltype(filtered<HEAD,TAIL...>());
184
+ };
185
+
186
+
187
+
188
+ //! A `TaggedSemaphore` is a semaphore that is tagged with information
189
+ //! identifying the type that contains the tag. This is useful for determining
190
+ //! the type of a barrier/future that contains the semaphore.
191
+ struct TaggedSemaphore {
192
+ unsigned int sem;
193
+ unsigned int tag;
194
+
195
+ TaggedSemaphore() = default;
196
+ __host__ __device__ TaggedSemaphore(unsigned int sem_val, unsigned int tag_val)
197
+ : sem(sem_val)
198
+ , tag(tag_val)
199
+ {}
200
+ };
201
+
202
+
203
+
204
+ //! The `LazyLoad` template struct wraps a reference to a value of a type mathcing
205
+ //! its type parameter. Instances of this template struct represent a parameter
206
+ //! to an asynchronous call that should be filled in by an asynchronous load from
207
+ //! a location in global memory.
208
+ template <typename TYPE>
209
+ struct LazyLoad {
210
+ using Type = TYPE;
211
+ Type& reference;
212
+
213
+ __host__ __device__ LazyLoad<TYPE>(Type& ref) : reference(ref) {}
214
+
215
+ __host__ __device__ operator TYPE ()
216
+ {
217
+ return reference;
218
+ }
219
+ };
220
+
221
+
222
+
223
+
224
+ //! The `has_load` template function returns true if and only if the supplied
225
+ //! parameters contains at least one argument that specializes the `LazyLoad`
226
+ //! template struct.
227
+ template <typename... TYPES>
228
+ __host__ __device__ bool has_load(TYPES...){
229
+ return false;
230
+ }
231
+
232
+ template <typename HEAD, typename... TAIL>
233
+ __host__ __device__ bool has_load(HEAD head, TAIL... tail){
234
+ return has_load(tail...);
235
+ }
236
+
237
+ template <typename HEAD, typename... TAIL>
238
+ __host__ __device__ bool has_load(LazyLoad<HEAD> head, TAIL... tail){
239
+ return true;
240
+ }
241
+
242
+
243
+
244
+ //! The `ArgTuple` template struct represents the storage of an arbitrary
245
+ //! number of fields of arbitrary type, and is used to store the argument
246
+ //! data of asynchonous calls inside promises.
247
+ template <typename... TAIL>
248
+ struct ArgTuple;
249
+
250
+ template <>
251
+ struct ArgTuple <>
252
+ {
253
+ __host__ __device__ ArgTuple<>(){}
254
+
255
+ #ifdef ASYNC_LOADS
256
+ template<typename PROGRAM>
257
+ __device__ void async_load(PROGRAM prog) {}
258
+ #endif
259
+ };
260
+
261
+ template <typename HEAD, typename... TAIL>
262
+ struct ArgTuple <HEAD, TAIL...>
263
+ {
264
+ using Tail = ArgTuple<TAIL...>;
265
+ using Head = HEAD;
266
+
267
+ Head head;
268
+ Tail tail;
269
+
270
+ __host__ __device__ ArgTuple<HEAD,TAIL...> (HEAD h, TAIL... t)
271
+ : head(h)
272
+ , tail(t...)
273
+ {}
274
+
275
+ __host__ __device__ ArgTuple<HEAD,TAIL...> (LazyLoad<HEAD> h, TAIL... t)
276
+ : head(h)
277
+ , tail(t...)
278
+ {}
279
+
280
+ template<typename PROGRAM, typename LOAD_HEAD, typename... LOAD_TAIL>
281
+ __device__ void async_load(PROGRAM prog, LOAD_HEAD, LOAD_TAIL...);
282
+
283
+
284
+ #ifdef ASYNC_LOADS
285
+ //! If asynchronous loading is enabled, an `ArgTuple` can be filled in with a
286
+ //! mixture of direct value write and asynchronous loads. This is done through
287
+ //! the async_load funtion, which direcctly writes all arguments passed in by
288
+ //! value and initiates async loads for all arguments passed in as `LazyLoad`
289
+ //! structs.
290
+ template<typename PROGRAM, typename... LOAD_TAIL>
291
+ __device__ void async_load(PROGRAM prog, HEAD h, LOAD_TAIL... t)
292
+ {
293
+ head = h;
294
+ tail.async_load(prog,t...);
295
+ }
296
+
297
+ template<typename PROGRAM, typename... LOAD_TAIL>
298
+ __device__ void async_load(PROGRAM prog, LazyLoad<HEAD> h, LOAD_TAIL... t)
299
+ {
300
+ cuda::memcpy_async(&head,&h.reference,sizeof(HEAD),prog._grp_ctx.load_barrier);
301
+ tail.async_load(prog,t...);
302
+ }
303
+ #endif
304
+
305
+ };
306
+
307
+
308
+
309
+ //! The `OpType` template struct is used to deduce the arguments and return
310
+ //! type of the supplied template argument. This is used to determine information
311
+ //! about the function signatures of operations.
312
+ template<typename TYPE>
313
+ struct OpType;
314
+
315
+ template<typename RETURN, typename... ARGS>
316
+ struct OpType < RETURN (*) (ARGS...) > {
317
+ typedef RETURN Return;
318
+ typedef ArgTuple<ARGS...> Args;
319
+ };
320
+
321
+
322
+
323
+
324
+
325
+ //! The `ReturnAdr` template struct is used to represent the destination
326
+ //! semaphore and (if applicable) future values that must be decremented
327
+ //! or filled in to resolve an operation. For void return types, only
328
+ //! a semaphore address is stored. In cases where there is no destination
329
+ //! value field, the value address is set to NULL. Likewise, promises
330
+ //! that have no barrier/future to resolve into are given a NULL semaphore
331
+ //! address.
332
+ template<typename TYPE>
333
+ struct ReturnAdr {
334
+ using Type = TYPE;
335
+
336
+ TaggedSemaphore *base;
337
+ Type *dest;
338
+ };
339
+
340
+ template<>
341
+ struct ReturnAdr<void> {
342
+ using Type = void;
343
+
344
+ TaggedSemaphore *base;
345
+ };
346
+
347
+
348
+
349
+ //! The `Return` template struct is used to represent the set of all types
350
+ //! that can be returned from an operation's `eval` function. This includes
351
+ //! returning by value, returning with a promise that returns the appropriate
352
+ //! type, or returning a future that will eventually define a value of the
353
+ //! appropriate type.
354
+ template <typename PROGRAM, typename TYPE>
355
+ struct Return {
356
+
357
+ using Type = TYPE;
358
+ using ProgramType = PROGRAM;
359
+ using ValidSet = typename OpReturnFilter<TYPE,typename ProgramType::OpSet>::Type;
360
+ using OpSet = typename ProgramType::OpSet;
361
+ using EnumType = PromiseEnum<OpSet>;
362
+ using RetAdrType = ReturnAdr<TYPE>;
363
+ using RFutureType = Future<Type,RemappingBarrier<typename PROGRAM::OpSet>>;
364
+ using UFutureType = Future<Type,UnitBarrier <typename PROGRAM::OpSet>>;
365
+
366
+ enum Form { VALUE, PROMISE, FUTURE };
367
+
368
+ union Data {
369
+ Type value;
370
+ EnumType promise;
371
+ RetAdrType future;
372
+
373
+ Data(Type val) : value (val) {}
374
+ Data(EnumType prm) : promise (prm) {}
375
+ Data(RetAdrType fut) : future (fut) {}
376
+ };
377
+
378
+ Form form;
379
+ Data data;
380
+
381
+ //! The `return_guard` function causes a compile time error when promises that do
382
+ //! not belong in the operation set of a `Return` are passed in as initializing
383
+ //! arguments. This helps to replace the more cryptic errors that come about when
384
+ //! trying to pass in an inappropriate `Promise` type.
385
+ template<typename OP_TYPE>
386
+ __host__ __device__ static constexpr Promise<OP_TYPE> return_guard(Promise<OP_TYPE> promise)
387
+ {
388
+ static_assert(
389
+ PromiseUnion<ValidSet>::template Lookup<OP_TYPE>::type::CONTAINED,
390
+ "\n\nTYPE ERROR: Type of returned promise does not match return type of "
391
+ "operation.\n\n"
392
+ "SUGGESTION: Ensure that the operation of the returned promise returns the "
393
+ "same type as the returning operation.\n\n"
394
+ );
395
+ return promise;
396
+ }
397
+
398
+ template<typename OP_TYPE>
399
+ __host__ __device__ Return<ProgramType,Type>(Promise<OP_TYPE> promise)
400
+ : form(Form::PROMISE)
401
+ , data(return_guard(promise))
402
+ {}
403
+
404
+ __host__ __device__ Return<ProgramType,Type>(Type value)
405
+ : form(Form::VALUE)
406
+ , data(value)
407
+ {}
408
+
409
+ __host__ __device__ Return<ProgramType,Type>(RetAdrType future)
410
+ : form(Form::FUTURE)
411
+ , data(future)
412
+ {}
413
+
414
+
415
+ /*
416
+ __host__ __device__ void resolve(PROGRAM program, RetAdrType ret){
417
+ switch(form){
418
+ case Form::VALUE:
419
+ ret.resolve(data.value);
420
+ break;
421
+ case Form::PROMISE:
422
+ data.promise.ret = ret;
423
+ program.async_call(data.promise);
424
+ break;
425
+ case Form::Future:
426
+
427
+ data.future.await(Promise<ReturnOp>(ret.base,ret.data,));
428
+ break;
429
+ default: //impossible
430
+ }
431
+ }
432
+ */
433
+
434
+ };
435
+
436
+
437
+ //! The `Promise` template struct represents the arguments and return information associated
438
+ //! with an asynchronous call to the input operation type parameter.
439
+ template<typename OPERATION>
440
+ struct Promise
441
+ {
442
+
443
+ typedef typename OpType< typename OPERATION::Type >::Return Return;
444
+ typedef typename OpType< typename OPERATION::Type >::Args Args;
445
+
446
+ ReturnAdr<Return> ret;
447
+ Args args;
448
+
449
+ //! The `inner_eval` template function is responsible for unpacking the components of an
450
+ //! `arg_tuple` so that they may be passed to an operation's `eval` function as individual
451
+ //! arguments.
452
+ template<typename PROGRAM, typename... TUPLE_ARGS, typename... UNROLL_ARGS>
453
+ __device__ Return inner_eval(PROGRAM program, ArgTuple<TUPLE_ARGS...> tuple_args, UNROLL_ARGS... unroll_args)
454
+ {
455
+ return inner_eval(program,tuple_args.tail,unroll_args...,tuple_args.head);
456
+ }
457
+
458
+
459
+ template<typename PROGRAM, typename... UNROLL_ARGS>
460
+ __device__ Return inner_eval(PROGRAM program, ArgTuple<> empty_tuple_args, UNROLL_ARGS... unroll_args)
461
+ {
462
+ return OPERATION::template eval<PROGRAM>(program,unroll_args...);
463
+ }
464
+
465
+
466
+ //! The call operator accepts the executing program as its only input and passes its
467
+ //! argument values alongside the program into the operation's `eval` function.
468
+ template<typename PROGRAM>
469
+ __device__ Return operator() (PROGRAM program) {
470
+ return inner_eval(program,args);
471
+ }
472
+
473
+ //! Instances of any given `Promise` specialization are constructed by passing in the
474
+ //! values of all arguments in the corresponding operation's `eval` function signature.
475
+ template<typename... ARGS>
476
+ __host__ __device__ Promise<OPERATION> ( ARGS... a ) : args(a...) {}
477
+
478
+
479
+ #ifdef ASYNC_LOADS
480
+ //! If asynchronous loading is enabled, a `Promise` can be filled in (after default
481
+ //! initialization) with a pack of argument values intermixed with appropriate
482
+ //! `LazyLoad` values. This fills in all arguments passed in by value and initiates
483
+ //! the asynchronous loading of all arguments replaced with `LazyLoad` structs.
484
+ template<typename PROGRAM, typename... ARGS>
485
+ __device__ void async_load ( PROGRAM prog, ARGS... a ) {
486
+ args.async_load(prog, a...);
487
+ }
488
+ #endif
489
+
490
+ };
491
+
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+ //!
500
+ //! The base case of the `PromiseUnion` template union defines empty functions to cap off the
501
+ //! recursion of non-base cases when evaluating promises.
502
+ //!
503
+ template <>
504
+ union PromiseUnion <OpUnion<>> {
505
+
506
+ struct Info {
507
+ static const OpDisc COUNT = 0;
508
+ };
509
+
510
+ template <typename PROGRAM, typename TYPE>
511
+ __host__ __device__ void rigid_eval( PROGRAM program ) {
512
+ return;
513
+ }
514
+
515
+ template <typename PROGRAM>
516
+ __host__ __device__ void loose_eval( PROGRAM program, OpDisc op_disc ) {
517
+ return;
518
+ }
519
+
520
+ __host__ __device__ void dyn_copy_as( OpDisc op_disc, PromiseUnion<OpUnion<>>& other){ }
521
+
522
+ PromiseUnion<OpUnion<>> () = default;
523
+
524
+ #ifdef ASYNC_LOADS
525
+ template<typename PROGRAM>
526
+ __device__ void async_load ( PROGRAM prog ) {}
527
+ #endif
528
+ };
529
+
530
+
531
+
532
+ //!
533
+ //! The recursive case of the `PromiseUnion` template union defines the `cast()`, `rigid_eval()`,
534
+ //! and `loose_eval()` template functions for the async function/parameter type corresponding to
535
+ //! the first template argument.
536
+ //!
537
+ template <typename HEAD, typename... TAIL>
538
+ union PromiseUnion<OpUnion<HEAD, TAIL...>>
539
+ {
540
+
541
+ using Head = Promise<HEAD>;
542
+ using Tail = PromiseUnion<OpUnion<TAIL...>>;
543
+
544
+ using OpSet = OpUnion<HEAD,TAIL...>;
545
+ using TailOpSet = OpUnion<TAIL...>;
546
+
547
+ Head head_form;
548
+ Tail tail_form;
549
+
550
+ public:
551
+
552
+ //! For some compilers, a union cannot diretly contain static const values.
553
+ //! To get around this, we wrap our compile time type information for PromiseUnions
554
+ //! with internl types such as the `Info` struct type or the `Looup` template
555
+ //! struct.
556
+ struct Info {
557
+ static const OpDisc COUNT = sizeof...(TAIL) + 1;
558
+ static const OpDisc INDEX = OpUnionLookup<HEAD,OpSet>::DISC;
559
+ };
560
+
561
+ template <typename TYPE>
562
+ struct Lookup { typedef OpUnionLookup<TYPE,OpSet> type; };
563
+
564
+
565
+ //! The `cast` template function allows a `PromiseUnion` to be cast to any `Promise`
566
+ //! specialization with a corresponding operation type contained by the union's
567
+ //! operation set.
568
+ template <typename TYPE>
569
+ __host__ __device__ typename std::enable_if<
570
+ std::is_same<TYPE,HEAD>::value,
571
+ Promise<TYPE>&
572
+ >::type
573
+ cast() {
574
+ return head_form;
575
+ }
576
+
577
+ template <typename TYPE>
578
+ __host__ __device__ typename std::enable_if<
579
+ (!std::is_same<TYPE,HEAD>::value) && OpUnionLookup<TYPE,TailOpSet>::CONTAINED,
580
+ Promise<TYPE>&
581
+ >::type
582
+ cast(){
583
+ return tail_form.template cast<TYPE>();
584
+ }
585
+
586
+ //! This specialization of the `cast` template function provide a useful compiler
587
+ //! error for cases when the supplied operation type is not contained by the operation
588
+ //! set of the `PromiseUnion`
589
+ template <typename TYPE>
590
+ __host__ __device__ typename std::enable_if<
591
+ (!std::is_same<TYPE,HEAD>::value) && (!OpUnionLookup<TYPE,TailOpSet>::CONTAINED),
592
+ Promise<TYPE>&
593
+ >::type
594
+ cast (){
595
+ static_assert(
596
+ (!OpUnionLookup<TYPE,TailOpSet>::CONTAINED),
597
+ "\n\nTYPE ERROR: Promise type does not exist in union.\n\n"
598
+ );
599
+ }
600
+
601
+
602
+ //! The `rigid_eval` template function evaluates the contained union as a `Promise`
603
+ //! corresponding to the supplied operation type. This allows the compiler to deduce
604
+ //! which type of operation is being performed and optimize out branches it knows
605
+ //! will never be taken.
606
+ template <typename PROGRAM, typename TYPE >
607
+ __host__ __device__ typename std::enable_if<
608
+ std::is_same<TYPE,HEAD>::value,
609
+ void
610
+ >::type
611
+ rigid_eval(
612
+ PROGRAM program
613
+ ) {
614
+ head_form(program);
615
+ }
616
+
617
+ template <typename PROGRAM, typename TYPE>
618
+ __host__ __device__ typename std::enable_if<
619
+ (!std::is_same<TYPE,HEAD>::value) && OpUnionLookup<TYPE,TailOpSet>::CONTAINED,
620
+ void
621
+ >::type
622
+ rigid_eval(
623
+ PROGRAM program
624
+ ) {
625
+ tail_form.template rigid_eval<PROGRAM,TYPE>(program);
626
+ }
627
+
628
+ template <typename PROGRAM, typename TYPE>
629
+ __host__ __device__ typename std::enable_if<
630
+ (!std::is_same<TYPE,HEAD>::value) && (!OpUnionLookup<TYPE,TailOpSet>::CONTAINED),
631
+ void
632
+ >::type
633
+ rigid_eval(
634
+ PROGRAM program
635
+ ) {
636
+ static_assert(
637
+ (!OpUnionLookup<TYPE,TailOpSet>::CONTAINED),
638
+ "\n\nTYPE ERROR: Promise type does not exist in union.\n\n"
639
+ );
640
+ }
641
+
642
+
643
+ //! The `loose_eval` template function evaluates the contained union as a `Promise`
644
+ //! corresponding to an operation with the supplied discriminant value. This should
645
+ //! be used only in situations where the type of the operation is not known at
646
+ //! compile time.
647
+ template <typename PROGRAM>
648
+ __host__ __device__ void loose_eval (
649
+ PROGRAM program,
650
+ OpDisc disc
651
+ ) {
652
+ if(disc == Info::INDEX){
653
+ head_form(program);
654
+ } else {
655
+ tail_form. template loose_eval<PROGRAM>(program,disc-1);
656
+ }
657
+
658
+ }
659
+
660
+
661
+ //! The copy_as and dyn_copy_as functions copy data from one union to another
662
+ //! as a particular type of `Promise`.
663
+ template <typename TYPE>
664
+ __host__ __device__ void copy_as(PromiseUnion<OpSet>& other){
665
+ cast<TYPE>() = other.template cast<TYPE>();
666
+ }
667
+
668
+
669
+ __host__ __device__ void dyn_copy_as(OpDisc disc, PromiseUnion<OpSet>& other){
670
+ if( disc == Info::INDEX ){
671
+ cast<HEAD>() = other.cast<HEAD>();
672
+ } else {
673
+ tail_form.dyn_copy_as(disc-1,other.tail_form);
674
+ }
675
+ }
676
+
677
+ __host__ __device__ PromiseUnion<OpSet> () : tail_form() {}
678
+
679
+ //! A `PromiseUnion` can be intialized by a `Promise` of any type contained by
680
+ //! its operation set. All other `Promise` types cause a compile time error to
681
+ //! be thrown alongside the helpful message below.
682
+ template< typename OP_TYPE>
683
+ __host__ __device__ PromiseUnion<OpSet> ( Promise<OP_TYPE> prom ) {
684
+ static_assert(
685
+ OpUnionLookup<OP_TYPE,OpSet>::CONTAINED,
686
+ "\n\nTYPE ERROR: Type of assigned promise does not exist in promise union.\n\n"
687
+ "SUGGESTION: Double-check the signature of the promise union and make sure "
688
+ "its OpUnion template parameter contains the desired operation type.\n\n"
689
+ "SUGGESTION: Double-check the type signature of the promise and make sure "
690
+ "it is the correct operation type.\n\n"
691
+ );
692
+ cast<OP_TYPE>() = prom;
693
+ }
694
+
695
+
696
+ };
697
+
698
+
699
+
700
+
701
+
702
+ //! A `PromiseEnum` is just a tagged version of a `PromiseUnion`, combining the
703
+ //! union type with a discriminant that annotates the type of `Promise` contained
704
+ //! by the union.
705
+ template <typename OP_SET>
706
+ struct PromiseEnum {
707
+
708
+ using UnionType = PromiseUnion<OP_SET>;
709
+
710
+ UnionType data;
711
+ OpDisc disc;
712
+
713
+ PromiseEnum() = default;
714
+
715
+ __host__ __device__ PromiseEnum<OP_SET>(UnionType uni, OpDisc d)
716
+ : data(uni)
717
+ , disc(d)
718
+ {}
719
+
720
+
721
+ template<typename OP_TYPE>
722
+ __host__ __device__ constexpr static Promise<OP_TYPE> promise_guard (Promise<OP_TYPE> promise){
723
+ static_assert(
724
+ UnionType::template Lookup<OP_TYPE>::type::CONTAINED,
725
+ "\n\nTYPE ERROR: Type of assigned promise does not exist in promise enum.\n\n"
726
+ "SUGGESTION: Double-check the signature of the promise enum and make sure "
727
+ "its OpUnion template parameter contains the desired operation type.\n\n"
728
+ "SUGGESTION: Double-check the type signature of the promise and make sure "
729
+ "it is the correct operation type.\n\n"
730
+ );
731
+ return promise;
732
+ }
733
+
734
+
735
+ template<typename OP_TYPE>
736
+ __host__ __device__ PromiseEnum<OP_SET>(Promise<OP_TYPE> promise)
737
+ : data(promise_guard(promise))
738
+ , disc(UnionType::template Lookup<OP_TYPE>::type::DISC)
739
+ {}
740
+
741
+
742
+ };
743
+
744
+
745
+
746
+ #endif
747
+