Kokkos and C++: some examples use KOKKOSLAMBDA(int i), but i have also seen…
- User
Kokkos and C++: some examples use KOKKOS_LAMBDA(int i), but i have also seen KOKKOS_LAMBDA(const int i). what should be used when variable i is the iteration parameter?
- Assistant
Short version
Both spellings compile, and both are “correct”.
The only difference is exactly the one you would expect in normal C++: the parameter isconstor it isn’t.
Because the index is passed by value, making itconstjust stops you from re-assigning it accidentally inside the kernel; it has no impact on Kokkos’ execution or on performance.Why you sometimes see the
const• In Kokkos tutorials the authors often write
parallel_for(N, KOKKOS_LAMBDA(const int i) { … });as a reminder that you should treat the loop index as read-only.
• The same style shows up in functors:KOKKOS_INLINE_FUNCTION void operator()(const int i) const { … }Here the outer
constafter the parameter list belongs to the functor itself (operator() const) and the inner decorates the parameter. Putting both in the lambda keeps the visual symmetry.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.