Skip to content

Commit 6462fcf

Browse files
committed
Add once_per_grid and once_per_block helper functions
`once_per_grid(acc)` returns true for a single thread within the kernel execution grid. Usually the condition is true for block 0 and thread 0, but these indices should not be relied upon. `once_per_block(acc)` returns true for a single thread within the block. Usually the condition is true for thread 0, but this index should not be relied upon.
1 parent 42360ab commit 6462fcf

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

HeterogeneousCore/AlpakaInterface/interface/workdivision.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,30 @@ namespace cms::alpakatools {
496496
const Idx range_;
497497
};
498498

499+
/* once_per_grid
500+
*
501+
* `once_per_grid(acc)` returns true for a single thread within the kernel execution grid.
502+
*
503+
* Usually the condition is true for block 0 and thread 0, but these indices should not be relied upon.
504+
*/
505+
506+
template <typename TAcc, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
507+
ALPAKA_FN_ACC inline constexpr bool once_per_grid(TAcc const& acc) {
508+
return alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc) == Vec<alpaka::Dim<TAcc>>::zeros();
509+
}
510+
511+
/* once_per_block
512+
*
513+
* `once_per_block(acc)` returns true for a single thread within the block.
514+
*
515+
* Usually the condition is true for thread 0, but this index should not be relied upon.
516+
*/
517+
518+
template <typename TAcc, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
519+
ALPAKA_FN_ACC inline constexpr bool once_per_block(TAcc const& acc) {
520+
return alpaka::getIdx<alpaka::Block, alpaka::Threads>(acc) == Vec<alpaka::Dim<TAcc>>::zeros();
521+
}
522+
499523
} // namespace cms::alpakatools
500524

501525
#endif // HeterogeneousCore_AlpakaInterface_interface_workdivision_h

0 commit comments

Comments
 (0)