DDC 0.10.0
Loading...
Searching...
No Matches
parallel_transform.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <utility>
8
9#include <Kokkos_Macros.hpp>
10
11#include "chunk_span.hpp"
12#include "chunk_traits.hpp"
13#include "parallel_for_each.hpp"
14
15namespace ddc {
16
17namespace detail {
18
19template <
20 class ElementType,
21 class SupportType,
22 class LayoutStridedPolicy,
23 class MemorySpace,
24 class Functor>
25class TransformKokkosLambdaAdapter
26{
27 ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> m_chunk;
28
29 Functor m_functor;
30
31public:
32 explicit TransformKokkosLambdaAdapter(
33 ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> const& chunk,
34 Functor const& functor)
35 : m_chunk(chunk)
36 , m_functor(functor)
37 {
38 }
39
40 KOKKOS_FUNCTION void operator()(
41 typename SupportType::discrete_element_type const i) const noexcept
42 {
43 ElementType& value = m_chunk(i);
44 value = m_functor(static_cast<ElementType const&>(value));
45 }
46};
47
48} // namespace detail
49
50/** Transform a borrowed chunk with a given transform functor
51 * @param[out] dst the borrowed chunk in which to copy
52 * @param[in] transform a unary FunctionObject that will be applied to each element of the input
53 * range. The return type must be assignable to dst
54 * @return dst as a ChunkSpan
55 */
56template <class ChunkDst, class UnaryTransformOp>
57auto parallel_transform(ChunkDst&& dst, UnaryTransformOp&& transform)
58{
59 static_assert(is_borrowed_chunk_v<ChunkDst>);
60 parallel_for_each(
61 "ddc_parallel_transform_default",
62 dst.domain(),
63 detail::TransformKokkosLambdaAdapter(
64 dst.span_view(),
65 std::forward<UnaryTransformOp>(transform)));
66 return dst.span_view();
67}
68
69/** Transform a borrowed chunk with a given transform functor
70 * @param[in] execution_space a Kokkos execution space where the loop will be executed on
71 * @param[out] dst the borrowed chunk in which to copy
72 * @param[in] transform a unary FunctionObject that will be applied to each element of the input
73 * range. The return type must be acceptable as input to reduce
74 * @return dst as a ChunkSpan
75 */
76template <class ExecSpace, class ChunkDst, class UnaryTransformOp>
78 ExecSpace const& execution_space,
79 ChunkDst&& dst,
80 UnaryTransformOp&& transform)
81{
82 static_assert(is_borrowed_chunk_v<ChunkDst>);
83 parallel_for_each(
84 "ddc_parallel_transform_default",
85 execution_space,
86 dst.domain(),
87 detail::TransformKokkosLambdaAdapter(
88 dst.span_view(),
89 std::forward<UnaryTransformOp>(transform)));
90 return dst.span_view();
91}
92
93} // namespace ddc
friend class ChunkSpan
The top-level namespace of DDC.
auto parallel_transform(ExecSpace const &execution_space, ChunkDst &&dst, UnaryTransformOp &&transform)
Transform a borrowed chunk with a given transform functor.
auto parallel_transform(ChunkDst &&dst, UnaryTransformOp &&transform)
Transform a borrowed chunk with a given transform functor.