forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_ExecPolicy.hpp
1221 lines (1044 loc) · 44.9 KB
/
Kokkos_ExecPolicy.hpp
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#include <Kokkos_Macros.hpp>
static_assert(false,
"Including non-public Kokkos header files is not allowed.");
#endif
#ifndef KOKKOS_EXECPOLICY_HPP
#define KOKKOS_EXECPOLICY_HPP
#include <Kokkos_Core_fwd.hpp>
#include <impl/Kokkos_Traits.hpp>
#include <impl/Kokkos_Error.hpp>
#include <impl/Kokkos_AnalyzePolicy.hpp>
#include <Kokkos_Concepts.hpp>
#include <typeinfo>
#include <limits>
//----------------------------------------------------------------------------
namespace Kokkos {
struct ParallelForTag {};
struct ParallelScanTag {};
struct ParallelReduceTag {};
struct ChunkSize {
int value;
ChunkSize(int value_) : value(value_) {}
};
/** \brief Execution policy for work over a range of an integral type.
*
* Valid template argument options:
*
* With a specified execution space:
* < ExecSpace , WorkTag , { IntConst | IntType } >
* < ExecSpace , WorkTag , void >
* < ExecSpace , { IntConst | IntType } , void >
* < ExecSpace , void , void >
*
* With the default execution space:
* < WorkTag , { IntConst | IntType } , void >
* < WorkTag , void , void >
* < { IntConst | IntType } , void , void >
* < void , void , void >
*
* IntType is a fundamental integral type
* IntConst is an Impl::integral_constant< IntType , Blocking >
*
* Blocking is the granularity of partitioning the range among threads.
*/
template <class... Properties>
class RangePolicy : public Impl::PolicyTraits<Properties...> {
public:
using traits = Impl::PolicyTraits<Properties...>;
private:
typename traits::execution_space m_space;
typename traits::index_type m_begin;
typename traits::index_type m_end;
typename traits::index_type m_granularity;
typename traits::index_type m_granularity_mask;
template <class... OtherProperties>
friend class RangePolicy;
public:
//! Tag this class as an execution policy
using execution_policy = RangePolicy<Properties...>;
using member_type = typename traits::index_type;
using index_type = typename traits::index_type;
KOKKOS_INLINE_FUNCTION const typename traits::execution_space& space() const {
return m_space;
}
KOKKOS_INLINE_FUNCTION member_type begin() const { return m_begin; }
KOKKOS_INLINE_FUNCTION member_type end() const { return m_end; }
// TODO: find a better workaround for Clangs weird instantiation order
// This thing is here because of an instantiation error, where the RangePolicy
// is inserted into FunctorValue Traits, which tries decltype on the operator.
// It tries to do this even though the first argument of parallel for clearly
// doesn't match.
void operator()(const int&) const {}
template <class... OtherProperties>
RangePolicy(const RangePolicy<OtherProperties...>& p)
: traits(p), // base class may contain data such as desired occupancy
m_space(p.m_space),
m_begin(p.m_begin),
m_end(p.m_end),
m_granularity(p.m_granularity),
m_granularity_mask(p.m_granularity_mask) {}
inline RangePolicy()
: m_space(),
m_begin(0),
m_end(0),
m_granularity(0),
m_granularity_mask(0) {}
/** \brief Total range */
template <typename IndexType1, typename IndexType2,
std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
std::is_convertible_v<IndexType2, member_type>),
bool> = false>
inline RangePolicy(const IndexType1 work_begin, const IndexType2 work_end)
: RangePolicy(typename traits::execution_space(), work_begin, work_end) {}
/** \brief Total range */
template <typename IndexType1, typename IndexType2,
std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
std::is_convertible_v<IndexType2, member_type>),
bool> = false>
inline RangePolicy(const typename traits::execution_space& work_space,
const IndexType1 work_begin, const IndexType2 work_end)
: m_space(work_space),
m_begin(work_begin),
m_end(work_end),
m_granularity(0),
m_granularity_mask(0) {
check_conversion_safety(work_begin);
check_conversion_safety(work_end);
check_bounds_validity();
set_auto_chunk_size();
}
template <typename IndexType1, typename IndexType2,
std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
std::is_convertible_v<IndexType2, member_type>),
bool> = false>
RangePolicy(const typename traits::execution_space& work_space,
const IndexType1 work_begin, const IndexType2 work_end,
const ChunkSize chunk_size)
: m_space(work_space),
m_begin(work_begin),
m_end(work_end),
m_granularity(0),
m_granularity_mask(0) {
check_conversion_safety(work_begin);
check_conversion_safety(work_end);
check_bounds_validity();
set_chunk_size(chunk_size.value);
}
/** \brief Total range */
template <typename IndexType1, typename IndexType2, typename... Args,
std::enable_if_t<(std::is_convertible_v<IndexType1, member_type> &&
std::is_convertible_v<IndexType2, member_type>),
bool> = false>
RangePolicy(const IndexType1 work_begin, const IndexType2 work_end,
const ChunkSize chunk_size)
: RangePolicy(typename traits::execution_space(), work_begin, work_end,
chunk_size) {}
public:
#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
KOKKOS_DEPRECATED_WITH_COMMENT("Use set_chunk_size instead")
inline void set(ChunkSize chunksize) {
m_granularity = chunksize.value;
m_granularity_mask = m_granularity - 1;
}
#endif
public:
/** \brief return chunk_size */
inline member_type chunk_size() const { return m_granularity; }
/** \brief set chunk_size to a discrete value*/
inline RangePolicy& set_chunk_size(int chunk_size) {
m_granularity = chunk_size;
m_granularity_mask = m_granularity - 1;
return *this;
}
private:
/** \brief finalize chunk_size if it was set to AUTO*/
inline void set_auto_chunk_size() {
#ifdef KOKKOS_ENABLE_SYCL
if (std::is_same_v<typename traits::execution_space,
Kokkos::Experimental::SYCL>) {
// chunk_size <=1 lets the compiler choose the workgroup size when
// launching kernels
m_granularity = 1;
m_granularity_mask = 0;
return;
}
#endif
auto concurrency = static_cast<int64_t>(m_space.concurrency());
if (concurrency == 0) concurrency = 1;
if (m_granularity > 0) {
if (!Impl::is_integral_power_of_two(m_granularity))
Kokkos::abort("RangePolicy blocking granularity must be power of two");
}
int64_t new_chunk_size = 1;
while (new_chunk_size * 100 * concurrency <
static_cast<int64_t>(m_end - m_begin))
new_chunk_size *= 2;
if (new_chunk_size < 128) {
new_chunk_size = 1;
while ((new_chunk_size * 40 * concurrency <
static_cast<int64_t>(m_end - m_begin)) &&
(new_chunk_size < 128))
new_chunk_size *= 2;
}
m_granularity = new_chunk_size;
m_granularity_mask = m_granularity - 1;
}
void check_bounds_validity() {
if (m_end < m_begin) {
std::string msg = "Kokkos::RangePolicy bounds error: The lower bound (" +
std::to_string(m_begin) +
") is greater than the upper bound (" +
std::to_string(m_end) + ").\n";
#ifndef KOKKOS_ENABLE_DEPRECATED_CODE_4
Kokkos::abort(msg.c_str());
#endif
m_begin = 0;
m_end = 0;
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
Kokkos::Impl::log_warning(msg);
#endif
}
}
// To be replaced with std::in_range (c++20)
template <typename IndexType>
static void check_conversion_safety(const IndexType bound) {
#if !defined(KOKKOS_ENABLE_DEPRECATED_CODE_4) || \
defined(KOKKOS_ENABLE_DEPRECATION_WARNINGS)
std::string msg =
"Kokkos::RangePolicy bound type error: an unsafe implicit conversion "
"is performed on a bound (" +
std::to_string(bound) +
"), which may "
"not preserve its original value.\n";
bool warn = false;
if constexpr (std::is_signed_v<IndexType> !=
std::is_signed_v<member_type>) {
// check signed to unsigned
if constexpr (std::is_signed_v<IndexType>)
warn |= (bound < static_cast<IndexType>(
std::numeric_limits<member_type>::min()));
// check unsigned to signed
if constexpr (std::is_signed_v<member_type>)
warn |= (bound > static_cast<IndexType>(
std::numeric_limits<member_type>::max()));
}
// check narrowing
warn |= (static_cast<IndexType>(static_cast<member_type>(bound)) != bound);
if (warn) {
#ifndef KOKKOS_ENABLE_DEPRECATED_CODE_4
Kokkos::abort(msg.c_str());
#endif
#ifdef KOKKOS_ENABLE_DEPRECATION_WARNINGS
Kokkos::Impl::log_warning(msg);
#endif
}
#else
(void)bound;
#endif
}
public:
/** \brief Subrange for a partition's rank and size.
*
* Typically used to partition a range over a group of threads.
*/
struct WorkRange {
using work_tag = typename RangePolicy<Properties...>::work_tag;
using member_type = typename RangePolicy<Properties...>::member_type;
KOKKOS_INLINE_FUNCTION member_type begin() const { return m_begin; }
KOKKOS_INLINE_FUNCTION member_type end() const { return m_end; }
/** \brief Subrange for a partition's rank and size.
*
* Typically used to partition a range over a group of threads.
*/
KOKKOS_INLINE_FUNCTION
WorkRange(const RangePolicy& range, const int part_rank,
const int part_size)
: m_begin(0), m_end(0) {
if (part_size) {
// Split evenly among partitions, then round up to the granularity.
const member_type work_part =
((((range.end() - range.begin()) + (part_size - 1)) / part_size) +
range.m_granularity_mask) &
~member_type(range.m_granularity_mask);
m_begin = range.begin() + work_part * part_rank;
m_end = m_begin + work_part;
if (range.end() < m_begin) m_begin = range.end();
if (range.end() < m_end) m_end = range.end();
}
}
private:
member_type m_begin;
member_type m_end;
WorkRange();
WorkRange& operator=(const WorkRange&);
};
};
} // namespace Kokkos
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
namespace Kokkos {
namespace Impl {
template <class ExecSpace, class... Properties>
class TeamPolicyInternal : public Impl::PolicyTraits<Properties...> {
private:
using traits = Impl::PolicyTraits<Properties...>;
public:
using index_type = typename traits::index_type;
//----------------------------------------
/** \brief Query maximum team size for a given functor.
*
* This size takes into account execution space concurrency limitations and
* scratch memory space limitations for reductions, team reduce/scan, and
* team shared memory.
*
* This function only works for single-operator functors.
* With multi-operator functors it cannot be determined
* which operator will be called.
*/
template <class FunctorType>
static int team_size_max(const FunctorType&);
/** \brief Query recommended team size for a given functor.
*
* This size takes into account execution space concurrency limitations and
* scratch memory space limitations for reductions, team reduce/scan, and
* team shared memory.
*
* This function only works for single-operator functors.
* With multi-operator functors it cannot be determined
* which operator will be called.
*/
template <class FunctorType>
static int team_size_recommended(const FunctorType&);
template <class FunctorType>
static int team_size_recommended(const FunctorType&, const int&);
template <class FunctorType>
int team_size_recommended(const FunctorType& functor,
const int vector_length);
//----------------------------------------
/** \brief Construct policy with the given instance of the execution space */
TeamPolicyInternal(const typename traits::execution_space&,
int league_size_request, int team_size_request,
int vector_length_request = 1);
TeamPolicyInternal(const typename traits::execution_space&,
int league_size_request, const Kokkos::AUTO_t&,
int vector_length_request = 1);
/** \brief Construct policy with the default instance of the execution space
*/
TeamPolicyInternal(int league_size_request, int team_size_request,
int vector_length_request = 1);
TeamPolicyInternal(int league_size_request, const Kokkos::AUTO_t&,
int vector_length_request = 1);
/* TeamPolicyInternal( int league_size_request , int team_size_request );
TeamPolicyInternal( int league_size_request , const Kokkos::AUTO_t & );*/
/** \brief The actual league size (number of teams) of the policy.
*
* This may be smaller than the requested league size due to limitations
* of the execution space.
*/
KOKKOS_INLINE_FUNCTION int league_size() const;
/** \brief The actual team size (number of threads per team) of the policy.
*
* This may be smaller than the requested team size due to limitations
* of the execution space.
*/
KOKKOS_INLINE_FUNCTION int team_size() const;
/** \brief Whether the policy has an automatically determined team size
*/
inline bool impl_auto_team_size() const;
/** \brief Whether the policy has an automatically determined vector length
*/
inline bool impl_auto_vector_length() const;
static int vector_length_max();
KOKKOS_INLINE_FUNCTION int impl_vector_length() const;
inline typename traits::index_type chunk_size() const;
inline TeamPolicyInternal& set_chunk_size(int chunk_size);
/** \brief Parallel execution of a functor calls the functor once with
* each member of the execution policy.
*/
struct member_type {
/** \brief Handle to the currently executing team shared scratch memory */
KOKKOS_INLINE_FUNCTION
typename traits::execution_space::scratch_memory_space team_shmem() const;
/** \brief Rank of this team within the league of teams */
KOKKOS_INLINE_FUNCTION int league_rank() const;
/** \brief Number of teams in the league */
KOKKOS_INLINE_FUNCTION int league_size() const;
/** \brief Rank of this thread within this team */
KOKKOS_INLINE_FUNCTION int team_rank() const;
/** \brief Number of threads in this team */
KOKKOS_INLINE_FUNCTION int team_size() const;
/** \brief Barrier among the threads of this team */
KOKKOS_INLINE_FUNCTION void team_barrier() const;
/** \brief Intra-team reduction. Returns join of all values of the team
* members. */
template <class JoinOp>
KOKKOS_INLINE_FUNCTION typename JoinOp::value_type team_reduce(
const typename JoinOp::value_type, const JoinOp&) const;
/** \brief Intra-team exclusive prefix sum with team_rank() ordering.
*
* The highest rank thread can compute the reduction total as
* reduction_total = dev.team_scan( value ) + value ;
*/
template <typename Type>
KOKKOS_INLINE_FUNCTION Type team_scan(const Type& value) const;
/** \brief Intra-team exclusive prefix sum with team_rank() ordering
* with intra-team non-deterministic ordering accumulation.
*
* The global inter-team accumulation value will, at the end of the
* league's parallel execution, be the scan's total.
* Parallel execution ordering of the league's teams is non-deterministic.
* As such the base value for each team's scan operation is similarly
* non-deterministic.
*/
template <typename Type>
KOKKOS_INLINE_FUNCTION Type team_scan(const Type& value,
Type* const global_accum) const;
};
};
struct PerTeamValue {
size_t value;
PerTeamValue(size_t arg);
};
struct PerThreadValue {
size_t value;
PerThreadValue(size_t arg);
};
template <class iType, class... Args>
struct ExtractVectorLength {
static inline iType value(
std::enable_if_t<std::is_integral<iType>::value, iType> val, Args...) {
return val;
}
static inline std::enable_if_t<!std::is_integral<iType>::value, int> value(
std::enable_if_t<!std::is_integral<iType>::value, iType>, Args...) {
return 1;
}
};
template <class iType, class... Args>
inline std::enable_if_t<std::is_integral<iType>::value, iType>
extract_vector_length(iType val, Args...) {
return val;
}
template <class iType, class... Args>
inline std::enable_if_t<!std::is_integral<iType>::value, int>
extract_vector_length(iType, Args...) {
return 1;
}
} // namespace Impl
Impl::PerTeamValue PerTeam(const size_t& arg);
Impl::PerThreadValue PerThread(const size_t& arg);
struct ScratchRequest {
int level;
size_t per_team;
size_t per_thread;
inline ScratchRequest(const int& level_,
const Impl::PerTeamValue& team_value) {
level = level_;
per_team = team_value.value;
per_thread = 0;
}
inline ScratchRequest(const int& level_,
const Impl::PerThreadValue& thread_value) {
level = level_;
per_team = 0;
per_thread = thread_value.value;
}
inline ScratchRequest(const int& level_, const Impl::PerTeamValue& team_value,
const Impl::PerThreadValue& thread_value) {
level = level_;
per_team = team_value.value;
per_thread = thread_value.value;
}
inline ScratchRequest(const int& level_,
const Impl::PerThreadValue& thread_value,
const Impl::PerTeamValue& team_value) {
level = level_;
per_team = team_value.value;
per_thread = thread_value.value;
}
};
// Throws a runtime exception if level is not `0` or `1`
void team_policy_check_valid_storage_level_argument(int level);
/** \brief Execution policy for parallel work over a league of teams of
* threads.
*
* The work functor is called for each thread of each team such that
* the team's member threads are guaranteed to be concurrent.
*
* The team's threads have access to team shared scratch memory and
* team collective operations.
*
* If the WorkTag is non-void then the first calling argument of the
* work functor's parentheses operator is 'const WorkTag &'.
* This allows a functor to have multiple work member functions.
*
* Order of template arguments does not matter, since the implementation
* uses variadic templates. Each and any of the template arguments can
* be omitted.
*
* Possible Template arguments and their default values:
* ExecutionSpace (DefaultExecutionSpace): where to execute code. Must be
* enabled. WorkTag (none): Tag which is used as the first argument for the
* functor operator. Schedule<Type> (Schedule<Static>): Scheduling Policy
* (Dynamic, or Static). IndexType<Type> (IndexType<ExecutionSpace::size_type>:
* Integer Index type used to iterate over the Index space.
* LaunchBounds<unsigned,unsigned> Launch Bounds for CUDA compilation,
* default of LaunchBounds<0,0> indicates no launch bounds specified.
*/
template <class... Properties>
class TeamPolicy
: public Impl::TeamPolicyInternal<
typename Impl::PolicyTraits<Properties...>::execution_space,
Properties...> {
using internal_policy = Impl::TeamPolicyInternal<
typename Impl::PolicyTraits<Properties...>::execution_space,
Properties...>;
template <class... OtherProperties>
friend class TeamPolicy;
public:
using traits = Impl::PolicyTraits<Properties...>;
using execution_policy = TeamPolicy<Properties...>;
TeamPolicy() : internal_policy(0, AUTO) {}
/** \brief Construct policy with the given instance of the execution space */
TeamPolicy(const typename traits::execution_space& space_,
int league_size_request, int team_size_request,
int vector_length_request = 1)
: internal_policy(space_, league_size_request, team_size_request,
vector_length_request) {}
TeamPolicy(const typename traits::execution_space& space_,
int league_size_request, const Kokkos::AUTO_t&,
int vector_length_request = 1)
: internal_policy(space_, league_size_request, Kokkos::AUTO(),
vector_length_request) {}
TeamPolicy(const typename traits::execution_space& space_,
int league_size_request, const Kokkos::AUTO_t&,
const Kokkos::AUTO_t&)
: internal_policy(space_, league_size_request, Kokkos::AUTO(),
Kokkos::AUTO()) {}
TeamPolicy(const typename traits::execution_space& space_,
int league_size_request, const int team_size_request,
const Kokkos::AUTO_t&)
: internal_policy(space_, league_size_request, team_size_request,
Kokkos::AUTO()) {}
/** \brief Construct policy with the default instance of the execution space
*/
TeamPolicy(int league_size_request, int team_size_request,
int vector_length_request = 1)
: internal_policy(league_size_request, team_size_request,
vector_length_request) {}
TeamPolicy(int league_size_request, const Kokkos::AUTO_t&,
int vector_length_request = 1)
: internal_policy(league_size_request, Kokkos::AUTO(),
vector_length_request) {}
TeamPolicy(int league_size_request, const Kokkos::AUTO_t&,
const Kokkos::AUTO_t&)
: internal_policy(league_size_request, Kokkos::AUTO(), Kokkos::AUTO()) {}
TeamPolicy(int league_size_request, const int team_size_request,
const Kokkos::AUTO_t&)
: internal_policy(league_size_request, team_size_request,
Kokkos::AUTO()) {}
template <class... OtherProperties>
TeamPolicy(const TeamPolicy<OtherProperties...> p) : internal_policy(p) {
// Cannot call converting constructor in the member initializer list because
// it is not a direct base.
internal_policy::traits::operator=(p);
}
private:
TeamPolicy(const internal_policy& p) : internal_policy(p) {}
public:
inline TeamPolicy& set_chunk_size(int chunk) {
static_assert(std::is_same<decltype(internal_policy::set_chunk_size(chunk)),
internal_policy&>::value,
"internal set_chunk_size should return a reference");
return static_cast<TeamPolicy&>(internal_policy::set_chunk_size(chunk));
}
inline TeamPolicy& set_scratch_size(const int& level,
const Impl::PerTeamValue& per_team) {
static_assert(std::is_same<decltype(internal_policy::set_scratch_size(
level, per_team)),
internal_policy&>::value,
"internal set_chunk_size should return a reference");
team_policy_check_valid_storage_level_argument(level);
return static_cast<TeamPolicy&>(
internal_policy::set_scratch_size(level, per_team));
}
inline TeamPolicy& set_scratch_size(const int& level,
const Impl::PerThreadValue& per_thread) {
team_policy_check_valid_storage_level_argument(level);
return static_cast<TeamPolicy&>(
internal_policy::set_scratch_size(level, per_thread));
}
inline TeamPolicy& set_scratch_size(const int& level,
const Impl::PerTeamValue& per_team,
const Impl::PerThreadValue& per_thread) {
team_policy_check_valid_storage_level_argument(level);
return static_cast<TeamPolicy&>(
internal_policy::set_scratch_size(level, per_team, per_thread));
}
inline TeamPolicy& set_scratch_size(const int& level,
const Impl::PerThreadValue& per_thread,
const Impl::PerTeamValue& per_team) {
team_policy_check_valid_storage_level_argument(level);
return static_cast<TeamPolicy&>(
internal_policy::set_scratch_size(level, per_team, per_thread));
}
};
namespace Impl {
template <typename iType, class TeamMemberType>
struct TeamThreadRangeBoundariesStruct {
private:
KOKKOS_INLINE_FUNCTION static iType ibegin(const iType& arg_begin,
const iType& arg_end,
const iType& arg_rank,
const iType& arg_size) {
return arg_begin +
((arg_end - arg_begin + arg_size - 1) / arg_size) * arg_rank;
}
KOKKOS_INLINE_FUNCTION static iType iend(const iType& arg_begin,
const iType& arg_end,
const iType& arg_rank,
const iType& arg_size) {
const iType end_ =
arg_begin +
((arg_end - arg_begin + arg_size - 1) / arg_size) * (arg_rank + 1);
return end_ < arg_end ? end_ : arg_end;
}
public:
using index_type = iType;
const iType start;
const iType end;
enum { increment = 1 };
const TeamMemberType& thread;
KOKKOS_INLINE_FUNCTION
TeamThreadRangeBoundariesStruct(const TeamMemberType& arg_thread,
const iType& arg_end)
: start(
ibegin(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
end(iend(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
thread(arg_thread) {}
KOKKOS_INLINE_FUNCTION
TeamThreadRangeBoundariesStruct(const TeamMemberType& arg_thread,
const iType& arg_begin, const iType& arg_end)
: start(ibegin(arg_begin, arg_end, arg_thread.team_rank(),
arg_thread.team_size())),
end(iend(arg_begin, arg_end, arg_thread.team_rank(),
arg_thread.team_size())),
thread(arg_thread) {}
};
template <typename iType, class TeamMemberType>
struct TeamVectorRangeBoundariesStruct {
private:
KOKKOS_INLINE_FUNCTION static iType ibegin(const iType& arg_begin,
const iType& arg_end,
const iType& arg_rank,
const iType& arg_size) {
return arg_begin +
((arg_end - arg_begin + arg_size - 1) / arg_size) * arg_rank;
}
KOKKOS_INLINE_FUNCTION static iType iend(const iType& arg_begin,
const iType& arg_end,
const iType& arg_rank,
const iType& arg_size) {
const iType end_ =
arg_begin +
((arg_end - arg_begin + arg_size - 1) / arg_size) * (arg_rank + 1);
return end_ < arg_end ? end_ : arg_end;
}
public:
using index_type = iType;
const iType start;
const iType end;
enum { increment = 1 };
const TeamMemberType& thread;
KOKKOS_INLINE_FUNCTION
TeamVectorRangeBoundariesStruct(const TeamMemberType& arg_thread,
const iType& arg_end)
: start(
ibegin(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
end(iend(0, arg_end, arg_thread.team_rank(), arg_thread.team_size())),
thread(arg_thread) {}
KOKKOS_INLINE_FUNCTION
TeamVectorRangeBoundariesStruct(const TeamMemberType& arg_thread,
const iType& arg_begin, const iType& arg_end)
: start(ibegin(arg_begin, arg_end, arg_thread.team_rank(),
arg_thread.team_size())),
end(iend(arg_begin, arg_end, arg_thread.team_rank(),
arg_thread.team_size())),
thread(arg_thread) {}
};
template <typename iType, class TeamMemberType>
struct ThreadVectorRangeBoundariesStruct {
using index_type = iType;
const index_type start;
const index_type end;
enum { increment = 1 };
KOKKOS_INLINE_FUNCTION
constexpr ThreadVectorRangeBoundariesStruct(const TeamMemberType,
const index_type& count) noexcept
: start(static_cast<index_type>(0)), end(count) {}
KOKKOS_INLINE_FUNCTION
constexpr ThreadVectorRangeBoundariesStruct(
const TeamMemberType, const index_type& arg_begin,
const index_type& arg_end) noexcept
: start(static_cast<index_type>(arg_begin)), end(arg_end) {}
};
template <class TeamMemberType>
struct ThreadSingleStruct {
const TeamMemberType& team_member;
KOKKOS_INLINE_FUNCTION
ThreadSingleStruct(const TeamMemberType& team_member_)
: team_member(team_member_) {}
};
template <class TeamMemberType>
struct VectorSingleStruct {
const TeamMemberType& team_member;
KOKKOS_INLINE_FUNCTION
VectorSingleStruct(const TeamMemberType& team_member_)
: team_member(team_member_) {}
};
} // namespace Impl
/** \brief Execution policy for parallel work over a threads within a team.
*
* The range is split over all threads in a team. The Mapping scheme depends on
* the architecture. This policy is used together with a parallel pattern as a
* nested layer within a kernel launched with the TeamPolicy. This variant
* expects a single count. So the range is (0,count].
*/
template <typename iType, class TeamMemberType, class _never_use_this_overload>
KOKKOS_INLINE_FUNCTION_DELETED
Impl::TeamThreadRangeBoundariesStruct<iType, TeamMemberType>
TeamThreadRange(const TeamMemberType&, const iType& count) = delete;
/** \brief Execution policy for parallel work over a threads within a team.
*
* The range is split over all threads in a team. The Mapping scheme depends on
* the architecture. This policy is used together with a parallel pattern as a
* nested layer within a kernel launched with the TeamPolicy. This variant
* expects a begin and end. So the range is (begin,end].
*/
template <typename iType1, typename iType2, class TeamMemberType,
class _never_use_this_overload>
KOKKOS_INLINE_FUNCTION_DELETED Impl::TeamThreadRangeBoundariesStruct<
std::common_type_t<iType1, iType2>, TeamMemberType>
TeamThreadRange(const TeamMemberType&, const iType1& begin,
const iType2& end) = delete;
/** \brief Execution policy for parallel work over a threads within a team.
*
* The range is split over all threads in a team. The Mapping scheme depends on
* the architecture. This policy is used together with a parallel pattern as a
* nested layer within a kernel launched with the TeamPolicy. This variant
* expects a single count. So the range is (0,count].
*/
template <typename iType, class TeamMemberType, class _never_use_this_overload>
KOKKOS_INLINE_FUNCTION_DELETED
Impl::TeamThreadRangeBoundariesStruct<iType, TeamMemberType>
TeamVectorRange(const TeamMemberType&, const iType& count) = delete;
/** \brief Execution policy for parallel work over a threads within a team.
*
* The range is split over all threads in a team. The Mapping scheme depends on
* the architecture. This policy is used together with a parallel pattern as a
* nested layer within a kernel launched with the TeamPolicy. This variant
* expects a begin and end. So the range is (begin,end].
*/
template <typename iType1, typename iType2, class TeamMemberType,
class _never_use_this_overload>
KOKKOS_INLINE_FUNCTION_DELETED Impl::TeamThreadRangeBoundariesStruct<
std::common_type_t<iType1, iType2>, TeamMemberType>
TeamVectorRange(const TeamMemberType&, const iType1& begin,
const iType2& end) = delete;
/** \brief Execution policy for a vector parallel loop.
*
* The range is split over all vector lanes in a thread. The Mapping scheme
* depends on the architecture. This policy is used together with a parallel
* pattern as a nested layer within a kernel launched with the TeamPolicy. This
* variant expects a single count. So the range is (0,count].
*/
template <typename iType, class TeamMemberType, class _never_use_this_overload>
KOKKOS_INLINE_FUNCTION_DELETED
Impl::ThreadVectorRangeBoundariesStruct<iType, TeamMemberType>
ThreadVectorRange(const TeamMemberType&, const iType& count) = delete;
template <typename iType1, typename iType2, class TeamMemberType,
class _never_use_this_overload>
KOKKOS_INLINE_FUNCTION_DELETED Impl::ThreadVectorRangeBoundariesStruct<
std::common_type_t<iType1, iType2>, TeamMemberType>
ThreadVectorRange(const TeamMemberType&, const iType1& arg_begin,
const iType2& arg_end) = delete;
namespace Impl {
enum class TeamMDRangeLastNestLevel : bool { NotLastNestLevel, LastNestLevel };
enum class TeamMDRangeParThread : bool { NotParThread, ParThread };
enum class TeamMDRangeParVector : bool { NotParVector, ParVector };
enum class TeamMDRangeThreadAndVector : bool { NotBoth, Both };
template <typename Rank, TeamMDRangeThreadAndVector ThreadAndVector>
struct HostBasedNestLevel;
template <typename Rank, TeamMDRangeThreadAndVector ThreadAndVector>
struct AcceleratorBasedNestLevel;
// ThreadAndVectorNestLevel determines on which nested level parallelization
// happens.
// - Rank is Kokkos::Rank<TotalNestLevel, Iter>
// - TotalNestLevel is the total number of loop nests
// - Iter is whether to go forward or backward through ranks (i.e. the
// iteration order for MDRangePolicy)
// - ThreadAndVector determines whether both vector and thread parallelism is
// in use
template <typename Rank, typename ExecSpace,
TeamMDRangeThreadAndVector ThreadAndVector>
struct ThreadAndVectorNestLevel;
struct NoReductionTag {};
template <typename Rank, typename TeamMDPolicy, typename Lambda,
typename ReductionValueType>
KOKKOS_INLINE_FUNCTION void md_parallel_impl(TeamMDPolicy const& policy,
Lambda const& lambda,
ReductionValueType&& val);
} // namespace Impl
template <typename Rank, typename TeamHandle>
struct TeamThreadMDRange;
template <unsigned N, Iterate OuterDir, Iterate InnerDir, typename TeamHandle>
struct TeamThreadMDRange<Rank<N, OuterDir, InnerDir>, TeamHandle> {
using NestLevelType = int;
using BoundaryType = int;
using TeamHandleType = TeamHandle;
using ExecutionSpace = typename TeamHandleType::execution_space;
using ArrayLayout = typename ExecutionSpace::array_layout;
static constexpr NestLevelType total_nest_level =
Rank<N, OuterDir, InnerDir>::rank;
static constexpr Iterate iter = OuterDir;
static constexpr auto par_thread = Impl::TeamMDRangeParThread::ParThread;
static constexpr auto par_vector = Impl::TeamMDRangeParVector::NotParVector;
static constexpr Iterate direction =
OuterDir == Iterate::Default
? layout_iterate_type_selector<ArrayLayout>::outer_iteration_pattern
: iter;
template <class... Args>
KOKKOS_FUNCTION TeamThreadMDRange(TeamHandleType const& team_, Args&&... args)
: team(team_), boundaries{static_cast<BoundaryType>(args)...} {
static_assert(sizeof...(Args) == total_nest_level);
}
TeamHandleType const& team;
BoundaryType boundaries[total_nest_level];
};
template <typename TeamHandle, typename... Args>
TeamThreadMDRange(TeamHandle const&, Args&&...)
->TeamThreadMDRange<Rank<sizeof...(Args), Iterate::Default>, TeamHandle>;
template <typename Rank, typename TeamHandle>
struct ThreadVectorMDRange;
template <unsigned N, Iterate OuterDir, Iterate InnerDir, typename TeamHandle>
struct ThreadVectorMDRange<Rank<N, OuterDir, InnerDir>, TeamHandle> {
using NestLevelType = int;
using BoundaryType = int;
using TeamHandleType = TeamHandle;
using ExecutionSpace = typename TeamHandleType::execution_space;
using ArrayLayout = typename ExecutionSpace::array_layout;
static constexpr NestLevelType total_nest_level =
Rank<N, OuterDir, InnerDir>::rank;
static constexpr Iterate iter = OuterDir;
static constexpr auto par_thread = Impl::TeamMDRangeParThread::NotParThread;
static constexpr auto par_vector = Impl::TeamMDRangeParVector::ParVector;
static constexpr Iterate direction =
OuterDir == Iterate::Default
? layout_iterate_type_selector<ArrayLayout>::outer_iteration_pattern
: iter;
template <class... Args>
KOKKOS_INLINE_FUNCTION ThreadVectorMDRange(TeamHandleType const& team_,
Args&&... args)
: team(team_), boundaries{static_cast<BoundaryType>(args)...} {
static_assert(sizeof...(Args) == total_nest_level);