Skip to content

Commit

Permalink
a simpler fix for constexpr_for in amrex (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
zingale authored Dec 19, 2023
1 parent 4d7d029 commit 8a61b96
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
26 changes: 1 addition & 25 deletions interfaces/rhs_type.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,13 @@
#define rhs_type_H

#include <AMReX_REAL.H>
#include <AMReX_Loop.H>
#include <network_properties.H>
#ifdef SCREENING
#include <screen.H>
#endif
#include <tfactors.H>

// Implementation of "constexpr for" based on
// https://artificial-mind.net/blog/2020/10/31/constexpr-for
//
// Approximates what one would get from a compile-time
// unrolling of the loop
// for (int i = 0; i < N; ++i) {
// f(i);
// }
//
// The mechanism is recursive, we evaluate f(i) at the current
// i and then call the for loop at i+1. f() is a lambda function
// that provides the body of the loop and takes only an integer
// i as its argument. It is assumed that at the loop call site
// the lambda is declared [&] so that it can operate on whatever
// data it needs.

template<auto I, auto N, class F>
AMREX_GPU_HOST_DEVICE AMREX_INLINE
constexpr void constexpr_for (F&& f)
{
if constexpr (I < N) {
f(std::integral_constant<decltype(I), I>());
constexpr_for<I+1, N>(f);
}
}

namespace RHS
{
Expand Down
1 change: 1 addition & 0 deletions networks/rhs.H
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <AMReX.H>
#include <AMReX_REAL.H>
#include <AMReX_Print.H>
#include <AMReX_Loop.H>

#include <ArrayUtilities.H>
#include <rhs_type.H>
Expand Down

0 comments on commit 8a61b96

Please sign in to comment.