Distributed Ranges
Loading...
Searching...
No Matches
for_each.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include <algorithm>
8#include <execution>
9#include <type_traits>
10#include <utility>
11
12#include <dr/concepts/concepts.hpp>
13#include <dr/detail/logger.hpp>
14#include <dr/detail/onedpl_direct_iterator.hpp>
15#include <dr/detail/ranges_shim.hpp>
16#include <dr/detail/sycl_utils.hpp>
17#include <dr/mp/global.hpp>
18
19namespace dr::mp {
20
22void for_each(dr::distributed_range auto &&dr, auto op) {
23 dr::drlog.debug(dr::logger::for_each, "for_each: parallel execution\n");
24 if (rng::empty(dr)) {
25 return;
26 }
27 assert(aligned(dr));
28
29 for (const auto &s : local_segments(dr)) {
30 if (mp::use_sycl()) {
31 dr::drlog.debug(" using sycl\n");
32
33 assert(rng::distance(s) > 0);
34#ifdef SYCL_LANGUAGE_VERSION
35 dr::__detail::parallel_for(
36 dr::mp::sycl_queue(), sycl::range<1>(rng::distance(s)),
37 [first = rng::begin(s), op](auto idx) { op(first[idx]); })
38 .wait();
39#else
40 assert(false);
41#endif
42 } else {
43 dr::drlog.debug(" using cpu\n");
44 rng::for_each(s, op);
45 }
46 }
47 barrier();
48}
49
51template <dr::distributed_iterator DI>
52void for_each(DI first, DI last, auto op) {
53 mp::for_each(rng::subrange(first, last), op);
54}
55
57template <dr::distributed_iterator DI, std::integral I>
58DI for_each_n(DI first, I n, auto op) {
59 auto last = first;
60 rng::advance(last, n);
61 mp::for_each(first, last, op);
62 return last;
63}
64
65} // namespace dr::mp
Definition: concepts.hpp:20