forked from ECP-copa/Cabana
/
Cabana_Parallel.hpp
1628 lines (1377 loc) · 65 KB
/
Cabana_Parallel.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
/****************************************************************************
* Copyright (c) 2018-2023 by the Cabana authors *
* All rights reserved. *
* *
* This file is part of the Cabana library. Cabana is distributed under a *
* BSD 3-clause license. For the licensing terms see the LICENSE file in *
* the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
/*!
\file Cabana_Parallel.hpp
\brief SIMD and neighbor extension of Kokkos parallel iteration
*/
#ifndef CABANA_PARALLEL_HPP
#define CABANA_PARALLEL_HPP
#include <Cabana_ExecutionPolicy.hpp>
#include <Cabana_LinkedCellList.hpp>
#include <Cabana_NeighborList.hpp>
#include <Cabana_Types.hpp> // is_accessible_from
#include <impl/Cabana_CartesianGrid.hpp>
#include <Kokkos_Core.hpp>
#include <cstdlib>
#include <type_traits>
namespace Cabana
{
//---------------------------------------------------------------------------//
namespace Impl
{
//! \cond Impl
// No work tag was provided so call without a tag argument.
template <class WorkTag, class FunctorType, class... IndexTypes>
KOKKOS_FORCEINLINE_FUNCTION
typename std::enable_if<std::is_same<WorkTag, void>::value>::type
functorTagDispatch( const FunctorType& functor, IndexTypes&&... indices )
{
functor( std::forward<IndexTypes>( indices )... );
}
// The user gave us a tag so call the version using that.
template <class WorkTag, class FunctorType, class... IndexTypes>
KOKKOS_FORCEINLINE_FUNCTION
typename std::enable_if<!std::is_same<WorkTag, void>::value>::type
functorTagDispatch( const FunctorType& functor, IndexTypes&&... indices )
{
const WorkTag t{};
functor( t, std::forward<IndexTypes>( indices )... );
}
// No work tag was provided so call reduce without a tag argument.
template <class WorkTag, class FunctorType, class... IndexTypes,
class ReduceType>
KOKKOS_FORCEINLINE_FUNCTION
typename std::enable_if<std::is_same<WorkTag, void>::value>::type
functorTagDispatch( const FunctorType& functor, IndexTypes&&... indices,
ReduceType& reduce_val )
{
functor( std::forward<IndexTypes>( indices )..., reduce_val );
}
// The user gave us a tag so call the reduce version using that.
template <class WorkTag, class FunctorType, class... IndexTypes,
class ReduceType>
KOKKOS_FORCEINLINE_FUNCTION
typename std::enable_if<!std::is_same<WorkTag, void>::value>::type
functorTagDispatch( const FunctorType& functor, IndexTypes&&... indices,
ReduceType& reduce_val )
{
const WorkTag t{};
functor( t, std::forward<IndexTypes>( indices )..., reduce_val );
}
template <class ExecutionPolicy, class Functor>
struct ParallelFor;
template <class Functor, int VectorLength, class... Properties>
struct ParallelFor<SimdPolicy<VectorLength, Properties...>, Functor>
{
using simd_policy = SimdPolicy<VectorLength, Properties...>;
using team_policy = typename simd_policy::base_type;
using work_tag = typename team_policy::work_tag;
using index_type = typename team_policy::index_type;
using member_type = typename team_policy::member_type;
simd_policy exec_policy_;
Functor functor_;
ParallelFor( std::string label, simd_policy exec_policy, Functor functor )
: exec_policy_( std::move( exec_policy ) )
, functor_( std::move( functor ) )
{
if ( label.empty() )
Kokkos::parallel_for(
dynamic_cast<const team_policy&>( exec_policy_ ), *this );
else
Kokkos::parallel_for(
label, dynamic_cast<const team_policy&>( exec_policy_ ),
*this );
}
template <class WorkTag>
KOKKOS_FUNCTION std::enable_if_t<!std::is_void<WorkTag>::value &&
std::is_same<WorkTag, work_tag>::value>
operator()( WorkTag, member_type const& team ) const
{
this->operator()( team );
}
KOKKOS_FUNCTION void operator()( member_type const& team ) const
{
index_type s = team.league_rank() + exec_policy_.structBegin();
Kokkos::parallel_for(
Kokkos::ThreadVectorRange( team, exec_policy_.arrayBegin( s ),
exec_policy_.arrayEnd( s ) ),
[&]( index_type a )
{ Impl::functorTagDispatch<work_tag>( functor_, s, a ); } );
}
};
//! \endcond
} // end namespace Impl
//---------------------------------------------------------------------------//
// SIMD Parallel For
//---------------------------------------------------------------------------//
/*!
\brief Execute a vectorized functor in parallel with a 2d execution policy.
\tparam FunctorType The functor type to execute.
\tparam VectorLength The length of the vector over which to execute the
vectorized code.
\tparam ExecParameters Execution policy parameters.
\param exec_policy The 2D range policy over which to execute the functor.
\param functor The vectorized functor to execute in parallel. Must accept
both a struct and array index.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_for called by this code and can be used for
identification and profiling purposes.
A "functor" is a callable object containing the function to execute in
parallel, data needed for that execution, and an optional \c execution_space
typedef. Here is an example functor for parallel_for:
\code
class FunctorType {
public:
typedef ... execution_space ;
void operator() ( const int struct, const int array ) const ;
};
\endcode
In the above example, \c struct defines an index to a given AoSoA/Slice
struct and array defines and index to the given array element in that struct.
Its <tt>operator()</tt> method defines the operation to parallelize, over
the range of indices <tt>idx=[begin,end]</tt>. The kernel represented by the
functor is intended to vectorize of the array index.
\note The work tag gets applied at the user functor level, not at the level
of the functor in this implementation that wraps the user functor.
*/
template <class FunctorType, int VectorLength, class... ExecParameters>
inline void simd_parallel_for(
const SimdPolicy<VectorLength, ExecParameters...>& exec_policy,
const FunctorType& functor, const std::string& str = "" )
{
Kokkos::Profiling::pushRegion( "Cabana::simd_parallel_for" );
Impl::ParallelFor<SimdPolicy<VectorLength, ExecParameters...>, FunctorType>(
str, exec_policy, functor );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
// Neighbor Parallel For
//---------------------------------------------------------------------------//
// Algorithm tags.
//! Loop over particle neighbors.
class FirstNeighborsTag
{
};
//! Loop over particle neighbors (first) and neighbor's neighbors (second)
class SecondNeighborsTag
{
};
//! Neighbor operations are executed in serial on each particle thread.
class SerialOpTag
{
};
//! Neighbor operations are executed with team parallelism.
class TeamOpTag
{
};
//! Neighbor operations are executed with team vector parallelism.
class TeamVectorOpTag
{
};
//---------------------------------------------------------------------------//
/*!
\brief Execute functor in parallel according to the execution policy over
particles with a thread-local serial loop over particle first neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param FirstNeighborsTag Tag indicating operations over particle first
neighbors.
\param SerialOpTag Tag indicating a serial loop strategy over neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_for called by this code and can be used for
identification and profiling purposes.
A "functor" is a class containing the function to execute in parallel, data
needed for that execution, and an optional \c execution_space typedef. Here
is an example functor for neighbor parallel_for:
\code
class FunctorType {
public:
typedef ... execution_space ;
void operator() ( const int particle_index, const int neighbor_index ) const ;
};
\endcode
In the above example, \c Index is a Cabana index to a given AoSoA element
for a particle and its neighbor. Its <tt>operator()</tt> method defines the
operation to parallelize, over the range of indices
<tt>idx=[begin,end]</tt>. This compares to a single iteration \c idx of a
\c for loop.
*/
template <class FunctorType, class NeighborListType, class... ExecParameters>
inline void neighbor_parallel_for(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const FirstNeighborsTag, const SerialOpTag, const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_for" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using index_type =
typename Kokkos::RangePolicy<ExecParameters...>::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
auto begin = exec_policy.begin();
auto end = exec_policy.end();
using linear_policy_type = Kokkos::RangePolicy<execution_space, void, void>;
linear_policy_type linear_exec_policy( begin, end );
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
auto neigh_func = KOKKOS_LAMBDA( const index_type i )
{
for ( index_type n = 0;
n < neighbor_list_traits::numNeighbor( list, i ); ++n )
Impl::functorTagDispatch<work_tag>(
functor, i,
static_cast<index_type>(
neighbor_list_traits::getNeighbor( list, i, n ) ) );
};
if ( str.empty() )
Kokkos::parallel_for( linear_exec_policy, neigh_func );
else
Kokkos::parallel_for( str, linear_exec_policy, neigh_func );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor in parallel according to the execution policy over
particles with thread-local serial loops over particle first and second
neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param SecondNeighborsTag Tag indicating operations over particle first and
second neighbors.
\param SerialOpTag Tag indicating a serial loop strategy over neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_for called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class... ExecParameters>
inline void neighbor_parallel_for(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const SecondNeighborsTag, const SerialOpTag, const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_for" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using index_type =
typename Kokkos::RangePolicy<ExecParameters...>::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
auto begin = exec_policy.begin();
auto end = exec_policy.end();
using linear_policy_type = Kokkos::RangePolicy<execution_space, void, void>;
linear_policy_type linear_exec_policy( begin, end );
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
auto neigh_func = KOKKOS_LAMBDA( const index_type i )
{
const index_type nn = neighbor_list_traits::numNeighbor( list, i );
for ( index_type n = 0; n < nn; ++n )
{
const index_type j =
neighbor_list_traits::getNeighbor( list, i, n );
for ( index_type a = n + 1; a < nn; ++a )
{
const index_type k =
neighbor_list_traits::getNeighbor( list, i, a );
Impl::functorTagDispatch<work_tag>( functor, i, j, k );
}
}
};
if ( str.empty() )
Kokkos::parallel_for( linear_exec_policy, neigh_func );
else
Kokkos::parallel_for( str, linear_exec_policy, neigh_func );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor in parallel according to the execution policy over
particles with team parallelism over particle first neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param FirstNeighborsTag Tag indicating operations over particle first
neighbors.
\param TeamOpTag Tag indicating a team parallel strategy over neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_for called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class... ExecParameters>
inline void neighbor_parallel_for(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const FirstNeighborsTag, const TeamOpTag, const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_for" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using kokkos_policy =
Kokkos::TeamPolicy<execution_space, Kokkos::Schedule<Kokkos::Dynamic>>;
kokkos_policy team_policy( exec_policy.end() - exec_policy.begin(),
Kokkos::AUTO );
using index_type = typename kokkos_policy::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
const auto range_begin = exec_policy.begin();
auto neigh_func =
KOKKOS_LAMBDA( const typename kokkos_policy::member_type& team )
{
index_type i = team.league_rank() + range_begin;
Kokkos::parallel_for(
Kokkos::TeamThreadRange(
team, neighbor_list_traits::numNeighbor( list, i ) ),
[&]( const index_type n )
{
Impl::functorTagDispatch<work_tag>(
functor, i,
static_cast<index_type>(
neighbor_list_traits::getNeighbor( list, i, n ) ) );
} );
};
if ( str.empty() )
Kokkos::parallel_for( team_policy, neigh_func );
else
Kokkos::parallel_for( str, team_policy, neigh_func );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor in parallel according to the execution policy over
particles with team parallelism over particle first neighbors and serial loop
over second neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param SecondNeighborsTag Tag indicating operations over particle first and
second neighbors.
\param TeamOpTag Tag indicating a team parallel strategy over particle first
neighbors and serial execution over second neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_for called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class... ExecParameters>
inline void neighbor_parallel_for(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const SecondNeighborsTag, const TeamOpTag, const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_for" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using kokkos_policy =
Kokkos::TeamPolicy<execution_space, Kokkos::Schedule<Kokkos::Dynamic>>;
kokkos_policy team_policy( exec_policy.end() - exec_policy.begin(),
Kokkos::AUTO );
using index_type = typename kokkos_policy::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
const auto range_begin = exec_policy.begin();
auto neigh_func =
KOKKOS_LAMBDA( const typename kokkos_policy::member_type& team )
{
index_type i = team.league_rank() + range_begin;
const index_type nn = neighbor_list_traits::numNeighbor( list, i );
Kokkos::parallel_for(
Kokkos::TeamThreadRange( team, nn ),
[&]( const index_type n )
{
const index_type j =
neighbor_list_traits::getNeighbor( list, i, n );
for ( index_type a = n + 1; a < nn; ++a )
{
const index_type k =
neighbor_list_traits::getNeighbor( list, i, a );
Impl::functorTagDispatch<work_tag>( functor, i, j, k );
}
} );
};
if ( str.empty() )
Kokkos::parallel_for( team_policy, neigh_func );
else
Kokkos::parallel_for( str, team_policy, neigh_func );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor in parallel according to the execution policy over
particles with team parallelism over particle first neighbors and vector loop
parallelism over second neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param SecondNeighborsTag Tag indicating operations over particle first and
second neighbors.
\param TeamVectorOpTag Tag indicating a team parallel strategy over particle
first neighbors and vector parallel loop strategy over second neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_for called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class... ExecParameters>
inline void neighbor_parallel_for(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const SecondNeighborsTag, const TeamVectorOpTag,
const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_for" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using kokkos_policy =
Kokkos::TeamPolicy<execution_space, Kokkos::Schedule<Kokkos::Dynamic>>;
kokkos_policy team_policy( exec_policy.end() - exec_policy.begin(),
Kokkos::AUTO );
using index_type = typename kokkos_policy::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
const auto range_begin = exec_policy.begin();
auto neigh_func =
KOKKOS_LAMBDA( const typename kokkos_policy::member_type& team )
{
index_type i = team.league_rank() + range_begin;
const index_type nn = neighbor_list_traits::numNeighbor( list, i );
Kokkos::parallel_for(
Kokkos::TeamThreadRange( team, nn ),
[&]( const index_type n )
{
const index_type j =
neighbor_list_traits::getNeighbor( list, i, n );
Kokkos::parallel_for(
Kokkos::ThreadVectorRange( team, n + 1, nn ),
[&]( const index_type a )
{
const index_type k =
neighbor_list_traits::getNeighbor( list, i, a );
Impl::functorTagDispatch<work_tag>( functor, i, j, k );
} );
} );
};
if ( str.empty() )
Kokkos::parallel_for( team_policy, neigh_func );
else
Kokkos::parallel_for( str, team_policy, neigh_func );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
// Neighbor Parallel Reduce
//---------------------------------------------------------------------------//
/*!
\brief Execute functor reduction in parallel according to the execution policy
over particles with a thread-local serial loop over particle first neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\tparam ReduceType The reduction type.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param FirstNeighborsTag Tag indicating operations over particle first
neighbors.
\param SerialOpTag Tag indicating a serial loop strategy over
neighbors.
\param reduce_val Scalar to be reduced across particles and neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_reduce called by this code and can be used for
identification and profiling purposes.
A "functor" is a class containing the function to execute in parallel, data
needed for that execution, and an optional \c execution_space typedef. Here
is an example functor for neighbor parallel_for:
\code
class FunctorType {
public:
typedef ... execution_space ;
void operator() ( const int particle_index, const int neighbor_index ) const ;
};
\endcode
In the above example, \c Index is a Cabana index to a given AoSoA element
for a particle and its neighbor. Its <tt>operator()</tt> method defines the
operation to parallelize, over the range of indices
<tt>idx=[begin,end]</tt>. This compares to a single iteration \c idx of a
\c for loop.
*/
template <class FunctorType, class NeighborListType, class ReduceType,
class... ExecParameters>
inline void neighbor_parallel_reduce(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const FirstNeighborsTag, const SerialOpTag, ReduceType& reduce_val,
const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_reduce" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using index_type =
typename Kokkos::RangePolicy<ExecParameters...>::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
auto begin = exec_policy.begin();
auto end = exec_policy.end();
using linear_policy_type = Kokkos::RangePolicy<execution_space, void, void>;
linear_policy_type linear_exec_policy( begin, end );
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
auto neigh_reduce = KOKKOS_LAMBDA( const index_type i, ReduceType& ival )
{
for ( index_type n = 0;
n < neighbor_list_traits::numNeighbor( list, i ); ++n )
Impl::functorTagDispatch<work_tag>(
functor, i,
static_cast<index_type>(
neighbor_list_traits::getNeighbor( list, i, n ) ),
ival );
};
if ( str.empty() )
Kokkos::parallel_reduce( linear_exec_policy, neigh_reduce, reduce_val );
else
Kokkos::parallel_reduce( str, linear_exec_policy, neigh_reduce,
reduce_val );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor reduction in parallel according to the execution policy
over particles with thread-local serial loops over particle first and second
neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\tparam ReduceType The reduction type.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param SecondNeighborsTag Tag indicating operations over particle first and
second neighbors.
\param SerialOpTag Tag indicating a serial loop strategy over neighbors.
\param reduce_val Scalar to be reduced across particles and neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_reduce called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class ReduceType,
class... ExecParameters>
inline void neighbor_parallel_reduce(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const SecondNeighborsTag, const SerialOpTag, ReduceType& reduce_val,
const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_reduce" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using index_type =
typename Kokkos::RangePolicy<ExecParameters...>::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
auto begin = exec_policy.begin();
auto end = exec_policy.end();
using linear_policy_type = Kokkos::RangePolicy<execution_space, void, void>;
linear_policy_type linear_exec_policy( begin, end );
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
auto neigh_reduce = KOKKOS_LAMBDA( const index_type i, ReduceType& ival )
{
const index_type nn = neighbor_list_traits::numNeighbor( list, i );
for ( index_type n = 0; n < nn; ++n )
{
const index_type j =
neighbor_list_traits::getNeighbor( list, i, n );
for ( index_type a = n + 1; a < nn; ++a )
{
const index_type k =
neighbor_list_traits::getNeighbor( list, i, a );
Impl::functorTagDispatch<work_tag>( functor, i, j, k, ival );
}
}
};
if ( str.empty() )
Kokkos::parallel_reduce( linear_exec_policy, neigh_reduce, reduce_val );
else
Kokkos::parallel_reduce( str, linear_exec_policy, neigh_reduce,
reduce_val );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor reduction in parallel according to the execution policy
over particles with team parallelism over particle first neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\tparam ReduceType The reduction type.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param FirstNeighborsTag Tag indicating operations over particle first
neighbors.
\param TeamOpTag Tag indicating a team parallel strategy over particle
neighbors.
\param reduce_val Scalar to be reduced across particles and neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_reduce called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class ReduceType,
class... ExecParameters>
inline void neighbor_parallel_reduce(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const FirstNeighborsTag, const TeamOpTag, ReduceType& reduce_val,
const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_reduce" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using kokkos_policy =
Kokkos::TeamPolicy<execution_space, Kokkos::Schedule<Kokkos::Dynamic>>;
kokkos_policy team_policy( exec_policy.end() - exec_policy.begin(),
Kokkos::AUTO );
using index_type = typename kokkos_policy::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
const auto range_begin = exec_policy.begin();
auto neigh_reduce = KOKKOS_LAMBDA(
const typename kokkos_policy::member_type& team, ReduceType& ival )
{
index_type i = team.league_rank() + range_begin;
ReduceType reduce_n = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(
team, neighbor_list_traits::numNeighbor( list, i ) ),
[&]( const index_type n, ReduceType& nval )
{
Impl::functorTagDispatch<work_tag>(
functor, i,
static_cast<index_type>(
neighbor_list_traits::getNeighbor( list, i, n ) ),
nval );
},
reduce_n );
Kokkos::single( Kokkos::PerTeam( team ), [&]() { ival += reduce_n; } );
};
if ( str.empty() )
Kokkos::parallel_reduce( team_policy, neigh_reduce, reduce_val );
else
Kokkos::parallel_reduce( str, team_policy, neigh_reduce, reduce_val );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor reduction in parallel according to the execution policy
over particles with team parallelism over particle first neighbors and serial
loop over second neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\tparam ReduceType The reduction type.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param SecondNeighborsTag Tag indicating operations over particle first and
second neighbors.
\param TeamOpTag Tag indicating a team parallel strategy over particle first
neighbors and serial loops over second neighbors.
\param reduce_val Scalar to be reduced across particles and neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_reduce called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class ReduceType,
class... ExecParameters>
inline void neighbor_parallel_reduce(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const SecondNeighborsTag, const TeamOpTag, ReduceType& reduce_val,
const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_reduce" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using kokkos_policy =
Kokkos::TeamPolicy<execution_space, Kokkos::Schedule<Kokkos::Dynamic>>;
kokkos_policy team_policy( exec_policy.end() - exec_policy.begin(),
Kokkos::AUTO );
using index_type = typename kokkos_policy::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );
const auto range_begin = exec_policy.begin();
auto neigh_reduce = KOKKOS_LAMBDA(
const typename kokkos_policy::member_type& team, ReduceType& ival )
{
index_type i = team.league_rank() + range_begin;
ReduceType reduce_n = 0;
const index_type nn = neighbor_list_traits::numNeighbor( list, i );
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange( team, nn ),
[&]( const index_type n, ReduceType& nval )
{
const index_type j =
neighbor_list_traits::getNeighbor( list, i, n );
for ( index_type a = n + 1; a < nn; ++a )
{
const index_type k =
neighbor_list_traits::getNeighbor( list, i, a );
Impl::functorTagDispatch<work_tag>( functor, i, j, k,
nval );
}
},
reduce_n );
Kokkos::single( Kokkos::PerTeam( team ), [&]() { ival += reduce_n; } );
};
if ( str.empty() )
Kokkos::parallel_reduce( team_policy, neigh_reduce, reduce_val );
else
Kokkos::parallel_reduce( str, team_policy, neigh_reduce, reduce_val );
Kokkos::Profiling::popRegion();
}
//---------------------------------------------------------------------------//
/*!
\brief Execute functor reduction in parallel according to the execution policy
over particles with team parallelism over particle first neighbors and vector
loop parallelism over second neighbors.
\tparam FunctorType The functor type to execute.
\tparam NeighborListType The neighbor list type.
\tparam ExecParams The Kokkos range policy parameters.
\tparam ReduceType The reduction type.
\param exec_policy The policy over which to execute the functor.
\param functor The functor to execute in parallel
\param list The neighbor list over which to execute the neighbor operations.
\param SecondNeighborsTag Tag indicating operations over particle first and
second neighbors.
\param TeamVectorOpTag Tag indicating a team parallel strategy over particle
first neighbors and vector loops over second neighbors.
\param reduce_val Scalar to be reduced across particles and neighbors.
\param str Optional name for the functor. Will be forwarded if non-empty to
the Kokkos::parallel_reduce called by this code and can be used for
identification and profiling purposes.
*/
template <class FunctorType, class NeighborListType, class ReduceType,
class... ExecParameters>
inline void neighbor_parallel_reduce(
const Kokkos::RangePolicy<ExecParameters...>& exec_policy,
const FunctorType& functor, const NeighborListType& list,
const SecondNeighborsTag, const TeamVectorOpTag, ReduceType& reduce_val,
const std::string& str = "",
typename std::enable_if<( !is_linked_cell_list<NeighborListType>::value ),
int>::type* = 0 )
{
Kokkos::Profiling::pushRegion( "Cabana::neighbor_parallel_reduce" );
using work_tag = typename Kokkos::RangePolicy<ExecParameters...>::work_tag;
using execution_space =
typename Kokkos::RangePolicy<ExecParameters...>::execution_space;
using kokkos_policy =
Kokkos::TeamPolicy<execution_space, Kokkos::Schedule<Kokkos::Dynamic>>;
kokkos_policy team_policy( exec_policy.end() - exec_policy.begin(),
Kokkos::AUTO );
using index_type = typename kokkos_policy::index_type;
using neighbor_list_traits = NeighborList<NeighborListType>;
using memory_space = typename neighbor_list_traits::memory_space;
static_assert( is_accessible_from<memory_space, execution_space>{}, "" );