16#ifdef MAIA_NVHPC_COMPILER
18#include <thrust/iterator/counting_iterator.h>
25#if !defined(CHUNK_SIZE)
26#define CHUNK_SIZE (4096)
31#if defined(MAIA_PSTL) && !defined(MAIA_NVHPC_COMPILER)
98template <
class UnaryFunction>
101#pragma omp parallel for schedule(static, CHUNK_SIZE) default(none) shared(begin, end, f)
103 for(
MInt i = begin; i < end; i++) {
113template <
class UnaryFunction>
115#if defined(MAIA_PSTL)
116#if defined(MAIA_NVHPC_COMPILER)
120 auto begin_ = thrust::counting_iterator(
MInt{begin});
128 std::for_each_n(std::execution::par_unseq, begin_, end_, f);
146template <MBool portedToGpu = false,
class UnaryFunction>
148 if constexpr(portedToGpu) {
160template <
class UnaryFunction,
class T>
162 const MInt end = container.size();
164#pragma omp parallel for schedule(static, CHUNK_SIZE) default(none) shared(end, f, container)
166 for(
MInt i = 0; i < end; i++) {
176template <
class UnaryFunction,
class T>
178#if defined(MAIA_PSTL)
179 std::for_each_n(std::execution::par_unseq, container.begin(), container.end(), f);
196template <MBool portedToGpu = false,
class UnaryFunction,
class T>
197inline void parallelFor(
const std::vector<T>& container, UnaryFunction&& f) {
198 if constexpr(portedToGpu) {
211template <MInt nDim,
class UnaryFunction>
212inline void parallelFor_base(std::array<MInt, nDim> begin, std::array<MInt, nDim> end, UnaryFunction&& f) {
213 if constexpr(nDim == 3) {
215#pragma omp parallel for schedule(static, CHUNK_SIZE) default(none) shared(begin, end, f) collapse(3)
217 for(
MInt k = begin[2]; k < end[2]; k++) {
218 for(
MInt j = begin[1]; j < end[1]; j++) {
219 for(
MInt i = begin[0]; i < end[0]; i++) {
224 }
else if constexpr(nDim == 2) {
226#pragma omp parallel for schedule(static, CHUNK_SIZE) default(none) shared(begin, end, f) collapse(2)
228 for(
MInt j = begin[1]; j < end[1]; j++) {
229 for(
MInt i = begin[0]; i < end[0]; i++) {
234 mTerm(1, AT_,
"Only nDim==2 and nDim==3 supported");
243template <MInt nDim,
class UnaryFunction>
244inline void parallelFor_pstl(std::array<MInt, nDim> begin, std::array<MInt, nDim> end, UnaryFunction&& f) {
245#if defined(MAIA_PSTL)
246 std::array<MInt, nDim> size{};
247 const MInt beginI = 0;
249 for(
MInt dim = 0; dim < nDim; ++dim) {
250 size[dim] = end[dim] - begin[dim];
253#if defined(MAIA_NVHPC_COMPILER)
257 auto begin_ = thrust::counting_iterator(
MInt{beginI});
264 if constexpr(nDim == 3) {
265 std::for_each_n(std::execution::par_unseq, begin_, end_, [=](
auto& I) {
266 const MInt k = (I / (size[0] * size[1])) + begin[2];
267 const MInt j = ((I - k * size[0] * size[1]) / size[0]) + begin[1];
268 const MInt i = (I % size[0]) + begin[0];
272 }
else if constexpr(nDim == 2) {
273 std::for_each_n(std::execution::par_unseq, begin_, end_, [=](
auto& I) {
274 const MInt j = (I / size[0]) + begin[1];
275 const MInt i = (I % size[0]) + begin[0];
281 parallelFor_base<nDim>(begin, end, f);
305template <MBool portedToGpu = false, MInt nDim,
class UnaryFunction>
306inline void parallelFor(std::array<MInt, nDim> begin, std::array<MInt, nDim> end, UnaryFunction&& f) {
307 if constexpr(portedToGpu) {
308 parallelFor_pstl<nDim>(begin, end, f);
310 parallelFor_base<nDim>(begin, end, f);
void mTerm(const MInt errorCode, const MString &location, const MString &message)
Namespace for auxiliary functions/classes.
void parallelFor_base(MInt begin, MInt end, UnaryFunction &&f)
Wrapper function for parallel for loop (no PSTL)
void parallelFor_pstl(MInt begin, MInt end, UnaryFunction &&f)
Wrapper function for parallel for loop (PSTL)
void parallelFor(MInt begin, MInt end, UnaryFunction &&f)
Wrapper function for parallel for loop.
std::ptrdiff_t difference_type
RangeIterator operator++(MInt)
RangeIterator(value_type value)
RangeIterator & operator--()
friend MBool operator!=(const RangeIterator &a, const RangeIterator &b)
reference operator*() const
RangeIterator operator--(MInt)
RangeIterator & operator++()
friend MBool operator==(const RangeIterator &a, const RangeIterator &b)
friend MBool operator>(const RangeIterator &a, const RangeIterator &b)
std::forward_iterator_tag iterator_category