Block-Structured AMR Software Framework
AMReX_ConstexprFor.H
Go to the documentation of this file.
1 #ifndef AMREX_CONSTEXPR_FOR_H_
2 #define AMREX_CONSTEXPR_FOR_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_GpuQualifiers.H>
6 #include <AMReX_Extension.H>
7 
8 #include <type_traits>
9 
10 namespace amrex {
11 
12 // Implementation of "constexpr for" based on
13 // https://artificial-mind.net/blog/2020/10/31/constexpr-for
14 //
15 // Approximates what one would get from a compile-time
16 // unrolling of the loop
17 // for (int i = 0; i < N; ++i) {
18 // f(i);
19 // }
20 //
21 // The mechanism is recursive: we evaluate f(i) at the current
22 // i and then call the for loop at i+1. f() is a lambda function
23 // that provides the body of the loop and takes only an integer
24 // i as its argument.
25 
26 template<auto I, auto N, class F>
28 constexpr void constexpr_for (F const& f)
29 {
30  if constexpr (I < N) {
31  f(std::integral_constant<decltype(I), I>());
32  constexpr_for<I+1, N>(f);
33  }
34 }
35 
36 }
37 
38 #endif
#define AMREX_INLINE
Definition: AMReX_Extension.H:127
#define AMREX_GPU_HOST_DEVICE
Definition: AMReX_GpuQualifiers.H:20
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:44
Definition: AMReX_Amr.cpp:49
AMREX_GPU_HOST_DEVICE constexpr AMREX_INLINE void constexpr_for(F const &f)
Definition: AMReX_ConstexprFor.H:28