|
| 1 | +#ifndef AMREX_CTO_PARALLEL_FOR_H_ |
| 2 | +#define AMREX_CTO_PARALLEL_FOR_H_ |
| 3 | + |
| 4 | +#include <AMReX_BLassert.H> |
| 5 | +#include <AMReX_Box.H> |
| 6 | +#include <AMReX_Tuple.H> |
| 7 | + |
| 8 | +#include <array> |
| 9 | +#include <type_traits> |
| 10 | + |
| 11 | +/* This header is not for the users to include directly. It's meant to be |
| 12 | + * included in AMReX_GpuLaunch.H, which has included the headers needed |
| 13 | + * here. */ |
| 14 | + |
| 15 | +/* Thank Maikel Nadolski and Alex Sinn for the techniques used here! */ |
| 16 | + |
| 17 | +namespace amrex { |
| 18 | + |
| 19 | +template <int... ctr> |
| 20 | +struct CompileTimeOptions { |
| 21 | + // TypeList is defined in AMReX_Tuple.H |
| 22 | + using list_type = TypeList<std::integral_constant<int, ctr>...>; |
| 23 | +}; |
| 24 | + |
| 25 | +#if (__cplusplus >= 201703L) |
| 26 | + |
| 27 | +namespace meta |
| 28 | +{ |
| 29 | + template <typename... As, typename... Bs> |
| 30 | + constexpr auto operator+ (TypeList<As...>, TypeList<Bs...>) { |
| 31 | + return TypeList<As..., Bs...>{}; |
| 32 | + } |
| 33 | + |
| 34 | + template <typename... Ls, typename A> |
| 35 | + constexpr auto single_product (TypeList<Ls...>, A) { |
| 36 | + return TypeList<decltype(Ls{} + TypeList<A>{})...>{}; |
| 37 | + } |
| 38 | + |
| 39 | + template <typename LLs, typename... As> |
| 40 | + constexpr auto operator* (LLs, TypeList<As...>) { |
| 41 | + return (TypeList<>{} + ... + single_product(LLs{}, As{})); |
| 42 | + } |
| 43 | + |
| 44 | + template <typename... Ls> |
| 45 | + constexpr auto cartesian_product_n (TypeList<Ls...>) { |
| 46 | + return (TypeList<TypeList<>>{} * ... * Ls{}); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +namespace detail |
| 51 | +{ |
| 52 | + template <int MT, typename T, class F, typename... As> |
| 53 | + std::enable_if_t<std::is_integral<T>::value || std::is_same<T,Box>::value, bool> |
| 54 | + ParallelFor_helper2 (T const& N, F&& f, TypeList<As...>, |
| 55 | + std::array<int,sizeof...(As)> const& runtime_options) |
| 56 | + { |
| 57 | + if (runtime_options == std::array<int,sizeof...(As)>{As::value...}) { |
| 58 | + if constexpr (std::is_integral<T>::value) { |
| 59 | + ParallelFor<MT>(N, [f] AMREX_GPU_DEVICE (T i) noexcept |
| 60 | + { |
| 61 | + f(i, As{}...); |
| 62 | + }); |
| 63 | + } else { |
| 64 | + ParallelFor<MT>(N, [f] AMREX_GPU_DEVICE (int i, int j, int k) noexcept |
| 65 | + { |
| 66 | + f(i, j, k, As{}...); |
| 67 | + }); |
| 68 | + } |
| 69 | + return true; |
| 70 | + } else { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + template <int MT, typename T, class F, typename... As> |
| 76 | + std::enable_if_t<std::is_integral<T>::value, bool> |
| 77 | + ParallelFor_helper2 (Box const& box, T ncomp, F&& f, TypeList<As...>, |
| 78 | + std::array<int,sizeof...(As)> const& runtime_options) |
| 79 | + { |
| 80 | + if (runtime_options == std::array<int,sizeof...(As)>{As::value...}) { |
| 81 | + ParallelFor<MT>(box, ncomp, [f] AMREX_GPU_DEVICE (int i, int j, int k, T n) noexcept |
| 82 | + { |
| 83 | + f(i, j, k, n, As{}...); |
| 84 | + }); |
| 85 | + return true; |
| 86 | + } else { |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + template <int MT, typename T, class F, typename... PPs, typename RO> |
| 92 | + std::enable_if_t<std::is_integral<T>::value || std::is_same<T,Box>::value> |
| 93 | + ParallelFor_helper1 (T const& N, F&& f, TypeList<PPs...>, |
| 94 | + RO const& runtime_options) |
| 95 | + { |
| 96 | + bool found_option = (false || ... || |
| 97 | + ParallelFor_helper2<MT>(N, std::forward<F>(f), |
| 98 | + PPs{}, runtime_options)); |
| 99 | + amrex::ignore_unused(found_option); |
| 100 | + AMREX_ASSERT(found_option); |
| 101 | + } |
| 102 | + |
| 103 | + template <int MT, typename T, class F, typename... PPs, typename RO> |
| 104 | + std::enable_if_t<std::is_integral<T>::value> |
| 105 | + ParallelFor_helper1 (Box const& box, T ncomp, F&& f, TypeList<PPs...>, |
| 106 | + RO const& runtime_options) |
| 107 | + { |
| 108 | + bool found_option = (false || ... || |
| 109 | + ParallelFor_helper2<MT>(box, ncomp, std::forward<F>(f), |
| 110 | + PPs{}, runtime_options)); |
| 111 | + amrex::ignore_unused(found_option); |
| 112 | + AMREX_ASSERT(found_option); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#endif |
| 117 | + |
| 118 | +template <int MT, typename T, class F, typename... CTOs> |
| 119 | +std::enable_if_t<std::is_integral<T>::value> |
| 120 | +ParallelFor (TypeList<CTOs...> /*list_of_compile_time_options*/, |
| 121 | + std::array<int,sizeof...(CTOs)> const& runtime_options, |
| 122 | + T N, F&& f) |
| 123 | +{ |
| 124 | +#if (__cplusplus >= 201703L) |
| 125 | + using OptionsListList = TypeList<typename CTOs::list_type...>; |
| 126 | + detail::ParallelFor_helper1<MT>(N, std::forward<F>(f), |
| 127 | + meta::cartesian_product_n(OptionsListList{}), |
| 128 | + runtime_options); |
| 129 | +#else |
| 130 | + amrex::ignore_unused(N, f, runtime_options); |
| 131 | + static_assert(std::is_integral<F>::value, "This requires C++17"); |
| 132 | +#endif |
| 133 | +} |
| 134 | + |
| 135 | +template <int MT, class F, typename... CTOs> |
| 136 | +void ParallelFor (TypeList<CTOs...> /*list_of_compile_time_options*/, |
| 137 | + std::array<int,sizeof...(CTOs)> const& runtime_options, |
| 138 | + Box const& box, F&& f) |
| 139 | +{ |
| 140 | +#if (__cplusplus >= 201703L) |
| 141 | + using OptionsListList = TypeList<typename CTOs::list_type...>; |
| 142 | + detail::ParallelFor_helper1<MT>(box, std::forward<F>(f), |
| 143 | + meta::cartesian_product_n(OptionsListList{}), |
| 144 | + runtime_options); |
| 145 | +#else |
| 146 | + amrex::ignore_unused(box, f, runtime_options); |
| 147 | + static_assert(std::is_integral<F>::value, "This requires C++17"); |
| 148 | +#endif |
| 149 | +} |
| 150 | + |
| 151 | +template <int MT, typename T, class F, typename... CTOs> |
| 152 | +std::enable_if_t<std::is_integral<T>::value> |
| 153 | +ParallelFor (TypeList<CTOs...> /*list_of_compile_time_options*/, |
| 154 | + std::array<int,sizeof...(CTOs)> const& runtime_options, |
| 155 | + Box const& box, T ncomp, F&& f) |
| 156 | +{ |
| 157 | +#if (__cplusplus >= 201703L) |
| 158 | + using OptionsListList = TypeList<typename CTOs::list_type...>; |
| 159 | + detail::ParallelFor_helper1<MT>(box, ncomp, std::forward<F>(f), |
| 160 | + meta::cartesian_product_n(OptionsListList{}), |
| 161 | + runtime_options); |
| 162 | +#else |
| 163 | + amrex::ignore_unused(box, ncomp, f, runtime_options); |
| 164 | + static_assert(std::is_integral<F>::value, "This requires C++17"); |
| 165 | +#endif |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * \brief ParallelFor with compile time optimization of kernels with run time options. |
| 170 | + * |
| 171 | + * It uses fold expression to generate kernel launches for all combinations |
| 172 | + * of the run time options. The kernel function can use constexpr if to |
| 173 | + * discard unused code blocks for better run time performance. In the |
| 174 | + * example below, the code will be expanded into 4*2=8 normal ParallelFors |
| 175 | + * for all combinations of the run time parameters. |
| 176 | + \verbatim |
| 177 | + int A_runtime_option = ...; |
| 178 | + int B_runtime_option = ...; |
| 179 | + enum A_options : int { A0, A1, A2, A3}; |
| 180 | + enum B_options : int { B0, B1 }; |
| 181 | + ParallelFor(TypeList<CompileTimeOptions<A0,A1,A2,A3>, |
| 182 | + CompileTimeOptions<B0,B1>>{}, |
| 183 | + {A_runtime_option, B_runtime_option}, |
| 184 | + N, [=] AMREX_GPU_DEVICE (int i, auto A_control, auto B_control) |
| 185 | + { |
| 186 | + ... |
| 187 | + if constexpr (A_control.value == A0) { |
| 188 | + ... |
| 189 | + } else if constexpr (A_control.value == A1) { |
| 190 | + ... |
| 191 | + } else if constexpr (A_control.value == A2) { |
| 192 | + ... |
| 193 | + else { |
| 194 | + ... |
| 195 | + } |
| 196 | + if constexpr (A_control.value != A3 && B_control.value == B1) { |
| 197 | + ... |
| 198 | + } |
| 199 | + ... |
| 200 | + }); |
| 201 | + \endverbatim |
| 202 | + * Note that due to a limitation of CUDA's extended device lambda, the |
| 203 | + * constexpr if block cannot be the one that captures a variable first. |
| 204 | + * If nvcc complains about it, you will have to manually capture it outside |
| 205 | + * constexpr if. The data type for the parameters is int. |
| 206 | + * |
| 207 | + * \param ctos list of all possible values of the parameters. |
| 208 | + * \param option the run time parameters. |
| 209 | + * \param N an interger specifying the 1D for loop's range. |
| 210 | + * \param f a callable object taking an integer and working on that iteration. |
| 211 | + */ |
| 212 | +template <typename T, class F, typename... CTOs> |
| 213 | +std::enable_if_t<std::is_integral<T>::value> |
| 214 | +ParallelFor (TypeList<CTOs...> ctos, |
| 215 | + std::array<int,sizeof...(CTOs)> const& option, |
| 216 | + T N, F&& f) |
| 217 | +{ |
| 218 | + ParallelFor<AMREX_GPU_MAX_THREADS>(ctos, option, N, std::forward<F>(f)); |
| 219 | +} |
| 220 | + |
| 221 | +/** |
| 222 | + * \brief ParallelFor with compile time optimization of kernels with run time options. |
| 223 | + * |
| 224 | + * It uses fold expression to generate kernel launches for all combinations |
| 225 | + * of the run time options. The kernel function can use constexpr if to |
| 226 | + * discard unused code blocks for better run time performance. In the |
| 227 | + * example below, the code will be expanded into 4*2=8 normal ParallelFors |
| 228 | + * for all combinations of the run time parameters. |
| 229 | + \verbatim |
| 230 | + int A_runtime_option = ...; |
| 231 | + int B_runtime_option = ...; |
| 232 | + enum A_options : int { A0, A1, A2, A3}; |
| 233 | + enum B_options : int { B0, B1 }; |
| 234 | + ParallelFor(TypeList<CompileTimeOptions<A0,A1,A2,A3>, |
| 235 | + CompileTimeOptions<B0,B1>>{}, |
| 236 | + {A_runtime_option, B_runtime_option}, |
| 237 | + box, [=] AMREX_GPU_DEVICE (int i, int j, int k, |
| 238 | + auto A_control, auto B_control) |
| 239 | + { |
| 240 | + ... |
| 241 | + if constexpr (A_control.value == A0) { |
| 242 | + ... |
| 243 | + } else if constexpr (A_control.value == A1) { |
| 244 | + ... |
| 245 | + } else if constexpr (A_control.value == A2) { |
| 246 | + ... |
| 247 | + else { |
| 248 | + ... |
| 249 | + } |
| 250 | + if constexpr (A_control.value != A3 && B_control.value == B1) { |
| 251 | + ... |
| 252 | + } |
| 253 | + ... |
| 254 | + }); |
| 255 | + \endverbatim |
| 256 | + * Note that due to a limitation of CUDA's extended device lambda, the |
| 257 | + * constexpr if block cannot be the one that captures a variable first. |
| 258 | + * If nvcc complains about it, you will have to manually capture it outside |
| 259 | + * constexpr if. The data type for the parameters is int. |
| 260 | + * |
| 261 | + * \param ctos list of all possible values of the parameters. |
| 262 | + * \param option the run time parameters. |
| 263 | + * \param box a Box specifying the 3D for loop's range. |
| 264 | + * \param f a callable object taking three integers and working on the given cell. |
| 265 | + */ |
| 266 | +template <class F, typename... CTOs> |
| 267 | +void ParallelFor (TypeList<CTOs...> ctos, |
| 268 | + std::array<int,sizeof...(CTOs)> const& option, |
| 269 | + Box const& box, F&& f) |
| 270 | +{ |
| 271 | + ParallelFor<AMREX_GPU_MAX_THREADS>(ctos, option, box, std::forward<F>(f)); |
| 272 | +} |
| 273 | + |
| 274 | +/** |
| 275 | + * \brief ParallelFor with compile time optimization of kernels with run time options. |
| 276 | + * |
| 277 | + * It uses fold expression to generate kernel launches for all combinations |
| 278 | + * of the run time options. The kernel function can use constexpr if to |
| 279 | + * discard unused code blocks for better run time performance. In the |
| 280 | + * example below, the code will be expanded into 4*2=8 normal ParallelFors |
| 281 | + * for all combinations of the run time parameters. |
| 282 | + \verbatim |
| 283 | + int A_runtime_option = ...; |
| 284 | + int B_runtime_option = ...; |
| 285 | + enum A_options : int { A0, A1, A2, A3}; |
| 286 | + enum B_options : int { B0, B1 }; |
| 287 | + ParallelFor(TypeList<CompileTimeOptions<A0,A1,A2,A3>, |
| 288 | + CompileTimeOptions<B0,B1>>{}, |
| 289 | + {A_runtime_option, B_runtime_option}, |
| 290 | + box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n, |
| 291 | + auto A_control, auto B_control) |
| 292 | + { |
| 293 | + ... |
| 294 | + if constexpr (A_control.value == A0) { |
| 295 | + ... |
| 296 | + } else if constexpr (A_control.value == A1) { |
| 297 | + ... |
| 298 | + } else if constexpr (A_control.value == A2) { |
| 299 | + ... |
| 300 | + else { |
| 301 | + ... |
| 302 | + } |
| 303 | + if constexpr (A_control.value != A3 && B_control.value == B1) { |
| 304 | + ... |
| 305 | + } |
| 306 | + ... |
| 307 | + }); |
| 308 | + \endverbatim |
| 309 | + * Note that due to a limitation of CUDA's extended device lambda, the |
| 310 | + * constexpr if block cannot be the one that captures a variable first. |
| 311 | + * If nvcc complains about it, you will have to manually capture it outside |
| 312 | + * constexpr if. The data type for the parameters is int. |
| 313 | + * |
| 314 | + * \param ctos list of all possible values of the parameters. |
| 315 | + * \param option the run time parameters. |
| 316 | + * \param box a Box specifying the iteration in 3D space. |
| 317 | + * \param ncomp an integer specifying the range for iteration over components. |
| 318 | + * \param f a callable object taking three integers and working on the given cell. |
| 319 | + */ |
| 320 | +template <typename T, class F, typename... CTOs> |
| 321 | +std::enable_if_t<std::is_integral<T>::value> |
| 322 | +ParallelFor (TypeList<CTOs...> ctos, |
| 323 | + std::array<int,sizeof...(CTOs)> const& option, |
| 324 | + Box const& box, T ncomp, F&& f) |
| 325 | +{ |
| 326 | + ParallelFor<AMREX_GPU_MAX_THREADS>(ctos, option, box, ncomp, std::forward<F>(f)); |
| 327 | +} |
| 328 | + |
| 329 | +} |
| 330 | + |
| 331 | +#endif |
0 commit comments