| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #ifndef _CG_PARTITIONING_H
|
| #define _CG_PARTITIONING_H
|
|
|
| #include "info.h"
|
| #include "helpers.h"
|
|
|
| _CG_BEGIN_NAMESPACE
|
|
|
| namespace details {
|
|
|
| template <typename TyGroup>
|
| _CG_STATIC_QUALIFIER coalesced_group _binary_partition(const TyGroup &tile, bool pred) {
|
| const unsigned int fullMask = ~0u;
|
|
|
| unsigned int thisMask = _coalesced_group_data_access::get_mask(tile);
|
| unsigned int predMask = pred ? 0 : fullMask;
|
| unsigned int setMask = __ballot_sync(thisMask, pred);
|
|
|
| if (setMask == thisMask || setMask == 0) {
|
| coalesced_group subTile = _coalesced_group_data_access::construct_from_mask<coalesced_group>(thisMask);
|
| _coalesced_group_data_access::modify_meta_group(subTile, 0, 1);
|
| return subTile;
|
| }
|
| else {
|
| unsigned int subMask = thisMask & (setMask ^ predMask);
|
| coalesced_group subTile = _coalesced_group_data_access::construct_from_mask<coalesced_group>(subMask);
|
| _coalesced_group_data_access::modify_meta_group(subTile, pred, 2);
|
| return subTile;
|
| }
|
| }
|
|
|
| #if defined(_CG_HAS_MATCH_COLLECTIVE) && defined(_CG_CPP11_FEATURES)
|
| template <typename TyPredicate>
|
| struct _labeled_partition_dispatch {
|
| template <typename TyGroup>
|
| _CG_QUALIFIER coalesced_group operator()(const TyGroup &tile, TyPredicate pred) {
|
| unsigned int thisMask = _coalesced_group_data_access::get_mask(tile);
|
| unsigned int thisBias = __ffs(thisMask) - 1;
|
| unsigned int subMask = __match_any_sync(thisMask, pred);
|
|
|
| coalesced_group subTile = _coalesced_group_data_access::construct_from_mask<coalesced_group>(subMask);
|
|
|
| int leaderLaneId = subTile.shfl(details::laneid(), 0);
|
|
|
| bool isLeader = !subTile.thread_rank();
|
| unsigned int leaderMask = __ballot_sync(thisMask, isLeader);
|
| unsigned int tileRank = __fns(leaderMask, leaderLaneId, 0) - thisBias;
|
|
|
| _coalesced_group_data_access::modify_meta_group(subTile, tileRank, __popc(leaderMask));
|
|
|
| return subTile;
|
| }
|
| };
|
|
|
| template <>
|
| struct _labeled_partition_dispatch<bool> {
|
| template <typename TyGroup>
|
| _CG_QUALIFIER coalesced_group operator()(const TyGroup &tile, bool pred) {
|
| return _binary_partition(tile, pred);
|
| }
|
| };
|
|
|
| template <typename TyPredicate>
|
| struct _labeled_partition_dispatch<TyPredicate*> {
|
| template <typename TyGroup>
|
| _CG_QUALIFIER coalesced_group operator()(const TyGroup &tile, TyPredicate* pred) {
|
| auto impl = _labeled_partition_dispatch<unsigned long long>();
|
| return impl(tile, reinterpret_cast<unsigned long long>(pred));
|
| }
|
| };
|
| #endif
|
| };
|
|
|
| _CG_STATIC_QUALIFIER coalesced_group binary_partition(const coalesced_group &tile, bool pred) {
|
| return details::_binary_partition(tile, pred);
|
| }
|
|
|
| template <unsigned int Size, typename ParentT>
|
| _CG_STATIC_QUALIFIER coalesced_group binary_partition(const thread_block_tile<Size, ParentT> &tile, bool pred) {
|
| #ifdef _CG_CPP11_FEATURES
|
| static_assert(Size <= 32, "Binary partition is available only for tiles of size smaller or equal to 32");
|
| #endif
|
| return details::_binary_partition(tile, pred);
|
| }
|
|
|
|
|
| #if defined(_CG_HAS_MATCH_COLLECTIVE) && defined(_CG_CPP11_FEATURES)
|
| template <typename TyPredicate>
|
| _CG_STATIC_QUALIFIER coalesced_group labeled_partition(const coalesced_group &tile, TyPredicate pred) {
|
| static_assert(_CG_STL_NAMESPACE::is_integral<TyPredicate>::value ||
|
| _CG_STL_NAMESPACE::is_pointer<TyPredicate>::value,
|
| "labeled_partition predicate must be an integral or pointer type");
|
| auto dispatch = details::_labeled_partition_dispatch<details::remove_qual<TyPredicate>>();
|
| return dispatch(tile, pred);
|
| }
|
|
|
| template <typename TyPredicate, unsigned int Size, typename ParentT>
|
| _CG_STATIC_QUALIFIER coalesced_group labeled_partition(const thread_block_tile<Size, ParentT> &tile, TyPredicate pred) {
|
| static_assert(_CG_STL_NAMESPACE::is_integral<TyPredicate>::value ||
|
| _CG_STL_NAMESPACE::is_pointer<TyPredicate>::value,
|
| "labeled_partition predicate must be an integral or pointer type");
|
| static_assert(Size <= 32, "Labeled partition is available only for tiles of size smaller or equal to 32");
|
| auto dispatch = details::_labeled_partition_dispatch<details::remove_qual<TyPredicate>>();
|
| return dispatch(tile, pred);
|
| }
|
| #endif
|
|
|
| _CG_END_NAMESPACE
|
|
|
| #endif
|
|
|