Skip to content

Add a helper function for "for each element loop" #170

Description

@makortel

From https://github.com/cms-patatrack/pixeltrack-standalone/pull/165/files#r565486352

A pattern

const auto gridDim = ...;
const auto [firstNoStride, endNoStride] = ...;
for (auto threadIdx = firstNoStride, endElementIdx = endNoStride; threadIdx < MAX; threadIdx += gridDimension, endElementIdx += gridDimension) {
  for (auto i = threadIdx; i < std::min(endElementIdx, MAX); ++i ) {
    <body>
  }
}

seems to repeat often. The pattern could be abstracted along

namespace cms::alpakatools {
  // the name should be more descriptive...
  template <typename T_Acc, typename N, typename Func>
  inline void for_each_element(const T_Acc& acc, const N nitems, Func func) {
    const auto gridDim = ...;
    const auto [firstNoStride, endNoStride] = ...;
    for (auto threadIdx = firstNoStride, endElementIdx = endNoStride; threadIdx < nitems; threadIdx += gridDimension, endElementIdx += gridDimension) {
      for (auto i = threadIdx; i < std::min(endElementIdx, nitems); ++i ) {
        func(i);
      }
    }
  }
}

that could be called along

const uint32_t nt = offsets[nh];
cms::alpakatools::for_each_element(acc, nt, [&](uint32_t i) {
  auto off = alpaka_std::upper_bound(offsets, offsets + nh + 1, i);
  ...
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions